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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
|
/*
* kdc/kdc_preauth.c
*
* Copyright 1995, 2003 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.
*/
#include "k5-int.h"
#include "kdc_util.h"
#include "extern.h"
#include <stdio.h>
#include "adm_proto.h"
#include <syslog.h>
/* 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 krb5_error_code (*verify_proc)
(krb5_context, krb5_db_entry *client,
krb5_kdc_req *request,
krb5_enc_tkt_part * enc_tkt_reply, krb5_pa_data *data);
typedef krb5_error_code (*edata_proc)
(krb5_context, krb5_kdc_req *request,
krb5_db_entry *client, krb5_db_entry *server,
krb5_pa_data *data);
typedef krb5_error_code (*return_proc)
(krb5_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);
typedef struct _krb5_preauth_systems {
char * name;
int type;
int flags;
edata_proc get_edata;
verify_proc verify_padata;
return_proc return_padata;
} krb5_preauth_systems;
static krb5_error_code verify_enc_timestamp
(krb5_context, krb5_db_entry *client,
krb5_kdc_req *request,
krb5_enc_tkt_part * enc_tkt_reply, 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,
krb5_pa_data *data);
static krb5_error_code return_pw_salt
(krb5_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);
/* SAM preauth support */
static krb5_error_code verify_sam_response
(krb5_context, krb5_db_entry *client,
krb5_kdc_req *request,
krb5_enc_tkt_part * enc_tkt_reply, krb5_pa_data *data);
static krb5_error_code get_sam_edata
(krb5_context, krb5_kdc_req *request,
krb5_db_entry *client, krb5_db_entry *server,
krb5_pa_data *data);
static krb5_error_code return_sam_data
(krb5_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);
/*
* Preauth property flags
*/
#define PA_HARDWARE 0x00000001
#define PA_REQUIRED 0x00000002
#define PA_SUFFICIENT 0x00000004
/* Not really a padata, so don't include it in the etype list*/
#define PA_PSEUDO 0x00000008
static krb5_preauth_systems preauth_systems[] = {
{
"timestamp",
KRB5_PADATA_ENC_TIMESTAMP,
0,
0,
verify_enc_timestamp,
0
},
{
"etype-info",
KRB5_PADATA_ETYPE_INFO,
0,
get_etype_info,
0,
0
},
{
"pw-salt",
KRB5_PADATA_PW_SALT,
PA_PSEUDO, /* Don't include this in the error list */
0,
0,
return_pw_salt
},
{
"sam-response",
KRB5_PADATA_SAM_RESPONSE,
0,
0,
verify_sam_response,
return_sam_data
},
{
"sam-challenge",
KRB5_PADATA_SAM_CHALLENGE,
PA_HARDWARE, /* causes get_preauth_hint_list to use this */
get_sam_edata,
0,
0
},
{ "[end]", -1,}
};
#define MAX_PREAUTH_SYSTEMS (sizeof(preauth_systems)/sizeof(preauth_systems[0]))
static krb5_error_code
find_pa_system(int type, krb5_preauth_systems **preauth)
{
krb5_preauth_systems *ap = preauth_systems;
while ((ap->type != -1) && (ap->type != type))
ap++;
if (ap->type == -1)
return(KRB5_PREAUTH_BAD_TYPE);
*preauth = ap;
return 0;
}
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);
pa_data = malloc(sizeof(krb5_pa_data *) * (MAX_PREAUTH_SYSTEMS+1));
if (pa_data == 0)
return;
memset(pa_data, 0, sizeof(krb5_pa_data *) * (MAX_PREAUTH_SYSTEMS+1));
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, *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" : "");
}
retval = encode_krb5_padata_sequence((const krb5_pa_data **) pa_data,
&edat);
if (retval)
goto errout;
*e_data = *edat;
free(edat);
errout:
krb5_free_pa_data(kdc_context, pa_data);
return;
}
/*
* This routine is called to verify the preauthentication information
* for a V5 request.
*
* Returns 0 if the pre-authentication is valid, non-zero to indicate
* an error code of some sort.
*/
krb5_error_code
check_padata (krb5_context context, krb5_db_entry *client,
krb5_kdc_req *request, krb5_enc_tkt_part *enc_tkt_reply)
{
krb5_error_code retval = 0;
krb5_pa_data **padata;
krb5_preauth_systems *pa_sys;
int pa_ok = 0, pa_found = 0;
if (request->padata == 0)
return 0;
#ifdef DEBUG
krb5_klog_syslog (LOG_DEBUG, "checking padata");
#endif
for (padata = request->padata; *padata; padata++) {
#ifdef DEBUG
krb5_klog_syslog (LOG_DEBUG, ".. pa_type 0x%x", (*padata)->pa_type);
#endif
if (find_pa_system((*padata)->pa_type, &pa_sys))
continue;
#ifdef DEBUG
krb5_klog_syslog (LOG_DEBUG, ".. pa_type %s", pa_sys->name);
#endif
if (pa_sys->verify_padata == 0)
continue;
pa_found++;
retval = pa_sys->verify_padata(context, client, request,
enc_tkt_reply, *padata);
if (retval) {
krb5_klog_syslog (LOG_INFO, "preauth (%s) verify failure: %s",
pa_sys->name, error_message (retval));
if (pa_sys->flags & PA_REQUIRED) {
pa_ok = 0;
break;
}
} else {
#ifdef DEBUG
krb5_klog_syslog (LOG_DEBUG, ".. .. ok");
#endif
pa_ok = 1;
if (pa_sys->flags & PA_SUFFICIENT)
break;
}
}
if (pa_ok)
return 0;
/* pa system was not found, but principal doesn't require preauth */
if (!pa_found &&
!isflagset(client->attributes, KRB5_KDB_REQUIRES_PRE_AUTH) &&
!isflagset(client->attributes, KRB5_KDB_REQUIRES_HW_AUTH))
return 0;
if (!pa_found)
krb5_klog_syslog (LOG_INFO, "no valid preauth type found: %s",
error_message (retval));
/* The following switch statement allows us
* to return some preauth system errors back to the client.
*/
switch(retval) {
case KRB5KRB_AP_ERR_SKEW:
return retval;
default:
return KRB5KDC_ERR_PREAUTH_FAILED;
}
}
/*
* return_padata creates any necessary preauthentication
* structures which should be returned by the KDC to the client
*/
krb5_error_code
return_padata(krb5_context context, krb5_db_entry *client,
krb5_kdc_req *request, krb5_kdc_rep *reply,
krb5_key_data *client_key, krb5_keyblock *encrypting_key)
{
krb5_error_code retval;
krb5_pa_data ** padata;
krb5_pa_data ** send_pa_list;
krb5_pa_data ** send_pa;
krb5_pa_data * pa = 0;
krb5_preauth_systems * ap;
int size = 0;
for (ap = preauth_systems; ap->type != -1; ap++) {
if (ap->return_padata)
size++;
}
if ((send_pa_list = malloc((size+1) * sizeof(krb5_pa_data *))) == NULL)
return ENOMEM;
send_pa = send_pa_list;
*send_pa = 0;
for (ap = preauth_systems; ap->type != -1; ap++) {
if (ap->return_padata == 0)
continue;
pa = 0;
if (request->padata) {
for (padata = request->padata; *padata; padata++) {
if ((*padata)->pa_type == ap->type) {
pa = *padata;
break;
}
}
}
if ((retval = ap->return_padata(context, pa, client, request, reply,
client_key, encrypting_key, send_pa)))
goto cleanup;
if (*send_pa)
send_pa++;
*send_pa = 0;
}
retval = 0;
if (send_pa_list[0]) {
reply->padata = send_pa_list;
send_pa_list = 0;
}
cleanup:
if (send_pa_list)
krb5_free_pa_data(context, send_pa_list);
return (retval);
}
static krb5_boolean
request_contains_enctype (krb5_context context, const krb5_kdc_req *request,
krb5_enctype enctype)
{
int i;
for (i =0; i < request->nktypes; i++)
if (request->ktype[i] == enctype)
return 1;
return 0;
}
static krb5_error_code
verify_enc_timestamp(krb5_context context, krb5_db_entry *client,
krb5_kdc_req *request, krb5_enc_tkt_part *enc_tkt_reply,
krb5_pa_data *pa)
{
krb5_pa_enc_ts * pa_enc = 0;
krb5_error_code retval;
krb5_data scratch;
krb5_data enc_ts_data;
krb5_enc_data *enc_data = 0;
krb5_keyblock key;
krb5_key_data * client_key;
krb5_int32 start;
krb5_timestamp timenow;
krb5_error_code decrypt_err;
scratch.data = pa->contents;
scratch.length = pa->length;
enc_ts_data.data = 0;
if ((retval = decode_krb5_enc_data(&scratch, &enc_data)) != 0)
goto cleanup;
enc_ts_data.length = enc_data->ciphertext.length;
if ((enc_ts_data.data = (char *) malloc(enc_ts_data.length)) == NULL)
goto cleanup;
start = 0;
decrypt_err = 0;
while (1) {
if ((retval = krb5_dbe_search_enctype(context, client,
&start, enc_data->enctype,
-1, 0, &client_key)))
goto cleanup;
if ((retval = krb5_dbekd_decrypt_key_data(context, &master_keyblock,
client_key, &key, NULL)))
goto cleanup;
key.enctype = enc_data->enctype;
retval = krb5_c_decrypt(context, &key, KRB5_KEYUSAGE_AS_REQ_PA_ENC_TS,
0, enc_data, &enc_ts_data);
krb5_free_keyblock_contents(context, &key);
if (retval == 0)
break;
else
decrypt_err = retval;
}
if ((retval = decode_krb5_pa_enc_ts(&enc_ts_data, &pa_enc)) != 0)
goto cleanup;
if ((retval = krb5_timeofday(context, &timenow)) != 0)
goto cleanup;
if (labs(timenow - pa_enc->patimestamp) > context->clockskew) {
retval = KRB5KRB_AP_ERR_SKEW;
goto cleanup;
}
setflag(enc_tkt_reply->flags, TKT_FLG_PRE_AUTH);
retval = 0;
cleanup:
if (enc_data) {
krb5_free_data_contents(context, &enc_data->ciphertext);
free(enc_data);
}
krb5_free_data_contents(context, &enc_ts_data);
if (pa_enc)
free(pa_enc);
/*
* If we get NO_MATCHING_KEY and decryption previously failed, and
* we failed to find any other keys of the correct enctype after
* that failed decryption, it probably means that the password was
* incorrect.
*/
if (retval == KRB5_KDB_NO_MATCHING_KEY && decrypt_err != 0)
retval = decrypt_err;
return retval;
}
/*
* This function returns the etype information for a particular
* client, to be passed back in the preauth list in the KRB_ERROR
* message.
*/
static krb5_error_code
get_etype_info(krb5_context context, krb5_kdc_req *request,
krb5_db_entry *client, krb5_db_entry *server,
krb5_pa_data *pa_data)
{
krb5_etype_info_entry ** entry = 0;
krb5_key_data *client_key;
krb5_error_code retval;
krb5_data salt;
krb5_data * scratch;
krb5_enctype db_etype;
int i = 0;
int start = 0;
salt.data = 0;
entry = malloc((client->n_key_data * 2 + 1) * sizeof(krb5_etype_info_entry *));
if (entry == NULL)
return ENOMEM;
entry[0] = NULL;
while (1) {
retval = krb5_dbe_search_enctype(context, client, &start, -1,
-1, 0, &client_key);
if (retval == KRB5_KDB_NO_MATCHING_KEY)
break;
if (retval)
goto cleanup;
db_etype = client_key->key_data_type[0];
if (db_etype == ENCTYPE_DES_CBC_MD4 || db_etype == ENCTYPE_DES_CBC_MD5)
db_etype = ENCTYPE_DES_CBC_CRC;
while (1) {
if (!request_contains_enctype(context,
request, db_etype)) {
if (db_etype == ENCTYPE_DES_CBC_CRC) {
db_etype = ENCTYPE_DES_CBC_MD5;
continue;
}
else break;
}
if ((entry[i] = malloc(sizeof(krb5_etype_info_entry))) == NULL) {
retval = ENOMEM;
goto cleanup;
}
entry[i+1] = 0;
entry[i]->magic = KV5M_ETYPE_INFO_ENTRY;
entry[i]->etype = db_etype;
entry[i]->length = KRB5_ETYPE_NO_SALT;
entry[i]->salt = 0;
retval = get_salt_from_key(context, request->client,
client_key, &salt);
if (retval)
goto cleanup;
if (salt.length >= 0 && salt.length != SALT_TYPE_NO_LENGTH) {
entry[i]->length = salt.length;
entry[i]->salt = salt.data;
salt.data = 0;
}
i++;
/*
* If we have a DES_CRC key, it can also be used as a
* DES_MD5 key.
*/
if (db_etype == ENCTYPE_DES_CBC_CRC)
db_etype = ENCTYPE_DES_CBC_MD5;
else
break;
}
}
retval = encode_krb5_etype_info((const krb5_etype_info_entry **) entry,
&scratch);
if (retval)
goto cleanup;
pa_data->contents = scratch->data;
pa_data->length = scratch->length;
free(scratch);
retval = 0;
cleanup:
if (entry)
krb5_free_etype_info(context, entry);
if (salt.data)
free(salt.data);
return retval;
}
static krb5_error_code
return_pw_salt(krb5_context context, krb5_pa_data *in_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)
{
krb5_error_code retval;
krb5_pa_data * padata;
krb5_data * scratch;
krb5_data salt_data;
if (client_key->key_data_ver == 1 ||
client_key->key_data_type[1] == KRB5_KDB_SALTTYPE_NORMAL)
return 0;
if ((padata = malloc(sizeof(krb5_pa_data))) == NULL)
return ENOMEM;
padata->magic = KV5M_PA_DATA;
padata->pa_type = KRB5_PADATA_PW_SALT;
switch (client_key->key_data_type[1]) {
case KRB5_KDB_SALTTYPE_V4:
/* send an empty (V4) salt */
padata->contents = 0;
padata->length = 0;
break;
case KRB5_KDB_SALTTYPE_NOREALM:
if ((retval = krb5_principal2salt_norealm(kdc_context,
request->client,
&salt_data)))
goto cleanup;
padata->contents = (krb5_octet *)salt_data.data;
padata->length = salt_data.length;
break;
case KRB5_KDB_SALTTYPE_AFS3:
/* send an AFS style realm-based salt */
/* for now, just pass the realm back and let the client
do the work. In the future, add a kdc configuration
variable that specifies the old cell name. */
padata->pa_type = KRB5_PADATA_AFS3_SALT;
/* it would be just like ONLYREALM, but we need to pass the 0 */
scratch = krb5_princ_realm(kdc_context, request->client);
if ((padata->contents = malloc(scratch->length+1)) == NULL) {
retval = ENOMEM;
goto cleanup;
}
memcpy(padata->contents, scratch->data, scratch->length);
padata->length = scratch->length+1;
padata->contents[scratch->length] = 0;
break;
case KRB5_KDB_SALTTYPE_ONLYREALM:
scratch = krb5_princ_realm(kdc_context, request->client);
if ((padata->contents = malloc(scratch->length)) == NULL) {
retval = ENOMEM;
goto cleanup;
}
memcpy(padata->contents, scratch->data, scratch->length);
padata->length = scratch->length;
break;
case KRB5_KDB_SALTTYPE_SPECIAL:
if ((padata->contents = malloc(client_key->key_data_length[1]))
== NULL) {
retval = ENOMEM;
goto cleanup;
}
memcpy(padata->contents, client_key->key_data_contents[1],
client_key->key_data_length[1]);
padata->length = client_key->key_data_length[1];
break;
default:
free(padata);
return 0;
}
*send_pa = padata;
return 0;
cleanup:
free(padata);
return retval;
}
static krb5_error_code
return_sam_data(krb5_context context, krb5_pa_data *in_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)
{
krb5_error_code retval;
krb5_data scratch;
int i;
krb5_sam_response *sr = 0;
krb5_predicted_sam_response *psr = 0;
if (in_padata == 0)
return 0;
/*
* We start by doing the same thing verify_sam_response() does:
* extract the psr from the padata (which is an sr). Nothing
* here should generate errors! We've already successfully done
* all this once.
*/
scratch.data = in_padata->contents;
scratch.length = in_padata->length;
if ((retval = decode_krb5_sam_response(&scratch, &sr))) {
com_err("krb5kdc", retval,
"return_sam_data(): decode_krb5_sam_response failed");
goto cleanup;
}
{
krb5_enc_data tmpdata;
tmpdata.enctype = ENCTYPE_UNKNOWN;
tmpdata.ciphertext = sr->sam_track_id;
scratch.length = tmpdata.ciphertext.length;
if ((scratch.data = (char *) malloc(scratch.length)) == NULL) {
retval = ENOMEM;
goto cleanup;
}
if ((retval = krb5_c_decrypt(context, &psr_key, /* XXX */ 0, 0,
&tmpdata, &scratch))) {
com_err("krb5kdc", retval,
"return_sam_data(): decrypt track_id failed");
free(scratch.data);
goto cleanup;
}
}
if ((retval = decode_krb5_predicted_sam_response(&scratch, &psr))) {
com_err("krb5kdc", retval,
"return_sam_data(): decode_krb5_predicted_sam_response failed");
free(scratch.data);
goto cleanup;
}
/* We could use sr->sam_flags, but it may be absent or altered. */
if (psr->sam_flags & KRB5_SAM_MUST_PK_ENCRYPT_SAD) {
com_err("krb5kdc", retval = KRB5KDC_ERR_PREAUTH_FAILED,
"Unsupported SAM flag must-pk-encrypt-sad");
goto cleanup;
}
if (psr->sam_flags & KRB5_SAM_SEND_ENCRYPTED_SAD) {
/* No key munging */
goto cleanup;
}
if (psr->sam_flags & KRB5_SAM_USE_SAD_AS_KEY) {
/* Use sam_key instead of client key */
krb5_free_keyblock_contents(context, encrypting_key);
krb5_copy_keyblock_contents(context, &psr->sam_key, encrypting_key);
/* XXX Attach a useful pa_data */
goto cleanup;
}
/* Otherwise (no flags set), we XOR the keys */
/* XXX The passwords-04 draft is underspecified here wrt different
key types. We will do what I hope to get into the -05 draft. */
{
krb5_octet *p = encrypting_key->contents;
krb5_octet *q = psr->sam_key.contents;
int length = ((encrypting_key->length < psr->sam_key.length)
? encrypting_key->length
: psr->sam_key.length);
for (i = 0; i < length; i++)
p[i] ^= q[i];
}
/* Post-mixing key correction */
switch (encrypting_key->enctype) {
case ENCTYPE_DES_CBC_CRC:
case ENCTYPE_DES_CBC_MD4:
case ENCTYPE_DES_CBC_MD5:
case ENCTYPE_DES_CBC_RAW:
mit_des_fixup_key_parity(encrypting_key->contents);
if (mit_des_is_weak_key(encrypting_key->contents))
((krb5_octet *) encrypting_key->contents)[7] ^= 0xf0;
break;
/* XXX case ENCTYPE_DES3_CBC_MD5: listed in 1510bis-04 draft */
case ENCTYPE_DES3_CBC_SHA: /* XXX deprecated? */
case ENCTYPE_DES3_CBC_RAW:
case ENCTYPE_DES3_CBC_SHA1:
for (i = 0; i < 3; i++) {
mit_des_fixup_key_parity(encrypting_key->contents + i * 8);
if (mit_des_is_weak_key(encrypting_key->contents + i * 8))
((krb5_octet *) encrypting_key->contents)[7 + i * 8] ^= 0xf0;
}
break;
default:
com_err("krb5kdc", retval = KRB5KDC_ERR_PREAUTH_FAILED,
"Unimplemented keytype for SAM key mixing");
goto cleanup;
}
/* XXX Attach a useful pa_data */
cleanup:
if (sr)
krb5_free_sam_response(context, sr);
if (psr)
krb5_free_predicted_sam_response(context, psr);
return retval;
}
static struct {
char* name;
int sam_type;
} *sam_ptr, sam_inst_map[] = {
{ "SNK4", PA_SAM_TYPE_DIGI_PATH, },
{ "SECURID", PA_SAM_TYPE_SECURID, },
{ "GRAIL", PA_SAM_TYPE_GRAIL, },
{ 0, 0 },
};
static krb5_error_code
get_sam_edata(krb5_context context, krb5_kdc_req *request,
krb5_db_entry *client, krb5_db_entry *server,
krb5_pa_data *pa_data)
{
krb5_error_code retval;
krb5_sam_challenge sc;
krb5_predicted_sam_response psr;
krb5_data * scratch;
krb5_keyblock encrypting_key;
char response[9];
char inputblock[8];
krb5_data predict_response;
memset(&sc, 0, sizeof(sc));
memset(&psr, 0, sizeof(psr));
/* Given the client name we can figure out what type of preauth
they need. The spec is currently for querying the database for
names that match the types of preauth used. Later we should
make this mapping show up in kdc.conf. In the meantime, we
hardcode the following:
/SNK4 -- Digital Pathways SNK/4 preauth.
/GRAIL -- experimental preauth
The first one found is used. See sam_inst_map above.
For SNK4 in particular, the key in the database is the key for
the device; kadmin needs a special interface for it.
*/
{
int npr = 1;
krb5_boolean more;
krb5_db_entry assoc;
krb5_key_data *assoc_key;
krb5_principal newp;
int probeslot;
sc.sam_type = 0;
retval = krb5_copy_principal(kdc_context, request->client, &newp);
if (retval) {
com_err("krb5kdc", retval, "copying client name for preauth probe");
return retval;
}
probeslot = krb5_princ_size(context, newp)++;
krb5_princ_name(kdc_context, newp) =
realloc(krb5_princ_name(kdc_context, newp),
krb5_princ_size(context, newp) * sizeof(krb5_data));
for(sam_ptr = sam_inst_map; sam_ptr->name; sam_ptr++) {
krb5_princ_component(kdc_context,newp,probeslot)->data = sam_ptr->name;
krb5_princ_component(kdc_context,newp,probeslot)->length =
strlen(sam_ptr->name);
npr = 1;
retval = krb5_db_get_principal(kdc_context, newp, &assoc, &npr, &more);
if(!retval && npr) {
sc.sam_type = sam_ptr->sam_type;
break;
}
}
krb5_princ_component(kdc_context,newp,probeslot)->data = 0;
krb5_princ_component(kdc_context,newp,probeslot)->length = 0;
krb5_princ_size(context, newp)--;
krb5_free_principal(kdc_context, newp);
/* if sc.sam_type is set, it worked */
if (sc.sam_type) {
/* so use assoc to get the key out! */
{
/* here's what do_tgs_req does */
retval = krb5_dbe_find_enctype(kdc_context, &assoc,
ENCTYPE_DES_CBC_RAW,
KRB5_KDB_SALTTYPE_NORMAL,
0, /* Get highest kvno */
&assoc_key);
if (retval) {
char *sname;
krb5_unparse_name(kdc_context, request->client, &sname);
com_err("krb5kdc", retval,
"snk4 finding the enctype and key <%s>", sname);
free(sname);
return retval;
}
/* convert server.key into a real key */
retval = krb5_dbekd_decrypt_key_data(kdc_context,
&master_keyblock,
assoc_key, &encrypting_key,
NULL);
if (retval) {
com_err("krb5kdc", retval,
"snk4 pulling out key entry");
return retval;
}
/* now we can use encrypting_key... */
}
} else {
/* SAM is not an option - so don't return as hint */
return KRB5_PREAUTH_BAD_TYPE;
}
}
sc.magic = KV5M_SAM_CHALLENGE;
psr.sam_flags = sc.sam_flags = KRB5_SAM_USE_SAD_AS_KEY;
/* Replay prevention */
if ((retval = krb5_copy_principal(context, request->client, &psr.client)))
return retval;
#ifdef USE_RCACHE
if ((retval = krb5_us_timeofday(context, &psr.stime, &psr.susec)))
return retval;
#endif /* USE_RCACHE */
switch (sc.sam_type) {
case PA_SAM_TYPE_GRAIL:
sc.sam_type_name.data = "Experimental System";
sc.sam_type_name.length = strlen(sc.sam_type_name.data);
sc.sam_challenge_label.data = "experimental challenge label";
sc.sam_challenge_label.length = strlen(sc.sam_challenge_label.data);
sc.sam_challenge.data = "12345";
sc.sam_challenge.length = strlen(sc.sam_challenge.data);
#if 0 /* Enable this to test "normal" (no flags set) mode. */
psr.sam_flags = sc.sam_flags = 0;
#endif
psr.magic = KV5M_PREDICTED_SAM_RESPONSE;
/* string2key on sc.sam_challenge goes in here */
/* eblock is just to set the enctype */
{
const krb5_enctype type = ENCTYPE_DES_CBC_MD5;
if ((retval = krb5_c_string_to_key(context, type, &sc.sam_challenge,
0 /* salt */, &psr.sam_key)))
goto cleanup;
if ((retval = encode_krb5_predicted_sam_response(&psr, &scratch)))
goto cleanup;
{
size_t enclen;
krb5_enc_data tmpdata;
if ((retval = krb5_c_encrypt_length(context,
psr_key.enctype,
scratch->length, &enclen)))
goto cleanup;
if ((tmpdata.ciphertext.data = (char *) malloc(enclen)) == NULL) {
retval = ENOMEM;
goto cleanup;
}
tmpdata.ciphertext.length = enclen;
if ((retval = krb5_c_encrypt(context, &psr_key,
/* XXX */ 0, 0, scratch, &tmpdata)))
goto cleanup;
sc.sam_track_id = tmpdata.ciphertext;
}
}
sc.sam_response_prompt.data = "response prompt";
sc.sam_response_prompt.length = strlen(sc.sam_response_prompt.data);
sc.sam_pk_for_sad.length = 0;
sc.sam_nonce = 0;
/* Generate checksum */
/*krb5_checksum_size(context, ctype)*/
/*krb5_calculate_checksum(context,ctype,in,in_length,seed,
seed_length,outcksum) */
/*krb5_verify_checksum(context,ctype,cksum,in,in_length,seed,
seed_length) */
#if 0 /* XXX a) glue appears broken; b) this gives up the SAD */
sc.sam_cksum.contents = (krb5_octet *)
malloc(krb5_checksum_size(context, CKSUMTYPE_RSA_MD5_DES));
if (sc.sam_cksum.contents == NULL) return(ENOMEM);
retval = krb5_calculate_checksum(context, CKSUMTYPE_RSA_MD5_DES,
sc.sam_challenge.data,
sc.sam_challenge.length,
psr.sam_key.contents, /* key */
psr.sam_key.length, /* key length */
&sc.sam_cksum);
if (retval) { free(sc.sam_cksum.contents); return(retval); }
#endif /* 0 */
retval = encode_krb5_sam_challenge(&sc, &scratch);
if (retval) goto cleanup;
pa_data->magic = KV5M_PA_DATA;
pa_data->pa_type = KRB5_PADATA_SAM_CHALLENGE;
pa_data->contents = scratch->data;
pa_data->length = scratch->length;
retval = 0;
break;
case PA_SAM_TYPE_DIGI_PATH:
sc.sam_type_name.data = "Digital Pathways";
sc.sam_type_name.length = strlen(sc.sam_type_name.data);
#if 1
sc.sam_challenge_label.data = "Enter the following on your keypad";
sc.sam_challenge_label.length = strlen(sc.sam_challenge_label.data);
#endif
/* generate digit string, take it mod 1000000 (six digits.) */
{
int j;
krb5_keyblock session_key;
char outputblock[8];
int i;
session_key.contents = 0;
memset(inputblock, 0, 8);
retval = krb5_c_make_random_key(kdc_context, ENCTYPE_DES_CBC_CRC,
&session_key);
if (retval) {
/* random key failed */
com_err("krb5kdc", retval,"generating random challenge for preauth");
return retval;
}
/* now session_key has a key which we can pick bits out of */
/* we need six decimal digits. Grab 6 bytes, div 2, mod 10 each. */
if (session_key.length != 8) {
com_err("krb5kdc", retval = KRB5KDC_ERR_ETYPE_NOSUPP,
"keytype didn't match code expectations");
return retval;
}
for(i = 0; i<6; i++) {
inputblock[i] = '0' + ((session_key.contents[i]/2) % 10);
}
if (session_key.contents)
krb5_free_keyblock_contents(kdc_context, &session_key);
/* retval = krb5_finish_key(kdc_context, &eblock); */
/* now we have inputblock containing the 8 byte input to DES... */
sc.sam_challenge.data = inputblock;
sc.sam_challenge.length = 6;
encrypting_key.enctype = ENCTYPE_DES_CBC_RAW;
if (retval) {
com_err("krb5kdc", retval, "snk4 processing key");
}
{
krb5_data plain;
krb5_enc_data cipher;
plain.length = 8;
plain.data = inputblock;
/* XXX I know this is enough because of the fixed raw enctype.
if it's not, the underlying code will return a reasonable
error, which should never happen */
cipher.ciphertext.length = 8;
cipher.ciphertext.data = outputblock;
if ((retval = krb5_c_encrypt(kdc_context, &encrypting_key,
/* XXX */ 0, 0, &plain, &cipher))) {
com_err("krb5kdc", retval, "snk4 response generation failed");
return retval;
}
}
/* now output block is the raw bits of the response; convert it
to display form */
for (j=0; j<4; j++) {
char n[2];
int k;
n[0] = outputblock[j] & 0xf;
n[1] = (outputblock[j]>>4) & 0xf;
for (k=0; k<2; k++) {
if(n[k] > 9) n[k] = ((n[k]-1)>>2);
/* This is equivalent to:
if(n[k]>=0xa && n[k]<=0xc) n[k] = 2;
if(n[k]>=0xd && n[k]<=0xf) n[k] = 3;
*/
}
/* for v4, we keygen: *(j+(char*)&key1) = (n[1]<<4) | n[0]; */
/* for v5, we just generate a string */
response[2*j+0] = '0' + n[1];
response[2*j+1] = '0' + n[0];
/* and now, response has what we work with. */
}
response[8] = 0;
predict_response.data = response;
predict_response.length = 8;
#if 0 /* for debugging, hack the output too! */
sc.sam_challenge_label.data = response;
sc.sam_challenge_label.length = strlen(sc.sam_challenge_label.data);
#endif
}
psr.magic = KV5M_PREDICTED_SAM_RESPONSE;
/* string2key on sc.sam_challenge goes in here */
/* eblock is just to set the enctype */
{
retval = krb5_c_string_to_key(context, ENCTYPE_DES_CBC_MD5,
&predict_response, 0 /* salt */,
&psr.sam_key);
if (retval) goto cleanup;
retval = encode_krb5_predicted_sam_response(&psr, &scratch);
if (retval) goto cleanup;
{
size_t enclen;
krb5_enc_data tmpdata;
if ((retval = krb5_c_encrypt_length(context,
psr_key.enctype,
scratch->length, &enclen)))
goto cleanup;
if ((tmpdata.ciphertext.data = (char *) malloc(enclen)) == NULL) {
retval = ENOMEM;
goto cleanup;
}
tmpdata.ciphertext.length = enclen;
if ((retval = krb5_c_encrypt(context, &psr_key,
/* XXX */ 0, 0, scratch, &tmpdata)))
goto cleanup;
sc.sam_track_id = tmpdata.ciphertext;
}
if (retval) goto cleanup;
}
sc.sam_response_prompt.data = "Enter the displayed response";
sc.sam_response_prompt.length = strlen(sc.sam_response_prompt.data);
sc.sam_pk_for_sad.length = 0;
sc.sam_nonce = 0;
/* Generate checksum */
/*krb5_checksum_size(context, ctype)*/
/*krb5_calculate_checksum(context,ctype,in,in_length,seed,
seed_length,outcksum) */
/*krb5_verify_checksum(context,ctype,cksum,in,in_length,seed,
seed_length) */
#if 0 /* XXX a) glue appears broken; b) this gives up the SAD */
sc.sam_cksum.contents = (krb5_octet *)
malloc(krb5_checksum_size(context, CKSUMTYPE_RSA_MD5_DES));
if (sc.sam_cksum.contents == NULL) return(ENOMEM);
retval = krb5_calculate_checksum(context, CKSUMTYPE_RSA_MD5_DES,
sc.sam_challenge.data,
sc.sam_challenge.length,
psr.sam_key.contents, /* key */
psr.sam_key.length, /* key length */
&sc.sam_cksum);
if (retval) { free(sc.sam_cksum.contents); return(retval); }
#endif /* 0 */
retval = encode_krb5_sam_challenge(&sc, &scratch);
if (retval) goto cleanup;
pa_data->magic = KV5M_PA_DATA;
pa_data->pa_type = KRB5_PADATA_SAM_CHALLENGE;
pa_data->contents = scratch->data;
pa_data->length = scratch->length;
retval = 0;
break;
}
cleanup:
krb5_free_keyblock_contents(context, &encrypting_key);
return retval;
}
static krb5_error_code
verify_sam_response(krb5_context context, krb5_db_entry *client,
krb5_kdc_req *request, krb5_enc_tkt_part *enc_tkt_reply,
krb5_pa_data *pa)
{
krb5_error_code retval;
krb5_data scratch;
krb5_sam_response *sr = 0;
krb5_predicted_sam_response *psr = 0;
krb5_enc_sam_response_enc *esre = 0;
krb5_timestamp timenow;
char *princ_req = 0, *princ_psr = 0;
scratch.data = pa->contents;
scratch.length = pa->length;
if ((retval = decode_krb5_sam_response(&scratch, &sr))) {
scratch.data = 0;
com_err("krb5kdc", retval, "decode_krb5_sam_response failed");
goto cleanup;
}
/* XXX We can only handle the challenge/response model of SAM.
See passwords-04, par 4.1, 4.2 */
{
krb5_enc_data tmpdata;
tmpdata.enctype = ENCTYPE_UNKNOWN;
tmpdata.ciphertext = sr->sam_track_id;
scratch.length = tmpdata.ciphertext.length;
if ((scratch.data = (char *) malloc(scratch.length)) == NULL) {
retval = ENOMEM;
goto cleanup;
}
if ((retval = krb5_c_decrypt(context, &psr_key, /* XXX */ 0, 0,
&tmpdata, &scratch))) {
com_err("krb5kdc", retval, "decrypt track_id failed");
goto cleanup;
}
}
if ((retval = decode_krb5_predicted_sam_response(&scratch, &psr))) {
com_err("krb5kdc", retval,
"decode_krb5_predicted_sam_response failed -- replay attack?");
goto cleanup;
}
/* Replay detection */
if ((retval = krb5_unparse_name(context, request->client, &princ_req)))
goto cleanup;
if ((retval = krb5_unparse_name(context, psr->client, &princ_psr)))
goto cleanup;
if (strcmp(princ_req, princ_psr) != 0) {
com_err("krb5kdc", retval = KRB5KDC_ERR_PREAUTH_FAILED,
"Principal mismatch in SAM psr! -- replay attack?");
goto cleanup;
}
if ((retval = krb5_timeofday(context, &timenow)))
goto cleanup;
#ifdef USE_RCACHE
{
krb5_donot_replay rep;
extern krb5_deltat rc_lifetime;
/*
* Verify this response came back in a timely manner.
* We do this b/c otherwise very old (expunged from the rcache)
* psr's would be able to be replayed.
*/
if (timenow - psr->stime > rc_lifetime) {
com_err("krb5kdc", retval = KRB5KDC_ERR_PREAUTH_FAILED,
"SAM psr came back too late! -- replay attack?");
goto cleanup;
}
/* Now check the replay cache. */
rep.client = princ_psr;
rep.server = "SAM/rc"; /* Should not match any principal name. */
rep.ctime = psr->stime;
rep.cusec = psr->susec;
retval = krb5_rc_store(kdc_context, kdc_rcache, &rep);
if (retval) {
com_err("krb5kdc", retval, "SAM psr replay attack!");
goto cleanup;
}
}
#endif /* USE_RCACHE */
{
free(scratch.data);
scratch.length = sr->sam_enc_nonce_or_ts.ciphertext.length;
if ((scratch.data = (char *) malloc(scratch.length)) == NULL) {
retval = ENOMEM;
goto cleanup;
}
if ((retval = krb5_c_decrypt(context, &psr->sam_key, /* XXX */ 0,
0, &sr->sam_enc_nonce_or_ts, &scratch))) {
com_err("krb5kdc", retval, "decrypt nonce_or_ts failed");
goto cleanup;
}
}
if ((retval = decode_krb5_enc_sam_response_enc(&scratch, &esre))) {
com_err("krb5kdc", retval, "decode_krb5_enc_sam_response_enc failed");
goto cleanup;
}
if (esre->sam_timestamp != sr->sam_patimestamp) {
retval = KRB5KDC_ERR_PREAUTH_FAILED;
goto cleanup;
}
if (labs(timenow - sr->sam_patimestamp) > context->clockskew) {
retval = KRB5KRB_AP_ERR_SKEW;
goto cleanup;
}
setflag(enc_tkt_reply->flags, TKT_FLG_HW_AUTH);
cleanup:
if (retval)
com_err("krb5kdc", retval, "sam verify failure");
if (scratch.data) free(scratch.data);
if (sr) free(sr);
if (psr) free(psr);
if (esre) free(esre);
return retval;
}
|