Implement gssEapDeriveRFC3961Key
[mech_eap.git] / 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     GSSEAP_KRB_INIT(&krbContext);
84
85     acceptorFlag = CTX_IS_INITIATOR(ctx) ? 0 : TOK_FLAG_SENDER_IS_ACCEPTOR;
86     keyUsage = ((toktype == TOK_TYPE_WRAP)
87                 ? (CTX_IS_INITIATOR(ctx)
88                    ? KEY_USAGE_INITIATOR_SEAL
89                    : KEY_USAGE_ACCEPTOR_SEAL)
90                 : (CTX_IS_INITIATOR(ctx)
91                    ? KEY_USAGE_INITIATOR_SIGN
92                    : KEY_USAGE_ACCEPTOR_SIGN));
93
94     gssEapIovMessageLength(iov, iov_count, &dataLen, &assocDataLen);
95
96     header = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_HEADER);
97     if (header == NULL) {
98         *minor = EINVAL;
99         return GSS_S_FAILURE;
100     }
101
102     padding = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_PADDING);
103     if (padding != NULL)
104         padding->buffer.length = 0;
105
106     trailer = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_TRAILER);
107
108     if (toktype == TOK_TYPE_WRAP && conf_req_flag) {
109         unsigned int krbHeaderLen, krbTrailerLen, krbPadLen;
110         size_t ec = 0;
111         size_t confDataLen = dataLen - assocDataLen;
112
113         code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
114                                     KRB5_CRYPTO_TYPE_HEADER, &krbHeaderLen);
115         if (code != 0)
116             goto cleanup;
117
118         code = krb5_c_padding_length(krbContext, ctx->encryptionType,
119                                      confDataLen + 16 /* E(Header) */,
120                                      &krbPadLen);
121         if (code != 0)
122             goto cleanup;
123
124         if (krbPadLen == 0 && (ctx->gssFlags & GSS_C_DCE_STYLE)) {
125             /* Windows rejects AEAD tokens with non-zero EC */
126             code = krb5_c_block_size(krbContext, ctx->encryptionType, &ec);
127             if (code != 0)
128                 goto cleanup;
129         } else
130             ec = krbPadLen;
131
132         code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
133                                     KRB5_CRYPTO_TYPE_TRAILER, &krbTrailerLen);
134         if (code != 0)
135             goto cleanup;
136
137         gssHeaderLen = 16 /* Header */ + krbHeaderLen;
138         gssTrailerLen = ec + 16 /* E(Header) */ + krbTrailerLen;
139
140         if (trailer == NULL) {
141             rrc = gssTrailerLen;
142             /* Workaround for Windows bug where it rotates by EC + RRC */
143             if (ctx->gssFlags & GSS_C_DCE_STYLE)
144                 rrc -= ec;
145             gssHeaderLen += gssTrailerLen;
146         }
147
148         if (header->type & GSS_IOV_BUFFER_FLAG_ALLOCATE) {
149             code = gssEapAllocIov(header, (size_t)gssHeaderLen);
150         } else if (header->buffer.length < gssHeaderLen)
151             code = KRB5_BAD_MSIZE;
152         if (code != 0)
153             goto cleanup;
154         outbuf = (unsigned char *)header->buffer.value;
155         header->buffer.length = (size_t)gssHeaderLen;
156
157         if (trailer != NULL) {
158             if (trailer->type & GSS_IOV_BUFFER_FLAG_ALLOCATE)
159                 code = gssEapAllocIov(trailer, (size_t)gssTrailerLen);
160             else if (trailer->buffer.length < gssTrailerLen)
161                 code = KRB5_BAD_MSIZE;
162             if (code != 0)
163                 goto cleanup;
164             trailer->buffer.length = (size_t)gssTrailerLen;
165         }
166
167         /* TOK_ID */
168         store_uint16_be((uint16_t)toktype, outbuf);
169         /* flags */
170         outbuf[2] = (acceptorFlag
171                      | (conf_req_flag ? TOK_FLAG_WRAP_CONFIDENTIAL : 0)
172                      | (0 ? TOK_FLAG_ACCEPTOR_SUBKEY : 0));
173         /* filler */
174         outbuf[3] = 0xFF;
175         /* EC */
176         store_uint16_be(ec, outbuf + 4);
177         /* RRC */
178         store_uint16_be(0, outbuf + 6);
179         store_64_be(ctx->sendSeq, outbuf + 8);
180
181         /*
182          * EC | copy of header to be encrypted, located in
183          * (possibly rotated) trailer
184          */
185         if (trailer == NULL)
186             tbuf = (unsigned char *)header->buffer.value + 16; /* Header */
187         else
188             tbuf = (unsigned char *)trailer->buffer.value;
189
190         memset(tbuf, 0xFF, ec);
191         memcpy(tbuf + ec, header->buffer.value, 16);
192
193         code = gssEapEncrypt(krbContext,
194                              ((ctx->gssFlags & GSS_C_DCE_STYLE) != 0),
195                              ec, rrc, &ctx->rfc3961Key,
196                              keyUsage, 0, iov, iov_count);
197         if (code != 0)
198             goto cleanup;
199
200         /* RRC */
201         store_uint16_be(rrc, outbuf + 6);
202
203         ctx->sendSeq++;
204     } else if (toktype == TOK_TYPE_WRAP && !conf_req_flag) {
205     wrap_with_checksum:
206
207         gssHeaderLen = 16;
208
209         code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
210                                     KRB5_CRYPTO_TYPE_CHECKSUM,
211                                     &gssTrailerLen);
212         if (code != 0)
213             goto cleanup;
214
215         assert(gssTrailerLen <= 0xFFFF);
216
217         if (trailer == NULL) {
218             rrc = gssTrailerLen;
219             gssHeaderLen += gssTrailerLen;
220         }
221
222         if (header->type & GSS_IOV_BUFFER_FLAG_ALLOCATE)
223             code = gssEapAllocIov(header, (size_t)gssHeaderLen);
224         else if (header->buffer.length < gssHeaderLen)
225             code = KRB5_BAD_MSIZE;
226         if (code != 0)
227             goto cleanup;
228         outbuf = (unsigned char *)header->buffer.value;
229         header->buffer.length = (size_t)gssHeaderLen;
230
231         if (trailer != NULL) {
232             if (trailer->type & GSS_IOV_BUFFER_FLAG_ALLOCATE)
233                 code = gssEapAllocIov(trailer, (size_t)gssTrailerLen);
234             else if (trailer->buffer.length < gssTrailerLen)
235                 code = KRB5_BAD_MSIZE;
236             if (code != 0)
237                 goto cleanup;
238             trailer->buffer.length = (size_t)gssTrailerLen;
239         }
240
241         /* TOK_ID */
242         store_uint16_be((uint16_t)toktype, outbuf);
243         /* flags */
244         outbuf[2] = (acceptorFlag
245                      | (0 ? TOK_FLAG_ACCEPTOR_SUBKEY : 0));
246         /* filler */
247         outbuf[3] = 0xFF;
248         if (toktype == TOK_TYPE_WRAP) {
249             /* Use 0 for checksum calculation, substitute
250              * checksum length later.
251              */
252             /* EC */
253             store_uint16_be(0, outbuf + 4);
254             /* RRC */
255             store_uint16_be(0, outbuf + 6);
256         } else {
257             /* MIC and DEL store 0xFF in EC and RRC */
258             store_uint16_be(0xFFFF, outbuf + 4);
259             store_uint16_be(0xFFFF, outbuf + 6);
260         }
261         store_64_be(ctx->sendSeq, outbuf + 8);
262
263         code = gssEapSign(krbContext, ctx->checksumType,
264                           rrc, &ctx->rfc3961Key, keyUsage,
265                           iov, iov_count);
266         if (code != 0)
267             goto cleanup;
268
269         ctx->sendSeq++;
270
271         if (toktype == TOK_TYPE_WRAP) {
272             /* Fix up EC field */
273             store_uint16_be(gssTrailerLen, outbuf + 4);
274             /* Fix up RRC field */
275             store_uint16_be(rrc, outbuf + 6);
276         }
277     } else if (toktype == TOK_TYPE_MIC) {
278         trailer = NULL;
279         goto wrap_with_checksum;
280     } else if (toktype == TOK_TYPE_DELETE) {
281         goto wrap_with_checksum;
282     } else {
283         abort();
284     }
285
286     code = 0;
287
288 cleanup:
289     if (code != 0)
290         gssEapReleaseIov(iov, iov_count);
291
292     *minor = code;
293
294     if (code == 0)
295         return GSS_S_FAILURE;
296     else
297         return GSS_S_COMPLETE;
298 }
299
300 OM_uint32
301 gss_wrap_iov(OM_uint32 *minor,
302              gss_ctx_id_t ctx,
303              int conf_req_flag,
304              gss_qop_t qop_req,
305              int *conf_state,
306              gss_iov_buffer_desc *iov,
307              int iov_count)
308 {
309     return gssEapWrapOrGetMIC(minor, ctx, conf_req_flag, conf_state,
310                              iov, iov_count, TOK_TYPE_WRAP);
311 }