summaryrefslogtreecommitdiff
path: root/third_party/heimdal/lib/krb5/init_creds_pw.c
blob: 4ad5deba0900d14b3a87d2d5f472642f1543a961 (plain)
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
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
/*
 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
 * (Royal Institute of Technology, Stockholm, Sweden).
 * All rights reserved.
 *
 * Portions Copyright (c) 2009 - 2010 Apple Inc. All rights reserved.
 * Portions Copyright (c) 2021, PADL Software Pty Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. 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.
 *
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE 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 INSTITUTE 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 "krb5_locl.h"
#include "../base/heimbasepriv.h" /* XXX */

struct pa_info_data {
    krb5_enctype etype;
    krb5_salt salt;
    krb5_data *s2kparams;
};

struct krb5_gss_init_ctx_data {
    krb5_gssic_step step;
    krb5_gssic_finish finish;
    krb5_gssic_release_cred release_cred;
    krb5_gssic_delete_sec_context delete_sec_context;

    const struct gss_OID_desc_struct *mech;
    struct gss_cred_id_t_desc_struct *cred;

    struct {
	unsigned int release_cred : 1;
    } flags;
};

struct krb5_get_init_creds_ctx {
    KDCOptions flags;
    krb5_creds cred;
    krb5_addresses *addrs;
    krb5_enctype *etypes;
    krb5_preauthtype *pre_auth_types;
    char *in_tkt_service;
    unsigned nonce;
    unsigned pk_nonce;

    krb5_data req_buffer;
    AS_REQ as_req;
    int pa_counter;

    /* password and keytab_data is freed on completion */
    char *password;
    krb5_keytab_key_proc_args *keytab_data;

    krb5_pointer *keyseed;
    krb5_s2k_proc keyproc;

    krb5_get_init_creds_tristate req_pac;

    krb5_pk_init_ctx pk_init_ctx;
    krb5_gss_init_ctx gss_init_ctx;
    int ic_flags;

    char *kdc_hostname;
    char *sitename;

    struct {
	unsigned int change_password:1;
	unsigned int change_password_prompt:1;
	unsigned int allow_enc_pa_rep:1;
	unsigned int allow_save_as_reply_key:1;
    } runflags;

    struct pa_info_data paid;

    METHOD_DATA md;
    KRB_ERROR error;
    EncKDCRepPart enc_part;

    krb5_prompter_fct prompter;
    void *prompter_data;
    int warned_user;

    struct pa_info_data *ppaid;

    struct krb5_fast_state fast_state;
    krb5_enctype as_enctype;
    krb5_keyblock *as_reply_key;

    /* current and available pa mechansm in this exchange */
    struct pa_auth_mech *pa_mech;
    heim_array_t available_pa_mechs;
    const char *pa_used;

    struct {
	struct timeval run_time;
    } stats;
};

static void
free_paid(krb5_context context, struct pa_info_data *ppaid)
{
    krb5_free_salt(context, ppaid->salt);
    if (ppaid->s2kparams)
	krb5_free_data(context, ppaid->s2kparams);
    memset(ppaid, 0, sizeof(*ppaid));
}

static krb5_error_code KRB5_CALLCONV
default_s2k_func(krb5_context context, krb5_enctype type,
		 krb5_const_pointer keyseed,
		 krb5_salt salt, krb5_data *s2kparms,
		 krb5_keyblock **key)
{
    krb5_error_code ret;
    krb5_data password;
    krb5_data opaque;

    if (_krb5_have_debug(context, 5)) {
	char *str = NULL;
	ret = krb5_enctype_to_string(context, type, &str);
	if (ret)
	    return ret;

	_krb5_debug(context, 5, "krb5_get_init_creds: using default_s2k_func: %s (%d)", str, (int)type);
	free(str);
    }

    password.data = rk_UNCONST(keyseed);
    password.length = keyseed ? strlen(keyseed) : 0;
    if (s2kparms)
	opaque = *s2kparms;
    else
	krb5_data_zero(&opaque);

    *key = malloc(sizeof(**key));
    if (*key == NULL)
	return krb5_enomem(context);
    ret = krb5_string_to_key_data_salt_opaque(context, type, password,
					      salt, opaque, *key);
    if (ret) {
	free(*key);
	*key = NULL;
    }
    return ret;
}

static void
free_gss_init_ctx(krb5_context context, krb5_gss_init_ctx gssic)
{
    if (gssic == NULL)
	return;

    if (gssic->flags.release_cred)
	gssic->release_cred(context, gssic, gssic->cred);
    free(gssic);
}

static void
free_init_creds_ctx(krb5_context context, krb5_init_creds_context ctx)
{
    if (ctx->etypes)
	free(ctx->etypes);
    if (ctx->pre_auth_types)
	free (ctx->pre_auth_types);
    if (ctx->in_tkt_service)
	free(ctx->in_tkt_service);
    if (ctx->keytab_data)
	free(ctx->keytab_data);
    if (ctx->password) {
	size_t len;
	len = strlen(ctx->password);
	memset_s(ctx->password, len, 0, len);
	free(ctx->password);
    }
    free_gss_init_ctx(context, ctx->gss_init_ctx);
    /*
     * FAST state
     */
    _krb5_fast_free(context, &ctx->fast_state);
    if (ctx->as_reply_key)
	krb5_free_keyblock(context, ctx->as_reply_key);

    krb5_data_free(&ctx->req_buffer);
    krb5_free_cred_contents(context, &ctx->cred);
    free_METHOD_DATA(&ctx->md);
    free_EncKDCRepPart(&ctx->enc_part);
    free_KRB_ERROR(&ctx->error);
    free_AS_REQ(&ctx->as_req);

    heim_release(ctx->available_pa_mechs);
    heim_release(ctx->pa_mech);
    ctx->pa_mech = NULL;
    free(ctx->kdc_hostname);
    free(ctx->sitename);
    free_paid(context, &ctx->paid);
    memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx));
}

static krb5_deltat
get_config_time (krb5_context context,
		 const char *realm,
		 const char *name,
		 int def)
{
    krb5_deltat ret;

    ret = krb5_config_get_time (context, NULL,
				"realms",
				realm,
				name,
				NULL);
    if (ret >= 0)
	return ret;
    ret = krb5_config_get_time (context, NULL,
				"libdefaults",
				name,
				NULL);
    if (ret >= 0)
	return ret;
    return def;
}

static krb5_error_code
init_cred (krb5_context context,
	   krb5_creds *cred,
	   krb5_principal client,
	   krb5_deltat start_time,
	   krb5_get_init_creds_opt *options)
{
    krb5_error_code ret;
    krb5_deltat tmp;
    krb5_timestamp now;

    krb5_timeofday (context, &now);

    memset (cred, 0, sizeof(*cred));

    if (client)
	ret = krb5_copy_principal(context, client, &cred->client);
    else
	ret = krb5_get_default_principal(context, &cred->client);
    if (ret)
	goto out;

    if (start_time)
	cred->times.starttime  = now + start_time;

    if (options->flags & KRB5_GET_INIT_CREDS_OPT_TKT_LIFE)
	tmp = options->tkt_life;
    else
	tmp = KRB5_TKT_LIFETIME_DEFAULT;
    cred->times.endtime = now + tmp;

    if ((options->flags & KRB5_GET_INIT_CREDS_OPT_RENEW_LIFE)) {
	if (options->renew_life > 0)
	    tmp = options->renew_life;
	else
	    tmp = KRB5_TKT_RENEW_LIFETIME_DEFAULT;
	cred->times.renew_till = now + tmp;
    }

    return 0;

out:
    krb5_free_cred_contents (context, cred);
    return ret;
}

/*
 * Print a message (str) to the user about the expiration in `lr'
 */

static void
report_expiration (krb5_context context,
		   krb5_prompter_fct prompter,
		   krb5_data *data,
		   const char *str,
		   time_t now)
{
    char *p = NULL;

    if (asprintf(&p, "%s%s", str, ctime(&now)) < 0 || p == NULL)
	return;
    (*prompter)(context, data, NULL, p, 0, NULL);
    free(p);
}

/*
 * Check the context, and in the case there is a expiration warning,
 * use the prompter to print the warning.
 *
 * @param context A Kerberos 5 context.
 * @param options An GIC options structure
 * @param ctx The krb5_init_creds_context check for expiration.
 */

krb5_error_code
krb5_process_last_request(krb5_context context,
			  krb5_get_init_creds_opt *options,
			  krb5_init_creds_context ctx)
{
    LastReq *lr;
    size_t i;

    /*
     * First check if there is a API consumer.
     */

    lr = &ctx->enc_part.last_req;

    if (options && options->opt_private && options->opt_private->lr.func) {
	krb5_last_req_entry **lre;

	lre = calloc(lr->len + 1, sizeof(*lre));
	if (lre == NULL)
	    return krb5_enomem(context);

	for (i = 0; i < lr->len; i++) {
	    lre[i] = calloc(1, sizeof(*lre[i]));
	    if (lre[i] == NULL)
		break;
	    lre[i]->lr_type = lr->val[i].lr_type;
	    lre[i]->value = lr->val[i].lr_value;
	}

	(*options->opt_private->lr.func)(context, lre,
					 options->opt_private->lr.ctx);

	for (i = 0; i < lr->len; i++)
	    free(lre[i]);
	free(lre);
    }

    return krb5_init_creds_warn_user(context, ctx);
}

/**
 * Warn the user using prompter in the krb5_init_creds_context about
 * possible password and account expiration.
 *
 * @param context a Kerberos 5 context.
 * @param ctx a krb5_init_creds_context context.
 *
 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
 * @ingroup krb5_credential
 */

KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_init_creds_warn_user(krb5_context context,
			  krb5_init_creds_context ctx)
{
    krb5_timestamp sec;
    krb5_const_realm realm;
    krb5_enctype weak_enctype = KRB5_ENCTYPE_NULL;
    LastReq *lr;
    unsigned i;
    time_t t;

    if (ctx->prompter == NULL)
	return 0;

    if (ctx->warned_user)
	return 0;

    ctx->warned_user = 1;

    krb5_timeofday (context, &sec);

    realm = krb5_principal_get_realm (context, ctx->cred.client);
    lr = &ctx->enc_part.last_req;

    t = sec + get_config_time (context,
			       realm,
			       "warn_pwexpire",
			       7 * 24 * 60 * 60);

    for (i = 0; i < lr->len; ++i) {
	if (lr->val[i].lr_value <= t) {
	    switch (lr->val[i].lr_type) {
	    case LR_PW_EXPTIME :
		report_expiration(context, ctx->prompter,
				  ctx->prompter_data,
				  "Your password will expire at ",
				  lr->val[i].lr_value);
		break;
	    case LR_ACCT_EXPTIME :
		report_expiration(context, ctx->prompter,
				  ctx->prompter_data,
				  "Your account will expire at ",
				  lr->val[i].lr_value);
		break;
	    default:
		break;
	    }
	}
    }

    if (krb5_is_enctype_weak(context, ctx->as_enctype))
	weak_enctype = ctx->as_enctype;
    else if (krb5_is_enctype_weak(context, ctx->cred.session.keytype))
	weak_enctype = ctx->cred.session.keytype;

    if (ctx->prompter && weak_enctype != KRB5_ENCTYPE_NULL) {
	int suppress = krb5_config_get_bool_default(context, NULL, false,
						    "libdefaults",
						    "suppress_weak_enctype", NULL);
	if (!suppress) {
	    char *str = NULL, *p = NULL;
	    int aret;
	    krb5_enctype_to_string(context, weak_enctype, &str);

	    aret = asprintf(&p, "Encryption type %s(%d) used for authentication is weak and will be deprecated",
			    str ? str : "unknown", weak_enctype);
	    if (aret >= 0 && p) {
		(*ctx->prompter)(context, ctx->prompter_data, NULL, p, 0, NULL);
		free(p);
	    }
	    free(str);
	}
    }

    return 0;
}

static krb5_addresses no_addrs = { 0, NULL };

static krb5_error_code
get_init_creds_common(krb5_context context,
		      krb5_principal client,
		      krb5_prompter_fct prompter,
		      void *prompter_data,
		      krb5_deltat start_time,
		      krb5_get_init_creds_opt *options,
		      krb5_init_creds_context ctx)
{
    krb5_get_init_creds_opt *default_opt = NULL;
    krb5_error_code ret;
    krb5_enctype *etypes;
    krb5_preauthtype *pre_auth_types;

    memset(ctx, 0, sizeof(*ctx));

    if (options == NULL) {
	const char *realm = krb5_principal_get_realm(context, client);

        krb5_get_init_creds_opt_alloc (context, &default_opt);
	options = default_opt;
	krb5_get_init_creds_opt_set_default_flags(context, NULL, realm, options);
    }

    if (options->opt_private) {
	if (options->opt_private->password) {
	    ret = krb5_init_creds_set_password(context, ctx,
					       options->opt_private->password);
	    if (ret)
		goto out;
	}

	ctx->keyproc = options->opt_private->key_proc;
	ctx->req_pac = options->opt_private->req_pac;
	ctx->pk_init_ctx = options->opt_private->pk_init_ctx;
	ctx->ic_flags = options->opt_private->flags;
    } else
	ctx->req_pac = KRB5_INIT_CREDS_TRISTATE_UNSET;

    if (ctx->keyproc == NULL)
	ctx->keyproc = default_s2k_func;

    if (ctx->ic_flags & KRB5_INIT_CREDS_CANONICALIZE)
	ctx->flags.canonicalize = 1;
    if (krb5_principal_get_type(context, client) == KRB5_NT_ENTERPRISE_PRINCIPAL)
	ctx->flags.canonicalize = 1;

    ctx->pre_auth_types = NULL;
    ctx->addrs = NULL;
    ctx->etypes = NULL;
    ctx->pre_auth_types = NULL;

    ret = init_cred(context, &ctx->cred, client, start_time, options);
    if (ret) {
	if (default_opt)
	    krb5_get_init_creds_opt_free(context, default_opt);
	return ret;
    }

    ret = krb5_init_creds_set_service(context, ctx, NULL);
    if (ret)
	goto out;

    if (options->flags & KRB5_GET_INIT_CREDS_OPT_FORWARDABLE)
	ctx->flags.forwardable = options->forwardable;

    if (options->flags & KRB5_GET_INIT_CREDS_OPT_PROXIABLE)
	ctx->flags.proxiable = options->proxiable;

    if (start_time)
	ctx->flags.postdated = 1;
    if (ctx->cred.times.renew_till)
	ctx->flags.renewable = 1;
    if (options->flags & KRB5_GET_INIT_CREDS_OPT_ADDRESS_LIST) {
	ctx->addrs = options->address_list;
    } else if (options->opt_private) {
	switch (options->opt_private->addressless) {
	case KRB5_INIT_CREDS_TRISTATE_UNSET:
#if KRB5_ADDRESSLESS_DEFAULT == TRUE
	    ctx->addrs = &no_addrs;
#else
	    ctx->addrs = NULL;
#endif
	    break;
	case KRB5_INIT_CREDS_TRISTATE_FALSE:
	    ctx->addrs = NULL;
	    break;
	case KRB5_INIT_CREDS_TRISTATE_TRUE:
	    ctx->addrs = &no_addrs;
	    break;
	}
    }
    if (options->flags & KRB5_GET_INIT_CREDS_OPT_ETYPE_LIST) {
	if (ctx->etypes)
	    free(ctx->etypes);

	etypes = malloc((options->etype_list_length + 1)
			* sizeof(krb5_enctype));
	if (etypes == NULL) {
	    ret = krb5_enomem(context);
	    goto out;
	}
	memcpy (etypes, options->etype_list,
		options->etype_list_length * sizeof(krb5_enctype));
	etypes[options->etype_list_length] = ETYPE_NULL;
	ctx->etypes = etypes;
    }
    if (options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST) {
	pre_auth_types = malloc((options->preauth_list_length + 1)
				* sizeof(krb5_preauthtype));
	if (pre_auth_types == NULL) {
	    ret = krb5_enomem(context);
	    goto out;
	}
	memcpy (pre_auth_types, options->preauth_list,
		options->preauth_list_length * sizeof(krb5_preauthtype));
	pre_auth_types[options->preauth_list_length] = KRB5_PADATA_NONE;
	ctx->pre_auth_types = pre_auth_types;
    }
    if (options->flags & KRB5_GET_INIT_CREDS_OPT_ANONYMOUS)
	ctx->flags.request_anonymous = options->anonymous;

    ctx->prompter = prompter;
    ctx->prompter_data = prompter_data;

    if ((options->flags & KRB5_GET_INIT_CREDS_OPT_CHANGE_PASSWORD_PROMPT) &&
	!options->change_password_prompt)
	ctx->runflags.change_password_prompt = 0;
    else
	ctx->runflags.change_password_prompt = ctx->prompter != NULL;

    if (default_opt)
        krb5_get_init_creds_opt_free(context, default_opt);
    return 0;

 out:
    if (default_opt)
	krb5_get_init_creds_opt_free(context, default_opt);
    return ret;
}

static krb5_error_code
change_password (krb5_context context,
		 krb5_principal client,
		 const char *password,
		 char *newpw,
		 size_t newpw_sz,
		 krb5_prompter_fct prompter,
		 void *data,
		 krb5_get_init_creds_opt *old_options)
{
    krb5_prompt prompts[2];
    krb5_error_code ret;
    krb5_creds cpw_cred;
    char buf1[BUFSIZ], buf2[BUFSIZ];
    krb5_data password_data[2];
    int result_code;
    krb5_data result_code_string;
    krb5_data result_string;
    char *p;
    krb5_get_init_creds_opt *options;

    heim_assert(prompter != NULL, "unexpected NULL prompter");

    memset (&cpw_cred, 0, sizeof(cpw_cred));

    ret = krb5_get_init_creds_opt_alloc(context, &options);
    if (ret)
        return ret;
    krb5_get_init_creds_opt_set_tkt_life (options, 60);
    krb5_get_init_creds_opt_set_forwardable (options, FALSE);
    krb5_get_init_creds_opt_set_proxiable (options, FALSE);
    if (old_options &&
        (old_options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST))
	krb5_get_init_creds_opt_set_preauth_list(options,
						 old_options->preauth_list,
						 old_options->preauth_list_length);
    if (old_options &&
        (old_options->flags & KRB5_GET_INIT_CREDS_OPT_CHANGE_PASSWORD_PROMPT))
        krb5_get_init_creds_opt_set_change_password_prompt(options,
                                                           old_options->change_password_prompt);

    krb5_data_zero (&result_code_string);
    krb5_data_zero (&result_string);

    ret = krb5_get_init_creds_password (context,
					&cpw_cred,
					client,
					password,
					prompter,
					data,
					0,
					"kadmin/changepw",
					options);
    krb5_get_init_creds_opt_free(context, options);
    if (ret)
	goto out;

    for(;;) {
	password_data[0].data   = buf1;
	password_data[0].length = sizeof(buf1);

	prompts[0].hidden = 1;
	prompts[0].prompt = "New password: ";
	prompts[0].reply  = &password_data[0];
	prompts[0].type   = KRB5_PROMPT_TYPE_NEW_PASSWORD;

	password_data[1].data   = buf2;
	password_data[1].length = sizeof(buf2);

	prompts[1].hidden = 1;
	prompts[1].prompt = "Repeat new password: ";
	prompts[1].reply  = &password_data[1];
	prompts[1].type   = KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN;

	ret = (*prompter) (context, data, NULL, "Changing password",
			   2, prompts);
	if (ret) {
	    memset (buf1, 0, sizeof(buf1));
	    memset (buf2, 0, sizeof(buf2));
	    goto out;
	}

	if (strcmp (buf1, buf2) == 0)
	    break;
	memset (buf1, 0, sizeof(buf1));
	memset (buf2, 0, sizeof(buf2));
    }

    ret = krb5_set_password (context,
			     &cpw_cred,
			     buf1,
			     client,
			     &result_code,
			     &result_code_string,
			     &result_string);
    if (ret)
	goto out;

    if (asprintf(&p, "%s: %.*s\n",
		 result_code ? "Error" : "Success",
		 (int)result_string.length,
		 result_string.length > 0 ? (char*)result_string.data : "") < 0)
    {
	ret = krb5_enomem(context);
	goto out;
    }

    /* return the result */
    (*prompter) (context, data, NULL, p, 0, NULL);

    if (result_code == 0) {
	strlcpy (newpw, buf1, newpw_sz);
	ret = 0;
    } else {
	ret = ENOTTY;
	krb5_set_error_message(context, ret,
			       N_("failed changing password: %s", ""), p);
    }
    free (p);

out:
    memset_s(buf1, sizeof(buf1), 0, sizeof(buf1));
    memset_s(buf2, sizeof(buf2), 0, sizeof(buf2));
    krb5_data_free (&result_string);
    krb5_data_free (&result_code_string);
    krb5_free_cred_contents (context, &cpw_cred);
    return ret;
}


KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_keyblock_key_proc (krb5_context context,
			krb5_keytype type,
			krb5_data *salt,
			krb5_const_pointer keyseed,
			krb5_keyblock **key)
{
    return krb5_copy_keyblock (context, keyseed, key);
}

/*
 *
 */

static krb5_error_code
init_as_req (krb5_context context,
	     KDCOptions opts,
	     const krb5_creds *creds,
	     const krb5_addresses *addrs,
	     const krb5_enctype *etypes,
	     AS_REQ *a)
{
    krb5_error_code ret;

    memset(a, 0, sizeof(*a));

    a->pvno = 5;
    a->msg_type = krb_as_req;
    a->req_body.kdc_options = opts;
    a->req_body.cname = calloc(1, sizeof(*a->req_body.cname));
    if (a->req_body.cname == NULL) {
	ret = krb5_enomem(context);
	goto fail;
    }
    a->req_body.sname = calloc(1, sizeof(*a->req_body.sname));
    if (a->req_body.sname == NULL) {
	ret = krb5_enomem(context);
	goto fail;
    }

    ret = _krb5_principal2principalname (a->req_body.cname, creds->client);
    if (ret)
	goto fail;
    ret = copy_Realm(&creds->client->realm, &a->req_body.realm);
    if (ret)
	goto fail;

    ret = _krb5_principal2principalname (a->req_body.sname, creds->server);
    if (ret)
	goto fail;

    if(creds->times.starttime) {
	a->req_body.from = malloc(sizeof(*a->req_body.from));
	if (a->req_body.from == NULL) {
	    ret = krb5_enomem(context);
	    goto fail;
	}
	*a->req_body.from = creds->times.starttime;
    }
    if(creds->times.endtime){
	if ((ALLOC(a->req_body.till, 1)) != NULL)
            *a->req_body.till = creds->times.endtime;
        else {
            ret = krb5_enomem(context);
            goto fail;
        }
    }
    if(creds->times.renew_till){
	a->req_body.rtime = malloc(sizeof(*a->req_body.rtime));
	if (a->req_body.rtime == NULL) {
	    ret = krb5_enomem(context);
	    goto fail;
	}
	*a->req_body.rtime = creds->times.renew_till;
    }
    a->req_body.nonce = 0;
    ret = _krb5_init_etype(context,
			   KRB5_PDU_AS_REQUEST,
			   &a->req_body.etype.len,
			   &a->req_body.etype.val,
			   etypes);
    if (ret)
	goto fail;

    /*
     * This means no addresses
     */

    if (addrs && addrs->len == 0) {
	a->req_body.addresses = NULL;
    } else {
	a->req_body.addresses = malloc(sizeof(*a->req_body.addresses));
	if (a->req_body.addresses == NULL) {
	    ret = krb5_enomem(context);
	    goto fail;
	}

	if (addrs)
	    ret = krb5_copy_addresses(context, addrs, a->req_body.addresses);
	else {
	    ret = krb5_get_all_client_addrs (context, a->req_body.addresses);
	    if(ret == 0 && a->req_body.addresses->len == 0) {
		free(a->req_body.addresses);
		a->req_body.addresses = NULL;
	    }
	}
	if (ret)
	    goto fail;
    }

    a->req_body.enc_authorization_data = NULL;
    a->req_body.additional_tickets = NULL;

    a->padata = NULL;

    return 0;
 fail:
    free_AS_REQ(a);
    memset_s(a, sizeof(*a), 0, sizeof(*a));
    return ret;
}


static krb5_error_code
set_paid(struct pa_info_data *paid, krb5_context context,
	 krb5_enctype etype,
	 krb5_salttype salttype, void *salt_string, size_t salt_len,
	 krb5_data *s2kparams)
{
    paid->etype = etype;
    paid->salt.salttype = salttype;
    paid->salt.saltvalue.data = malloc(salt_len + 1);
    if (paid->salt.saltvalue.data == NULL) {
	krb5_clear_error_message(context);
	return krb5_enomem(context);
    }
    memcpy(paid->salt.saltvalue.data, salt_string, salt_len);
    ((char *)paid->salt.saltvalue.data)[salt_len] = '\0';
    paid->salt.saltvalue.length = salt_len;
    if (s2kparams) {
	krb5_error_code ret;

	ret = krb5_copy_data(context, s2kparams, &paid->s2kparams);
	if (ret) {
	    krb5_clear_error_message(context);
	    krb5_free_salt(context, paid->salt);
	    return ret;
	}
    } else
	paid->s2kparams = NULL;

    return 0;
}

static struct pa_info_data *
pa_etype_info2(krb5_context context,
	       const krb5_principal client,
	       const AS_REQ *asreq,
	       struct pa_info_data *paid,
	       heim_octet_string *data)
{
    krb5_error_code ret;
    ETYPE_INFO2 e;
    size_t sz;
    size_t i, j;

    memset(&e, 0, sizeof(e));
    ret = decode_ETYPE_INFO2(data->data, data->length, &e, &sz);
    if (ret)
	goto out;
    if (e.len == 0)
	goto out;
    for (j = 0; j < asreq->req_body.etype.len; j++) {
	for (i = 0; i < e.len; i++) {

	    if (krb5_enctype_valid(context, e.val[i].etype) != 0)
		continue;

	    if (asreq->req_body.etype.val[j] == e.val[i].etype) {
		krb5_salt salt;
		if (e.val[i].salt == NULL)
		    ret = krb5_get_pw_salt(context, client, &salt);
		else {
		    salt.saltvalue.data = *e.val[i].salt;
		    salt.saltvalue.length = strlen(*e.val[i].salt);
		    ret = 0;
		}
		if (ret == 0)
		    ret = set_paid(paid, context, e.val[i].etype,
				   KRB5_PW_SALT,
				   salt.saltvalue.data,
				   salt.saltvalue.length,
				   e.val[i].s2kparams);
		if (e.val[i].salt == NULL)
		    krb5_free_salt(context, salt);
		if (ret == 0) {
		    free_ETYPE_INFO2(&e);
		    return paid;
		}
	    }
	}
    }
 out:
    free_ETYPE_INFO2(&e);
    return NULL;
}

static struct pa_info_data *
pa_etype_info(krb5_context context,
	      const krb5_principal client,
	      const AS_REQ *asreq,
	      struct pa_info_data *paid,
	      heim_octet_string *data)
{
    krb5_error_code ret;
    ETYPE_INFO e;
    size_t sz;
    size_t i, j;

    memset(&e, 0, sizeof(e));
    ret = decode_ETYPE_INFO(data->data, data->length, &e, &sz);
    if (ret)
	goto out;
    if (e.len == 0)
	goto out;
    for (j = 0; j < asreq->req_body.etype.len; j++) {
	for (i = 0; i < e.len; i++) {

	    if (krb5_enctype_valid(context, e.val[i].etype) != 0)
		continue;

	    if (asreq->req_body.etype.val[j] == e.val[i].etype) {
		krb5_salt salt;
		salt.salttype = KRB5_PW_SALT;
		if (e.val[i].salt == NULL)
		    ret = krb5_get_pw_salt(context, client, &salt);
		else {
		    salt.saltvalue = *e.val[i].salt;
		    ret = 0;
		}
		if (e.val[i].salttype)
		    salt.salttype = *e.val[i].salttype;
		if (ret == 0) {
		    ret = set_paid(paid, context, e.val[i].etype,
				   salt.salttype,
				   salt.saltvalue.data,
				   salt.saltvalue.length,
				   NULL);
		    if (e.val[i].salt == NULL)
			krb5_free_salt(context, salt);
		}
		if (ret == 0) {
		    free_ETYPE_INFO(&e);
		    return paid;
		}
	    }
	}
    }
 out:
    free_ETYPE_INFO(&e);
    return NULL;
}

static struct pa_info_data *
pa_pw_or_afs3_salt(krb5_context context,
		   const krb5_principal client,
		   const AS_REQ *asreq,
		   struct pa_info_data *paid,
		   heim_octet_string *data)
{
    krb5_error_code ret;
    if (paid->etype == KRB5_ENCTYPE_NULL)
	return NULL;
    if (krb5_enctype_valid(context, paid->etype) != 0)
	return NULL;

    ret = set_paid(paid, context,
		   paid->etype,
		   paid->salt.salttype,
		   data->data,
		   data->length,
		   NULL);
    if (ret)
	return NULL;
    return paid;
}


static krb5_error_code
make_pa_enc_timestamp(krb5_context context, METHOD_DATA *md,
		      krb5_enctype etype, krb5_keyblock *key)
{
    PA_ENC_TS_ENC p;
    unsigned char *buf;
    size_t buf_size;
    size_t len = 0;
    EncryptedData encdata;
    krb5_error_code ret;
    int32_t usec;
    int usec2;
    krb5_crypto crypto;

    krb5_us_timeofday (context, &p.patimestamp, &usec);
    usec2         = usec;
    p.pausec      = &usec2;

    ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC, buf, buf_size, &p, &len, ret);
    if (ret)
	return ret;
    if(buf_size != len)
	krb5_abortx(context, "internal error in ASN.1 encoder");

    ret = krb5_crypto_init(context, key, 0, &crypto);
    if (ret) {
	free(buf);
	return ret;
    }
    ret = krb5_encrypt_EncryptedData(context,
				     crypto,
				     KRB5_KU_PA_ENC_TIMESTAMP,
				     buf,
				     len,
				     0,
				     &encdata);
    free(buf);
    krb5_crypto_destroy(context, crypto);
    if (ret)
	return ret;

    ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_size, &encdata, &len, ret);
    free_EncryptedData(&encdata);
    if (ret)
	return ret;
    if(buf_size != len)
	krb5_abortx(context, "internal error in ASN.1 encoder");

    ret = krb5_padata_add(context, md, KRB5_PADATA_ENC_TIMESTAMP, buf, len);
    if (ret)
	free(buf);
    return ret;
}

static krb5_error_code
add_enc_ts_padata(krb5_context context,
		  METHOD_DATA *md,
		  krb5_principal client,
		  krb5_s2k_proc keyproc,
		  krb5_const_pointer keyseed,
		  krb5_enctype *enctypes,
		  unsigned netypes,
		  krb5_salt *salt,
		  krb5_data *s2kparams)
{
    krb5_error_code ret;
    krb5_salt salt2;
    krb5_enctype *ep;
    size_t i;

    memset(&salt2, 0, sizeof(salt2));

    if(salt == NULL) {
	/* default to standard salt */
	ret = krb5_get_pw_salt (context, client, &salt2);
	if (ret)
	    return ret;
	salt = &salt2;
    }
    if (!enctypes) {
	enctypes = context->etypes;
	netypes = 0;
	for (ep = enctypes; *ep != (krb5_enctype)ETYPE_NULL; ep++)
	    netypes++;
    }

    for (i = 0; i < netypes; ++i) {
	krb5_keyblock *key;

	_krb5_debug(context, 5, "krb5_get_init_creds: using ENC-TS with enctype %d", enctypes[i]);

	ret = (*keyproc)(context, enctypes[i], keyseed,
			 *salt, s2kparams, &key);
	if (ret)
	    continue;
	ret = make_pa_enc_timestamp (context, md, enctypes[i], key);
	krb5_free_keyblock (context, key);
	if (ret)
	    return ret;
    }
    if(salt == &salt2)
	krb5_free_salt(context, salt2);
    return 0;
}

static krb5_error_code
pa_data_to_md_ts_enc(krb5_context context,
		     const AS_REQ *a,
		     const krb5_principal client,
		     krb5_init_creds_context ctx,
		     struct pa_info_data *ppaid,
		     METHOD_DATA *md)
{
    if (ctx->keyproc == NULL || ctx->keyseed == NULL)
	return 0;

    if (ppaid) {
	add_enc_ts_padata(context, md, client,
			  ctx->keyproc, ctx->keyseed,
			  &ppaid->etype, 1,
			  &ppaid->salt, ppaid->s2kparams);
    } else {
	krb5_salt salt;

	_krb5_debug(context, 5, "krb5_get_init_creds: pa-info not found, guessing salt");

	/* make a v5 salted pa-data */
	add_enc_ts_padata(context, md, client,
			  ctx->keyproc, ctx->keyseed,
			  a->req_body.etype.val, a->req_body.etype.len,
			  NULL, NULL);

	/* make a v4 salted pa-data */
	salt.salttype = KRB5_PW_SALT;
	krb5_data_zero(&salt.saltvalue);
	add_enc_ts_padata(context, md, client,
			  ctx->keyproc, ctx->keyseed,
			  a->req_body.etype.val, a->req_body.etype.len,
			  &salt, NULL);
    }
    return 0;
}

static krb5_error_code
pa_data_to_key_plain(krb5_context context,
		     const krb5_principal client,
		     krb5_init_creds_context ctx,
		     krb5_salt salt,
		     krb5_data *s2kparams,
		     krb5_enctype etype,
		     krb5_keyblock **key)
{
    krb5_error_code ret;

    ret = (*ctx->keyproc)(context, etype, ctx->keyseed,
			   salt, s2kparams, key);
    return ret;
}

struct pkinit_context {
    unsigned int win2k : 1;
    unsigned int used_pkinit : 1;
};


static krb5_error_code
pa_data_to_md_pkinit(krb5_context context,
		     const AS_REQ *a,
		     const krb5_principal client,
		     int win2k,
		     krb5_init_creds_context ctx,
		     METHOD_DATA *md)
{
    if (ctx->pk_init_ctx == NULL)
	return 0;
#ifdef PKINIT
    return _krb5_pk_mk_padata(context,
			      ctx->pk_init_ctx,
			      ctx->ic_flags,
			      win2k,
			      &a->req_body,
			      ctx->pk_nonce,
			      md);
#else
    krb5_set_error_message(context, EINVAL,
			   N_("no support for PKINIT compiled in", ""));
    return EINVAL;
#endif
}

static krb5_error_code
pkinit_configure_ietf(krb5_context context, krb5_init_creds_context ctx, void *pa_ctx)
{
    struct pkinit_context *pkinit_ctx = pa_ctx;

    pkinit_ctx->win2k = 0;

    if (ctx->pk_init_ctx == NULL)
	return HEIM_ERR_PA_CANT_CONTINUE;

    return 0;
}

static krb5_error_code
pkinit_configure_win(krb5_context context, krb5_init_creds_context ctx, void *pa_ctx)
{
    struct pkinit_context *pkinit_ctx = pa_ctx;

    pkinit_ctx->win2k = 1;
    pkinit_ctx->used_pkinit = 0;

    if (ctx->pk_init_ctx == NULL)
	return HEIM_ERR_PA_CANT_CONTINUE;

    return 0;
}

static krb5_error_code
pkinit_step(krb5_context context, krb5_init_creds_context ctx, void *pa_ctx, PA_DATA *pa, const AS_REQ *a,
	    const AS_REP *rep, const krb5_krbhst_info *hi, METHOD_DATA *in_md, METHOD_DATA *out_md)
{
    krb5_error_code ret = HEIM_ERR_PA_CANT_CONTINUE;
    struct pkinit_context *pkinit_ctx = pa_ctx;

    if (rep == NULL) {
	if (pkinit_ctx->used_pkinit) {
	    krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
				   "Already tried PKINIT(%s), looping",
				   pkinit_ctx->win2k ? "win2k" : "ietf");
	} else {
	    ret = pa_data_to_md_pkinit(context, a, ctx->cred.client,
				       (pkinit_ctx->win2k != 0),
				       ctx, out_md);
	    if (ret == 0)
		ret = HEIM_ERR_PA_CONTINUE_NEEDED;

	    pkinit_ctx->used_pkinit = 1;
	}
    } else if (pa) {
	ret = _krb5_pk_rd_pa_reply(context,
				   a->req_body.realm,
				   ctx->pk_init_ctx,
				   rep->enc_part.etype,
				   hi,
				   ctx->pk_nonce,
				   &ctx->req_buffer,
				   pa,
				   &ctx->fast_state.reply_key);
	if (ret == 0)
	    ctx->runflags.allow_save_as_reply_key = 1;
    }

    return ret;
}

static void
pkinit_release(void *pa_ctx)
{
}

/*
 * GSS-API pre-authentication support
 */

struct pa_gss_context {
    struct gss_ctx_id_t_desc_struct *context_handle;
    int open;
};

static krb5_error_code
pa_gss_configure(krb5_context context,
		 krb5_init_creds_context ctx,
		 void *pa_ctx)
{
    krb5_gss_init_ctx gssic = ctx->gss_init_ctx;
    struct pa_gss_context *pa_gss_ctx = pa_ctx;

    if (gssic == NULL)
	return HEIM_ERR_PA_CANT_CONTINUE;

    pa_gss_ctx->context_handle = NULL;
    pa_gss_ctx->open = 0;

    return 0;
}

static krb5_error_code
pa_data_to_md_gss(krb5_context context,
		  const AS_REQ *a,
		  const krb5_creds *creds,
		  krb5_init_creds_context ctx,
		  struct pa_gss_context *pa_gss_ctx,
		  PA_DATA *pa,
		  METHOD_DATA *out_md)
{
    krb5_error_code ret;
    krb5_gss_init_ctx gssic = ctx->gss_init_ctx;
    krb5_data req_body;
    krb5_data *input_token, output_token;
    size_t len = 0;

    krb5_data_zero(&req_body);
    krb5_data_zero(&output_token);

    input_token = pa ? &pa->padata_value : NULL;

    if ((input_token == NULL || input_token->length == 0) &&
	pa_gss_ctx->context_handle) {
	krb5_set_error_message(context, HEIM_ERR_PA_CANT_CONTINUE,
			       "Missing GSS preauthentication data from KDC");
	return HEIM_ERR_PA_CANT_CONTINUE;
    }

    ASN1_MALLOC_ENCODE(KDC_REQ_BODY, req_body.data, req_body.length,
		       &ctx->as_req.req_body, &len, ret);
    if (ret)
	goto out;
    heim_assert(req_body.length == len, "ASN.1 internal error");

    ret = gssic->step(context, gssic, creds, &pa_gss_ctx->context_handle,
		      ctx->flags, &req_body,
		      input_token, &output_token);

    /*
     * If FAST authenticated the KDC (which will be the case unless anonymous
     * PKINIT was used without KDC certificate validation) then we can relax
     * the mutual authentication requirement.
     */
    if (ret == KRB5_MUTUAL_FAILED &&
	(ctx->fast_state.flags & KRB5_FAST_EXPECTED) &&
	(ctx->fast_state.flags & KRB5_FAST_KDC_VERIFIED))
	ret = 0;
    if (ret == 0) {
	/*
	 * Always require a strengthen key if FAST was used, to avoid a MITM
	 * attack that could result in unintended privilege escalation should
	 * the KDC add positive authorization data from the armor ticket.
	 */
	if ((ctx->fast_state.flags & KRB5_FAST_EXPECTED) &&
	    ctx->fast_state.strengthen_key == NULL) {
	    krb5_set_error_message(context, HEIM_ERR_PA_CANT_CONTINUE,
				   "FAST GSS pre-authentication without strengthen key");
	    ret = KRB5_KDCREP_MODIFIED;
	    goto out;
	}

	pa_gss_ctx->open = 1;
    }

    if (output_token.length) {
	ret = krb5_padata_add(context, out_md, KRB5_PADATA_GSS,
			      output_token.data, output_token.length);
	if (ret)
	    goto out;

	krb5_data_zero(&output_token);
    }

out:
    krb5_data_free(&output_token);
    krb5_data_free(&req_body);

    return ret;
}

static krb5_error_code
pa_gss_step(krb5_context context,
	    krb5_init_creds_context ctx,
	    void *pa_ctx,
	    PA_DATA *pa,
	    const AS_REQ *a,
	    const AS_REP *rep,
	    const krb5_krbhst_info *hi,
	    METHOD_DATA *in_md,
	    METHOD_DATA *out_md)
{
    krb5_error_code ret;
    krb5_principal cname;
    krb5_gss_init_ctx gssic = ctx->gss_init_ctx;
    struct pa_gss_context *pa_gss_ctx = pa_ctx;

    heim_assert(gssic != NULL, "invalid context passed to pa_gss_step");

    if (!pa_gss_ctx->open) {
	ret = pa_data_to_md_gss(context, a, &ctx->cred, ctx,
				pa_gss_ctx, pa, out_md);
	if (ret == HEIM_ERR_PA_CONTINUE_NEEDED && rep) {
	    krb5_set_error_message(context, KRB5_PREAUTH_FAILED,
				   "KDC sent AS-REP before GSS "
				   "pre-authentication completed");
	    ret = KRB5_KDCREP_MODIFIED;
	} else if (ret == 0 && rep == NULL) {
	    ret = HEIM_ERR_PA_CONTINUE_NEEDED; /* odd number of legs */
	}
	if (ret)
	    return ret;
    } else if (pa && pa->padata_value.length) {
	krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
			       "Already completed GSS pre-authentication");
	return KRB5_GET_IN_TKT_LOOP;
    } else if (rep == NULL) {
	krb5_set_error_message(context, KRB5_PREAUTH_FAILED,
			       "Completed GSS pre-authentication before KDC");
	return KRB5_PREAUTH_FAILED;
    }

    heim_assert(pa_gss_ctx->open,
		"GSS pre-authentication incomplete");

    ret = gssic->finish(context, gssic, &ctx->cred,
			pa_gss_ctx->context_handle, ctx->nonce,
			rep->enc_part.etype, &cname,
			&ctx->fast_state.reply_key);
    if (ret)
	return ret;

    {
	char *from = NULL;
	char *to = NULL;

	if (krb5_unparse_name(context, ctx->cred.client, &from) == 0 &&
	    krb5_unparse_name(context, cname, &to) == 0) {
	    _krb5_debug(context, 1, "pa_gss_step: %s as %s",
			from, to);
	    krb5_xfree(from);
	    krb5_xfree(to);
	}
    }

    if (krb5_principal_is_federated(context, ctx->cred.client)) {
	/*
	 * The well-known federated name will be replaced with the cname
	 * in the AS-REP, but save the locally mapped initiator name in the
	 * cred for logging.
	 */
	krb5_free_principal(context, ctx->cred.client);
	ctx->cred.client = cname;

	ctx->ic_flags |= KRB5_INIT_CREDS_NO_C_CANON_CHECK;
    } else {
	krb5_free_principal(context, cname);
    }

    ctx->runflags.allow_save_as_reply_key = 1;

    gssic->delete_sec_context(context, gssic, pa_gss_ctx->context_handle);
    pa_gss_ctx->context_handle = NULL;
    pa_gss_ctx->open = 0;

    return 0;
}

static krb5_error_code
pa_gss_restart(krb5_context context,
	       krb5_init_creds_context ctx,
	       void *pa_ctx)
{
    krb5_gss_init_ctx gssic = ctx->gss_init_ctx;
    struct pa_gss_context *pa_gss_ctx = pa_ctx;

    if (gssic == NULL)
	return HEIM_ERR_PA_CANT_CONTINUE;

    gssic->delete_sec_context(context, gssic, pa_gss_ctx->context_handle);
    pa_gss_ctx->context_handle = NULL;
    pa_gss_ctx->open = 0;

    return 0;
}

static void
pa_gss_release(void *pa_ctx)
{
}

krb5_error_code
_krb5_make_pa_enc_challenge(krb5_context context,
			    krb5_crypto crypto,
			    krb5_key_usage usage,
			    METHOD_DATA *md)
{
    PA_ENC_TS_ENC p;
    unsigned char *buf;
    size_t buf_size;
    size_t len = 0;
    EncryptedData encdata;
    krb5_error_code ret;
    int32_t usec;
    int usec2;

    krb5_us_timeofday (context, &p.patimestamp, &usec);
    usec2         = usec;
    p.pausec      = &usec2;

    ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC, buf, buf_size, &p, &len, ret);
    if (ret)
	return ret;
    if(buf_size != len)
	krb5_abortx(context, "internal error in ASN.1 encoder");

    ret = krb5_encrypt_EncryptedData(context,
				     crypto,
				     usage,
				     buf,
				     len,
				     0,
				     &encdata);
    free(buf);
    if (ret)
	return ret;

    ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_size, &encdata, &len, ret);
    free_EncryptedData(&encdata);
    if (ret)
	return ret;
    if(buf_size != len)
	krb5_abortx(context, "internal error in ASN.1 encoder");

    ret = krb5_padata_add(context, md, KRB5_PADATA_ENCRYPTED_CHALLENGE, buf, len);
    if (ret)
	free(buf);
    return ret;
}

krb5_error_code
_krb5_validate_pa_enc_challenge(krb5_context context,
				krb5_crypto crypto,
				krb5_key_usage usage,
				EncryptedData *enc_data,
				const char *peer_name)
{
    krb5_error_code ret;
    krb5_data ts_data;
    PA_ENC_TS_ENC p;
    time_t timestamp;
    int32_t usec;
    size_t size;

    ret = krb5_decrypt_EncryptedData(context, crypto, usage, enc_data, &ts_data);
    if (ret)
	return ret;

    ret = decode_PA_ENC_TS_ENC(ts_data.data,
			       ts_data.length,
			       &p,
			       &size);
    krb5_data_free(&ts_data);
    if(ret){
	ret = KRB5KDC_ERR_PREAUTH_FAILED;
	_krb5_debug(context, 5, "Failed to decode PA-ENC-TS_ENC -- %s", peer_name);
	goto out;
    }

    krb5_us_timeofday(context, &timestamp, &usec);

    if (krb5_time_abs(timestamp, p.patimestamp) > context->max_skew) {
	char client_time[100];

	krb5_format_time(context, p.patimestamp,
			 client_time, sizeof(client_time), TRUE);

	ret = KRB5KRB_AP_ERR_SKEW;
	_krb5_debug(context, 0, "Too large time skew, "
		    "client time %s is out by %u > %d seconds -- %s",
		    client_time,
		    (unsigned)krb5_time_abs(timestamp, p.patimestamp),
		    (int)context->max_skew,
		    peer_name);
    } else {
	ret = 0;
    }

 out:
    free_PA_ENC_TS_ENC(&p);

    return ret;
}


static struct pa_info_data *
process_pa_info(krb5_context, const krb5_principal, const AS_REQ *, struct pa_info_data *, METHOD_DATA *);


static krb5_error_code
enc_chal_step(krb5_context context, krb5_init_creds_context ctx, void *pa_ctx, PA_DATA *pa, const AS_REQ *a,
	      const AS_REP *rep, const krb5_krbhst_info *hi, METHOD_DATA *in_md, METHOD_DATA *out_md)
{
    struct pa_info_data paid, *ppaid;
    krb5_keyblock challengekey;
    krb5_data pepper1, pepper2;
    krb5_crypto crypto = NULL;
    krb5_enctype aenctype;
    krb5_error_code ret;

    memset(&paid, 0, sizeof(paid));

    if (rep == NULL)
	paid.etype = KRB5_ENCTYPE_NULL;
    else
	paid.etype = rep->enc_part.etype;
    ppaid = process_pa_info(context, ctx->cred.client, a, &paid, in_md);

    /*
     * If we don't have ppaid, ts because the KDC have not sent any
     * salt info, lets to the first roundtrip so the KDC have a chance
     * to send any.
     */
    if (ppaid == NULL) {
	_krb5_debug(context, 5, "no ppaid found");
	return HEIM_ERR_PA_CONTINUE_NEEDED;
    }
    if (ppaid->etype == KRB5_ENCTYPE_NULL) {
	return HEIM_ERR_PA_CANT_CONTINUE;
    }

    if (ctx->fast_state.reply_key)
	krb5_free_keyblock(context, ctx->fast_state.reply_key);

    ret = pa_data_to_key_plain(context, ctx->cred.client, ctx,
			       ppaid->salt, ppaid->s2kparams, ppaid->etype,
			       &ctx->fast_state.reply_key);
    free_paid(context, &paid);
    if (ret) {
	_krb5_debug(context, 5, "enc-chal: failed to build key");
	return ret;
    }

    ret = krb5_crypto_init(context, ctx->fast_state.reply_key, 0, &crypto);
    if (ret)
	return ret;

    krb5_crypto_getenctype(context, ctx->fast_state.armor_crypto, &aenctype);

    pepper1.data = rep ? "kdcchallengearmor" : "clientchallengearmor";
    pepper1.length = strlen(pepper1.data);
    pepper2.data = "challengelongterm";
    pepper2.length = strlen(pepper2.data);

    ret = krb5_crypto_fx_cf2(context, ctx->fast_state.armor_crypto, crypto,
			     &pepper1, &pepper2, aenctype,
			     &challengekey);
    krb5_crypto_destroy(context, crypto);
    if (ret)
	return ret;

    ret = krb5_crypto_init(context, &challengekey, 0, &crypto);
    krb5_free_keyblock_contents(context, &challengekey);
    if (ret)
	return ret;

    if (rep) {
	EncryptedData enc_data;
	size_t size;

	if (ret) {
	    _krb5_debug(context, 5, "enc-chal: failed to create reply key");
	    return ret;
	}

	_krb5_debug(context, 5, "ENC_CHAL rep key");

	if (ctx->fast_state.strengthen_key == NULL) {
	    krb5_crypto_destroy(context, crypto);
	    _krb5_debug(context, 5, "ENC_CHAL w/o strengthen_key");
	    return KRB5_KDCREP_MODIFIED;
	}

	if (pa == NULL) {
	    krb5_crypto_destroy(context, crypto);
	    _krb5_debug(context, 0, "KDC response missing");
	    return HEIM_ERR_PA_CANT_CONTINUE;
	}

	ret = decode_EncryptedData(pa->padata_value.data,
				   pa->padata_value.length,
				   &enc_data,
				   &size);
	if (ret) {
	    ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
	    _krb5_debug(context, 5, "Failed to decode ENC_CHAL KDC reply");
	    return ret;
	}

	ret = _krb5_validate_pa_enc_challenge(context, crypto,
					      KRB5_KU_ENC_CHALLENGE_KDC,
					      &enc_data,
					      "KDC");
	free_EncryptedData(&enc_data);
	krb5_crypto_destroy(context, crypto);

	return ret;

    } else {

	ret = _krb5_make_pa_enc_challenge(context, crypto,
					  KRB5_KU_ENC_CHALLENGE_CLIENT,
					  out_md);
	krb5_crypto_destroy(context, crypto);
	if (ret) {
	    _krb5_debug(context, 5, "enc-chal: failed build enc challenge");
	    return ret;
	}

	return HEIM_ERR_PA_CONTINUE_NEEDED;
    }
}

struct enc_ts_context {
    int used_pa_types;
#define  USED_ENC_TS_GUESS	4
#define  USED_ENC_TS_INFO	8
#define  USED_ENC_TS_RENEG	16
    krb5_principal user;
};

static krb5_error_code
enc_ts_restart(krb5_context context, krb5_init_creds_context ctx, void *pa_ctx)
{
    struct enc_ts_context *pactx = (struct enc_ts_context *)pa_ctx;
    pactx->used_pa_types = 0;
    krb5_free_principal(context, pactx->user);
    pactx->user = NULL;
    return 0;
}

static krb5_error_code
enc_ts_step(krb5_context context, krb5_init_creds_context ctx, void *pa_ctx, PA_DATA *pa,
	    const AS_REQ *a,
	    const AS_REP *rep,
	    const krb5_krbhst_info *hi,
	    METHOD_DATA *in_md, METHOD_DATA *out_md)
{
    struct enc_ts_context *pactx = (struct enc_ts_context *)pa_ctx;
    struct pa_info_data paid, *ppaid;
    krb5_error_code ret;
    const char *state;
    unsigned flag;

    /*
     * Keep track of the user we used so that we can restart
     * authentication when we get referrals.
     */

    if (pactx->user && !krb5_principal_compare(context, pactx->user, ctx->cred.client)) {
	pactx->used_pa_types = 0;
	krb5_free_principal(context, pactx->user);
	pactx->user = NULL;
    }

    if (pactx->user == NULL) {
	ret = krb5_copy_principal(context, ctx->cred.client, &pactx->user);
	if (ret)
	    return ret;
    }

    memset(&paid, 0, sizeof(paid));

    if (rep == NULL)
	paid.etype = KRB5_ENCTYPE_NULL;
    else
	paid.etype = rep->enc_part.etype;

    ppaid = process_pa_info(context, ctx->cred.client, a, &paid, in_md);

    if (rep) {
	/*
	 * Some KDC's don't send salt info in the reply when there is
	 * success pre-auth happned before, so use cached copy (or
	 * even better, if there is just one pre-auth, save reply-key).
	 */
	if (ppaid == NULL && ctx->paid.etype != KRB5_ENCTYPE_NULL) {
	    ppaid = &ctx->paid;

	} else if (ppaid == NULL) {
	    _krb5_debug(context, 0, "no paid when building key, build a default salt structure ?");
	    return HEIM_ERR_PA_CANT_CONTINUE;
	}

	ret = pa_data_to_key_plain(context, ctx->cred.client, ctx,
				   ppaid->salt, ppaid->s2kparams, rep->enc_part.etype,
				   &ctx->fast_state.reply_key);
	free_paid(context, &paid);
	return ret;
    }

    /*
     * If we don't have ppaid, ts because the KDC have not sent any
     * salt info, lets to the first roundtrip so the KDC have a chance
     * to send any.
     *
     * Don't bother guessing, it sounds like a good idea until you run
     * into KDCs that are doing failed auth counting based on the
     * ENC_TS tries.
     *
     * Stashing the salt for the next run is a diffrent issue and
     * could be considered in the future.
     */

    if (ppaid == NULL) {
	_krb5_debug(context, 5,
		     "TS-ENC: waiting for KDC to set pw-salt/etype_info{,2}");
	return HEIM_ERR_PA_CONTINUE_NEEDED;
    }
    if (ppaid->etype == KRB5_ENCTYPE_NULL) {
	free_paid(context, &paid);
	_krb5_debug(context, 5,
		     "TS-ENC: kdc proposes enctype NULL ?");
	return HEIM_ERR_PA_CANT_CONTINUE;
    }

    /*
     * We have to allow the KDC to re-negotiate the PA-TS data
     * once, this is since the in the case of a windows read only
     * KDC that doesn't have the keys simply guesses what the
     * master is supposed to support. In the case where this
     * breaks in when the RO-KDC is a newer version the the RW-KDC
     * and the RO-KDC announced a enctype that the older doesn't
     * support.
     */
    if (pactx->used_pa_types & USED_ENC_TS_INFO) {
	flag = USED_ENC_TS_RENEG;
	state = "reneg";
    } else {
	flag = USED_ENC_TS_INFO;
	state = "info";
    }

    if (pactx->used_pa_types & flag) {
	free_paid(context, &paid);
	krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
			       "Already tried ENC-TS-%s, looping", state);
	return KRB5_GET_IN_TKT_LOOP;
    }

    pactx->used_pa_types |= flag;

    free_paid(context, &ctx->paid);
    ctx->paid = *ppaid;

    ret = pa_data_to_md_ts_enc(context, a, ctx->cred.client, ctx, ppaid, out_md);
    if (ret)
	return ret;

    return HEIM_ERR_PA_CONTINUE_NEEDED;
}

static void
enc_ts_release(void *pa_ctx)
{
    struct enc_ts_context *pactx = (struct enc_ts_context *)pa_ctx;

    if (pactx->user)
	krb5_free_principal(NULL, pactx->user);
}

static krb5_error_code
pa_pac_step(krb5_context context, krb5_init_creds_context ctx, void *pa_ctx, PA_DATA *pa, const AS_REQ *a,
	    const AS_REP *rep, const krb5_krbhst_info *hi,
	    METHOD_DATA *in_md, METHOD_DATA *out_md)
{
    size_t len = 0, length;
    krb5_error_code ret;
    PA_PAC_REQUEST req;
    void *buf;

    switch (ctx->req_pac) {
    case KRB5_INIT_CREDS_TRISTATE_UNSET:
	return 0; /* don't bother */
    case KRB5_INIT_CREDS_TRISTATE_TRUE:
	req.include_pac = 1;
	break;
    case KRB5_INIT_CREDS_TRISTATE_FALSE:
	req.include_pac = 0;
    }

    ASN1_MALLOC_ENCODE(PA_PAC_REQUEST, buf, length,
		       &req, &len, ret);
    if (ret)
	return ret;
    heim_assert(len == length, "internal error in ASN.1 encoder");

    ret = krb5_padata_add(context, out_md, KRB5_PADATA_PA_PAC_REQUEST, buf, len);
    if (ret)
	free(buf);

    return 0;
}

static krb5_error_code
pa_enc_pa_rep_step(krb5_context context, krb5_init_creds_context ctx, void *pa_ctx, PA_DATA *pa, const AS_REQ *a,
		   const AS_REP *rep, const krb5_krbhst_info *hi,
		   METHOD_DATA *in_md, METHOD_DATA *out_md)
{
    if (ctx->runflags.allow_enc_pa_rep)
	return krb5_padata_add(context, out_md, KRB5_PADATA_REQ_ENC_PA_REP, NULL, 0);

    return 0;
}

static krb5_error_code
pa_fx_cookie_step(krb5_context context,
		  krb5_init_creds_context ctx,
		  void *pa_ctx,
		  PA_DATA *pa,
		  const AS_REQ *a,
		  const AS_REP *rep,
		  const krb5_krbhst_info *hi,
		  METHOD_DATA *in_md,
		  METHOD_DATA *out_md)
{
    krb5_error_code ret;
    void *cookie;
    PA_DATA *pad;
    int idx = 0;

    pad = krb5_find_padata(in_md->val, in_md->len, KRB5_PADATA_FX_COOKIE, &idx);
    if (pad == NULL) {
	/*
	 * RFC 6113 5.4.3: PA-FX-COOKIE MUST be included if the KDC
	 * expects at least one more message from the client.
	 */
	if (ctx->error.error_code == KRB5_KDC_ERR_MORE_PREAUTH_DATA_REQUIRED)
	    return KRB5_PREAUTH_FAILED;
	else
	    return 0;
    }

    cookie = malloc(pad->padata_value.length);
    if (cookie == NULL)
	return krb5_enomem(context);

    memcpy(cookie, pad->padata_value.data, pad->padata_value.length);

    ret = krb5_padata_add(context, out_md, KRB5_PADATA_FX_COOKIE,
			  cookie, pad->padata_value.length);
    if (ret)
	free(cookie);
    else
	_krb5_debug(context, 5, "Mirrored FX-COOKIE to KDC");

    return ret;
}

typedef struct pa_info_data *(*pa_salt_info_f)(krb5_context, const krb5_principal, const AS_REQ *, struct pa_info_data *, heim_octet_string *);
typedef krb5_error_code (*pa_configure_f)(krb5_context, krb5_init_creds_context, void *);
typedef krb5_error_code (*pa_restart_f)(krb5_context, krb5_init_creds_context, void *);
typedef krb5_error_code (*pa_step_f)(krb5_context, krb5_init_creds_context, void *, PA_DATA *, const AS_REQ *, const AS_REP *, const krb5_krbhst_info *, METHOD_DATA *, METHOD_DATA *);
typedef void            (*pa_release_f)(void *);

struct patype {
    int type;
    char *name;
    int flags;
#define PA_F_ANNOUNCE		1
#define PA_F_CONFIG		2
#define PA_F_FAST		4 /* available inside FAST */
#define PA_F_NOT_FAST		8 /* only available without FAST */
    size_t pa_ctx_size;
    pa_salt_info_f salt_info;
    /**
     * Return 0 if the PA-mechanism is available and optionally set pa_ctx pointer to non-NULL.
     */
    pa_configure_f configure;
    /**
     * Return 0 if the PA-mechanism can be restarted (time skew, referrals, etc)
     */
    pa_restart_f restart;
    /**
     * Return 0 if the when complete, HEIM_ERR_PA_CONTINUE_NEEDED if more steps are require
     */
    pa_step_f step;
    pa_release_f release;
} patypes[] = {
    {
	KRB5_PADATA_PK_AS_REP,
	"PKINIT(IETF)",
	PA_F_FAST | PA_F_NOT_FAST,
	sizeof(struct pkinit_context),
	NULL,
	pkinit_configure_ietf,
	NULL,
	pkinit_step,
	pkinit_release
    },
    {
	KRB5_PADATA_PK_AS_REP_19,
	"PKINIT(win)",
	PA_F_FAST | PA_F_NOT_FAST,
	sizeof(struct pkinit_context),
	NULL,
	pkinit_configure_win,
	NULL,
	pkinit_step,
	pkinit_release
    },
    {
	KRB5_PADATA_GSS,
	"GSS",
	PA_F_FAST | PA_F_NOT_FAST,
	sizeof(struct pa_gss_context),
	NULL,
	pa_gss_configure,
	pa_gss_restart,
	pa_gss_step,
	pa_gss_release
    },
    {
	KRB5_PADATA_ENCRYPTED_CHALLENGE,
	"ENCRYPTED_CHALLENGE",
	PA_F_FAST,
	0,
	NULL,
	NULL,
	NULL,
	enc_chal_step,
	NULL
    },
    {
	KRB5_PADATA_ENC_TIMESTAMP,
	"ENCRYPTED_TIMESTAMP",
	PA_F_NOT_FAST,
	sizeof(struct enc_ts_context),
	NULL,
	NULL,
	enc_ts_restart,
	enc_ts_step,
	enc_ts_release
    },
    {
	KRB5_PADATA_PA_PAC_REQUEST,
	"PA_PAC_REQUEST",
	PA_F_CONFIG,
	0,
	NULL,
	NULL,
	NULL,
	pa_pac_step,
	NULL
    },
    {
	KRB5_PADATA_REQ_ENC_PA_REP,
	"REQ-ENC-PA-REP",
	PA_F_CONFIG,
	0,
	NULL,
	NULL,
	NULL,
	pa_enc_pa_rep_step,
	NULL
    },
    {
	KRB5_PADATA_FX_COOKIE,
	"FX-COOKIE",
	PA_F_CONFIG,
	0,
	NULL,
	NULL,
	NULL,
	pa_fx_cookie_step,
	NULL
    },
#define patype_salt(n, f) { KRB5_PADATA_##n, #n, 0, 0, f, NULL, NULL, NULL, NULL }
    patype_salt(ETYPE_INFO2, pa_etype_info2),
    patype_salt(ETYPE_INFO, pa_etype_info),
    patype_salt(PW_SALT, pa_pw_or_afs3_salt),
    patype_salt(AFS3_SALT, pa_pw_or_afs3_salt),
#undef patype_salt
    /* below are just for pretty printing */
#define patype_info(n) { KRB5_PADATA_##n, #n, 0, 0, NULL, NULL, NULL, NULL, NULL }
    patype_info(AUTHENTICATION_SET),
    patype_info(AUTH_SET_SELECTED),
    patype_info(FX_FAST),
    patype_info(FX_ERROR),
    patype_info(PKINIT_KX),
    patype_info(PK_AS_REQ)
#undef patype_info
};

static const char *
get_pa_type_name(int type)
{
    size_t n;
    for (n = 0; n < sizeof(patypes)/sizeof(patypes[0]); n++)
	if (type == patypes[n].type)
	    return patypes[n].name;
    return "unknown";
}

/*
 *
 */

struct pa_auth_mech {
    struct patype *patype;
    struct pa_auth_mech *next; /* when doing authentication sets */
    char pactx[1];
};

/*
 *
 */

static struct pa_info_data *
process_pa_info(krb5_context context,
		const krb5_principal client,
		const AS_REQ *asreq,
		struct pa_info_data *paid,
		METHOD_DATA *md)
{
    struct pa_info_data *p = NULL;
    PA_DATA *pa;
    size_t i;

    if (md == NULL)
	return NULL;

    for (i = 0; p == NULL && i < sizeof(patypes)/sizeof(patypes[0]); i++) {
	int idx = 0;

	if (patypes[i].salt_info == NULL)
	    continue;

	pa = krb5_find_padata(md->val, md->len, patypes[i].type, &idx);
	if (pa == NULL)
	    continue;

	paid->salt.salttype = (krb5_salttype)patypes[i].type;
	p = patypes[i].salt_info(context, client, asreq, paid, &pa->padata_value);
    }
    return p;
}

static void
pa_announce(krb5_context context,
	    int types,
	    krb5_init_creds_context ctx,
	    METHOD_DATA *in_md,
	    METHOD_DATA *out_md)
{
    size_t n;

    for (n = 0; n < sizeof(patypes)/sizeof(patypes[0]); n++) {
	if ((patypes[n].flags & types) == 0)
	    continue;

	if (patypes[n].step)
	    patypes[n].step(context, ctx, NULL, NULL, NULL, NULL, NULL, in_md, out_md);
	else
	    krb5_padata_add(context, out_md, patypes[n].type, NULL, 0);
    }
}


static void
mech_dealloc(void *ctx)
{
    struct pa_auth_mech *pa_mech = ctx;
    if (pa_mech->patype->release)
	pa_mech->patype->release((void *)&pa_mech->pactx[0]);
}

struct heim_type_data pa_auth_mech_object = {
    HEIM_TID_PA_AUTH_MECH,
    "heim-pa-mech-context",
    NULL,
    mech_dealloc,
    NULL,
    NULL,
    NULL,
    NULL
};

static struct pa_auth_mech *
pa_mech_create(krb5_context context, krb5_init_creds_context ctx, int pa_type)
{
    struct pa_auth_mech *pa_mech;
    struct patype *patype = NULL;
    size_t n;

    for (n = 0; patype == NULL && n < sizeof(patypes)/sizeof(patypes[0]); n++) {
	if (patypes[n].type == pa_type)
	    patype = &patypes[n];
    }
    if (patype == NULL)
	return NULL;

    pa_mech = _heim_alloc_object(&pa_auth_mech_object, sizeof(*pa_mech) - 1 + patype->pa_ctx_size);
    if (pa_mech == NULL)
	return NULL;

    pa_mech->patype = patype;

    if (pa_mech->patype->configure) {
	krb5_error_code ret;

	ret = pa_mech->patype->configure(context, ctx, &pa_mech->pactx[0]);
	if (ret) {
	    heim_release(pa_mech);
	    return NULL;
	}
    }

    _krb5_debug(context, 5, "Adding PA mech: %s", patype->name);

    return pa_mech;
}

static void
pa_mech_add(krb5_context context, krb5_init_creds_context ctx, int pa_type)
{
    struct pa_auth_mech *mech;

    mech = pa_mech_create(context, ctx, pa_type);
    if (mech) {
	heim_array_append_value(ctx->available_pa_mechs, mech);
	heim_release(mech);
    }
}

static krb5_error_code
pa_configure(krb5_context context,
	     krb5_init_creds_context ctx,
	     METHOD_DATA *in_md)
{
    ctx->available_pa_mechs = heim_array_create();

    if (ctx->gss_init_ctx) {
	pa_mech_add(context, ctx, KRB5_PADATA_GSS);
    } else if (ctx->pk_init_ctx) {
	pa_mech_add(context, ctx, KRB5_PADATA_PK_AS_REP);
	pa_mech_add(context, ctx, KRB5_PADATA_PK_AS_REP_19);
    } else if (ctx->keyproc || ctx->keyseed || ctx->prompter) {
	pa_mech_add(context, ctx, KRB5_PADATA_ENCRYPTED_CHALLENGE);
	pa_mech_add(context, ctx, KRB5_PADATA_ENC_TIMESTAMP);
    }
    /* XXX setup context based on KDC reply */

    return 0;
}

static krb5_error_code
pa_restart(krb5_context context,
	   krb5_init_creds_context ctx)
{
    krb5_error_code ret = HEIM_ERR_PA_CANT_CONTINUE;

    if (ctx->pa_mech && ctx->pa_mech->patype->restart)
	ret = ctx->pa_mech->patype->restart(context, ctx, (void *)&ctx->pa_mech->pactx[0]);

    return ret;
}


static krb5_error_code
pa_step(krb5_context context,
	krb5_init_creds_context ctx,
	const AS_REQ *a,
	const AS_REP *rep,
	const krb5_krbhst_info *hi,
	METHOD_DATA *in_md,
	METHOD_DATA *out_md)
{
    krb5_error_code ret;
    PA_DATA *pa = NULL;
    int idx;

 next:
    do {
	if (ctx->pa_mech == NULL) {
	    size_t len = heim_array_get_length(ctx->available_pa_mechs);
	    if (len == 0) {
		_krb5_debug(context, 0, "no more available_pa_mechs to try");
		return HEIM_ERR_NO_MORE_PA_MECHS;
	    }

	    ctx->pa_mech = heim_array_copy_value(ctx->available_pa_mechs, 0);
	    heim_array_delete_value(ctx->available_pa_mechs, 0);
	}

	if (ctx->fast_state.armor_crypto) {
	    if ((ctx->pa_mech->patype->flags & PA_F_FAST) == 0) {
		_krb5_debug(context, 0, "pa-mech %s dropped under FAST (not supported)",
			    ctx->pa_mech->patype->name);
		heim_release(ctx->pa_mech);
		ctx->pa_mech = NULL;
		continue;
	    }
	} else {
	    if ((ctx->pa_mech->patype->flags & PA_F_NOT_FAST) == 0) {
		_krb5_debug(context, 0, "dropped pa-mech %s since not running under FAST",
			    ctx->pa_mech->patype->name);
		heim_release(ctx->pa_mech);
		ctx->pa_mech = NULL;
		continue;
	    }
	}

	_krb5_debug(context, 0, "pa-mech trying: %s, searching for %d",
		    ctx->pa_mech->patype->name, ctx->pa_mech->patype->type);

	idx = 0;
	if (in_md)
	    pa = krb5_find_padata(in_md->val, in_md->len, ctx->pa_mech->patype->type, &idx);
	else
	    pa = NULL;

    } while (ctx->pa_mech == NULL);

    _krb5_debug(context, 5, "Stepping pa-mech: %s", ctx->pa_mech->patype->name);

    ret = ctx->pa_mech->patype->step(context, ctx, (void *)&ctx->pa_mech->pactx[0], pa, a, rep, hi, in_md, out_md);
    _krb5_debug(context, 10, "PA type %s returned %d", ctx->pa_mech->patype->name, ret);
    if (ret == 0) {
	struct pa_auth_mech *next_pa = ctx->pa_mech->next;

	if (next_pa) {
	    _krb5_debug(context, 5, "Next PA type in set is: %s",
			 next_pa->patype->name);
	    ret = HEIM_ERR_PA_CONTINUE_NEEDED;
	} else if (rep == NULL) {
	    _krb5_debug(context, 5, "PA %s done, but no ticket in sight!!!",
			 ctx->pa_mech->patype->name);
	    ret = HEIM_ERR_PA_CANT_CONTINUE;
	} else {
	    ctx->pa_used = ctx->pa_mech->patype->name;
	}

	heim_retain(next_pa);
	heim_release(ctx->pa_mech);
	ctx->pa_mech = next_pa;
    }

    if (ret == HEIM_ERR_PA_CANT_CONTINUE) {
	if (ctx->pa_mech) {
	    _krb5_debug(context, 5, "Dropping PA type %s", ctx->pa_mech->patype->name);
	    heim_release(ctx->pa_mech);
	    ctx->pa_mech = NULL;
	}
	goto next;
    } else if (ret == HEIM_ERR_PA_CONTINUE_NEEDED) {
	_krb5_debug(context, 5, "Continue needed for %s", ctx->pa_mech->patype->name);
    } else if (ret != 0) {
	_krb5_debug(context, 5, "Other error from mech %s: %d", ctx->pa_mech->patype->name, ret);
	heim_release(ctx->pa_mech);
	ctx->pa_mech = NULL;
    }

    return ret;
}

static void
log_kdc_pa_types(krb5_context context, METHOD_DATA *in_md)
{
    if (_krb5_have_debug(context, 5)) {
	unsigned i;
	_krb5_debug(context, 5, "KDC sent %d patypes", in_md->len);
	for (i = 0; i < in_md->len; i++)
	    _krb5_debug(context, 5, "KDC sent PA-DATA type: %d (%s)",
			 in_md->val[i].padata_type,
			 get_pa_type_name(in_md->val[i].padata_type));
    }
}

/*
 * Assumes caller always will free `out_md', even on error.
 */

static krb5_error_code
process_pa_data_to_md(krb5_context context,
		      const krb5_creds *creds,
		      const AS_REQ *a,
		      krb5_init_creds_context ctx,
		      METHOD_DATA *in_md,
		      METHOD_DATA **out_md)
{
    krb5_error_code ret;

    ALLOC(*out_md, 1);
    if (*out_md == NULL) {
	return krb5_enomem(context);
    }
    (*out_md)->len = 0;
    (*out_md)->val = NULL;

    log_kdc_pa_types(context, in_md);

    ret = pa_step(context, ctx, a, NULL, NULL, in_md, *out_md);
    if (ret == HEIM_ERR_PA_CONTINUE_NEEDED) {
	_krb5_debug(context, 0, "pamech need more stepping");
    } else if (ret == 0) {
	_krb5_debug(context, 0, "pamech done step");
    } else {
	return ret;
    }

    /*
     * Send announcement (what we support) and configuration (user
     * introduced behavior change)
     */

    pa_announce(context, PA_F_ANNOUNCE|PA_F_CONFIG, ctx, in_md, *out_md);

    /*
     *
     */

    if ((*out_md)->len == 0) {
	free(*out_md);
	*out_md = NULL;
    }

    return 0;
}

static krb5_error_code
process_pa_data_to_key(krb5_context context,
		       krb5_init_creds_context ctx,
		       krb5_creds *creds,
		       AS_REQ *a,
		       AS_REP *rep,
		       const krb5_krbhst_info *hi,
		       krb5_keyblock **key)
{
    struct pa_info_data paid, *ppaid = NULL;
    krb5_error_code ret;
    krb5_enctype etype = rep->enc_part.etype;

    memset(&paid, 0, sizeof(paid));

    if (rep->padata)
	log_kdc_pa_types(context, rep->padata);

    if (rep->padata) {
	paid.etype = etype;
	ppaid = process_pa_info(context, creds->client, a, &paid,
				rep->padata);
    }
    if (ppaid == NULL) {
	if (ctx->paid.etype == KRB5_ENCTYPE_NULL) {
	    ctx->paid.etype = etype;
	    ctx->paid.s2kparams = NULL;
	    ret = krb5_get_pw_salt (context, creds->client, &ctx->paid.salt);
	    if (ret)
		return ret;
	}
    }

    ret = pa_step(context, ctx, a, rep, hi, rep->padata, NULL);
    if (ret == HEIM_ERR_PA_CONTINUE_NEEDED) {
	_krb5_debug(context, 0, "In final stretch and pa require more stepping ?");
	return ret;
    } else if (ret == 0) {
	_krb5_debug(context, 0, "final pamech done step");
	goto out;
    } else {
	return ret;
    }
 out:
    free_paid(context, &paid);
    return ret;
}

/*
 *
 */

static krb5_error_code
capture_lkdc_domain(krb5_context context,
		    krb5_init_creds_context ctx)
{
    size_t len;

    len = strlen(_krb5_wellknown_lkdc);

    if (ctx->kdc_hostname != NULL ||
	strncmp(ctx->cred.client->realm, _krb5_wellknown_lkdc, len) != 0 ||
	ctx->cred.client->realm[len] != ':')
	return 0;

    ctx->kdc_hostname = strdup(&ctx->cred.client->realm[len + 1]);

    _krb5_debug(context, 5, "krb5_get_init_creds: setting LKDC hostname to: %s",
		ctx->kdc_hostname);
    return 0;
}

/**
 * Start a new context to get a new initial credential.
 *
 * @param context A Kerberos 5 context.
 * @param client The Kerberos principal to get the credential for, if
 *     NULL is given, the default principal is used as determined by
 *     krb5_get_default_principal().
 * @param prompter
 * @param prompter_data
 * @param start_time the time the ticket should start to be valid or 0 for now.
 * @param options a options structure, can be NULL for default options.
 * @param rctx A new allocated free with krb5_init_creds_free().
 *
 * @return 0 for success or an Kerberos 5 error code, see krb5_get_error_message().
 *
 * @ingroup krb5_credential
 */

KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_init_creds_init(krb5_context context,
		     krb5_principal client,
		     krb5_prompter_fct prompter,
		     void *prompter_data,
		     krb5_deltat start_time,
		     krb5_get_init_creds_opt *options,
		     krb5_init_creds_context *rctx)
{
    krb5_init_creds_context ctx;
    krb5_error_code ret;

    *rctx = NULL;

    ctx = calloc(1, sizeof(*ctx));
    if (ctx == NULL)
	return krb5_enomem(context);

    ret = get_init_creds_common(context, client, prompter, prompter_data,
				 start_time, options, ctx);
    if (ret) {
	free(ctx);
	return ret;
    }

    /* Set a new nonce. */
    /* FIXME should generate a new nonce for each AS-REQ */
    krb5_generate_random_block (&ctx->nonce, sizeof(ctx->nonce));
    ctx->nonce &= 0x7fffffff;
    /* XXX these just needs to be the same when using Windows PK-INIT */
    ctx->pk_nonce = ctx->nonce;

    ctx->prompter = prompter;
    ctx->prompter_data = prompter_data;

    /* pick up hostname from LKDC realm name */
    ret = capture_lkdc_domain(context, ctx);
    if (ret) {
	free_init_creds_ctx(context, ctx);
	return ret;
    }

    ctx->runflags.allow_enc_pa_rep = 1;

    ctx->fast_state.flags |= KRB5_FAST_AS_REQ;

    *rctx = ctx;

    return ret;
}

/**
 * Set the KDC hostname for the initial request, it will not be
 * considered in referrals to another KDC.
 *
 * @param context a Kerberos 5 context.
 * @param ctx a krb5_init_creds_context context.
 * @param hostname the hostname for the KDC of realm
 *
 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
 * @ingroup krb5_credential
 */

krb5_error_code KRB5_LIB_FUNCTION
krb5_init_creds_set_kdc_hostname(krb5_context context,
				 krb5_init_creds_context ctx,
				 const char *hostname)
{
    if (ctx->kdc_hostname)
	free(ctx->kdc_hostname);
    ctx->kdc_hostname = strdup(hostname);
    if (ctx->kdc_hostname == NULL)
	return krb5_enomem(context);
    return 0;
}

/**
 * Set the sitename for the request
 *
 */

krb5_error_code KRB5_LIB_FUNCTION
krb5_init_creds_set_sitename(krb5_context context,
			     krb5_init_creds_context ctx,
			     const char *sitename)
{
    if (ctx->sitename)
	free(ctx->sitename);
    ctx->sitename = strdup(sitename);
    if (ctx->sitename == NULL)
	return krb5_enomem(context);
    return 0;
}

/**
 * Sets the service that the is requested. This call is only neede for
 * special initial tickets, by default the a krbtgt is fetched in the default realm.
 *
 * @param context a Kerberos 5 context.
 * @param ctx a krb5_init_creds_context context.
 * @param service the service given as a string, for example
 *        "kadmind/admin". If NULL, the default krbtgt in the clients
 *        realm is set.
 *
 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
 * @ingroup krb5_credential
 */

KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_init_creds_set_service(krb5_context context,
			    krb5_init_creds_context ctx,
			    const char *service)
{
    krb5_const_realm client_realm;
    krb5_principal principal;
    krb5_error_code ret;

    client_realm = krb5_principal_get_realm (context, ctx->cred.client);

    if (service) {
	ret = krb5_parse_name (context, service, &principal);
	if (ret)
	    return ret;
	krb5_principal_set_realm (context, principal, client_realm);
    } else {
	ret = krb5_make_principal(context, &principal,
				  client_realm, KRB5_TGS_NAME, client_realm,
				  NULL);
	if (ret)
	    return ret;
    }

    /*
     * This is for Windows RODC that are picky about what name type
     * the server principal have, and the really strange part is that
     * they are picky about the AS-REQ name type and not the TGS-REQ
     * later. Oh well.
     */

    if (krb5_principal_is_krbtgt(context, principal))
	krb5_principal_set_type(context, principal, KRB5_NT_SRV_INST);

    krb5_free_principal(context, ctx->cred.server);
    ctx->cred.server = principal;

    return 0;
}

/**
 * Sets the password that will use for the request.
 *
 * @param context a Kerberos 5 context.
 * @param ctx ctx krb5_init_creds_context context.
 * @param password the password to use.
 *
 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
 * @ingroup krb5_credential
 */

KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_init_creds_set_password(krb5_context context,
			     krb5_init_creds_context ctx,
			     const char *password)
{
    if (ctx->password) {
	size_t len;
	len = strlen(ctx->password);
	memset_s(ctx->password, len, 0, len);
	free(ctx->password);
    }
    if (password) {
	ctx->password = strdup(password);
	if (ctx->password == NULL)
	    return krb5_enomem(context);
	ctx->keyseed = (void *) ctx->password;
    } else {
	ctx->keyseed = NULL;
	ctx->password = NULL;
    }

    return 0;
}

static krb5_error_code KRB5_CALLCONV
keytab_key_proc(krb5_context context, krb5_enctype enctype,
		krb5_const_pointer keyseed,
		krb5_salt salt, krb5_data *s2kparms,
		krb5_keyblock **key)
{
    krb5_keytab_key_proc_args *args  = rk_UNCONST(keyseed);
    krb5_keytab keytab = args->keytab;
    krb5_principal principal = args->principal;
    krb5_error_code ret;
    krb5_keytab real_keytab;
    krb5_keytab_entry entry;

    if (keytab == NULL) {
	ret = krb5_kt_default(context, &real_keytab);
	if (ret)
	    return ret;
    } else
	real_keytab = keytab;

    ret = krb5_kt_get_entry (context, real_keytab, principal,
			     0, enctype, &entry);

    if (keytab == NULL)
	krb5_kt_close (context, real_keytab);

    if (ret)
	return ret;

    ret = krb5_copy_keyblock (context, &entry.keyblock, key);
    krb5_kt_free_entry(context, &entry);
    return ret;
}


/**
 * Set the keytab to use for authentication.
 *
 * @param context a Kerberos 5 context.
 * @param ctx ctx krb5_init_creds_context context.
 * @param keytab the keytab to read the key from.
 *
 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
 * @ingroup krb5_credential
 */

KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_init_creds_set_keytab(krb5_context context,
			   krb5_init_creds_context ctx,
			   krb5_keytab keytab)
{
    krb5_keytab_key_proc_args *a;
    krb5_keytab_entry entry;
    krb5_kt_cursor cursor;
    krb5_enctype *etypes = NULL;
    krb5_error_code ret;
    size_t netypes = 0;
    int kvno = 0, found = 0;
    unsigned n;

    a = malloc(sizeof(*a));
    if (a == NULL)
	return krb5_enomem(context);

    a->principal = ctx->cred.client;
    a->keytab    = keytab;

    ctx->keytab_data = a;
    ctx->keyseed = (void *)a;
    ctx->keyproc = keytab_key_proc;

    /*
     * We need to the KDC what enctypes we support for this keytab,
     * esp if the keytab is really a password based entry, then the
     * KDC might have more enctypes in the database then what we have
     * in the keytab.
     */

    ret = krb5_kt_start_seq_get(context, keytab, &cursor);
    if(ret)
	goto out;

    while(krb5_kt_next_entry(context, keytab, &entry, &cursor) == 0){
	void *ptr;

	if (!krb5_principal_compare(context, entry.principal, ctx->cred.client))
	    goto next;

	found = 1;

	/* check if we ahve this kvno already */
	if (entry.vno > kvno) {
	    /* remove old list of etype */
	    if (etypes)
		free(etypes);
	    etypes = NULL;
	    netypes = 0;
	    kvno = entry.vno;
	} else if (entry.vno != kvno)
	    goto next;

	/* check if enctype is supported */
	if (krb5_enctype_valid(context, entry.keyblock.keytype) != 0)
	    goto next;

	/*
	 * If user already provided a enctype list, use that as an
	 * additonal filter.
	 */
	if (ctx->etypes) {
	    for (n = 0; ctx->etypes[n] != KRB5_ENCTYPE_NULL; n++) {
		if (ctx->etypes[n] == entry.keyblock.keytype)
		    break;
	    }
	    if (ctx->etypes[n] == KRB5_ENCTYPE_NULL)
		goto next;
	}

	/* add enctype to supported list */
	ptr = realloc(etypes, sizeof(etypes[0]) * (netypes + 2));
	if (ptr == NULL) {
	    free(etypes);
	    ret = krb5_enomem(context);
	    goto out;
	}

	etypes = ptr;
	etypes[netypes] = entry.keyblock.keytype;
	etypes[netypes + 1] = ETYPE_NULL;
	netypes++;
    next:
	krb5_kt_free_entry(context, &entry);
    }
    krb5_kt_end_seq_get(context, keytab, &cursor);

    if (etypes) {
	if (ctx->etypes)
	    free(ctx->etypes);
	ctx->etypes = etypes;
    }

 out:
    if (!found) {
	if (ret == 0)
	    ret = KRB5_KT_NOTFOUND;
	_krb5_kt_principal_not_found(context, ret, keytab, ctx->cred.client, 0, 0);
    }

    return ret;
}

static krb5_error_code KRB5_CALLCONV
keyblock_key_proc(krb5_context context, krb5_enctype enctype,
		  krb5_const_pointer keyseed,
		  krb5_salt salt, krb5_data *s2kparms,
		  krb5_keyblock **key)
{
    return krb5_copy_keyblock (context, keyseed, key);
}

KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_init_creds_set_keyblock(krb5_context context,
			     krb5_init_creds_context ctx,
			     krb5_keyblock *keyblock)
{
    ctx->keyseed = (void *)keyblock;
    ctx->keyproc = keyblock_key_proc;

    return 0;
}

KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_init_creds_set_fast_ccache(krb5_context context,
				krb5_init_creds_context ctx,
				krb5_ccache fast_ccache)
{
    krb5_creds *cred = NULL;
    krb5_error_code ret;
    krb5_data data;

    ret = _krb5_get_krbtgt(context, fast_ccache, NULL, &cred);
    if (ret)
	return ret;

    ret = krb5_cc_get_config(context, fast_ccache, cred->server,
			     "fast_avail", &data);
    krb5_free_creds(context, cred);
    if (ret == 0) {
	ctx->fast_state.armor_ccache = fast_ccache;
	ctx->fast_state.flags |= KRB5_FAST_REQUIRED;
	ctx->fast_state.flags |= KRB5_FAST_KDC_VERIFIED;
    } else {
	krb5_set_error_message(context, EINVAL, N_("FAST not available for the KDC in the armor ccache", ""));
	return EINVAL;
    }
    return 0;
}

static krb5_error_code
validate_pkinit_fx(krb5_context context,
		   krb5_init_creds_context ctx,
		   AS_REP *rep,
		   krb5_keyblock *ticket_sessionkey)
{
    PA_DATA *pa = NULL;
    int idx = 0;

    if (rep->padata)
	pa = krb5_find_padata(rep->padata->val, rep->padata->len, KRB5_PADATA_PKINIT_KX, &idx);

    if (pa == NULL) {
	if (ctx->flags.request_anonymous && ctx->pk_init_ctx) {
	    /* XXX handle the case where pkinit is not used */
	    krb5_set_error_message(context, KRB5_KDCREP_MODIFIED,
				   N_("Requested anonymous with PKINIT and KDC didn't set PKINIT_KX", ""));
	    return KRB5_KDCREP_MODIFIED;
	}

	return 0;
    }

    heim_assert(ctx->fast_state.reply_key != NULL, "must have a reply key at this stage");

    return _krb5_pk_kx_confirm(context,
			       ctx->pk_init_ctx,
			       ctx->fast_state.reply_key,
			       ticket_sessionkey,
			       pa);
}

KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_init_creds_set_fast_ap_armor_service(krb5_context context,
					  krb5_init_creds_context ctx,
					  krb5_const_principal armor_service)
{
    krb5_error_code ret;

    if (ctx->fast_state.armor_service)
	krb5_free_principal(context, ctx->fast_state.armor_service);
    if (armor_service) {
	ret = krb5_copy_principal(context, armor_service, &ctx->fast_state.armor_service);
	if (ret)
	    return ret;
    } else {
	ctx->fast_state.armor_service = NULL;
    }
    ctx->fast_state.flags |= KRB5_FAST_AP_ARMOR_SERVICE;
    return 0;
}

KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_init_creds_set_fast_anon_pkinit(krb5_context context,
				     krb5_init_creds_context ctx)
{
    if (ctx->fast_state.armor_ccache)
	return EINVAL;

    ctx->fast_state.flags |= KRB5_FAST_REQUIRED;
    ctx->fast_state.flags |= KRB5_FAST_ANON_PKINIT_ARMOR;
    return 0;
}

static size_t
available_padata_count(METHOD_DATA *md)
{
    size_t i, count = 0;

    for (i = 0; i < md->len; i++) {
	PA_DATA *pa = &md->val[i];

	if (pa->padata_type == KRB5_PADATA_FX_COOKIE ||
	    pa->padata_type == KRB5_PADATA_FX_ERROR)
	    continue;

	count++;
    }

    return count;
}

static krb5_error_code
init_creds_step(krb5_context context,
		krb5_init_creds_context ctx,
		krb5_data *in,
		krb5_data *out,
		krb5_krbhst_info *hostinfo,
		unsigned int *flags)
{
    struct timeval start_time, end_time;
    krb5_data checksum_data;
    krb5_error_code ret;
    size_t len = 0;
    size_t size;
    AS_REQ req2;

    gettimeofday(&start_time, NULL);

    krb5_data_zero(out);
    krb5_data_zero(&checksum_data);

    if (ctx->as_req.req_body.cname == NULL) {
	ret = init_as_req(context, ctx->flags, &ctx->cred,
			  ctx->addrs, ctx->etypes, &ctx->as_req);
	if (ret)
	    return ret;
	if (ctx->fast_state.flags & KRB5_FAST_REQUIRED)
	    ;
	else if (ctx->fast_state.flags & KRB5_FAST_AP_ARMOR_SERVICE)
	    /* Check with armor service if there is FAST */;
	else
	    ctx->fast_state.flags |= KRB5_FAST_DISABLED;


	/* XXX should happen after we get back reply from KDC */
	pa_configure(context, ctx, NULL);
    }

#define MAX_PA_COUNTER 15
    if (ctx->pa_counter > MAX_PA_COUNTER) {
	krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
			       N_("Looping %d times while getting "
				  "initial credentials", ""),
			       ctx->pa_counter);
	return KRB5_GET_IN_TKT_LOOP;
    }
    ctx->pa_counter++;

    _krb5_debug(context, 5, "krb5_get_init_creds: loop %d", ctx->pa_counter);

    /* Lets process the input packet */
    if (in && in->length) {
	krb5_kdc_rep rep;

	memset(&rep, 0, sizeof(rep));

	_krb5_debug(context, 5, "krb5_get_init_creds: processing input");

	ret = decode_AS_REP(in->data, in->length, &rep.kdc_rep, &size);
	if (ret == 0) {
	    unsigned eflags = EXTRACT_TICKET_AS_REQ | EXTRACT_TICKET_TIMESYNC;
	    krb5_data data;

	    /*
	     * Unwrap AS-REP
	     */
	    ASN1_MALLOC_ENCODE(Ticket, data.data, data.length,
			       &rep.kdc_rep.ticket, &size, ret);
	    if (ret)
		goto out;
	    heim_assert(data.length == size, "ASN.1 internal error");

	    ret = _krb5_fast_unwrap_kdc_rep(context, ctx->nonce, &data,
					    &ctx->fast_state, &rep.kdc_rep);
	    krb5_data_free(&data);
	    if (ret)
		goto out;

	    /*
	     * Now check and extract the ticket
	     */

	    if (ctx->flags.canonicalize) {
		eflags |= EXTRACT_TICKET_ALLOW_SERVER_MISMATCH;
		eflags |= EXTRACT_TICKET_MATCH_REALM;
	    }
	    if (ctx->ic_flags & KRB5_INIT_CREDS_NO_C_CANON_CHECK)
		eflags |= EXTRACT_TICKET_ALLOW_CNAME_MISMATCH;
	    if (ctx->flags.request_anonymous)
		eflags |= EXTRACT_TICKET_MATCH_ANON;

	    ret = process_pa_data_to_key(context, ctx, &ctx->cred,
					 &ctx->as_req, &rep.kdc_rep,
					 hostinfo, &ctx->fast_state.reply_key);
	    if (ret) {
		free_AS_REP(&rep.kdc_rep);
		goto out;
	    }

	    if (ctx->fast_state.strengthen_key) {
		krb5_keyblock result;

		_krb5_debug(context, 5, "krb5_get_init_creds: FAST strengthen_key");

		ret = _krb5_fast_cf2(context,
				     ctx->fast_state.strengthen_key,
				     "strengthenkey",
				     ctx->fast_state.reply_key,
				     "replykey",
				     &result,
				     NULL);
		if (ret) {
		    free_AS_REP(&rep.kdc_rep);
		    goto out;
		}

		ctx->runflags.allow_save_as_reply_key = 1;

		krb5_free_keyblock_contents(context, ctx->fast_state.reply_key);
		*ctx->fast_state.reply_key = result;
	    }

	    _krb5_debug(context, 5, "krb5_get_init_creds: extracting ticket");

	    ret = _krb5_extract_ticket(context,
				       &rep,
				       &ctx->cred,
				       ctx->fast_state.reply_key,
				       NULL,
				       KRB5_KU_AS_REP_ENC_PART,
				       NULL,
				       ctx->nonce,
				       eflags,
				       &ctx->req_buffer,
				       NULL,
				       NULL);

	    if (ret == 0)
		ret = copy_EncKDCRepPart(&rep.enc_part, &ctx->enc_part);
	    if (ret == 0)
		ret = validate_pkinit_fx(context, ctx, &rep.kdc_rep, &ctx->cred.session);

	    ctx->as_enctype = ctx->fast_state.reply_key->keytype;

	    if (ctx->runflags.allow_save_as_reply_key) {
		ctx->as_reply_key = ctx->fast_state.reply_key;
		ctx->fast_state.reply_key = NULL;
	    } else {
		krb5_free_keyblock(context, ctx->fast_state.reply_key);
		ctx->fast_state.reply_key = NULL;
	    }
	    ctx->ic_flags |= KRB5_INIT_CREDS_DONE;
	    *flags = 0;

	    free_AS_REP(&rep.kdc_rep);
	    free_EncASRepPart(&rep.enc_part);

	    gettimeofday(&end_time, NULL);
	    timevalsub(&end_time, &start_time);
	    timevaladd(&ctx->stats.run_time, &end_time);

	    _krb5_debug(context, 1, "krb5_get_init_creds: wc: %lld.%06ld",
			(long long)ctx->stats.run_time.tv_sec,
			(long)ctx->stats.run_time.tv_usec);
	    return ret;

	} else {
	    /* let's try to parse it as a KRB-ERROR */

	    _krb5_debug(context, 5, "krb5_get_init_creds: got an KRB-ERROR from KDC");

	    free_KRB_ERROR(&ctx->error);

	    ret = krb5_rd_error(context, in, &ctx->error);
	    if(ret && in->length && ((char*)in->data)[0] == 4)
		ret = KRB5KRB_AP_ERR_V4_REPLY;
	    if (ret) {
		_krb5_debug(context, 5, "krb5_get_init_creds: failed to read error");
		goto out;
	    }

	    /*
	     * Unwrap method-data, if there is any,
	     * fast_unwrap_error() below might replace it with a
	     * wrapped version if we are using FAST.
	     */

	    free_METHOD_DATA(&ctx->md);
	    memset(&ctx->md, 0, sizeof(ctx->md));

	    if (ctx->error.e_data) {
		krb5_error_code ret2;

		ret2 = decode_METHOD_DATA(ctx->error.e_data->data,
					 ctx->error.e_data->length,
					 &ctx->md,
					 NULL);
		if (ret2) {
		    /*
		     * Just ignore any error, the error will be pushed
		     * out from krb5_error_from_rd_error() if there
		     * was one.
		     */
		    _krb5_debug(context, 5, N_("Failed to decode METHOD-DATA", ""));
		}
	    }

	    /*
	     * Unwrap KRB-ERROR, we are always calling this so that
	     * FAST can tell us if your peer KDC suddenly dropped FAST
	     * wrapping and its really an attacker's packet (or a bug
	     * in the KDC).
	     */
	    ret = _krb5_fast_unwrap_error(context, ctx->nonce, &ctx->fast_state,
					  &ctx->md, &ctx->error);
	    if (ret)
		goto out;

	    /*
	     *
	     */

	    ret = krb5_error_from_rd_error(context, &ctx->error, &ctx->cred);

	    /* log the failure */
	    if (_krb5_have_debug(context, 5)) {
		const char *str = krb5_get_error_message(context, ret);
		_krb5_debug(context, 5, "krb5_get_init_creds: KRB-ERROR %d/%s", ret, str);
		krb5_free_error_message(context, str);
	    }

	    /*
	     * Handle special error codes
	     */

	    if (ret == KRB5KDC_ERR_PREAUTH_REQUIRED
		|| ret == KRB5_KDC_ERR_MORE_PREAUTH_DATA_REQUIRED
		|| ret == KRB5KDC_ERR_ETYPE_NOSUPP)
	    {
		/*
		 * If no preauth was set and KDC requires it, give it one
		 * more try.
		 *
		 * If the KDC returned KRB5KDC_ERR_ETYPE_NOSUPP, just loop
		 * one more time since that might mean we are dealing with
		 * a Windows KDC that is confused about what enctypes are
		 * available.
		 */

		if (available_padata_count(&ctx->md) == 0) {
		    krb5_set_error_message(context, ret,
					   N_("Preauth required but no preauth "
					      "options send by KDC", ""));
		    goto out;
		}
	    } else if (ret == KRB5KRB_AP_ERR_SKEW && context->kdc_sec_offset == 0) {
		/*
		 * Try adapt to timeskrew when we are using pre-auth, and
		 * if there was a time skew, try again.
		 */
		krb5_set_real_time(context, ctx->error.stime, -1);
		if (context->kdc_sec_offset)
		    ret = 0;

		_krb5_debug(context, 10, "init_creds: err skew updating kdc offset to %d",
			    context->kdc_sec_offset);
		if (ret)
		    goto out;

		pa_restart(context, ctx);

	    } else if (ret == KRB5_KDC_ERR_WRONG_REALM && ctx->flags.canonicalize) {
		/* client referral to a new realm */
		char *ref_realm;

		if (ctx->error.crealm == NULL) {
		    krb5_set_error_message(context, ret,
					   N_("Got a client referral, not but no realm", ""));
		    goto out;
		}
		ref_realm = *ctx->error.crealm;

		_krb5_debug(context, 5, "krb5_get_init_creds: referral to realm %s",
			    ref_realm);

		/*
		 * If its a krbtgt, lets updat the requested krbtgt too
		 */
		if (krb5_principal_is_krbtgt(context, ctx->cred.server)) {

		    free(ctx->cred.server->name.name_string.val[1]);
		    ctx->cred.server->name.name_string.val[1] = strdup(ref_realm);
		    if (ctx->cred.server->name.name_string.val[1] == NULL) {
			ret = krb5_enomem(context);
			goto out;
		    }

		    free_PrincipalName(ctx->as_req.req_body.sname);
		    ret = _krb5_principal2principalname(ctx->as_req.req_body.sname, ctx->cred.server);
		    if (ret)
			goto out;
		}

		free(ctx->as_req.req_body.realm);
		ret = copy_Realm(&ref_realm, &ctx->as_req.req_body.realm);
		if (ret)
		    goto out;

		ret = krb5_principal_set_realm(context,
					       ctx->cred.client,
					       *ctx->error.crealm);
		if (ret)
		    goto out;

		ret = krb5_unparse_name(context, ctx->cred.client, &ref_realm);
		if (ret == 0) {
		    _krb5_debug(context, 5, "krb5_get_init_creds: got referral to %s", ref_realm);
		    krb5_xfree(ref_realm);
		}

		pa_restart(context, ctx);

	    } else if (ret == KRB5KDC_ERR_KEY_EXP && ctx->runflags.change_password == 0 &&
		       ctx->runflags.change_password_prompt) {
		char buf2[1024];

		ctx->runflags.change_password = 1;

		ctx->prompter(context, ctx->prompter_data, NULL, N_("Password has expired", ""), 0, NULL);

		/* try to avoid recursion */
		if (ctx->in_tkt_service != NULL && strcmp(ctx->in_tkt_service, "kadmin/changepw") == 0)
		    goto out;

		/* don't include prompter in runtime */
		gettimeofday(&end_time, NULL);
		timevalsub(&end_time, &start_time);
		timevaladd(&ctx->stats.run_time, &end_time);

		ret = change_password(context,
				      ctx->cred.client,
				      ctx->password,
				      buf2,
				      sizeof(buf2),
				      ctx->prompter,
				      ctx->prompter_data,
				      NULL);
		if (ret)
		    goto out;

		gettimeofday(&start_time, NULL);

		krb5_init_creds_set_password(context, ctx, buf2);

		pa_restart(context, ctx);

	    } else if (ret == KRB5KDC_ERR_PREAUTH_FAILED) {

		/*
		 * Old MIT KDC can't handle KRB5_PADATA_REQ_ENC_PA_REP,
		 * so drop it and try again. But only try that for MIT
		 * Kerberos servers by keying of no METHOD-DATA.
		 */
		if (ctx->runflags.allow_enc_pa_rep) {
		    if (ctx->md.len != 0) {
			_krb5_debug(context, 10, "Server sent PA data with KRB-ERROR, "
				    "so not a pre 1.7 MIT KDC and won't retry w/o ENC-PA-REQ");
			goto out;
		    }
		    _krb5_debug(context, 10, "Disabling allow_enc_pa_rep and trying again");
		    ctx->runflags.allow_enc_pa_rep = 0;
		    goto retry;
		}

		if (ctx->fast_state.flags & KRB5_FAST_DISABLED) {
		    _krb5_debug(context, 10, "FAST disabled and got preauth failed");
		    goto out;
		}

		if ((ctx->fast_state.flags & KRB5_FAST_OPTIMISTIC) == 0) {
		    _krb5_debug(context, 10, "Preauth failed");
		    goto out;
		}

		_krb5_debug(context, 10, "preauth failed with optimistic FAST, trying w/o FAST");

		ctx->fast_state.flags &= ~KRB5_FAST_OPTIMISTIC;
		ctx->fast_state.flags |= KRB5_FAST_DISABLED;

	    retry:
		pa_restart(context, ctx);

	    } else if (ctx->fast_state.flags & KRB5_FAST_OPTIMISTIC) {
		_krb5_debug(context, 10,
			    "Some other error %d failed with optimistic FAST, trying w/o FAST", ret);

		ctx->fast_state.flags &= ~KRB5_FAST_OPTIMISTIC;
		ctx->fast_state.flags |= KRB5_FAST_DISABLED;
		pa_restart(context, ctx);
	    } else {
		/* some other error code from the KDC, lets' return it to the user */
		goto out;
	    }
	}
    }

    if (ctx->as_req.padata) {
	free_METHOD_DATA(ctx->as_req.padata);
	free(ctx->as_req.padata);
	ctx->as_req.padata = NULL;
    }

    ret = _krb5_fast_create_armor(context, &ctx->fast_state,
				  ctx->cred.client->realm);
    if (ret)
	goto out;

    /* Set a new nonce. */
    ctx->as_req.req_body.nonce = ctx->nonce;


    /*
     * Step and announce PA-DATA
     */

    ret = process_pa_data_to_md(context, &ctx->cred, &ctx->as_req, ctx,
				&ctx->md, &ctx->as_req.padata);
    if (ret)
	goto out;


    /*
     * Wrap with FAST
     */
    ret = copy_AS_REQ(&ctx->as_req, &req2);
    if (ret)
	goto out;

    ret = _krb5_fast_wrap_req(context,
			      &ctx->fast_state,
			      NULL,
			      &req2);

    krb5_data_free(&checksum_data);
    if (ret) {
	free_AS_REQ(&req2);
	goto out;
    }

    krb5_data_free(&ctx->req_buffer);

    ASN1_MALLOC_ENCODE(AS_REQ,
		       ctx->req_buffer.data, ctx->req_buffer.length,
		       &req2, &len, ret);
    free_AS_REQ(&req2);
    if (ret)
	goto out;
    if(len != ctx->req_buffer.length)
	krb5_abortx(context, "internal error in ASN.1 encoder");

    out->data = ctx->req_buffer.data;
    out->length = ctx->req_buffer.length;

    *flags = KRB5_INIT_CREDS_STEP_FLAG_CONTINUE;

    gettimeofday(&end_time, NULL);
    timevalsub(&end_time, &start_time);
    timevaladd(&ctx->stats.run_time, &end_time);

    return 0;
 out:
    return ret;
}

/**
 * The core loop if krb5_get_init_creds() function family. Create the
 * packets and have the caller send them off to the KDC.
 *
 * If the caller want all work been done for them, use
 * krb5_init_creds_get() instead.
 *
 * @param context a Kerberos 5 context.
 * @param ctx ctx krb5_init_creds_context context.
 * @param in input data from KDC, first round it should be reset by krb5_data_zer().
 * @param out reply to KDC.
 * @param hostinfo KDC address info, first round it can be NULL.
 * @param flags status of the round, if
 *        KRB5_INIT_CREDS_STEP_FLAG_CONTINUE is set, continue one more round.
 *
 * @return 0 for success, or an Kerberos 5 error code, see
 *     krb5_get_error_message().
 *
 * @ingroup krb5_credential
 */

KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_init_creds_step(krb5_context context,
		     krb5_init_creds_context ctx,
		     krb5_data *in,
		     krb5_data *out,
		     krb5_krbhst_info *hostinfo,
		     unsigned int *flags)
{
    krb5_error_code ret;
    krb5_data empty;

    krb5_data_zero(&empty);

    if ((ctx->fast_state.flags & KRB5_FAST_ANON_PKINIT_ARMOR) &&
	ctx->fast_state.armor_ccache == NULL) {
	ret = _krb5_fast_anon_pkinit_step(context, ctx, &ctx->fast_state,
					  in, out, hostinfo, flags);
	if (ret ||
	    ((*flags & KRB5_INIT_CREDS_STEP_FLAG_CONTINUE) == 0) ||
	    out->length)
	    return ret;

	in = &empty;
    }

    return init_creds_step(context, ctx, in, out, hostinfo, flags);
}

/**
 * Extract the newly acquired credentials from krb5_init_creds_context
 * context.
 *
 * @param context A Kerberos 5 context.
 * @param ctx
 * @param cred credentials, free with krb5_free_cred_contents().
 *
 * @return 0 for sucess or An Kerberos error code, see krb5_get_error_message().
 */

KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_init_creds_get_creds(krb5_context context,
			  krb5_init_creds_context ctx,
			  krb5_creds *cred)
{
    return krb5_copy_creds_contents(context, &ctx->cred, cred);
}

/**
 * Extract the as-reply key from the context.
 *
 * Only allowed when the as-reply-key is not directly derived from the
 * password like PK-INIT, GSS, FAST hardened key, etc.
 *
 * @param context A Kerberos 5 context.
 * @param ctx ctx krb5_init_creds_context context.
 * @param as_reply_key keyblock, free with krb5_free_keyblock_contents().
 *
 * @return 0 for sucess or An Kerberos error code, see krb5_get_error_message().
 */

KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_init_creds_get_as_reply_key(krb5_context context,
				 krb5_init_creds_context ctx,
				 krb5_keyblock *as_reply_key)
{
    if (ctx->as_reply_key == NULL)
	return KRB5KDC_ERR_PREAUTH_REQUIRED;
    return krb5_copy_keyblock_contents(context, ctx->as_reply_key, as_reply_key);
}

KRB5_LIB_FUNCTION krb5_timestamp KRB5_LIB_CALL
_krb5_init_creds_get_cred_starttime(krb5_context context, krb5_init_creds_context ctx)
{
    return ctx->cred.times.starttime;
}

KRB5_LIB_FUNCTION krb5_timestamp KRB5_LIB_CALL
_krb5_init_creds_get_cred_endtime(krb5_context context, krb5_init_creds_context ctx)
{
    return ctx->cred.times.endtime;
}

KRB5_LIB_FUNCTION krb5_principal KRB5_LIB_CALL
_krb5_init_creds_get_cred_client(krb5_context context, krb5_init_creds_context ctx)
{
    return ctx->cred.client;
}

/**
 * Get the last error from the transaction.
 *
 * @return Returns 0 or an error code
 *
 * @ingroup krb5_credential
 */

KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_init_creds_get_error(krb5_context context,
			  krb5_init_creds_context ctx,
			  KRB_ERROR *error)
{
    krb5_error_code ret;

    ret = copy_KRB_ERROR(&ctx->error, error);
    if (ret)
	krb5_enomem(context);

    return ret;
}

/**
 * Store config
 *
 * @param context A Kerberos 5 context.
 * @param ctx The krb5_init_creds_context to free.
 * @param id store
 *
 * @return Returns 0 or an error code
 *
 * @ingroup krb5_credential
 */

krb5_error_code KRB5_LIB_FUNCTION
krb5_init_creds_store_config(krb5_context context,
			     krb5_init_creds_context ctx,
			     krb5_ccache id)
{
    krb5_error_code ret;

    if (ctx->kdc_hostname) {
	krb5_data data;
	data.length = strlen(ctx->kdc_hostname);
	data.data = ctx->kdc_hostname;

	ret = krb5_cc_set_config(context, id, NULL, "lkdc-hostname", &data);
	if (ret)
	    return ret;
    }
    if (ctx->sitename) {
	krb5_data data;
	data.length = strlen(ctx->sitename);
	data.data = ctx->sitename;

	ret = krb5_cc_set_config(context, id, NULL, "sitename", &data);
	if (ret)
	    return ret;
    }

    return 0;
}

/**
 *
 * @ingroup krb5_credential
 */

krb5_error_code
krb5_init_creds_store(krb5_context context,
		      krb5_init_creds_context ctx,
		      krb5_ccache id)
{
    krb5_error_code ret;

    if (ctx->cred.client == NULL) {
	ret = KRB5KDC_ERR_PREAUTH_REQUIRED;
	krb5_set_error_message(context, ret, "init creds not completed yet");
	return ret;
    }

    ret = krb5_cc_initialize(context, id, ctx->cred.client);
    if (ret)
	return ret;

    ret = krb5_cc_store_cred(context, id, &ctx->cred);
    if (ret)
	return ret;

    if (ctx->cred.flags.b.enc_pa_rep) {
	krb5_data data = { 3, rk_UNCONST("yes") };
	ret = krb5_cc_set_config(context, id, ctx->cred.server,
				 "fast_avail", &data);
	if (ret)
	    return ret;
    }

    return 0;
}

/**
 * Free the krb5_init_creds_context allocated by krb5_init_creds_init().
 *
 * @param context A Kerberos 5 context.
 * @param ctx The krb5_init_creds_context to free.
 *
 * @ingroup krb5_credential
 */

KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_init_creds_free(krb5_context context,
		     krb5_init_creds_context ctx)
{
    free_init_creds_ctx(context, ctx);
    free(ctx);
}

/**
 * Get new credentials as setup by the krb5_init_creds_context.
 *
 * @param context A Kerberos 5 context.
 * @param ctx The krb5_init_creds_context to process.
 *
 * @ingroup krb5_credential
 */

KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_init_creds_get(krb5_context context, krb5_init_creds_context ctx)
{
    krb5_sendto_ctx stctx = NULL;
    krb5_krbhst_info *hostinfo = NULL;
    krb5_error_code ret;
    krb5_data in, out;
    unsigned int flags = 0;

    krb5_data_zero(&in);
    krb5_data_zero(&out);

    ret = krb5_sendto_ctx_alloc(context, &stctx);
    if (ret)
	goto out;
    krb5_sendto_ctx_set_func(stctx, _krb5_kdc_retry, NULL);

    if (ctx->kdc_hostname)
	krb5_sendto_set_hostname(context, stctx, ctx->kdc_hostname);
    if (ctx->sitename)
	krb5_sendto_set_sitename(context, stctx, ctx->sitename);

    while (1) {
	struct timeval nstart, nend;

	flags = 0;
	ret = krb5_init_creds_step(context, ctx, &in, &out, hostinfo, &flags);
	krb5_data_free(&in);
	if (ret)
	    goto out;

	if ((flags & KRB5_INIT_CREDS_STEP_FLAG_CONTINUE) == 0)
	    break;

	gettimeofday(&nstart, NULL);

	ret = krb5_sendto_context (context, stctx, &out,
				   ctx->cred.client->realm, &in);
    	if (ret)
	    goto out;

	gettimeofday(&nend, NULL);
	timevalsub(&nend, &nstart);
	timevaladd(&ctx->stats.run_time, &nend);
    }

 out:
    if (stctx)
	krb5_sendto_ctx_free(context, stctx);

    return ret;
}

/**
 * Get new credentials using password.
 *
 * @ingroup krb5_credential
 */


KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_get_init_creds_password(krb5_context context,
			     krb5_creds *creds,
			     krb5_principal client,
			     const char *password,
			     krb5_prompter_fct prompter,
			     void *data,
			     krb5_deltat start_time,
			     const char *in_tkt_service,
			     krb5_get_init_creds_opt *options)
{
    krb5_init_creds_context ctx;
    char buf[BUFSIZ], buf2[BUFSIZ];
    krb5_error_code ret;
    int chpw = 0;

 again:
    ret = krb5_init_creds_init(context, client, prompter, data, start_time, options, &ctx);
    if (ret)
	goto out;

    ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
    if (ret)
	goto out;

    if (prompter != NULL && ctx->password == NULL && password == NULL) {
	krb5_prompt prompt;
	krb5_data password_data;
	char *p, *q = NULL;
	int aret;

	ret = krb5_unparse_name(context, client, &p);
	if (ret)
	    goto out;

	aret = asprintf(&q, "%s's Password: ", p);
	free (p);
	if (aret == -1 || q == NULL) {
	    ret = krb5_enomem(context);
	    goto out;
	}
	prompt.prompt = q;
	password_data.data   = buf;
	password_data.length = sizeof(buf);
	prompt.hidden = 1;
	prompt.reply  = &password_data;
	prompt.type   = KRB5_PROMPT_TYPE_PASSWORD;

	ret = (*prompter) (context, data, NULL, NULL, 1, &prompt);
	free (q);
	if (ret) {
	    memset_s(buf, sizeof(buf), 0, sizeof(buf));
	    ret = KRB5_LIBOS_PWDINTR;
	    krb5_clear_error_message (context);
	    goto out;
	}
	password = password_data.data;
    }

    if (password) {
	ret = krb5_init_creds_set_password(context, ctx, password);
	if (ret)
	    goto out;
    }

    ret = krb5_init_creds_get(context, ctx);

    if (ret == 0)
	krb5_process_last_request(context, options, ctx);


    if (ret == KRB5KDC_ERR_KEY_EXPIRED && chpw == 0) {
	/* try to avoid recursion */
	if (in_tkt_service != NULL && strcmp(in_tkt_service, "kadmin/changepw") == 0)
	   goto out;

	/* don't try to change password if no prompter or prompting disabled */
	if (!ctx->runflags.change_password_prompt)
	    goto out;

	ret = change_password (context,
			       client,
			       ctx->password,
			       buf2,
			       sizeof(buf2),
			       prompter,
			       data,
			       options);
	if (ret)
	    goto out;
	password = buf2;
	chpw = 1;
	krb5_init_creds_free(context, ctx);
	goto again;
    }

 out:
    if (ret == 0)
	krb5_init_creds_get_creds(context, ctx, creds);

    if (ctx)
	krb5_init_creds_free(context, ctx);

    memset_s(buf, sizeof(buf), 0, sizeof(buf));
    memset_s(buf2, sizeof(buf), 0, sizeof(buf2));
    return ret;
}

/**
 * Get new credentials using keyblock.
 *
 * @ingroup krb5_credential
 */

KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_get_init_creds_keyblock(krb5_context context,
			     krb5_creds *creds,
			     krb5_principal client,
			     krb5_keyblock *keyblock,
			     krb5_deltat start_time,
			     const char *in_tkt_service,
			     krb5_get_init_creds_opt *options)
{
    krb5_init_creds_context ctx;
    krb5_error_code ret;

    memset(creds, 0, sizeof(*creds));

    ret = krb5_init_creds_init(context, client, NULL, NULL, start_time, options, &ctx);
    if (ret)
	goto out;

    ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
    if (ret)
	goto out;

    ret = krb5_init_creds_set_keyblock(context, ctx, keyblock);
    if (ret)
	goto out;

    ret = krb5_init_creds_get(context, ctx);

    if (ret == 0)
        krb5_process_last_request(context, options, ctx);

 out:
    if (ret == 0)
	krb5_init_creds_get_creds(context, ctx, creds);

    if (ctx)
	krb5_init_creds_free(context, ctx);

    return ret;
}

/**
 * Get new credentials using keytab.
 *
 * @ingroup krb5_credential
 */

KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_get_init_creds_keytab(krb5_context context,
			   krb5_creds *creds,
			   krb5_principal client,
			   krb5_keytab keytab,
			   krb5_deltat start_time,
			   const char *in_tkt_service,
			   krb5_get_init_creds_opt *options)
{
    krb5_init_creds_context ctx;
    krb5_keytab_entry ktent;
    krb5_error_code ret;

    memset(&ktent, 0, sizeof(ktent));
    memset(creds, 0, sizeof(*creds));

    if (strcmp(client->realm, "") == 0) {
        /*
         * Referral realm.  We have a keytab, so pick a realm by
         * matching in the keytab.
         */
        ret = krb5_kt_get_entry(context, keytab, client, 0, 0, &ktent);
        if (ret == 0)
            client = ktent.principal;
    }

    ret = krb5_init_creds_init(context, client, NULL, NULL, start_time, options, &ctx);
    if (ret)
	goto out;

    ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
    if (ret)
	goto out;

    ret = krb5_init_creds_set_keytab(context, ctx, keytab);
    if (ret)
	goto out;

    ret = krb5_init_creds_get(context, ctx);
    if (ret == 0)
        krb5_process_last_request(context, options, ctx);

 out:
    krb5_kt_free_entry(context, &ktent);
    if (ret == 0)
	krb5_init_creds_get_creds(context, ctx, creds);

    if (ctx)
	krb5_init_creds_free(context, ctx);

    return ret;
}

KRB5_LIB_FUNCTION void KRB5_LIB_CALL
_krb5_init_creds_set_gss_mechanism(krb5_context context,
				   krb5_gss_init_ctx gssic,
				   const struct gss_OID_desc_struct *gss_mech)
{
    gssic->mech = gss_mech; /* OIDs are interned, so no copy required */
}

KRB5_LIB_FUNCTION const struct gss_OID_desc_struct * KRB5_LIB_CALL
_krb5_init_creds_get_gss_mechanism(krb5_context context,
				   krb5_gss_init_ctx gssic)
{
    return gssic->mech;
}

KRB5_LIB_FUNCTION void KRB5_LIB_CALL
_krb5_init_creds_set_gss_cred(krb5_context context,
			      krb5_gss_init_ctx gssic,
			      struct gss_cred_id_t_desc_struct *gss_cred)
{
    if (gssic->cred != gss_cred && gssic->flags.release_cred)
	gssic->release_cred(context, gssic, gssic->cred);

    gssic->cred = gss_cred;
    gssic->flags.release_cred = 1;
}

KRB5_LIB_FUNCTION const struct gss_cred_id_t_desc_struct * KRB5_LIB_CALL
_krb5_init_creds_get_gss_cred(krb5_context context,
			      krb5_gss_init_ctx gssic)
{
    return gssic->cred;
}

KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
_krb5_init_creds_init_gss(krb5_context context,
			  krb5_init_creds_context ctx,
			  krb5_gssic_step step,
			  krb5_gssic_finish finish,
			  krb5_gssic_release_cred release_cred,
			  krb5_gssic_delete_sec_context delete_sec_context,
			  const struct gss_cred_id_t_desc_struct *gss_cred,
			  const struct gss_OID_desc_struct *gss_mech,
			  unsigned int flags)
{
    krb5_gss_init_ctx gssic = ctx->gss_init_ctx;

    gssic = calloc(1, sizeof(*gssic));
    if (gssic == NULL)
	return krb5_enomem(context);

    if (ctx->gss_init_ctx)
	free_gss_init_ctx(context, ctx->gss_init_ctx);
    ctx->gss_init_ctx = gssic;

    gssic->cred = (struct gss_cred_id_t_desc_struct *)gss_cred;
    gssic->mech = gss_mech;
    if (flags & KRB5_GSS_IC_FLAG_RELEASE_CRED)
	gssic->flags.release_cred = 1;

    gssic->step = step;
    gssic->finish = finish;
    gssic->release_cred = release_cred;
    gssic->delete_sec_context = delete_sec_context;

    return 0;
}