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