10a325446f6ffbd04c682edca1da6d33c0858b4c
[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 unsigned char
59 rfc4121Flags(gss_ctx_id_t ctx, int receiving)
60 {
61     unsigned char flags;
62     int isAcceptor;
63
64     isAcceptor = !CTX_IS_INITIATOR(ctx);
65     if (receiving)
66         isAcceptor = !isAcceptor;
67
68     flags = 0;
69     if (isAcceptor)
70         flags |= TOK_FLAG_SENDER_IS_ACCEPTOR;
71
72     if ((ctx->flags & CTX_FLAG_KRB_REAUTH_GSS) &&
73         (ctx->gssFlags & GSS_C_MUTUAL_FLAG))
74         flags |= TOK_FLAG_ACCEPTOR_SUBKEY;
75
76     return flags;
77 }
78
79 OM_uint32
80 gssEapWrapOrGetMIC(OM_uint32 *minor,
81                    gss_ctx_id_t ctx,
82                    int conf_req_flag,
83                    int *conf_state,
84                    gss_iov_buffer_desc *iov,
85                    int iov_count,
86                    enum gss_eap_token_type toktype)
87 {
88     krb5_error_code code = 0;
89     gss_iov_buffer_t header;
90     gss_iov_buffer_t padding;
91     gss_iov_buffer_t trailer;
92     unsigned char flags;
93     unsigned char *outbuf = NULL;
94     unsigned char *tbuf = NULL;
95     int keyUsage;
96     size_t rrc = 0;
97     unsigned int gssHeaderLen, gssTrailerLen;
98     size_t dataLen, assocDataLen;
99     krb5_context krbContext;
100
101     if (ctx->encryptionType == ENCTYPE_NULL)
102         return GSS_S_UNAVAILABLE;
103
104     GSSEAP_KRB_INIT(&krbContext);
105
106     flags = rfc4121Flags(ctx, FALSE);
107
108     if (toktype == TOK_TYPE_WRAP) {
109         keyUsage = CTX_IS_INITIATOR(ctx)
110                    ? KEY_USAGE_INITIATOR_SEAL
111                    : KEY_USAGE_ACCEPTOR_SEAL;
112     } else {
113         keyUsage = CTX_IS_INITIATOR(ctx)
114                    ? KEY_USAGE_INITIATOR_SIGN
115                    : KEY_USAGE_ACCEPTOR_SIGN;
116     }
117
118     gssEapIovMessageLength(iov, iov_count, &dataLen, &assocDataLen);
119
120     header = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_HEADER);
121     if (header == NULL) {
122         *minor = EINVAL;
123         return GSS_S_FAILURE;
124     }
125
126     padding = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_PADDING);
127     if (padding != NULL)
128         padding->buffer.length = 0;
129
130     trailer = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_TRAILER);
131
132     if (toktype == TOK_TYPE_WRAP && conf_req_flag) {
133         unsigned int krbHeaderLen, krbTrailerLen, krbPadLen;
134         size_t ec = 0;
135         size_t confDataLen = dataLen - assocDataLen;
136
137         code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
138                                     KRB5_CRYPTO_TYPE_HEADER, &krbHeaderLen);
139         if (code != 0)
140             goto cleanup;
141
142         code = krb5_c_padding_length(krbContext, ctx->encryptionType,
143                                      confDataLen + 16 /* E(Header) */,
144                                      &krbPadLen);
145         if (code != 0)
146             goto cleanup;
147
148         if (krbPadLen == 0 && (ctx->gssFlags & GSS_C_DCE_STYLE)) {
149             /* Windows rejects AEAD tokens with non-zero EC */
150             code = krb5_c_block_size(krbContext, ctx->encryptionType, &ec);
151             if (code != 0)
152                 goto cleanup;
153         } else
154             ec = krbPadLen;
155
156         code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
157                                     KRB5_CRYPTO_TYPE_TRAILER, &krbTrailerLen);
158         if (code != 0)
159             goto cleanup;
160
161         gssHeaderLen = 16 /* Header */ + krbHeaderLen;
162         gssTrailerLen = ec + 16 /* E(Header) */ + krbTrailerLen;
163
164         if (trailer == NULL) {
165             rrc = gssTrailerLen;
166             /* Workaround for Windows bug where it rotates by EC + RRC */
167             if (ctx->gssFlags & GSS_C_DCE_STYLE)
168                 rrc -= ec;
169             gssHeaderLen += gssTrailerLen;
170         }
171
172         if (header->type & GSS_IOV_BUFFER_FLAG_ALLOCATE) {
173             code = gssEapAllocIov(header, (size_t)gssHeaderLen);
174         } else if (header->buffer.length < gssHeaderLen)
175             code = KRB5_BAD_MSIZE;
176         if (code != 0)
177             goto cleanup;
178         outbuf = (unsigned char *)header->buffer.value;
179         header->buffer.length = (size_t)gssHeaderLen;
180
181         if (trailer != NULL) {
182             if (trailer->type & GSS_IOV_BUFFER_FLAG_ALLOCATE)
183                 code = gssEapAllocIov(trailer, (size_t)gssTrailerLen);
184             else if (trailer->buffer.length < gssTrailerLen)
185                 code = KRB5_BAD_MSIZE;
186             if (code != 0)
187                 goto cleanup;
188             trailer->buffer.length = (size_t)gssTrailerLen;
189         }
190
191         /* TOK_ID */
192         store_uint16_be((uint16_t)toktype, outbuf);
193         /* flags */
194         outbuf[2] = flags
195                      | (conf_req_flag ? TOK_FLAG_WRAP_CONFIDENTIAL : 0);
196         /* filler */
197         outbuf[3] = 0xFF;
198         /* EC */
199         store_uint16_be(ec, outbuf + 4);
200         /* RRC */
201         store_uint16_be(0, outbuf + 6);
202         store_uint64_be(ctx->sendSeq, outbuf + 8);
203
204         /*
205          * EC | copy of header to be encrypted, located in
206          * (possibly rotated) trailer
207          */
208         if (trailer == NULL)
209             tbuf = (unsigned char *)header->buffer.value + 16; /* Header */
210         else
211             tbuf = (unsigned char *)trailer->buffer.value;
212
213         memset(tbuf, 0xFF, ec);
214         memcpy(tbuf + ec, header->buffer.value, 16);
215
216         code = gssEapEncrypt(krbContext,
217                              ((ctx->gssFlags & GSS_C_DCE_STYLE) != 0),
218                              ec, rrc, &ctx->rfc3961Key,
219                              keyUsage, 0, iov, iov_count);
220         if (code != 0)
221             goto cleanup;
222
223         /* RRC */
224         store_uint16_be(rrc, outbuf + 6);
225
226         ctx->sendSeq++;
227     } else if (toktype == TOK_TYPE_WRAP && !conf_req_flag) {
228     wrap_with_checksum:
229
230         gssHeaderLen = 16;
231
232         code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
233                                     KRB5_CRYPTO_TYPE_CHECKSUM,
234                                     &gssTrailerLen);
235         if (code != 0)
236             goto cleanup;
237
238         assert(gssTrailerLen <= 0xFFFF);
239
240         if (trailer == NULL) {
241             rrc = gssTrailerLen;
242             gssHeaderLen += gssTrailerLen;
243         }
244
245         if (header->type & GSS_IOV_BUFFER_FLAG_ALLOCATE)
246             code = gssEapAllocIov(header, (size_t)gssHeaderLen);
247         else if (header->buffer.length < gssHeaderLen)
248             code = KRB5_BAD_MSIZE;
249         if (code != 0)
250             goto cleanup;
251         outbuf = (unsigned char *)header->buffer.value;
252         header->buffer.length = (size_t)gssHeaderLen;
253
254         if (trailer != NULL) {
255             if (trailer->type & GSS_IOV_BUFFER_FLAG_ALLOCATE)
256                 code = gssEapAllocIov(trailer, (size_t)gssTrailerLen);
257             else if (trailer->buffer.length < gssTrailerLen)
258                 code = KRB5_BAD_MSIZE;
259             if (code != 0)
260                 goto cleanup;
261             trailer->buffer.length = (size_t)gssTrailerLen;
262         }
263
264         /* TOK_ID */
265         store_uint16_be((uint16_t)toktype, outbuf);
266         /* flags */
267         outbuf[2] = flags;
268         /* filler */
269         outbuf[3] = 0xFF;
270         if (toktype == TOK_TYPE_WRAP) {
271             /* Use 0 for checksum calculation, substitute
272              * checksum length later.
273              */
274             /* EC */
275             store_uint16_be(0, outbuf + 4);
276             /* RRC */
277             store_uint16_be(0, outbuf + 6);
278         } else {
279             /* MIC and DEL store 0xFF in EC and RRC */
280             store_uint16_be(0xFFFF, outbuf + 4);
281             store_uint16_be(0xFFFF, outbuf + 6);
282         }
283         store_uint64_be(ctx->sendSeq, outbuf + 8);
284
285         code = gssEapSign(krbContext, ctx->checksumType,
286                           rrc, &ctx->rfc3961Key, keyUsage,
287                           iov, iov_count);
288         if (code != 0)
289             goto cleanup;
290
291         ctx->sendSeq++;
292
293         if (toktype == TOK_TYPE_WRAP) {
294             /* Fix up EC field */
295             store_uint16_be(gssTrailerLen, outbuf + 4);
296             /* Fix up RRC field */
297             store_uint16_be(rrc, outbuf + 6);
298         }
299     } else if (toktype == TOK_TYPE_MIC) {
300         trailer = NULL;
301         goto wrap_with_checksum;
302     } else if (toktype == TOK_TYPE_DELETE_CONTEXT) {
303         trailer = NULL;
304         goto wrap_with_checksum;
305     } else {
306         abort();
307     }
308
309     code = 0;
310
311 cleanup:
312     if (code != 0)
313         gssEapReleaseIov(iov, iov_count);
314
315     *minor = code;
316
317     return (code == 0) ? GSS_S_COMPLETE : GSS_S_FAILURE;
318 }
319
320 OM_uint32
321 gss_wrap_iov(OM_uint32 *minor,
322              gss_ctx_id_t ctx,
323              int conf_req_flag,
324              gss_qop_t qop_req,
325              int *conf_state,
326              gss_iov_buffer_desc *iov,
327              int iov_count)
328 {
329     OM_uint32 major;
330
331     if (ctx == GSS_C_NO_CONTEXT) {
332         *minor = EINVAL;
333         return GSS_S_NO_CONTEXT;
334     }
335
336     *minor = 0;
337
338     GSSEAP_MUTEX_LOCK(&ctx->mutex);
339
340     if (!CTX_IS_ESTABLISHED(ctx)) {
341         major = GSS_S_NO_CONTEXT;
342         goto cleanup;
343     }
344
345     major = gssEapWrapOrGetMIC(minor, ctx, conf_req_flag, conf_state,
346                                iov, iov_count, TOK_TYPE_WRAP);
347     if (GSS_ERROR(major))
348         goto cleanup;
349
350 cleanup:
351     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
352
353     return major;
354 }