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 "gssapi_locl.h"
36 RCSID("$Id: 8003.c,v 1.12.2.2 2003/09/18 21:30:57 lha Exp $");
39 gssapi_encode_om_uint32(OM_uint32 n, u_char *p)
41 p[0] = (n >> 0) & 0xFF;
42 p[1] = (n >> 8) & 0xFF;
43 p[2] = (n >> 16) & 0xFF;
44 p[3] = (n >> 24) & 0xFF;
49 gssapi_encode_be_om_uint32(OM_uint32 n, u_char *p)
51 p[0] = (n >> 24) & 0xFF;
52 p[1] = (n >> 16) & 0xFF;
53 p[2] = (n >> 8) & 0xFF;
54 p[3] = (n >> 0) & 0xFF;
59 gssapi_decode_om_uint32(u_char *p, OM_uint32 *n)
61 *n = (p[0] << 0) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
66 gssapi_decode_be_om_uint32(u_char *p, OM_uint32 *n)
68 *n = (p[0] <<24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
72 static krb5_error_code
73 hash_input_chan_bindings (const gss_channel_bindings_t b,
80 gssapi_encode_om_uint32 (b->initiator_addrtype, num);
81 MD5_Update (&md5, num, sizeof(num));
82 gssapi_encode_om_uint32 (b->initiator_address.length, num);
83 MD5_Update (&md5, num, sizeof(num));
84 if (b->initiator_address.length)
86 b->initiator_address.value,
87 b->initiator_address.length);
88 gssapi_encode_om_uint32 (b->acceptor_addrtype, num);
89 MD5_Update (&md5, num, sizeof(num));
90 gssapi_encode_om_uint32 (b->acceptor_address.length, num);
91 MD5_Update (&md5, num, sizeof(num));
92 if (b->acceptor_address.length)
94 b->acceptor_address.value,
95 b->acceptor_address.length);
96 gssapi_encode_om_uint32 (b->application_data.length, num);
97 MD5_Update (&md5, num, sizeof(num));
98 if (b->application_data.length)
100 b->application_data.value,
101 b->application_data.length);
107 * create a checksum over the chanel bindings in
108 * `input_chan_bindings', `flags' and `fwd_data' and return it in
113 gssapi_krb5_create_8003_checksum (
114 OM_uint32 *minor_status,
115 const gss_channel_bindings_t input_chan_bindings,
117 const krb5_data *fwd_data,
123 * see rfc1964 (section 1.1.1 (Initial Token), and the checksum value
125 result->cksumtype = 0x8003;
126 if (fwd_data->length > 0 && (flags & GSS_C_DELEG_FLAG))
127 result->checksum.length = 24 + 4 + fwd_data->length;
129 result->checksum.length = 24;
130 result->checksum.data = malloc (result->checksum.length);
131 if (result->checksum.data == NULL) {
132 *minor_status = ENOMEM;
133 return GSS_S_FAILURE;
136 p = result->checksum.data;
137 gssapi_encode_om_uint32 (16, p);
139 if (input_chan_bindings == GSS_C_NO_CHANNEL_BINDINGS) {
142 hash_input_chan_bindings (input_chan_bindings, p);
145 gssapi_encode_om_uint32 (flags, p);
148 if (fwd_data->length > 0 && (flags & GSS_C_DELEG_FLAG)) {
152 result->checksum.length = 28 + fwd_data->length;
153 tmp = realloc(result->checksum.data, result->checksum.length);
156 result->checksum.data = tmp;
158 p = (u_char*)result->checksum.data + 24;
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;
169 return GSS_S_COMPLETE;
173 * verify the checksum in `cksum' over `input_chan_bindings'
174 * returning `flags' and `fwd_data'
178 gssapi_krb5_verify_8003_checksum(
179 OM_uint32 *minor_status,
180 const gss_channel_bindings_t input_chan_bindings,
181 const Checksum *cksum,
185 unsigned char hash[16];
189 static unsigned char zeros[16];
191 /* XXX should handle checksums > 24 bytes */
192 if(cksum->cksumtype != 0x8003 || cksum->checksum.length < 24) {
194 return GSS_S_BAD_BINDINGS;
197 p = cksum->checksum.data;
198 gssapi_decode_om_uint32(p, &length);
199 if(length != sizeof(hash)) {
201 return GSS_S_BAD_BINDINGS;
206 if (input_chan_bindings != GSS_C_NO_CHANNEL_BINDINGS
207 && memcmp(p, zeros, sizeof(zeros)) != 0) {
208 if(hash_input_chan_bindings(input_chan_bindings, hash) != 0) {
210 return GSS_S_BAD_BINDINGS;
212 if(memcmp(hash, p, sizeof(hash)) != 0) {
214 return GSS_S_BAD_BINDINGS;
220 gssapi_decode_om_uint32(p, flags);
223 if (cksum->checksum.length > 24 && (*flags & GSS_C_DELEG_FLAG)) {
224 if(cksum->checksum.length < 28) {
226 return GSS_S_BAD_BINDINGS;
229 DlgOpt = (p[0] << 0) | (p[1] << 8);
233 return GSS_S_BAD_BINDINGS;
236 fwd_data->length = (p[0] << 0) | (p[1] << 8);
238 if(cksum->checksum.length < 28 + fwd_data->length) {
240 return GSS_S_BAD_BINDINGS;
242 fwd_data->data = malloc(fwd_data->length);
243 if (fwd_data->data == NULL) {
244 *minor_status = ENOMEM;
245 return GSS_S_FAILURE;
247 memcpy(fwd_data->data, p, fwd_data->length);
250 return GSS_S_COMPLETE;