cb96ef174bbf39399eaba3695afe84d324e2a4e9
[mech_eap.orig] / wrap_iov.c
1 /*
2  * Copyright (c) 2010, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 /*
33  * Copyright 2008 by the Massachusetts Institute of Technology.
34  * All Rights Reserved.
35  *
36  * Export of this software from the United States of America may
37  *   require a specific license from the United States Government.
38  *   It is the responsibility of any person or organization contemplating
39  *   export to obtain such a license before exporting.
40  *
41  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
42  * distribute this software and its documentation for any purpose and
43  * without fee is hereby granted, provided that the above copyright
44  * notice appear in all copies and that both that copyright notice and
45  * this permission notice appear in supporting documentation, and that
46  * the name of M.I.T. not be used in advertising or publicity pertaining
47  * to distribution of the software without specific, written prior
48  * permission.  Furthermore if you modify this software you must label
49  * your software as modified software and not distribute it in such a
50  * fashion that it might be confused with the original M.I.T. software.
51  * M.I.T. makes no representations about the suitability of
52  * this software for any purpose.  It is provided "as is" without express
53  * or implied warranty.
54  */
55
56 #include "gssapiP_eap.h"
57
58 OM_uint32
59 gssEapWrapOrGetMIC(OM_uint32 *minor,
60                    gss_ctx_id_t ctx,
61                    int conf_req_flag,
62                    int *conf_state,
63                    gss_iov_buffer_desc *iov,
64                    int iov_count,
65                    enum gss_eap_token_type toktype)
66 {
67     krb5_error_code code = 0;
68     gss_iov_buffer_t header;
69     gss_iov_buffer_t padding;
70     gss_iov_buffer_t trailer;
71     unsigned char acceptorFlag;
72     unsigned char *outbuf = NULL;
73     unsigned char *tbuf = NULL;
74     int keyUsage;
75     size_t rrc = 0;
76     unsigned int gssHeaderLen, gssTrailerLen;
77     size_t dataLen, assocDataLen;
78     krb5_context krbContext;
79
80     if (!CTX_IS_ESTABLISHED(ctx))
81         return GSS_S_NO_CONTEXT;
82
83     if (ctx->encryptionType == ENCTYPE_NULL)
84         return GSS_S_UNAVAILABLE;
85
86     GSSEAP_KRB_INIT(&krbContext);
87
88     acceptorFlag = CTX_IS_INITIATOR(ctx) ? 0 : TOK_FLAG_SENDER_IS_ACCEPTOR;
89     keyUsage = ((toktype == TOK_TYPE_WRAP)
90                 ? (CTX_IS_INITIATOR(ctx)
91                    ? KEY_USAGE_INITIATOR_SEAL
92                    : KEY_USAGE_ACCEPTOR_SEAL)
93                 : (CTX_IS_INITIATOR(ctx)
94                    ? KEY_USAGE_INITIATOR_SIGN
95                    : KEY_USAGE_ACCEPTOR_SIGN));
96
97     gssEapIovMessageLength(iov, iov_count, &dataLen, &assocDataLen);
98
99     header = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_HEADER);
100     if (header == NULL) {
101         *minor = EINVAL;
102         return GSS_S_FAILURE;
103     }
104
105     padding = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_PADDING);
106     if (padding != NULL)
107         padding->buffer.length = 0;
108
109     trailer = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_TRAILER);
110
111     if (toktype == TOK_TYPE_WRAP && conf_req_flag) {
112         unsigned int krbHeaderLen, krbTrailerLen, krbPadLen;
113         size_t ec = 0;
114         size_t confDataLen = dataLen - assocDataLen;
115
116         code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
117                                     KRB5_CRYPTO_TYPE_HEADER, &krbHeaderLen);
118         if (code != 0)
119             goto cleanup;
120
121         code = krb5_c_padding_length(krbContext, ctx->encryptionType,
122                                      confDataLen + 16 /* E(Header) */,
123                                      &krbPadLen);
124         if (code != 0)
125             goto cleanup;
126
127         if (krbPadLen == 0 && (ctx->gssFlags & GSS_C_DCE_STYLE)) {
128             /* Windows rejects AEAD tokens with non-zero EC */
129             code = krb5_c_block_size(krbContext, ctx->encryptionType, &ec);
130             if (code != 0)
131                 goto cleanup;
132         } else
133             ec = krbPadLen;
134
135         code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
136                                     KRB5_CRYPTO_TYPE_TRAILER, &krbTrailerLen);
137         if (code != 0)
138             goto cleanup;
139
140         gssHeaderLen = 16 /* Header */ + krbHeaderLen;
141         gssTrailerLen = ec + 16 /* E(Header) */ + krbTrailerLen;
142
143         if (trailer == NULL) {
144             rrc = gssTrailerLen;
145             /* Workaround for Windows bug where it rotates by EC + RRC */
146             if (ctx->gssFlags & GSS_C_DCE_STYLE)
147                 rrc -= ec;
148             gssHeaderLen += gssTrailerLen;
149         }
150
151         if (header->type & GSS_IOV_BUFFER_FLAG_ALLOCATE) {
152             code = gssEapAllocIov(header, (size_t)gssHeaderLen);
153         } else if (header->buffer.length < gssHeaderLen)
154             code = KRB5_BAD_MSIZE;
155         if (code != 0)
156             goto cleanup;
157         outbuf = (unsigned char *)header->buffer.value;
158         header->buffer.length = (size_t)gssHeaderLen;
159
160         if (trailer != NULL) {
161             if (trailer->type & GSS_IOV_BUFFER_FLAG_ALLOCATE)
162                 code = gssEapAllocIov(trailer, (size_t)gssTrailerLen);
163             else if (trailer->buffer.length < gssTrailerLen)
164                 code = KRB5_BAD_MSIZE;
165             if (code != 0)
166                 goto cleanup;
167             trailer->buffer.length = (size_t)gssTrailerLen;
168         }
169
170         /* TOK_ID */
171         store_uint16_be((uint16_t)toktype, outbuf);
172         /* flags */
173         outbuf[2] = (acceptorFlag
174                      | (conf_req_flag ? TOK_FLAG_WRAP_CONFIDENTIAL : 0)
175                      | (0 ? TOK_FLAG_ACCEPTOR_SUBKEY : 0));
176         /* filler */
177         outbuf[3] = 0xFF;
178         /* EC */
179         store_uint16_be(ec, outbuf + 4);
180         /* RRC */
181         store_uint16_be(0, outbuf + 6);
182         store_64_be(ctx->sendSeq, outbuf + 8);
183
184         /*
185          * EC | copy of header to be encrypted, located in
186          * (possibly rotated) trailer
187          */
188         if (trailer == NULL)
189             tbuf = (unsigned char *)header->buffer.value + 16; /* Header */
190         else
191             tbuf = (unsigned char *)trailer->buffer.value;
192
193         memset(tbuf, 0xFF, ec);
194         memcpy(tbuf + ec, header->buffer.value, 16);
195
196         code = gssEapEncrypt(krbContext,
197                              ((ctx->gssFlags & GSS_C_DCE_STYLE) != 0),
198                              ec, rrc, &ctx->rfc3961Key,
199                              keyUsage, 0, iov, iov_count);
200         if (code != 0)
201             goto cleanup;
202
203         /* RRC */
204         store_uint16_be(rrc, outbuf + 6);
205
206         ctx->sendSeq++;
207     } else if (toktype == TOK_TYPE_WRAP && !conf_req_flag) {
208     wrap_with_checksum:
209
210         gssHeaderLen = 16;
211
212         code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
213                                     KRB5_CRYPTO_TYPE_CHECKSUM,
214                                     &gssTrailerLen);
215         if (code != 0)
216             goto cleanup;
217
218         assert(gssTrailerLen <= 0xFFFF);
219
220         if (trailer == NULL) {
221             rrc = gssTrailerLen;
222             gssHeaderLen += gssTrailerLen;
223         }
224
225         if (header->type & GSS_IOV_BUFFER_FLAG_ALLOCATE)
226             code = gssEapAllocIov(header, (size_t)gssHeaderLen);
227         else if (header->buffer.length < gssHeaderLen)
228             code = KRB5_BAD_MSIZE;
229         if (code != 0)
230             goto cleanup;
231         outbuf = (unsigned char *)header->buffer.value;
232         header->buffer.length = (size_t)gssHeaderLen;
233
234         if (trailer != NULL) {
235             if (trailer->type & GSS_IOV_BUFFER_FLAG_ALLOCATE)
236                 code = gssEapAllocIov(trailer, (size_t)gssTrailerLen);
237             else if (trailer->buffer.length < gssTrailerLen)
238                 code = KRB5_BAD_MSIZE;
239             if (code != 0)
240                 goto cleanup;
241             trailer->buffer.length = (size_t)gssTrailerLen;
242         }
243
244         /* TOK_ID */
245         store_uint16_be((uint16_t)toktype, outbuf);
246         /* flags */
247         outbuf[2] = (acceptorFlag
248                      | (0 ? TOK_FLAG_ACCEPTOR_SUBKEY : 0));
249         /* filler */
250         outbuf[3] = 0xFF;
251         if (toktype == TOK_TYPE_WRAP) {
252             /* Use 0 for checksum calculation, substitute
253              * checksum length later.
254              */
255             /* EC */
256             store_uint16_be(0, outbuf + 4);
257             /* RRC */
258             store_uint16_be(0, outbuf + 6);
259         } else {
260             /* MIC and DEL store 0xFF in EC and RRC */
261             store_uint16_be(0xFFFF, outbuf + 4);
262             store_uint16_be(0xFFFF, outbuf + 6);
263         }
264         store_64_be(ctx->sendSeq, outbuf + 8);
265
266         code = gssEapSign(krbContext, 0, /* 0 == pick from crypto */
267                           rrc, &ctx->rfc3961Key, keyUsage,
268                           iov, iov_count);
269         if (code != 0)
270             goto cleanup;
271
272         ctx->sendSeq++;
273
274         if (toktype == TOK_TYPE_WRAP) {
275             /* Fix up EC field */
276             store_uint16_be(gssTrailerLen, outbuf + 4);
277             /* Fix up RRC field */
278             store_uint16_be(rrc, outbuf + 6);
279         }
280     } else if (toktype == TOK_TYPE_MIC) {
281         trailer = NULL;
282         goto wrap_with_checksum;
283     } else if (toktype == TOK_TYPE_DELETE_CONTEXT) {
284         trailer = NULL;
285         goto wrap_with_checksum;
286     } else {
287         abort();
288     }
289
290     code = 0;
291
292 cleanup:
293     if (code != 0)
294         gssEapReleaseIov(iov, iov_count);
295
296     *minor = code;
297
298     if (code == 0)
299         return GSS_S_FAILURE;
300     else
301         return GSS_S_COMPLETE;
302 }
303
304 OM_uint32
305 gss_wrap_iov(OM_uint32 *minor,
306              gss_ctx_id_t ctx,
307              int conf_req_flag,
308              gss_qop_t qop_req,
309              int *conf_state,
310              gss_iov_buffer_desc *iov,
311              int iov_count)
312 {
313     return gssEapWrapOrGetMIC(minor, ctx, conf_req_flag, conf_state,
314                              iov, iov_count, TOK_TYPE_WRAP);
315 }