2 * Copyright (c) 1997 - 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "gsskrb5_locl.h"
37 _gsskrb5_encode_om_uint32(OM_uint32 n, u_char *p)
39 p[0] = (n >> 0) & 0xFF;
40 p[1] = (n >> 8) & 0xFF;
41 p[2] = (n >> 16) & 0xFF;
42 p[3] = (n >> 24) & 0xFF;
47 _gsskrb5_encode_be_om_uint32(OM_uint32 n, u_char *p)
49 p[0] = (n >> 24) & 0xFF;
50 p[1] = (n >> 16) & 0xFF;
51 p[2] = (n >> 8) & 0xFF;
52 p[3] = (n >> 0) & 0xFF;
57 _gsskrb5_decode_om_uint32(const void *ptr, OM_uint32 *n)
59 const u_char *p = ptr;
61 | ((uint32_t)p[1] << 8)
62 | ((uint32_t)p[2] << 16)
63 | ((uint32_t)p[3] << 24);
68 _gsskrb5_decode_be_om_uint32(const void *ptr, OM_uint32 *n)
70 const u_char *p = ptr;
71 *n = ((uint32_t)p[0] <<24)
72 | ((uint32_t)p[1] << 16)
73 | ((uint32_t)p[2] << 8)
78 static krb5_error_code
79 hash_input_chan_bindings (const gss_channel_bindings_t b,
85 ctx = EVP_MD_CTX_create();
86 EVP_DigestInit_ex(ctx, EVP_md5(), NULL);
88 _gsskrb5_encode_om_uint32 (b->initiator_addrtype, num);
89 EVP_DigestUpdate(ctx, num, sizeof(num));
90 _gsskrb5_encode_om_uint32 (b->initiator_address.length, num);
91 EVP_DigestUpdate(ctx, num, sizeof(num));
92 if (b->initiator_address.length)
94 b->initiator_address.value,
95 b->initiator_address.length);
96 _gsskrb5_encode_om_uint32 (b->acceptor_addrtype, num);
97 EVP_DigestUpdate(ctx, num, sizeof(num));
98 _gsskrb5_encode_om_uint32 (b->acceptor_address.length, num);
99 EVP_DigestUpdate(ctx, num, sizeof(num));
100 if (b->acceptor_address.length)
101 EVP_DigestUpdate(ctx,
102 b->acceptor_address.value,
103 b->acceptor_address.length);
104 _gsskrb5_encode_om_uint32 (b->application_data.length, num);
105 EVP_DigestUpdate(ctx, num, sizeof(num));
106 if (b->application_data.length)
107 EVP_DigestUpdate(ctx,
108 b->application_data.value,
109 b->application_data.length);
110 EVP_DigestFinal_ex(ctx, p, NULL);
111 EVP_MD_CTX_destroy(ctx);
117 * create a checksum over the chanel bindings in
118 * `input_chan_bindings', `flags' and `fwd_data' and return it in
123 _gsskrb5_create_8003_checksum (
124 OM_uint32 *minor_status,
125 const gss_channel_bindings_t input_chan_bindings,
127 const krb5_data *fwd_data,
133 * see rfc1964 (section 1.1.1 (Initial Token), and the checksum value
135 result->cksumtype = CKSUMTYPE_GSSAPI;
136 if (fwd_data->length > 0 && (flags & GSS_C_DELEG_FLAG))
137 result->checksum.length = 24 + 4 + fwd_data->length;
139 result->checksum.length = 24;
140 result->checksum.data = malloc (result->checksum.length);
141 if (result->checksum.data == NULL) {
142 *minor_status = ENOMEM;
143 return GSS_S_FAILURE;
146 p = result->checksum.data;
147 _gsskrb5_encode_om_uint32 (16, p);
149 if (input_chan_bindings == GSS_C_NO_CHANNEL_BINDINGS) {
152 hash_input_chan_bindings (input_chan_bindings, p);
155 _gsskrb5_encode_om_uint32 (flags, p);
158 if (fwd_data->length > 0 && (flags & GSS_C_DELEG_FLAG)) {
160 *p++ = (1 >> 0) & 0xFF; /* DlgOpt */ /* == 1 */
161 *p++ = (1 >> 8) & 0xFF; /* DlgOpt */ /* == 0 */
162 *p++ = (fwd_data->length >> 0) & 0xFF; /* Dlgth */
163 *p++ = (fwd_data->length >> 8) & 0xFF; /* Dlgth */
164 memcpy(p, (unsigned char *) fwd_data->data, fwd_data->length);
166 /* p += fwd_data->length; */ /* commented out to quiet warning */
169 return GSS_S_COMPLETE;
172 static krb5_error_code
173 check_ap_options_cbt(void *ad_data, size_t ad_len,
174 krb5_boolean *client_asserted_cb)
176 uint32_t ad_ap_options;
178 *client_asserted_cb = FALSE;
180 if (ad_len != sizeof(uint32_t))
181 return KRB5KRB_AP_ERR_MSG_TYPE;
183 _gss_mg_decode_le_uint32(ad_data, &ad_ap_options);
185 if (ad_ap_options & KERB_AP_OPTIONS_CBT)
186 *client_asserted_cb = TRUE;
191 static krb5_error_code
192 find_ap_options(krb5_context context,
193 krb5_authenticator authenticator,
194 krb5_boolean *client_asserted_cb)
200 *client_asserted_cb = FALSE;
202 ad = authenticator->authorization_data;
206 ret = _krb5_get_ad(context, ad, NULL, KRB5_AUTHDATA_AP_OPTIONS, &data);
208 return ret == ENOENT ? 0 : ret;
210 ret = check_ap_options_cbt(data.data, data.length, client_asserted_cb);
211 krb5_data_free(&data);
217 * verify the checksum in `cksum' over `input_chan_bindings'
218 * returning `flags' and `fwd_data'
222 _gsskrb5_verify_8003_checksum(
223 krb5_context context,
224 OM_uint32 *minor_status,
225 const gss_channel_bindings_t input_chan_bindings,
226 krb5_authenticator authenticator,
230 unsigned char hash[16];
234 static unsigned char zeros[16];
235 krb5_boolean channel_bound = FALSE;
236 const Checksum *cksum = authenticator->cksum;
237 krb5_boolean client_asserted_cb;
240 /* XXX should handle checksums > 24 bytes */
241 if(cksum->cksumtype != CKSUMTYPE_GSSAPI || cksum->checksum.length < 24) {
243 return GSS_S_BAD_BINDINGS;
246 p = cksum->checksum.data;
247 _gsskrb5_decode_om_uint32(p, &length);
248 if(length != sizeof(hash)) {
250 return GSS_S_BAD_BINDINGS;
255 ret = find_ap_options(context, authenticator, &client_asserted_cb);
258 return GSS_S_FAILURE;
261 if (input_chan_bindings != GSS_C_NO_CHANNEL_BINDINGS
262 && (memcmp(p, zeros, sizeof(zeros)) != 0 || client_asserted_cb)) {
263 if(hash_input_chan_bindings(input_chan_bindings, hash) != 0) {
265 return GSS_S_BAD_BINDINGS;
267 if(ct_memcmp(hash, p, sizeof(hash)) != 0) {
269 return GSS_S_BAD_BINDINGS;
271 channel_bound = TRUE;
276 _gsskrb5_decode_om_uint32(p, flags);
279 if (cksum->checksum.length > 24 && (*flags & GSS_C_DELEG_FLAG)) {
280 if(cksum->checksum.length < 28) {
282 return GSS_S_BAD_BINDINGS;
285 DlgOpt = (p[0] << 0) | (p[1] << 8);
289 return GSS_S_BAD_BINDINGS;
292 fwd_data->length = (p[0] << 0) | (p[1] << 8);
294 if(cksum->checksum.length < 28 + fwd_data->length) {
296 return GSS_S_BAD_BINDINGS;
298 fwd_data->data = malloc(fwd_data->length);
299 if (fwd_data->data == NULL) {
300 *minor_status = ENOMEM;
301 return GSS_S_FAILURE;
303 memcpy(fwd_data->data, p, fwd_data->length);
307 *flags |= GSS_C_CHANNEL_BOUND_FLAG;
309 *flags &= ~GSS_C_CHANNEL_BOUND_FLAG;
312 return GSS_S_COMPLETE;