2 * Copyright (c) 2011, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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
33 * Copyright 2008 by the Massachusetts Institute of Technology.
34 * All Rights Reserved.
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.
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.
57 * Message protection services: determine protected message size.
60 #include "gssapiP_eap.h"
62 #define INIT_IOV_DATA(_iov) do { (_iov)->buffer.value = NULL; \
63 (_iov)->buffer.length = 0; } \
67 gssEapWrapIovLength(OM_uint32 *minor,
72 gss_iov_buffer_desc *iov,
75 gss_iov_buffer_t header, trailer, padding;
76 size_t dataLength, assocDataLength;
77 size_t gssHeaderLen, gssPadLen, gssTrailerLen;
78 size_t krbHeaderLen = 0, krbTrailerLen = 0, krbPadLen = 0;
80 krb5_context krbContext;
83 #ifdef HAVE_HEIMDAL_VERSION
84 krb5_crypto krbCrypto = NULL;
87 if (qop_req != GSS_C_QOP_DEFAULT) {
88 *minor = GSSEAP_UNKNOWN_QOP;
89 return GSS_S_UNAVAILABLE;
92 if (ctx->encryptionType == ENCTYPE_NULL) {
93 *minor = GSSEAP_KEY_UNAVAILABLE;
94 return GSS_S_UNAVAILABLE;
97 GSSEAP_KRB_INIT(&krbContext);
99 header = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_HEADER);
100 if (header == NULL) {
101 *minor = GSSEAP_MISSING_IOV;
102 return GSS_S_FAILURE;
104 INIT_IOV_DATA(header);
106 trailer = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_TRAILER);
107 if (trailer != NULL) {
108 INIT_IOV_DATA(trailer);
111 dce_style = ((ctx->gssFlags & GSS_C_DCE_STYLE) != 0);
113 /* For CFX, EC is used instead of padding, and is placed in header or trailer */
114 padding = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_PADDING);
115 if (padding != NULL) {
116 INIT_IOV_DATA(padding);
119 gssEapIovMessageLength(iov, iov_count, &dataLength, &assocDataLength);
121 if (conf_req_flag && gssEapIsIntegrityOnly(iov, iov_count))
122 conf_req_flag = FALSE;
124 gssHeaderLen = gssPadLen = gssTrailerLen = 0;
126 #ifdef HAVE_HEIMDAL_VERSION
127 code = krb5_crypto_init(krbContext, &ctx->rfc3961Key, ETYPE_NULL, &krbCrypto);
132 code = krbCryptoLength(krbContext, KRB_CRYPTO_CONTEXT(ctx),
134 KRB5_CRYPTO_TYPE_TRAILER : KRB5_CRYPTO_TYPE_CHECKSUM,
138 return GSS_S_FAILURE;
142 code = krbCryptoLength(krbContext, KRB_CRYPTO_CONTEXT(ctx),
143 KRB5_CRYPTO_TYPE_HEADER, &krbHeaderLen);
146 return GSS_S_FAILURE;
150 gssHeaderLen = 16; /* Header */
152 gssHeaderLen += krbHeaderLen; /* Kerb-Header */
153 gssTrailerLen = 16 /* E(Header) */ + krbTrailerLen; /* Kerb-Trailer */
155 code = krbPaddingLength(krbContext, KRB_CRYPTO_CONTEXT(ctx),
156 dataLength - assocDataLength + 16 /* E(Header) */,
160 return GSS_S_FAILURE;
163 if (krbPadLen == 0 && dce_style) {
164 /* Windows rejects AEAD tokens with non-zero EC */
165 code = krbBlockSize(krbContext, KRB_CRYPTO_CONTEXT(ctx), &ec);
168 return GSS_S_FAILURE;
175 gssTrailerLen = krbTrailerLen; /* Kerb-Checksum */
178 dataLength += gssPadLen;
181 gssHeaderLen += gssTrailerLen;
183 trailer->buffer.length = gssTrailerLen;
185 assert(gssPadLen == 0 || padding != NULL);
188 padding->buffer.length = gssPadLen;
190 header->buffer.length = gssHeaderLen;
192 if (conf_state != NULL)
193 *conf_state = conf_req_flag;
196 return GSS_S_COMPLETE;
199 OM_uint32 GSSAPI_CALLCONV
200 gss_wrap_iov_length(OM_uint32 *minor,
205 gss_iov_buffer_desc *iov,
210 if (ctx == GSS_C_NO_CONTEXT) {
212 return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CONTEXT;
217 GSSEAP_MUTEX_LOCK(&ctx->mutex);
219 if (!CTX_IS_ESTABLISHED(ctx)) {
220 major = GSS_S_NO_CONTEXT;
221 *minor = GSSEAP_CONTEXT_INCOMPLETE;
225 major = gssEapWrapIovLength(minor, ctx, conf_req_flag, qop_req,
226 conf_state, iov, iov_count);
227 if (GSS_ERROR(major))
231 GSSEAP_MUTEX_UNLOCK(&ctx->mutex);