Line data Source code
1 : /*
2 : * Copyright (c) 1997 - 2003 Kungliga Tekniska Högskolan
3 : * (Royal Institute of Technology, Stockholm, Sweden).
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions
8 : * are met:
9 : *
10 : * 1. Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : *
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.
16 : *
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.
20 : *
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
31 : * SUCH DAMAGE.
32 : */
33 :
34 : #include "gsskrb5_locl.h"
35 :
36 : krb5_error_code
37 36576 : _gsskrb5_encode_om_uint32(OM_uint32 n, u_char *p)
38 : {
39 36576 : p[0] = (n >> 0) & 0xFF;
40 36576 : p[1] = (n >> 8) & 0xFF;
41 36576 : p[2] = (n >> 16) & 0xFF;
42 36576 : p[3] = (n >> 24) & 0xFF;
43 36576 : return 0;
44 : }
45 :
46 : krb5_error_code
47 3653065 : _gsskrb5_encode_be_om_uint32(OM_uint32 n, u_char *p)
48 : {
49 3653065 : p[0] = (n >> 24) & 0xFF;
50 3653065 : p[1] = (n >> 16) & 0xFF;
51 3653065 : p[2] = (n >> 8) & 0xFF;
52 3653065 : p[3] = (n >> 0) & 0xFF;
53 3653065 : return 0;
54 : }
55 :
56 : krb5_error_code
57 88736 : _gsskrb5_decode_om_uint32(const void *ptr, OM_uint32 *n)
58 : {
59 88736 : const u_char *p = ptr;
60 177472 : *n = ((uint32_t)p[0])
61 88736 : | ((uint32_t)p[1] << 8)
62 88736 : | ((uint32_t)p[2] << 16)
63 88736 : | ((uint32_t)p[3] << 24);
64 88736 : return 0;
65 : }
66 :
67 : krb5_error_code
68 2204856 : _gsskrb5_decode_be_om_uint32(const void *ptr, OM_uint32 *n)
69 : {
70 2204856 : const u_char *p = ptr;
71 4409712 : *n = ((uint32_t)p[0] <<24)
72 2204856 : | ((uint32_t)p[1] << 16)
73 2204856 : | ((uint32_t)p[2] << 8)
74 2204856 : | ((uint32_t)p[3]);
75 2204856 : return 0;
76 : }
77 :
78 : static krb5_error_code
79 0 : hash_input_chan_bindings (const gss_channel_bindings_t b,
80 : u_char *p)
81 : {
82 : u_char num[4];
83 : EVP_MD_CTX *ctx;
84 :
85 0 : ctx = EVP_MD_CTX_create();
86 0 : EVP_DigestInit_ex(ctx, EVP_md5(), NULL);
87 :
88 0 : _gsskrb5_encode_om_uint32 (b->initiator_addrtype, num);
89 0 : EVP_DigestUpdate(ctx, num, sizeof(num));
90 0 : _gsskrb5_encode_om_uint32 (b->initiator_address.length, num);
91 0 : EVP_DigestUpdate(ctx, num, sizeof(num));
92 0 : if (b->initiator_address.length)
93 0 : EVP_DigestUpdate(ctx,
94 0 : b->initiator_address.value,
95 : b->initiator_address.length);
96 0 : _gsskrb5_encode_om_uint32 (b->acceptor_addrtype, num);
97 0 : EVP_DigestUpdate(ctx, num, sizeof(num));
98 0 : _gsskrb5_encode_om_uint32 (b->acceptor_address.length, num);
99 0 : EVP_DigestUpdate(ctx, num, sizeof(num));
100 0 : if (b->acceptor_address.length)
101 0 : EVP_DigestUpdate(ctx,
102 0 : b->acceptor_address.value,
103 : b->acceptor_address.length);
104 0 : _gsskrb5_encode_om_uint32 (b->application_data.length, num);
105 0 : EVP_DigestUpdate(ctx, num, sizeof(num));
106 0 : if (b->application_data.length)
107 0 : EVP_DigestUpdate(ctx,
108 0 : b->application_data.value,
109 : b->application_data.length);
110 0 : EVP_DigestFinal_ex(ctx, p, NULL);
111 0 : EVP_MD_CTX_destroy(ctx);
112 :
113 0 : return 0;
114 : }
115 :
116 : /*
117 : * create a checksum over the chanel bindings in
118 : * `input_chan_bindings', `flags' and `fwd_data' and return it in
119 : * `result'
120 : */
121 :
122 : OM_uint32
123 18288 : _gsskrb5_create_8003_checksum (
124 : OM_uint32 *minor_status,
125 : const gss_channel_bindings_t input_chan_bindings,
126 : OM_uint32 flags,
127 : const krb5_data *fwd_data,
128 : Checksum *result)
129 : {
130 : u_char *p;
131 :
132 : /*
133 : * see rfc1964 (section 1.1.1 (Initial Token), and the checksum value
134 : * field's format) */
135 18288 : result->cksumtype = CKSUMTYPE_GSSAPI;
136 18288 : if (fwd_data->length > 0 && (flags & GSS_C_DELEG_FLAG))
137 17089 : result->checksum.length = 24 + 4 + fwd_data->length;
138 : else
139 1199 : result->checksum.length = 24;
140 18288 : result->checksum.data = malloc (result->checksum.length);
141 18288 : if (result->checksum.data == NULL) {
142 0 : *minor_status = ENOMEM;
143 0 : return GSS_S_FAILURE;
144 : }
145 :
146 18288 : p = result->checksum.data;
147 18288 : _gsskrb5_encode_om_uint32 (16, p);
148 18288 : p += 4;
149 18288 : if (input_chan_bindings == GSS_C_NO_CHANNEL_BINDINGS) {
150 18288 : memset (p, 0, 16);
151 : } else {
152 0 : hash_input_chan_bindings (input_chan_bindings, p);
153 : }
154 18288 : p += 16;
155 18288 : _gsskrb5_encode_om_uint32 (flags, p);
156 18288 : p += 4;
157 :
158 18288 : if (fwd_data->length > 0 && (flags & GSS_C_DELEG_FLAG)) {
159 :
160 17089 : *p++ = (1 >> 0) & 0xFF; /* DlgOpt */ /* == 1 */
161 17089 : *p++ = (1 >> 8) & 0xFF; /* DlgOpt */ /* == 0 */
162 17089 : *p++ = (fwd_data->length >> 0) & 0xFF; /* Dlgth */
163 17089 : *p++ = (fwd_data->length >> 8) & 0xFF; /* Dlgth */
164 17089 : memcpy(p, (unsigned char *) fwd_data->data, fwd_data->length);
165 :
166 : /* p += fwd_data->length; */ /* commented out to quiet warning */
167 : }
168 :
169 18288 : return GSS_S_COMPLETE;
170 : }
171 :
172 : static krb5_error_code
173 0 : check_ap_options_cbt(void *ad_data, size_t ad_len,
174 : krb5_boolean *client_asserted_cb)
175 : {
176 : uint32_t ad_ap_options;
177 :
178 0 : *client_asserted_cb = FALSE;
179 :
180 0 : if (ad_len != sizeof(uint32_t))
181 0 : return KRB5KRB_AP_ERR_MSG_TYPE;
182 :
183 0 : _gss_mg_decode_le_uint32(ad_data, &ad_ap_options);
184 :
185 0 : if (ad_ap_options & KERB_AP_OPTIONS_CBT)
186 0 : *client_asserted_cb = TRUE;
187 :
188 0 : return 0;
189 : }
190 :
191 : static krb5_error_code
192 44368 : find_ap_options(krb5_context context,
193 : krb5_authenticator authenticator,
194 : krb5_boolean *client_asserted_cb)
195 : {
196 : krb5_error_code ret;
197 : krb5_authdata *ad;
198 : krb5_data data;
199 :
200 44368 : *client_asserted_cb = FALSE;
201 :
202 44368 : ad = authenticator->authorization_data;
203 44368 : if (ad == NULL)
204 154 : return 0;
205 :
206 44214 : ret = _krb5_get_ad(context, ad, NULL, KRB5_AUTHDATA_AP_OPTIONS, &data);
207 44214 : if (ret)
208 44214 : return ret == ENOENT ? 0 : ret;
209 :
210 0 : ret = check_ap_options_cbt(data.data, data.length, client_asserted_cb);
211 0 : krb5_data_free(&data);
212 :
213 0 : return ret;
214 : }
215 :
216 : /*
217 : * verify the checksum in `cksum' over `input_chan_bindings'
218 : * returning `flags' and `fwd_data'
219 : */
220 :
221 : OM_uint32
222 44368 : _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,
227 : OM_uint32 *flags,
228 : krb5_data *fwd_data)
229 : {
230 : unsigned char hash[16];
231 : unsigned char *p;
232 : OM_uint32 length;
233 : int DlgOpt;
234 : static unsigned char zeros[16];
235 44368 : krb5_boolean channel_bound = FALSE;
236 44368 : const Checksum *cksum = authenticator->cksum;
237 : krb5_boolean client_asserted_cb;
238 : krb5_error_code ret;
239 :
240 : /* XXX should handle checksums > 24 bytes */
241 44368 : if(cksum->cksumtype != CKSUMTYPE_GSSAPI || cksum->checksum.length < 24) {
242 0 : *minor_status = 0;
243 0 : return GSS_S_BAD_BINDINGS;
244 : }
245 :
246 44368 : p = cksum->checksum.data;
247 44368 : _gsskrb5_decode_om_uint32(p, &length);
248 44368 : if(length != sizeof(hash)) {
249 0 : *minor_status = 0;
250 0 : return GSS_S_BAD_BINDINGS;
251 : }
252 :
253 44368 : p += 4;
254 :
255 44368 : ret = find_ap_options(context, authenticator, &client_asserted_cb);
256 44368 : if (ret) {
257 0 : *minor_status = ret;
258 0 : return GSS_S_FAILURE;
259 : }
260 :
261 44368 : if (input_chan_bindings != GSS_C_NO_CHANNEL_BINDINGS
262 0 : && (memcmp(p, zeros, sizeof(zeros)) != 0 || client_asserted_cb)) {
263 0 : if(hash_input_chan_bindings(input_chan_bindings, hash) != 0) {
264 0 : *minor_status = 0;
265 0 : return GSS_S_BAD_BINDINGS;
266 : }
267 0 : if(ct_memcmp(hash, p, sizeof(hash)) != 0) {
268 0 : *minor_status = 0;
269 0 : return GSS_S_BAD_BINDINGS;
270 : }
271 0 : channel_bound = TRUE;
272 : }
273 :
274 44368 : p += sizeof(hash);
275 :
276 44368 : _gsskrb5_decode_om_uint32(p, flags);
277 44368 : p += 4;
278 :
279 44368 : if (cksum->checksum.length > 24 && (*flags & GSS_C_DELEG_FLAG)) {
280 41451 : if(cksum->checksum.length < 28) {
281 0 : *minor_status = 0;
282 0 : return GSS_S_BAD_BINDINGS;
283 : }
284 :
285 41451 : DlgOpt = (p[0] << 0) | (p[1] << 8);
286 41451 : p += 2;
287 41451 : if (DlgOpt != 1) {
288 0 : *minor_status = 0;
289 0 : return GSS_S_BAD_BINDINGS;
290 : }
291 :
292 41451 : fwd_data->length = (p[0] << 0) | (p[1] << 8);
293 41451 : p += 2;
294 41451 : if(cksum->checksum.length < 28 + fwd_data->length) {
295 0 : *minor_status = 0;
296 0 : return GSS_S_BAD_BINDINGS;
297 : }
298 41451 : fwd_data->data = malloc(fwd_data->length);
299 41451 : if (fwd_data->data == NULL) {
300 0 : *minor_status = ENOMEM;
301 0 : return GSS_S_FAILURE;
302 : }
303 41451 : memcpy(fwd_data->data, p, fwd_data->length);
304 : }
305 :
306 44368 : if (channel_bound) {
307 0 : *flags |= GSS_C_CHANNEL_BOUND_FLAG;
308 : } else {
309 44368 : *flags &= ~GSS_C_CHANNEL_BOUND_FLAG;
310 : }
311 :
312 44368 : return GSS_S_COMPLETE;
313 : }
|