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