1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
5 static krb5_boolean chk_heimdal_seqnum(krb5_ui_4, krb5_ui_4);
8 actx_copy_addr(krb5_context context, const krb5_address *inad, krb5_address **outad)
12 if (!(tmpad = (krb5_address *)malloc(sizeof(*tmpad))))
15 if (!(tmpad->contents = (krb5_octet *)malloc(inad->length))) {
19 memcpy(tmpad->contents, inad->contents, inad->length);
24 krb5_error_code KRB5_CALLCONV
25 krb5_auth_con_init(krb5_context context, krb5_auth_context *auth_context)
28 (krb5_auth_context)calloc(1, sizeof(struct _krb5_auth_context));
32 /* Default flags, do time not seq */
33 (*auth_context)->auth_context_flags =
34 KRB5_AUTH_CONTEXT_DO_TIME | KRB5_AUTH_CONN_INITIALIZED;
36 (*auth_context)->req_cksumtype = context->default_ap_req_sumtype;
37 (*auth_context)->safe_cksumtype = context->default_safe_sumtype;
38 (*auth_context)->checksum_func = NULL;
39 (*auth_context)->checksum_func_data = NULL;
40 (*auth_context)->negotiated_etype = ENCTYPE_NULL;
41 (*auth_context)->magic = KV5M_AUTH_CONTEXT;
45 krb5_error_code KRB5_CALLCONV
46 krb5_auth_con_free(krb5_context context, krb5_auth_context auth_context)
48 if (auth_context == NULL)
50 if (auth_context->local_addr)
51 krb5_free_address(context, auth_context->local_addr);
52 if (auth_context->remote_addr)
53 krb5_free_address(context, auth_context->remote_addr);
54 if (auth_context->local_port)
55 krb5_free_address(context, auth_context->local_port);
56 if (auth_context->remote_port)
57 krb5_free_address(context, auth_context->remote_port);
58 if (auth_context->authentp)
59 krb5_free_authenticator(context, auth_context->authentp);
60 if (auth_context->key)
61 krb5_k_free_key(context, auth_context->key);
62 if (auth_context->send_subkey)
63 krb5_k_free_key(context, auth_context->send_subkey);
64 if (auth_context->recv_subkey)
65 krb5_k_free_key(context, auth_context->recv_subkey);
66 if (auth_context->rcache)
67 krb5_rc_close(context, auth_context->rcache);
68 if (auth_context->permitted_etypes)
69 free(auth_context->permitted_etypes);
70 if (auth_context->ad_context)
71 krb5_authdata_context_free(context, auth_context->ad_context);
77 krb5_auth_con_setaddrs(krb5_context context, krb5_auth_context auth_context, krb5_address *local_addr, krb5_address *remote_addr)
79 krb5_error_code retval;
81 /* Free old addresses */
82 if (auth_context->local_addr)
83 (void) krb5_free_address(context, auth_context->local_addr);
84 if (auth_context->remote_addr)
85 (void) krb5_free_address(context, auth_context->remote_addr);
89 retval = actx_copy_addr(context,
91 &auth_context->local_addr);
93 auth_context->local_addr = NULL;
95 if (!retval && remote_addr)
96 retval = actx_copy_addr(context,
98 &auth_context->remote_addr);
100 auth_context->remote_addr = NULL;
105 krb5_error_code KRB5_CALLCONV
106 krb5_auth_con_getaddrs(krb5_context context, krb5_auth_context auth_context, krb5_address **local_addr, krb5_address **remote_addr)
108 krb5_error_code retval;
111 if (local_addr && auth_context->local_addr) {
112 retval = actx_copy_addr(context,
113 auth_context->local_addr,
116 if (!retval && (remote_addr) && auth_context->remote_addr) {
117 retval = actx_copy_addr(context,
118 auth_context->remote_addr,
124 krb5_error_code KRB5_CALLCONV
125 krb5_auth_con_setports(krb5_context context, krb5_auth_context auth_context, krb5_address *local_port, krb5_address *remote_port)
127 krb5_error_code retval;
129 /* Free old addresses */
130 if (auth_context->local_port)
131 (void) krb5_free_address(context, auth_context->local_port);
132 if (auth_context->remote_port)
133 (void) krb5_free_address(context, auth_context->remote_port);
137 retval = actx_copy_addr(context,
139 &auth_context->local_port);
141 auth_context->local_port = NULL;
143 if (!retval && remote_port)
144 retval = actx_copy_addr(context,
146 &auth_context->remote_port);
148 auth_context->remote_port = NULL;
155 * This function overloads the keyblock field. It is only useful prior to
156 * a krb5_rd_req_decode() call for user to user authentication where the
157 * server has the key and needs to use it to decrypt the incoming request.
158 * Once decrypted this key is no longer necessary and is then overwritten
159 * with the session key sent by the client.
161 krb5_error_code KRB5_CALLCONV
162 krb5_auth_con_setuseruserkey(krb5_context context, krb5_auth_context auth_context, krb5_keyblock *keyblock)
164 if (auth_context->key)
165 krb5_k_free_key(context, auth_context->key);
166 return(krb5_k_create_key(context, keyblock, &(auth_context->key)));
169 krb5_error_code KRB5_CALLCONV
170 krb5_auth_con_getkey(krb5_context context, krb5_auth_context auth_context, krb5_keyblock **keyblock)
172 if (auth_context->key)
173 return krb5_k_key_keyblock(context, auth_context->key, keyblock);
178 krb5_error_code KRB5_CALLCONV
179 krb5_auth_con_getlocalsubkey(krb5_context context, krb5_auth_context auth_context, krb5_keyblock **keyblock)
181 return krb5_auth_con_getsendsubkey(context, auth_context, keyblock);
184 krb5_error_code KRB5_CALLCONV
185 krb5_auth_con_getremotesubkey(krb5_context context, krb5_auth_context auth_context, krb5_keyblock **keyblock)
187 return krb5_auth_con_getrecvsubkey(context, auth_context, keyblock);
190 krb5_error_code KRB5_CALLCONV
191 krb5_auth_con_setsendsubkey(krb5_context ctx, krb5_auth_context ac, krb5_keyblock *keyblock)
193 if (ac->send_subkey != NULL)
194 krb5_k_free_key(ctx, ac->send_subkey);
195 ac->send_subkey = NULL;
197 return krb5_k_create_key(ctx, keyblock, &ac->send_subkey);
202 krb5_error_code KRB5_CALLCONV
203 krb5_auth_con_setrecvsubkey(krb5_context ctx, krb5_auth_context ac, krb5_keyblock *keyblock)
205 if (ac->recv_subkey != NULL)
206 krb5_k_free_key(ctx, ac->recv_subkey);
207 ac->recv_subkey = NULL;
208 if (keyblock != NULL)
209 return krb5_k_create_key(ctx, keyblock, &ac->recv_subkey);
214 krb5_error_code KRB5_CALLCONV
215 krb5_auth_con_getsendsubkey(krb5_context ctx, krb5_auth_context ac, krb5_keyblock **keyblock)
217 if (ac->send_subkey != NULL)
218 return krb5_k_key_keyblock(ctx, ac->send_subkey, keyblock);
223 krb5_error_code KRB5_CALLCONV
224 krb5_auth_con_getrecvsubkey(krb5_context ctx, krb5_auth_context ac, krb5_keyblock **keyblock)
226 if (ac->recv_subkey != NULL)
227 return krb5_k_key_keyblock(ctx, ac->recv_subkey, keyblock);
232 krb5_error_code KRB5_CALLCONV
233 krb5_auth_con_set_req_cksumtype(krb5_context context, krb5_auth_context auth_context, krb5_cksumtype cksumtype)
235 auth_context->req_cksumtype = cksumtype;
240 krb5_auth_con_set_safe_cksumtype(krb5_context context, krb5_auth_context auth_context, krb5_cksumtype cksumtype)
242 auth_context->safe_cksumtype = cksumtype;
246 krb5_error_code KRB5_CALLCONV
247 krb5_auth_con_getlocalseqnumber(krb5_context context, krb5_auth_context auth_context, krb5_int32 *seqnumber)
249 *seqnumber = auth_context->local_seq_number;
253 krb5_error_code KRB5_CALLCONV
254 krb5_auth_con_getauthenticator(krb5_context context, krb5_auth_context auth_context, krb5_authenticator **authenticator)
256 return (krb5_copy_authenticator(context, auth_context->authentp,
261 krb5_error_code KRB5_CALLCONV
262 krb5_auth_con_getremoteseqnumber(krb5_context context, krb5_auth_context auth_context, krb5_int32 *seqnumber)
264 *seqnumber = auth_context->remote_seq_number;
268 krb5_error_code KRB5_CALLCONV
269 krb5_auth_con_initivector(krb5_context context, krb5_auth_context auth_context)
272 krb5_enctype enctype;
274 if (auth_context->key) {
277 enctype = krb5_k_key_enctype(context, auth_context->key);
278 if ((ret = krb5_c_block_size(context, enctype, &blocksize)))
280 if ((auth_context->i_vector = (krb5_pointer)calloc(1,blocksize))) {
285 return EINVAL; /* XXX need an error for no keyblock */
289 krb5_auth_con_setivector(krb5_context context, krb5_auth_context auth_context, krb5_pointer ivector)
291 auth_context->i_vector = ivector;
296 krb5_auth_con_getivector(krb5_context context, krb5_auth_context auth_context, krb5_pointer *ivector)
298 *ivector = auth_context->i_vector;
302 krb5_error_code KRB5_CALLCONV
303 krb5_auth_con_setflags(krb5_context context, krb5_auth_context auth_context, krb5_int32 flags)
305 auth_context->auth_context_flags = flags;
309 krb5_error_code KRB5_CALLCONV
310 krb5_auth_con_getflags(krb5_context context, krb5_auth_context auth_context, krb5_int32 *flags)
312 *flags = auth_context->auth_context_flags;
316 krb5_error_code KRB5_CALLCONV
317 krb5_auth_con_setrcache(krb5_context context, krb5_auth_context auth_context, krb5_rcache rcache)
319 auth_context->rcache = rcache;
324 krb5_auth_con_getrcache(krb5_context context, krb5_auth_context auth_context, krb5_rcache *rcache)
326 *rcache = auth_context->rcache;
331 krb5_auth_con_setpermetypes(krb5_context context, krb5_auth_context auth_context, const krb5_enctype *permetypes)
333 krb5_enctype * newpe;
336 for (i=0; permetypes[i]; i++)
338 i++; /* include the zero */
340 if ((newpe = (krb5_enctype *) malloc(i*sizeof(krb5_enctype)))
344 if (auth_context->permitted_etypes)
345 free(auth_context->permitted_etypes);
347 auth_context->permitted_etypes = newpe;
349 memcpy(newpe, permetypes, i*sizeof(krb5_enctype));
355 krb5_auth_con_getpermetypes(krb5_context context, krb5_auth_context auth_context, krb5_enctype **permetypes)
357 krb5_enctype * newpe;
360 if (! auth_context->permitted_etypes) {
365 for (i=0; auth_context->permitted_etypes[i]; i++)
367 i++; /* include the zero */
369 if ((newpe = (krb5_enctype *) malloc(i*sizeof(krb5_enctype)))
375 memcpy(newpe, auth_context->permitted_etypes, i*sizeof(krb5_enctype));
380 krb5_error_code KRB5_CALLCONV
381 krb5_auth_con_set_checksum_func( krb5_context context,
382 krb5_auth_context auth_context,
383 krb5_mk_req_checksum_func func,
386 auth_context->checksum_func = func;
387 auth_context->checksum_func_data = data;
391 krb5_error_code KRB5_CALLCONV
392 krb5_auth_con_get_checksum_func( krb5_context context,
393 krb5_auth_context auth_context,
394 krb5_mk_req_checksum_func *func,
397 *func = auth_context->checksum_func;
398 *data = auth_context->checksum_func_data;
403 * krb5int_auth_con_chkseqnum
405 * We use a somewhat complex heuristic for validating received
406 * sequence numbers. We must accommodate both our older
407 * implementation, which sends negative sequence numbers, and the
408 * broken Heimdal implementation (at least as of 0.5.2), which
409 * violates X.690 BER for integer encodings. The requirement of
410 * handling negative sequence numbers removes one of easier means of
411 * detecting a Heimdal implementation, so we resort to this mess
414 * X.690 BER (and consequently DER, which are the required encoding
415 * rules in RFC1510) encode all integer types as signed integers.
416 * This means that the MSB being set on the first octet of the
417 * contents of the encoding indicates a negative value. Heimdal does
418 * not prepend the required zero octet to unsigned integer encodings
419 * which would otherwise have the MSB of the first octet of their
422 * Our ASN.1 library implements a special decoder for sequence
423 * numbers, accepting both negative and positive 32-bit numbers but
424 * mapping them both into the space of positive unsigned 32-bit
425 * numbers in the obvious bit-pattern-preserving way. This maintains
426 * compatibility with our older implementations. This also means that
427 * encodings emitted by Heimdal are ambiguous.
429 * Heimdal counter value received uint32 value
431 * 0x00000080 0xFFFFFF80
432 * 0x000000FF 0xFFFFFFFF
433 * 0x00008000 0xFFFF8000
434 * 0x0000FFFF 0xFFFFFFFF
435 * 0x00800000 0xFF800000
436 * 0x00FFFFFF 0xFFFFFFFF
437 * 0xFF800000 0xFF800000
438 * 0xFFFFFFFF 0xFFFFFFFF
440 * We use two auth_context flags, SANE_SEQ and HEIMDAL_SEQ, which are
441 * only set after we can unambiguously determine the sanity of the
442 * sending implementation. Once one of these flags is set, we accept
443 * only the sequence numbers appropriate to the remote implementation
444 * type. We can make the determination in two different ways. The
445 * first is to note the receipt of a "negative" sequence number when a
446 * "positive" one was expected. The second is to note the receipt of
447 * a sequence number that wraps through "zero" in a weird way. The
448 * latter corresponds to the receipt of an initial sequence number in
449 * the ambiguous range.
451 * There are 2^7 + 2^15 + 2^23 + 2^23 = 16810112 total ambiguous
452 * initial Heimdal counter values, but we receive them as one of 2^23
453 * possible values. There is a ~1/256 chance of a Heimdal
454 * implementation sending an intial sequence number in the ambiguous
457 * We have to do special treatment when receiving sequence numbers
458 * between 0xFF800000..0xFFFFFFFF, or when wrapping through zero
459 * weirdly (due to ambiguous initial sequence number). If we are
460 * expecting a value corresponding to an ambiguous Heimdal counter
461 * value, and we receive an exact match, we can mark the remote end as
465 krb5int_auth_con_chkseqnum(
467 krb5_auth_context ac,
472 exp_seq = ac->remote_seq_number;
475 * If sender is known to be sane, accept _only_ exact matches.
477 if (ac->auth_context_flags & KRB5_AUTH_CONN_SANE_SEQ)
478 return in_seq == exp_seq;
481 * If sender is not known to be sane, first check the ambiguous
482 * range of received values, 0xFF800000..0xFFFFFFFF.
484 if ((in_seq & 0xFF800000) == 0xFF800000) {
486 * If expected sequence number is in the range
487 * 0xFF800000..0xFFFFFFFF, then we can't make any
488 * determinations about the sanity of the sending
491 if ((exp_seq & 0xFF800000) == 0xFF800000 && in_seq == exp_seq)
494 * If sender is not known for certain to be a broken Heimdal
495 * implementation, check for exact match.
497 if (!(ac->auth_context_flags & KRB5_AUTH_CONN_HEIMDAL_SEQ)
498 && in_seq == exp_seq)
501 * Now apply hairy algorithm for matching sequence numbers
502 * sent by broken Heimdal implementations. If it matches, we
503 * know for certain it's a broken Heimdal sender.
505 if (chk_heimdal_seqnum(exp_seq, in_seq)) {
506 ac->auth_context_flags |= KRB5_AUTH_CONN_HEIMDAL_SEQ;
513 * Received value not in the ambiguous range? If the _expected_
514 * value is in the range of ambiguous Hemidal counter values, and
515 * it matches the received value, sender is known to be sane.
517 if (in_seq == exp_seq) {
518 if (( exp_seq & 0xFFFFFF80) == 0x00000080
519 || (exp_seq & 0xFFFF8000) == 0x00008000
520 || (exp_seq & 0xFF800000) == 0x00800000)
521 ac->auth_context_flags |= KRB5_AUTH_CONN_SANE_SEQ;
526 * Magic wraparound for the case where the intial sequence number
527 * is in the ambiguous range. This means that the sender's
528 * counter is at a different count than ours, so we correct ours,
529 * and mark the sender as being a broken Heimdal implementation.
532 && !(ac->auth_context_flags & KRB5_AUTH_CONN_HEIMDAL_SEQ)) {
537 ac->auth_context_flags |= KRB5_AUTH_CONN_HEIMDAL_SEQ;
548 chk_heimdal_seqnum(krb5_ui_4 exp_seq, krb5_ui_4 in_seq)
550 if (( exp_seq & 0xFF800000) == 0x00800000
551 && (in_seq & 0xFF800000) == 0xFF800000
552 && (in_seq & 0x00FFFFFF) == exp_seq)
554 else if (( exp_seq & 0xFFFF8000) == 0x00008000
555 && (in_seq & 0xFFFF8000) == 0xFFFF8000
556 && (in_seq & 0x0000FFFF) == exp_seq)
558 else if (( exp_seq & 0xFFFFFF80) == 0x00000080
559 && (in_seq & 0xFFFFFF80) == 0xFFFFFF80
560 && (in_seq & 0x000000FF) == exp_seq)
567 krb5_auth_con_get_subkey_enctype(krb5_context context,
568 krb5_auth_context auth_context,
571 *etype = auth_context->negotiated_etype;
575 krb5_error_code KRB5_CALLCONV
576 krb5_auth_con_get_authdata_context(krb5_context context,
577 krb5_auth_context auth_context,
578 krb5_authdata_context *ad_context)
580 *ad_context = auth_context->ad_context;
584 krb5_error_code KRB5_CALLCONV
585 krb5_auth_con_set_authdata_context(krb5_context context,
586 krb5_auth_context auth_context,
587 krb5_authdata_context ad_context)
589 auth_context->ad_context = ad_context;