/
kdc_preauth.c
2416 lines (2209 loc) · 80 KB
/
kdc_preauth.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* kdc/kdc_preauth.c
*
* Copyright 1995, 2003, 2007, 2009 by the Massachusetts Institute of Technology.
* All Rights Reserved.
*
* Export of this software from the United States of America may
* require a specific license from the United States Government.
* It is the responsibility of any person or organization contemplating
* export to obtain such a license before exporting.
*
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
* distribute this software and its documentation for any purpose and
* without fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation, and that
* the name of M.I.T. not be used in advertising or publicity pertaining
* to distribution of the software without specific, written prior
* permission. Furthermore if you modify this software you must label
* your software as modified software and not distribute it in such a
* fashion that it might be confused with the original M.I.T. software.
* M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is" without express
* or implied warranty.
*
* Preauthentication routines for the KDC.
*/
/*
* Copyright (C) 1998 by the FundsXpress, INC.
*
* All rights reserved.
*
* Export of this software from the United States of America may require
* a specific license from the United States Government. It is the
* responsibility of any person or organization contemplating export to
* obtain such a license before exporting.
*
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
* distribute this software and its documentation for any purpose and
* without fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation, and that
* the name of FundsXpress. not be used in advertising or publicity pertaining
* to distribution of the software without specific, written prior
* permission. FundsXpress makes no representations about the suitability of
* this software for any purpose. It is provided "as is" without express
* or implied warranty.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
* Copyright (c) 2006-2008, Novell, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The copyright holder's name is not used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "k5-int.h"
#include "kdc_util.h"
#include "extern.h"
#include <stdio.h>
#include "adm_proto.h"
#if APPLE_PKINIT
#include "pkinit_server.h"
#include "pkinit_cert_store.h"
#endif /* APPLE_PKINIT */
#include <syslog.h>
#include <assert.h>
#include "../include/krb5/preauth_plugin.h"
#if TARGET_OS_MAC
static const char *objdirs[] = { KRB5_PLUGIN_BUNDLE_DIR,
LIBDIR "/krb5/plugins/preauth",
NULL }; /* should be a list */
#else
static const char *objdirs[] = { LIBDIR "/krb5/plugins/preauth", NULL };
#endif
/* XXX This is ugly and should be in a header file somewhere */
#ifndef KRB5INT_DES_TYPES_DEFINED
#define KRB5INT_DES_TYPES_DEFINED
typedef unsigned char des_cblock[8]; /* crypto-block size */
#endif
typedef des_cblock mit_des_cblock;
extern void mit_des_fixup_key_parity (mit_des_cblock );
extern int mit_des_is_weak_key (mit_des_cblock );
typedef struct _krb5_preauth_systems {
const char *name;
int type;
int flags;
void *plugin_context;
preauth_server_init_proc init;
preauth_server_fini_proc fini;
preauth_server_edata_proc get_edata;
preauth_server_verify_proc verify_padata;
preauth_server_return_proc return_padata;
preauth_server_free_reqcontext_proc free_pa_reqctx;
} krb5_preauth_systems;
static krb5_error_code
verify_enc_timestamp (krb5_context, krb5_db_entry *client,
krb5_data *req_pkt,
krb5_kdc_req *request,
krb5_enc_tkt_part * enc_tkt_reply, krb5_pa_data *data,
preauth_get_entry_data_proc get_entry_data,
void *pa_system_context,
void **pa_request_context,
krb5_data **e_data,
krb5_authdata ***authz_data);
static krb5_error_code
get_enc_ts(krb5_context, krb5_kdc_req *request,
krb5_db_entry *client, krb5_db_entry *server,
preauth_get_entry_data_proc get_entry_data,
void *pa_system_context,
krb5_pa_data *data);
static krb5_error_code
get_etype_info(krb5_context, krb5_kdc_req *request,
krb5_db_entry *client, krb5_db_entry *server,
preauth_get_entry_data_proc get_entry_data,
void *pa_system_context,
krb5_pa_data *data);
static krb5_error_code
get_etype_info2(krb5_context context, krb5_kdc_req *request,
krb5_db_entry *client, krb5_db_entry *server,
preauth_get_entry_data_proc get_entry_data,
void *pa_system_context,
krb5_pa_data *pa_data);
static krb5_error_code
etype_info_as_rep_helper(krb5_context context, krb5_pa_data * padata,
krb5_db_entry *client,
krb5_kdc_req *request, krb5_kdc_rep *reply,
krb5_key_data *client_key,
krb5_keyblock *encrypting_key,
krb5_pa_data **send_pa,
int etype_info2);
static krb5_error_code
return_etype_info(krb5_context, krb5_pa_data * padata,
krb5_db_entry *client,
krb5_data *req_pkt,
krb5_kdc_req *request, krb5_kdc_rep *reply,
krb5_key_data *client_key,
krb5_keyblock *encrypting_key,
krb5_pa_data **send_pa,
preauth_get_entry_data_proc get_entry_data,
void *pa_system_context,
void **pa_request_context);
static krb5_error_code
return_etype_info2(krb5_context, krb5_pa_data * padata,
krb5_db_entry *client,
krb5_data *req_pkt,
krb5_kdc_req *request, krb5_kdc_rep *reply,
krb5_key_data *client_key,
krb5_keyblock *encrypting_key,
krb5_pa_data **send_pa,
preauth_get_entry_data_proc get_entry_data,
void *pa_system_context,
void **pa_request_context);
static krb5_error_code
return_pw_salt(krb5_context, krb5_pa_data * padata,
krb5_db_entry *client,
krb5_data *req_pkt,
krb5_kdc_req *request, krb5_kdc_rep *reply,
krb5_key_data *client_key,
krb5_keyblock *encrypting_key,
krb5_pa_data **send_pa,
preauth_get_entry_data_proc get_entry_data,
void *pa_system_context,
void **pa_request_context);
#if APPLE_PKINIT
/* PKINIT preauth support */
static krb5_error_code get_pkinit_edata(
krb5_context context,
krb5_kdc_req *request,
krb5_db_entry *client,
krb5_db_entry *server,
preauth_get_entry_data_proc get_entry_data,
void *pa_module_context,
krb5_pa_data *pa_data);
static krb5_error_code verify_pkinit_request(
krb5_context context,
krb5_db_entry *client,
krb5_data *req_pkt,
krb5_kdc_req *request,
krb5_enc_tkt_part *enc_tkt_reply,
krb5_pa_data *data,
preauth_get_entry_data_proc get_entry_data,
void *pa_module_context,
void **pa_request_context,
krb5_data **e_data,
krb5_authdata ***authz_data);
static krb5_error_code return_pkinit_response(
krb5_context context,
krb5_pa_data * padata,
krb5_db_entry *client,
krb5_data *req_pkt,
krb5_kdc_req *request,
krb5_kdc_rep *reply,
krb5_key_data *client_key,
krb5_keyblock *encrypting_key,
krb5_pa_data **send_pa,
preauth_get_entry_data_proc get_entry_data,
void *pa_module_context,
void **pa_request_context);
#endif /* APPLE_PKINIT */
static krb5_preauth_systems static_preauth_systems[] = {
#if APPLE_PKINIT
{
"pkinit",
KRB5_PADATA_PK_AS_REQ,
PA_SUFFICIENT,
NULL, /* pa_sys_context */
NULL, /* init */
NULL, /* fini */
get_pkinit_edata,
verify_pkinit_request,
return_pkinit_response,
NULL /* free_pa_request_context */
},
#endif /* APPLE_PKINIT */
{
"timestamp",
KRB5_PADATA_ENC_TIMESTAMP,
0,
NULL,
NULL,
NULL,
get_enc_ts,
verify_enc_timestamp,
0
},
{
"FAST",
KRB5_PADATA_FX_FAST,
PA_HARDWARE,
NULL,
NULL,
NULL,
NULL,
NULL,
0
},
{
"etype-info",
KRB5_PADATA_ETYPE_INFO,
0,
NULL,
NULL,
NULL,
get_etype_info,
0,
return_etype_info
},
{
"etype-info2",
KRB5_PADATA_ETYPE_INFO2,
0,
NULL,
NULL,
NULL,
get_etype_info2,
0,
return_etype_info2
},
{
"pw-salt",
KRB5_PADATA_PW_SALT,
PA_PSEUDO, /* Don't include this in the error list */
NULL,
NULL,
NULL,
0,
0,
return_pw_salt
},
{
"pac-request",
KRB5_PADATA_PAC_REQUEST,
PA_PSEUDO,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
},
#if 0
{
"server-referral",
KRB5_PADATA_SERVER_REFERRAL,
PA_PSEUDO,
0,
0,
return_server_referral
},
#endif
{ "[end]", -1,}
};
static krb5_preauth_systems *preauth_systems;
static int n_preauth_systems;
static struct plugin_dir_handle preauth_plugins;
/* Open plugin directories for preauth modules. */
static krb5_error_code
open_preauth_plugin_dirs(krb5_context ctx)
{
static const char *path[] = {
KRB5_CONF_LIBDEFAULTS, KRB5_CONF_PREAUTH_MODULE_DIR, NULL,
};
char **profpath = NULL;
const char **dirs;
krb5_error_code ret;
ret = profile_get_values(ctx->profile, path, &profpath);
if (ret != 0 && ret != PROF_NO_RELATION)
return ret;
dirs = (profpath != NULL) ? (const char **) profpath : objdirs;
ret = krb5int_open_plugin_dirs(dirs, NULL, &preauth_plugins, &ctx->err);
profile_free_list(profpath);
return ret;
}
krb5_error_code
load_preauth_plugins(krb5_context context)
{
void **preauth_plugins_ftables;
struct krb5plugin_preauth_server_ftable_v1 *ftable;
size_t module_count, i, j, k;
void *plugin_context;
preauth_server_init_proc server_init_proc = NULL;
char **kdc_realm_names = NULL;
/* Attempt to load all of the preauth plugins we can find. */
PLUGIN_DIR_INIT(&preauth_plugins);
if (PLUGIN_DIR_OPEN(&preauth_plugins) == 0) {
if (open_preauth_plugin_dirs(context) != 0)
return KRB5_PLUGIN_NO_HANDLE;
}
/* Get the method tables provided by the loaded plugins. */
preauth_plugins_ftables = NULL;
if (krb5int_get_plugin_dir_data(&preauth_plugins,
"preauthentication_server_1",
&preauth_plugins_ftables, &context->err) != 0) {
return KRB5_PLUGIN_NO_HANDLE;
}
/* Count the valid modules. */
module_count = sizeof(static_preauth_systems)
/ sizeof(static_preauth_systems[0]);
if (preauth_plugins_ftables != NULL) {
for (i = 0; preauth_plugins_ftables[i] != NULL; i++) {
ftable = preauth_plugins_ftables[i];
if ((ftable->flags_proc == NULL) &&
(ftable->edata_proc == NULL) &&
(ftable->verify_proc == NULL) &&
(ftable->return_proc == NULL)) {
continue;
}
for (j = 0;
ftable->pa_type_list != NULL &&
ftable->pa_type_list[j] > 0;
j++) {
module_count++;
}
}
}
/* Build the complete list of supported preauthentication options, and
* leave room for a terminator entry. */
preauth_systems = malloc(sizeof(krb5_preauth_systems) * (module_count + 1));
if (preauth_systems == NULL) {
krb5int_free_plugin_dir_data(preauth_plugins_ftables);
return ENOMEM;
}
/* Build a list of the names of the supported realms for this KDC.
* The list of names is terminated with a NULL. */
kdc_realm_names = malloc(sizeof(char *) * (kdc_numrealms + 1));
if (kdc_realm_names == NULL) {
krb5int_free_plugin_dir_data(preauth_plugins_ftables);
return ENOMEM;
}
for (i = 0; i < (size_t)kdc_numrealms; i++) {
kdc_realm_names[i] = kdc_realmlist[i]->realm_name;
}
kdc_realm_names[i] = NULL;
/* Add the locally-supplied mechanisms to the dynamic list first. */
for (i = 0, k = 0;
i < sizeof(static_preauth_systems) / sizeof(static_preauth_systems[0]);
i++) {
if (static_preauth_systems[i].type == -1)
break;
preauth_systems[k] = static_preauth_systems[i];
/* Try to initialize the preauth system. If it fails, we'll remove it
* from the list of systems we'll be using. */
plugin_context = NULL;
server_init_proc = static_preauth_systems[i].init;
if ((server_init_proc != NULL) &&
((*server_init_proc)(context, &plugin_context,
(const char **)kdc_realm_names) != 0)) {
memset(&preauth_systems[k], 0, sizeof(preauth_systems[k]));
continue;
}
preauth_systems[k].plugin_context = plugin_context;
k++;
}
/* Now add the dynamically-loaded mechanisms to the list. */
if (preauth_plugins_ftables != NULL) {
for (i = 0; preauth_plugins_ftables[i] != NULL; i++) {
ftable = preauth_plugins_ftables[i];
if ((ftable->flags_proc == NULL) &&
(ftable->edata_proc == NULL) &&
(ftable->verify_proc == NULL) &&
(ftable->return_proc == NULL)) {
continue;
}
plugin_context = NULL;
for (j = 0;
ftable->pa_type_list != NULL &&
ftable->pa_type_list[j] > 0;
j++) {
/* Try to initialize the plugin. If it fails, we'll remove it
* from the list of modules we'll be using. */
if (j == 0) {
server_init_proc = ftable->init_proc;
if (server_init_proc != NULL) {
krb5_error_code initerr;
initerr = (*server_init_proc)(context, &plugin_context,
(const char **)kdc_realm_names);
if (initerr) {
const char *emsg;
emsg = krb5_get_error_message(context, initerr);
if (emsg) {
krb5_klog_syslog(LOG_ERR,
"preauth %s failed to initialize: %s",
ftable->name, emsg);
krb5_free_error_message(context, emsg);
}
memset(&preauth_systems[k], 0,
sizeof(preauth_systems[k]));
break; /* skip all modules in this plugin */
}
}
}
preauth_systems[k].name = ftable->name;
preauth_systems[k].type = ftable->pa_type_list[j];
if (ftable->flags_proc != NULL)
preauth_systems[k].flags = ftable->flags_proc(context,
preauth_systems[k].type);
else
preauth_systems[k].flags = 0;
preauth_systems[k].plugin_context = plugin_context;
preauth_systems[k].init = server_init_proc;
/* Only call fini once for each plugin */
if (j == 0)
preauth_systems[k].fini = ftable->fini_proc;
else
preauth_systems[k].fini = NULL;
preauth_systems[k].get_edata = ftable->edata_proc;
preauth_systems[k].verify_padata = ftable->verify_proc;
preauth_systems[k].return_padata = ftable->return_proc;
preauth_systems[k].free_pa_reqctx =
ftable->freepa_reqcontext_proc;
k++;
}
}
krb5int_free_plugin_dir_data(preauth_plugins_ftables);
}
free(kdc_realm_names);
n_preauth_systems = k;
/* Add the end-of-list marker. */
preauth_systems[k].name = "[end]";
preauth_systems[k].type = -1;
return 0;
}
krb5_error_code
unload_preauth_plugins(krb5_context context)
{
int i;
if (preauth_systems != NULL) {
for (i = 0; i < n_preauth_systems; i++) {
if (preauth_systems[i].fini != NULL) {
(*preauth_systems[i].fini)(context,
preauth_systems[i].plugin_context);
}
memset(&preauth_systems[i], 0, sizeof(preauth_systems[i]));
}
free(preauth_systems);
preauth_systems = NULL;
n_preauth_systems = 0;
krb5int_close_plugin_dirs(&preauth_plugins);
}
return 0;
}
/*
* The make_padata_context() function creates a space for storing any context
* information which will be needed by return_padata() later. Each preauth
* type gets a context storage location of its own.
*/
struct request_pa_context {
int n_contexts;
struct {
krb5_preauth_systems *pa_system;
void *pa_context;
} *contexts;
};
static krb5_error_code
make_padata_context(krb5_context context, void **padata_context)
{
int i;
struct request_pa_context *ret;
ret = malloc(sizeof(*ret));
if (ret == NULL) {
return ENOMEM;
}
ret->n_contexts = n_preauth_systems;
ret->contexts = malloc(sizeof(ret->contexts[0]) * ret->n_contexts);
if (ret->contexts == NULL) {
free(ret);
return ENOMEM;
}
memset(ret->contexts, 0, sizeof(ret->contexts[0]) * ret->n_contexts);
for (i = 0; i < ret->n_contexts; i++) {
ret->contexts[i].pa_system = &preauth_systems[i];
ret->contexts[i].pa_context = NULL;
}
*padata_context = ret;
return 0;
}
/*
* The free_padata_context function frees any context information pointers
* which the check_padata() function created but which weren't already cleaned
* up by return_padata().
*/
krb5_error_code
free_padata_context(krb5_context kcontext, void **padata_context)
{
struct request_pa_context *context;
krb5_preauth_systems *preauth_system;
void **pctx, *mctx;
int i;
if (padata_context == NULL)
return 0;
context = *padata_context;
for (i = 0; i < context->n_contexts; i++) {
if (context->contexts[i].pa_context != NULL) {
preauth_system = context->contexts[i].pa_system;
mctx = preauth_system->plugin_context;
if (preauth_system->free_pa_reqctx != NULL) {
pctx = &context->contexts[i].pa_context;
(*preauth_system->free_pa_reqctx)(kcontext, mctx, pctx);
}
context->contexts[i].pa_context = NULL;
}
}
free(context->contexts);
free(context);
return 0;
}
/* Retrieve a specified tl_data item from the given entry, and return its
* contents in a new krb5_data, which must be freed by the caller.
*/
static krb5_error_code
get_entry_tl_data(krb5_context context, krb5_db_entry *entry,
krb5_int16 tl_data_type, krb5_data **result)
{
krb5_tl_data *tl;
for (tl = entry->tl_data; tl != NULL; tl = tl->tl_data_next) {
if (tl->tl_data_type == tl_data_type) {
*result = malloc(sizeof(krb5_data));
if (*result == NULL) {
return ENOMEM;
}
(*result)->magic = KV5M_DATA;
(*result)->data = malloc(tl->tl_data_length);
if ((*result)->data == NULL) {
free(*result);
*result = NULL;
return ENOMEM;
}
memcpy((*result)->data, tl->tl_data_contents, tl->tl_data_length);
return 0;
}
}
return ENOENT;
}
/*
* Retrieve a specific piece of information pertaining to the entry or the
* request and return it in a new krb5_data item which the caller must free.
*
* This may require massaging data into a contrived format, but it will
* hopefully keep us from having to reveal library-internal functions to
* modules.
*/
static krb5_error_code
get_entry_data(krb5_context context,
krb5_kdc_req *request, krb5_db_entry *entry,
krb5_int32 type,
krb5_data **result)
{
int i, k;
krb5_data *ret;
krb5_deltat *delta;
krb5_keyblock *keys;
krb5_key_data *entry_key;
krb5_error_code error;
struct kdc_request_state *state = request->kdc_state;
switch (type) {
case krb5plugin_preauth_entry_request_certificate:
return get_entry_tl_data(context, entry,
KRB5_TL_USER_CERTIFICATE, result);
break;
case krb5plugin_preauth_entry_max_time_skew:
ret = malloc(sizeof(krb5_data));
if (ret == NULL)
return ENOMEM;
delta = malloc(sizeof(krb5_deltat));
if (delta == NULL) {
free(ret);
return ENOMEM;
}
*delta = context->clockskew;
ret->data = (char *) delta;
ret->length = sizeof(*delta);
*result = ret;
return 0;
break;
case krb5plugin_preauth_keys:
ret = malloc(sizeof(krb5_data));
if (ret == NULL)
return ENOMEM;
keys = malloc(sizeof(krb5_keyblock) * (request->nktypes + 1));
if (keys == NULL) {
free(ret);
return ENOMEM;
}
ret->data = (char *) keys;
ret->length = sizeof(krb5_keyblock) * (request->nktypes + 1);
memset(ret->data, 0, ret->length);
k = 0;
for (i = 0; i < request->nktypes; i++) {
entry_key = NULL;
if (krb5_dbe_find_enctype(context, entry, request->ktype[i],
-1, 0, &entry_key) != 0)
continue;
if (krb5_dbe_decrypt_key_data(context, NULL, entry_key,
&keys[k], NULL) != 0) {
if (keys[k].contents != NULL)
krb5_free_keyblock_contents(context, &keys[k]);
memset(&keys[k], 0, sizeof(keys[k]));
continue;
}
k++;
}
if (k > 0) {
*result = ret;
return 0;
} else {
free(keys);
free(ret);
}
break;
case krb5plugin_preauth_request_body:
ret = NULL;
encode_krb5_kdc_req_body(request, &ret);
if (ret != NULL) {
*result = ret;
return 0;
}
return ASN1_PARSE_ERROR;
break;
case krb5plugin_preauth_fast_armor:
ret = calloc(1, sizeof(krb5_data));
if (ret == NULL)
return ENOMEM;
if (state->armor_key == NULL) {
*result = ret;
return 0;
}
error = krb5_copy_keyblock(context, state->armor_key, &keys);
if (error == 0) {
ret->data = (char *) keys;
ret->length = sizeof(krb5_keyblock);
*result = ret;
return 0;
}
free(ret);
return error;
case krb5plugin_preauth_free_fast_armor:
if ((*result)->data) {
keys = (krb5_keyblock *) (*result)->data;
krb5_free_keyblock(context, keys);
}
free(*result);
return 0;
default:
break;
}
return ENOENT;
}
static krb5_error_code
find_pa_system(int type, krb5_preauth_systems **preauth)
{
krb5_preauth_systems *ap;
ap = preauth_systems ? preauth_systems : static_preauth_systems;
while ((ap->type != -1) && (ap->type != type))
ap++;
if (ap->type == -1)
return(KRB5_PREAUTH_BAD_TYPE);
*preauth = ap;
return 0;
}
static krb5_error_code
find_pa_context(krb5_preauth_systems *pa_sys,
struct request_pa_context *context,
void ***pa_context)
{
int i;
*pa_context = 0;
if (context == NULL)
return KRB5KRB_ERR_GENERIC;
for (i = 0; i < context->n_contexts; i++) {
if (context->contexts[i].pa_system == pa_sys) {
*pa_context = &context->contexts[i].pa_context;
return 0;
}
}
return KRB5KRB_ERR_GENERIC;
}
/*
* Create a list of indices into the preauth_systems array, sorted by order of
* preference.
*/
static krb5_boolean
pa_list_includes(krb5_pa_data **pa_data, krb5_preauthtype pa_type)
{
while (*pa_data != NULL) {
if ((*pa_data)->pa_type == pa_type)
return TRUE;
pa_data++;
}
return FALSE;
}
static void
sort_pa_order(krb5_context context, krb5_kdc_req *request, int *pa_order)
{
int i, j, k, n_repliers, n_key_replacers;
/* First, set up the default order. */
i = 0;
for (j = 0; j < n_preauth_systems; j++) {
if (preauth_systems[j].return_padata != NULL)
pa_order[i++] = j;
}
n_repliers = i;
pa_order[n_repliers] = -1;
/* Reorder so that PA_REPLACES_KEY modules are listed first. */
for (i = 0; i < n_repliers; i++) {
/* If this module replaces the key, then it's okay to leave it where it
* is in the order. */
if (preauth_systems[pa_order[i]].flags & PA_REPLACES_KEY)
continue;
/* If not, search for a module which does, and swap in the first one we
* find. */
for (j = i + 1; j < n_repliers; j++) {
if (preauth_systems[pa_order[j]].flags & PA_REPLACES_KEY) {
k = pa_order[j];
pa_order[j] = pa_order[i];
pa_order[i] = k;
break;
}
}
}
if (request->padata != NULL) {
/* Now reorder the subset of modules which replace the key,
* bubbling those which handle pa_data types provided by the
* client ahead of the others.
*/
for (i = 0; preauth_systems[pa_order[i]].flags & PA_REPLACES_KEY; i++) {
continue;
}
n_key_replacers = i;
for (i = 0; i < n_key_replacers; i++) {
if (pa_list_includes(request->padata,
preauth_systems[pa_order[i]].type))
continue;
for (j = i + 1; j < n_key_replacers; j++) {
if (pa_list_includes(request->padata,
preauth_systems[pa_order[j]].type)) {
k = pa_order[j];
pa_order[j] = pa_order[i];
pa_order[i] = k;
break;
}
}
}
}
#ifdef DEBUG
krb5_klog_syslog(LOG_DEBUG, "original preauth mechanism list:");
for (i = 0; i < n_preauth_systems; i++) {
if (preauth_systems[i].return_padata != NULL)
krb5_klog_syslog(LOG_DEBUG, "... %s(%d)", preauth_systems[i].name,
preauth_systems[i].type);
}
krb5_klog_syslog(LOG_DEBUG, "sorted preauth mechanism list:");
for (i = 0; pa_order[i] != -1; i++) {
krb5_klog_syslog(LOG_DEBUG, "... %s(%d)",
preauth_systems[pa_order[i]].name,
preauth_systems[pa_order[i]].type);
}
#endif
}
const char *missing_required_preauth(krb5_db_entry *client,
krb5_db_entry *server,
krb5_enc_tkt_part *enc_tkt_reply)
{
#if 0
/*
* If this is the pwchange service, and the pre-auth bit is set,
* allow it even if the HW preauth would normally be required.
*
* Sandia national labs wanted this for some strange reason... we
* leave it disabled normally.
*/
if (isflagset(server->attributes, KRB5_KDB_PWCHANGE_SERVICE) &&
isflagset(enc_tkt_reply->flags, TKT_FLG_PRE_AUTH))
return 0;
#endif
#ifdef DEBUG
krb5_klog_syslog (
LOG_DEBUG,
"client needs %spreauth, %shw preauth; request has %spreauth, %shw preauth",
isflagset (client->attributes, KRB5_KDB_REQUIRES_PRE_AUTH) ? "" : "no ",
isflagset (client->attributes, KRB5_KDB_REQUIRES_HW_AUTH) ? "" : "no ",
isflagset (enc_tkt_reply->flags, TKT_FLG_PRE_AUTH) ? "" : "no ",
isflagset (enc_tkt_reply->flags, TKT_FLG_HW_AUTH) ? "" : "no ");
#endif
if (isflagset(client->attributes, KRB5_KDB_REQUIRES_PRE_AUTH) &&
!isflagset(enc_tkt_reply->flags, TKT_FLG_PRE_AUTH))
return "NEEDED_PREAUTH";
if (isflagset(client->attributes, KRB5_KDB_REQUIRES_HW_AUTH) &&
!isflagset(enc_tkt_reply->flags, TKT_FLG_HW_AUTH))
return "NEEDED_HW_PREAUTH";
return 0;
}
void
get_preauth_hint_list(krb5_kdc_req *request, krb5_db_entry *client,
krb5_db_entry *server, krb5_data *e_data)
{
int hw_only;
krb5_preauth_systems *ap;
krb5_pa_data **pa_data, **pa;
krb5_data *edat;
krb5_error_code retval;
/* Zero these out in case we need to abort */
e_data->length = 0;
e_data->data = 0;
hw_only = isflagset(client->attributes, KRB5_KDB_REQUIRES_HW_AUTH);
/* Allocate two extra entries for the cookie and the terminator. */
pa_data = calloc(n_preauth_systems + 2, sizeof(krb5_pa_data *));
if (pa_data == 0)
return;
pa = pa_data;
for (ap = preauth_systems; ap->type != -1; ap++) {
if (hw_only && !(ap->flags & PA_HARDWARE))
continue;
if (ap->flags & PA_PSEUDO)
continue;
*pa = malloc(sizeof(krb5_pa_data));
if (*pa == 0)
goto errout;
memset(*pa, 0, sizeof(krb5_pa_data));
(*pa)->magic = KV5M_PA_DATA;
(*pa)->pa_type = ap->type;
if (ap->get_edata) {
retval = (ap->get_edata)(kdc_context, request, client, server,
get_entry_data, ap->plugin_context, *pa);
if (retval) {
/* just failed on this type, continue */
free(*pa);
*pa = 0;
continue;
}
}
pa++;
}
if (pa_data[0] == 0) {
krb5_klog_syslog (LOG_INFO,
"%spreauth required but hint list is empty",
hw_only ? "hw" : "");
}
/*
* If we fail to get the cookie it is probably
* still reasonable to continue with the response
*/
kdc_preauth_get_cookie(request->kdc_state, pa);
retval = encode_krb5_padata_sequence(pa_data, &edat);
if (retval)
goto errout;
*e_data = *edat;
free(edat);
errout:
krb5_free_pa_data(kdc_context, pa_data);
return;
}
/*
* Add authorization data returned from preauth modules to the ticket
* It is assumed that ad is a "null-terminated" array of krb5_authdata ptrs
*/
static krb5_error_code
add_authorization_data(krb5_enc_tkt_part *enc_tkt_part, krb5_authdata **ad)
{
krb5_authdata **newad;
int oldones, newones;
int i;
if (enc_tkt_part == NULL || ad == NULL)
return EINVAL;