File: unbound-anchor.c

package info (click to toggle)
unbound 1.6.0-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 20,444 kB
  • sloc: ansic: 79,862; sh: 5,040; yacc: 1,900; makefile: 1,315; python: 1,302; perl: 141
file content (2343 lines) | stat: -rw-r--r-- 61,984 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
/*
 * unbound-anchor.c - update the root anchor if necessary.
 *
 * Copyright (c) 2010, NLnet Labs. All rights reserved.
 *
 * This software is open source.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * 
 * Neither the name of the NLNET LABS nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * \file
 *
 * This file checks to see that the current 5011 keys work to prime the
 * current root anchor.  If not a certificate is used to update the anchor,
 * with RFC7958 https xml fetch.
 *
 * This is a concept solution for distribution of the DNSSEC root
 * trust anchor.  It is a small tool, called "unbound-anchor", that
 * runs before the main validator starts.  I.e. in the init script:
 * unbound-anchor; unbound.  Thus it is meant to run at system boot time.
 *
 * Management-Abstract:
 *    * first run: fill root.key file with hardcoded DS record.
 *    * mostly: use RFC5011 tracking, quick . DNSKEY UDP query.
 *    * failover: use RFC7958 builtin certificate, do https and update.
 * Special considerations:
 *    * 30-days RFC5011 timer saves a lot of https traffic.
 *    * DNSKEY probe must be NOERROR, saves a lot of https traffic.
 *    * fail if clock before sign date of the root, if cert expired.
 *    * if the root goes back to unsigned, deals with it.
 *
 * It has hardcoded the root DS anchors and the ICANN CA root certificate.
 * It allows with options to override those.  It also takes root-hints (it
 * has to do a DNS resolve), and also has hardcoded defaults for those.
 *
 * Once it starts, just before the validator starts, it quickly checks if
 * the root anchor file needs to be updated.  First it tries to use
 * RFC5011-tracking of the root key.  If that fails (and for 30-days since
 * last successful probe), then it attempts to update using the
 * certificate.  So most of the time, the RFC5011 tracking will work fine,
 * and within a couple milliseconds, the main daemon can start.  It will
 * have only probed the . DNSKEY, not done expensive https transfers on the
 * root infrastructure.
 *
 * If there is no root key in the root.key file, it bootstraps the
 * RFC5011-tracking with its builtin DS anchors; if that fails it
 * bootstraps the RFC5011-tracking using the certificate.  (again to avoid
 * https, and it is also faster).
 * 
 * It uses the XML file by converting it to DS records and writing that to the
 * key file.  Unbound can detect that the 'special comments' are gone, and
 * the file contains a list of normal DNSKEY/DS records, and uses that to
 * bootstrap 5011 (the KSK is made VALID).
 *
 * The certificate RFC7958 update is done by fetching root-anchors.xml and
 * root-anchors.p7s via SSL.  The HTTPS certificate can be logged but is
 * not validated (https for channel security; the security comes from the
 * certificate).  The 'data.iana.org' domain name A and AAAA are resolved
 * without DNSSEC.  It tries a random IP until the transfer succeeds.  It
 * then checks the p7s signature.
 *
 * On any failure, it leaves the root key file untouched.  The main
 * validator has to cope with it, it cannot fix things (So a failure does
 * not go 'without DNSSEC', no downgrade).  If it used its builtin stuff or
 * did the https, it exits with an exit code, so that this can trigger the
 * init script to log the event and potentially alert the operator that can
 * do a manual check.
 *
 * The date is also checked.  Before 2010-07-15 is a failure (root not
 * signed yet; avoids attacks on system clock).  The
 * last-successful-RFC5011-probe (if available) has to be more than 30 days
 * in the past (otherwise, RFC5011 should have worked).  This keeps
 * unnecessary https traffic down.  If the main certificate is expired, it
 * fails.
 *
 * The dates on the keys in the xml are checked (uses the libexpat xml
 * parser), only the valid ones are used to re-enstate RFC5011 tracking.
 * If 0 keys are valid, the zone has gone to insecure (a special marker is
 * written in the keyfile that tells the main validator daemon the zone is
 * insecure).
 *
 * Only the root ICANN CA is shipped, not the intermediate ones.  The
 * intermediate CAs are included in the p7s file that was downloaded.  (the
 * root cert is valid to 2028 and the intermediate to 2014, today).
 *
 * Obviously, the tool also has options so the operator can provide a new
 * keyfile, a new certificate and new URLs, and fresh root hints.  By
 * default it logs nothing on failure and success; it 'just works'.
 *
 */

#include "config.h"
#include "libunbound/unbound.h"
#include "sldns/rrdef.h"
#include "sldns/parseutil.h"
#include <expat.h>
#ifndef HAVE_EXPAT_H
#error "need libexpat to parse root-anchors.xml file."
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#ifdef HAVE_OPENSSL_SSL_H
#include <openssl/ssl.h>
#endif
#ifdef HAVE_OPENSSL_ERR_H
#include <openssl/err.h>
#endif
#ifdef HAVE_OPENSSL_RAND_H
#include <openssl/rand.h>
#endif
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/pem.h>

/** name of server in URL to fetch HTTPS from */
#define URLNAME "data.iana.org"
/** path on HTTPS server to xml file */
#define XMLNAME "root-anchors/root-anchors.xml"
/** path on HTTPS server to p7s file */
#define P7SNAME "root-anchors/root-anchors.p7s"
/** name of the signer of the certificate */
#define P7SIGNER "dnssec@iana.org"
/** port number for https access */
#define HTTPS_PORT 443

#ifdef USE_WINSOCK
/* sneakily reuse the the wsa_strerror function, on windows */
char* wsa_strerror(int err);
#endif

/** verbosity for this application */
static int verb = 0;

/** list of IP addresses */
struct ip_list {
	/** next in list */
	struct ip_list* next;
	/** length of addr */
	socklen_t len;
	/** address ready to connect to */
	struct sockaddr_storage addr;
	/** has the address been used */
	int used;
};

/** Give unbound-anchor usage, and exit (1). */
static void
usage(void)
{
	printf("Usage:	unbound-anchor [opts]\n");
	printf("	Setup or update root anchor. "
		"Most options have defaults.\n");
	printf("	Run this program before you start the validator.\n");
	printf("\n");
	printf("	The anchor and cert have default builtin content\n");
	printf("	if the file does not exist or is empty.\n");
	printf("\n");
	printf("-a file		root key file, default %s\n", ROOT_ANCHOR_FILE);
	printf("		The key is input and output for this tool.\n");
	printf("-c file		cert file, default %s\n", ROOT_CERT_FILE);
	printf("-l		list builtin key and cert on stdout\n");
	printf("-u name		server in https url, default %s\n", URLNAME);
	printf("-x path		pathname to xml in url, default %s\n", XMLNAME);
	printf("-s path		pathname to p7s in url, default %s\n", P7SNAME);
	printf("-n name		signer's subject emailAddress, default %s\n", P7SIGNER);
	printf("-4		work using IPv4 only\n");
	printf("-6		work using IPv6 only\n");
	printf("-f resolv.conf	use given resolv.conf to resolve -u name\n");
	printf("-r root.hints	use given root.hints to resolve -u name\n"
		"		builtin root hints are used by default\n");
	printf("-v		more verbose\n");
	printf("-C conf		debug, read config\n");
	printf("-P port		use port for https connect, default 443\n");
	printf("-F 		debug, force update with cert\n");
	printf("-h		show this usage help\n");
	printf("Version %s\n", PACKAGE_VERSION);
	printf("BSD licensed, see LICENSE in source package for details.\n");
	printf("Report bugs to %s\n", PACKAGE_BUGREPORT);
	exit(1);
}

/** return the built in root update certificate */
static const char*
get_builtin_cert(void)
{
	return
/* The ICANN CA fetched at 24 Sep 2010.  Valid to 2028 */
"-----BEGIN CERTIFICATE-----\n"
"MIIDdzCCAl+gAwIBAgIBATANBgkqhkiG9w0BAQsFADBdMQ4wDAYDVQQKEwVJQ0FO\n"
"TjEmMCQGA1UECxMdSUNBTk4gQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkxFjAUBgNV\n"
"BAMTDUlDQU5OIFJvb3QgQ0ExCzAJBgNVBAYTAlVTMB4XDTA5MTIyMzA0MTkxMloX\n"
"DTI5MTIxODA0MTkxMlowXTEOMAwGA1UEChMFSUNBTk4xJjAkBgNVBAsTHUlDQU5O\n"
"IENlcnRpZmljYXRpb24gQXV0aG9yaXR5MRYwFAYDVQQDEw1JQ0FOTiBSb290IENB\n"
"MQswCQYDVQQGEwJVUzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKDb\n"
"cLhPNNqc1NB+u+oVvOnJESofYS9qub0/PXagmgr37pNublVThIzyLPGCJ8gPms9S\n"
"G1TaKNIsMI7d+5IgMy3WyPEOECGIcfqEIktdR1YWfJufXcMReZwU4v/AdKzdOdfg\n"
"ONiwc6r70duEr1IiqPbVm5T05l1e6D+HkAvHGnf1LtOPGs4CHQdpIUcy2kauAEy2\n"
"paKcOcHASvbTHK7TbbvHGPB+7faAztABLoneErruEcumetcNfPMIjXKdv1V1E3C7\n"
"MSJKy+jAqqQJqjZoQGB0necZgUMiUv7JK1IPQRM2CXJllcyJrm9WFxY0c1KjBO29\n"
"iIKK69fcglKcBuFShUECAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8B\n"
"Af8EBAMCAf4wHQYDVR0OBBYEFLpS6UmDJIZSL8eZzfyNa2kITcBQMA0GCSqGSIb3\n"
"DQEBCwUAA4IBAQAP8emCogqHny2UYFqywEuhLys7R9UKmYY4suzGO4nkbgfPFMfH\n"
"6M+Zj6owwxlwueZt1j/IaCayoKU3QsrYYoDRolpILh+FPwx7wseUEV8ZKpWsoDoD\n"
"2JFbLg2cfB8u/OlE4RYmcxxFSmXBg0yQ8/IoQt/bxOcEEhhiQ168H2yE5rxJMt9h\n"
"15nu5JBSewrCkYqYYmaxyOC3WrVGfHZxVI7MpIFcGdvSb2a1uyuua8l0BKgk3ujF\n"
"0/wsHNeP22qNyVO+XVBzrM8fk8BSUFuiT/6tZTYXRtEt5aKQZgXbKU5dUF3jT9qg\n"
"j/Br5BZw3X/zd325TvnswzMC1+ljLzHnQGGk\n"
"-----END CERTIFICATE-----\n"
		;
}

/** return the built in root DS trust anchor */
static const char*
get_builtin_ds(void)
{
	return
". IN DS 19036 8 2 49AAC11D7B6F6446702E54A1607371607A1A41855200FD2CE1CDDE32F24E8FB5\n";
}

/** print hex data */
static void
print_data(const char* msg, const char* data, int len)
{
	int i;
	printf("%s: ", msg);
	for(i=0; i<len; i++) {
		printf(" %2.2x", (unsigned char)data[i]);
	}
	printf("\n");
}

/** print ub context creation error and exit */
static void
ub_ctx_error_exit(struct ub_ctx* ctx, const char* str, const char* str2)
{
	ub_ctx_delete(ctx);
	if(str && str2 && verb) printf("%s: %s\n", str, str2);
	if(verb) printf("error: could not create unbound resolver context\n");
	exit(0);
}

/**
 * Create a new unbound context with the commandline settings applied
 */
static struct ub_ctx* 
create_unbound_context(const char* res_conf, const char* root_hints,
	const char* debugconf, int ip4only, int ip6only)
{
	int r;
	struct ub_ctx* ctx = ub_ctx_create();
	if(!ctx) {
		if(verb) printf("out of memory\n");
		exit(0);
	}
	/* do not waste time and network traffic to fetch extra nameservers */
	r = ub_ctx_set_option(ctx, "target-fetch-policy:", "0 0 0 0 0");
	if(r && verb) printf("ctx targetfetchpolicy: %s\n", ub_strerror(r));
	/* read config file first, so its settings can be overridden */
	if(debugconf) {
		r = ub_ctx_config(ctx, debugconf);
		if(r) ub_ctx_error_exit(ctx, debugconf, ub_strerror(r));
	}
	if(res_conf) {
		r = ub_ctx_resolvconf(ctx, res_conf);
		if(r) ub_ctx_error_exit(ctx, res_conf, ub_strerror(r));
	}
	if(root_hints) {
		r = ub_ctx_set_option(ctx, "root-hints:", root_hints);
		if(r) ub_ctx_error_exit(ctx, root_hints, ub_strerror(r));
	}
	if(ip4only) {
		r = ub_ctx_set_option(ctx, "do-ip6:", "no");
		if(r) ub_ctx_error_exit(ctx, "ip4only", ub_strerror(r));
	}
	if(ip6only) {
		r = ub_ctx_set_option(ctx, "do-ip4:", "no");
		if(r) ub_ctx_error_exit(ctx, "ip6only", ub_strerror(r));
	}
	return ctx;
}

/** printout certificate in detail */
static void
verb_cert(const char* msg, X509* x)
{
	if(verb == 0 || verb == 1) return;
	if(verb == 2) {
		if(msg) printf("%s\n", msg);
		X509_print_ex_fp(stdout, x, 0, (unsigned long)-1
			^(X509_FLAG_NO_SUBJECT
			|X509_FLAG_NO_ISSUER|X509_FLAG_NO_VALIDITY));
		return;
	}
	if(msg) printf("%s\n", msg);
	X509_print_fp(stdout, x);
}

/** printout certificates in detail */
static void
verb_certs(const char* msg, STACK_OF(X509)* sk)
{
	int i, num = sk_X509_num(sk);
	if(verb == 0 || verb == 1) return;
	for(i=0; i<num; i++) {
		printf("%s (%d/%d)\n", msg, i, num);
		verb_cert(NULL, sk_X509_value(sk, i));
	}
}

/** read certificates from a PEM bio */
static STACK_OF(X509)*
read_cert_bio(BIO* bio)
{
	STACK_OF(X509) *sk = sk_X509_new_null();
	if(!sk) {
		if(verb) printf("out of memory\n");
		exit(0);
	}
	while(!BIO_eof(bio)) {
		X509* x = PEM_read_bio_X509(bio, NULL, 0, NULL);
		if(x == NULL) {
			if(verb) {
				printf("failed to read X509\n");
			 	ERR_print_errors_fp(stdout);
			}
			continue;
		}
		if(!sk_X509_push(sk, x)) {
			if(verb) printf("out of memory\n");
			exit(0);
		}
	}
	return sk;
}

/* read the certificate file */
static STACK_OF(X509)*
read_cert_file(const char* file)
{
	STACK_OF(X509)* sk;
	FILE* in;
	int content = 0;
	char buf[128];
	if(file == NULL || strcmp(file, "") == 0) {
		return NULL;
	}
	sk = sk_X509_new_null();
	if(!sk) {
		if(verb) printf("out of memory\n");
		exit(0);
	}
	in = fopen(file, "r");
	if(!in) {
		if(verb) printf("%s: %s\n", file, strerror(errno));
#ifndef S_SPLINT_S
		sk_X509_pop_free(sk, X509_free);
#endif
		return NULL;
	}
	while(!feof(in)) {
		X509* x = PEM_read_X509(in, NULL, 0, NULL);
		if(x == NULL) {
			if(verb) {
				printf("failed to read X509 file\n");
			 	ERR_print_errors_fp(stdout);
			}
			continue;
		}
		if(!sk_X509_push(sk, x)) {
			if(verb) printf("out of memory\n");
			fclose(in);
			exit(0);
		}
		content = 1;
		/* read away newline after --END CERT-- */
		if(!fgets(buf, (int)sizeof(buf), in))
			break;
	}
	fclose(in);
	if(!content) {
		if(verb) printf("%s is empty\n", file);
#ifndef S_SPLINT_S
		sk_X509_pop_free(sk, X509_free);
#endif
		return NULL;
	}
	return sk;
}

/** read certificates from the builtin certificate */
static STACK_OF(X509)*
read_builtin_cert(void)
{
	const char* builtin_cert = get_builtin_cert();
	STACK_OF(X509)* sk;
	BIO *bio;
	char* d = strdup(builtin_cert); /* to avoid const warnings in the
		changed prototype of BIO_new_mem_buf */
	if(!d) {
		if(verb) printf("out of memory\n");
		exit(0);
	}
	bio = BIO_new_mem_buf(d, (int)strlen(d));
	if(!bio) {
		if(verb) printf("out of memory\n");
		exit(0);
	}
	sk = read_cert_bio(bio);
	if(!sk) {
		if(verb) printf("internal error, out of memory\n");
		exit(0);
	}
	BIO_free(bio);
	free(d);
	return sk;
}

/** read update cert file or use builtin */
static STACK_OF(X509)*
read_cert_or_builtin(const char* file)
{
	STACK_OF(X509) *sk = read_cert_file(file);
	if(!sk) {
		if(verb) printf("using builtin certificate\n");
		sk = read_builtin_cert();
	}
	if(verb) printf("have %d trusted certificates\n", sk_X509_num(sk));
	verb_certs("trusted certificates", sk);
	return sk;
}

static void
do_list_builtin(void)
{
	const char* builtin_cert = get_builtin_cert();
	const char* builtin_ds = get_builtin_ds();
	printf("%s\n", builtin_ds);
	printf("%s\n", builtin_cert);
	exit(0);
}

/** printout IP address with message */
static void
verb_addr(const char* msg, struct ip_list* ip)
{
	if(verb) {
		char out[100];
		void* a = &((struct sockaddr_in*)&ip->addr)->sin_addr;
		if(ip->len != (socklen_t)sizeof(struct sockaddr_in))
			a = &((struct sockaddr_in6*)&ip->addr)->sin6_addr;

		if(inet_ntop((int)((struct sockaddr_in*)&ip->addr)->sin_family,
			a, out, (socklen_t)sizeof(out))==0)
			printf("%s (inet_ntop error)\n", msg);
		else printf("%s %s\n", msg, out);
	}
}

/** free ip_list */
static void
ip_list_free(struct ip_list* p)
{
	struct ip_list* np;
	while(p) {
		np = p->next;
		free(p);
		p = np;
	}
}

/** create ip_list entry for a RR record */
static struct ip_list*
RR_to_ip(int tp, char* data, int len, int port)
{
	struct ip_list* ip = (struct ip_list*)calloc(1, sizeof(*ip));
	uint16_t p = (uint16_t)port;
	if(tp == LDNS_RR_TYPE_A) {
		struct sockaddr_in* sa = (struct sockaddr_in*)&ip->addr;
		ip->len = (socklen_t)sizeof(*sa);
		sa->sin_family = AF_INET;
		sa->sin_port = (in_port_t)htons(p);
		if(len != (int)sizeof(sa->sin_addr)) {
			if(verb) printf("skipped badly formatted A\n");
			free(ip);
			return NULL;
		}
		memmove(&sa->sin_addr, data, sizeof(sa->sin_addr));

	} else if(tp == LDNS_RR_TYPE_AAAA) {
		struct sockaddr_in6* sa = (struct sockaddr_in6*)&ip->addr;
		ip->len = (socklen_t)sizeof(*sa);
		sa->sin6_family = AF_INET6;
		sa->sin6_port = (in_port_t)htons(p);
		if(len != (int)sizeof(sa->sin6_addr)) {
			if(verb) printf("skipped badly formatted AAAA\n");
			free(ip);
			return NULL;
		}
		memmove(&sa->sin6_addr, data, sizeof(sa->sin6_addr));
	} else {
		if(verb) printf("internal error: bad type in RRtoip\n");
		free(ip);
		return NULL;
	}
	verb_addr("resolved server address", ip);
	return ip;
}

/** Resolve name, type, class and add addresses to iplist */
static void
resolve_host_ip(struct ub_ctx* ctx, const char* host, int port, int tp, int cl,
	struct ip_list** head)
{
	struct ub_result* res = NULL;
	int r;
	int i;

	r = ub_resolve(ctx, host, tp, cl, &res);
	if(r) {
		if(verb) printf("error: resolve %s %s: %s\n", host,
			(tp==LDNS_RR_TYPE_A)?"A":"AAAA", ub_strerror(r));
		return;
	}
	if(!res) {
		if(verb) printf("out of memory\n");
		ub_ctx_delete(ctx);
		exit(0);
	}
	if(!res->havedata || res->rcode || !res->data) {
		if(verb) printf("resolve %s %s: no result\n", host,
			(tp==LDNS_RR_TYPE_A)?"A":"AAAA");
		return;
	}
	for(i = 0; res->data[i]; i++) {
		struct ip_list* ip = RR_to_ip(tp, res->data[i], res->len[i],
			port);
		if(!ip) continue;
		ip->next = *head;
		*head = ip;
	}
	ub_resolve_free(res);
}

/** parse a text IP address into a sockaddr */
static struct ip_list*
parse_ip_addr(const char* str, int port)
{
	socklen_t len = 0;
	union {
		struct sockaddr_in6 a6;
		struct sockaddr_in a;
	} addr;
	struct ip_list* ip;
	uint16_t p = (uint16_t)port;
	memset(&addr, 0, sizeof(addr));

	if(inet_pton(AF_INET6, str, &addr.a6.sin6_addr) > 0) {
		/* it is an IPv6 */
		addr.a6.sin6_family = AF_INET6;
		addr.a6.sin6_port = (in_port_t)htons(p);
		len = (socklen_t)sizeof(addr.a6);
	}
	if(inet_pton(AF_INET, str, &addr.a.sin_addr) > 0) {
		/* it is an IPv4 */
		addr.a.sin_family = AF_INET;
		addr.a.sin_port = (in_port_t)htons(p);
		len = (socklen_t)sizeof(struct sockaddr_in);
	}
	if(!len) return NULL;
	ip = (struct ip_list*)calloc(1, sizeof(*ip));
	if(!ip) {
		if(verb) printf("out of memory\n");
		exit(0);
	}
	ip->len = len;
	memmove(&ip->addr, &addr, len);
	if(verb) printf("server address is %s\n", str);
	return ip;
}

/**
 * Resolve a domain name (even though the resolver is down and there is
 * no trust anchor).  Without DNSSEC validation.
 * @param host: the name to resolve.
 * 	If this name is an IP4 or IP6 address this address is returned.
 * @param port: the port number used for the returned IP structs.
 * @param res_conf: resolv.conf (if any).
 * @param root_hints: root hints (if any).
 * @param debugconf: unbound.conf for debugging options.
 * @param ip4only: use only ip4 for resolve and only lookup A
 * @param ip6only: use only ip6 for resolve and only lookup AAAA
 * 	default is to lookup A and AAAA using ip4 and ip6.
 * @return list of IP addresses.
 */
static struct ip_list*
resolve_name(const char* host, int port, const char* res_conf,
	const char* root_hints, const char* debugconf, int ip4only, int ip6only)
{
	struct ub_ctx* ctx;
	struct ip_list* list = NULL;
	/* first see if name is an IP address itself */
	if( (list=parse_ip_addr(host, port)) ) {
		return list;
	}
	
	/* create resolver context */
	ctx = create_unbound_context(res_conf, root_hints, debugconf,
        	ip4only, ip6only);

	/* try resolution of A */
	if(!ip6only) {
		resolve_host_ip(ctx, host, port, LDNS_RR_TYPE_A,
			LDNS_RR_CLASS_IN, &list);
	}

	/* try resolution of AAAA */
	if(!ip4only) {
		resolve_host_ip(ctx, host, port, LDNS_RR_TYPE_AAAA,
			LDNS_RR_CLASS_IN, &list);
	}

	ub_ctx_delete(ctx);
	if(!list) {
		if(verb) printf("%s has no IP addresses I can use\n", host);
		exit(0);
	}
	return list;
}

/** clear used flags */
static void
wipe_ip_usage(struct ip_list* p)
{
	while(p) {
		p->used = 0;
		p = p->next;
	}
}

/** cound unused IPs */
static int
count_unused(struct ip_list* p)
{
	int num = 0;
	while(p) {
		if(!p->used) num++;
		p = p->next;
	}
	return num;
}

/** pick random unused element from IP list */
static struct ip_list*
pick_random_ip(struct ip_list* list)
{
	struct ip_list* p = list;
	int num = count_unused(list);
	int sel;
	if(num == 0) return NULL;
	/* not perfect, but random enough */
	sel = (int)arc4random_uniform((uint32_t)num);
	/* skip over unused elements that we did not select */
	while(sel > 0 && p) {
		if(!p->used) sel--;
		p = p->next;
	}
	/* find the next unused element */
	while(p && p->used)
		p = p->next;
	if(!p) return NULL; /* robustness */
	return p;
}

/** close the fd */
static void
fd_close(int fd)
{
#ifndef USE_WINSOCK
	close(fd);
#else
	closesocket(fd);
#endif
}

/** printout socket errno */
static void
print_sock_err(const char* msg)
{
#ifndef USE_WINSOCK
	if(verb) printf("%s: %s\n", msg, strerror(errno));
#else
	if(verb) printf("%s: %s\n", msg, wsa_strerror(WSAGetLastError()));
#endif
}

/** connect to IP address */
static int
connect_to_ip(struct ip_list* ip)
{
	int fd;
	verb_addr("connect to", ip);
	fd = socket(ip->len==(socklen_t)sizeof(struct sockaddr_in)?
		AF_INET:AF_INET6, SOCK_STREAM, 0);
	if(fd == -1) {
		print_sock_err("socket");
		return -1;
	}
	if(connect(fd, (struct sockaddr*)&ip->addr, ip->len) < 0) {
		print_sock_err("connect");
		fd_close(fd);
		return -1;
	}
	return fd;
}

/** create SSL context */
static SSL_CTX*
setup_sslctx(void)
{
	SSL_CTX* sslctx = SSL_CTX_new(SSLv23_client_method());
	if(!sslctx) {
		if(verb) printf("SSL_CTX_new error\n");
		return NULL;
	}
	return sslctx;
}

/** initiate TLS on a connection */
static SSL*
TLS_initiate(SSL_CTX* sslctx, int fd)
{
	X509* x;
	int r;
	SSL* ssl = SSL_new(sslctx);
	if(!ssl) {
		if(verb) printf("SSL_new error\n");
		return NULL;
	}
	SSL_set_connect_state(ssl);
	(void)SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
	if(!SSL_set_fd(ssl, fd)) {
		if(verb) printf("SSL_set_fd error\n");
		SSL_free(ssl);
		return NULL;
	}
	while(1) {
		ERR_clear_error();
		if( (r=SSL_do_handshake(ssl)) == 1)
			break;
		r = SSL_get_error(ssl, r);
		if(r != SSL_ERROR_WANT_READ && r != SSL_ERROR_WANT_WRITE) {
			if(verb) printf("SSL handshake failed\n");
			SSL_free(ssl);
			return NULL;
		}
		/* wants to be called again */
	}
	x = SSL_get_peer_certificate(ssl);
	if(!x) {
		if(verb) printf("Server presented no peer certificate\n");
		SSL_free(ssl);
		return NULL;
	}
	verb_cert("server SSL certificate", x);
	X509_free(x);
	return ssl;
}

/** perform neat TLS shutdown */
static void
TLS_shutdown(int fd, SSL* ssl, SSL_CTX* sslctx)
{
	/* shutdown the SSL connection nicely */
	if(SSL_shutdown(ssl) == 0) {
		SSL_shutdown(ssl);
	}
	SSL_free(ssl);
	SSL_CTX_free(sslctx);
	fd_close(fd);
}

/** write a line over SSL */
static int
write_ssl_line(SSL* ssl, const char* str, const char* sec)
{
	char buf[1024];
	size_t l;
	if(sec) {
		snprintf(buf, sizeof(buf), str, sec);
	} else {
		snprintf(buf, sizeof(buf), "%s", str);
	}
	l = strlen(buf);
	if(l+2 >= sizeof(buf)) {
		if(verb) printf("line too long\n");
		return 0;
	}
	if(verb >= 2) printf("SSL_write: %s\n", buf);
	buf[l] = '\r';
	buf[l+1] = '\n';
	buf[l+2] = 0;
	/* add \r\n */
	if(SSL_write(ssl, buf, (int)strlen(buf)) <= 0) {
		if(verb) printf("could not SSL_write %s", str);
		return 0;
	}
	return 1;
}

/** process header line, check rcode and keeping track of size */
static int
process_one_header(char* buf, size_t* clen, int* chunked)
{
	if(verb>=2) printf("header: '%s'\n", buf);
	if(strncasecmp(buf, "HTTP/1.1 ", 9) == 0) {
		/* check returncode */
		if(buf[9] != '2') {
			if(verb) printf("bad status %s\n", buf+9);
			return 0;
		}
	} else if(strncasecmp(buf, "Content-Length: ", 16) == 0) {
		if(!*chunked)
			*clen = (size_t)atoi(buf+16);
	} else if(strncasecmp(buf, "Transfer-Encoding: chunked", 19+7) == 0) {
		*clen = 0;
		*chunked = 1;
	}
	return 1;
}

/** 
 * Read one line from SSL
 * zero terminates.
 * skips "\r\n" (but not copied to buf).
 * @param ssl: the SSL connection to read from (blocking).
 * @param buf: buffer to return line in.
 * @param len: size of the buffer.
 * @return 0 on error, 1 on success.
 */
static int
read_ssl_line(SSL* ssl, char* buf, size_t len)
{
	size_t n = 0;
	int r;
	int endnl = 0;
	while(1) {
		if(n >= len) {
			if(verb) printf("line too long\n");
			return 0;
		}
		if((r = SSL_read(ssl, buf+n, 1)) <= 0) {
			if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
				/* EOF */
				break;
			}
			if(verb) printf("could not SSL_read\n");
			return 0;
		}
		if(endnl && buf[n] == '\n') {
			break;
		} else if(endnl) {
			/* bad data */
			if(verb) printf("error: stray linefeeds\n");
			return 0;
		} else if(buf[n] == '\r') {
			/* skip \r, and also \n on the wire */
			endnl = 1;
			continue;
		} else if(buf[n] == '\n') {
			/* skip the \n, we are done */
			break;
		} else n++;
	}
	buf[n] = 0;
	return 1;
}

/** read http headers and process them */
static size_t
read_http_headers(SSL* ssl, size_t* clen)
{
	char buf[1024];
	int chunked = 0;
	*clen = 0;
	while(read_ssl_line(ssl, buf, sizeof(buf))) {
		if(buf[0] == 0)
			return 1;
		if(!process_one_header(buf, clen, &chunked))
			return 0;
	}
	return 0;
}

/** read a data chunk */
static char*
read_data_chunk(SSL* ssl, size_t len)
{
	size_t got = 0;
	int r;
	char* data;
	if(len >= 0xfffffff0)
		return NULL; /* to protect against integer overflow in malloc*/
	data = malloc(len+1);
	if(!data) {
		if(verb) printf("out of memory\n");
		return NULL;
	}
	while(got < len) {
		if((r = SSL_read(ssl, data+got, (int)(len-got))) <= 0) {
			if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
				/* EOF */
				if(verb) printf("could not SSL_read: unexpected EOF\n");
				free(data);
				return NULL;
			}
			if(verb) printf("could not SSL_read\n");
			free(data);
			return NULL;
		}
		if(verb >= 2) printf("at %d/%d\n", (int)got, (int)len);
		got += r;
	}
	if(verb>=2) printf("read %d data\n", (int)len);
	data[len] = 0;
	return data;
}

/** parse chunk header */
static int
parse_chunk_header(char* buf, size_t* result)
{
	char* e = NULL;
	size_t v = (size_t)strtol(buf, &e, 16);
	if(e == buf)
		return 0;
	*result = v;
	return 1;
}

/** read chunked data from connection */
static BIO*
do_chunked_read(SSL* ssl)
{
	char buf[1024];
	size_t len;
	char* body;
	BIO* mem = BIO_new(BIO_s_mem());
	if(verb>=3) printf("do_chunked_read\n");
	if(!mem) {
		if(verb) printf("out of memory\n");
		return NULL;
	}
	while(read_ssl_line(ssl, buf, sizeof(buf))) {
		/* read the chunked start line */
		if(verb>=2) printf("chunk header: %s\n", buf);
		if(!parse_chunk_header(buf, &len)) {
			BIO_free(mem);
			if(verb>=3) printf("could not parse chunk header\n");
			return NULL;
		}
		if(verb>=2) printf("chunk len: %d\n", (int)len);
		/* are we done? */
		if(len == 0) {
			char z = 0;
			/* skip end-of-chunk-trailer lines,
			 * until the empty line after that */
			do {
				if(!read_ssl_line(ssl, buf, sizeof(buf))) {
					BIO_free(mem);
					return NULL;
				}
			} while (strlen(buf) > 0);
			/* end of chunks, zero terminate it */
			if(BIO_write(mem, &z, 1) <= 0) {
				if(verb) printf("out of memory\n");
				BIO_free(mem);
				return NULL;
			}
			return mem;
		}
		/* read the chunked body */
		body = read_data_chunk(ssl, len);
		if(!body) {
			BIO_free(mem);
			return NULL;
		}
		if(BIO_write(mem, body, (int)len) <= 0) {
			if(verb) printf("out of memory\n");
			free(body);
			BIO_free(mem);
			return NULL;
		}
		free(body);
		/* skip empty line after data chunk */
		if(!read_ssl_line(ssl, buf, sizeof(buf))) {
			BIO_free(mem);
			return NULL;
		}
	}
	BIO_free(mem);
	return NULL;
}

/** start HTTP1.1 transaction on SSL */
static int
write_http_get(SSL* ssl, const char* pathname, const char* urlname)
{
	if(write_ssl_line(ssl, "GET /%s HTTP/1.1", pathname) &&
	   write_ssl_line(ssl, "Host: %s", urlname) &&
	   write_ssl_line(ssl, "User-Agent: unbound-anchor/%s",
	   	PACKAGE_VERSION) &&
	   /* We do not really do multiple queries per connection,
	    * but this header setting is also not needed.
	    * write_ssl_line(ssl, "Connection: close", NULL) &&*/
	   write_ssl_line(ssl, "", NULL)) {
		return 1;
	}
	return 0;
}

/** read chunked data and zero terminate; len is without zero */
static char*
read_chunked_zero_terminate(SSL* ssl, size_t* len)
{
	/* do the chunked version */
	BIO* tmp = do_chunked_read(ssl);
	char* data, *d = NULL;
	size_t l;
	if(!tmp) {
		if(verb) printf("could not read from https\n");
		return NULL;
	}
	l = (size_t)BIO_get_mem_data(tmp, &d);
	if(verb>=2) printf("chunked data is %d\n", (int)l);
	if(l == 0 || d == NULL) {
		if(verb) printf("out of memory\n");
		return NULL;
	}
	*len = l-1;
	data = (char*)malloc(l);
	if(data == NULL) {
		if(verb) printf("out of memory\n");
		return NULL;
	}
	memcpy(data, d, l);
	BIO_free(tmp);
	return data;
}

/** read HTTP result from SSL */
static BIO*
read_http_result(SSL* ssl)
{
	size_t len = 0;
	char* data;
	BIO* m;
	if(!read_http_headers(ssl, &len)) {
		return NULL;
	}
	if(len == 0) {
		data = read_chunked_zero_terminate(ssl, &len);
	} else {
		data = read_data_chunk(ssl, len);
	}
	if(!data) return NULL;
	if(verb >= 4) print_data("read data", data, (int)len);
	m = BIO_new_mem_buf(data, (int)len);
	if(!m) {
		if(verb) printf("out of memory\n");
		exit(0);
	}
	return m;
}

/** https to an IP addr, return BIO with pathname or NULL */
static BIO*
https_to_ip(struct ip_list* ip, const char* pathname, const char* urlname)
{
	int fd;
	SSL* ssl;
	BIO* bio;
	SSL_CTX* sslctx = setup_sslctx();
	if(!sslctx) {
		return NULL;
	}
	fd = connect_to_ip(ip);
	if(fd == -1) {
		SSL_CTX_free(sslctx);
		return NULL;
	}
	ssl = TLS_initiate(sslctx, fd);
	if(!ssl) {
		SSL_CTX_free(sslctx);
		fd_close(fd);
		return NULL;
	}
	if(!write_http_get(ssl, pathname, urlname)) {
		if(verb) printf("could not write to server\n");
		SSL_free(ssl);
		SSL_CTX_free(sslctx);
		fd_close(fd);
		return NULL;
	}
	bio = read_http_result(ssl);
	TLS_shutdown(fd, ssl, sslctx);
	return bio;
}

/**
 * Do a HTTPS, HTTP1.1 over TLS, to fetch a file
 * @param ip_list: list of IP addresses to use to fetch from.
 * @param pathname: pathname of file on server to GET.
 * @param urlname: name to pass as the virtual host for this request.
 * @return a memory BIO with the file in it.
 */
static BIO*
https(struct ip_list* ip_list, const char* pathname, const char* urlname)
{
	struct ip_list* ip;
	BIO* bio = NULL;
	/* try random address first, and work through the list */
	wipe_ip_usage(ip_list);
	while( (ip = pick_random_ip(ip_list)) ) {
		ip->used = 1;
		bio = https_to_ip(ip, pathname, urlname);
		if(bio) break;
	}
	if(!bio) {
		if(verb) printf("could not fetch %s\n", pathname);
		exit(0);
	} else {
		if(verb) printf("fetched %s (%d bytes)\n",
			pathname, (int)BIO_ctrl_pending(bio));
	}
	return bio;
}

/** free up a downloaded file BIO */
static void
free_file_bio(BIO* bio)
{
	char* pp = NULL;
	(void)BIO_reset(bio);
	(void)BIO_get_mem_data(bio, &pp);
	free(pp);
	BIO_free(bio);
}

/** XML parse private data during the parse */
struct xml_data {
	/** the parser, reference */
	XML_Parser parser;
	/** the current tag; malloced; or NULL outside of tags */
	char* tag;
	/** current date to use during the parse */
	time_t date;
	/** number of keys usefully read in */
	int num_keys;
	/** the compiled anchors as DS records */
	BIO* ds;

	/** do we want to use this anchor? */
	int use_key;
	/** the current anchor: Zone */
	BIO* czone;
	/** the current anchor: KeyTag */
	BIO* ctag;
	/** the current anchor: Algorithm */
	BIO* calgo;
	/** the current anchor: DigestType */
	BIO* cdigtype;
	/** the current anchor: Digest*/
	BIO* cdigest;
};

/** The BIO for the tag */
static BIO*
xml_selectbio(struct xml_data* data, const char* tag)
{
	BIO* b = NULL;
	if(strcasecmp(tag, "KeyTag") == 0)
		b = data->ctag;
	else if(strcasecmp(tag, "Algorithm") == 0)
		b = data->calgo;
	else if(strcasecmp(tag, "DigestType") == 0)
		b = data->cdigtype;
	else if(strcasecmp(tag, "Digest") == 0)
		b = data->cdigest;
	return b;
}

/**
 * XML handle character data, the data inside an element.
 * @param userData: xml_data structure
 * @param s: the character data.  May not all be in one callback.
 * 	NOT zero terminated.
 * @param len: length of this part of the data.
 */
static void
xml_charhandle(void *userData, const XML_Char *s, int len)
{
	struct xml_data* data = (struct xml_data*)userData;
	BIO* b = NULL;
	/* skip characters outside of elements */
	if(!data->tag)
		return;
	if(verb>=4) {
		int i;
		printf("%s%s charhandle: '",
			data->use_key?"use ":"",
			data->tag?data->tag:"none");
		for(i=0; i<len; i++)
			printf("%c", s[i]);
		printf("'\n");
	}
	if(strcasecmp(data->tag, "Zone") == 0) {
		if(BIO_write(data->czone, s, len) < 0) {
			if(verb) printf("out of memory in BIO_write\n");
			exit(0);
		}
		return;
	}
	/* only store if key is used */
	if(!data->use_key)
		return;
	b = xml_selectbio(data, data->tag);
	if(b) {
		if(BIO_write(b, s, len) < 0) {
			if(verb) printf("out of memory in BIO_write\n");
			exit(0);
		}
	}
}

/**
 * XML fetch value of particular attribute(by name) or NULL if not present.
 * @param atts: attribute array (from xml_startelem).
 * @param name: name of attribute to look for.
 * @return the value or NULL. (ptr into atts).
 */
static const XML_Char*
find_att(const XML_Char **atts, const XML_Char* name)
{
	int i;
	for(i=0; atts[i]; i+=2) {
		if(strcasecmp(atts[i], name) == 0)
			return atts[i+1];
	}
	return NULL;
}

/**
 * XML convert DateTime element to time_t.
 * [-]CCYY-MM-DDThh:mm:ss[Z|(+|-)hh:mm]
 * (with optional .ssssss fractional seconds)
 * @param str: the string
 * @return a time_t representation or 0 on failure.
 */
static time_t
xml_convertdate(const char* str)
{
	time_t t = 0;
	struct tm tm;
	const char* s;
	/* for this application, ignore minus in front;
	 * only positive dates are expected */
	s = str;
	if(s[0] == '-') s++;
	memset(&tm, 0, sizeof(tm));
	/* parse initial content of the string (lots of whitespace allowed) */
	s = strptime(s, "%t%Y%t-%t%m%t-%t%d%tT%t%H%t:%t%M%t:%t%S%t", &tm);
	if(!s) {
		if(verb) printf("xml_convertdate parse failure %s\n", str);
		return 0;
	}
	/* parse remainder of date string */
	if(*s == '.') {
		/* optional '.' and fractional seconds */
		int frac = 0, n = 0;
		if(sscanf(s+1, "%d%n", &frac, &n) < 1) {
			if(verb) printf("xml_convertdate f failure %s\n", str);
			return 0;
		}
		/* fraction is not used, time_t has second accuracy */
		s++;
		s+=n;
	}
	if(*s == 'Z' || *s == 'z') {
		/* nothing to do for this */
		s++;
	} else if(*s == '+' || *s == '-') {
		/* optional timezone spec: Z or +hh:mm or -hh:mm */
		int hr = 0, mn = 0, n = 0;
		if(sscanf(s+1, "%d:%d%n", &hr, &mn, &n) < 2) {
			if(verb) printf("xml_convertdate tz failure %s\n", str);
			return 0;
		}
		if(*s == '+') {
			tm.tm_hour += hr;
			tm.tm_min += mn;
		} else {
			tm.tm_hour -= hr;
			tm.tm_min -= mn;
		}
		s++;
		s += n;
	}
	if(*s != 0) {
		/* not ended properly */
		/* but ignore, (lenient) */
	}

	t = sldns_mktime_from_utc(&tm);
	if(t == (time_t)-1) {
		if(verb) printf("xml_convertdate mktime failure\n");
		return 0;
	}
	return t;
}

/**
 * XML handle the KeyDigest start tag, check validity periods.
 */
static void
handle_keydigest(struct xml_data* data, const XML_Char **atts)
{
	data->use_key = 0;
	if(find_att(atts, "validFrom")) {
		time_t from = xml_convertdate(find_att(atts, "validFrom"));
		if(from == 0) {
			if(verb) printf("error: xml cannot be parsed\n");
			exit(0);
		}
		if(data->date < from)
			return;
	}
	if(find_att(atts, "validUntil")) {
		time_t until = xml_convertdate(find_att(atts, "validUntil"));
		if(until == 0) {
			if(verb) printf("error: xml cannot be parsed\n");
			exit(0);
		}
		if(data->date > until)
			return;
	}
	/* yes we want to use this key */
	data->use_key = 1;
	(void)BIO_reset(data->ctag);
	(void)BIO_reset(data->calgo);
	(void)BIO_reset(data->cdigtype);
	(void)BIO_reset(data->cdigest);
}

/** See if XML element equals the zone name */
static int
xml_is_zone_name(BIO* zone, const char* name)
{
	char buf[1024];
	char* z = NULL;
	long zlen;
	(void)BIO_seek(zone, 0);
	zlen = BIO_get_mem_data(zone, &z);
	if(!zlen || !z) return 0;
	/* zero terminate */
	if(zlen >= (long)sizeof(buf)) return 0;
	memmove(buf, z, (size_t)zlen);
	buf[zlen] = 0;
	/* compare */
	return (strncasecmp(buf, name, strlen(name)) == 0);
}

/** 
 * XML start of element. This callback is called whenever an XML tag starts.
 * XML_Char is UTF8.
 * @param userData: the xml_data structure.
 * @param name: the tag that starts.
 * @param atts: array of strings, pairs of attr = value, ends with NULL.
 * 	i.e. att[0]="att[1]" att[2]="att[3]" att[4]isNull
 */
static void
xml_startelem(void *userData, const XML_Char *name, const XML_Char **atts)
{
	struct xml_data* data = (struct xml_data*)userData;
	BIO* b;
	if(verb>=4) printf("xml tag start '%s'\n", name);
	free(data->tag);
	data->tag = strdup(name);
	if(!data->tag) {
		if(verb) printf("out of memory\n");
		exit(0);
	}
	if(verb>=4) {
		int i;
		for(i=0; atts[i]; i+=2) {
			printf("  %s='%s'\n", atts[i], atts[i+1]);
		}
	}
	/* handle attributes to particular types */
	if(strcasecmp(name, "KeyDigest") == 0) {
		handle_keydigest(data, atts);
		return;
	} else if(strcasecmp(name, "Zone") == 0) {
		(void)BIO_reset(data->czone);
		return;
	}

	/* for other types we prepare to pick up the data */
	if(!data->use_key)
		return;
	b = xml_selectbio(data, data->tag);
	if(b) {
		/* empty it */
		(void)BIO_reset(b);
	}
}

/** Append str to bio */
static void
xml_append_str(BIO* b, const char* s)
{
	if(BIO_write(b, s, (int)strlen(s)) < 0) {
		if(verb) printf("out of memory in BIO_write\n");
		exit(0);
	}
}

/** Append bio to bio */
static void
xml_append_bio(BIO* b, BIO* a)
{
	char* z = NULL;
	long i, len;
	(void)BIO_seek(a, 0);
	len = BIO_get_mem_data(a, &z);
	if(!len || !z) {
		if(verb) printf("out of memory in BIO_write\n");
		exit(0);
	}
	/* remove newlines in the data here */
	for(i=0; i<len; i++) {
		if(z[i] == '\r' || z[i] == '\n')
			z[i] = ' ';
	}
	/* write to BIO */
	if(BIO_write(b, z, len) < 0) {
		if(verb) printf("out of memory in BIO_write\n");
		exit(0);
	}
}

/** write the parsed xml-DS to the DS list */
static void
xml_append_ds(struct xml_data* data)
{
	/* write DS to accumulated DS */
	xml_append_str(data->ds, ". IN DS ");
	xml_append_bio(data->ds, data->ctag);
	xml_append_str(data->ds, " ");
	xml_append_bio(data->ds, data->calgo);
	xml_append_str(data->ds, " ");
	xml_append_bio(data->ds, data->cdigtype);
	xml_append_str(data->ds, " ");
	xml_append_bio(data->ds, data->cdigest);
	xml_append_str(data->ds, "\n");
	data->num_keys++;
}

/**
 * XML end of element. This callback is called whenever an XML tag ends.
 * XML_Char is UTF8.
 * @param userData: the xml_data structure
 * @param name: the tag that ends.
 */
static void
xml_endelem(void *userData, const XML_Char *name)
{
	struct xml_data* data = (struct xml_data*)userData;
	if(verb>=4) printf("xml tag end   '%s'\n", name);
	free(data->tag);
	data->tag = NULL;
	if(strcasecmp(name, "KeyDigest") == 0) {
		if(data->use_key)
			xml_append_ds(data);
		data->use_key = 0;
	} else if(strcasecmp(name, "Zone") == 0) {
		if(!xml_is_zone_name(data->czone, ".")) {
			if(verb) printf("xml not for the right zone\n");
			exit(0);
		}
	}
}

/* Stop the parser when an entity declaration is encountered. For safety. */
static void
xml_entitydeclhandler(void *userData,
	const XML_Char *ATTR_UNUSED(entityName),
	int ATTR_UNUSED(is_parameter_entity),
	const XML_Char *ATTR_UNUSED(value), int ATTR_UNUSED(value_length),
	const XML_Char *ATTR_UNUSED(base),
	const XML_Char *ATTR_UNUSED(systemId),
	const XML_Char *ATTR_UNUSED(publicId),
	const XML_Char *ATTR_UNUSED(notationName))
{
#if HAVE_DECL_XML_STOPPARSER
	(void)XML_StopParser((XML_Parser)userData, XML_FALSE);
#else
	(void)userData;
#endif
}

/**
 * XML parser setup of the callbacks for the tags
 */
static void
xml_parse_setup(XML_Parser parser, struct xml_data* data, time_t now)
{
	char buf[1024];
	memset(data, 0, sizeof(*data));
	XML_SetUserData(parser, data);
	data->parser = parser;
	data->date = now;
	data->ds = BIO_new(BIO_s_mem());
	data->ctag = BIO_new(BIO_s_mem());
	data->czone = BIO_new(BIO_s_mem());
	data->calgo = BIO_new(BIO_s_mem());
	data->cdigtype = BIO_new(BIO_s_mem());
	data->cdigest = BIO_new(BIO_s_mem());
	if(!data->ds || !data->ctag || !data->calgo || !data->czone ||
		!data->cdigtype || !data->cdigest) {
		if(verb) printf("out of memory\n");
		exit(0);
	}
	snprintf(buf, sizeof(buf), "; created by unbound-anchor on %s",
		ctime(&now));
	if(BIO_write(data->ds, buf, (int)strlen(buf)) < 0) {
		if(verb) printf("out of memory\n");
		exit(0);
	}
	XML_SetEntityDeclHandler(parser, xml_entitydeclhandler);
	XML_SetElementHandler(parser, xml_startelem, xml_endelem);
	XML_SetCharacterDataHandler(parser, xml_charhandle);
}

/**
 * Perform XML parsing of the root-anchors file
 * Its format description can be read here
 * https://data.iana.org/root-anchors/draft-icann-dnssec-trust-anchor.txt
 * It uses libexpat.
 * @param xml: BIO with xml data.
 * @param now: the current time for checking DS validity periods.
 * @return memoryBIO with the DS data in zone format.
 * 	or NULL if the zone is insecure.
 * 	(It exit()s on error)
 */
static BIO*
xml_parse(BIO* xml, time_t now)
{
	char* pp;
	int len;
	XML_Parser parser;
	struct xml_data data;

	parser = XML_ParserCreate(NULL);
	if(!parser) {
		if(verb) printf("could not XML_ParserCreate\n");
		exit(0);
	}

	/* setup callbacks */
	xml_parse_setup(parser, &data, now);

	/* parse it */
	(void)BIO_reset(xml);
	len = (int)BIO_get_mem_data(xml, &pp);
	if(!len || !pp) {
		if(verb) printf("out of memory\n");
		exit(0);
	}
	if(!XML_Parse(parser, pp, len, 1 /*isfinal*/ )) {
		const char *e = XML_ErrorString(XML_GetErrorCode(parser));
		if(verb) printf("XML_Parse failure %s\n", e?e:"");
		exit(0);
	}

	/* parsed */
	if(verb) printf("XML was parsed successfully, %d keys\n",
			data.num_keys);
	free(data.tag);
	XML_ParserFree(parser);

	if(verb >= 4) {
		(void)BIO_seek(data.ds, 0);
		len = BIO_get_mem_data(data.ds, &pp);
		printf("got DS bio %d: '", len);
		if(!fwrite(pp, (size_t)len, 1, stdout))
			/* compilers do not allow us to ignore fwrite .. */
			fprintf(stderr, "error writing to stdout\n");
		printf("'\n");
	}
	BIO_free(data.czone);
	BIO_free(data.ctag);
	BIO_free(data.calgo);
	BIO_free(data.cdigtype);
	BIO_free(data.cdigest);

	if(data.num_keys == 0) {
		/* the root zone seems to have gone insecure */
		BIO_free(data.ds);
		return NULL;
	} else {
		return data.ds;
	}
}

/* get key usage out of its extension, returns 0 if no key_usage extension */
static unsigned long
get_usage_of_ex(X509* cert)
{
	unsigned long val = 0;
	ASN1_BIT_STRING* s;
	if((s=X509_get_ext_d2i(cert, NID_key_usage, NULL, NULL))) {
		if(s->length > 0) {
			val = s->data[0];
			if(s->length > 1)
				val |= s->data[1] << 8;
		}
		ASN1_BIT_STRING_free(s);
	}
	return val;
}

/** get valid signers from the list of signers in the signature */
static STACK_OF(X509)*
get_valid_signers(PKCS7* p7, const char* p7signer)
{
	int i;
	STACK_OF(X509)* validsigners = sk_X509_new_null();
	STACK_OF(X509)* signers = PKCS7_get0_signers(p7, NULL, 0);
	unsigned long usage = 0;
	if(!validsigners) {
		if(verb) printf("out of memory\n");
		sk_X509_free(signers);
		return NULL;
	}
	if(!signers) {
		if(verb) printf("no signers in pkcs7 signature\n");
		sk_X509_free(validsigners);
		return NULL;
	}
	for(i=0; i<sk_X509_num(signers); i++) {
		X509_NAME* nm = X509_get_subject_name(
			sk_X509_value(signers, i));
		char buf[1024];
		if(!nm) {
			if(verb) printf("signer %d: cert has no subject name\n", i);
			continue;
		}
		if(verb && nm) {
			char* nmline = X509_NAME_oneline(nm, buf,
				(int)sizeof(buf));
			printf("signer %d: Subject: %s\n", i,
				nmline?nmline:"no subject");
			if(verb >= 3 && X509_NAME_get_text_by_NID(nm,
				NID_commonName, buf, (int)sizeof(buf)))
				printf("commonName: %s\n", buf);
			if(verb >= 3 && X509_NAME_get_text_by_NID(nm,
				NID_pkcs9_emailAddress, buf, (int)sizeof(buf)))
				printf("emailAddress: %s\n", buf);
		}
		if(verb) {
			int ku_loc = X509_get_ext_by_NID(
				sk_X509_value(signers, i), NID_key_usage, -1);
			if(verb >= 3 && ku_loc >= 0) {
				X509_EXTENSION *ex = X509_get_ext(
					sk_X509_value(signers, i), ku_loc);
				if(ex) {
					printf("keyUsage: ");
					X509V3_EXT_print_fp(stdout, ex, 0, 0);
					printf("\n");
				}
			}
		}
		if(!p7signer || strcmp(p7signer, "")==0) {
			/* there is no name to check, return all records */
			if(verb) printf("did not check commonName of signer\n");
		} else {
			if(!X509_NAME_get_text_by_NID(nm,
				NID_pkcs9_emailAddress,
				buf, (int)sizeof(buf))) {
				if(verb) printf("removed cert with no name\n");
				continue; /* no name, no use */
			}
			if(strcmp(buf, p7signer) != 0) {
				if(verb) printf("removed cert with wrong name\n");
				continue; /* wrong name, skip it */
			}
		}

		/* check that the key usage allows digital signatures
		 * (the p7s) */
		usage = get_usage_of_ex(sk_X509_value(signers, i));
		if(!(usage & KU_DIGITAL_SIGNATURE)) {
			if(verb) printf("removed cert with no key usage Digital Signature allowed\n");
			continue;
		}

		/* we like this cert, add it to our list of valid
		 * signers certificates */
		sk_X509_push(validsigners, sk_X509_value(signers, i));
	}
	sk_X509_free(signers);
	return validsigners;
}

/** verify a PKCS7 signature, false on failure */
static int
verify_p7sig(BIO* data, BIO* p7s, STACK_OF(X509)* trust, const char* p7signer)
{
	PKCS7* p7;
	X509_STORE *store = X509_STORE_new();
	STACK_OF(X509)* validsigners;
	int secure = 0;
	int i;
#ifdef X509_V_FLAG_CHECK_SS_SIGNATURE
	X509_VERIFY_PARAM* param = X509_VERIFY_PARAM_new();
	if(!param) {
		if(verb) printf("out of memory\n");
		X509_STORE_free(store);
		return 0;
	}
	/* do the selfcheck on the root certificate; it checks that the
	 * input is valid */
	X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CHECK_SS_SIGNATURE);
	if(store) X509_STORE_set1_param(store, param);
#endif
	if(!store) {
		if(verb) printf("out of memory\n");
#ifdef X509_V_FLAG_CHECK_SS_SIGNATURE
		X509_VERIFY_PARAM_free(param);
#endif
		return 0;
	}
#ifdef X509_V_FLAG_CHECK_SS_SIGNATURE
	X509_VERIFY_PARAM_free(param);
#endif

	(void)BIO_reset(p7s);
	(void)BIO_reset(data);

	/* convert p7s to p7 (the signature) */
	p7 = d2i_PKCS7_bio(p7s, NULL);
	if(!p7) {
		if(verb) printf("could not parse p7s signature file\n");
		X509_STORE_free(store);
		return 0;
	}
	if(verb >= 2) printf("parsed the PKCS7 signature\n");

	/* convert trust to trusted certificate store */
	for(i=0; i<sk_X509_num(trust); i++) {
		if(!X509_STORE_add_cert(store, sk_X509_value(trust, i))) {
			if(verb) printf("failed X509_STORE_add_cert\n");
			X509_STORE_free(store);
			PKCS7_free(p7);
			return 0;
		}
	}
	if(verb >= 2) printf("setup the X509_STORE\n");

	/* check what is in the Subject name of the certificates,
	 * and build a stack that contains only the right certificates */
	validsigners = get_valid_signers(p7, p7signer);
	if(!validsigners) {
			X509_STORE_free(store);
			PKCS7_free(p7);
			return 0;
	}
	if(PKCS7_verify(p7, validsigners, store, data, NULL, PKCS7_NOINTERN) == 1) {
		secure = 1;
		if(verb) printf("the PKCS7 signature verified\n");
	} else {
		if(verb) {
			ERR_print_errors_fp(stdout);
		}
	}

	sk_X509_free(validsigners);
	X509_STORE_free(store);
	PKCS7_free(p7);
	return secure;
}

/** write unsigned root anchor file, a 5011 revoked tp */
static void
write_unsigned_root(const char* root_anchor_file)
{
	FILE* out;
	time_t now = time(NULL);
	out = fopen(root_anchor_file, "w");
	if(!out) {
		if(verb) printf("%s: %s\n", root_anchor_file, strerror(errno));
		return;
	}
	if(fprintf(out, "; autotrust trust anchor file\n"
		";;REVOKED\n"
		";;id: . 1\n"
		"; This file was written by unbound-anchor on %s"
		"; It indicates that the root does not use DNSSEC\n"
		"; to restart DNSSEC overwrite this file with a\n"
		"; valid trustanchor or (empty-it and run unbound-anchor)\n"
		, ctime(&now)) < 0) {
		if(verb) printf("failed to write 'unsigned' to %s\n",
			root_anchor_file);
		if(verb && errno != 0) printf("%s\n", strerror(errno));
	}
	fflush(out);
#ifdef HAVE_FSYNC
	fsync(fileno(out));
#else
	FlushFileBuffers((HANDLE)_get_osfhandle(_fileno(out)));
#endif
	fclose(out);
}

/** write root anchor file */
static void
write_root_anchor(const char* root_anchor_file, BIO* ds)
{
	char* pp = NULL;
	int len;
	FILE* out;
	(void)BIO_seek(ds, 0);
	len = BIO_get_mem_data(ds, &pp);
	if(!len || !pp) {
		if(verb) printf("out of memory\n");
		return;
	}
	out = fopen(root_anchor_file, "w");
	if(!out) {
		if(verb) printf("%s: %s\n", root_anchor_file, strerror(errno));
		return;
	}
	if(fwrite(pp, (size_t)len, 1, out) != 1) {
		if(verb) printf("failed to write all data to %s\n",
			root_anchor_file);
		if(verb && errno != 0) printf("%s\n", strerror(errno));
	}
	fflush(out);
#ifdef HAVE_FSYNC
	fsync(fileno(out));
#else
	FlushFileBuffers((HANDLE)_get_osfhandle(_fileno(out)));
#endif
	fclose(out);
}

/** Perform the verification and update of the trustanchor file */
static void
verify_and_update_anchor(const char* root_anchor_file, BIO* xml, BIO* p7s,
	STACK_OF(X509)* cert, const char* p7signer)
{
	BIO* ds;

	/* verify xml file */
	if(!verify_p7sig(xml, p7s, cert, p7signer)) {
		printf("the PKCS7 signature failed\n");
		exit(0);
	}

	/* parse the xml file into DS records */
	ds = xml_parse(xml, time(NULL));
	if(!ds) {
		/* the root zone is unsigned now */
		write_unsigned_root(root_anchor_file);
	} else {
		/* reinstate 5011 tracking */
		write_root_anchor(root_anchor_file, ds);
	}
	BIO_free(ds);
}

#ifdef USE_WINSOCK
static void do_wsa_cleanup(void) { WSACleanup(); }
#endif

/** perform actual certupdate work */
static int
do_certupdate(const char* root_anchor_file, const char* root_cert_file,
	const char* urlname, const char* xmlname, const char* p7sname,
	const char* p7signer, const char* res_conf, const char* root_hints,
	const char* debugconf, int ip4only, int ip6only, int port,
	struct ub_result* dnskey)
{
	STACK_OF(X509)* cert;
	BIO *xml, *p7s;
	struct ip_list* ip_list = NULL;

	/* read pem file or provide builtin */
	cert = read_cert_or_builtin(root_cert_file);

	/* lookup A, AAAA for the urlname (or parse urlname if IP address) */
	ip_list = resolve_name(urlname, port, res_conf, root_hints, debugconf,
		ip4only, ip6only);

#ifdef USE_WINSOCK
	if(1) { /* libunbound finished, startup WSA for the https connection */
		WSADATA wsa_data;
		int r;
		if((r = WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0) {
			if(verb) printf("WSAStartup failed: %s\n",
				wsa_strerror(r));
			exit(0);
		}
		atexit(&do_wsa_cleanup);
	}
#endif

	/* fetch the necessary files over HTTPS */
	xml = https(ip_list, xmlname, urlname);
	p7s = https(ip_list, p7sname, urlname);

	/* verify and update the root anchor */
	verify_and_update_anchor(root_anchor_file, xml, p7s, cert, p7signer);
	if(verb) printf("success: the anchor has been updated "
			"using the cert\n");

	free_file_bio(xml);
	free_file_bio(p7s);
#ifndef S_SPLINT_S
	sk_X509_pop_free(cert, X509_free);
#endif
	ub_resolve_free(dnskey);
	ip_list_free(ip_list);
	return 1;
}

/**
 * Try to read the root RFC5011 autotrust anchor file,
 * @param file: filename.
 * @return:
 * 	0 if does not exist or empty
 * 	1 if trust-point-revoked-5011
 * 	2 if it is OK.
 */
static int
try_read_anchor(const char* file)
{
	int empty = 1;
	char line[10240];
	char* p;
	FILE* in = fopen(file, "r");
	if(!in) {
		/* only if the file does not exist, can we fix it */
		if(errno != ENOENT) {
			if(verb) printf("%s: %s\n", file, strerror(errno));
			if(verb) printf("error: cannot access the file\n");
			exit(0);
		}
		if(verb) printf("%s does not exist\n", file);
		return 0;
	}
	while(fgets(line, (int)sizeof(line), in)) {
		line[sizeof(line)-1] = 0;
		if(strncmp(line, ";;REVOKED", 9) == 0) {
			fclose(in);
			if(verb) printf("%s : the trust point is revoked\n"
				"and the zone is considered unsigned.\n"
				"if you wish to re-enable, delete the file\n",
				file);
			return 1;
		}
		p=line;
		while(*p == ' ' || *p == '\t')
			p++;
		if(p[0]==0 || p[0]=='\n' || p[0]==';') continue;
		/* this line is a line of content */
		empty = 0;
	}
	fclose(in);
	if(empty) {
		if(verb) printf("%s is empty\n", file);
		return 0;
	}
	if(verb) printf("%s has content\n", file);
	return 2;
}

/** Write the builtin root anchor to a file */
static void
write_builtin_anchor(const char* file)
{
	const char* builtin_root_anchor = get_builtin_ds();
	FILE* out = fopen(file, "w");
	if(!out) {
		if(verb) printf("%s: %s\n", file, strerror(errno));
		if(verb) printf("  could not write builtin anchor\n");
		return;
	}
	if(!fwrite(builtin_root_anchor, strlen(builtin_root_anchor), 1, out)) {
		if(verb) printf("%s: %s\n", file, strerror(errno));
		if(verb) printf("  could not complete write builtin anchor\n");
	}
	fclose(out);
}

/** 
 * Check the root anchor file.
 * If does not exist, provide builtin and write file.
 * If empty, provide builtin and write file.
 * If trust-point-revoked-5011 file: make the program exit.
 * @param root_anchor_file: filename of the root anchor.
 * @param used_builtin: set to 1 if the builtin is written.
 * @return 0 if trustpoint is insecure, 1 on success.  Exit on failure.
 */
static int
provide_builtin(const char* root_anchor_file, int* used_builtin)
{
	/* try to read it */
	switch(try_read_anchor(root_anchor_file))
	{
		case 0: /* no exist or empty */
			write_builtin_anchor(root_anchor_file);
			*used_builtin = 1;
			break;
		case 1: /* revoked tp */
			return 0;	
		case 2: /* it is fine */
		default:
			break;
	}
	return 1;
}

/**
 * add an autotrust anchor for the root to the context
 */
static void
add_5011_probe_root(struct ub_ctx* ctx, const char* root_anchor_file)
{
	int r;
	r = ub_ctx_set_option(ctx, "auto-trust-anchor-file:", root_anchor_file);
	if(r) {
		if(verb) printf("add 5011 probe to ctx: %s\n", ub_strerror(r));
		ub_ctx_delete(ctx);
		exit(0);
	}
}

/**
 * Prime the root key and return the result.  Exit on error.
 * @param ctx: the unbound context to perform the priming with.
 * @return: the result of the prime, on error it exit()s.
 */
static struct ub_result*
prime_root_key(struct ub_ctx* ctx)
{
	struct ub_result* res = NULL;
	int r;
	r = ub_resolve(ctx, ".", LDNS_RR_TYPE_DNSKEY, LDNS_RR_CLASS_IN, &res);
	if(r) {
		if(verb) printf("resolve DNSKEY: %s\n", ub_strerror(r));
		ub_ctx_delete(ctx);
		exit(0);
	}
	if(!res) {
		if(verb) printf("out of memory\n");
		ub_ctx_delete(ctx);
		exit(0);
	}
	return res;
}

/** see if ADDPEND keys exist in autotrust file (if possible) */
static int
read_if_pending_keys(const char* file)
{
	FILE* in = fopen(file, "r");
	char line[8192];
	if(!in) {
		if(verb>=2) printf("%s: %s\n", file, strerror(errno));
		return 0;
	}
	while(fgets(line, (int)sizeof(line), in)) {
		if(line[0]==';') continue;
		if(strstr(line, "[ ADDPEND ]")) {
			fclose(in);
			if(verb) printf("RFC5011-state has ADDPEND keys\n");
			return 1;
		}
	}
	fclose(in);
	return 0;
}

/** read last successful probe time from autotrust file (if possible) */
static int32_t
read_last_success_time(const char* file)
{
	FILE* in = fopen(file, "r");
	char line[1024];
	if(!in) {
		if(verb) printf("%s: %s\n", file, strerror(errno));
		return 0;
	}
	while(fgets(line, (int)sizeof(line), in)) {
		if(strncmp(line, ";;last_success: ", 16) == 0) {
			char* e;
			time_t x = (unsigned int)strtol(line+16, &e, 10);
			fclose(in);
			if(line+16 == e) {
				if(verb) printf("failed to parse "
					"last_success probe time\n");
				return 0;
			}
			if(verb) printf("last successful probe: %s", ctime(&x));
			return (int32_t)x;
		}
	}
	fclose(in);
	if(verb) printf("no last_success probe time in anchor file\n");
	return 0;
}

/**
 * Read autotrust 5011 probe file and see if the date
 * compared to the current date allows a certupdate.
 * If the last successful probe was recent then 5011 cannot be behind,
 * and the failure cannot be solved with a certupdate.
 * The debugconf is to validation-override the date for testing.
 * @param root_anchor_file: filename of root key
 * @return true if certupdate is ok.
 */
static int
probe_date_allows_certupdate(const char* root_anchor_file)
{
	int has_pending_keys = read_if_pending_keys(root_anchor_file);
	int32_t last_success = read_last_success_time(root_anchor_file);
	int32_t now = (int32_t)time(NULL);
	int32_t leeway = 30 * 24 * 3600; /* 30 days leeway */
	/* if the date is before 2010-07-15:00.00.00 then the root has not
	 * been signed yet, and thus we refuse to take action. */
	if(time(NULL) < xml_convertdate("2010-07-15T00:00:00")) {
		if(verb) printf("the date is before the root was first signed,"
			" please correct the clock\n");
		return 0;
	}
	if(last_success == 0)
		return 1; /* no probe time */
	if(has_pending_keys)
		return 1; /* key in ADDPEND state, a previous probe has
		inserted that, and it was present in all recent probes,
		but it has not become active.  The 30 day timer may not have
		expired, but we know(for sure) there is a rollover going on.
		If we only managed to pickup the new key on its last day
		of announcement (for example) this can happen. */
	if(now - last_success < 0) {
		if(verb) printf("the last successful probe is in the future,"
			" clock was modified\n");
		return 0;
	}
	if(now - last_success >= leeway) {
		if(verb) printf("the last successful probe was more than 30 "
			"days ago\n");
		return 1;
	}
	if(verb) printf("the last successful probe is recent\n");
	return 0;
}

/** perform the unbound-anchor work */
static int
do_root_update_work(const char* root_anchor_file, const char* root_cert_file,
	const char* urlname, const char* xmlname, const char* p7sname,
	const char* p7signer, const char* res_conf, const char* root_hints,
	const char* debugconf, int ip4only, int ip6only, int force, int port)
{
	struct ub_ctx* ctx;
	struct ub_result* dnskey;
	int used_builtin = 0;

	/* see if builtin rootanchor needs to be provided, or if
	 * rootanchor is 'revoked-trust-point' */
	if(!provide_builtin(root_anchor_file, &used_builtin))
		return 0;

	/* make unbound context with 5011-probe for root anchor,
	 * and probe . DNSKEY */
	ctx = create_unbound_context(res_conf, root_hints, debugconf,
		ip4only, ip6only);
	add_5011_probe_root(ctx, root_anchor_file);
	dnskey = prime_root_key(ctx);
	ub_ctx_delete(ctx);
	
	/* if secure: exit */
	if(dnskey->secure && !force) {
		if(verb) printf("success: the anchor is ok\n");
		ub_resolve_free(dnskey);
		return used_builtin;
	}
	if(force && verb) printf("debug cert update forced\n");

	/* if not (and NOERROR): check date and do certupdate */
	if((dnskey->rcode == 0 &&
		probe_date_allows_certupdate(root_anchor_file)) || force) {
		if(do_certupdate(root_anchor_file, root_cert_file, urlname,
			xmlname, p7sname, p7signer, res_conf, root_hints,
			debugconf, ip4only, ip6only, port, dnskey))
			return 1;
		return used_builtin;
	}
	if(verb) printf("fail: the anchor is NOT ok and could not be fixed\n");
	ub_resolve_free(dnskey);
	return used_builtin;
}

/** getopt global, in case header files fail to declare it. */
extern int optind;
/** getopt global, in case header files fail to declare it. */
extern char* optarg;

/** Main routine for unbound-anchor */
int main(int argc, char* argv[])
{
	int c;
	const char* root_anchor_file = ROOT_ANCHOR_FILE;
	const char* root_cert_file = ROOT_CERT_FILE;
	const char* urlname = URLNAME;
	const char* xmlname = XMLNAME;
	const char* p7sname = P7SNAME;
	const char* p7signer = P7SIGNER;
	const char* res_conf = NULL;
	const char* root_hints = NULL;
	const char* debugconf = NULL;
	int dolist=0, ip4only=0, ip6only=0, force=0, port = HTTPS_PORT;
	/* parse the options */
	while( (c=getopt(argc, argv, "46C:FP:a:c:f:hln:r:s:u:vx:")) != -1) {
		switch(c) {
		case 'l':
			dolist = 1;
			break;
		case '4':
			ip4only = 1;
			break;
		case '6':
			ip6only = 1;
			break;
		case 'a':
			root_anchor_file = optarg;
			break;
		case 'c':
			root_cert_file = optarg;
			break;
		case 'u':
			urlname = optarg;
			break;
		case 'x':
			xmlname = optarg;
			break;
		case 's':
			p7sname = optarg;
			break;
		case 'n':
			p7signer = optarg;
			break;
		case 'f':
			res_conf = optarg;
			break;
		case 'r':
			root_hints = optarg;
			break;
		case 'C':
			debugconf = optarg;
			break;
		case 'F':
			force = 1;
			break;
		case 'P':
			port = atoi(optarg);
			break;
		case 'v':
			verb++;
			break;
		case '?':
		case 'h':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;
	if(argc != 0)
		usage();

#ifdef HAVE_ERR_LOAD_CRYPTO_STRINGS
	ERR_load_crypto_strings();
#endif
	ERR_load_SSL_strings();
#if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_CRYPTO)
	OpenSSL_add_all_algorithms();
#else
	OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
		| OPENSSL_INIT_ADD_ALL_DIGESTS
		| OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
#endif
#if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
	(void)SSL_library_init();
#else
	(void)OPENSSL_init_ssl(0, NULL);
#endif

	if(dolist) do_list_builtin();

	return do_root_update_work(root_anchor_file, root_cert_file, urlname,
		xmlname, p7sname, p7signer, res_conf, root_hints, debugconf,
		ip4only, ip6only, force, port);
}