make mark-cstyle
[krb5.git] / src / lib / krb5 / krb / auth_con.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 #include "k5-int.h"
3 #include "auth_con.h"
4
5 static krb5_boolean chk_heimdal_seqnum(krb5_ui_4, krb5_ui_4);
6
7 static krb5_error_code
8 actx_copy_addr(krb5_context context, const krb5_address *inad, krb5_address **outad)
9 {
10     krb5_address *tmpad;
11
12     if (!(tmpad = (krb5_address *)malloc(sizeof(*tmpad))))
13         return ENOMEM;
14     *tmpad = *inad;
15     if (!(tmpad->contents = (krb5_octet *)malloc(inad->length))) {
16         free(tmpad);
17         return ENOMEM;
18     }
19     memcpy(tmpad->contents, inad->contents, inad->length);
20     *outad = tmpad;
21     return 0;
22 }
23
24 krb5_error_code KRB5_CALLCONV
25 krb5_auth_con_init(krb5_context context, krb5_auth_context *auth_context)
26 {
27     *auth_context =
28         (krb5_auth_context)calloc(1, sizeof(struct _krb5_auth_context));
29     if (!*auth_context)
30         return ENOMEM;
31
32     /* Default flags, do time not seq */
33     (*auth_context)->auth_context_flags =
34         KRB5_AUTH_CONTEXT_DO_TIME |  KRB5_AUTH_CONN_INITIALIZED;
35
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;
42     return 0;
43 }
44
45 krb5_error_code KRB5_CALLCONV
46 krb5_auth_con_free(krb5_context context, krb5_auth_context auth_context)
47 {
48     if (auth_context == NULL)
49         return 0;
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);
72     free(auth_context);
73     return 0;
74 }
75
76 krb5_error_code
77 krb5_auth_con_setaddrs(krb5_context context, krb5_auth_context auth_context, krb5_address *local_addr, krb5_address *remote_addr)
78 {
79     krb5_error_code     retval;
80
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);
86
87     retval = 0;
88     if (local_addr)
89         retval = actx_copy_addr(context,
90                                 local_addr,
91                                 &auth_context->local_addr);
92     else
93         auth_context->local_addr = NULL;
94
95     if (!retval && remote_addr)
96         retval = actx_copy_addr(context,
97                                 remote_addr,
98                                 &auth_context->remote_addr);
99     else
100         auth_context->remote_addr = NULL;
101
102     return retval;
103 }
104
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)
107 {
108     krb5_error_code     retval;
109
110     retval = 0;
111     if (local_addr && auth_context->local_addr) {
112         retval = actx_copy_addr(context,
113                                 auth_context->local_addr,
114                                 local_addr);
115     }
116     if (!retval && (remote_addr) && auth_context->remote_addr) {
117         retval = actx_copy_addr(context,
118                                 auth_context->remote_addr,
119                                 remote_addr);
120     }
121     return retval;
122 }
123
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)
126 {
127     krb5_error_code     retval;
128
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);
134
135     retval = 0;
136     if (local_port)
137         retval = actx_copy_addr(context,
138                                 local_port,
139                                 &auth_context->local_port);
140     else
141         auth_context->local_port = NULL;
142
143     if (!retval && remote_port)
144         retval = actx_copy_addr(context,
145                                 remote_port,
146                                 &auth_context->remote_port);
147     else
148         auth_context->remote_port = NULL;
149
150     return retval;
151 }
152
153
154 /*
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.
160  */
161 krb5_error_code KRB5_CALLCONV
162 krb5_auth_con_setuseruserkey(krb5_context context, krb5_auth_context auth_context, krb5_keyblock *keyblock)
163 {
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)));
167 }
168
169 krb5_error_code KRB5_CALLCONV
170 krb5_auth_con_getkey(krb5_context context, krb5_auth_context auth_context, krb5_keyblock **keyblock)
171 {
172     if (auth_context->key)
173         return krb5_k_key_keyblock(context, auth_context->key, keyblock);
174     *keyblock = NULL;
175     return 0;
176 }
177
178 krb5_error_code KRB5_CALLCONV
179 krb5_auth_con_getlocalsubkey(krb5_context context, krb5_auth_context auth_context, krb5_keyblock **keyblock)
180 {
181     return krb5_auth_con_getsendsubkey(context, auth_context, keyblock);
182 }
183
184 krb5_error_code KRB5_CALLCONV
185 krb5_auth_con_getremotesubkey(krb5_context context, krb5_auth_context auth_context, krb5_keyblock **keyblock)
186 {
187     return krb5_auth_con_getrecvsubkey(context, auth_context, keyblock);
188 }
189
190 krb5_error_code KRB5_CALLCONV
191 krb5_auth_con_setsendsubkey(krb5_context ctx, krb5_auth_context ac, krb5_keyblock *keyblock)
192 {
193     if (ac->send_subkey != NULL)
194         krb5_k_free_key(ctx, ac->send_subkey);
195     ac->send_subkey = NULL;
196     if (keyblock !=NULL)
197         return krb5_k_create_key(ctx, keyblock, &ac->send_subkey);
198     else
199         return 0;
200 }
201
202 krb5_error_code KRB5_CALLCONV
203 krb5_auth_con_setrecvsubkey(krb5_context ctx, krb5_auth_context ac, krb5_keyblock *keyblock)
204 {
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);
210     else
211         return 0;
212 }
213
214 krb5_error_code KRB5_CALLCONV
215 krb5_auth_con_getsendsubkey(krb5_context ctx, krb5_auth_context ac, krb5_keyblock **keyblock)
216 {
217     if (ac->send_subkey != NULL)
218         return krb5_k_key_keyblock(ctx, ac->send_subkey, keyblock);
219     *keyblock = NULL;
220     return 0;
221 }
222
223 krb5_error_code KRB5_CALLCONV
224 krb5_auth_con_getrecvsubkey(krb5_context ctx, krb5_auth_context ac, krb5_keyblock **keyblock)
225 {
226     if (ac->recv_subkey != NULL)
227         return krb5_k_key_keyblock(ctx, ac->recv_subkey, keyblock);
228     *keyblock = NULL;
229     return 0;
230 }
231
232 krb5_error_code KRB5_CALLCONV
233 krb5_auth_con_set_req_cksumtype(krb5_context context, krb5_auth_context auth_context, krb5_cksumtype cksumtype)
234 {
235     auth_context->req_cksumtype = cksumtype;
236     return 0;
237 }
238
239 krb5_error_code
240 krb5_auth_con_set_safe_cksumtype(krb5_context context, krb5_auth_context auth_context, krb5_cksumtype cksumtype)
241 {
242     auth_context->safe_cksumtype = cksumtype;
243     return 0;
244 }
245
246 krb5_error_code KRB5_CALLCONV
247 krb5_auth_con_getlocalseqnumber(krb5_context context, krb5_auth_context auth_context, krb5_int32 *seqnumber)
248 {
249     *seqnumber = auth_context->local_seq_number;
250     return 0;
251 }
252 #ifndef LEAN_CLIENT
253 krb5_error_code KRB5_CALLCONV
254 krb5_auth_con_getauthenticator(krb5_context context, krb5_auth_context auth_context, krb5_authenticator **authenticator)
255 {
256     return (krb5_copy_authenticator(context, auth_context->authentp,
257                                     authenticator));
258 }
259 #endif
260
261 krb5_error_code KRB5_CALLCONV
262 krb5_auth_con_getremoteseqnumber(krb5_context context, krb5_auth_context auth_context, krb5_int32 *seqnumber)
263 {
264     *seqnumber = auth_context->remote_seq_number;
265     return 0;
266 }
267
268 krb5_error_code KRB5_CALLCONV
269 krb5_auth_con_initivector(krb5_context context, krb5_auth_context auth_context)
270 {
271     krb5_error_code ret;
272     krb5_enctype enctype;
273
274     if (auth_context->key) {
275         size_t blocksize;
276
277         enctype = krb5_k_key_enctype(context, auth_context->key);
278         if ((ret = krb5_c_block_size(context, enctype, &blocksize)))
279             return(ret);
280         if ((auth_context->i_vector = (krb5_pointer)calloc(1,blocksize))) {
281             return 0;
282         }
283         return ENOMEM;
284     }
285     return EINVAL; /* XXX need an error for no keyblock */
286 }
287
288 krb5_error_code
289 krb5_auth_con_setivector(krb5_context context, krb5_auth_context auth_context, krb5_pointer ivector)
290 {
291     auth_context->i_vector = ivector;
292     return 0;
293 }
294
295 krb5_error_code
296 krb5_auth_con_getivector(krb5_context context, krb5_auth_context auth_context, krb5_pointer *ivector)
297 {
298     *ivector = auth_context->i_vector;
299     return 0;
300 }
301
302 krb5_error_code KRB5_CALLCONV
303 krb5_auth_con_setflags(krb5_context context, krb5_auth_context auth_context, krb5_int32 flags)
304 {
305     auth_context->auth_context_flags = flags;
306     return 0;
307 }
308
309 krb5_error_code KRB5_CALLCONV
310 krb5_auth_con_getflags(krb5_context context, krb5_auth_context auth_context, krb5_int32 *flags)
311 {
312     *flags = auth_context->auth_context_flags;
313     return 0;
314 }
315
316 krb5_error_code KRB5_CALLCONV
317 krb5_auth_con_setrcache(krb5_context context, krb5_auth_context auth_context, krb5_rcache rcache)
318 {
319     auth_context->rcache = rcache;
320     return 0;
321 }
322
323 krb5_error_code
324 krb5_auth_con_getrcache(krb5_context context, krb5_auth_context auth_context, krb5_rcache *rcache)
325 {
326     *rcache = auth_context->rcache;
327     return 0;
328 }
329
330 krb5_error_code
331 krb5_auth_con_setpermetypes(krb5_context context, krb5_auth_context auth_context, const krb5_enctype *permetypes)
332 {
333     krb5_enctype        * newpe;
334     int i;
335
336     for (i=0; permetypes[i]; i++)
337         ;
338     i++; /* include the zero */
339
340     if ((newpe = (krb5_enctype *) malloc(i*sizeof(krb5_enctype)))
341         == NULL)
342         return(ENOMEM);
343
344     if (auth_context->permitted_etypes)
345         free(auth_context->permitted_etypes);
346
347     auth_context->permitted_etypes = newpe;
348
349     memcpy(newpe, permetypes, i*sizeof(krb5_enctype));
350
351     return 0;
352 }
353
354 krb5_error_code
355 krb5_auth_con_getpermetypes(krb5_context context, krb5_auth_context auth_context, krb5_enctype **permetypes)
356 {
357     krb5_enctype        * newpe;
358     int i;
359
360     if (! auth_context->permitted_etypes) {
361         *permetypes = NULL;
362         return(0);
363     }
364
365     for (i=0; auth_context->permitted_etypes[i]; i++)
366         ;
367     i++; /* include the zero */
368
369     if ((newpe = (krb5_enctype *) malloc(i*sizeof(krb5_enctype)))
370         == NULL)
371         return(ENOMEM);
372
373     *permetypes = newpe;
374
375     memcpy(newpe, auth_context->permitted_etypes, i*sizeof(krb5_enctype));
376
377     return(0);
378 }
379
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,
384                                  void *data)
385 {
386     auth_context->checksum_func = func;
387     auth_context->checksum_func_data = data;
388     return 0;
389 }
390
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,
395                                  void **data)
396 {
397     *func = auth_context->checksum_func;
398     *data = auth_context->checksum_func_data;
399     return 0;
400 }
401
402 /*
403  * krb5int_auth_con_chkseqnum
404  *
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
412  * here.
413  *
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
420  * encodings set.
421  *
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.
428  *
429  * Heimdal counter value        received uint32 value
430  *
431  * 0x00000080                   0xFFFFFF80
432  * 0x000000FF                   0xFFFFFFFF
433  * 0x00008000                   0xFFFF8000
434  * 0x0000FFFF                   0xFFFFFFFF
435  * 0x00800000                   0xFF800000
436  * 0x00FFFFFF                   0xFFFFFFFF
437  * 0xFF800000                   0xFF800000
438  * 0xFFFFFFFF                   0xFFFFFFFF
439  *
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.
450  *
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
455  * range.
456  *
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
462  * sane.
463  */
464 krb5_boolean
465 krb5int_auth_con_chkseqnum(
466     krb5_context ctx,
467     krb5_auth_context ac,
468     krb5_ui_4 in_seq)
469 {
470     krb5_ui_4 exp_seq;
471
472     exp_seq = ac->remote_seq_number;
473
474     /*
475      * If sender is known to be sane, accept _only_ exact matches.
476      */
477     if (ac->auth_context_flags & KRB5_AUTH_CONN_SANE_SEQ)
478         return in_seq == exp_seq;
479
480     /*
481      * If sender is not known to be sane, first check the ambiguous
482      * range of received values, 0xFF800000..0xFFFFFFFF.
483      */
484     if ((in_seq & 0xFF800000) == 0xFF800000) {
485         /*
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
489          * implementation.
490          */
491         if ((exp_seq & 0xFF800000) == 0xFF800000 && in_seq == exp_seq)
492             return 1;
493         /*
494          * If sender is not known for certain to be a broken Heimdal
495          * implementation, check for exact match.
496          */
497         if (!(ac->auth_context_flags & KRB5_AUTH_CONN_HEIMDAL_SEQ)
498             && in_seq == exp_seq)
499             return 1;
500         /*
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.
504          */
505         if (chk_heimdal_seqnum(exp_seq, in_seq)) {
506             ac->auth_context_flags |= KRB5_AUTH_CONN_HEIMDAL_SEQ;
507             return 1;
508         }
509         return 0;
510     }
511
512     /*
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.
516      */
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;
522         return 1;
523     }
524
525     /*
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.
530      */
531     if (exp_seq == 0
532         && !(ac->auth_context_flags & KRB5_AUTH_CONN_HEIMDAL_SEQ)) {
533         switch (in_seq) {
534         case 0x100:
535         case 0x10000:
536         case 0x1000000:
537             ac->auth_context_flags |= KRB5_AUTH_CONN_HEIMDAL_SEQ;
538             exp_seq = in_seq;
539             return 1;
540         default:
541             return 0;
542         }
543     }
544     return 0;
545 }
546
547 static krb5_boolean
548 chk_heimdal_seqnum(krb5_ui_4 exp_seq, krb5_ui_4 in_seq)
549 {
550     if (( exp_seq & 0xFF800000) == 0x00800000
551         && (in_seq & 0xFF800000) == 0xFF800000
552         && (in_seq & 0x00FFFFFF) == exp_seq)
553         return 1;
554     else if ((  exp_seq & 0xFFFF8000) == 0x00008000
555              && (in_seq & 0xFFFF8000) == 0xFFFF8000
556              && (in_seq & 0x0000FFFF) == exp_seq)
557         return 1;
558     else if ((  exp_seq & 0xFFFFFF80) == 0x00000080
559              && (in_seq & 0xFFFFFF80) == 0xFFFFFF80
560              && (in_seq & 0x000000FF) == exp_seq)
561         return 1;
562     else
563         return 0;
564 }
565
566 krb5_error_code
567 krb5_auth_con_get_subkey_enctype(krb5_context context,
568                                  krb5_auth_context auth_context,
569                                  krb5_enctype *etype)
570 {
571     *etype = auth_context->negotiated_etype;
572     return 0;
573 }
574
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)
579 {
580     *ad_context = auth_context->ad_context;
581     return 0;
582 }
583
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)
588 {
589     auth_context->ad_context = ad_context;
590     return 0;
591 }