Fixes for Heimdal (macOS) builds from Stefan.
[mech_eap.git] / mech_eap / wrap_iov_length.c
1 /*
2  * Copyright (c) 2011, 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_const_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                     enum gss_eap_token_type toktype)
75 {
76     gss_iov_buffer_t header, trailer, padding;
77     size_t dataLength, assocDataLength;
78     size_t gssHeaderLen, gssPadLen, gssTrailerLen;
79     size_t krbHeaderLen = 0, krbTrailerLen = 0, krbPadLen = 0;
80     krb5_error_code code;
81     krb5_context krbContext;
82     int dce_or_mic;
83     size_t ec;
84 #ifdef HAVE_HEIMDAL_VERSION
85     krb5_crypto krbCrypto = NULL;
86 #endif
87
88     if (qop_req != GSS_C_QOP_DEFAULT) {
89         *minor = GSSEAP_UNKNOWN_QOP;
90         return GSS_S_UNAVAILABLE;
91     }
92
93     if (ctx->encryptionType == ENCTYPE_NULL) {
94         *minor = GSSEAP_KEY_UNAVAILABLE;
95         return GSS_S_UNAVAILABLE;
96     }
97
98     GSSEAP_KRB_INIT(&krbContext);
99
100     header = gssEapLocateHeaderIov(iov, iov_count, toktype);
101     if (header == NULL) {
102         *minor = GSSEAP_MISSING_IOV;
103         return GSS_S_FAILURE;
104     }
105     INIT_IOV_DATA(header);
106
107     trailer = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_TRAILER);
108     if (trailer != NULL) {
109         INIT_IOV_DATA(trailer);
110     }
111
112     /* MIC tokens and DCE-style wrap tokens have similar length considerations:
113      * no padding, and the framing surrounds the header only, not the data. */
114     dce_or_mic = ((ctx->gssFlags & GSS_C_DCE_STYLE) != 0 ||
115                   toktype == TOK_TYPE_MIC);
116
117     /* For CFX, EC is used instead of padding, and is placed in header or trailer */
118     padding = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_PADDING);
119     if (padding != NULL) {
120         INIT_IOV_DATA(padding);
121     }
122
123     gssEapIovMessageLength(iov, iov_count, &dataLength, &assocDataLength);
124
125     if (conf_req_flag && gssEapIsIntegrityOnly(iov, iov_count))
126         conf_req_flag = FALSE;
127
128     gssPadLen = gssTrailerLen = 0;
129
130 #ifdef HAVE_HEIMDAL_VERSION
131     code = krb5_crypto_init(krbContext, &ctx->rfc3961Key, ETYPE_NULL, &krbCrypto);
132     if (code != 0)
133         return code;
134 #endif
135
136     code = krbCryptoLength(krbContext, KRB_CRYPTO_CONTEXT(ctx),
137                            conf_req_flag ?
138                                 KRB5_CRYPTO_TYPE_TRAILER : KRB5_CRYPTO_TYPE_CHECKSUM,
139                            &krbTrailerLen);
140     if (code != 0) {
141         *minor = code;
142         return GSS_S_FAILURE;
143     }
144
145     if (conf_req_flag) {
146         code = krbCryptoLength(krbContext, KRB_CRYPTO_CONTEXT(ctx),
147                                KRB5_CRYPTO_TYPE_HEADER, &krbHeaderLen);
148         if (code != 0) {
149             *minor = code;
150             return GSS_S_FAILURE;
151         }
152     }
153
154     gssHeaderLen = 16; /* Header */
155     if (conf_req_flag) {
156         gssHeaderLen += krbHeaderLen; /* Kerb-Header */
157         gssTrailerLen = 16 /* E(Header) */ + krbTrailerLen; /* Kerb-Trailer */
158
159         code = krbPaddingLength(krbContext, KRB_CRYPTO_CONTEXT(ctx),
160                                 dataLength - assocDataLength + 16 /* E(Header) */,
161                                 &krbPadLen);
162         if (code != 0) {
163             *minor = code;
164             return GSS_S_FAILURE;
165         }
166
167         if (krbPadLen == 0 && dce_or_mic) {
168             /* Windows rejects AEAD tokens with non-zero EC */
169             code = krbBlockSize(krbContext, KRB_CRYPTO_CONTEXT(ctx), &ec);
170             if (code != 0) {
171                 *minor = code;
172                 return GSS_S_FAILURE;
173             }
174         } else
175             ec = krbPadLen;
176
177         gssTrailerLen += ec;
178     } else {
179         gssTrailerLen = krbTrailerLen; /* Kerb-Checksum */
180     }
181
182     dataLength += gssPadLen;
183
184     if (trailer == NULL)
185         gssHeaderLen += gssTrailerLen;
186     else
187         trailer->buffer.length = gssTrailerLen;
188
189     GSSEAP_ASSERT(gssPadLen == 0 || padding != NULL);
190
191     if (padding != NULL)
192         padding->buffer.length = gssPadLen;
193
194     header->buffer.length = gssHeaderLen;
195
196     if (conf_state != NULL)
197         *conf_state = conf_req_flag;
198
199     *minor = 0;
200     return GSS_S_COMPLETE;
201 }
202
203 OM_uint32 GSSAPI_CALLCONV
204 gss_wrap_iov_length(OM_uint32 *minor,
205                     gss_ctx_id_t ctx,
206                     int conf_req_flag,
207                     gss_qop_t qop_req,
208                     int *conf_state,
209                     gss_iov_buffer_desc *iov,
210                     int iov_count)
211 {
212     OM_uint32 major;
213
214     if (ctx == GSS_C_NO_CONTEXT) {
215         *minor = EINVAL;
216         return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CONTEXT;
217     }
218
219     *minor = 0;
220
221     GSSEAP_MUTEX_LOCK(&ctx->mutex);
222
223     if (!CTX_IS_ESTABLISHED(ctx)) {
224         major = GSS_S_NO_CONTEXT;
225         *minor = GSSEAP_CONTEXT_INCOMPLETE;
226         goto cleanup;
227     }
228
229     major = gssEapWrapIovLength(minor, ctx, conf_req_flag, qop_req,
230                                 conf_state, iov, iov_count, TOK_TYPE_WRAP);
231     if (GSS_ERROR(major))
232         goto cleanup;
233
234 cleanup:
235     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
236
237     return major;
238 }
239
240 OM_uint32 GSSAPI_CALLCONV
241 gss_get_mic_iov_length(OM_uint32 *minor,
242                        gss_ctx_id_t ctx,
243                        gss_qop_t qop_req,
244                        gss_iov_buffer_desc *iov,
245                        int iov_count)
246 {
247     OM_uint32 major;
248
249     if (ctx == GSS_C_NO_CONTEXT) {
250         *minor = EINVAL;
251         return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CONTEXT;
252     }
253
254     *minor = 0;
255
256     GSSEAP_MUTEX_LOCK(&ctx->mutex);
257
258     if (!CTX_IS_ESTABLISHED(ctx)) {
259         major = GSS_S_NO_CONTEXT;
260         *minor = GSSEAP_CONTEXT_INCOMPLETE;
261         goto cleanup;
262     }
263
264     major = gssEapWrapIovLength(minor, ctx, FALSE, qop_req,
265                                 NULL, iov, iov_count, TOK_TYPE_MIC);
266     if (GSS_ERROR(major))
267         goto cleanup;
268
269 cleanup:
270     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
271
272     return major;
273 }