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