summaryrefslogtreecommitdiff
path: root/src/certtool.c
blob: a470f42377c776f320e731a9cd7625a5fab013f2 (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
/*
 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
 *
 * This file is part of GnuTLS.
 *
 * GnuTLS is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuTLS is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <gnutls/gnutls.h>
#include <gnutls/extra.h>
#include <gnutls/x509.h>
#include <gnutls/openpgp.h>
#include <gnutls/pkcs12.h>
#include <gnutls/pkcs11.h>
#include <gnutls/abstract.h>

#include <gcrypt.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <error.h>

/* Gnulib portability files. */
#include <read-file.h>
#include <progname.h>
#include <version-etc.h>

#include <certtool-cfg.h>
#include "certtool-gaa.h"
#include "certtool-common.h"

static void print_crl_info (gnutls_x509_crl_t crl, FILE * out);
void pkcs7_info (void);
void crq_info (void);
void smime_to_pkcs7 (void);
void pkcs12_info (void);
void generate_pkcs12 (common_info_st *);
void generate_pkcs8 (common_info_st *);
void verify_chain (void);
void verify_crl (common_info_st * cinfo);
void pubkey_info (gnutls_x509_crt crt, common_info_st *);
void pgp_privkey_info (void);
void pgp_ring_info (void);
void certificate_info (int, common_info_st *);
void pgp_certificate_info (void);
void crl_info (void);
void privkey_info (void);
static void gaa_parser (int argc, char **argv);
void generate_self_signed (common_info_st *);
void generate_request (common_info_st *);
static void print_certificate_info (gnutls_x509_crt_t crt, FILE * out,
                                    unsigned int all);

static void print_hex_datum (gnutls_datum_t * dat);

static gaainfo info;
FILE *outfile;
FILE *infile;
gnutls_digest_algorithm_t default_dig;

/* non interactive operation if set
 */
int batch;


static void
tls_log_func (int level, const char *str)
{
  fprintf (stderr, "|<%d>| %s", level, str);
}

int
main (int argc, char **argv)
{
  set_program_name (argv[0]);
  cfg_init ();
  gaa_parser (argc, argv);

  return 0;
}

static const char *
raw_to_string (const unsigned char *raw, size_t raw_size)
{
  static char buf[1024];
  size_t i;
  if (raw_size == 0)
    return NULL;

  if (raw_size * 3 + 1 >= sizeof (buf))
    return NULL;

  for (i = 0; i < raw_size; i++)
    {
      sprintf (&(buf[i * 3]), "%02X%s", raw[i],
               (i == raw_size - 1) ? "" : ":");
    }
  buf[sizeof (buf) - 1] = '\0';

  return buf;
}

static void
print_dsa_pkey (gnutls_datum_t * x, gnutls_datum_t * y, gnutls_datum_t * p,
                gnutls_datum_t * q, gnutls_datum_t * g)
{
  if (x)
    {
      fprintf (outfile, "private key:");
      print_hex_datum (x);
    }
  fprintf (outfile, "public key:");
  print_hex_datum (y);
  fprintf (outfile, "p:");
  print_hex_datum (p);
  fprintf (outfile, "q:");
  print_hex_datum (q);
  fprintf (outfile, "g:");
  print_hex_datum (g);
}

static void
print_rsa_pkey (gnutls_datum_t * m, gnutls_datum_t * e, gnutls_datum_t * d,
                gnutls_datum_t * p, gnutls_datum_t * q, gnutls_datum_t * u,
                gnutls_datum_t * exp1, gnutls_datum_t * exp2)
{
  fprintf (outfile, "modulus:");
  print_hex_datum (m);
  fprintf (outfile, "public exponent:");
  print_hex_datum (e);
  if (d)
    {
      fprintf (outfile, "private exponent:");
      print_hex_datum (d);
      fprintf (outfile, "prime1:");
      print_hex_datum (p);
      fprintf (outfile, "prime2:");
      print_hex_datum (q);
      fprintf (outfile, "coefficient:");
      print_hex_datum (u);
      if (exp1 && exp2)
        {
          fprintf (outfile, "exp1:");
          print_hex_datum (exp1);
          fprintf (outfile, "exp2:");
          print_hex_datum (exp2);
        }
    }
}

static gnutls_sec_param_t
str_to_sec_param (const char *str)
{
  if (strcasecmp (str, "low") == 0)
    {
      return GNUTLS_SEC_PARAM_LOW;
    }
  else if (strcasecmp (str, "normal") == 0)
    {
      return GNUTLS_SEC_PARAM_NORMAL;
    }
  else if (strcasecmp (str, "high") == 0)
    {
      return GNUTLS_SEC_PARAM_HIGH;
    }
  else if (strcasecmp (str, "ultra") == 0)
    {
      return GNUTLS_SEC_PARAM_ULTRA;
    }
  else
    {
      fprintf (stderr, "Unknown security parameter string: %s\n", str);
      exit (1);
    }

}

int
get_bits (gnutls_pk_algorithm_t key_type)
{
  int bits;

  if (info.bits != 0)
    {
      static int warned = 0;

      if (warned == 0)
        {
          warned = 1;
          fprintf (stderr,
                   "** Note: Please use the --sec-param instead of --bits\n");
        }
      bits = info.bits;
    }
  else
    {
      if (info.sec_param)
        {
          bits =
            gnutls_sec_param_to_pk_bits (key_type,
                                         str_to_sec_param (info.sec_param));
        }
      else
        bits =
          gnutls_sec_param_to_pk_bits (key_type, GNUTLS_SEC_PARAM_NORMAL);
    }

  return bits;
}


static gnutls_x509_privkey_t
generate_private_key_int (void)
{
  gnutls_x509_privkey_t key;
  int ret, key_type, bits;

  if (info.dsa)
    {
      key_type = GNUTLS_PK_DSA;
    }
  else
    key_type = GNUTLS_PK_RSA;

  ret = gnutls_x509_privkey_init (&key);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "privkey_init: %s", gnutls_strerror (ret));

  bits = get_bits (key_type);

  fprintf (stderr, "Generating a %d bit %s private key...\n",
           get_bits (key_type), gnutls_pk_algorithm_get_name (key_type));

  if (info.quick_random == 0)
    fprintf (stderr,
             "This might take several minutes depending on availability of randomness"
             " in /dev/random.\n");

  ret = gnutls_x509_privkey_generate (key, key_type, get_bits (key_type), 0);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "privkey_generate: %s", gnutls_strerror (ret));

  return key;
}

static int
cipher_to_flags (const char *cipher)
{
  if (strcasecmp (cipher, "3des") == 0)
    {
      return GNUTLS_PKCS_USE_PBES2_3DES;
    }
  else if (strcasecmp (cipher, "3des-pkcs12") == 0)
    {
      return GNUTLS_PKCS_USE_PKCS12_3DES;
    }
  else if (strcasecmp (cipher, "arcfour") == 0)
    {
      return GNUTLS_PKCS_USE_PKCS12_ARCFOUR;
    }
  else if (strcasecmp (cipher, "aes-128") == 0)
    {
      return GNUTLS_PKCS_USE_PBES2_AES_128;
    }
  else if (strcasecmp (cipher, "aes-192") == 0)
    {
      return GNUTLS_PKCS_USE_PBES2_AES_192;
    }
  else if (strcasecmp (cipher, "aes-256") == 0)
    {
      return GNUTLS_PKCS_USE_PBES2_AES_256;
    }
  else if (strcasecmp (cipher, "rc2-40") == 0)
    {
      return GNUTLS_PKCS_USE_PKCS12_RC2_40;
    }

  error (EXIT_FAILURE, 0, "Unknown cipher %s\n", cipher);
  return -1;
}


static void
print_private_key (gnutls_x509_privkey_t key)
{
  int ret;
  size_t size;

  if (!key)
    return;

  if (!info.pkcs8)
    {
      size = buffer_size;
      ret = gnutls_x509_privkey_export (key, info.outcert_format,
                                        buffer, &size);
      if (ret < 0)
        error (EXIT_FAILURE, 0, "privkey_export: %s", gnutls_strerror (ret));
    }
  else
    {
      unsigned int flags;
      const char *pass;

      if (info.export)
        flags = GNUTLS_PKCS_USE_PKCS12_RC2_40;
      else
        flags = cipher_to_flags (info.pkcs_cipher);

      if ((pass = get_confirmed_pass (true)) == NULL || *pass == '\0')
        flags = GNUTLS_PKCS_PLAIN;

      size = buffer_size;
      ret =
        gnutls_x509_privkey_export_pkcs8 (key, info.outcert_format, pass,
                                          flags, buffer, &size);
      if (ret < 0)
        error (EXIT_FAILURE, 0, "privkey_export_pkcs8: %s",
               gnutls_strerror (ret));
    }

  fwrite (buffer, 1, size, outfile);
}

static void
generate_private_key (void)
{
  gnutls_x509_privkey_t key;

  key = generate_private_key_int ();

  print_private_key (key);

  gnutls_x509_privkey_deinit (key);
}


static gnutls_x509_crt_t
generate_certificate (gnutls_x509_privkey_t * ret_key,
                      gnutls_x509_crt_t ca_crt, int proxy,
                      common_info_st * cinfo)
{
  gnutls_x509_crt_t crt;
  gnutls_x509_privkey_t key = NULL;
  size_t size;
  int ret;
  int client;
  int days, result, ca_status = 0, is_ike = 0, path_len;
  int vers;
  unsigned int usage = 0, server;
  gnutls_x509_crq_t crq;        /* request */

  ret = gnutls_x509_crt_init (&crt);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "crt_init: %s", gnutls_strerror (ret));

  crq = load_request (cinfo);

  if (crq == NULL)
    {

      key = load_private_key (1, cinfo);

      if (!batch)
        fprintf (stderr,
                 "Please enter the details of the certificate's distinguished name. "
                 "Just press enter to ignore a field.\n");

      /* set the DN.
       */
      if (proxy)
        {
          result = gnutls_x509_crt_set_proxy_dn (crt, ca_crt, 0, NULL, 0);
          if (result < 0)
            error (EXIT_FAILURE, 0, "set_proxy_dn: %s",
                   gnutls_strerror (result));

          get_cn_crt_set (crt);
        }
      else
        {
          get_country_crt_set (crt);
          get_organization_crt_set (crt);
          get_unit_crt_set (crt);
          get_locality_crt_set (crt);
          get_state_crt_set (crt);
          get_cn_crt_set (crt);
          get_uid_crt_set (crt);
          get_oid_crt_set (crt);
          get_key_purpose_set (crt);

          if (!batch)
            fprintf (stderr,
                     "This field should not be used in new certificates.\n");

          get_pkcs9_email_crt_set (crt);
        }

      result = gnutls_x509_crt_set_key (crt, key);
      if (result < 0)
        error (EXIT_FAILURE, 0, "set_key: %s", gnutls_strerror (result));
    }
  else
    {
      result = gnutls_x509_crt_set_crq (crt, crq);
      if (result < 0)
        error (EXIT_FAILURE, 0, "set_crq: %s", gnutls_strerror (result));
    }


  {
    int serial = get_serial ();
    char bin_serial[5];

    bin_serial[4] = serial & 0xff;
    bin_serial[3] = (serial >> 8) & 0xff;
    bin_serial[2] = (serial >> 16) & 0xff;
    bin_serial[1] = (serial >> 24) & 0xff;
    bin_serial[0] = 0;

    result = gnutls_x509_crt_set_serial (crt, bin_serial, 5);
    if (result < 0)
      error (EXIT_FAILURE, 0, "serial: %s", gnutls_strerror (result));
  }

  if (!batch)
    fprintf (stderr, "\n\nActivation/Expiration time.\n");

  gnutls_x509_crt_set_activation_time (crt, time (NULL));

  days = get_days ();

  result =
    gnutls_x509_crt_set_expiration_time (crt,
                                         time (NULL) + days * 24 * 60 * 60);
  if (result < 0)
    error (EXIT_FAILURE, 0, "set_expiration: %s", gnutls_strerror (result));

  if (!batch)
    fprintf (stderr, "\n\nExtensions.\n");

  /* do not allow extensions on a v1 certificate */
  if (crq && get_crq_extensions_status () != 0)
    {
      result = gnutls_x509_crt_set_crq_extensions (crt, crq);
      if (result < 0)
        error (EXIT_FAILURE, 0, "set_crq: %s", gnutls_strerror (result));
    }

  /* append additional extensions */
  if (info.v1_cert == 0)
    {

      if (proxy)
        {
          const char *policylanguage;
          char *policy;
          size_t policylen;
          int proxypathlen = get_path_len ();

          if (!batch)
            {
              printf ("1.3.6.1.5.5.7.21.1 ::= id-ppl-inheritALL\n");
              printf ("1.3.6.1.5.5.7.21.2 ::= id-ppl-independent\n");
            }

          policylanguage = get_proxy_policy (&policy, &policylen);

          result =
            gnutls_x509_crt_set_proxy (crt, proxypathlen, policylanguage,
                                       policy, policylen);
          if (result < 0)
            error (EXIT_FAILURE, 0, "set_proxy: %s",
                   gnutls_strerror (result));
        }

      if (!proxy)
        ca_status = get_ca_status ();
      if (ca_status)
        path_len = get_path_len ();
      else
        path_len = -1;

      result =
        gnutls_x509_crt_set_basic_constraints (crt, ca_status, path_len);
      if (result < 0)
        error (EXIT_FAILURE, 0, "basic_constraints: %s",
               gnutls_strerror (result));

      client = get_tls_client_status ();
      if (client != 0)
        {
          result = gnutls_x509_crt_set_key_purpose_oid (crt,
                                                        GNUTLS_KP_TLS_WWW_CLIENT,
                                                        0);
          if (result < 0)
            error (EXIT_FAILURE, 0, "key_kp: %s", gnutls_strerror (result));
        }

      is_ike = get_ipsec_ike_status ();
      server = get_tls_server_status ();
      if ((server != 0 && !proxy) || is_ike)
        {
          get_dns_name_set (TYPE_CRT, crt);
          get_ip_addr_set (TYPE_CRT, crt);
        }

      if (server != 0)
        {
          result = 0;

          result =
            gnutls_x509_crt_set_key_purpose_oid (crt,
                                                 GNUTLS_KP_TLS_WWW_SERVER, 0);
          if (result < 0)
            error (EXIT_FAILURE, 0, "key_kp: %s", gnutls_strerror (result));
        }
      else if (!proxy)
        {
          get_email_set (TYPE_CRT, crt);
        }

      if (!ca_status || server)
        {
          int pk;


          pk = gnutls_x509_crt_get_pk_algorithm (crt, NULL);

          if (pk != GNUTLS_PK_DSA)
            {                   /* DSA keys can only sign.
                                 */
              result = get_sign_status (server);
              if (result)
                usage |= GNUTLS_KEY_DIGITAL_SIGNATURE;

              result = get_encrypt_status (server);
              if (result)
                usage |= GNUTLS_KEY_KEY_ENCIPHERMENT;
            }
          else
            usage |= GNUTLS_KEY_DIGITAL_SIGNATURE;

          if (is_ike)
            {
              result =
                gnutls_x509_crt_set_key_purpose_oid (crt,
                                                     GNUTLS_KP_IPSEC_IKE, 0);
              if (result < 0)
                error (EXIT_FAILURE, 0, "key_kp: %s",
                       gnutls_strerror (result));
            }
        }


      if (ca_status)
        {
          result = get_cert_sign_status ();
          if (result)
            usage |= GNUTLS_KEY_KEY_CERT_SIGN;

          result = get_crl_sign_status ();
          if (result)
            usage |= GNUTLS_KEY_CRL_SIGN;

          result = get_code_sign_status ();
          if (result)
            {
              result =
                gnutls_x509_crt_set_key_purpose_oid (crt,
                                                     GNUTLS_KP_CODE_SIGNING,
                                                     0);
              if (result < 0)
                error (EXIT_FAILURE, 0, "key_kp: %s",
                       gnutls_strerror (result));
            }

          result = get_ocsp_sign_status ();
          if (result)
            {
              result =
                gnutls_x509_crt_set_key_purpose_oid (crt,
                                                     GNUTLS_KP_OCSP_SIGNING,
                                                     0);
              if (result < 0)
                error (EXIT_FAILURE, 0, "key_kp: %s",
                       gnutls_strerror (result));
            }

          result = get_time_stamp_status ();
          if (result)
            {
              result =
                gnutls_x509_crt_set_key_purpose_oid (crt,
                                                     GNUTLS_KP_TIME_STAMPING,
                                                     0);
              if (result < 0)
                error (EXIT_FAILURE, 0, "key_kp: %s",
                       gnutls_strerror (result));
            }
        }

      if (usage != 0)
        {
          /* http://tools.ietf.org/html/rfc4945#section-5.1.3.2: if any KU is
             set, then either digitalSignature or the nonRepudiation bits in the
             KeyUsage extension MUST for all IKE certs */
          if (is_ike && (get_sign_status (server) != 1))
            usage |= GNUTLS_KEY_NON_REPUDIATION;
          result = gnutls_x509_crt_set_key_usage (crt, usage);
          if (result < 0)
            error (EXIT_FAILURE, 0, "key_usage: %s",
                   gnutls_strerror (result));
        }

      /* Subject Key ID.
       */
      size = buffer_size;
      result = gnutls_x509_crt_get_key_id (crt, 0, buffer, &size);
      if (result >= 0)
        {
          result = gnutls_x509_crt_set_subject_key_id (crt, buffer, size);
          if (result < 0)
            error (EXIT_FAILURE, 0, "set_subject_key_id: %s",
                   gnutls_strerror (result));
        }

      /* Authority Key ID.
       */
      if (ca_crt != NULL)
        {
          size = buffer_size;
          result = gnutls_x509_crt_get_subject_key_id (ca_crt, buffer,
                                                       &size, NULL);
          if (result < 0)
            {
              size = buffer_size;
              result = gnutls_x509_crt_get_key_id (ca_crt, 0, buffer, &size);
            }
          if (result >= 0)
            {
              result =
                gnutls_x509_crt_set_authority_key_id (crt, buffer, size);
              if (result < 0)
                error (EXIT_FAILURE, 0, "set_authority_key_id: %s",
                       gnutls_strerror (result));
            }
        }
    }

  /* Version.
   */
  if (info.v1_cert != 0)
    vers = 1;
  else
    vers = 3;
  result = gnutls_x509_crt_set_version (crt, vers);
  if (result < 0)
    error (EXIT_FAILURE, 0, "set_version: %s", gnutls_strerror (result));

  *ret_key = key;
  return crt;

}

static gnutls_x509_crl_t
generate_crl (gnutls_x509_crt_t ca_crt, common_info_st * cinfo)
{
  gnutls_x509_crl_t crl;
  gnutls_x509_crt_t *crts;
  size_t size;
  int days, result;
  unsigned int i;
  time_t now = time (NULL);

  result = gnutls_x509_crl_init (&crl);
  if (result < 0)
    error (EXIT_FAILURE, 0, "crl_init: %s", gnutls_strerror (result));

  crts = load_cert_list (0, &size, cinfo);

  for (i = 0; i < size; i++)
    {
      result = gnutls_x509_crl_set_crt (crl, crts[i], now);
      if (result < 0)
        error (EXIT_FAILURE, 0, "crl_set_crt: %s", gnutls_strerror (result));
    }

  result = gnutls_x509_crl_set_this_update (crl, now);
  if (result < 0)
    error (EXIT_FAILURE, 0, "this_update: %s", gnutls_strerror (result));

  fprintf (stderr, "Update times.\n");
  days = get_crl_next_update ();

  result = gnutls_x509_crl_set_next_update (crl, now + days * 24 * 60 * 60);
  if (result < 0)
    error (EXIT_FAILURE, 0, "next_update: %s", gnutls_strerror (result));

  result = gnutls_x509_crl_set_version (crl, 2);
  if (result < 0)
    error (EXIT_FAILURE, 0, "set_version: %s", gnutls_strerror (result));

  /* Authority Key ID.
   */
  if (ca_crt != NULL)
    {
      size = buffer_size;
      result = gnutls_x509_crt_get_subject_key_id (ca_crt, buffer,
                                                   &size, NULL);
      if (result < 0)
        {
          size = buffer_size;
          result = gnutls_x509_crt_get_key_id (ca_crt, 0, buffer, &size);
        }
      if (result >= 0)
        {
          result = gnutls_x509_crl_set_authority_key_id (crl, buffer, size);
          if (result < 0)
            error (EXIT_FAILURE, 0, "set_authority_key_id: %s",
                   gnutls_strerror (result));
        }
    }

  {
    unsigned int number = get_crl_number ();
    char bin_number[5];

    bin_number[4] = number & 0xff;
    bin_number[3] = (number >> 8) & 0xff;
    bin_number[2] = (number >> 16) & 0xff;
    bin_number[1] = (number >> 24) & 0xff;
    bin_number[0] = 0;

    result = gnutls_x509_crl_set_number (crl, bin_number, 5);
    if (result < 0)
      error (EXIT_FAILURE, 0, "set_number: %s", gnutls_strerror (result));
  }

  return crl;
}

static gnutls_digest_algorithm_t
get_dig (gnutls_x509_crt crt)
{
  gnutls_digest_algorithm_t dig;
  int result;
  unsigned int mand;

  result = gnutls_x509_crt_get_preferred_hash_algorithm (crt, &dig, &mand);
  if (result < 0)
    {
      error (EXIT_FAILURE, 0, "crl_preferred_hash_algorithm: %s",
             gnutls_strerror (result));
    }

  /* if algorithm allows alternatives */
  if (mand == 0 && default_dig != GNUTLS_DIG_UNKNOWN)
    dig = default_dig;

  return dig;
}

void
generate_self_signed (common_info_st * cinfo)
{
  gnutls_x509_crt_t crt;
  gnutls_x509_privkey_t key;
  size_t size;
  int result;
  const char *uri;

  fprintf (stderr, "Generating a self signed certificate...\n");

  crt = generate_certificate (&key, NULL, 0, cinfo);

  if (!key)
    key = load_private_key (1, cinfo);

  uri = get_crl_dist_point_url ();
  if (uri)
    {
      result = gnutls_x509_crt_set_crl_dist_points (crt, GNUTLS_SAN_URI,
                                                    uri,
                                                    0 /* all reasons */ );
      if (result < 0)
        error (EXIT_FAILURE, 0, "crl_dist_points: %s",
               gnutls_strerror (result));
    }

  print_certificate_info (crt, stderr, 0);

  fprintf (stderr, "\n\nSigning certificate...\n");

  result = gnutls_x509_crt_sign2 (crt, crt, key, get_dig (crt), 0);
  if (result < 0)
    error (EXIT_FAILURE, 0, "crt_sign: %s", gnutls_strerror (result));

  size = buffer_size;
  result = gnutls_x509_crt_export (crt, info.outcert_format, buffer, &size);
  if (result < 0)
    error (EXIT_FAILURE, 0, "crt_export: %s", gnutls_strerror (result));

  fwrite (buffer, 1, size, outfile);

  gnutls_x509_crt_deinit (crt);
  gnutls_x509_privkey_deinit (key);
}

static void
generate_signed_certificate (common_info_st * cinfo)
{
  gnutls_x509_crt_t crt;
  gnutls_x509_privkey_t key;
  size_t size;
  int result;
  gnutls_x509_privkey_t ca_key;
  gnutls_x509_crt_t ca_crt;

  fprintf (stderr, "Generating a signed certificate...\n");

  ca_key = load_ca_private_key (cinfo);
  ca_crt = load_ca_cert (cinfo);

  crt = generate_certificate (&key, ca_crt, 0, cinfo);

  /* Copy the CRL distribution points.
   */
  gnutls_x509_crt_cpy_crl_dist_points (crt, ca_crt);
  /* it doesn't matter if we couldn't copy the CRL dist points.
   */

  print_certificate_info (crt, stderr, 0);

  fprintf (stderr, "\n\nSigning certificate...\n");

  result = gnutls_x509_crt_sign2 (crt, ca_crt, ca_key, get_dig (ca_crt), 0);
  if (result < 0)
    error (EXIT_FAILURE, 0, "crt_sign: %s", gnutls_strerror (result));

  size = buffer_size;
  result = gnutls_x509_crt_export (crt, info.outcert_format, buffer, &size);
  if (result < 0)
    error (EXIT_FAILURE, 0, "crt_export: %s", gnutls_strerror (result));

  fwrite (buffer, 1, size, outfile);

  gnutls_x509_crt_deinit (crt);
  gnutls_x509_privkey_deinit (key);
}

static void
generate_proxy_certificate (common_info_st * cinfo)
{
  gnutls_x509_crt_t crt, eecrt;
  gnutls_x509_privkey_t key, eekey;
  size_t size;
  int result;

  fprintf (stderr, "Generating a proxy certificate...\n");

  eekey = load_ca_private_key (cinfo);
  eecrt = load_cert (1, cinfo);

  crt = generate_certificate (&key, eecrt, 1, cinfo);

  print_certificate_info (crt, stderr, 0);

  fprintf (stderr, "\n\nSigning certificate...\n");

  result = gnutls_x509_crt_sign2 (crt, eecrt, eekey, get_dig (eecrt), 0);
  if (result < 0)
    error (EXIT_FAILURE, 0, "crt_sign: %s", gnutls_strerror (result));

  size = buffer_size;
  result = gnutls_x509_crt_export (crt, info.outcert_format, buffer, &size);
  if (result < 0)
    error (EXIT_FAILURE, 0, "crt_export: %s", gnutls_strerror (result));

  fwrite (buffer, 1, size, outfile);

  gnutls_x509_crt_deinit (crt);
  gnutls_x509_privkey_deinit (key);
}

static void
generate_signed_crl (common_info_st * cinfo)
{
  gnutls_x509_crl_t crl;
  int result;
  gnutls_x509_privkey_t ca_key;
  gnutls_x509_crt_t ca_crt;

  fprintf (stderr, "Generating a signed CRL...\n");

  ca_key = load_ca_private_key (cinfo);
  ca_crt = load_ca_cert (cinfo);
  crl = generate_crl (ca_crt, cinfo);

  fprintf (stderr, "\n");

  result = gnutls_x509_crl_sign (crl, ca_crt, ca_key);
  if (result < 0)
    error (EXIT_FAILURE, 0, "crl_sign: %s", gnutls_strerror (result));

  print_crl_info (crl, stderr);

  gnutls_x509_crl_deinit (crl);
}

static void
update_signed_certificate (common_info_st * cinfo)
{
  gnutls_x509_crt_t crt;
  size_t size;
  int result;
  gnutls_x509_privkey_t ca_key;
  gnutls_x509_crt_t ca_crt;
  int days;
  time_t tim = time (NULL);

  fprintf (stderr, "Generating a signed certificate...\n");

  ca_key = load_ca_private_key (cinfo);
  ca_crt = load_ca_cert (cinfo);
  crt = load_cert (1, cinfo);

  fprintf (stderr, "Activation/Expiration time.\n");
  gnutls_x509_crt_set_activation_time (crt, tim);

  days = get_days ();

  result =
    gnutls_x509_crt_set_expiration_time (crt, tim + days * 24 * 60 * 60);
  if (result < 0)
    error (EXIT_FAILURE, 0, "set_expiration: %s", gnutls_strerror (result));

  fprintf (stderr, "\n\nSigning certificate...\n");

  result = gnutls_x509_crt_sign2 (crt, ca_crt, ca_key, get_dig (ca_crt), 0);
  if (result < 0)
    error (EXIT_FAILURE, 0, "crt_sign: %s", gnutls_strerror (result));

  size = buffer_size;
  result = gnutls_x509_crt_export (crt, info.outcert_format, buffer, &size);
  if (result < 0)
    error (EXIT_FAILURE, 0, "crt_export: %s", gnutls_strerror (result));

  fwrite (buffer, 1, size, outfile);

  gnutls_x509_crt_deinit (crt);
}

void
gaa_parser (int argc, char **argv)
{
  int ret;
  common_info_st cinfo;

  if (gaa (argc, argv, &info) != -1)
    {
      fprintf (stderr, "Try `%s --help' for more information.\n",
               program_name);
      exit (1);
    }

  if (info.outfile)
    {
      outfile = safe_open_rw (info.outfile, info.privkey_op);
      if (outfile == NULL)
        error (EXIT_FAILURE, errno, "%s", info.outfile);
    }
  else
    outfile = stdout;

  if (info.infile)
    {
      infile = fopen (info.infile, "rb");
      if (infile == NULL)
        error (EXIT_FAILURE, errno, "%s", info.infile);
    }
  else
    infile = stdin;

  if (info.incert_format)
    info.incert_format = GNUTLS_X509_FMT_DER;
  else
    info.incert_format = GNUTLS_X509_FMT_PEM;

  if (info.outcert_format)
    info.outcert_format = GNUTLS_X509_FMT_DER;
  else
    info.outcert_format = GNUTLS_X509_FMT_PEM;

  default_dig = GNUTLS_DIG_UNKNOWN;
  if (info.hash != NULL)
    {
      if (strcasecmp (info.hash, "md5") == 0)
        {
          fprintf (stderr,
                   "Warning: MD5 is broken, and should not be used any more for digital signatures.\n");
          default_dig = GNUTLS_DIG_MD5;
        }
      else if (strcasecmp (info.hash, "sha1") == 0)
        default_dig = GNUTLS_DIG_SHA1;
      else if (strcasecmp (info.hash, "sha256") == 0)
        default_dig = GNUTLS_DIG_SHA256;
      else if (strcasecmp (info.hash, "sha224") == 0)
        default_dig = GNUTLS_DIG_SHA224;
      else if (strcasecmp (info.hash, "sha384") == 0)
        default_dig = GNUTLS_DIG_SHA384;
      else if (strcasecmp (info.hash, "sha512") == 0)
        default_dig = GNUTLS_DIG_SHA512;
      else if (strcasecmp (info.hash, "rmd160") == 0)
        default_dig = GNUTLS_DIG_RMD160;
      else
        error (EXIT_FAILURE, 0, "invalid hash: %s", info.hash);
    }

  batch = 0;
  if (info.template)
    {
      batch = 1;
      template_parse (info.template);
    }

  gnutls_global_set_log_function (tls_log_func);
  gnutls_global_set_log_level (info.debug);
  if (info.debug > 1)
    printf ("Setting log level to %d\n", info.debug);

  if ((ret = gnutls_global_init ()) < 0)
    error (EXIT_FAILURE, 0, "global_init: %s", gnutls_strerror (ret));

  if ((ret = gnutls_global_init_extra ()) < 0)
    error (EXIT_FAILURE, 0, "global_init_extra: %s", gnutls_strerror (ret));

  memset (&cinfo, 0, sizeof (cinfo));
  cinfo.privkey = info.privkey;
  cinfo.pubkey = info.pubkey;
  cinfo.pkcs8 = info.pkcs8;
  cinfo.incert_format = info.incert_format;
  cinfo.cert = info.cert;
  cinfo.request = info.request;
  cinfo.ca = info.ca;
  cinfo.ca_privkey = info.ca_privkey;

  switch (info.action)
    {
    case ACTION_SELF_SIGNED:
      generate_self_signed (&cinfo);
      break;
    case ACTION_GENERATE_PRIVKEY:
      generate_private_key ();
      break;
    case ACTION_CERT_INFO:
      certificate_info (0, &cinfo);
      break;
    case ACTION_CERT_PUBKEY:
      certificate_info (1, &cinfo);
      break;
    case ACTION_GENERATE_REQUEST:
      generate_request (&cinfo);
      break;
    case ACTION_GENERATE_CERTIFICATE:
      generate_signed_certificate (&cinfo);
      break;
    case ACTION_VERIFY_CHAIN:
      verify_chain ();
      break;
    case ACTION_PRIVKEY_INFO:
      privkey_info ();
      break;
    case ACTION_PUBKEY_INFO:
      pubkey_info (NULL, &cinfo);
      break;
    case ACTION_UPDATE_CERTIFICATE:
      update_signed_certificate (&cinfo);
      break;
    case ACTION_TO_PKCS12:
      generate_pkcs12 (&cinfo);
      break;
    case ACTION_PKCS12_INFO:
      pkcs12_info ();
      break;
    case ACTION_GENERATE_DH:
      generate_prime (1);
      break;
    case ACTION_GET_DH:
      generate_prime (0);
      break;
    case ACTION_CRL_INFO:
      crl_info ();
      break;
    case ACTION_P7_INFO:
      pkcs7_info ();
      break;
    case ACTION_GENERATE_CRL:
      generate_signed_crl (&cinfo);
      break;
    case ACTION_VERIFY_CRL:
      verify_crl (&cinfo);
      break;
    case ACTION_SMIME_TO_P7:
      smime_to_pkcs7 ();
      break;
    case ACTION_GENERATE_PROXY:
      generate_proxy_certificate (&cinfo);
      break;
    case ACTION_GENERATE_PKCS8:
      generate_pkcs8 (&cinfo);
      break;
#ifdef ENABLE_OPENPGP
    case ACTION_PGP_INFO:
      pgp_certificate_info ();
      break;
    case ACTION_PGP_PRIVKEY_INFO:
      pgp_privkey_info ();
      break;
    case ACTION_RING_INFO:
      pgp_ring_info ();
      break;
#endif
    case ACTION_REQUEST:
      crq_info ();
      break;
    default:
      gaa_help ();
      exit (0);
    }
  fclose (outfile);

  gnutls_pkcs11_deinit ();
  gnutls_global_deinit ();
}

#define MAX_CRTS 500
void
certificate_info (int pubkey, common_info_st * cinfo)
{
  gnutls_x509_crt_t crt[MAX_CRTS];
  size_t size;
  int ret, i, count;
  gnutls_datum_t pem;
  unsigned int crt_num;

  pem.data = fread_file (infile, &size);
  pem.size = size;

  crt_num = MAX_CRTS;
  ret =
    gnutls_x509_crt_list_import (crt, &crt_num, &pem, info.incert_format,
                                 GNUTLS_X509_CRT_LIST_IMPORT_FAIL_IF_EXCEED);
  if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER)
    {
      error (0, 0, "too many certificates (%d); "
             "will only read the first %d", crt_num, MAX_CRTS);
      crt_num = MAX_CRTS;
      ret = gnutls_x509_crt_list_import (crt, &crt_num, &pem,
                                         info.incert_format, 0);
    }
  if (ret < 0)
    error (EXIT_FAILURE, 0, "import error: %s", gnutls_strerror (ret));

  free (pem.data);

  count = ret;

  if (count > 1 && info.outcert_format == GNUTLS_X509_FMT_DER)
    {
      error (0, 0, "cannot output multiple certificates in DER format; "
             "using PEM instead");
      info.outcert_format = GNUTLS_X509_FMT_PEM;
    }

  for (i = 0; i < count; i++)
    {
      if (i > 0)
        fprintf (outfile, "\n");

      if (info.outcert_format == GNUTLS_X509_FMT_PEM)
        print_certificate_info (crt[i], outfile, 1);

      size = buffer_size;
      ret = gnutls_x509_crt_export (crt[i], info.outcert_format, buffer,
                                    &size);
      if (ret < 0)
        error (EXIT_FAILURE, 0, "export error: %s", gnutls_strerror (ret));

      fwrite (buffer, 1, size, outfile);

      if (pubkey)
        pubkey_info (crt[i], cinfo);

      gnutls_x509_crt_deinit (crt[i]);
    }
}

#ifdef ENABLE_OPENPGP

void
pgp_certificate_info (void)
{
  gnutls_openpgp_crt_t crt;
  size_t size;
  int ret;
  gnutls_datum_t pem, out_data;
  unsigned int verify_status;

  pem.data = fread_file (infile, &size);
  pem.size = size;

  ret = gnutls_openpgp_crt_init (&crt);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "openpgp_crt_init: %s", gnutls_strerror (ret));

  ret = gnutls_openpgp_crt_import (crt, &pem, info.incert_format);

  if (ret < 0)
    error (EXIT_FAILURE, 0, "import error: %s", gnutls_strerror (ret));

  free (pem.data);

  if (info.outcert_format == GNUTLS_OPENPGP_FMT_BASE64)
    {
      ret = gnutls_openpgp_crt_print (crt, 0, &out_data);

      if (ret == 0)
        {
          fprintf (outfile, "%s\n", out_data.data);
          gnutls_free (out_data.data);
        }
    }


  ret = gnutls_openpgp_crt_verify_self (crt, 0, &verify_status);
  if (ret < 0)
    {
      error (EXIT_FAILURE, 0, "verify signature error: %s",
             gnutls_strerror (ret));
    }

  if (verify_status & GNUTLS_CERT_INVALID)
    {
      fprintf (outfile, "Self Signature verification: failed\n\n");
    }
  else
    {
      fprintf (outfile, "Self Signature verification: ok (%x)\n\n",
               verify_status);
    }

  size = buffer_size;
  ret = gnutls_openpgp_crt_export (crt, info.outcert_format, buffer, &size);
  if (ret < 0)
    {
      error (EXIT_FAILURE, 0, "export error: %s", gnutls_strerror (ret));
      fwrite (buffer, 1, size, outfile);
    }

  fprintf (outfile, "%s\n", buffer);
  gnutls_openpgp_crt_deinit (crt);
}

void
pgp_privkey_info (void)
{
  gnutls_openpgp_privkey_t key;
  gnutls_openpgp_keyid_t keyid;
  size_t size;
  int ret, i, subkeys;
  gnutls_datum_t pem;
  const char *cprint;

  size = fread (buffer, 1, buffer_size - 1, infile);
  buffer[size] = 0;

  gnutls_openpgp_privkey_init (&key);

  pem.data = buffer;
  pem.size = size;

  ret = gnutls_openpgp_privkey_import (key, &pem, info.incert_format,
                                       NULL, 0);

  if (ret < 0)
    error (EXIT_FAILURE, 0, "import error: %s", gnutls_strerror (ret));

  /* Public key algorithm
   */
  subkeys = gnutls_openpgp_privkey_get_subkey_count (key);
  if (subkeys < 0)
    error (EXIT_FAILURE, 0, "privkey_get_subkey_count: %s",
           gnutls_strerror (subkeys));

  for (i = -1; i < subkeys; i++)
    {

      if (i != -1)
        fprintf (outfile, "Subkey[%d]:\n", i);

      fprintf (outfile, "Public Key Info:\n");

      if (i == -1)
        ret = gnutls_openpgp_privkey_get_pk_algorithm (key, NULL);
      else
        ret = gnutls_openpgp_privkey_get_subkey_pk_algorithm (key, i, NULL);

      fprintf (outfile, "\tPublic Key Algorithm: ");
      cprint = gnutls_pk_algorithm_get_name (ret);
      fprintf (outfile, "%s\n", cprint ? cprint : "Unknown");
      fprintf (outfile, "\tKey Security Level: %s\n",
               gnutls_sec_param_get_name (gnutls_openpgp_privkey_sec_param
                                          (key)));

      /* Print the raw public and private keys
       */

      if (ret == GNUTLS_PK_RSA)
        {
          gnutls_datum_t m, e, d, p, q, u;

          if (i == -1)
            ret =
              gnutls_openpgp_privkey_export_rsa_raw (key, &m, &e, &d, &p,
                                                     &q, &u);
          else
            ret =
              gnutls_openpgp_privkey_export_subkey_rsa_raw (key, i, &m,
                                                            &e, &d, &p,
                                                            &q, &u);
          if (ret < 0)
            fprintf (stderr, "Error in key RSA data export: %s\n",
                     gnutls_strerror (ret));
          else
            print_rsa_pkey (&m, &e, &d, &p, &q, &u, NULL, NULL);

        }
      else if (ret == GNUTLS_PK_DSA)
        {
          gnutls_datum_t p, q, g, y, x;

          if (i == -1)
            ret =
              gnutls_openpgp_privkey_export_dsa_raw (key, &p, &q, &g, &y, &x);
          else
            ret =
              gnutls_openpgp_privkey_export_subkey_dsa_raw (key, i, &p,
                                                            &q, &g, &y, &x);
          if (ret < 0)
            fprintf (stderr, "Error in key DSA data export: %s\n",
                     gnutls_strerror (ret));
          else
            print_dsa_pkey (&x, &y, &p, &q, &g);
        }

      fprintf (outfile, "\n");

      size = buffer_size;
      if (i == -1)
        ret = gnutls_openpgp_privkey_get_key_id (key, keyid);
      else
        ret = gnutls_openpgp_privkey_get_subkey_id (key, i, keyid);

      if (ret < 0)
        {
          fprintf (stderr, "Error in key id calculation: %s\n",
                   gnutls_strerror (ret));
        }
      else
        {
          fprintf (outfile, "Public Key ID: %s\n", raw_to_string (keyid, 8));
        }

    }

  size = buffer_size;
  ret = gnutls_openpgp_privkey_export (key, GNUTLS_OPENPGP_FMT_BASE64,
                                       NULL, 0, buffer, &size);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "export error: %s", gnutls_strerror (ret));

  fprintf (outfile, "\n%s\n", buffer);

  gnutls_openpgp_privkey_deinit (key);
}

void
pgp_ring_info (void)
{
  gnutls_openpgp_keyring_t ring;
  gnutls_openpgp_crt_t crt;
  size_t size;
  int ret, i, count;
  gnutls_datum_t pem;

  pem.data = fread_file (infile, &size);
  pem.size = size;

  ret = gnutls_openpgp_keyring_init (&ring);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "openpgp_keyring_init: %s",
           gnutls_strerror (ret));

  ret = gnutls_openpgp_keyring_import (ring, &pem, info.incert_format);

  if (ret < 0)
    error (EXIT_FAILURE, 0, "import error: %s", gnutls_strerror (ret));

  free (pem.data);

  count = gnutls_openpgp_keyring_get_crt_count (ring);
  if (count > 0)
    fprintf (outfile, "Keyring contains %d OpenPGP certificates\n\n", count);
  else
    error (EXIT_FAILURE, 0, "keyring error: %s", gnutls_strerror (count));

  for (i = 0; i < count; i++)
    {
      ret = gnutls_openpgp_keyring_get_crt (ring, i, &crt);
      if (ret < 0)
        error (EXIT_FAILURE, 0, "export error: %s", gnutls_strerror (ret));

      size = buffer_size;
      ret = gnutls_openpgp_crt_export (crt, info.outcert_format,
                                       buffer, &size);
      if (ret < 0)
        error (EXIT_FAILURE, 0, "export error: %s", gnutls_strerror (ret));

      fwrite (buffer, 1, size, outfile);
      fprintf (outfile, "\n\n");

      gnutls_openpgp_crt_deinit (crt);


    }

  gnutls_openpgp_keyring_deinit (ring);
}


#endif

static void
print_hex_datum (gnutls_datum_t * dat)
{
  unsigned int j;
#define SPACE "\t"
  fprintf (outfile, "\n" SPACE);
  for (j = 0; j < dat->size; j++)
    {
      fprintf (outfile, "%.2x:", (unsigned char) dat->data[j]);
      if ((j + 1) % 15 == 0)
        fprintf (outfile, "\n" SPACE);
    }
  fprintf (outfile, "\n");
}


static void
print_certificate_info (gnutls_x509_crt_t crt, FILE * out, unsigned int all)
{
  gnutls_datum_t cinfo;
  int ret;

  if (all)
    ret = gnutls_x509_crt_print (crt, GNUTLS_CRT_PRINT_FULL, &cinfo);
  else
    ret = gnutls_x509_crt_print (crt, GNUTLS_CRT_PRINT_UNSIGNED_FULL, &cinfo);
  if (ret == 0)
    {
      fprintf (out, "%s\n", cinfo.data);
      gnutls_free (cinfo.data);
    }

  if (out == stderr && batch == 0)      /* interactive */
    if (read_yesno ("Is the above information ok? (y/N): ") == 0)
      {
        exit (1);
      }
}

static void
print_crl_info (gnutls_x509_crl_t crl, FILE * out)
{
  gnutls_datum_t cinfo;
  int ret;
  size_t size;

  ret = gnutls_x509_crl_print (crl, GNUTLS_CRT_PRINT_FULL, &cinfo);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "crl_print: %s", gnutls_strerror (ret));

  fprintf (out, "%s\n", cinfo.data);

  gnutls_free (cinfo.data);

  size = buffer_size;
  ret = gnutls_x509_crl_export (crl, GNUTLS_X509_FMT_PEM, buffer, &size);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "crl_export: %s", gnutls_strerror (ret));

  fwrite (buffer, 1, size, outfile);
}

void
crl_info (void)
{
  gnutls_x509_crl_t crl;
  int ret;
  size_t size;
  gnutls_datum_t pem;

  ret = gnutls_x509_crl_init (&crl);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "crl_init: %s", gnutls_strerror (ret));

  pem.data = fread_file (infile, &size);
  pem.size = size;

  if (!pem.data)
    error (EXIT_FAILURE, errno, "%s", info.infile ? info.infile :
           "standard input");

  ret = gnutls_x509_crl_import (crl, &pem, info.incert_format);

  free (pem.data);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "import error: %s", gnutls_strerror (ret));

  print_crl_info (crl, outfile);

  gnutls_x509_crl_deinit (crl);
}

static void
print_crq_info (gnutls_x509_crq_t crq, FILE * out)
{
  gnutls_datum_t cinfo;
  int ret;
  size_t size;

  if (info.outcert_format == GNUTLS_X509_FMT_PEM)
    {
      ret = gnutls_x509_crq_print (crq, GNUTLS_CRT_PRINT_FULL, &cinfo);
      if (ret < 0)
        error (EXIT_FAILURE, 0, "crq_print: %s", gnutls_strerror (ret));

      fprintf (out, "%s\n", cinfo.data);

      gnutls_free (cinfo.data);
    }

  size = buffer_size;
  ret = gnutls_x509_crq_export (crq, info.outcert_format, buffer, &size);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "crq_export: %s", gnutls_strerror (ret));

  fwrite (buffer, 1, size, outfile);
}

void
crq_info (void)
{
  gnutls_x509_crq_t crq;
  int ret;
  size_t size;
  gnutls_datum_t pem;

  ret = gnutls_x509_crq_init (&crq);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "crq_init: %s", gnutls_strerror (ret));

  pem.data = fread_file (infile, &size);
  pem.size = size;

  if (!pem.data)
    error (EXIT_FAILURE, errno, "%s", info.infile ? info.infile :
           "standard input");

  ret = gnutls_x509_crq_import (crq, &pem, info.incert_format);

  free (pem.data);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "import error: %s", gnutls_strerror (ret));

  print_crq_info (crq, outfile);

  gnutls_x509_crq_deinit (crq);
}

void
privkey_info (void)
{
  gnutls_x509_privkey_t key;
  size_t size;
  int ret;
  gnutls_datum_t pem;
  const char *cprint;
  const char *pass;

  size = fread (buffer, 1, buffer_size - 1, infile);
  buffer[size] = 0;

  gnutls_x509_privkey_init (&key);

  pem.data = buffer;
  pem.size = size;

  ret = 0;
  if (!info.pkcs8)
    ret = gnutls_x509_privkey_import (key, &pem, info.incert_format);

  /* If we failed to import the certificate previously try PKCS #8 */
  if (info.pkcs8 || ret == GNUTLS_E_BASE64_UNEXPECTED_HEADER_ERROR)
    {
      if (info.pass)
        pass = info.pass;
      else
        pass = get_pass ();
      ret = gnutls_x509_privkey_import_pkcs8 (key, &pem,
                                              info.incert_format, pass, 0);
    }
  if (ret < 0)
    error (EXIT_FAILURE, 0, "import error: %s", gnutls_strerror (ret));

  /* Public key algorithm
   */
  fprintf (outfile, "Public Key Info:\n");
  ret = gnutls_x509_privkey_get_pk_algorithm (key);
  fprintf (outfile, "\tPublic Key Algorithm: ");

  cprint = gnutls_pk_algorithm_get_name (ret);
  fprintf (outfile, "%s\n", cprint ? cprint : "Unknown");
  fprintf (outfile, "\tKey Security Level: %s\n",
           gnutls_sec_param_get_name (gnutls_x509_privkey_sec_param (key)));

  /* Print the raw public and private keys
   */
  if (ret == GNUTLS_PK_RSA)
    {
      gnutls_datum_t m, e, d, p, q, u, exp1, exp2;

      ret =
        gnutls_x509_privkey_export_rsa_raw2 (key, &m, &e, &d, &p, &q, &u,
                                             &exp1, &exp2);
      if (ret < 0)
        fprintf (stderr, "Error in key RSA data export: %s\n",
                 gnutls_strerror (ret));
      else
        {
          print_rsa_pkey (&m, &e, &d, &p, &q, &u, &exp1, &exp2);
          gnutls_free (m.data);
          gnutls_free (e.data);
          gnutls_free (d.data);
          gnutls_free (p.data);
          gnutls_free (q.data);
          gnutls_free (u.data);
          gnutls_free (exp1.data);
          gnutls_free (exp2.data);
        }
    }
  else if (ret == GNUTLS_PK_DSA)
    {
      gnutls_datum_t p, q, g, y, x;

      ret = gnutls_x509_privkey_export_dsa_raw (key, &p, &q, &g, &y, &x);
      if (ret < 0)
        fprintf (stderr, "Error in key DSA data export: %s\n",
                 gnutls_strerror (ret));
      else
        {
          print_dsa_pkey (&x, &y, &p, &q, &g);
          gnutls_free (x.data);
          gnutls_free (y.data);
          gnutls_free (p.data);
          gnutls_free (q.data);
          gnutls_free (g.data);
        }
    }

  fprintf (outfile, "\n");

  size = buffer_size;
  if ((ret = gnutls_x509_privkey_get_key_id (key, 0, buffer, &size)) < 0)
    {
      fprintf (stderr, "Error in key id calculation: %s\n",
               gnutls_strerror (ret));
    }
  else
    {
      fprintf (outfile, "Public Key ID: %s\n", raw_to_string (buffer, size));
    }

  if (info.fix_key != 0)
    {
      ret = gnutls_x509_privkey_fix (key);
      if (ret < 0)
        error (EXIT_FAILURE, 0, "privkey_fix: %s", gnutls_strerror (ret));
    }

  size = buffer_size;
  ret = gnutls_x509_privkey_export (key, GNUTLS_X509_FMT_PEM, buffer, &size);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "export error: %s", gnutls_strerror (ret));

  fprintf (outfile, "\n%s\n", buffer);

  gnutls_x509_privkey_deinit (key);
}


/* Generate a PKCS #10 certificate request.
 */
void
generate_request (common_info_st * cinfo)
{
  gnutls_x509_crq_t crq;
  gnutls_x509_privkey_t key;
  int ret, ca_status, path_len;
  const char *pass;
  unsigned int usage = 0;

  fprintf (stderr, "Generating a PKCS #10 certificate request...\n");

  ret = gnutls_x509_crq_init (&crq);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "crq_init: %s", gnutls_strerror (ret));

  /* Load the private key.
   */
  key = load_private_key (0, cinfo);
  if (!key)
    {
      key = generate_private_key_int ();

      print_private_key (key);
    }

  /* Set the DN.
   */
  get_country_crq_set (crq);
  get_organization_crq_set (crq);
  get_unit_crq_set (crq);
  get_locality_crq_set (crq);
  get_state_crq_set (crq);
  get_cn_crq_set (crq);
  get_uid_crq_set (crq);
  get_oid_crq_set (crq);

  get_dns_name_set (TYPE_CRQ, crq);
  get_ip_addr_set (TYPE_CRQ, crq);
  get_email_set (TYPE_CRQ, crq);

  pass = get_challenge_pass ();

  if (pass != NULL && pass[0] != 0)
    {
      ret = gnutls_x509_crq_set_challenge_password (crq, pass);
      if (ret < 0)
        error (EXIT_FAILURE, 0, "set_pass: %s", gnutls_strerror (ret));
    }

  if (info.crq_extensions != 0)
    {
      ca_status = get_ca_status ();
      if (ca_status)
        path_len = get_path_len ();
      else
        path_len = -1;

      ret = gnutls_x509_crq_set_basic_constraints (crq, ca_status, path_len);
      if (ret < 0)
        error (EXIT_FAILURE, 0, "set_basic_constraints: %s",
               gnutls_strerror (ret));

      ret = get_sign_status (1);
      if (ret)
        usage |= GNUTLS_KEY_DIGITAL_SIGNATURE;

      ret = get_encrypt_status (1);
      if (ret)
        usage |= GNUTLS_KEY_KEY_ENCIPHERMENT;
      else
        usage |= GNUTLS_KEY_DIGITAL_SIGNATURE;

      if (ca_status)
        {
          ret = get_cert_sign_status ();
          if (ret)
            usage |= GNUTLS_KEY_KEY_CERT_SIGN;

          ret = get_crl_sign_status ();
          if (ret)
            usage |= GNUTLS_KEY_CRL_SIGN;

          ret = get_code_sign_status ();
          if (ret)
            {
              ret = gnutls_x509_crq_set_key_purpose_oid
                (crq, GNUTLS_KP_CODE_SIGNING, 0);
              if (ret < 0)
                error (EXIT_FAILURE, 0, "key_kp: %s", gnutls_strerror (ret));
            }

          ret = get_ocsp_sign_status ();
          if (ret)
            {
              ret = gnutls_x509_crq_set_key_purpose_oid
                (crq, GNUTLS_KP_OCSP_SIGNING, 0);
              if (ret < 0)
                error (EXIT_FAILURE, 0, "key_kp: %s", gnutls_strerror (ret));
            }

          ret = get_time_stamp_status ();
          if (ret)
            {
              ret = gnutls_x509_crq_set_key_purpose_oid
                (crq, GNUTLS_KP_TIME_STAMPING, 0);
              if (ret < 0)
                error (EXIT_FAILURE, 0, "key_kp: %s", gnutls_strerror (ret));
            }

          ret = get_ipsec_ike_status ();
          if (ret)
            {
              ret = gnutls_x509_crq_set_key_purpose_oid
                (crq, GNUTLS_KP_IPSEC_IKE, 0);
              if (ret < 0)
                error (EXIT_FAILURE, 0, "key_kp: %s", gnutls_strerror (ret));
            }
        }

      ret = gnutls_x509_crq_set_key_usage (crq, usage);
      if (ret < 0)
        error (EXIT_FAILURE, 0, "key_usage: %s", gnutls_strerror (ret));

      ret = get_tls_client_status ();
      if (ret != 0)
        {
          ret = gnutls_x509_crq_set_key_purpose_oid
            (crq, GNUTLS_KP_TLS_WWW_CLIENT, 0);
          if (ret < 0)
            error (EXIT_FAILURE, 0, "key_kp: %s", gnutls_strerror (ret));
        }

      ret = get_tls_server_status ();
      if (ret != 0)
        {
          ret = gnutls_x509_crq_set_key_purpose_oid
            (crq, GNUTLS_KP_TLS_WWW_SERVER, 0);
          if (ret < 0)
            error (EXIT_FAILURE, 0, "key_kp: %s", gnutls_strerror (ret));
        }
    }

  ret = gnutls_x509_crq_set_key (crq, key);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "set_key: %s", gnutls_strerror (ret));

  ret = gnutls_x509_crq_sign (crq, key);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "sign: %s", gnutls_strerror (ret));

  print_crq_info (crq, outfile);

  gnutls_x509_crq_deinit (crq);
  gnutls_x509_privkey_deinit (key);

}

static void print_verification_res (gnutls_x509_crt_t crt,
                                    gnutls_x509_crt_t issuer,
                                    gnutls_x509_crl_t * crl_list,
                                    int crl_list_size, unsigned int flags);

#define CERT_SEP "-----BEGIN CERT"
#define CRL_SEP "-----BEGIN X509 CRL"
static int
_verify_x509_mem (const void *cert, int cert_size)
{
  const char *ptr;
  int ret, i;
  char name[512];
  char issuer_name[512];
  size_t name_size;
  size_t issuer_name_size;
  gnutls_datum_t tmp;
  gnutls_x509_crt_t *x509_cert_list = NULL;
  gnutls_x509_crl_t *x509_crl_list = NULL;
  int x509_ncerts, x509_ncrls;


  /* Decode the CA certificate
   */

  /* Decode the CRL list
   */
  ptr = cert;

  i = 1;

  if (strstr (ptr, CRL_SEP) != NULL)    /* if CRLs exist */
    do
      {
        x509_crl_list =
          (gnutls_x509_crl_t *) realloc (x509_crl_list,
                                         i * sizeof (gnutls_x509_crl_t));
        if (x509_crl_list == NULL)
          error (EXIT_FAILURE, 0, "memory error");

        tmp.data = (char *) ptr;
        tmp.size = cert_size;
        tmp.size -=
          (unsigned int) ((unsigned char *) ptr - (unsigned char *) cert);

        ret = gnutls_x509_crl_init (&x509_crl_list[i - 1]);
        if (ret < 0)
          error (EXIT_FAILURE, 0, "error parsing CRL[%d]: %s", i,
                 gnutls_strerror (ret));

        ret = gnutls_x509_crl_import (x509_crl_list[i - 1], &tmp,
                                      GNUTLS_X509_FMT_PEM);
        if (ret < 0)
          error (EXIT_FAILURE, 0, "error parsing CRL[%d]: %s", i,
                 gnutls_strerror (ret));

        /* now we move ptr after the pem header */
        ptr = strstr (ptr, CRL_SEP);
        if (ptr != NULL)
          ptr++;

        i++;
      }
    while ((ptr = strstr (ptr, CRL_SEP)) != NULL);

  x509_ncrls = i - 1;


  /* Decode the certificate chain. 
   */
  ptr = cert;

  i = 1;

  do
    {
      x509_cert_list =
        (gnutls_x509_crt_t *) realloc (x509_cert_list,
                                       i * sizeof (gnutls_x509_crt_t));
      if (x509_cert_list == NULL)
        error (EXIT_FAILURE, 0, "memory error");


      tmp.data = (char *) ptr;
      tmp.size = cert_size;
      tmp.size -=
        (unsigned int) ((unsigned char *) ptr - (unsigned char *) cert);

      ret = gnutls_x509_crt_init (&x509_cert_list[i - 1]);
      if (ret < 0)
        error (EXIT_FAILURE, 0, "error parsing certificate[%d]: %s", i,
               gnutls_strerror (ret));

      ret =
        gnutls_x509_crt_import (x509_cert_list[i - 1], &tmp,
                                GNUTLS_X509_FMT_PEM);
      if (ret < 0)
        error (EXIT_FAILURE, 0, "error parsing certificate[%d]: %s", i,
               gnutls_strerror (ret));


      if (i - 1 != 0)
        {
          /* verify the previous certificate using this one 
           * as CA.
           */

          name_size = sizeof (name);
          ret =
            gnutls_x509_crt_get_dn (x509_cert_list[i - 2], name, &name_size);
          if (ret < 0)
            error (EXIT_FAILURE, 0, "get_dn: %s", gnutls_strerror (ret));

          fprintf (outfile, "Certificate[%d]: %s\n", i - 2, name);

          /* print issuer 
           */
          issuer_name_size = sizeof (issuer_name);
          ret =
            gnutls_x509_crt_get_issuer_dn (x509_cert_list[i - 2],
                                           issuer_name, &issuer_name_size);
          if (ret < 0)
            error (EXIT_FAILURE, 0, "get_issuer_dn: %s",
                   gnutls_strerror (ret));

          fprintf (outfile, "\tIssued by: %s\n", issuer_name);

          /* Get the Issuer's name
           */
          name_size = sizeof (name);
          ret =
            gnutls_x509_crt_get_dn (x509_cert_list[i - 1], name, &name_size);
          if (ret < 0)
            error (EXIT_FAILURE, 0, "get_dn: %s", gnutls_strerror (ret));

          fprintf (outfile, "\tVerifying against certificate[%d].\n", i - 1);

          if (strcmp (issuer_name, name) != 0)
            {
              fprintf (stderr, "Error: Issuer's name: %s\n", name);
              error (EXIT_FAILURE, 0,
                     "issuer name does not match the next certificate");
            }

          fprintf (outfile, "\tVerification output: ");
          print_verification_res (x509_cert_list[i - 2],
                                  x509_cert_list[i - 1], x509_crl_list,
                                  x509_ncrls,
                                  GNUTLS_VERIFY_DO_NOT_ALLOW_SAME);
          fprintf (outfile, ".\n\n");

        }


      /* now we move ptr after the pem header 
       */
      ptr = strstr (ptr, CERT_SEP);
      if (ptr != NULL)
        ptr++;

      i++;
    }
  while ((ptr = strstr (ptr, CERT_SEP)) != NULL);

  x509_ncerts = i - 1;

  /* The last certificate in the list will be used as
   * a CA (should be self signed).
   */
  name_size = sizeof (name);
  ret = gnutls_x509_crt_get_dn (x509_cert_list[x509_ncerts - 1], name,
                                &name_size);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "get_dn: %s", gnutls_strerror (ret));

  fprintf (outfile, "Certificate[%d]: %s\n", x509_ncerts - 1, name);

  /* print issuer 
   */
  issuer_name_size = sizeof (issuer_name);
  ret =
    gnutls_x509_crt_get_issuer_dn (x509_cert_list[x509_ncerts - 1],
                                   issuer_name, &issuer_name_size);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "get_issuer_dn: %s", gnutls_strerror (ret));

  fprintf (outfile, "\tIssued by: %s\n", name);

  if (strcmp (issuer_name, name) != 0)
    error (EXIT_FAILURE, 0, "the last certificate is not self signed");

  fprintf (outfile, "\tVerification output: ");
  print_verification_res (x509_cert_list[x509_ncerts - 1],
                          x509_cert_list[x509_ncerts - 1], x509_crl_list,
                          /* we add GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT since it is
                           * self signed. */
                          x509_ncrls,
                          GNUTLS_VERIFY_DO_NOT_ALLOW_SAME |
                          GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);

  fprintf (outfile, ".\n\n");

  /* Verify using internal algorithm too. */
  {
    int verify_status;

    ret = gnutls_x509_crt_list_verify (x509_cert_list, x509_ncerts,
                                       &x509_cert_list[x509_ncerts - 1], 1,
                                       x509_crl_list,
                                       x509_ncrls,
                                       GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT |
                                       GNUTLS_VERIFY_DO_NOT_ALLOW_SAME,
                                       &verify_status);
    if (ret < 0)
      error (EXIT_FAILURE, 0, "gnutls_x509_crt_list_verify: %s",
             gnutls_strerror (ret));

    fprintf (outfile, "Chain verification output: ");

    if (verify_status & GNUTLS_CERT_INVALID)
      {
        fprintf (outfile, "Not verified");
      }
    else
      {
        fprintf (outfile, "Verified");
      }

    if (verify_status & GNUTLS_CERT_SIGNER_NOT_CA)
      {
        fprintf (outfile, ", ");
        fprintf (outfile, "Issuer is not a CA");
      }

    if (verify_status & GNUTLS_CERT_INSECURE_ALGORITHM)
      {
        fprintf (outfile, ", ");
        fprintf (outfile, "Insecure algorithm");
      }

    fprintf (outfile, ".\n");
  }

  for (i = 0; i < x509_ncerts; i++)
    gnutls_x509_crt_deinit (x509_cert_list[i]);

  for (i = 0; i < x509_ncrls; i++)
    gnutls_x509_crl_deinit (x509_crl_list[i]);

  free (x509_cert_list);
  free (x509_crl_list);

  if (ret < 0)
    error (EXIT_FAILURE, 0, "verification error: %s", gnutls_strerror (ret));

  return 0;
}

static void
print_verification_res (gnutls_x509_crt_t crt,
                        gnutls_x509_crt_t issuer,
                        gnutls_x509_crl_t * crl_list, int crl_list_size,
                        unsigned int flags)
{
  unsigned int output;
  int comma = 0;
  int ret;

  ret = gnutls_x509_crt_verify (crt, &issuer, 1, flags, &output);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "verification error: %s", gnutls_strerror (ret));

  if (output & GNUTLS_CERT_INVALID)
    {
      fprintf (outfile, "Not verified");
      comma = 1;
    }
  else
    {
      fprintf (outfile, "Verified");
      comma = 1;
    }

  if (output & GNUTLS_CERT_SIGNER_NOT_CA)
    {
      if (comma)
        fprintf (outfile, ", ");
      fprintf (outfile, "Issuer is not a CA");
      comma = 1;
    }

  if (output & GNUTLS_CERT_INSECURE_ALGORITHM)
    {
      if (comma)
        fprintf (outfile, ", ");
      fprintf (outfile, "Insecure algorithm");
      comma = 1;
    }

  if (output & GNUTLS_CERT_NOT_ACTIVATED)
    {
      if (comma)
        fprintf (outfile, ", ");
      fprintf (outfile, "Not activated");
      comma = 1;
    }

  if (output & GNUTLS_CERT_EXPIRED)
    {
      if (comma)
        fprintf (outfile, ", ");
      fprintf (outfile, "Expired");
      comma = 1;
    }

  ret = gnutls_x509_crt_check_revocation (crt, crl_list, crl_list_size);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "revocation check: %s", gnutls_strerror (ret));

  if (ret == 1)
    {                           /* revoked */
      if (comma)
        fprintf (outfile, ", ");
      comma = 1;
      fprintf (outfile, "Revoked");
    }
}

void
verify_chain (void)
{
  char *buf;
  size_t size;

  buf = fread_file (infile, &size);
  if (buf == NULL)
    error (EXIT_FAILURE, errno, "reading chain");

  buf[size] = 0;

  _verify_x509_mem (buf, size);

}

void
verify_crl (common_info_st * cinfo)
{
  size_t size, dn_size;
  char dn[128];
  unsigned int output;
  int comma = 0;
  int ret;
  gnutls_datum_t pem;
  gnutls_x509_crl_t crl;
  time_t now = time (0);
  gnutls_x509_crt_t issuer;

  issuer = load_ca_cert (cinfo);

  fprintf (outfile, "\nCA certificate:\n");

  dn_size = sizeof (dn);
  ret = gnutls_x509_crt_get_dn (issuer, dn, &dn_size);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "crt_get_dn: %s", gnutls_strerror (ret));

  fprintf (outfile, "\tSubject: %s\n\n", dn);

  ret = gnutls_x509_crl_init (&crl);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "crl_init: %s", gnutls_strerror (ret));

  pem.data = fread_file (infile, &size);
  pem.size = size;

  ret = gnutls_x509_crl_import (crl, &pem, info.incert_format);
  free (pem.data);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "import error: %s", gnutls_strerror (ret));

  print_crl_info (crl, outfile);

  fprintf (outfile, "Verification output: ");
  ret = gnutls_x509_crl_verify (crl, &issuer, 1, 0, &output);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "verification error: %s", gnutls_strerror (ret));

  if (output & GNUTLS_CERT_INVALID)
    {
      fprintf (outfile, "Not verified");
      comma = 1;
    }
  else
    {
      fprintf (outfile, "Verified");
      comma = 1;
    }

  if (output & GNUTLS_CERT_SIGNER_NOT_CA)
    {
      if (comma)
        fprintf (outfile, ", ");
      fprintf (outfile, "Issuer is not a CA");
      comma = 1;
    }

  if (output & GNUTLS_CERT_INSECURE_ALGORITHM)
    {
      if (comma)
        fprintf (outfile, ", ");
      fprintf (outfile, "Insecure algorithm");
      comma = 1;
    }

  /* Check expiration dates.
   */

  if (gnutls_x509_crl_get_this_update (crl) > now)
    {
      if (comma)
        fprintf (outfile, ", ");
      comma = 1;
      fprintf (outfile, "Issued in the future!");
    }

  if (gnutls_x509_crl_get_next_update (crl) < now)
    {
      if (comma)
        fprintf (outfile, ", ");
      comma = 1;
      fprintf (outfile, "CRL is not up to date");
    }

  fprintf (outfile, "\n");
}


void
generate_pkcs8 (common_info_st * cinfo)
{
  gnutls_x509_privkey_t key;
  int result;
  size_t size;
  int flags = 0;
  const char *password;

  fprintf (stderr, "Generating a PKCS #8 key structure...\n");

  key = load_private_key (1, cinfo);

  if (info.pass)
    password = info.pass;
  else
    password = get_pass ();

  if (info.export)
    flags = GNUTLS_PKCS_USE_PKCS12_RC2_40;
  else
    flags = cipher_to_flags (info.pkcs_cipher);

  if (password == NULL || password[0] == 0)
    {
      flags = GNUTLS_PKCS_PLAIN;
    }

  size = buffer_size;
  result =
    gnutls_x509_privkey_export_pkcs8 (key, info.outcert_format,
                                      password, flags, buffer, &size);

  if (result < 0)
    error (EXIT_FAILURE, 0, "key_export: %s", gnutls_strerror (result));

  fwrite (buffer, 1, size, outfile);

}


#include <gnutls/pkcs12.h>
#include <unistd.h>

void
generate_pkcs12 (common_info_st * cinfo)
{
  gnutls_pkcs12_t pkcs12;
  gnutls_x509_crt_t *crts;
  gnutls_x509_privkey_t key;
  int result;
  size_t size;
  gnutls_datum_t data;
  const char *pass;
  const char *name;
  unsigned int flags, i;
  gnutls_datum_t key_id;
  unsigned char _key_id[20];
  int indx;
  size_t ncrts;

  fprintf (stderr, "Generating a PKCS #12 structure...\n");

  key = load_private_key (0, cinfo);
  crts = load_cert_list (0, &ncrts, cinfo);

  name = get_pkcs12_key_name ();

  result = gnutls_pkcs12_init (&pkcs12);
  if (result < 0)
    error (EXIT_FAILURE, 0, "pkcs12_init: %s", gnutls_strerror (result));

  if (info.pass)
    pass = info.pass;
  else
    pass = get_pass ();

  for (i = 0; i < ncrts; i++)
    {
      gnutls_pkcs12_bag_t bag;

      result = gnutls_pkcs12_bag_init (&bag);
      if (result < 0)
        error (EXIT_FAILURE, 0, "bag_init: %s", gnutls_strerror (result));

      result = gnutls_pkcs12_bag_set_crt (bag, crts[i]);
      if (result < 0)
        error (EXIT_FAILURE, 0, "set_crt[%d]: %s", i,
               gnutls_strerror (result));

      indx = result;

      result = gnutls_pkcs12_bag_set_friendly_name (bag, indx, name);
      if (result < 0)
        error (EXIT_FAILURE, 0, "bag_set_friendly_name: %s",
               gnutls_strerror (result));

      size = sizeof (_key_id);
      result = gnutls_x509_crt_get_key_id (crts[i], 0, _key_id, &size);
      if (result < 0)
        error (EXIT_FAILURE, 0, "key_id[%d]: %s", i,
               gnutls_strerror (result));

      key_id.data = _key_id;
      key_id.size = size;

      result = gnutls_pkcs12_bag_set_key_id (bag, indx, &key_id);
      if (result < 0)
        error (EXIT_FAILURE, 0, "bag_set_key_id: %s",
               gnutls_strerror (result));

      if (info.export)
        flags = GNUTLS_PKCS_USE_PKCS12_RC2_40;
      else
        flags = cipher_to_flags (info.pkcs_cipher);

      result = gnutls_pkcs12_bag_encrypt (bag, pass, flags);
      if (result < 0)
        error (EXIT_FAILURE, 0, "bag_encrypt: %s", gnutls_strerror (result));

      result = gnutls_pkcs12_set_bag (pkcs12, bag);
      if (result < 0)
        error (EXIT_FAILURE, 0, "set_bag: %s", gnutls_strerror (result));
    }

  if (key)
    {
      gnutls_pkcs12_bag_t kbag;

      result = gnutls_pkcs12_bag_init (&kbag);
      if (result < 0)
        error (EXIT_FAILURE, 0, "bag_init: %s", gnutls_strerror (result));

      if (info.export)
        flags = GNUTLS_PKCS_USE_PKCS12_RC2_40;
      else
        flags = cipher_to_flags (info.pkcs_cipher);

      size = buffer_size;
      result =
        gnutls_x509_privkey_export_pkcs8 (key, GNUTLS_X509_FMT_DER,
                                          pass, flags, buffer, &size);
      if (result < 0)
        error (EXIT_FAILURE, 0, "key_export: %s", gnutls_strerror (result));

      data.data = buffer;
      data.size = size;
      result =
        gnutls_pkcs12_bag_set_data (kbag,
                                    GNUTLS_BAG_PKCS8_ENCRYPTED_KEY, &data);
      if (result < 0)
        error (EXIT_FAILURE, 0, "bag_set_data: %s", gnutls_strerror (result));

      indx = result;

      result = gnutls_pkcs12_bag_set_friendly_name (kbag, indx, name);
      if (result < 0)
        error (EXIT_FAILURE, 0, "bag_set_friendly_name: %s",
               gnutls_strerror (result));

      size = sizeof (_key_id);
      result = gnutls_x509_privkey_get_key_id (key, 0, _key_id, &size);
      if (result < 0)
        error (EXIT_FAILURE, 0, "key_id: %s", gnutls_strerror (result));

      key_id.data = _key_id;
      key_id.size = size;

      result = gnutls_pkcs12_bag_set_key_id (kbag, indx, &key_id);
      if (result < 0)
        error (EXIT_FAILURE, 0, "bag_set_key_id: %s",
               gnutls_strerror (result));

      result = gnutls_pkcs12_set_bag (pkcs12, kbag);
      if (result < 0)
        error (EXIT_FAILURE, 0, "set_bag: %s", gnutls_strerror (result));
    }

  result = gnutls_pkcs12_generate_mac (pkcs12, pass);
  if (result < 0)
    error (EXIT_FAILURE, 0, "generate_mac: %s", gnutls_strerror (result));

  size = buffer_size;
  result = gnutls_pkcs12_export (pkcs12, info.outcert_format, buffer, &size);
  if (result < 0)
    error (EXIT_FAILURE, 0, "pkcs12_export: %s", gnutls_strerror (result));

  fwrite (buffer, 1, size, outfile);

}

static const char *
BAGTYPE (gnutls_pkcs12_bag_type_t x)
{
  switch (x)
    {
    case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:
      return "PKCS #8 Encrypted key";
    case GNUTLS_BAG_EMPTY:
      return "Empty";
    case GNUTLS_BAG_PKCS8_KEY:
      return "PKCS #8 Key";
    case GNUTLS_BAG_CERTIFICATE:
      return "Certificate";
    case GNUTLS_BAG_ENCRYPTED:
      return "Encrypted";
    case GNUTLS_BAG_CRL:
      return "CRL";
    case GNUTLS_BAG_SECRET:
      return "Secret";
    default:
      return "Unknown";
    }
}

static void
print_bag_data (gnutls_pkcs12_bag_t bag)
{
  int result;
  int count, i, type;
  gnutls_datum_t cdata, id;
  const char *str, *name;
  gnutls_datum_t out;

  count = gnutls_pkcs12_bag_get_count (bag);
  if (count < 0)
    error (EXIT_FAILURE, 0, "get_count: %s", gnutls_strerror (count));

  fprintf (outfile, "\tElements: %d\n", count);

  for (i = 0; i < count; i++)
    {
      type = gnutls_pkcs12_bag_get_type (bag, i);
      if (type < 0)
        error (EXIT_FAILURE, 0, "get_type: %s", gnutls_strerror (type));

      fprintf (stderr, "\tType: %s\n", BAGTYPE (type));

      name = NULL;
      result = gnutls_pkcs12_bag_get_friendly_name (bag, i, (char **) &name);
      if (result < 0)
        error (EXIT_FAILURE, 0, "get_friendly_name: %s",
               gnutls_strerror (type));
      if (name)
        fprintf (outfile, "\tFriendly name: %s\n", name);

      id.data = NULL;
      id.size = 0;
      result = gnutls_pkcs12_bag_get_key_id (bag, i, &id);
      if (result < 0)
        error (EXIT_FAILURE, 0, "get_key_id: %s", gnutls_strerror (type));
      fprintf (outfile, "\tKey ID: %s\n", raw_to_string (id.data, id.size));

      result = gnutls_pkcs12_bag_get_data (bag, i, &cdata);
      if (result < 0)
        error (EXIT_FAILURE, 0, "get_data: %s", gnutls_strerror (result));

      switch (type)
        {
        case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:
          str = "ENCRYPTED PRIVATE KEY";
          break;
        case GNUTLS_BAG_PKCS8_KEY:
          str = "PRIVATE KEY";
          break;
        case GNUTLS_BAG_CERTIFICATE:
          str = "CERTIFICATE";
          break;
        case GNUTLS_BAG_CRL:
          str = "CRL";
          break;
        case GNUTLS_BAG_ENCRYPTED:
        case GNUTLS_BAG_EMPTY:
        default:
          str = NULL;
        }

      if (str != NULL)
        {
          gnutls_pem_base64_encode_alloc (str, &cdata, &out);
          fprintf (outfile, "%s\n", out.data);

          gnutls_free (out.data);
        }

    }
}

void
pkcs12_info (void)
{
  gnutls_pkcs12_t pkcs12;
  gnutls_pkcs12_bag_t bag;
  int result;
  size_t size;
  gnutls_datum_t data;
  const char *pass;
  int indx;

  result = gnutls_pkcs12_init (&pkcs12);
  if (result < 0)
    error (EXIT_FAILURE, 0, "p12_init: %s", gnutls_strerror (result));

  data.data = fread_file (infile, &size);
  data.size = size;

  result = gnutls_pkcs12_import (pkcs12, &data, info.incert_format, 0);
  free (data.data);
  if (result < 0)
    error (EXIT_FAILURE, 0, "p12_import: %s", gnutls_strerror (result));

  if (info.pass)
    pass = info.pass;
  else
    pass = get_pass ();

  result = gnutls_pkcs12_verify_mac (pkcs12, pass);
  if (result < 0)
    error (0, 0, "verify_mac: %s", gnutls_strerror (result));

  for (indx = 0;; indx++)
    {
      result = gnutls_pkcs12_bag_init (&bag);
      if (result < 0)
        error (EXIT_FAILURE, 0, "bag_init: %s", gnutls_strerror (result));

      result = gnutls_pkcs12_get_bag (pkcs12, indx, bag);
      if (result < 0)
        break;

      result = gnutls_pkcs12_bag_get_count (bag);
      if (result < 0)
        error (EXIT_FAILURE, 0, "bag_count: %s", gnutls_strerror (result));

      fprintf (outfile, "BAG #%d\n", indx);

      result = gnutls_pkcs12_bag_get_type (bag, 0);
      if (result < 0)
        error (EXIT_FAILURE, 0, "bag_init: %s", gnutls_strerror (result));

      if (result == GNUTLS_BAG_ENCRYPTED)
        {
          fprintf (stderr, "\tType: %s\n", BAGTYPE (result));
          fprintf (stderr, "\n\tDecrypting...\n");

          result = gnutls_pkcs12_bag_decrypt (bag, pass);

          if (result < 0)
            {
              error (0, 0, "bag_decrypt: %s", gnutls_strerror (result));
              continue;
            }

          result = gnutls_pkcs12_bag_get_count (bag);
          if (result < 0)
            error (EXIT_FAILURE, 0, "encrypted bag_count: %s",
                   gnutls_strerror (result));
        }

      print_bag_data (bag);

      gnutls_pkcs12_bag_deinit (bag);
    }
}

void
pkcs7_info (void)
{
  gnutls_pkcs7_t pkcs7;
  int result;
  size_t size;
  gnutls_datum_t data, b64;
  int indx, count;

  result = gnutls_pkcs7_init (&pkcs7);
  if (result < 0)
    error (EXIT_FAILURE, 0, "p7_init: %s", gnutls_strerror (result));

  data.data = fread_file (infile, &size);
  data.size = size;

  result = gnutls_pkcs7_import (pkcs7, &data, info.incert_format);
  free (data.data);
  if (result < 0)
    error (EXIT_FAILURE, 0, "import error: %s", gnutls_strerror (result));

  /* Read and print the certificates.
   */
  result = gnutls_pkcs7_get_crt_count (pkcs7);
  if (result < 0)
    error (EXIT_FAILURE, 0, "p7_crt_count: %s", gnutls_strerror (result));

  count = result;

  if (count > 0)
    fprintf (outfile, "Number of certificates: %u\n", count);

  for (indx = 0; indx < count; indx++)
    {
      fputs ("\n", outfile);

      size = buffer_size;
      result = gnutls_pkcs7_get_crt_raw (pkcs7, indx, buffer, &size);
      if (result < 0)
        break;

      data.data = buffer;
      data.size = size;

      result = gnutls_pem_base64_encode_alloc ("CERTIFICATE", &data, &b64);
      if (result < 0)
        error (EXIT_FAILURE, 0, "encoding: %s", gnutls_strerror (result));

      fputs (b64.data, outfile);
      gnutls_free (b64.data);
    }

  /* Read the CRLs now.
   */
  result = gnutls_pkcs7_get_crl_count (pkcs7);
  if (result < 0)
    error (EXIT_FAILURE, 0, "p7_crl_count: %s", gnutls_strerror (result));

  count = result;

  if (count > 0)
    fprintf (outfile, "\nNumber of CRLs: %u\n", count);

  for (indx = 0; indx < count; indx++)
    {
      fputs ("\n", outfile);

      size = buffer_size;
      result = gnutls_pkcs7_get_crl_raw (pkcs7, indx, buffer, &size);
      if (result < 0)
        break;

      data.data = buffer;
      data.size = size;

      result = gnutls_pem_base64_encode_alloc ("X509 CRL", &data, &b64);
      if (result < 0)
        error (EXIT_FAILURE, 0, "encoding: %s", gnutls_strerror (result));

      fputs (b64.data, outfile);
      gnutls_free (b64.data);
    }
}

void
smime_to_pkcs7 (void)
{
  size_t linesize = 0;
  char *lineptr = NULL;
  ssize_t len;

  /* Find body.  FIXME: Handle non-b64 Content-Transfer-Encoding.
     Reject non-S/MIME tagged Content-Type's? */
  do
    {
      len = getline (&lineptr, &linesize, infile);
      if (len == -1)
        error (EXIT_FAILURE, 0, "cannot find RFC 2822 header/body separator");
    }
  while (strcmp (lineptr, "\r\n") != 0 && strcmp (lineptr, "\n") != 0);

  do
    {
      len = getline (&lineptr, &linesize, infile);
      if (len == -1)
        error (EXIT_FAILURE, 0, "message has RFC 2822 header but no body");
    }
  while (strcmp (lineptr, "\r\n") == 0 && strcmp (lineptr, "\n") == 0);

  fprintf (outfile, "%s", "-----BEGIN PKCS7-----\n");

  do
    {
      while (len > 0
             && (lineptr[len - 1] == '\r' || lineptr[len - 1] == '\n'))
        lineptr[--len] = '\0';
      if (strcmp (lineptr, "") != 0)
        fprintf (outfile, "%s\n", lineptr);
      len = getline (&lineptr, &linesize, infile);
    }
  while (len != -1);

  fprintf (outfile, "%s", "-----END PKCS7-----\n");

  free (lineptr);
}

void
certtool_version (void)
{
  const char *p = PACKAGE_NAME;
  if (strcmp (gnutls_check_version (NULL), PACKAGE_VERSION) != 0)
    p = PACKAGE_STRING;
  version_etc (stdout, program_name, p, gnutls_check_version (NULL),
               "Nikos Mavrogiannopoulos", "Simon Josefsson", (char *) NULL);
}

static void
print_key_usage (FILE * outfile, unsigned int usage)
{
  if (usage & GNUTLS_KEY_DIGITAL_SIGNATURE)
    {
      fprintf (outfile, "\tDigital signature.\n");
    }

  if (usage & GNUTLS_KEY_NON_REPUDIATION)
    {
      fprintf (outfile, "\tNon repudiation.\n");
    }

  if (usage & GNUTLS_KEY_KEY_ENCIPHERMENT)
    {
      fprintf (outfile, "\tKey encipherment.\n");
    }

  if (usage & GNUTLS_KEY_DATA_ENCIPHERMENT)
    {
      fprintf (outfile, "\tData encipherment.\n");
    }

  if (usage & GNUTLS_KEY_KEY_AGREEMENT)
    {
      fprintf (outfile, "\tKey agreement.\n");
    }

  if (usage & GNUTLS_KEY_KEY_CERT_SIGN)
    {
      fprintf (outfile, "\tCertificate signing.\n");
    }

  if (usage & GNUTLS_KEY_NON_REPUDIATION)
    {
      fprintf (outfile, "\tCRL signing.\n");
    }

  if (usage & GNUTLS_KEY_ENCIPHER_ONLY)
    {
      fprintf (outfile, "\tKey encipher only.\n");
    }

  if (usage & GNUTLS_KEY_DECIPHER_ONLY)
    {
      fprintf (outfile, "\tKey decipher only.\n");
    }
}

void
pubkey_info (gnutls_x509_crt crt, common_info_st * cinfo)
{
  gnutls_pubkey_t pubkey;
  unsigned int bits, usage;
  int ret;
  size_t size;
  const char *cprint;

  ret = gnutls_pubkey_init (&pubkey);
  if (ret < 0)
    {
      error (EXIT_FAILURE, 0, "pubkey_init: %s", gnutls_strerror (ret));
    }

  if (crt == NULL)
    {
      crt = load_cert (0, cinfo);
    }

  if (crt != NULL)
    {
      ret = gnutls_pubkey_import_x509 (pubkey, crt, 0);
      if (ret < 0)
        {
          error (EXIT_FAILURE, 0, "pubkey_import_x509: %s",
                 gnutls_strerror (ret));
        }
    }
  else
    {
      pubkey = load_pubkey (1, cinfo);
    }

  fprintf (outfile, "Public Key Info:\n\n");
  ret = gnutls_pubkey_get_pk_algorithm (pubkey, &bits);
  fprintf (outfile, "Public Key Algorithm: ");

  cprint = gnutls_pk_algorithm_get_name (ret);
  fprintf (outfile, "%s (%u bits)\n", cprint ? cprint : "Unknown", bits);


  /* Print the raw public and private keys
   */
  if (ret == GNUTLS_PK_RSA)
    {
      gnutls_datum_t m, e;

      ret = gnutls_pubkey_get_pk_rsa_raw (pubkey, &m, &e);
      if (ret < 0)
        fprintf (stderr, "Error in key RSA data export: %s\n",
                 gnutls_strerror (ret));
      else
        {
          print_rsa_pkey (&m, &e, NULL, NULL, NULL, NULL, NULL, NULL);
          gnutls_free (m.data);
          gnutls_free (e.data);
        }
    }
  else if (ret == GNUTLS_PK_DSA)
    {
      gnutls_datum_t p, q, g, y;

      ret = gnutls_pubkey_get_pk_dsa_raw (pubkey, &p, &q, &g, &y);
      if (ret < 0)
        fprintf (stderr, "Error in key DSA data export: %s\n",
                 gnutls_strerror (ret));
      else
        {
          print_dsa_pkey (NULL, &y, &p, &q, &g);
          gnutls_free (y.data);
          gnutls_free (p.data);
          gnutls_free (q.data);
          gnutls_free (g.data);
        }
    }

  ret = gnutls_pubkey_get_key_usage (pubkey, &usage);
  if (ret < 0)
    {
      error (EXIT_FAILURE, 0, "pubkey_get_key_usage: %s",
             gnutls_strerror (ret));
    }

  fprintf (outfile, "Public Key Usage:\n");
  print_key_usage (outfile, usage);

  fprintf (outfile, "\n");

  size = buffer_size;
  if ((ret = gnutls_pubkey_get_key_id (pubkey, 0, buffer, &size)) < 0)
    {
      fprintf (stderr, "Error in key id calculation: %s\n",
               gnutls_strerror (ret));
    }
  else
    {
      fprintf (outfile, "Public Key ID: %s\n", raw_to_string (buffer, size));
    }

  size = buffer_size;
  ret = gnutls_pubkey_export (pubkey, GNUTLS_X509_FMT_PEM, buffer, &size);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "export error: %s", gnutls_strerror (ret));

  fprintf (outfile, "\n%s\n", buffer);

  gnutls_pubkey_deinit (pubkey);
}