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