c3372e97ae776d2614fc1a829c8d49cb24fe2d05
[mech_eap.orig] / wrap_iov_length.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: determine protected message size.
58  */
59
60 #include "gssapiP_eap.h"
61
62 #define INIT_IOV_DATA(_iov)     do { (_iov)->buffer.value = NULL;       \
63         (_iov)->buffer.length = 0; }                                    \
64     while (0)
65
66 OM_uint32
67 gssEapWrapIovLength(OM_uint32 *minor,
68                     gss_ctx_id_t ctx,
69                     int conf_req_flag,
70                     gss_qop_t qop_req,
71                     int *conf_state,
72                     gss_iov_buffer_desc *iov,
73                     int iov_count)
74 {
75     gss_iov_buffer_t header, trailer, padding;
76     size_t dataLength, assocDataLength;
77     size_t gssHeaderLen, gssPadLen, gssTrailerLen;
78     unsigned int krbHeaderLen = 0, krbTrailerLen = 0, krbPadLen = 0;
79     krb5_error_code code;
80     krb5_context krbContext;
81     int dce_style;
82     size_t ec;
83
84     if (qop_req != GSS_C_QOP_DEFAULT) {
85         *minor = GSSEAP_UNKNOWN_QOP;
86         return GSS_S_UNAVAILABLE;
87     }
88
89     if (ctx->encryptionType == ENCTYPE_NULL) {
90         *minor = GSSEAP_KEY_UNAVAILABLE;
91         return GSS_S_UNAVAILABLE;
92     }
93
94     GSSEAP_KRB_INIT(&krbContext);
95
96     header = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_HEADER);
97     if (header == NULL) {
98         *minor = GSSEAP_MISSING_IOV;
99         return GSS_S_FAILURE;
100     }
101     INIT_IOV_DATA(header);
102
103     trailer = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_TRAILER);
104     if (trailer != NULL) {
105         INIT_IOV_DATA(trailer);
106     }
107
108     dce_style = ((ctx->gssFlags & GSS_C_DCE_STYLE) != 0);
109
110     /* For CFX, EC is used instead of padding, and is placed in header or trailer */
111     padding = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_PADDING);
112     if (padding != NULL) {
113         INIT_IOV_DATA(padding);
114     }
115
116     gssEapIovMessageLength(iov, iov_count, &dataLength, &assocDataLength);
117
118     if (conf_req_flag && gssEapIsIntegrityOnly(iov, iov_count))
119         conf_req_flag = FALSE;
120
121     gssHeaderLen = gssPadLen = gssTrailerLen = 0;
122
123     code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
124                                 conf_req_flag ?
125                                 KRB5_CRYPTO_TYPE_TRAILER : KRB5_CRYPTO_TYPE_CHECKSUM,
126                                 &krbTrailerLen);
127     if (code != 0) {
128         *minor = code;
129         return GSS_S_FAILURE;
130     }
131
132     if (conf_req_flag) {
133         code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
134                                     KRB5_CRYPTO_TYPE_HEADER, &krbHeaderLen);
135         if (code != 0) {
136             *minor = code;
137             return GSS_S_FAILURE;
138         }
139     }
140
141     gssHeaderLen = 16; /* Header */
142     if (conf_req_flag) {
143         gssHeaderLen += krbHeaderLen; /* Kerb-Header */
144         gssTrailerLen = 16 /* E(Header) */ + krbTrailerLen; /* Kerb-Trailer */
145
146         code = krb5_c_padding_length(krbContext, ctx->encryptionType,
147                                      dataLength - assocDataLength + 16 /* E(Header) */,
148                                      &krbPadLen);
149         if (code != 0) {
150             *minor = code;
151             return GSS_S_FAILURE;
152         }
153
154         if (krbPadLen == 0 && 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                 *minor = code;
159                 return GSS_S_FAILURE;
160             }
161         } else
162             ec = krbPadLen;
163
164         gssTrailerLen += ec;
165     } else {
166         gssTrailerLen = krbTrailerLen; /* Kerb-Checksum */
167     }
168
169     dataLength += gssPadLen;
170
171     if (trailer == NULL)
172         gssHeaderLen += gssTrailerLen;
173     else
174         trailer->buffer.length = gssTrailerLen;
175
176     assert(gssPadLen == 0 || padding != NULL);
177
178     if (padding != NULL)
179         padding->buffer.length = gssPadLen;
180
181     header->buffer.length = gssHeaderLen;
182
183     if (conf_state != NULL)
184         *conf_state = conf_req_flag;
185
186     *minor = 0;
187     return GSS_S_COMPLETE;
188 }
189
190 OM_uint32
191 gss_wrap_iov_length(OM_uint32 *minor,
192                     gss_ctx_id_t ctx,
193                     int conf_req_flag,
194                     gss_qop_t qop_req,
195                     int *conf_state,
196                     gss_iov_buffer_desc *iov,
197                     int iov_count)
198 {
199     OM_uint32 major;
200
201     if (ctx == GSS_C_NO_CONTEXT) {
202         *minor = EINVAL;
203         return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CONTEXT;
204     }
205
206     *minor = 0;
207
208     GSSEAP_MUTEX_LOCK(&ctx->mutex);
209
210     if (!CTX_IS_ESTABLISHED(ctx)) {
211         major = GSS_S_NO_CONTEXT;
212         *minor = GSSEAP_CONTEXT_INCOMPLETE;
213         goto cleanup;
214     }
215
216     major = gssEapWrapIovLength(minor, ctx, conf_req_flag, qop_req,
217                                 conf_state, iov, iov_count);
218     if (GSS_ERROR(major))
219         goto cleanup;
220
221 cleanup:
222     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
223
224     return major;
225 }