File: parse-packet.c

package info (click to toggle)
gnupg 1.4.18-7+deb8u5
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 34,476 kB
  • sloc: ansic: 127,074; sh: 7,535; asm: 4,610; makefile: 1,186; yacc: 291; perl: 196; pascal: 72; sed: 16
file content (2538 lines) | stat: -rw-r--r-- 70,216 bytes parent folder | download | duplicates (2)
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
/* parse-packet.c  - read packets
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
 *               2008 Free Software Foundation, Inc.
 *
 * This file is part of GnuPG.
 *
 * GnuPG 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.
 *
 * GnuPG 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "packet.h"
#include "iobuf.h"
#include "mpi.h"
#include "util.h"
#include "cipher.h"
#include "memory.h"
#include "filter.h"
#include "photoid.h"
#include "options.h"
#include "main.h"
#include "i18n.h"
#include "host2net.h"

#ifndef MAX_EXTERN_MPI_BITS
#define MAX_EXTERN_MPI_BITS 16384
#endif

/* Maximum length of packets to avoid excessive memory allocation.  */
#define MAX_KEY_PACKET_LENGTH     (256 * 1024)
#define MAX_UID_PACKET_LENGTH     (  2 * 1024)
#define MAX_COMMENT_PACKET_LENGTH ( 64 * 1024)
#define MAX_ATTR_PACKET_LENGTH    ( 16 * 1024*1024)


static int mpi_print_mode;
static int list_mode;
static FILE *listfp;

static int  parse( IOBUF inp, PACKET *pkt, int onlykeypkts,
                  off_t *retpos, int *skip, IOBUF out, int do_skip
#ifdef DEBUG_PARSE_PACKET
		   ,const char *dbg_w, const char *dbg_f, int dbg_l
#endif
		 );
static int  copy_packet( IOBUF inp, IOBUF out, int pkttype,
			 unsigned long pktlen, int partial );
static void skip_packet( IOBUF inp, int pkttype,
			 unsigned long pktlen, int partial );
static void *read_rest( IOBUF inp, size_t pktlen, int partial );
static int  parse_marker( IOBUF inp, int pkttype, unsigned long pktlen );
static int  parse_symkeyenc( IOBUF inp, int pkttype, unsigned long pktlen,
							     PACKET *packet );
static int  parse_pubkeyenc( IOBUF inp, int pkttype, unsigned long pktlen,
							     PACKET *packet );
static int  parse_onepass_sig( IOBUF inp, int pkttype, unsigned long pktlen,
							PKT_onepass_sig *ops );
static int  parse_key( IOBUF inp, int pkttype, unsigned long pktlen,
				      byte *hdr, int hdrlen, PACKET *packet );
static int  parse_user_id( IOBUF inp, int pkttype, unsigned long pktlen,
							   PACKET *packet );
static int  parse_attribute( IOBUF inp, int pkttype, unsigned long pktlen,
							   PACKET *packet );
static int  parse_comment( IOBUF inp, int pkttype, unsigned long pktlen,
							   PACKET *packet );
static void parse_trust( IOBUF inp, int pkttype, unsigned long pktlen,
							   PACKET *packet );
static int  parse_plaintext( IOBUF inp, int pkttype, unsigned long pktlen,
			     PACKET *packet, int new_ctb, int partial);
static int  parse_compressed( IOBUF inp, int pkttype, unsigned long pktlen,
					       PACKET *packet, int new_ctb );
static int  parse_encrypted( IOBUF inp, int pkttype, unsigned long pktlen,
			     PACKET *packet, int new_ctb, int partial);
static int  parse_mdc( IOBUF inp, int pkttype, unsigned long pktlen,
					       PACKET *packet, int new_ctb);
static int  parse_gpg_control( IOBUF inp, int pkttype, unsigned long pktlen,
                               PACKET *packet, int partial );

static unsigned short
read_16(IOBUF inp)
{
    unsigned short a;
    a = (unsigned short)iobuf_get_noeof(inp) << 8;
    a |= iobuf_get_noeof(inp);
    return a;
}

static unsigned long
read_32(IOBUF inp)
{
    unsigned long a;
    a =  (unsigned long)iobuf_get_noeof(inp) << 24;
    a |= iobuf_get_noeof(inp) << 16;
    a |= iobuf_get_noeof(inp) << 8;
    a |= iobuf_get_noeof(inp);
    return a;
}


int
set_packet_list_mode( int mode )
{
    int old = list_mode;
    list_mode = mode;
    mpi_print_mode = DBG_MPI;
    /* We use stdout print only if invoked by the --list-packets
       command but switch to stderr in all otehr cases.  This breaks
       the previous behaviour but that seems to be more of a bug than
       intentional.  I don't believe that any application makes use of
       this long standing annoying way of printing to stdout except
       when doing a --list-packets. If this assumption fails, it will
       be easy to add an option for the listing stream.  Note that we
       initialize it only once; mainly because some code may switch
       the option value later back to 1 and we want to have all output
       to the same stream.

       Using stderr is not actually very clean because it bypasses the
       logging code but it is a special thing anyay.  I am not sure
       whether using log_stream() would be better.  Perhaps we should
       enable the list mdoe only with a special option. */
    if (!listfp)
        listfp = opt.list_packets == 2 ? stdout : stderr;
    return old;
}

static void
unknown_pubkey_warning( int algo )
{
    static byte unknown_pubkey_algos[256];

    algo &= 0xff;
    if( !unknown_pubkey_algos[algo] ) {
	if( opt.verbose )
	    log_info(_("can't handle public key algorithm %d\n"), algo );
	unknown_pubkey_algos[algo] = 1;
    }
}

/****************
 * Parse a Packet and return it in packet
 * Returns: 0 := valid packet in pkt
 *	   -1 := no more packets
 *	   >0 := error
 * Note: The function may return an error and a partly valid packet;
 * caller must free this packet.
 */
#ifdef DEBUG_PARSE_PACKET
int
dbg_parse_packet( IOBUF inp, PACKET *pkt, const char *dbg_f, int dbg_l )
{
    int skip, rc;

    do {
	rc = parse( inp, pkt, 0, NULL, &skip, NULL, 0, "parse", dbg_f, dbg_l );
    } while( skip );
    return rc;
}
#else
int
parse_packet( IOBUF inp, PACKET *pkt )
{
    int skip, rc;

    do {
	rc = parse( inp, pkt, 0, NULL, &skip, NULL, 0 );
    } while( skip );
    return rc;
}
#endif

/****************
 * Like parse packet, but only return secret or public (sub)key packets.
 */
#ifdef DEBUG_PARSE_PACKET
int
dbg_search_packet( IOBUF inp, PACKET *pkt, off_t *retpos, int with_uid,
		   const char *dbg_f, int dbg_l )
{
    int skip, rc;

    do {
	rc = parse( inp, pkt, with_uid?2:1, retpos, &skip, NULL, 0, "search", dbg_f, dbg_l );
    } while( skip );
    return rc;
}
#else
int
search_packet( IOBUF inp, PACKET *pkt, off_t *retpos, int with_uid )
{
    int skip, rc;

    do {
	rc = parse( inp, pkt, with_uid?2:1, retpos, &skip, NULL, 0 );
    } while( skip );
    return rc;
}
#endif

/****************
 * Copy all packets from INP to OUT, thereby removing unused spaces.
 */
#ifdef DEBUG_PARSE_PACKET
int
dbg_copy_all_packets( IOBUF inp, IOBUF out,
		   const char *dbg_f, int dbg_l )
{
    PACKET pkt;
    int skip, rc=0;
    do {
	init_packet(&pkt);
    } while( !(rc = parse( inp, &pkt, 0, NULL, &skip, out, 0, "copy", dbg_f, dbg_l )));
    return rc;
}
#else
int
copy_all_packets( IOBUF inp, IOBUF out )
{
    PACKET pkt;
    int skip, rc=0;
    do {
	init_packet(&pkt);
    } while( !(rc = parse( inp, &pkt, 0, NULL, &skip, out, 0 )));
    return rc;
}
#endif

/****************
 * Copy some packets from INP to OUT, thereby removing unused spaces.
 * Stop at offset STOPoff (i.e. don't copy packets at this or later offsets)
 */
#ifdef DEBUG_PARSE_PACKET
int
dbg_copy_some_packets( IOBUF inp, IOBUF out, off_t stopoff,
		   const char *dbg_f, int dbg_l )
{
    PACKET pkt;
    int skip, rc=0;
    do {
	if( iobuf_tell(inp) >= stopoff )
	    return 0;
	init_packet(&pkt);
    } while( !(rc = parse( inp, &pkt, 0, NULL, &skip, out, 0,
				     "some", dbg_f, dbg_l )) );
    return rc;
}
#else
int
copy_some_packets( IOBUF inp, IOBUF out, off_t stopoff )
{
    PACKET pkt;
    int skip, rc=0;
    do {
	if( iobuf_tell(inp) >= stopoff )
	    return 0;
	init_packet(&pkt);
    } while( !(rc = parse( inp, &pkt, 0, NULL, &skip, out, 0 )) );
    return rc;
}
#endif

/****************
 * Skip over N packets
 */
#ifdef DEBUG_PARSE_PACKET
int
dbg_skip_some_packets( IOBUF inp, unsigned n,
		   const char *dbg_f, int dbg_l )
{
    int skip, rc=0;
    PACKET pkt;

    for( ;n && !rc; n--) {
	init_packet(&pkt);
	rc = parse( inp, &pkt, 0, NULL, &skip, NULL, 1, "skip", dbg_f, dbg_l );
    }
    return rc;
}
#else
int
skip_some_packets( IOBUF inp, unsigned n )
{
    int skip, rc=0;
    PACKET pkt;

    for( ;n && !rc; n--) {
	init_packet(&pkt);
	rc = parse( inp, &pkt, 0, NULL, &skip, NULL, 1 );
    }
    return rc;
}
#endif


/****************
 * Parse packet. Set the variable skip points to 1 if the packet
 * should be skipped; this is the case if either ONLYKEYPKTS is set
 * and the parsed packet isn't one or the
 * packet-type is 0, indicating deleted stuff.
 * if OUT is not NULL, a special copymode is used.
 */
static int
parse( IOBUF inp, PACKET *pkt, int onlykeypkts, off_t *retpos,
       int *skip, IOBUF out, int do_skip
#ifdef DEBUG_PARSE_PACKET
       ,const char *dbg_w, const char *dbg_f, int dbg_l
#endif
     )
{
    int rc=0, c, ctb, pkttype, lenbytes;
    unsigned long pktlen;
    byte hdr[8];
    int hdrlen;
    int new_ctb = 0, partial=0;
    int with_uid = (onlykeypkts == 2);

    *skip = 0;
    assert( !pkt->pkt.generic );
    if( retpos )
	*retpos = iobuf_tell(inp);

    if( (ctb = iobuf_get(inp)) == -1 ) {
	rc = -1;
	goto leave;
    }
    hdrlen=0;
    hdr[hdrlen++] = ctb;
    if( !(ctb & 0x80) ) {
        log_error("%s: invalid packet (ctb=%02x)\n", iobuf_where(inp), ctb );
	rc = G10ERR_INVALID_PACKET;
	goto leave;
    }
    pktlen = 0;
    new_ctb = !!(ctb & 0x40);
    if( new_ctb ) {
        pkttype = ctb & 0x3f;
	if( (c = iobuf_get(inp)) == -1 ) {
	    log_error("%s: 1st length byte missing\n", iobuf_where(inp) );
	    rc = G10ERR_INVALID_PACKET;
	    goto leave;
	}
/* The follwing code has been here for ages (2002-08-30) but it is
   clearly wrong: For example passing a 0 as second argument to
   iobuf_set_partial_block_mode stops the partial block mode which we
   definitely do not want.  Also all values < 224 or 255 are not
   valid.  Let's disable it and put PKT_COMPRESSED into the list of
   allowed packets with partial header until someone complains. */
/*         if (pkttype == PKT_COMPRESSED) { */
/*              iobuf_set_partial_block_mode(inp, c & 0xff); */
/*              pktlen = 0;/\* to indicate partial length *\/ */
/* 	     partial=1; */
/*         } */
/*         else { */
        {
             hdr[hdrlen++] = c;
             if( c < 192 )
	       pktlen = c;
             else if( c < 224 )
	       {
		 pktlen = (c - 192) * 256;
		 if( (c = iobuf_get(inp)) == -1 )
		   {
		     log_error("%s: 2nd length byte missing\n",
			       iobuf_where(inp) );
		     rc = G10ERR_INVALID_PACKET;
		     goto leave;
		   }
		 hdr[hdrlen++] = c;
		 pktlen += c + 192;
	       }
             else if( c == 255 )
	       {
		 pktlen  =
                   (unsigned long)(hdr[hdrlen++] = iobuf_get_noeof(inp)) << 24;
		 pktlen |= (hdr[hdrlen++] = iobuf_get_noeof(inp)) << 16;
		 pktlen |= (hdr[hdrlen++] = iobuf_get_noeof(inp)) << 8;
		 if( (c = iobuf_get(inp)) == -1 )
		   {
		     log_error("%s: 4 byte length invalid\n",
			       iobuf_where(inp) );
		     rc = G10ERR_INVALID_PACKET;
		     goto leave;
		   }
		 pktlen |= (hdr[hdrlen++] = c );
	       }
             else
	       {
		 /* Partial body length.  */
		 switch (pkttype)
                   {
                   case PKT_PLAINTEXT:
                   case PKT_ENCRYPTED:
                   case PKT_ENCRYPTED_MDC:
                   case PKT_COMPRESSED:
		     iobuf_set_partial_block_mode (inp, (c & 0xff));
		     pktlen = 0; /* Indicate partial length.  */
		     partial= 1;
		     break;

                   default:
		     log_error ("%s: partial length for invalid"
                                " packet type %d\n", iobuf_where(inp),pkttype);
		     rc = G10ERR_INVALID_PACKET;
		     goto leave;
		   }
	       }
	}
    }
    else
      {
	pkttype = (ctb>>2)&0xf;
	lenbytes = ((ctb&3)==3)? 0 : (1<<(ctb & 3));
	if( !lenbytes )
	  {
	    pktlen = 0; /* don't know the value */
	    /* This isn't really partial, but we can treat it the same
	       in a "read until the end" sort of way. */
	    partial=1;
	    if(pkttype!=PKT_ENCRYPTED && pkttype!=PKT_PLAINTEXT
	       && pkttype!=PKT_COMPRESSED)
	      {
		log_error ("%s: indeterminate length for invalid"
			   " packet type %d\n", iobuf_where(inp), pkttype );
		rc = G10ERR_INVALID_PACKET;
		goto leave;
	      }
	  }
	else
	  {
	    for( ; lenbytes; lenbytes-- )
	      {
		pktlen <<= 8;
		pktlen |= hdr[hdrlen++] = iobuf_get_noeof(inp);
	      }
	  }
      }

    if (pktlen == (unsigned long)(-1)) {
        /* With some probability this is caused by a problem in the
         * the uncompressing layer - in some error cases it just loops
         * and spits out 0xff bytes. */
        log_error ("%s: garbled packet detected\n", iobuf_where(inp) );
	g10_exit (2);
    }

    if( out && pkttype	) {
	if( iobuf_write( out, hdr, hdrlen ) == -1 )
	    rc = G10ERR_WRITE_FILE;
	else
	    rc = copy_packet(inp, out, pkttype, pktlen, partial );
	goto leave;
    }

    if (with_uid && pkttype == PKT_USER_ID)
        ;
    else if( do_skip
        || !pkttype
        || (onlykeypkts && pkttype != PKT_PUBLIC_SUBKEY
                        && pkttype != PKT_PUBLIC_KEY
                        && pkttype != PKT_SECRET_SUBKEY
                        && pkttype != PKT_SECRET_KEY  ) ) {
	iobuf_skip_rest(inp, pktlen, partial);
	*skip = 1;
	rc = 0;
	goto leave;
    }

    if( DBG_PACKET ) {
#ifdef DEBUG_PARSE_PACKET
	log_debug("parse_packet(iob=%d): type=%d length=%lu%s (%s.%s.%d)\n",
		   iobuf_id(inp), pkttype, pktlen, new_ctb?" (new_ctb)":"",
		    dbg_w, dbg_f, dbg_l );
#else
	log_debug("parse_packet(iob=%d): type=%d length=%lu%s\n",
		   iobuf_id(inp), pkttype, pktlen, new_ctb?" (new_ctb)":"" );
#endif
    }
    pkt->pkttype = pkttype;
    rc = G10ERR_UNKNOWN_PACKET; /* default error */
    switch( pkttype ) {
      case PKT_PUBLIC_KEY:
      case PKT_PUBLIC_SUBKEY:
	pkt->pkt.public_key = xmalloc_clear(sizeof *pkt->pkt.public_key );
	rc = parse_key(inp, pkttype, pktlen, hdr, hdrlen, pkt );
	break;
      case PKT_SECRET_KEY:
      case PKT_SECRET_SUBKEY:
	pkt->pkt.secret_key = xmalloc_clear(sizeof *pkt->pkt.secret_key );
	rc = parse_key(inp, pkttype, pktlen, hdr, hdrlen, pkt );
	break;
      case PKT_SYMKEY_ENC:
	rc = parse_symkeyenc( inp, pkttype, pktlen, pkt );
	break;
      case PKT_PUBKEY_ENC:
	rc = parse_pubkeyenc(inp, pkttype, pktlen, pkt );
	break;
      case PKT_SIGNATURE:
	pkt->pkt.signature = xmalloc_clear(sizeof *pkt->pkt.signature );
	rc = parse_signature(inp, pkttype, pktlen, pkt->pkt.signature );
	break;
      case PKT_ONEPASS_SIG:
	pkt->pkt.onepass_sig = xmalloc_clear(sizeof *pkt->pkt.onepass_sig );
	rc = parse_onepass_sig(inp, pkttype, pktlen, pkt->pkt.onepass_sig );
	break;
      case PKT_USER_ID:
	rc = parse_user_id(inp, pkttype, pktlen, pkt );
	break;
      case PKT_ATTRIBUTE:
	pkt->pkttype = pkttype = PKT_USER_ID;  /* we store it in the userID */
	rc = parse_attribute(inp, pkttype, pktlen, pkt);
	break;
      case PKT_OLD_COMMENT:
      case PKT_COMMENT:
	rc = parse_comment(inp, pkttype, pktlen, pkt);
	break;
      case PKT_RING_TRUST:
	parse_trust(inp, pkttype, pktlen, pkt);
	rc = 0;
	break;
      case PKT_PLAINTEXT:
	rc = parse_plaintext(inp, pkttype, pktlen, pkt, new_ctb, partial );
	break;
      case PKT_COMPRESSED:
	rc = parse_compressed(inp, pkttype, pktlen, pkt, new_ctb );
	break;
      case PKT_ENCRYPTED:
      case PKT_ENCRYPTED_MDC:
	rc = parse_encrypted(inp, pkttype, pktlen, pkt, new_ctb, partial );
	break;
      case PKT_MDC:
	rc = parse_mdc(inp, pkttype, pktlen, pkt, new_ctb );
	break;
      case PKT_GPG_CONTROL:
        rc = parse_gpg_control(inp, pkttype, pktlen, pkt, partial );
        break;
    case PKT_MARKER:
        rc = parse_marker(inp,pkttype,pktlen);
	break;
      default:
	skip_packet(inp, pkttype, pktlen, partial);
	break;
    }

  leave:
    if( !rc && iobuf_error(inp) )
	rc = G10ERR_INV_KEYRING;
    return rc;
}

static void
dump_hex_line( int c, int *i )
{
    if( *i && !(*i%8) ) {
	if( *i && !(*i%24) )
	    fprintf (listfp, "\n%4d:", *i );
	else
	    putc (' ', listfp);
    }
    if( c == -1 )
	fprintf (listfp, " EOF" );
    else
	fprintf (listfp, " %02x", c );
    ++*i;
}


static int
copy_packet( IOBUF inp, IOBUF out, int pkttype,
	     unsigned long pktlen, int partial )
{
    int n;
    char buf[100];

    if( partial ) {
	while( (n = iobuf_read( inp, buf, 100 )) != -1 )
	    if( iobuf_write(out, buf, n ) )
		return G10ERR_WRITE_FILE; /* write error */
    }
    else if( !pktlen && pkttype == PKT_COMPRESSED ) {
	log_debug("copy_packet: compressed!\n");
	/* compressed packet, copy till EOF */
	while( (n = iobuf_read( inp, buf, 100 )) != -1 )
	    if( iobuf_write(out, buf, n ) )
		return G10ERR_WRITE_FILE; /* write error */
    }
    else {
	for( ; pktlen; pktlen -= n ) {
	    n = pktlen > 100 ? 100 : pktlen;
	    n = iobuf_read( inp, buf, n );
	    if( n == -1 )
		return G10ERR_READ_FILE;
	    if( iobuf_write(out, buf, n ) )
		return G10ERR_WRITE_FILE; /* write error */
	}
    }
    return 0;
}


static void
skip_packet( IOBUF inp, int pkttype, unsigned long pktlen, int partial )
{
  if( list_mode )
    {
      fprintf (listfp, ":unknown packet: type %2d, length %lu\n",
	       pkttype, pktlen);
      if( pkttype )
	{
	  int c, i=0 ;
	  fputs("dump:", listfp );
	  if( partial )
	    {
	      while( (c=iobuf_get(inp)) != -1 )
		dump_hex_line(c, &i);
	    }
	  else
	    {
	      for( ; pktlen; pktlen-- )
                {
                  dump_hex_line ((c=iobuf_get(inp)), &i);
                  if (c==-1)
                    break;
                }
	    }
	  putc ('\n', listfp);
	  return;
	}
    }
  iobuf_skip_rest(inp,pktlen,partial);
}

static void *
read_rest( IOBUF inp, size_t pktlen, int partial )
{
    byte *p;
    int i;

    if( partial ) {
	log_error("read_rest: can't store stream data\n");
	p = NULL;
    }
    else {
	p = xmalloc( pktlen );
	for(i=0; pktlen; pktlen--, i++ )
	    p[i] = iobuf_get(inp);
    }
    return p;
}

static int
parse_marker( IOBUF inp, int pkttype, unsigned long pktlen )
{
  if(pktlen!=3)
    goto fail;

  if(iobuf_get(inp)!='P')
    {
      pktlen--;
      goto fail;
    }

  if(iobuf_get(inp)!='G')
    {
      pktlen--;
      goto fail;
    }

  if(iobuf_get(inp)!='P')
    {
      pktlen--;
      goto fail;
    }

  if(list_mode)
    fputs(":marker packet: PGP\n", listfp );

  return 0;

 fail:
  log_error("invalid marker packet\n");
  iobuf_skip_rest(inp,pktlen,0);
  return G10ERR_INVALID_PACKET;
}

static int
parse_symkeyenc( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
{
    PKT_symkey_enc *k;
    int rc = 0;
    int i, version, s2kmode, cipher_algo, hash_algo, seskeylen, minlen;

    if( pktlen < 4 ) {
	log_error("packet(%d) too short\n", pkttype);
        rc = G10ERR_INVALID_PACKET;
	goto leave;
    }
    version = iobuf_get_noeof(inp); pktlen--;
    if( version != 4 ) {
	log_error("packet(%d) with unknown version %d\n", pkttype, version);
        rc = G10ERR_INVALID_PACKET;
	goto leave;
    }
    if( pktlen > 200 ) { /* (we encode the seskeylen in a byte) */
	log_error("packet(%d) too large\n", pkttype);
        rc = G10ERR_INVALID_PACKET;
	goto leave;
    }
    cipher_algo = iobuf_get_noeof(inp); pktlen--;
    s2kmode = iobuf_get_noeof(inp); pktlen--;
    hash_algo = iobuf_get_noeof(inp); pktlen--;
    switch( s2kmode ) {
      case 0:  /* simple s2k */
	minlen = 0;
	break;
      case 1:  /* salted s2k */
	minlen = 8;
	break;
      case 3:  /* iterated+salted s2k */
	minlen = 9;
	break;
      default:
	log_error("unknown S2K %d\n", s2kmode );
	goto leave;
    }
    if( minlen > pktlen ) {
	log_error("packet with S2K %d too short\n", s2kmode );
        rc = G10ERR_INVALID_PACKET;
	goto leave;
    }
    seskeylen = pktlen - minlen;
    k = packet->pkt.symkey_enc = xmalloc_clear( sizeof *packet->pkt.symkey_enc
						+ seskeylen - 1 );
    k->version = version;
    k->cipher_algo = cipher_algo;
    k->s2k.mode = s2kmode;
    k->s2k.hash_algo = hash_algo;
    if( s2kmode == 1 || s2kmode == 3 ) {
	for(i=0; i < 8 && pktlen; i++, pktlen-- )
	    k->s2k.salt[i] = iobuf_get_noeof(inp);
    }
    if( s2kmode == 3 ) {
	k->s2k.count = iobuf_get(inp); pktlen--;
    }
    k->seskeylen = seskeylen;
    if(k->seskeylen)
      {
	for(i=0; i < seskeylen && pktlen; i++, pktlen-- )
	  k->seskey[i] = iobuf_get_noeof(inp);

	/* What we're watching out for here is a session key decryptor
	   with no salt.  The RFC says that using salt for this is a
	   MUST. */
	if(s2kmode!=1 && s2kmode!=3)
	  log_info(_("WARNING: potentially insecure symmetrically"
		     " encrypted session key\n"));
      }
    assert( !pktlen );

    if( list_mode ) {
	fprintf (listfp, ":symkey enc packet: version %d, cipher %d, s2k %d, hash %d",
	       version, cipher_algo, s2kmode, hash_algo);
	if(seskeylen)
	  fprintf (listfp, ", seskey %d bits",(seskeylen-1)*8);
	fprintf (listfp, "\n");
	if( s2kmode == 1 || s2kmode == 3 ) {
	    fprintf (listfp, "\tsalt ");
	    for(i=0; i < 8; i++ )
		fprintf (listfp, "%02x", k->s2k.salt[i]);
	    if( s2kmode == 3 )
		fprintf (listfp, ", count %lu (%lu)",
			 S2K_DECODE_COUNT((ulong)k->s2k.count),
			 (ulong)k->s2k.count );
	    fprintf (listfp, "\n");
	}
    }

  leave:
    iobuf_skip_rest(inp, pktlen, 0);
    return rc;
}

static int
parse_pubkeyenc( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
{
    unsigned int n;
    int rc = 0;
    int i, ndata;
    PKT_pubkey_enc *k;

    k = packet->pkt.pubkey_enc = xmalloc_clear(sizeof *packet->pkt.pubkey_enc);
    if( pktlen < 12 ) {
	log_error("packet(%d) too short\n", pkttype);
        rc = G10ERR_INVALID_PACKET;
	goto leave;
    }
    k->version = iobuf_get_noeof(inp); pktlen--;
    if( k->version != 2 && k->version != 3 ) {
	log_error("packet(%d) with unknown version %d\n", pkttype, k->version);
        rc = G10ERR_INVALID_PACKET;
	goto leave;
    }
    k->keyid[0] = read_32(inp); pktlen -= 4;
    k->keyid[1] = read_32(inp); pktlen -= 4;
    k->pubkey_algo = iobuf_get_noeof(inp); pktlen--;
    k->throw_keyid = 0; /* only used as flag for build_packet */
    if( list_mode )
	fprintf (listfp, ":pubkey enc packet: version %d, algo %d, keyid %08lX%08lX\n",
	  k->version, k->pubkey_algo, (ulong)k->keyid[0], (ulong)k->keyid[1]);

    ndata = pubkey_get_nenc(k->pubkey_algo);
    if( !ndata ) {
	if( list_mode )
	    fprintf (listfp, "\tunsupported algorithm %d\n", k->pubkey_algo );
	unknown_pubkey_warning( k->pubkey_algo );
	k->data[0] = NULL;  /* no need to store the encrypted data */
    }
    else {
	for( i=0; i < ndata; i++ ) {
	    n = pktlen;
	    k->data[i] = mpi_read(inp, &n, 0); pktlen -=n;
	    if( list_mode ) {
		fprintf (listfp, "\tdata: ");
		mpi_print(listfp, k->data[i], mpi_print_mode );
		putc ('\n', listfp);
	    }
            if (!k->data[i])
                rc = G10ERR_INVALID_PACKET;
	}
    }

  leave:
    iobuf_skip_rest(inp, pktlen, 0);
    return rc;
}


static void
dump_sig_subpkt( int hashed, int type, int critical,
		 const byte *buffer, size_t buflen, size_t length )
{
    const char *p=NULL;
    int i;

    /* The CERT has warning out with explains how to use GNUPG to
     * detect the ARRs - we print our old message here when it is a faked
     * ARR and add an additional notice */
    if ( type == SIGSUBPKT_ARR && !hashed ) {
        fprintf (listfp,
                 "\tsubpkt %d len %u (additional recipient request)\n"
                 "WARNING: PGP versions > 5.0 and < 6.5.8 will automagically "
                 "encrypt to this key and thereby reveal the plaintext to "
                 "the owner of this ARR key. Detailed info follows:\n",
                 type, (unsigned)length );
    }

    buffer++;
    length--;

    fprintf (listfp, "\t%s%ssubpkt %d len %u (", /*)*/
	      critical ? "critical ":"",
	      hashed ? "hashed ":"", type, (unsigned)length );
    if( length > buflen ) {
	fprintf (listfp, "too short: buffer is only %u)\n", (unsigned)buflen );
	return;
    }
    switch( type ) {
      case SIGSUBPKT_SIG_CREATED:
	if( length >= 4 )
	    fprintf (listfp, "sig created %s",
                     strtimestamp (buf32_to_u32(buffer)) );
	break;
      case SIGSUBPKT_SIG_EXPIRE:
	if( length >= 4 )
	  {
	    if(buf32_to_u32(buffer))
	      fprintf (listfp, "sig expires after %s",
		       strtimevalue( buf32_to_u32(buffer) ) );
	    else
	      fprintf (listfp, "sig does not expire");
	  }
	break;
      case SIGSUBPKT_EXPORTABLE:
	if( length )
	    fprintf (listfp, "%sexportable", *buffer? "":"not ");
	break;
      case SIGSUBPKT_TRUST:
	if(length!=2)
	  p="[invalid trust subpacket]";
	else
	  fprintf (listfp, "trust signature of depth %d, value %d",
                   buffer[0],buffer[1]);
	break;
      case SIGSUBPKT_REGEXP:
	if(!length)
	  p="[invalid regexp subpacket]";
	else
          {
            fprintf (listfp, "regular expression: \"");
            print_string (listfp, buffer, length, '\"');
            p = "\"";
          }
	break;
      case SIGSUBPKT_REVOCABLE:
	if( length )
	    fprintf (listfp, "%srevocable", *buffer? "":"not ");
	break;
      case SIGSUBPKT_KEY_EXPIRE:
	if( length >= 4 )
	  {
	    if(buf32_to_u32(buffer))
	      fprintf (listfp, "key expires after %s",
		       strtimevalue( buf32_to_u32(buffer) ) );
	    else
	      fprintf (listfp, "key does not expire");
	  }
	break;
      case SIGSUBPKT_PREF_SYM:
	fputs("pref-sym-algos:", listfp );
	for( i=0; i < length; i++ )
	    fprintf (listfp, " %d", buffer[i] );
	break;
      case SIGSUBPKT_REV_KEY:
	fputs("revocation key: ", listfp );
	if( length < 22 )
	    p = "[too short]";
	else {
	    fprintf (listfp, "c=%02x a=%d f=", buffer[0], buffer[1] );
	    for( i=2; i < length; i++ )
		fprintf (listfp, "%02X", buffer[i] );
	}
	break;
      case SIGSUBPKT_ISSUER:
	if( length >= 8 )
	    fprintf (listfp, "issuer key ID %08lX%08lX",
                     buf32_to_ulong (buffer),
                     buf32_to_ulong (buffer+4));
	break;
      case SIGSUBPKT_NOTATION:
	{
	    fputs("notation: ", listfp );
	    if( length < 8 )
		p = "[too short]";
	    else {
		const byte *s = buffer;
		size_t n1, n2;

		n1 = (s[4] << 8) | s[5];
		n2 = (s[6] << 8) | s[7];
		s += 8;
		if( 8+n1+n2 != length )
		    p = "[error]";
		else {
		    print_string( listfp, s, n1, ')' );
		    putc( '=', listfp );

		    if( *buffer & 0x80 )
		      print_string( listfp, s+n1, n2, ')' );
		    else
		      p = "[not human readable]";
		}
	    }
	}
	break;
      case SIGSUBPKT_PREF_HASH:
	fputs("pref-hash-algos:", listfp );
	for( i=0; i < length; i++ )
	    fprintf (listfp, " %d", buffer[i] );
	break;
      case SIGSUBPKT_PREF_COMPR:
	fputs("pref-zip-algos:", listfp );
	for( i=0; i < length; i++ )
	    fprintf (listfp, " %d", buffer[i] );
	break;
      case SIGSUBPKT_KS_FLAGS:
	fputs("key server preferences:",listfp);
	for(i=0;i<length;i++)
	  fprintf (listfp, " %02X", buffer[i]);
	break;
      case SIGSUBPKT_PREF_KS:
	fputs("preferred key server: ", listfp );
	print_string( listfp, buffer, length, ')' );
	break;
      case SIGSUBPKT_PRIMARY_UID:
	p = "primary user ID";
	break;
      case SIGSUBPKT_POLICY:
	fputs("policy: ", listfp );
	print_string( listfp, buffer, length, ')' );
	break;
      case SIGSUBPKT_KEY_FLAGS:
        fputs ( "key flags:", listfp );
        for( i=0; i < length; i++ )
            fprintf (listfp, " %02X", buffer[i] );
	break;
      case SIGSUBPKT_SIGNERS_UID:
	p = "signer's user ID";
	break;
      case SIGSUBPKT_REVOC_REASON:
        if( length ) {
	    fprintf (listfp, "revocation reason 0x%02x (", *buffer );
	    print_string( listfp, buffer+1, length-1, ')' );
	    p = ")";
	}
	break;
      case SIGSUBPKT_ARR:
        fputs("Big Brother's key (ignored): ", listfp );
	if( length < 22 )
	    p = "[too short]";
	else {
	    fprintf (listfp, "c=%02x a=%d f=", buffer[0], buffer[1] );
	    for( i=2; i < length; i++ )
		fprintf (listfp, "%02X", buffer[i] );
	}
        break;
      case SIGSUBPKT_FEATURES:
        fputs ( "features:", listfp );
        for( i=0; i < length; i++ )
            fprintf (listfp, " %02x", buffer[i] );
	break;
      case SIGSUBPKT_SIGNATURE:
	fputs("signature: ",listfp);
	if(length<17)
	  p="[too short]";
	else
	  fprintf (listfp, "v%d, class 0x%02X, algo %d, digest algo %d",
		 buffer[0],
		 buffer[0]==3?buffer[2]:buffer[1],
		 buffer[0]==3?buffer[15]:buffer[2],
		 buffer[0]==3?buffer[16]:buffer[3]);
	break;
      default:
	if(type>=100 && type<=110)
	  p="experimental / private subpacket";
	else
	  p = "?";
	break;
    }

    fprintf (listfp, "%s)\n", p? p: "");
}

/****************
 * Returns: >= 0 use this offset into buffer
 *	    -1 explicitly reject returning this type
 *	    -2 subpacket too short
 */
int
parse_one_sig_subpkt( const byte *buffer, size_t n, int type )
{
  switch( type )
    {
    case SIGSUBPKT_REV_KEY:
      if(n < 22)
	break;
      return 0;
    case SIGSUBPKT_SIG_CREATED:
    case SIGSUBPKT_SIG_EXPIRE:
    case SIGSUBPKT_KEY_EXPIRE:
      if( n < 4 )
	break;
      return 0;
    case SIGSUBPKT_KEY_FLAGS:
    case SIGSUBPKT_KS_FLAGS:
    case SIGSUBPKT_PREF_SYM:
    case SIGSUBPKT_PREF_HASH:
    case SIGSUBPKT_PREF_COMPR:
    case SIGSUBPKT_POLICY:
    case SIGSUBPKT_PREF_KS:
    case SIGSUBPKT_FEATURES:
    case SIGSUBPKT_REGEXP:
      return 0;
    case SIGSUBPKT_SIGNATURE:
    case SIGSUBPKT_EXPORTABLE:
    case SIGSUBPKT_REVOCABLE:
    case SIGSUBPKT_REVOC_REASON:
      if( !n )
	break;
      return 0;
    case SIGSUBPKT_ISSUER: /* issuer key ID */
      if( n < 8 )
	break;
      return 0;
    case SIGSUBPKT_NOTATION:
      /* minimum length needed, and the subpacket must be well-formed
	 where the name length and value length all fit inside the
	 packet. */
      if(n<8 || 8+((buffer[4]<<8)|buffer[5])+((buffer[6]<<8)|buffer[7]) != n)
	break;
      return 0;
    case SIGSUBPKT_PRIMARY_UID:
      if ( n != 1 )
	break;
      return 0;
    case SIGSUBPKT_TRUST:
      if ( n != 2 )
	break;
      return 0;
    default: return 0;
    }
  return -2;
}

/* Not many critical notations we understand yet... */
static int
can_handle_critical_notation(const byte *name,size_t len)
{
  if(len==32 && memcmp(name,"preferred-email-encoding@pgp.com",32)==0)
    return 1;
  if(len==21 && memcmp(name,"pka-address@gnupg.org",21)==0)
    return 1;

  return 0;
}

static int
can_handle_critical( const byte *buffer, size_t n, int type )
{
  switch( type )
    {
    case SIGSUBPKT_NOTATION:
      if (n >= 8)
	{
	  size_t notation_len = ((buffer[4] << 8) | buffer[5]);
	  if (n - 8 >= notation_len)
	    return can_handle_critical_notation (buffer + 8, notation_len);
	}
      return 0;
    case SIGSUBPKT_SIGNATURE:
    case SIGSUBPKT_SIG_CREATED:
    case SIGSUBPKT_SIG_EXPIRE:
    case SIGSUBPKT_KEY_EXPIRE:
    case SIGSUBPKT_EXPORTABLE:
    case SIGSUBPKT_REVOCABLE:
    case SIGSUBPKT_REV_KEY:
    case SIGSUBPKT_ISSUER:/* issuer key ID */
    case SIGSUBPKT_PREF_SYM:
    case SIGSUBPKT_PREF_HASH:
    case SIGSUBPKT_PREF_COMPR:
    case SIGSUBPKT_KEY_FLAGS:
    case SIGSUBPKT_PRIMARY_UID:
    case SIGSUBPKT_FEATURES:
    case SIGSUBPKT_TRUST:
    case SIGSUBPKT_REGEXP:
      /* Is it enough to show the policy or keyserver? */
    case SIGSUBPKT_POLICY:
    case SIGSUBPKT_PREF_KS:
      return 1;

    default:
      return 0;
    }
}


const byte *
enum_sig_subpkt( const subpktarea_t *pktbuf, sigsubpkttype_t reqtype,
		 size_t *ret_n, int *start, int *critical )
{
    const byte *buffer;
    int buflen;
    int type;
    int critical_dummy;
    int offset;
    size_t n;
    int seq = 0;
    int reqseq = start? *start: 0;

    if(!critical)
      critical=&critical_dummy;

    if( !pktbuf || reqseq == -1 ) {
	/* return some value different from NULL to indicate that
	 * there is no critical bit we do not understand.  The caller
	 * will never use the value.  Yes I know, it is an ugly hack */
	return reqtype == SIGSUBPKT_TEST_CRITICAL? (const byte*)&pktbuf : NULL;
    }
    buffer = pktbuf->data;
    buflen = pktbuf->len;
    while( buflen ) {
	n = *buffer++; buflen--;
	if( n == 255 ) { /* 4 byte length header */
	    if( buflen < 4 )
		goto too_short;
            n = buf32_to_size_t (buffer);
	    buffer += 4;
	    buflen -= 4;
	}
	else if( n >= 192 ) { /* 2 byte special encoded length header */
	    if( buflen < 2 )
		goto too_short;
	    n = (( n - 192 ) << 8) + *buffer + 192;
	    buffer++;
	    buflen--;
	}
	if( buflen < n )
	    goto too_short;
	type = *buffer;
	if( type & 0x80 ) {
	    type &= 0x7f;
	    *critical = 1;
	}
	else
	    *critical = 0;
	if( !(++seq > reqseq) )
	    ;
	else if( reqtype == SIGSUBPKT_TEST_CRITICAL ) {
	    if( *critical ) {
		if( n-1 > buflen+1 )
		    goto too_short;
		if( !can_handle_critical(buffer+1, n-1, type ) )
		  {
		    if(opt.verbose)
		      log_info(_("subpacket of type %d has "
				 "critical bit set\n"),type);
		    if( start )
		      *start = seq;
		    return NULL; /* this is an error */
		  }
	    }
	}
	else if( reqtype < 0 ) /* list packets */
	    dump_sig_subpkt( reqtype == SIGSUBPKT_LIST_HASHED,
				    type, *critical, buffer, buflen, n );
	else if( type == reqtype ) { /* found */
	    buffer++;
	    n--;
	    if( n > buflen )
		goto too_short;
	    if( ret_n )
		*ret_n = n;
	    offset = parse_one_sig_subpkt(buffer, n, type );
	    switch( offset ) {
	      case -2:
		log_error("subpacket of type %d too short\n", type);
		return NULL;
	      case -1:
		return NULL;
	      default:
		break;
	    }
	    if( start )
		*start = seq;
	    return buffer+offset;
	}
	buffer += n; buflen -=n;
    }
    if( reqtype == SIGSUBPKT_TEST_CRITICAL )
	return buffer; /* as value true to indicate that there is no */
		       /* critical bit we don't understand */
    if( start )
	*start = -1;
    return NULL; /* end of packets; not found */

  too_short:
    if(opt.verbose)
      log_info("buffer shorter than subpacket\n");
    if( start )
	*start = -1;
    return NULL;
}


const byte *
parse_sig_subpkt (const subpktarea_t *buffer, sigsubpkttype_t reqtype,
                  size_t *ret_n)
{
    return enum_sig_subpkt( buffer, reqtype, ret_n, NULL, NULL );
}

const byte *
parse_sig_subpkt2 (PKT_signature *sig, sigsubpkttype_t reqtype,
                   size_t *ret_n )
{
    const byte *p;

    p = parse_sig_subpkt (sig->hashed, reqtype, ret_n );
    if( !p )
	p = parse_sig_subpkt (sig->unhashed, reqtype, ret_n );
    return p;
}

/* Find all revocation keys. Look in hashed area only. */
void parse_revkeys(PKT_signature *sig)
{
  struct revocation_key *revkey;
  int seq=0;
  size_t len;

  if(sig->sig_class!=0x1F)
    return;

  while((revkey=
	 (struct revocation_key *)enum_sig_subpkt(sig->hashed,
						  SIGSUBPKT_REV_KEY,
						  &len,&seq,NULL)))
    {
      if(len==sizeof(struct revocation_key) &&
	 (revkey->class&0x80)) /* 0x80 bit must be set */
	{
	  sig->revkey=xrealloc(sig->revkey,
			  sizeof(struct revocation_key *)*(sig->numrevkeys+1));
	  sig->revkey[sig->numrevkeys]=revkey;
	  sig->numrevkeys++;
	}
    }
}

int
parse_signature( IOBUF inp, int pkttype, unsigned long pktlen,
					  PKT_signature *sig )
{
    int md5_len=0;
    unsigned n;
    int is_v4=0;
    int rc=0;
    int i, ndata;

    if( pktlen < 16 ) {
	log_error("packet(%d) too short\n", pkttype);
	goto leave;
    }
    sig->version = iobuf_get_noeof(inp); pktlen--;
    if( sig->version == 4 )
	is_v4=1;
    else if( sig->version != 2 && sig->version != 3 ) {
	log_error("packet(%d) with unknown version %d\n", pkttype, sig->version);
        rc = G10ERR_INVALID_PACKET;
	goto leave;
    }

    if( !is_v4 ) {
	md5_len = iobuf_get_noeof(inp); pktlen--;
    }
    sig->sig_class = iobuf_get_noeof(inp); pktlen--;
    if( !is_v4 ) {
	sig->timestamp = read_32(inp); pktlen -= 4;
	sig->keyid[0] = read_32(inp); pktlen -= 4;
	sig->keyid[1] = read_32(inp); pktlen -= 4;
    }
    sig->pubkey_algo = iobuf_get_noeof(inp); pktlen--;
    sig->digest_algo = iobuf_get_noeof(inp); pktlen--;
    sig->flags.exportable=1;
    sig->flags.revocable=1;
    if( is_v4 ) { /* read subpackets */
	n = read_16(inp); pktlen -= 2; /* length of hashed data */
	if( n > 10000 ) {
	    log_error("signature packet: hashed data too long\n");
	    rc = G10ERR_INVALID_PACKET;
	    goto leave;
	}
	if( n ) {
	    sig->hashed = xmalloc (sizeof (*sig->hashed) + n - 1 );
            sig->hashed->size = n;
	    sig->hashed->len = n;
	    if( iobuf_read (inp, sig->hashed->data, n ) != n ) {
		log_error ("premature eof while reading "
                           "hashed signature data\n");
		rc = -1;
		goto leave;
	    }
	    pktlen -= n;
	}
	n = read_16(inp); pktlen -= 2; /* length of unhashed data */
	if( n > 10000 ) {
	    log_error("signature packet: unhashed data too long\n");
	    rc = G10ERR_INVALID_PACKET;
	    goto leave;
	}
	if( n ) {
	    sig->unhashed = xmalloc (sizeof(*sig->unhashed) + n - 1 );
            sig->unhashed->size = n;
	    sig->unhashed->len = n;
	    if( iobuf_read(inp, sig->unhashed->data, n ) != n ) {
		log_error("premature eof while reading "
                          "unhashed signature data\n");
		rc = -1;
		goto leave;
	    }
	    pktlen -= n;
	}
    }

    if( pktlen < 5 ) { /* sanity check */
	log_error("packet(%d) too short\n", pkttype);
	rc = G10ERR_INVALID_PACKET;
	goto leave;
    }

    sig->digest_start[0] = iobuf_get_noeof(inp); pktlen--;
    sig->digest_start[1] = iobuf_get_noeof(inp); pktlen--;

    if( is_v4 && sig->pubkey_algo )
      { /*extract required information */
	const byte *p;
	size_t len;

	/* set sig->flags.unknown_critical if there is a
	 * critical bit set for packets which we do not understand */
	if( !parse_sig_subpkt (sig->hashed, SIGSUBPKT_TEST_CRITICAL, NULL)
	    || !parse_sig_subpkt (sig->unhashed, SIGSUBPKT_TEST_CRITICAL,
				  NULL) )
	  sig->flags.unknown_critical = 1;

	p = parse_sig_subpkt (sig->hashed, SIGSUBPKT_SIG_CREATED, NULL );
	if(p)
	  sig->timestamp = buf32_to_u32 (p);
	else if(!(sig->pubkey_algo>=100 && sig->pubkey_algo<=110)
		&& opt.verbose)
	  log_info ("signature packet without timestamp\n");

	p = parse_sig_subpkt2( sig, SIGSUBPKT_ISSUER, NULL );
	if(p)
	  {
	    sig->keyid[0] = buf32_to_u32 (p);
	    sig->keyid[1] = buf32_to_u32 (p+4);
	  }
	else if(!(sig->pubkey_algo>=100 && sig->pubkey_algo<=110)
		&& opt.verbose)
	  log_info ("signature packet without keyid\n");

	p=parse_sig_subpkt(sig->hashed,SIGSUBPKT_SIG_EXPIRE,NULL);
	if(p && buf32_to_u32 (p))
	  sig->expiredate = sig->timestamp + buf32_to_u32 (p);
	if(sig->expiredate && sig->expiredate<=make_timestamp())
	  sig->flags.expired=1;

	p=parse_sig_subpkt(sig->hashed,SIGSUBPKT_POLICY,NULL);
	if(p)
	  sig->flags.policy_url=1;

	p=parse_sig_subpkt(sig->hashed,SIGSUBPKT_PREF_KS,NULL);
	if(p)
	  sig->flags.pref_ks=1;

	p=parse_sig_subpkt(sig->hashed,SIGSUBPKT_NOTATION,NULL);
	if(p)
	  sig->flags.notation=1;

	p=parse_sig_subpkt(sig->hashed,SIGSUBPKT_REVOCABLE,NULL);
	if(p && *p==0)
	  sig->flags.revocable=0;

	p=parse_sig_subpkt(sig->hashed,SIGSUBPKT_TRUST,&len);
	if(p && len==2)
	  {
	    sig->trust_depth=p[0];
	    sig->trust_value=p[1];

	    /* Only look for a regexp if there is also a trust
	       subpacket. */
	    sig->trust_regexp=
	      parse_sig_subpkt(sig->hashed,SIGSUBPKT_REGEXP,&len);

	    /* If the regular expression is of 0 length, there is no
	       regular expression. */
	    if(len==0)
	      sig->trust_regexp=NULL;
	  }

	/* We accept the exportable subpacket from either the hashed
	   or unhashed areas as older versions of gpg put it in the
	   unhashed area.  In theory, anyway, we should never see this
	   packet off of a local keyring. */

	p=parse_sig_subpkt2(sig,SIGSUBPKT_EXPORTABLE,NULL);
	if(p && *p==0)
	  sig->flags.exportable=0;

	/* Find all revocation keys. */
	if(sig->sig_class==0x1F)
	  parse_revkeys(sig);
      }

    if( list_mode ) {
	fprintf (listfp, ":signature packet: algo %d, keyid %08lX%08lX\n"
	       "\tversion %d, created %lu, md5len %d, sigclass 0x%02x\n"
	       "\tdigest algo %d, begin of digest %02x %02x\n",
		sig->pubkey_algo,
		(ulong)sig->keyid[0], (ulong)sig->keyid[1],
		sig->version, (ulong)sig->timestamp, md5_len, sig->sig_class,
		sig->digest_algo,
		sig->digest_start[0], sig->digest_start[1] );
	if( is_v4 ) {
	    parse_sig_subpkt (sig->hashed,   SIGSUBPKT_LIST_HASHED, NULL );
	    parse_sig_subpkt (sig->unhashed, SIGSUBPKT_LIST_UNHASHED, NULL);
	}
    }

    ndata = pubkey_get_nsig(sig->pubkey_algo);
    if( !ndata ) {
	if( list_mode )
	    fprintf (listfp, "\tunknown algorithm %d\n", sig->pubkey_algo );
	unknown_pubkey_warning( sig->pubkey_algo );
	/* We store the plain material in data[0], so that we are able
	 * to write it back with build_packet() */
        if (pktlen > (5 * MAX_EXTERN_MPI_BITS/8))
          {
            /* However we include a limit to avoid too trivial DoS
               attacks by having gpg allocate too much memory.  */
	    log_error ("signature packet: too much data\n");
	    rc = G10ERR_INVALID_PACKET;
          }
        else
          {
            sig->data[0]= mpi_set_opaque (NULL, read_rest(inp, pktlen, 0),
                                          pktlen );
            pktlen = 0;
          }
    }
    else {
	for( i=0; i < ndata; i++ ) {
	    n = pktlen;
	    sig->data[i] = mpi_read(inp, &n, 0 );
	    pktlen -=n;
	    if( list_mode ) {
		fprintf (listfp, "\tdata: ");
		mpi_print(listfp, sig->data[i], mpi_print_mode );
		putc ('\n', listfp);
	    }
            if (!sig->data[i])
                rc = G10ERR_INVALID_PACKET;
	}
    }

  leave:
    iobuf_skip_rest(inp, pktlen, 0);
    return rc;
}


static int
parse_onepass_sig( IOBUF inp, int pkttype, unsigned long pktlen,
					     PKT_onepass_sig *ops )
{
    int version;
    int rc = 0;

    if( pktlen < 13 ) {
	log_error("packet(%d) too short\n", pkttype);
        rc = G10ERR_INVALID_PACKET;
	goto leave;
    }
    version = iobuf_get_noeof(inp); pktlen--;
    if( version != 3 ) {
	log_error("onepass_sig with unknown version %d\n", version);
        rc = G10ERR_INVALID_PACKET;
	goto leave;
    }
    ops->sig_class = iobuf_get_noeof(inp); pktlen--;
    ops->digest_algo = iobuf_get_noeof(inp); pktlen--;
    ops->pubkey_algo = iobuf_get_noeof(inp); pktlen--;
    ops->keyid[0] = read_32(inp); pktlen -= 4;
    ops->keyid[1] = read_32(inp); pktlen -= 4;
    ops->last = iobuf_get_noeof(inp); pktlen--;
    if( list_mode )
	fprintf (listfp, ":onepass_sig packet: keyid %08lX%08lX\n"
	      "\tversion %d, sigclass 0x%02x, digest %d, pubkey %d, last=%d\n",
		(ulong)ops->keyid[0], (ulong)ops->keyid[1],
		version, ops->sig_class,
		ops->digest_algo, ops->pubkey_algo, ops->last );


  leave:
    iobuf_skip_rest(inp, pktlen, 0);
    return rc;
}


static MPI
read_protected_v3_mpi (IOBUF inp, unsigned long *length)
{
  int c;
  unsigned int nbits, nbytes;
  unsigned char *buf, *p;
  MPI val;

  if (*length < 2)
    {
      log_error ("mpi too small\n");
      return NULL;
    }

  if ((c=iobuf_get (inp)) == -1)
    return NULL;
  --*length;
  nbits = c << 8;
  if ((c=iobuf_get(inp)) == -1)
    return NULL;
  --*length;
  nbits |= c;

  if (nbits > 16384)
    {
      log_error ("mpi too large (%u bits)\n", nbits);
      return NULL;
    }
  nbytes = (nbits+7) / 8;
  buf = p = xmalloc (2 + nbytes);
  *p++ = nbits >> 8;
  *p++ = nbits;
  for (; nbytes && *length; nbytes--, --*length)
    *p++ = iobuf_get (inp);
  if (nbytes)
    {
      log_error ("packet shorter tham mpi\n");
      xfree (buf);
      return NULL;
    }

  /* convert buffer into an opaque MPI */
  val = mpi_set_opaque (NULL, buf, p-buf);
  return val;
}


static int
parse_key( IOBUF inp, int pkttype, unsigned long pktlen,
			      byte *hdr, int hdrlen, PACKET *pkt )
{
    int i, version, algorithm;
    unsigned n;
    unsigned long timestamp, expiredate, max_expiredate;
    int npkey, nskey;
    int is_v4=0;
    int rc=0;
    u32 keyid[2];

    version = iobuf_get_noeof(inp); pktlen--;
    if( pkttype == PKT_PUBLIC_SUBKEY && version == '#' ) {
	/* early versions of G10 use old PGP comments packets;
	 * luckily all those comments are started by a hash */
	if( list_mode ) {
	    fprintf (listfp, ":rfc1991 comment packet: \"" );
	    for( ; pktlen; pktlen-- ) {
		int c;
		c = iobuf_get_noeof(inp);
		if( c >= ' ' && c <= 'z' )
		    putc (c, listfp);
		else
		    fprintf (listfp, "\\x%02x", c );
	    }
	    fprintf (listfp, "\"\n");
	}
	iobuf_skip_rest(inp, pktlen, 0);
	return 0;
    }
    else if( version == 4 )
	is_v4=1;
    else if( version != 2 && version != 3 ) {
	log_error("packet(%d) with unknown version %d\n", pkttype, version);
        rc = G10ERR_INVALID_PACKET;
	goto leave;
    }

    if( pktlen < 11 ) {
	log_error("packet(%d) too short\n", pkttype);
        rc = G10ERR_INVALID_PACKET;
	goto leave;
    }
    else if (pktlen > MAX_KEY_PACKET_LENGTH) {
        log_error ("packet(%d) too large\n", pkttype);
        if (list_mode)
            fputs (":key packet: [too large]\n", listfp);
        rc = G10ERR_INVALID_PACKET;
        goto leave;
    }

    timestamp = read_32(inp); pktlen -= 4;
    if( is_v4 ) {
	expiredate = 0; /* have to get it from the selfsignature */
	max_expiredate = 0;
    }
    else {
	unsigned short ndays;
	ndays = read_16(inp); pktlen -= 2;
	if( ndays )
	    expiredate = timestamp + ndays * 86400L;
	else
	    expiredate = 0;

	max_expiredate=expiredate;
    }
    algorithm = iobuf_get_noeof(inp); pktlen--;
    if( list_mode )
	fprintf (listfp, ":%s key packet:\n"
	       "\tversion %d, algo %d, created %lu, expires %lu\n",
		pkttype == PKT_PUBLIC_KEY? "public" :
		pkttype == PKT_SECRET_KEY? "secret" :
		pkttype == PKT_PUBLIC_SUBKEY? "public sub" :
		pkttype == PKT_SECRET_SUBKEY? "secret sub" : "??",
		version, algorithm, timestamp, expiredate );

    if( pkttype == PKT_SECRET_KEY || pkttype == PKT_SECRET_SUBKEY )  {
	PKT_secret_key *sk = pkt->pkt.secret_key;

	sk->timestamp = timestamp;
	sk->expiredate = expiredate;
	sk->max_expiredate = max_expiredate;
	sk->hdrbytes = hdrlen;
	sk->version = version;
	sk->is_primary = pkttype == PKT_SECRET_KEY;
	sk->pubkey_algo = algorithm;
	sk->req_usage = 0;
	sk->pubkey_usage = 0; /* not yet used */
    }
    else {
	PKT_public_key *pk = pkt->pkt.public_key;

	pk->timestamp = timestamp;
	pk->expiredate = expiredate;
	pk->max_expiredate = max_expiredate;
	pk->hdrbytes	= hdrlen;
	pk->version	= version;
	pk->is_primary = pkttype == PKT_PUBLIC_KEY;
	pk->pubkey_algo = algorithm;
	pk->req_usage = 0;
	pk->pubkey_usage = 0; /* not yet used */
        pk->is_revoked = 0;
	pk->is_disabled = 0;
	pk->keyid[0] = 0;
	pk->keyid[1] = 0;
    }
    nskey = pubkey_get_nskey( algorithm );
    npkey = pubkey_get_npkey( algorithm );
    if( !npkey ) {
	if( list_mode )
	    fprintf (listfp, "\tunknown algorithm %d\n", algorithm );
	unknown_pubkey_warning( algorithm );
    }

    if( pkttype == PKT_SECRET_KEY || pkttype == PKT_SECRET_SUBKEY ) {
	PKT_secret_key *sk = pkt->pkt.secret_key;
	byte temp[16];
        size_t snlen = 0;

	if( !npkey ) {
	    sk->skey[0] = mpi_set_opaque( NULL,
					  read_rest(inp, pktlen, 0), pktlen );
	    pktlen = 0;
	    goto leave;
	}

	for(i=0; i < npkey; i++ ) {
	    n = pktlen; sk->skey[i] = mpi_read(inp, &n, 0 ); pktlen -=n;
	    if( list_mode ) {
		fprintf (listfp,   "\tskey[%d]: ", i);
		mpi_print(listfp, sk->skey[i], mpi_print_mode  );
		putc ('\n', listfp);
	    }
            if (!sk->skey[i])
                rc = G10ERR_INVALID_PACKET;
	}
        if (rc) /* one of the MPIs were bad */
            goto leave;
        if (list_mode && npkey)
            keyid_from_sk (sk, keyid);

	sk->protect.algo = iobuf_get_noeof(inp); pktlen--;
        sk->protect.sha1chk = 0;
	if( sk->protect.algo ) {
	    sk->is_protected = 1;
	    sk->protect.s2k.count = 0;
	    if( sk->protect.algo == 254 || sk->protect.algo == 255 ) {
		if( pktlen < 3 ) {
		    rc = G10ERR_INVALID_PACKET;
		    goto leave;
		}
                sk->protect.sha1chk = (sk->protect.algo == 254);
		sk->protect.algo = iobuf_get_noeof(inp); pktlen--;
		/* Note that a sk->protect.algo > 110 is illegal, but
		   I'm not erroring on it here as otherwise there
		   would be no way to delete such a key. */
		sk->protect.s2k.mode  = iobuf_get_noeof(inp); pktlen--;
		sk->protect.s2k.hash_algo = iobuf_get_noeof(inp); pktlen--;
		/* check for the special GNU extension */
		if( is_v4 && sk->protect.s2k.mode == 101 ) {
		    for(i=0; i < 4 && pktlen; i++, pktlen-- )
			temp[i] = iobuf_get_noeof(inp);
		    if( i < 4 || memcmp( temp, "GNU", 3 ) ) {
			if( list_mode )
			    fprintf (listfp,   "\tunknown S2K %d\n",
						sk->protect.s2k.mode );
			rc = G10ERR_INVALID_PACKET;
			goto leave;
		    }
		    /* here we know that it is a gnu extension
		     * What follows is the GNU protection mode:
		     * All values have special meanings
		     * and they are mapped in the mode with a base of 1000.
		     */
		    sk->protect.s2k.mode = 1000 + temp[3];
		}
		switch( sk->protect.s2k.mode ) {
		  case 1:
		  case 3:
		    for(i=0; i < 8 && pktlen; i++, pktlen-- )
			temp[i] = iobuf_get_noeof(inp);
		    memcpy(sk->protect.s2k.salt, temp, 8 );
		    break;
		}
		switch( sk->protect.s2k.mode ) {
		  case 0: if( list_mode ) fprintf (listfp, "\tsimple S2K" );
		    break;
		  case 1: if( list_mode ) fprintf (listfp, "\tsalted S2K" );
		    break;
		  case 3: if( list_mode ) fprintf (listfp, "\titer+salt S2K" );
		    break;
		  case 1001: if( list_mode ) fprintf (listfp,
                                                      "\tgnu-dummy S2K" );
		    break;
		  case 1002: if (list_mode) fprintf (listfp,
                                                  "\tgnu-divert-to-card S2K");
		    break;
		  default:
		    if( list_mode )
			fprintf (listfp,   "\tunknown %sS2K %d\n",
				 sk->protect.s2k.mode < 1000? "":"GNU ",
						   sk->protect.s2k.mode );
		    rc = G10ERR_INVALID_PACKET;
		    goto leave;
		}

		if( list_mode ) {
		    fprintf (listfp, ", algo: %d,%s hash: %d",
				     sk->protect.algo,
                                     sk->protect.sha1chk?" SHA1 protection,"
                                                        :" simple checksum,",
				     sk->protect.s2k.hash_algo );
		    if( sk->protect.s2k.mode == 1
			|| sk->protect.s2k.mode == 3 ) {
			fprintf (listfp, ", salt: ");
			for(i=0; i < 8; i++ )
			    fprintf (listfp, "%02x", sk->protect.s2k.salt[i]);
		    }
		    putc ('\n', listfp);
		}

		if( sk->protect.s2k.mode == 3 ) {
		    if( pktlen < 1 ) {
			rc = G10ERR_INVALID_PACKET;
			goto leave;
		    }
		    sk->protect.s2k.count = iobuf_get(inp);
		    pktlen--;
		    if( list_mode )
			fprintf (listfp, "\tprotect count: %lu (%lu)\n",
                                (ulong)S2K_DECODE_COUNT
                                 ((ulong)sk->protect.s2k.count),
                                 (ulong)sk->protect.s2k.count);
		}
		else if( sk->protect.s2k.mode == 1002 ) {
                    /* Read the serial number. */
                    if (pktlen < 1) {
                      rc = G10ERR_INVALID_PACKET;
			goto leave;
		    }
		    snlen = iobuf_get (inp);
		    pktlen--;
                    if (pktlen < snlen || snlen == (size_t)(-1)) {
			rc = G10ERR_INVALID_PACKET;
			goto leave;
                    }
		}
	    }
	    /* Note that a sk->protect.algo > 110 is illegal, but I'm
	       not erroring on it here as otherwise there would be no
	       way to delete such a key. */
	    else { /* old version; no S2K, so we set mode to 0, hash MD5 */
		sk->protect.s2k.mode = 0;
		sk->protect.s2k.hash_algo = DIGEST_ALGO_MD5;
		if( list_mode )
		    fprintf (listfp,   "\tprotect algo: %d  (hash algo: %d)\n",
			 sk->protect.algo, sk->protect.s2k.hash_algo );
	    }
	    /* It is really ugly that we don't know the size
	     * of the IV here in cases we are not aware of the algorithm.
	     * so a
	     *	 sk->protect.ivlen = cipher_get_blocksize(sk->protect.algo);
	     * won't work.  The only solution I see is to hardwire it here.
	     * NOTE: if you change the ivlen above 16, don't forget to
	     * enlarge temp.
	     */
	    switch( sk->protect.algo ) {
	      case 7: case 8: case 9: /* AES */
	      case 10: /* Twofish */
	      case 11: case 12: case 13: /* Camellia */
		sk->protect.ivlen = 16;
		break;
	      default:
		sk->protect.ivlen = 8;
	    }
	    if( sk->protect.s2k.mode == 1001 )
		sk->protect.ivlen = 0;
	    else if( sk->protect.s2k.mode == 1002 )
		sk->protect.ivlen = snlen < 16? snlen : 16;

	    if( pktlen < sk->protect.ivlen ) {
		rc = G10ERR_INVALID_PACKET;
		goto leave;
	    }
	    for(i=0; i < sk->protect.ivlen && pktlen; i++, pktlen-- )
		temp[i] = iobuf_get_noeof(inp);
	    if( list_mode ) {
		fprintf (listfp,
                         sk->protect.s2k.mode == 1002? "\tserial-number: "
                                                     : "\tprotect IV: ");
		for(i=0; i < sk->protect.ivlen; i++ )
		    fprintf (listfp, " %02x", temp[i] );
		putc ('\n', listfp);
	    }
	    memcpy(sk->protect.iv, temp, sk->protect.ivlen );
	}
	else
	    sk->is_protected = 0;
	/* It does not make sense to read it into secure memory.
	 * If the user is so careless, not to protect his secret key,
	 * we can assume, that he operates an open system :=(.
	 * So we put the key into secure memory when we unprotect it. */
	if( sk->protect.s2k.mode == 1001
            || sk->protect.s2k.mode == 1002 ) {
	    /* better set some dummy stuff here */
	    sk->skey[npkey] = mpi_set_opaque(NULL, xstrdup("dummydata"), 10);
	    pktlen = 0;
	}
	else if( is_v4 && sk->is_protected ) {
	    /* ugly; the length is encrypted too, so we read all
	     * stuff up to the end of the packet into the first
	     * skey element */
	    sk->skey[npkey] = mpi_set_opaque(NULL,
					     read_rest(inp, pktlen, 0),pktlen);
	    pktlen = 0;
	    if( list_mode ) {
		fprintf (listfp, "\tencrypted stuff follows\n");
	    }
	}
	else { /* v3 method: the mpi length is not encrypted */
	    for(i=npkey; i < nskey; i++ ) {
                if ( sk->is_protected ) {
                    sk->skey[i] = read_protected_v3_mpi (inp, &pktlen);
                    if( list_mode )
                        fprintf (listfp,   "\tskey[%d]: [encrypted]\n", i);
                }
                else {
                    n = pktlen;
                    sk->skey[i] = mpi_read(inp, &n, 0 );
                    pktlen -=n;
                    if( list_mode ) {
                        fprintf (listfp,   "\tskey[%d]: ", i);
                        mpi_print(listfp, sk->skey[i], mpi_print_mode  );
                        putc ('\n', listfp);
                    }
                }

                if (!sk->skey[i])
                    rc = G10ERR_INVALID_PACKET;
	    }
            if (rc)
                goto leave;

	    sk->csum = read_16(inp); pktlen -= 2;
	    if( list_mode ) {
		fprintf (listfp, "\tchecksum: %04hx\n", sk->csum);
	    }
	}
    }
    else {
	PKT_public_key *pk = pkt->pkt.public_key;

	if( !npkey ) {
	    pk->pkey[0] = mpi_set_opaque( NULL,
					  read_rest(inp, pktlen, 0), pktlen );
	    pktlen = 0;
	    goto leave;
	}

	for(i=0; i < npkey; i++ ) {
	    n = pktlen; pk->pkey[i] = mpi_read(inp, &n, 0 ); pktlen -=n;
	    if( list_mode ) {
		fprintf (listfp,   "\tpkey[%d]: ", i);
		mpi_print(listfp, pk->pkey[i], mpi_print_mode  );
		putc ('\n', listfp);
	    }
            if (!pk->pkey[i])
                rc = G10ERR_INVALID_PACKET;
	}
        if (rc)
            goto leave;

        if (list_mode)
            keyid_from_pk (pk, keyid);
    }

  if (list_mode && npkey)
      fprintf (listfp, "\tkeyid: %08lX%08lX\n",
              (ulong) keyid[0], (ulong) keyid[1]);

  leave:
    iobuf_skip_rest(inp, pktlen, 0);
    return rc;
}

/* Attribute subpackets have the same format as v4 signature
   subpackets.  This is not part of OpenPGP, but is done in several
   versions of PGP nevertheless. */
int
parse_attribute_subpkts(PKT_user_id *uid)
{
  size_t n;
  int count=0;
  struct user_attribute *attribs=NULL;
  const byte *buffer=uid->attrib_data;
  int buflen=uid->attrib_len;
  byte type;

  xfree(uid->attribs);

  while(buflen)
    {
      n = *buffer++; buflen--;
      if( n == 255 ) { /* 4 byte length header */
	if( buflen < 4 )
	  goto too_short;
        n = buf32_to_size_t (buffer);
        buffer += 4;
	buflen -= 4;
      }
      else if( n >= 192 ) { /* 2 byte special encoded length header */
	if( buflen < 2 )
	  goto too_short;
	n = (( n - 192 ) << 8) + *buffer + 192;
	buffer++;
	buflen--;
      }
      if( buflen < n )
	goto too_short;

      if (!n)
        {
          /* Too short to encode the subpacket type.  */
          if (opt.verbose)
            log_info ("attribute subpacket too short\n");
          break;
        }

      attribs=xrealloc(attribs,(count+1)*sizeof(struct user_attribute));
      memset(&attribs[count],0,sizeof(struct user_attribute));

      type=*buffer;
      buffer++;
      buflen--;
      n--;

      attribs[count].type=type;
      attribs[count].data=buffer;
      attribs[count].len=n;
      buffer+=n;
      buflen-=n;
      count++;
    }

  uid->attribs=attribs;
  uid->numattribs=count;
  return count;

 too_short:
  if(opt.verbose)
    log_info("buffer shorter than attribute subpacket\n");
  uid->attribs=attribs;
  uid->numattribs=count;
  return count;
}


static int
parse_user_id( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
{
    byte *p;

    /* Cap the size of a user ID at 2k: a value absurdly large enough
       that there is no sane user ID string (which is printable text
       as of RFC2440bis) that won't fit in it, but yet small enough to
       avoid allocation problems.  A large pktlen may not be
       allocatable, and a very large pktlen could actually cause our
       allocation to wrap around in xmalloc to a small number. */

    if (pktlen > MAX_UID_PACKET_LENGTH)
      {
	log_error("packet(%d) too large\n", pkttype);
	iobuf_skip_rest(inp, pktlen, 0);
	return G10ERR_INVALID_PACKET;
      }

    packet->pkt.user_id = xmalloc_clear(sizeof *packet->pkt.user_id + pktlen);
    packet->pkt.user_id->len = pktlen;
    packet->pkt.user_id->ref=1;

    p = packet->pkt.user_id->name;
    for( ; pktlen; pktlen--, p++ )
	*p = iobuf_get_noeof(inp);
    *p = 0;

    if( list_mode ) {
	int n = packet->pkt.user_id->len;
	fprintf (listfp, ":user ID packet: \"");
	/* fixme: Hey why don't we replace this with print_string?? */
	for(p=packet->pkt.user_id->name; n; p++, n-- ) {
	    if( *p >= ' ' && *p <= 'z' )
		putc (*p, listfp);
	    else
		fprintf (listfp, "\\x%02x", *p );
	}
	fprintf (listfp, "\"\n");
    }
    return 0;
}


void
make_attribute_uidname(PKT_user_id *uid, size_t max_namelen)
{
  assert ( max_namelen > 70 );
  if(uid->numattribs<=0)
    sprintf(uid->name,"[bad attribute packet of size %lu]",uid->attrib_len);
  else if(uid->numattribs>1)
    sprintf(uid->name,"[%d attributes of size %lu]",
	    uid->numattribs,uid->attrib_len);
  else
    {
      /* Only one attribute, so list it as the "user id" */

      if(uid->attribs->type==ATTRIB_IMAGE)
	{
	  u32 len;
	  byte type;

	  if(parse_image_header(uid->attribs,&type,&len))
	    sprintf(uid->name,"[%.20s image of size %lu]",
		    image_type_to_string(type,1),(ulong)len);
	  else
	    sprintf(uid->name,"[invalid image]");
	}
      else
	sprintf(uid->name,"[unknown attribute of size %lu]",
		(ulong)uid->attribs->len);
    }

  uid->len = strlen(uid->name);
}

static int
parse_attribute( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
{
    byte *p;

    (void)pkttype;

    /* We better cap the size of an attribute packet to make DoS not
       too easy.  16MB should be more then enough for one attribute
       packet (ie. a photo).  */
    if (pktlen > MAX_ATTR_PACKET_LENGTH) {
        log_error ("packet(%d) too large\n", pkttype);
        if (list_mode)
          fprintf (listfp, ":attribute packet: [too large]\n");
        iobuf_skip_rest (inp, pktlen, 0);
        return G10ERR_INVALID_PACKET;
      }

#define EXTRA_UID_NAME_SPACE 71
    packet->pkt.user_id = xmalloc_clear(sizeof *packet->pkt.user_id
					+ EXTRA_UID_NAME_SPACE);
    packet->pkt.user_id->ref=1;
    packet->pkt.user_id->attrib_data = xmalloc(pktlen);
    packet->pkt.user_id->attrib_len = pktlen;

    p = packet->pkt.user_id->attrib_data;
    for( ; pktlen; pktlen--, p++ )
	*p = iobuf_get_noeof(inp);

    /* Now parse out the individual attribute subpackets.  This is
       somewhat pointless since there is only one currently defined
       attribute type (jpeg), but it is correct by the spec. */
    parse_attribute_subpkts(packet->pkt.user_id);

    make_attribute_uidname(packet->pkt.user_id, EXTRA_UID_NAME_SPACE);

    if( list_mode ) {
	fprintf (listfp, ":attribute packet: %s\n", packet->pkt.user_id->name );
    }
    return 0;
}


static int
parse_comment( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
{
    byte *p;

    /* Cap comment packet at a reasonable value to avoid an integer
       overflow in the malloc below.  Comment packets are actually not
       anymore define my OpenPGP and we even stopped to use our
       private comment packet. */
    if (pktlen > MAX_COMMENT_PACKET_LENGTH)
      {
	log_error ("packet(%d) too large\n", pkttype);
	iobuf_skip_rest (inp, pktlen, 0);
	return G10ERR_INVALID_PACKET;
      }
    packet->pkt.comment = xmalloc(sizeof *packet->pkt.comment + pktlen - 1);
    packet->pkt.comment->len = pktlen;
    p = packet->pkt.comment->data;
    for( ; pktlen; pktlen--, p++ )
	*p = iobuf_get_noeof(inp);

    if( list_mode ) {
	int n = packet->pkt.comment->len;
	fprintf (listfp, ":%scomment packet: \"", pkttype == PKT_OLD_COMMENT?
					 "OpenPGP draft " : "GnuPG " );
	for(p=packet->pkt.comment->data; n; p++, n-- ) {
	    if( *p >= ' ' && *p <= 'z' )
		putc (*p, listfp);
	    else
		fprintf (listfp, "\\x%02x", *p );
	}
	fprintf (listfp, "\"\n");
    }
    return 0;
}


static void
parse_trust( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *pkt )
{
  int c;

  (void)pkttype;

  pkt->pkt.ring_trust = xmalloc( sizeof *pkt->pkt.ring_trust );
  if (pktlen)
    {
      c = iobuf_get_noeof(inp);
      pktlen--;
      pkt->pkt.ring_trust->trustval = c;
      pkt->pkt.ring_trust->sigcache = 0;
      if (!c && pktlen==1)
        {
          c = iobuf_get_noeof (inp);
          pktlen--;
          /* we require that bit 7 of the sigcache is 0 (easier eof handling)*/
          if ( !(c & 0x80) )
            pkt->pkt.ring_trust->sigcache = c;
        }
      if( list_mode )
	fprintf (listfp, ":trust packet: flag=%02x sigcache=%02x\n",
               pkt->pkt.ring_trust->trustval,
               pkt->pkt.ring_trust->sigcache);
    }
  else
    {
      pkt->pkt.ring_trust->trustval = 0;
      pkt->pkt.ring_trust->sigcache = 0;
      if (list_mode)
        fprintf (listfp, ":trust packet: empty\n");
    }
  iobuf_skip_rest (inp, pktlen, 0);
}


static int
parse_plaintext( IOBUF inp, int pkttype, unsigned long pktlen,
		 PACKET *pkt, int new_ctb, int partial )
{
    int rc = 0;
    int mode, namelen;
    PKT_plaintext *pt;
    byte *p;
    int c, i;

    if( !partial && pktlen < 6 ) {
	log_error("packet(%d) too short (%lu)\n", pkttype, (ulong)pktlen);
        rc = G10ERR_INVALID_PACKET;
	goto leave;
    }
    mode = iobuf_get_noeof(inp); if( pktlen ) pktlen--;
    namelen = iobuf_get_noeof(inp); if( pktlen ) pktlen--;
    /* Note that namelen will never exceeds 255 byte. */
    pt = pkt->pkt.plaintext = xmalloc(sizeof *pkt->pkt.plaintext + namelen -1);
    pt->new_ctb = new_ctb;
    pt->mode = mode;
    pt->namelen = namelen;
    pt->is_partial = partial;
    if( pktlen ) {
	for( i=0; pktlen > 4 && i < namelen; pktlen--, i++ )
	    pt->name[i] = iobuf_get_noeof(inp);
    }
    else {
	for( i=0; i < namelen; i++ )
	    if( (c=iobuf_get(inp)) == -1 )
		break;
	    else
		pt->name[i] = c;
    }
    pt->timestamp = read_32(inp); if( pktlen) pktlen -= 4;
    pt->len = pktlen;
    pt->buf = inp;
    pktlen = 0;

    if( list_mode ) {
	fprintf (listfp, ":literal data packet:\n"
	       "\tmode %c (%X), created %lu, name=\"",
	            mode >= ' ' && mode <'z'? mode : '?', mode,
		    (ulong)pt->timestamp );
	for(p=pt->name,i=0; i < namelen; p++, i++ ) {
	    if( *p >= ' ' && *p <= 'z' )
		putc (*p, listfp);
	    else
		fprintf (listfp, "\\x%02x", *p );
	}
	fprintf (listfp, "\",\n\traw data: ");
	if(partial)
	  fprintf (listfp, "unknown length\n");
	else
	  fprintf (listfp, "%lu bytes\n", (ulong)pt->len );
    }

  leave:
    return rc;
}


static int
parse_compressed( IOBUF inp, int pkttype, unsigned long pktlen,
		  PACKET *pkt, int new_ctb )
{
    PKT_compressed *zd;

    /* pktlen is here 0, but data follows
     * (this should be the last object in a file or
     *	the compress algorithm should know the length)
     */
    zd = pkt->pkt.compressed =	xmalloc(sizeof *pkt->pkt.compressed );
    zd->algorithm = iobuf_get_noeof(inp);
    zd->len = 0; /* not used */
    zd->new_ctb = new_ctb;
    zd->buf = inp;
    if( list_mode )
	fprintf (listfp, ":compressed packet: algo=%d\n", zd->algorithm);
    return 0;
}


static int
parse_encrypted( IOBUF inp, int pkttype, unsigned long pktlen,
		 PACKET *pkt, int new_ctb, int partial )
{
    int rc = 0;
    PKT_encrypted *ed;
    unsigned long orig_pktlen = pktlen;

    ed = pkt->pkt.encrypted =  xmalloc(sizeof *pkt->pkt.encrypted );
    ed->len = pktlen;
    /* we don't know the extralen which is (cipher_blocksize+2)
       because the algorithm ist not specified in this packet.
       However, it is only important to know this for some sanity
       checks on the packet length - it doesn't matter that we can't
       do it */
    ed->extralen = 0;
    ed->buf = NULL;
    ed->new_ctb = new_ctb;
    ed->is_partial = partial;
    ed->mdc_method = 0;
    if( pkttype == PKT_ENCRYPTED_MDC ) {
	/* fixme: add some pktlen sanity checks */
	int version;

	version = iobuf_get_noeof(inp);
        if (orig_pktlen)
            pktlen--;
	if( version != 1 ) {
	    log_error("encrypted_mdc packet with unknown version %d\n",
								version);
            /*skip_rest(inp, pktlen); should we really do this? */
            rc = G10ERR_INVALID_PACKET;
	    goto leave;
	}
	ed->mdc_method = DIGEST_ALGO_SHA1;
    }
    if( orig_pktlen && pktlen < 10 ) { /* actually this is blocksize+2 */
	log_error("packet(%d) too short\n", pkttype);
        rc = G10ERR_INVALID_PACKET;
	iobuf_skip_rest(inp, pktlen, partial);
	goto leave;
    }
    if( list_mode ) {
	if( orig_pktlen )
	    fprintf (listfp, ":encrypted data packet:\n\tlength: %lu\n",
                     orig_pktlen);
	else
	    fprintf (listfp, ":encrypted data packet:\n\tlength: unknown\n");
	if( ed->mdc_method )
	    fprintf (listfp, "\tmdc_method: %d\n", ed->mdc_method );
    }

    ed->buf = inp;

  leave:
    return rc;
}


static int
parse_mdc( IOBUF inp, int pkttype, unsigned long pktlen,
				   PACKET *pkt, int new_ctb )
{
    int rc = 0;
    PKT_mdc *mdc;
    byte *p;

    mdc = pkt->pkt.mdc=  xmalloc(sizeof *pkt->pkt.mdc );
    if( list_mode )
	fprintf (listfp, ":mdc packet: length=%lu\n", pktlen);
    if( !new_ctb || pktlen != 20 ) {
	log_error("mdc_packet with invalid encoding\n");
        rc = G10ERR_INVALID_PACKET;
	goto leave;
    }
    p = mdc->hash;
    for( ; pktlen; pktlen--, p++ )
	*p = iobuf_get_noeof(inp);

  leave:
    return rc;
}


/*
 * This packet is internally generated by GPG (by armor.c) to
 * transfer some information to the lower layer.  To make sure that
 * this packet is really a GPG faked one and not one comming from outside,
 * we first check that there is a unique tag in it.
 * The format of such a control packet is:
 *   n byte  session marker
 *   1 byte  control type CTRLPKT_xxxxx
 *   m byte  control data
 */

static int
parse_gpg_control( IOBUF inp, int pkttype,
		   unsigned long pktlen, PACKET *packet, int partial )
{
    byte *p;
    const byte *sesmark;
    size_t sesmarklen;
    int i;

    if ( list_mode )
        fprintf (listfp, ":packet 63: length %lu ",  pktlen);

    sesmark = get_session_marker ( &sesmarklen );
    if ( pktlen < sesmarklen+1 ) /* 1 is for the control bytes */
        goto skipit;
    for( i=0; i < sesmarklen; i++, pktlen-- ) {
	if ( sesmark[i] != iobuf_get_noeof(inp) )
            goto skipit;
    }
    if (pktlen > 4096)
      goto skipit; /* Definitely too large.  We skip it to avoid an
                      overflow in the malloc. */
    if ( list_mode )
        puts ("- gpg control packet");

    packet->pkt.gpg_control = xmalloc(sizeof *packet->pkt.gpg_control
                                      + pktlen - 1);
    packet->pkt.gpg_control->control = iobuf_get_noeof(inp); pktlen--;
    packet->pkt.gpg_control->datalen = pktlen;
    p = packet->pkt.gpg_control->data;
    for( ; pktlen; pktlen--, p++ )
	*p = iobuf_get_noeof(inp);

    return 0;

 skipit:
    if ( list_mode ) {
        int c;

        i=0;
        fprintf (listfp, "- private (rest length %lu)\n",  pktlen);
        if( partial ) {
            while( (c=iobuf_get(inp)) != -1 )
                dump_hex_line(c, &i);
        }
        else {
            for( ; pktlen; pktlen-- )
              {
                dump_hex_line ((c=iobuf_get (inp)), &i);
                if (c == -1)
                  break;
              }
        }
        putc ('\n', listfp);
    }
    iobuf_skip_rest(inp,pktlen, 0);
    return G10ERR_INVALID_PACKET;
}

/* create a gpg control packet to be used internally as a placeholder */
PACKET *
create_gpg_control( ctrlpkttype_t type, const byte *data, size_t datalen )
{
    PACKET *packet;
    byte *p;

    packet = xmalloc( sizeof *packet );
    init_packet(packet);
    packet->pkttype = PKT_GPG_CONTROL;
    packet->pkt.gpg_control = xmalloc(sizeof *packet->pkt.gpg_control
                                      + datalen - 1);
    packet->pkt.gpg_control->control = type;
    packet->pkt.gpg_control->datalen = datalen;
    p = packet->pkt.gpg_control->data;
    for( ; datalen; datalen--, p++ )
	*p = *data++;

    return packet;
}