summaryrefslogtreecommitdiff
path: root/src/exchange/taler-exchange-httpd_keystate.c
blob: 4115fe66e83c9c2c1b027fe3fb1f4e43e7fc539a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
/*
  This file is part of TALER
  Copyright (C) 2014--2019 GNUnet e.V.

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Affero General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  TALER 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 Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file taler-exchange-httpd_keystate.c
 * @brief management of our coin signing keys
 * @author Florian Dold
 * @author Benedikt Mueller
 * @author Christian Grothoff
 */
#include "platform.h"
#include <pthread.h>
#include "taler_json_lib.h"
#include "taler_mhd_lib.h"
#include "taler-exchange-httpd_keystate.h"
#include "taler-exchange-httpd_responses.h"
#include "taler_exchangedb_plugin.h"


/**
 * Taler protocol version in the format CURRENT:REVISION:AGE
 * as used by GNU libtool.  See
 * https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html
 *
 * Please be very careful when updating and follow
 * https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html#Updating-version-info
 * precisely.  Note that this version has NOTHING to do with the
 * release version, and the format is NOT the same that semantic
 * versioning uses either.
 *
 * When changing this version, you likely want to also update
 * #TALER_PROTOCOL_CURRENT and #TALER_PROTOCOL_AGE in
 * exchange_api_handle.c!
 */
#define TALER_PROTOCOL_VERSION "6:0:0"


/**
 * Signatures of an auditor over a denomination key of this exchange.
 */
struct AuditorSignature
{
  /**
   * We store the signatures in a DLL.
   */
  struct AuditorSignature *prev;

  /**
   * We store the signatures in a DLL.
   */
  struct AuditorSignature *next;

  /**
   * A signature from the auditor.
   */
  struct TALER_AuditorSignatureP asig;

  /**
   * Public key of the auditor.
   */
  struct TALER_AuditorPublicKeyP apub;

  /**
   * URL of the auditor. Allocated at the end of this struct.
   */
  const char *auditor_url;

};


/**
 * Entry in sorted array of denomination keys.  Sorted by starting
 * "start" time (validity period) of the `struct
 * TALER_DenominationKeyValidityPS`.
 */
struct DenominationKeyEntry
{

  /**
   * Reference to the public key.
   * (Must also be in the `denomkey_map`).
   */
  const struct TALER_EXCHANGEDB_DenominationKeyIssueInformation *dki;

  /**
   * Head of DLL of signatures for this @e dki.
   */
  struct AuditorSignature *as_head;

  /**
   * Tail of DLL of signatures for this @e dki.
   */
  struct AuditorSignature *as_tail;

  /**
   * Hash of the public denomination key.
   */
  struct GNUNET_HashCode denom_key_hash;

#ifdef OPTIMIZE_5777_AUDITOR_BY_COUNT_REALTIME_DETECTION

  /**
   * Mutex that must be held before threads may access or update
   * @e known_coin_counter or @e known_coin_counter_db.
   */
  pthread_mutex_t known_coin_counter_mutex;

  /**
   * Mutex that must be held before threads may access or update
   * @e issued_coin_counter or @e issued_coin_counter_db.
   */
  pthread_mutex_t issued_coin_counter_mutex;

  /**
   * How many coins of this denomination have been redeemed so far (according
   * to only this process)?
   */
  uint64_t known_coin_counter;

  /**
   * How many coins of this denomination have been redeemed so far (based on
   * the last time we synchronized the value with our database).
   */
  uint64_t known_coin_counter_db;

  /**
   * How many coins of this denomination have been issued so far (according
   * to only this process)?
   */
  uint64_t issued_coin_counter;

  /**
   * How many coins of this denomination have been issued so far (based on the
   * last time we synchronized the value with our database)?
   */
  uint64_t issued_coin_counter_db;

#endif

};


/**
 * Entry in (sorted) array with possible pre-build responses for /keys.
 * We keep pre-build responses for the various (valid) cherry-picking
 * values around.
 */
struct KeysResponseData
{

  /**
   * Response to return if the client supports (gzip) compression.
   */
  struct MHD_Response *response_compressed;

  /**
   * Response to return if the client does not support compression.
   */
  struct MHD_Response *response_uncompressed;

  /**
   * Cherry-picking timestamp the client must have set for this
   * response to be valid.  0 if this is the "full" response.
   * The client's request must include this date or a higher one
   * for this response to be applicable.
   */
  struct GNUNET_TIME_Absolute cherry_pick_date;

};


/**
 * State we keep around while building an individual entry in the
 * `struct KeysResponseData` array, i.e. the global state for ONE of
 * the responses.
 */
struct ResponseBuilderContext
{

  /**
   * Hash context we used to combine the hashes of all denomination
   * keys into one big hash for signing.
   */
  struct GNUNET_HashContext *hash_context;

  /**
   * JSON array with denomination key information.
   */
  json_t *denom_keys_array;

  /**
   * JSON array with auditor information.
   */
  json_t *auditors_array;

  /**
   * Keys after what issue date do we care about?
   */
  struct GNUNET_TIME_Absolute last_issue_date;

  /**
   * Flag set to #GNUNET_SYSERR on internal errors
   */
  int error;

};


/**
 * State we keep around while building the `struct KeysResponseData`
 * array, i.e. the global state for all of the responses.
 */
struct ResponseFactoryContext
{

  /**
   * JSON array with revoked denomination keys.  Every response
   * always returns the full list (cherry picking does not apply
   * for key revocations, as we cannot sort those by issue date).
   */
  json_t *payback_array;

  /**
   * JSON array with signing keys.  Every response includes the full
   * list, as it should be quite short anyway, and for simplicity the
   * client only communicates the one time stamp of the last
   * denomination key it knows when cherry picking.
   */
  json_t *sign_keys_array;

  /**
   * Sorted array of denomination keys.  Length is @e denomkey_array_length.
   * Entries are sorted by the validity period's starting time.
   */
  struct DenominationKeyEntry *denomkey_array;

  /**
   * The main key state we are building everything for.
   */
  struct TEH_KS_StateHandle *key_state;

  /**
   * Length of the @e denomkey_array.
   */
  unsigned int denomkey_array_length;

  /**
   * Time stamp used as "now".
   */
  struct GNUNET_TIME_Absolute now;
};


/**
 * Snapshot of the (coin and signing) keys (including private keys) of
 * the exchange.  There can be multiple instances of this struct, as it is
 * reference counted and only destroyed once the last user is done
 * with it.  The current instance is acquired using
 * #TEH_KS_acquire().  Using this function increases the
 * reference count.  The contents of this structure (except for the
 * reference counter) should be considered READ-ONLY until it is
 * ultimately destroyed (as there can be many concurrent users).
 */
struct TEH_KS_StateHandle
{

  /**
   * Mapping from denomination keys to denomination key issue struct.
   * Used to lookup the key by hash.
   */
  struct GNUNET_CONTAINER_MultiHashMap *denomkey_map;

  /**
   * Mapping from revoked denomination keys to denomination key issue struct.
   * Used to lookup the key by hash.
   */
  struct GNUNET_CONTAINER_MultiHashMap *revoked_map;

  /**
   * Sorted array of responses to /keys (sorted by cherry-picking date) of
   * length @e krd_array_length;
   */
  struct KeysResponseData *krd_array;

  /**
   * When did we initiate the key reloading?
   */
  struct GNUNET_TIME_Absolute reload_time;

  /**
   * When is the next key invalid and we have to reload? (We also
   * reload on SIGUSR1.)
   */
  struct GNUNET_TIME_Absolute next_reload;

  /**
   * When does the first active denomination key expire (for deposit)?
   */
  struct GNUNET_TIME_Absolute min_dk_expire;

  /**
   * Exchange signing key that should be used currently.
   */
  struct TALER_EXCHANGEDB_PrivateSigningKeyInformationP current_sign_key_issue;

  /**
   * Reference count.  The struct is released when the RC hits zero.
   */
  unsigned int refcnt;

  /**
   * Length of the @e krd_array.
   */
  unsigned int krd_array_length;
};


/**
 * Exchange key state.  This is the long-term, read-only internal global state,
 * which the various threads "lock" to use in read-only ways.  We eventually
 * create a completely new object "on the side" and then start to return
 * the new read-only object to threads that ask. Once none of the threads
 * use the previous object (RC drops to zero), we discard it.
 *
 * Thus, this instance should never be used directly, instead reserve
 * access via #TEH_KS_acquire() and release it via #TEH_KS_release().
 */
static struct TEH_KS_StateHandle *internal_key_state;

/**
 * Mutex protecting access to #internal_key_state.
 */
static pthread_mutex_t internal_key_state_mutex = PTHREAD_MUTEX_INITIALIZER;


/* ************************** Clean up logic *********************** */


/**
 * Release memory used by @a rfc.
 *
 * @param rfc factory to release (but do not #GNUNET_free() rfc itself!)
 */
static void
destroy_response_factory (struct ResponseFactoryContext *rfc)
{
  if (NULL != rfc->payback_array)
  {
    json_decref (rfc->payback_array);
    rfc->payback_array = NULL;
  }
  if (NULL != rfc->sign_keys_array)
  {
    json_decref (rfc->sign_keys_array);
    rfc->sign_keys_array = NULL;
  }
  for (unsigned int i = 0; i<rfc->denomkey_array_length; i++)
  {
    struct DenominationKeyEntry *dke = &rfc->denomkey_array[i];
    struct AuditorSignature *as;

    while (NULL != (as = dke->as_head))
    {
      GNUNET_CONTAINER_DLL_remove (dke->as_head,
                                   dke->as_tail,
                                   as);
      GNUNET_free (as);
    }
  }
  GNUNET_array_grow (rfc->denomkey_array,
                     rfc->denomkey_array_length,
                     0);
}


/**
 * Release memory used by @a rbc.
 */
static void
destroy_response_builder (struct ResponseBuilderContext *rbc)
{
  if (NULL != rbc->denom_keys_array)
  {
    json_decref (rbc->denom_keys_array);
    rbc->denom_keys_array = NULL;
  }
  if (NULL != rbc->auditors_array)
  {
    json_decref (rbc->auditors_array);
    rbc->auditors_array = NULL;
  }
}


/**
 * Iterator for freeing denomination keys.
 *
 * @param cls closure with the `struct TEH_KS_StateHandle`
 * @param key key for the denomination key
 * @param value coin details
 * @return #GNUNET_OK to continue to iterate,
 *  #GNUNET_NO to stop iteration with no error,
 *  #GNUNET_SYSERR to abort iteration with error!
 */
static int
free_denom_key (void *cls,
                const struct GNUNET_HashCode *key,
                void *value)
{
  struct TALER_EXCHANGEDB_DenominationKeyIssueInformation *dki = value;

  (void) cls;
  (void) key;
  if (NULL != dki->denom_priv.rsa_private_key)
    GNUNET_CRYPTO_rsa_private_key_free (dki->denom_priv.rsa_private_key);
  GNUNET_CRYPTO_rsa_public_key_free (dki->denom_pub.rsa_public_key);
  GNUNET_free (dki);
  return GNUNET_OK;
}


/**
 * Release key state, free if necessary (if reference count gets to zero).
 * Internal method used when the mutex is already held.
 *
 * @param key_state the key state to release
 */
static void
ks_release (struct TEH_KS_StateHandle *key_state)
{
  GNUNET_assert (0 < key_state->refcnt);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "KS release called (%p/%d)\n",
              key_state,
              key_state->refcnt);
  key_state->refcnt--;
  if (0 == key_state->refcnt)
  {
    if (NULL != key_state->denomkey_map)
    {
      GNUNET_CONTAINER_multihashmap_iterate (key_state->denomkey_map,
                                             &free_denom_key,
                                             key_state);
      GNUNET_CONTAINER_multihashmap_destroy (key_state->denomkey_map);
      key_state->denomkey_map = NULL;
    }
    if (NULL != key_state->revoked_map)
    {
      GNUNET_CONTAINER_multihashmap_iterate (key_state->revoked_map,
                                             &free_denom_key,
                                             key_state);
      GNUNET_CONTAINER_multihashmap_destroy (key_state->revoked_map);
      key_state->revoked_map = NULL;
    }
    for (unsigned int i = 0; i<key_state->krd_array_length; i++)
    {
      struct KeysResponseData *krd = &key_state->krd_array[i];

      if (NULL != krd->response_compressed)
        MHD_destroy_response (krd->response_compressed);
      if (NULL != krd->response_uncompressed)
        MHD_destroy_response (krd->response_uncompressed);
    }
    GNUNET_array_grow (key_state->krd_array,
                       key_state->krd_array_length,
                       0);
    GNUNET_assert (key_state != internal_key_state);
    GNUNET_free (key_state);
  }
}


/* ************************* Signal logic ************************** */

/**
 * Pipe used for signaling reloading of our key state.
 */
static int reload_pipe[2];


/**
 * Handle a signal, writing relevant signal numbers to the pipe.
 *
 * @param signal_number the signal number
 */
static void
handle_signal (int signal_number)
{
  ssize_t res;
  char c = signal_number;

  res = write (reload_pipe[1],
               &c,
               1);
  if ( (res < 0) &&
       (EINTR != errno) )
  {
    GNUNET_break (0);
    return;
  }
  if (0 == res)
  {
    GNUNET_break (0);
    return;
  }
}


/* ************************** State builder ************************ */


/**
 * Convert the public part of a denomination key issue to a JSON
 * object.
 *
 * @param pk public key of the denomination key
 * @param dki the denomination key issue
 * @return a JSON object describing the denomination key isue (public part)
 */
static json_t *
denom_key_issue_to_json (const struct TALER_DenominationPublicKey *pk,
                         const struct
                         TALER_EXCHANGEDB_DenominationKeyInformationP *dki)
{
  struct TALER_Amount value;
  struct TALER_Amount fee_withdraw;
  struct TALER_Amount fee_deposit;
  struct TALER_Amount fee_refresh;
  struct TALER_Amount fee_refund;

  TALER_amount_ntoh (&value,
                     &dki->properties.value);
  TALER_amount_ntoh (&fee_withdraw,
                     &dki->properties.fee_withdraw);
  TALER_amount_ntoh (&fee_deposit,
                     &dki->properties.fee_deposit);
  TALER_amount_ntoh (&fee_refresh,
                     &dki->properties.fee_refresh);
  TALER_amount_ntoh (&fee_refund,
                     &dki->properties.fee_refund);
  return
    json_pack ("{s:o, s:o, s:o, s:o, s:o, s:o, s:o, s:o, s:o, s:o, s:o}",
               "master_sig",
               GNUNET_JSON_from_data_auto (&dki->signature),
               "stamp_start",
               GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (
                                            dki->properties.start)),
               "stamp_expire_withdraw",
               GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (
                                            dki->properties.expire_withdraw)),
               "stamp_expire_deposit",
               GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (
                                            dki->properties.expire_deposit)),
               "stamp_expire_legal",
               GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (
                                            dki->properties.expire_legal)),
               "denom_pub",
               GNUNET_JSON_from_rsa_public_key (pk->rsa_public_key),
               "value",
               TALER_JSON_from_amount (&value),
               "fee_withdraw",
               TALER_JSON_from_amount (&fee_withdraw),
               "fee_deposit",
               TALER_JSON_from_amount (&fee_deposit),
               "fee_refresh",
               TALER_JSON_from_amount (&fee_refresh),
               "fee_refund",
               TALER_JSON_from_amount (&fee_refund));
}


/**
 * Store a copy of @a dki in @a map.
 *
 * @param map hash map to store @a dki in
 * @param dki information to store in @a map
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO if such an entry already exists
 */
static int
store_in_map (struct GNUNET_CONTAINER_MultiHashMap *map,
              const struct
              TALER_EXCHANGEDB_DenominationKeyIssueInformation *dki)
{
  struct TALER_EXCHANGEDB_DenominationKeyIssueInformation *d2;
  int res;

  {
    const struct TALER_EXCHANGEDB_DenominationKeyInformationP *dkip;
    struct TALER_DenominationKeyValidityPS denom_key_issue;

    dkip = &dki->issue;
    denom_key_issue = dkip->properties;
    denom_key_issue.purpose.purpose
      = htonl (TALER_SIGNATURE_MASTER_DENOMINATION_KEY_VALIDITY);
    denom_key_issue.purpose.size
      = htonl (sizeof (struct TALER_DenominationKeyValidityPS));
    denom_key_issue.master = TEH_master_public_key;
    if (GNUNET_SYSERR ==
        GNUNET_CRYPTO_eddsa_verify (
          TALER_SIGNATURE_MASTER_DENOMINATION_KEY_VALIDITY,
          &denom_key_issue.purpose,
          &dkip->signature.eddsa_signature,
          &TEH_master_public_key.eddsa_pub))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "Invalid signature on denomination key `%s'\n",
                  GNUNET_h2s (&dkip->properties.denom_hash));
      return GNUNET_SYSERR;
    }
  }

  d2 = GNUNET_new (struct TALER_EXCHANGEDB_DenominationKeyIssueInformation);
  d2->issue = dki->issue;
  if (NULL != dki->denom_priv.rsa_private_key)
    d2->denom_priv.rsa_private_key
      = GNUNET_CRYPTO_rsa_private_key_dup (dki->denom_priv.rsa_private_key);
  d2->denom_pub.rsa_public_key
    = GNUNET_CRYPTO_rsa_public_key_dup (dki->denom_pub.rsa_public_key);
  res = GNUNET_CONTAINER_multihashmap_put (map,
                                           &d2->issue.properties.denom_hash,
                                           d2,
                                           GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
  if (GNUNET_OK != res)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Duplicate denomination key `%s'\n",
                GNUNET_h2s (&d2->issue.properties.denom_hash));
    if (NULL != d2->denom_priv.rsa_private_key)
      GNUNET_CRYPTO_rsa_private_key_free (d2->denom_priv.rsa_private_key);
    GNUNET_CRYPTO_rsa_public_key_free (d2->denom_pub.rsa_public_key);
    GNUNET_free (d2);
    return GNUNET_NO;
  }
  return GNUNET_OK;
}


/**
 * Closure for #add_revocations_transaction().
 */
struct AddRevocationContext
{
  /**
   * Denomination key that is revoked.
   */
  const struct TALER_EXCHANGEDB_DenominationKeyIssueInformation *dki;

  /**
   * Signature affirming the revocation.
   */
  const struct TALER_MasterSignatureP *revocation_master_sig;
};


/**
 * Get the relative time value that describes how
 * far in the future do we want to provide coin keys.
 *
 * @return the provide duration
 */
static struct GNUNET_TIME_Relative
TALER_EXCHANGE_conf_duration_provide ()
{
  struct GNUNET_TIME_Relative rel;

  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_time (cfg,
                                           "exchange",
                                           "LOOKAHEAD_PROVIDE",
                                           &rel))
  {
    GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
                               "exchange",
                               "LOOKAHEAD_PROVIDE",
                               "time value required");
    GNUNET_assert (0);
  }
  return rel;
}


/**
 * Execute transaction to add revocations.
 *
 * @param cls closure with the `struct AddRevocationContext *`
 * @param connection NULL
 * @param session database session to use
 * @param[out] mhd_ret not used
 * @return transaction status
 */
static enum GNUNET_DB_QueryStatus
add_revocations_transaction (void *cls,
                             struct MHD_Connection *connection,
                             struct TALER_EXCHANGEDB_Session *session,
                             int *mhd_ret)
{
  struct AddRevocationContext *arc = cls;
  enum GNUNET_DB_QueryStatus qs;
  struct TALER_MasterSignatureP master_sig;
  uint64_t rowid;

  (void) connection;
  (void) mhd_ret;
  qs = TEH_plugin->get_denomination_revocation (TEH_plugin->cls,
                                                session,
                                                &arc->dki->issue.properties.
                                                denom_hash,
                                                &master_sig,
                                                &rowid);
  if (0 > qs)
    return qs; /* failure */
  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
    return qs; /* already exists == success */
  return TEH_plugin->insert_denomination_revocation (TEH_plugin->cls,
                                                     session,
                                                     &arc->dki->issue.properties
                                                     .denom_hash,
                                                     arc->revocation_master_sig);
}


/**
 * Execute transaction to add a denomination to the DB.
 *
 * @param cls closure with the `const struct TALER_EXCHANGEDB_DenominationKeyIssueInformation *`
 * @param connection NULL
 * @param session database session to use
 * @param[out] mhd_ret not used
 * @return transaction status
 */
static enum GNUNET_DB_QueryStatus
add_denomination_transaction (void *cls,
                              struct MHD_Connection *connection,
                              struct TALER_EXCHANGEDB_Session *session,
                              int *mhd_ret)
{
  const struct TALER_EXCHANGEDB_DenominationKeyIssueInformation *dki = cls;
  enum GNUNET_DB_QueryStatus qs;
  struct TALER_EXCHANGEDB_DenominationKeyInformationP issue_exists;

  (void) connection;
  (void) mhd_ret;
  qs = TEH_plugin->get_denomination_info (TEH_plugin->cls,
                                          session,
                                          &dki->issue.properties.denom_hash,
                                          &issue_exists);
  if (0 > qs)
    return qs;
  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
    return qs;
  return TEH_plugin->insert_denomination_info (TEH_plugin->cls,
                                               session,
                                               &dki->denom_pub,
                                               &dki->issue);
}


/**
 * Iterator for (re)loading/initializing denomination keys.
 *
 * @param cls closure with a `struct ResponseFactoryContext *`
 * @param dki the denomination key issue
 * @param alias coin alias
 * @return #GNUNET_OK to continue to iterate,
 *  #GNUNET_NO to stop iteration with no error,
 *  #GNUNET_SYSERR to abort iteration with error!
 */
static int
reload_keys_denom_iter (void *cls,
                        const char *alias,
                        const struct
                        TALER_EXCHANGEDB_DenominationKeyIssueInformation *dki)
{
  struct ResponseFactoryContext *rfc = cls;
  struct TEH_KS_StateHandle *key_state = rfc->key_state;
  struct GNUNET_TIME_Absolute start;
  struct GNUNET_TIME_Absolute horizon;
  struct GNUNET_TIME_Absolute expire_deposit;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Loading denomination key `%s' (%s)\n",
              alias,
              GNUNET_h2s (&dki->issue.properties.denom_hash));
  expire_deposit = GNUNET_TIME_absolute_ntoh (
    dki->issue.properties.expire_deposit);
  if (expire_deposit.abs_value_us < rfc->now.abs_value_us)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Skipping expired denomination key `%s'\n",
                alias);
    return GNUNET_OK;
  }
  if (0 != GNUNET_memcmp (&dki->issue.properties.master,
                          &TEH_master_public_key))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Master key in denomination key file `%s' does not match! Skipping it.\n",
                alias);
    return GNUNET_OK;
  }

  horizon = GNUNET_TIME_absolute_add (rfc->now,
                                      TALER_EXCHANGE_conf_duration_provide ());
  start = GNUNET_TIME_absolute_ntoh (dki->issue.properties.start);
  if (start.abs_value_us > horizon.abs_value_us)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Skipping future denomination key `%s' (%s), validity starts at %s\n",
                alias,
                GNUNET_h2s (&dki->issue.properties.denom_hash),
                GNUNET_STRINGS_absolute_time_to_string (start));
    return GNUNET_OK;
  }

  if (GNUNET_OK !=
      TEH_DB_run_transaction (NULL,
                              "add denomination key",
                              NULL,
                              &add_denomination_transaction,
                              (void *) dki))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Could not persist denomination key %s in DB. Committing suicide via SIGTERM.\n",
                GNUNET_h2s (&dki->issue.properties.denom_hash));
    handle_signal (SIGTERM);
    return GNUNET_SYSERR;
  }

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Adding denomination key `%s' (%s) to active set\n",
              alias,
              GNUNET_h2s (&dki->issue.properties.denom_hash));
  if (GNUNET_NO /* entry already exists */ ==
      store_in_map (key_state->denomkey_map,
                    dki))
    return GNUNET_OK; /* do not update expiration if entry exists */
  key_state->min_dk_expire = GNUNET_TIME_absolute_min (key_state->min_dk_expire,
                                                       expire_deposit);
  return GNUNET_OK;
}


/**
 * Iterator for revocation of denomination keys.
 *
 * @param cls closure with a `struct ResponseFactoryContext *`
 * @param denom_hash hash of revoked denomination public key
 * @param revocation_master_sig signature showing @a denom_hash was revoked
 * @return #GNUNET_OK to continue to iterate,
 *  #GNUNET_NO to stop iteration with no error,
 *  #GNUNET_SYSERR to abort iteration with error!
 */
static int
revocations_iter (void *cls,
                  const struct GNUNET_HashCode *denom_hash,
                  const struct TALER_MasterSignatureP *revocation_master_sig)
{
  struct ResponseFactoryContext *rfc = cls;
  struct TEH_KS_StateHandle *key_state = rfc->key_state;
  struct AddRevocationContext arc;
  struct TALER_EXCHANGEDB_DenominationKeyIssueInformation *dki;

  dki = GNUNET_CONTAINER_multihashmap_get (key_state->denomkey_map,
                                           denom_hash);
  if (NULL == dki)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Revoked denomination `%s' unknown (or duplicate file), ignoring revocation\n",
                GNUNET_h2s (denom_hash));
    return GNUNET_OK;

  }
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Adding denomination key `%s' to revocation set\n",
              GNUNET_h2s (denom_hash));
  GNUNET_assert (GNUNET_YES ==
                 GNUNET_CONTAINER_multihashmap_remove (key_state->denomkey_map,
                                                       denom_hash,
                                                       dki));
  GNUNET_assert (GNUNET_YES ==
                 GNUNET_CONTAINER_multihashmap_put (key_state->revoked_map,
                                                    &dki->issue.properties.
                                                    denom_hash,
                                                    dki,
                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  /* Try to insert revocation into DB */
  arc.dki = dki;
  arc.revocation_master_sig = revocation_master_sig;
  if (GNUNET_OK !=
      TEH_DB_run_transaction (NULL,
                              "add denomination key revocation",
                              NULL,
                              &add_revocations_transaction,
                              &arc))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to add revocation to database. This is fatal. Committing suicide via SIGTERM.\n");
    handle_signal (SIGTERM);
    return GNUNET_SYSERR;
  }
  GNUNET_assert (0 ==
                 json_array_append_new (rfc->payback_array,
                                        GNUNET_JSON_from_data_auto (
                                          denom_hash)));
  return GNUNET_OK;
}


/**
 * Convert the public part of a sign key issue to a JSON object.
 *
 * @param ski the sign key issue
 * @param ski_sig signature over @a ski
 * @return a JSON object describing the sign key issue (public part)
 */
static json_t *
sign_key_issue_to_json (const struct TALER_ExchangeSigningKeyValidityPS *ski,
                        const struct TALER_MasterSignatureP *ski_sig)
{
  return
    json_pack ("{s:o, s:o, s:o, s:o, s:o}",
               "stamp_start",
               GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (
                                            ski->start)),
               "stamp_expire",
               GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (
                                            ski->expire)),
               "stamp_end",
               GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (ski->end)),
               "master_sig",
               GNUNET_JSON_from_data_auto (ski_sig),
               "key",
               GNUNET_JSON_from_data_auto (&ski->signkey_pub));
}


/**
 * Iterator for sign keys.  Adds current and near-future signing keys
 * to the `sign_keys_array` and stores the current one in the
 * `key_state`.
 *
 * @param cls closure with the `struct ResponseFactoryContext *`
 * @param filename name of the file the key came from
 * @param ski the sign key issue
 * @param ski_sig signature over @a ski
 * @return #GNUNET_OK to continue to iterate,
 *  #GNUNET_NO to stop iteration with no error,
 *  #GNUNET_SYSERR to abort iteration with error!
 */
static int
reload_keys_sign_iter (void *cls,
                       const char *filename,
                       const struct
                       TALER_EXCHANGEDB_PrivateSigningKeyInformationP *ski)
{
  struct ResponseFactoryContext *rfc = cls;
  struct TEH_KS_StateHandle *key_state = rfc->key_state;
  struct GNUNET_TIME_Absolute now;
  struct GNUNET_TIME_Absolute horizon;

  horizon = GNUNET_TIME_relative_to_absolute (
    TALER_EXCHANGE_conf_duration_provide ());
  if (GNUNET_TIME_absolute_ntoh (ski->issue.start).abs_value_us >
      horizon.abs_value_us)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Skipping future signing key `%s'\n",
                filename);
    return GNUNET_OK;
  }
  now = GNUNET_TIME_absolute_get ();
  if (GNUNET_TIME_absolute_ntoh (ski->issue.expire).abs_value_us <
      now.abs_value_us)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Skipping expired signing key `%s'\n",
                filename);
    return GNUNET_OK;
  }

  if (0 != GNUNET_memcmp (&ski->issue.master_public_key,
                          &TEH_master_public_key))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Master key in signing key file `%s' does not match! Skipping it.\n",
                filename);
    return GNUNET_OK;
  }

  /* The signkey is valid at this time, check if it's more recent than
     what we have so far! */
  if ( (GNUNET_TIME_absolute_ntoh (
          key_state->current_sign_key_issue.issue.start).abs_value_us <
        GNUNET_TIME_absolute_ntoh (ski->issue.start).abs_value_us) &&
       (GNUNET_TIME_absolute_ntoh (ski->issue.start).abs_value_us <
        now.abs_value_us) )
  {
    /* We use the most recent one, if it is valid now (not just in the near future) */
    key_state->current_sign_key_issue = *ski;
  }
  GNUNET_assert (0 ==
                 json_array_append_new (rfc->sign_keys_array,
                                        sign_key_issue_to_json (&ski->issue,
                                                                &ski->master_sig)));

  return GNUNET_OK;
}


/**
 * @brief Iterator called with auditor information.
 * Check that the @a mpub actually matches this exchange, and then
 * add the auditor information to our /keys response (if it is
 * (still) applicable).
 *
 * @param cls closure with the `struct ResponseFactoryContext *`
 * @param apub the auditor's public key
 * @param auditor_url URL of the auditor
 * @param mpub the exchange's public key (as expected by the auditor)
 * @param dki_len length of @a dki and @a asigs
 * @param asigs array with the auditor's signatures, of length @a dki_len
 * @param dki array of denomination coin data signed by the auditor
 * @return #GNUNET_OK to continue to iterate,
 *  #GNUNET_NO to stop iteration with no error,
 *  #GNUNET_SYSERR to abort iteration with error!
 */
static int
reload_auditor_iter (void *cls,
                     const struct TALER_AuditorPublicKeyP *apub,
                     const char *auditor_url,
                     const struct TALER_MasterPublicKeyP *mpub,
                     unsigned int dki_len,
                     const struct TALER_AuditorSignatureP *asigs,
                     const struct TALER_DenominationKeyValidityPS *dki)
{
  struct ResponseFactoryContext *rfc = cls;
  struct TEH_KS_StateHandle *key_state = rfc->key_state;

  /* Check if the signature is at least for this exchange. */
  if (0 != memcmp (&mpub->eddsa_pub,
                   &TEH_master_public_key,
                   sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Auditing information provided for a different exchange, ignored\n");
    return GNUNET_OK;
  }
  /* Filter the auditor information for those for which the
     keys actually match the denomination keys that are active right now */
  for (unsigned int i = 0; i<dki_len; i++)
  {
    int matched;

    if (GNUNET_YES !=
        GNUNET_CONTAINER_multihashmap_contains (key_state->denomkey_map,
                                                &dki[i].denom_hash))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Found auditor signature for DK `%s', but key is not in active map\n",
                  GNUNET_h2s (&dki[i].denom_hash));
      continue;
    }
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Found auditor signature for DK `%s'\n",
                GNUNET_h2s (&dki[i].denom_hash));
    /* Note: the array is sorted, we could theoretically
       speed this up using a binary search. */
    matched = GNUNET_NO;
    for (unsigned int j = 0; j<rfc->denomkey_array_length; j++)
    {
      struct DenominationKeyEntry *dke = &rfc->denomkey_array[j];
      struct AuditorSignature *as;

      if (0 !=
          memcmp (&dki[i].denom_hash,
                  &dke->dki->issue.properties.denom_hash,
                  sizeof (struct GNUNET_HashCode)))
        continue;
      if (0 !=
          memcmp (&dki[i],
                  &dke->dki->issue.properties,
                  sizeof (struct TALER_DenominationKeyValidityPS)))
      {
        /* if the hash is the same, the properties should also match! */
        GNUNET_break (0);
        continue;
      }
      as = GNUNET_malloc (sizeof (struct AuditorSignature)
                          + strlen (auditor_url) + 1);
      as->asig = asigs[i];
      as->apub = *apub;
      as->auditor_url = (const char *) &as[1];
      memcpy (&as[1],
              auditor_url,
              strlen (auditor_url) + 1);
      GNUNET_CONTAINER_DLL_insert (dke->as_head,
                                   dke->as_tail,
                                   as);
      matched = GNUNET_YES;
      break;
    }
    if (GNUNET_NO == matched)
    {
      GNUNET_break (0);
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "DK `%s' is in active map, but not in array!?\n",
                  GNUNET_h2s (&dki[i].denom_hash));
    }
  }
  return GNUNET_OK;
}


/**
 * Initialize the `denomkey_array`.  We are called once per
 * array index, which is tracked in `denomkey_array_length` (the
 * array will be of sufficient size).  Set the pointer to the
 * denomination key and increment the `denomkey_array_length`.
 *
 * @param cls a `struct ResponseFactoryContext`
 * @param denom_hash hash of a denomination key
 * @param value a `struct TALER_EXCHANGEDB_DenominationKeyIssueInformation *`
 * @return #GNUNET_OK
 */
static int
initialize_denomkey_array (void *cls,
                           const struct GNUNET_HashCode *denom_hash,
                           void *value)
{
  struct ResponseFactoryContext *rfc = cls;
  struct TALER_EXCHANGEDB_DenominationKeyIssueInformation *dki = value;

  rfc->denomkey_array[rfc->denomkey_array_length].denom_key_hash = *denom_hash;
  rfc->denomkey_array[rfc->denomkey_array_length++].dki = dki;
  return GNUNET_OK;
}


/**
 * Comparator used to sort the `struct DenominationKeyEntry` array
 * by the validity period's starting time of the keys.
 *
 * @param k1 a `struct DenominationKeyEntry *`
 * @param k2 a `struct DenominationKeyEntry *`
 * @return -1 if k1 starts before k2,
 *          1 if k2 starts before k1,
 *          0 if they start at the same time
 */
static int
denomkey_array_sort_comparator (const void *k1,
                                const void *k2)
{
  const struct DenominationKeyEntry *dke1 = k1;
  const struct DenominationKeyEntry *dke2 = k2;
  struct GNUNET_TIME_Absolute d1
    = GNUNET_TIME_absolute_ntoh (dke1->dki->issue.properties.start);
  struct GNUNET_TIME_Absolute d2
    = GNUNET_TIME_absolute_ntoh (dke2->dki->issue.properties.start);

  if (d1.abs_value_us < d2.abs_value_us)
    return -1;
  if (d1.abs_value_us > d2.abs_value_us)
    return 1;
  return 0;
}


/**
 * Produce HTTP "Date:" header.
 *
 * @param at time to write to @a date
 * @param[out] date where to write the header, with
 *        at least 128 bytes available space.
 */
static void
get_date_string (struct GNUNET_TIME_Absolute at,
                 char *date)
{
  static const char *const days[] =
  { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  static const char *const mons[] =
  { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
    "Nov", "Dec"};
  struct tm now;
  time_t t;
#if ! defined(HAVE_C11_GMTIME_S) && ! defined(HAVE_W32_GMTIME_S) && \
  ! defined(HAVE_GMTIME_R)
  struct tm*pNow;
#endif

  date[0] = 0;
  t = (time_t) (at.abs_value_us / 1000LL / 1000LL);
#if defined(HAVE_C11_GMTIME_S)
  if (NULL == gmtime_s (&t, &now))
    return;
#elif defined(HAVE_W32_GMTIME_S)
  if (0 != gmtime_s (&now, &t))
    return;
#elif defined(HAVE_GMTIME_R)
  if (NULL == gmtime_r (&t, &now))
    return;
#else
  pNow = gmtime (&t);
  if (NULL == pNow)
    return;
  now = *pNow;
#endif
  sprintf (date,
           "%3s, %02u %3s %04u %02u:%02u:%02u GMT",
           days[now.tm_wday % 7],
           (unsigned int) now.tm_mday,
           mons[now.tm_mon % 12],
           (unsigned int) (1900 + now.tm_year),
           (unsigned int) now.tm_hour,
           (unsigned int) now.tm_min,
           (unsigned int) now.tm_sec);
}


/**
 * Add the headers we want to set for every /keys response.
 *
 * @param key_state the key state to use
 * @param[in,out] response the response to modify
 * @return #GNUNET_OK on success
 */
static int
setup_general_response_headers (const struct TEH_KS_StateHandle *key_state,
                                struct MHD_Response *response)
{
  char dat[128];

  TALER_MHD_add_global_headers (response);
  GNUNET_break (MHD_YES ==
                MHD_add_response_header (response,
                                         MHD_HTTP_HEADER_CONTENT_TYPE,
                                         "application/json"));
  get_date_string (key_state->reload_time,
                   dat);
  GNUNET_break (MHD_YES ==
                MHD_add_response_header (response,
                                         MHD_HTTP_HEADER_LAST_MODIFIED,
                                         dat));
  if (0 != key_state->next_reload.abs_value_us)
  {
    struct GNUNET_TIME_Absolute m;

    m = GNUNET_TIME_relative_to_absolute (max_keys_caching);
    m = GNUNET_TIME_absolute_min (m,
                                  key_state->next_reload);
    get_date_string (m,
                     dat);
    // FIXME: setting 'm' to FOREVER here exposes
    // a crash-bug in lib/ where we access /keys
    // data after it was already free'd!
    GNUNET_break (MHD_YES ==
                  MHD_add_response_header (response,
                                           MHD_HTTP_HEADER_EXPIRES,
                                           dat));
  }
  return GNUNET_OK;
}


/**
 * Information about an auditor to be added.
 */
struct AuditorEntry
{
  /**
   * URL of the auditor (allocated still as part of a
   * `struct AuditorSignature`, do not free!).
   */
  const char *auditor_url;

  /**
   * Public key of the auditor (allocated still as part of a
   * `struct AuditorSignature`, do not free!).
   */
  const struct TALER_AuditorPublicKeyP *apub;

  /**
   * Array of denomination keys and auditor signatures.
   */
  json_t *ar;

};


/**
 * Convert auditor entries from the hash map to entries
 * in the auditor array, free the auditor entry as well.
 *
 * @param cls a `struct ResponseBuilderContext *`
 * @param key unused
 * @param value a `struct AuditorEntry` to add to the `auditors_array`
 * @return #GNUNET_OK (to continue to iterate)
 */
static int
add_auditor_entry (void *cls,
                   const struct GNUNET_HashCode *key,
                   void *value)
{
  struct ResponseBuilderContext *rbc = cls;
  struct AuditorEntry *ae = value;
  json_t *ao;

  (void) key;
  ao = json_pack ("{s:o, s:s, s:o}",
                  "denomination_keys", ae->ar,
                  "auditor_url", ae->auditor_url,
                  "auditor_pub", GNUNET_JSON_from_data_auto (ae->apub));
  GNUNET_assert (NULL != ao);
  GNUNET_assert (0 ==
                 json_array_append_new (rbc->auditors_array,
                                        ao));
  GNUNET_free (ae);
  return GNUNET_OK;
}


/**
 * Initialize @a krd for the given @a cherry_pick_date using
 * the key data in @a rfc.  This function actually builds the
 * respective JSON replies (compressed and uncompressed).
 *
 * @param rfc factory with key material
 * @param[out] krd response object to initialize
 * @param denom_off offset in the @a rfc's `denomkey_array` at which
 *        keys beyond the @a cherry_pick_date are stored
 * @param cherry_pick_date cut-off date to use
 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
 */
static int
build_keys_response (const struct ResponseFactoryContext *rfc,
                     struct KeysResponseData *krd,
                     unsigned int denom_off,
                     struct GNUNET_TIME_Absolute cherry_pick_date)
{
  struct ResponseBuilderContext rbc;
  json_t *keys;
  struct TALER_ExchangeKeySetPS ks;
  struct TALER_ExchangeSignatureP sig;
  char *keys_json;
  struct GNUNET_TIME_Relative reserve_closing_delay;
  void *keys_jsonz;
  size_t keys_jsonz_size;
  int comp;

  krd->cherry_pick_date = cherry_pick_date;

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Creating /keys for cherry pick date %s\n",
              GNUNET_STRINGS_absolute_time_to_string (cherry_pick_date));

  /* Initialize `rbc` */
  memset (&rbc,
          0,
          sizeof (rbc));
  rbc.denom_keys_array = json_array ();
  if (NULL == rbc.denom_keys_array)
  {
    GNUNET_break (0);
    return GNUNET_SYSERR;
  }
  rbc.auditors_array = json_array ();
  if (NULL == rbc.auditors_array)
  {
    destroy_response_builder (&rbc);
    GNUNET_break (0);
    return GNUNET_SYSERR;
  }
  rbc.hash_context = GNUNET_CRYPTO_hash_context_start ();

  /* Go over relevant denomination keys. */
  {
    struct GNUNET_CONTAINER_MultiHashMap *auditors;

    auditors = GNUNET_CONTAINER_multihashmap_create (4,
                                                     GNUNET_NO);
    for (unsigned int i = denom_off; i<rfc->denomkey_array_length; i++)
    {
      /* Add denomination key to the response */
      const struct DenominationKeyEntry *dke
        = &rfc->denomkey_array[i];
      const struct GNUNET_HashCode *denom_key_hash
        = &dke->denom_key_hash;

      GNUNET_CRYPTO_hash_context_read (rbc.hash_context,
                                       denom_key_hash,
                                       sizeof (struct GNUNET_HashCode));
      if (0 !=
          json_array_append_new (rbc.denom_keys_array,
                                 denom_key_issue_to_json (&dke->dki->denom_pub,
                                                          &dke->dki->issue)))
      {
        GNUNET_break (0);
        destroy_response_builder (&rbc);
        return GNUNET_SYSERR;
      }

      /* Add auditor data */
      for (const struct AuditorSignature *as = dke->as_head;
           NULL != as;
           as = as->next)
      {
        struct GNUNET_HashCode ahash;
        struct AuditorEntry *ae;

        GNUNET_CRYPTO_hash (&as->apub,
                            sizeof (as->apub),
                            &ahash);
        ae = GNUNET_CONTAINER_multihashmap_get (auditors,
                                                &ahash);
        if (NULL == ae)
        {
          ae = GNUNET_new (struct AuditorEntry);
          ae->auditor_url = as->auditor_url;
          ae->ar = json_array ();
          ae->apub = &as->apub;
          GNUNET_assert (GNUNET_YES ==
                         GNUNET_CONTAINER_multihashmap_put (auditors,
                                                            &ahash,
                                                            ae,
                                                            GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
        }
        GNUNET_assert (0 ==
                       json_array_append_new (ae->ar,
                                              json_pack ("{s:o, s:o}",
                                                         "denom_pub_h",
                                                         GNUNET_JSON_from_data_auto (
                                                           denom_key_hash),
                                                         "auditor_sig",
                                                         GNUNET_JSON_from_data_auto (
                                                           &as->asig))));
      }
    }

    GNUNET_CONTAINER_multihashmap_iterate (auditors,
                                           &add_auditor_entry,
                                           &rbc);
    GNUNET_CONTAINER_multihashmap_destroy (auditors);
  }

  /* Sign hash over denomination keys */
  ks.purpose.size = htonl (sizeof (ks));
  ks.purpose.purpose = htonl (TALER_SIGNATURE_EXCHANGE_KEY_SET);
  ks.list_issue_date = GNUNET_TIME_absolute_hton (rfc->key_state->reload_time);
  GNUNET_CRYPTO_hash_context_finish (rbc.hash_context,
                                     &ks.hc);
  rbc.hash_context = NULL;
  GNUNET_assert (GNUNET_OK ==
                 GNUNET_CRYPTO_eddsa_sign (
                   &rfc->key_state->current_sign_key_issue.signkey_priv.
                   eddsa_priv,
                   &ks.purpose,
                   &sig.eddsa_signature));
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_time (cfg,
                                           "exchangedb",
                                           "IDLE_RESERVE_EXPIRATION_TIME",
                                           &reserve_closing_delay))
  {
    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
                               "exchangedb",
                               "IDLE_RESERVE_EXPIRATION_TIME");
    /* use default */
    reserve_closing_delay = GNUNET_TIME_relative_multiply (
      GNUNET_TIME_UNIT_WEEKS,
      4);
  }
  /* Build /keys response */
  keys = json_pack ("{s:s, s:o, s:o, s:O, s:O,"
                    " s:o, s:o, s:o, s:o, s:o}",
                    /* 1-5 */
                    "version", TALER_PROTOCOL_VERSION,
                    "master_public_key", GNUNET_JSON_from_data_auto (
                      &TEH_master_public_key),
                    "reserve_closing_delay", GNUNET_JSON_from_time_rel (
                      reserve_closing_delay),
                    "signkeys", rfc->sign_keys_array,
                    "payback", rfc->payback_array,
                    /* 6-10 */
                    "denoms", rbc.denom_keys_array,
                    "auditors", rbc.auditors_array,
                    "list_issue_date", GNUNET_JSON_from_time_abs (
                      rfc->key_state->reload_time),
                    "eddsa_pub", GNUNET_JSON_from_data_auto (
                      &rfc->key_state->current_sign_key_issue.issue.signkey_pub),
                    "eddsa_sig", GNUNET_JSON_from_data_auto (&sig));
  if (NULL == keys)
  {
    destroy_response_builder (&rbc);
    GNUNET_break (0);
    return GNUNET_SYSERR;
  }
  rbc.denom_keys_array = NULL;
  rbc.auditors_array = NULL;
  destroy_response_builder (&rbc);

  /* Convert /keys response to UTF8-String */
  keys_json = json_dumps (keys,
                          JSON_INDENT (2));
  json_decref (keys);
  if (NULL == keys_json)
  {
    GNUNET_break (0);
    return GNUNET_SYSERR;
  }
  /* Keep copy for later compression... */
  keys_jsonz = GNUNET_strdup (keys_json);
  keys_jsonz_size = strlen (keys_json);

  /* Create uncompressed response */
  krd->response_uncompressed
    = MHD_create_response_from_buffer (keys_jsonz_size,
                                       keys_json,
                                       MHD_RESPMEM_MUST_FREE);
  if (NULL == krd->response_uncompressed)
  {
    GNUNET_break (0);
    GNUNET_free (keys_json);
    GNUNET_free (keys_jsonz);
    return GNUNET_SYSERR;
  }
  if (GNUNET_OK !=
      setup_general_response_headers (rfc->key_state,
                                      krd->response_uncompressed))
  {
    GNUNET_break (0);
    GNUNET_free (keys_jsonz);
    return GNUNET_SYSERR;
  }

  /* Also compute compressed version of /keys response */
  comp = TALER_MHD_body_compress (&keys_jsonz,
                                  &keys_jsonz_size);
  krd->response_compressed
    = MHD_create_response_from_buffer (keys_jsonz_size,
                                       keys_jsonz,
                                       MHD_RESPMEM_MUST_FREE);
  if (NULL == krd->response_compressed)
  {
    GNUNET_break (0);
    GNUNET_free (keys_jsonz);
    return GNUNET_SYSERR;
  }
  /* If the response is actually compressed, set the
     respective header. */
  if ( (MHD_YES == comp) &&
       (MHD_YES !=
        MHD_add_response_header (krd->response_compressed,
                                 MHD_HTTP_HEADER_CONTENT_ENCODING,
                                 "deflate")) )
  {
    GNUNET_break (0);
    return GNUNET_SYSERR;
  }
  if (GNUNET_OK !=
      setup_general_response_headers (rfc->key_state,
                                      krd->response_compressed))
  {
    GNUNET_break (0);
    return GNUNET_SYSERR;
  }
  return GNUNET_OK;
}


/**
 * Function called with information about the exchange's denomination
 * keys based on what is known in the database. Used to learn our
 * public keys (after the private keys are deleted, we still need to
 * have the public keys around for a while to verify signatures).
 *
 * This function checks if the @a denom_pub is already known to us,
 * and if not adds it to our set.
 *
 * @parma cls closure, a `struct ResponseFactoryContext *`
 * @param denom_pub public key of the denomination
 * @param issue detailed information about the denomination (value, expiration times, fees)
 */
static void
reload_public_denoms_cb (void *cls,
                         const struct TALER_DenominationPublicKey *denom_pub,
                         const struct
                         TALER_EXCHANGEDB_DenominationKeyInformationP *issue)
{
  struct ResponseFactoryContext *rfc = cls;
  struct TALER_EXCHANGEDB_DenominationKeyIssueInformation dki;
  int ret;

  if (rfc->now.abs_value_us > GNUNET_TIME_absolute_ntoh
        (issue->properties.expire_legal).abs_value_us)
  {
    /* Expired key, discard.  */
    return;
  }

  if (NULL !=
      GNUNET_CONTAINER_multihashmap_get (rfc->key_state->denomkey_map,
                                         &issue->properties.denom_hash))
    return; /* exists / known */
  if (NULL !=
      GNUNET_CONTAINER_multihashmap_get (rfc->key_state->revoked_map,
                                         &issue->properties.denom_hash))
    return; /* exists / known */
  /* zero-out, just for future-proofing */
  memset (&dki,
          0,
          sizeof (dki));
  dki.denom_priv.rsa_private_key = NULL; /* not available! */
  dki.denom_pub.rsa_public_key   = denom_pub->rsa_public_key;
  dki.issue = *issue;
  ret = store_in_map (rfc->key_state->denomkey_map,
                      &dki /* makes a deep copy of dki */);
  if (GNUNET_SYSERR == ret)
  {
    GNUNET_break (0);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Signature wrong on denomination key `%s' (skipping)!\n",
                GNUNET_h2s (&issue->properties.denom_hash));
    return;
  }
  /* we can assert here as we checked for duplicates just above */
  GNUNET_assert (GNUNET_OK == ret);
}


/**
 * Actual "main" logic that builds the state which this module
 * evolves around.  This function will import the key data from
 * the exchangedb module and convert it into (1) internally used
 * lookup tables, and (2) HTTP responses to be returned from
 * /keys.
 *
 * @return NULL on error (usually pretty fatal...)
 */
static struct TEH_KS_StateHandle *
make_fresh_key_state (struct GNUNET_TIME_Absolute now)
{
  struct TEH_KS_StateHandle *key_state;
  struct ResponseFactoryContext rfc;
  struct GNUNET_TIME_Absolute last;
  unsigned int off;
  enum GNUNET_DB_QueryStatus qs;

  memset (&rfc,
          0,
          sizeof (rfc));
  rfc.payback_array = json_array ();
  if (NULL == rfc.payback_array)
  {
    GNUNET_break (0);
    return NULL;
  }
  rfc.sign_keys_array = json_array ();
  if (NULL == rfc.sign_keys_array)
  {
    GNUNET_break (0);
    json_decref (rfc.payback_array);
    return NULL;
  }

  key_state = GNUNET_new (struct TEH_KS_StateHandle);
  rfc.key_state = key_state;
  rfc.now = now;
  key_state->min_dk_expire = GNUNET_TIME_UNIT_FOREVER_ABS;
  key_state->denomkey_map = GNUNET_CONTAINER_multihashmap_create (32,
                                                                  GNUNET_NO);
  key_state->revoked_map = GNUNET_CONTAINER_multihashmap_create (4,
                                                                 GNUNET_NO);
  key_state->reload_time = GNUNET_TIME_absolute_get ();
  GNUNET_TIME_round_abs (&key_state->reload_time);

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Loading keys from `%s'\n",
              TEH_exchange_directory);
  /* Initialize the 'denomkey_map' and the 'revoked_map' and
     'rfc.payback_array' */
  if (-1 ==
      TALER_EXCHANGEDB_denomination_keys_iterate (TEH_exchange_directory,
                                                  &reload_keys_denom_iter,
                                                  &rfc))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to load denomination keys from `%s'.\n",
                TEH_exchange_directory);
    key_state->refcnt = 1;
    ks_release (key_state);
    json_decref (rfc.payback_array);
    json_decref (rfc.sign_keys_array);
    return NULL;
  }

  /* We do not get expired DKIs from
     TALER_EXCHANGEDB_denomination_keys_iterate(), so we must fetch
     the old keys (where we only have the public keys) from the
     database! */
  qs = TEH_plugin->iterate_denomination_info (TEH_plugin->cls,
                                              &reload_public_denoms_cb,
                                              &rfc);
  GNUNET_break (0 <= qs); /* warn, but continue, fingers crossed */

  /* process revocations */
  if (-1 ==
      TALER_EXCHANGEDB_revocations_iterate (TEH_revocation_directory,
                                            &TEH_master_public_key,
                                            &revocations_iter,
                                            &rfc))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to load denomination keys from `%s'.\n",
                TEH_exchange_directory);
    key_state->refcnt = 1;
    ks_release (key_state);
    json_decref (rfc.payback_array);
    json_decref (rfc.sign_keys_array);
    return NULL;
  }

  /* Initialize `current_sign_key_issue` and `rfc.sign_keys_array` */
  TALER_EXCHANGEDB_signing_keys_iterate (TEH_exchange_directory,
                                         &reload_keys_sign_iter,
                                         &rfc);
  if (0 !=
      memcmp (&key_state->current_sign_key_issue.issue.master_public_key,
              &TEH_master_public_key,
              sizeof (struct TALER_MasterPublicKeyP)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Have no signing key. Bad configuration.\n");
    key_state->refcnt = 1;
    ks_release (key_state);
    destroy_response_factory (&rfc);
    return NULL;
  }

  /* sanity check */
  if (0 == GNUNET_CONTAINER_multihashmap_size (key_state->denomkey_map))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Have no denomination keys. Bad configuration.\n");
    key_state->refcnt = 1;
    ks_release (key_state);
    destroy_response_factory (&rfc);
    return NULL;
  }

  /* Initialize and sort the `denomkey_array` */
  rfc.denomkey_array
    = GNUNET_new_array (GNUNET_CONTAINER_multihashmap_size (
                          key_state->denomkey_map),
                        struct DenominationKeyEntry);
  GNUNET_CONTAINER_multihashmap_iterate (key_state->denomkey_map,
                                         &initialize_denomkey_array,
                                         &rfc);
  GNUNET_assert (rfc.denomkey_array_length ==
                 GNUNET_CONTAINER_multihashmap_size (key_state->denomkey_map));
  qsort (rfc.denomkey_array,
         rfc.denomkey_array_length,
         sizeof (struct DenominationKeyEntry),
         &denomkey_array_sort_comparator);

  /* Complete `denomkey_array` by adding auditor signature data */
  TALER_EXCHANGEDB_auditor_iterate (cfg,
                                    &reload_auditor_iter,
                                    &rfc);
  /* Sanity check: do we have auditors for all denomination keys? */
  for (unsigned int i = 0; i<rfc.denomkey_array_length; i++)
  {
    const struct DenominationKeyEntry *dke
      = &rfc.denomkey_array[i];

    if (NULL == dke->as_head)
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  "Denomination key `%s' at %p not signed by any auditor!\n",
                  GNUNET_h2s (&dke->denom_key_hash),
                  dke);
  }

  /* Determine size of `krd_array` by counting number of discrete
     denomination key starting times. */
  last = GNUNET_TIME_UNIT_ZERO_ABS;
  key_state->krd_array_length = 0;
  off = 1; /* reserve one slot for the "no keys" response */
  for (unsigned int i = 0; i<rfc.denomkey_array_length; i++)
  {
    const struct DenominationKeyEntry *dke
      = &rfc.denomkey_array[i];
    struct GNUNET_TIME_Absolute d
      = GNUNET_TIME_absolute_ntoh (dke->dki->issue.properties.start);

    if (last.abs_value_us == d.abs_value_us)
      continue;
    last = d;
    off++;
  }

  /* Compute next automatic reload time */
  key_state->next_reload =
    GNUNET_TIME_absolute_min (GNUNET_TIME_absolute_ntoh (
                                key_state->current_sign_key_issue.issue.expire),
                              key_state->min_dk_expire);
  GNUNET_assert (0 != key_state->next_reload.abs_value_us);


  /* Initialize `krd_array` */
  key_state->krd_array_length = off;
  key_state->krd_array
    = GNUNET_new_array (key_state->krd_array_length,
                        struct KeysResponseData);
  off = 0;
  last = GNUNET_TIME_UNIT_ZERO_ABS;
  for (unsigned int i = 0; i<rfc.denomkey_array_length; i++)
  {
    const struct DenominationKeyEntry *dke
      = &rfc.denomkey_array[i];
    struct GNUNET_TIME_Absolute d
      = GNUNET_TIME_absolute_ntoh (dke->dki->issue.properties.start);

    if (last.abs_value_us == d.abs_value_us)
      continue;
    if (GNUNET_OK !=
        build_keys_response (&rfc,
                             &key_state->krd_array[off++],
                             i,
                             last))
    {
      /* Fail hard, will be caught via test on `off` below */
      GNUNET_break (0);
      off = key_state->krd_array_length; /* flag as 'invalid' */
      break;
    }
    last = d;
  }

  /* Finally, build an `empty` response without denomination keys
     for requests past the last known denomination key start date */
  if ( (off + 1 < key_state->krd_array_length) ||
       (GNUNET_OK !=
        build_keys_response (&rfc,
                             &key_state->krd_array[off++],
                             rfc.denomkey_array_length,
                             last)) )
  {
    GNUNET_break (0);
    key_state->refcnt = 1;
    ks_release (key_state);
    destroy_response_factory (&rfc);
    return NULL;
  }

  /* Clean up intermediary state we don't need anymore and return
     new key_state! */
  destroy_response_factory (&rfc);
  return key_state;
}


/* ************************** Persistent part ********************** */

/**
 * Release key state, free if necessary (if reference count gets to zero).
 *
 * @param location name of the function in which the lock is acquired
 * @param key_state the key state to release
 */
void
TEH_KS_release_ (const char *location,
                 struct TEH_KS_StateHandle *key_state)
{
  GNUNET_assert (0 == pthread_mutex_lock (&internal_key_state_mutex));
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "KS released at %s (%p/%d)\n",
              location,
              key_state,
              key_state->refcnt);
  ks_release (key_state);
  GNUNET_assert (0 == pthread_mutex_unlock (&internal_key_state_mutex));
}


/**
 * Acquire the key state of the exchange.  Updates keys if necessary.
 * For every call to #TEH_KS_acquire(), a matching call
 * to #TEH_KS_release() must be made.
 *
 * @param location name of the function in which the lock is acquired
 * @return the key state, NULL on error (usually pretty fatal)
 */
struct TEH_KS_StateHandle *
TEH_KS_acquire_ (struct GNUNET_TIME_Absolute now,
                 const char *location)
{
  struct TEH_KS_StateHandle *key_state;
  unsigned int rcd;

  GNUNET_assert (0 == pthread_mutex_lock (&internal_key_state_mutex));
  rcd = 0;
  if ( (NULL != internal_key_state) &&
       (internal_key_state->next_reload.abs_value_us <= now.abs_value_us) )
  {
    struct TEH_KS_StateHandle *ks = internal_key_state;

    internal_key_state = NULL;
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "KS released in acquire due to expiration\n");
    ks_release (ks);
    rcd = 1; /* remember that we released 'internal_key_state' */
  }
  if (NULL == internal_key_state)
  {
    internal_key_state = make_fresh_key_state (now);
    /* bump RC by 1 if we released internal_key_state above */
    if (NULL == internal_key_state)
    {
      GNUNET_assert (0 == pthread_mutex_unlock (&internal_key_state_mutex));
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "Failed to initialize key state\n");
      return NULL;
    }
    internal_key_state->refcnt += rcd;
  }
  key_state = internal_key_state;
  if (NULL != key_state)
  {
    key_state->refcnt++;
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "KS acquired at %s (%p/%d)\n",
                location,
                key_state,
                key_state->refcnt);
  }
  else
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "KS acquire failed at %s\n",
                location);
  }
  GNUNET_assert (0 == pthread_mutex_unlock (&internal_key_state_mutex));

  return key_state;
}


/**
 * Look up the issue for a denom public key.  Note that the result
 * is only valid while the @a key_state is not released!
 *
 * @param key_state state to look in
 * @param denom_pub_hash hash of denomination public key
 * @param use purpose for which the key is being located
 * @param ec[out] set to the error code, in case the operation failed
 * @param hc[out] set to the HTTP status code to use
 * @return the denomination key issue,
 *         or NULL if denom_pub could not be found (or is not valid at this time for the given @a use)
 */
struct TALER_EXCHANGEDB_DenominationKeyIssueInformation *
TEH_KS_denomination_key_lookup_by_hash (const struct
                                        TEH_KS_StateHandle *key_state,
                                        const struct
                                        GNUNET_HashCode *denom_pub_hash,
                                        enum TEH_KS_DenominationKeyUse use,
                                        enum TALER_ErrorCode *ec,
                                        unsigned int *hc)
{
  struct TALER_EXCHANGEDB_DenominationKeyIssueInformation *dki;
  struct GNUNET_TIME_Absolute now;
  const struct GNUNET_CONTAINER_MultiHashMap *map;

  map = (TEH_KS_DKU_PAYBACK == use) ? key_state->revoked_map :
        key_state->denomkey_map;
  dki = GNUNET_CONTAINER_multihashmap_get (map,
                                           denom_pub_hash);
  if ( (NULL == dki) && (TEH_KS_DKU_ZOMBIE == use))
    dki = GNUNET_CONTAINER_multihashmap_get (key_state->revoked_map,
                                             denom_pub_hash);
  if (NULL == dki)
  {
    *hc = MHD_HTTP_NOT_FOUND;
    switch (use)
    {
    case TEH_KS_DKU_PAYBACK:
      *ec = TALER_EC_PAYBACK_DENOMINATION_KEY_UNKNOWN;
      break;
    case TEH_KS_DKU_ZOMBIE:
      *ec = TALER_EC_REFRESH_PAYBACK_DENOMINATION_KEY_NOT_FOUND;
      break;
    case TEH_KS_DKU_WITHDRAW:
      *ec = TALER_EC_WITHDRAW_DENOMINATION_KEY_NOT_FOUND;
      break;
    case TEH_KS_DKU_DEPOSIT:
      *ec = TALER_EC_DEPOSIT_DENOMINATION_KEY_UNKNOWN;
      break;
    }
    return NULL;
  }
  now = GNUNET_TIME_absolute_get ();
  if (now.abs_value_us <
      GNUNET_TIME_absolute_ntoh (dki->issue.properties.start).abs_value_us)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "Not returning DKI for %s, as start time is in the future\n",
                GNUNET_h2s (denom_pub_hash));
    *hc = MHD_HTTP_PRECONDITION_FAILED;
    switch (use)
    {
    case TEH_KS_DKU_PAYBACK:
      *ec = TALER_EC_PAYBACK_DENOMINATION_VALIDITY_IN_FUTURE;
      break;
    case TEH_KS_DKU_ZOMBIE:
      *ec = TALER_EC_REFRESH_PAYBACK_DENOMINATION_VALIDITY_IN_FUTURE;
      break;
    case TEH_KS_DKU_WITHDRAW:
      *ec = TALER_EC_WITHDRAW_VALIDITY_IN_FUTURE;
      break;
    case TEH_KS_DKU_DEPOSIT:
      *ec = TALER_EC_DEPOSIT_DENOMINATION_VALIDITY_IN_FUTURE;
      break;
    }
    return NULL;
  }
  now = GNUNET_TIME_absolute_get ();
  switch (use)
  {
  case TEH_KS_DKU_WITHDRAW:
    if (now.abs_value_us >
        GNUNET_TIME_absolute_ntoh (
          dki->issue.properties.expire_withdraw).abs_value_us)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "Not returning DKI for %s, as time to create coins has passed\n",
                  GNUNET_h2s (denom_pub_hash));
      *ec = TALER_EC_WITHDRAW_VALIDITY_IN_PAST;
      *hc = MHD_HTTP_GONE;
      return NULL;
    }
    if (NULL == dki->denom_priv.rsa_private_key)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "Not returning DKI of %s for WITHDRAW operation as we lack the private key, even though the withdraw period did not yet expire!\n",
                  GNUNET_h2s (denom_pub_hash));
      *ec = TALER_EC_DENOMINATION_KEY_LOST;
      *hc = MHD_HTTP_SERVICE_UNAVAILABLE;
      return NULL;
    }
    break;
  case TEH_KS_DKU_DEPOSIT:
    if (now.abs_value_us >
        GNUNET_TIME_absolute_ntoh (
          dki->issue.properties.expire_deposit).abs_value_us)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "Not returning DKI for %s, as time to spend coin has passed\n",
                  GNUNET_h2s (denom_pub_hash));
      *ec = TALER_EC_DEPOSIT_DENOMINATION_EXPIRED;
      *hc = MHD_HTTP_GONE;
      return NULL;
    }
    break;
  case TEH_KS_DKU_PAYBACK:
    if (now.abs_value_us >
        GNUNET_TIME_absolute_ntoh (
          dki->issue.properties.expire_deposit).abs_value_us)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "Not returning DKI for %s, as time to payback coin has passed\n",
                  GNUNET_h2s (denom_pub_hash));
      *ec = TALER_EC_REFRESH_PAYBACK_DENOMINATION_EXPIRED;
      *hc = MHD_HTTP_GONE;
      return NULL;
    }
    break;
  case TEH_KS_DKU_ZOMBIE:
    if (now.abs_value_us >
        GNUNET_TIME_absolute_ntoh (
          dki->issue.properties.expire_legal).abs_value_us)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "Not returning DKI for %s, as legal expiration of coin has passed\n",
                  GNUNET_h2s (denom_pub_hash));
      *ec = TALER_EC_REFRESH_ZOMBIE_DENOMINATION_EXPIRED;
      *hc = MHD_HTTP_GONE;
      return NULL;
    }
    break;
  }
  return dki;
}


/**
 * Call #handle_signal() to pass the received signal via
 * the control pipe.
 */
static void
handle_sigusr1 ()
{
  handle_signal (SIGUSR1);
}


/**
 * Call #handle_signal() to pass the received signal via
 * the control pipe.
 */
static void
handle_sigint ()
{
  handle_signal (SIGINT);
}


/**
 * Call #handle_signal() to pass the received signal via
 * the control pipe.
 */
static void
handle_sigterm ()
{
  handle_signal (SIGTERM);
}


/**
 * Call #handle_signal() to pass the received signal via
 * the control pipe.
 */
static void
handle_sighup ()
{
  handle_signal (SIGHUP);
}


/**
 * Call #handle_signal() to pass the received signal via
 * the control pipe.
 */
static void
handle_sigchld ()
{
  handle_signal (SIGCHLD);
}


/**
 * Read signals from a pipe in a loop, and reload keys from disk if
 * SIGUSR1 is received, terminate if SIGTERM/SIGINT is received, and
 * restart if SIGHUP is received.
 *
 * @return #GNUNET_SYSERR on errors,
 *         #GNUNET_OK to terminate normally
 *         #GNUNET_NO to restart an update version of the binary
 */
int
TEH_KS_loop (void)
{
  struct GNUNET_SIGNAL_Context *sigusr1;
  struct GNUNET_SIGNAL_Context *sigterm;
  struct GNUNET_SIGNAL_Context *sigint;
  struct GNUNET_SIGNAL_Context *sighup;
  struct GNUNET_SIGNAL_Context *sigchld;
  int ret;

  if (0 != pipe (reload_pipe))
  {
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
                         "pipe");
    return GNUNET_SYSERR;
  }
  sigusr1 = GNUNET_SIGNAL_handler_install (SIGUSR1,
                                           &handle_sigusr1);
  sigterm = GNUNET_SIGNAL_handler_install (SIGTERM,
                                           &handle_sigterm);
  sigint = GNUNET_SIGNAL_handler_install (SIGINT,
                                          &handle_sigint);
  sighup = GNUNET_SIGNAL_handler_install (SIGHUP,
                                          &handle_sighup);
  sigchld = GNUNET_SIGNAL_handler_install (SIGCHLD,
                                           &handle_sigchld);

  ret = 2;
  while (2 == ret)
  {
    char c;
    ssize_t res;

    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "(re-)loading keys\n");
    if (NULL != internal_key_state)
    {
      struct TEH_KS_StateHandle *ks = internal_key_state;

      internal_key_state = NULL;
      TEH_KS_release (ks);
    }
    /* This will re-initialize 'internal_key_state' with
       an initial refcnt of 1 */
    if (NULL == TEH_KS_acquire (GNUNET_TIME_absolute_get ()))
    {
      ret = GNUNET_SYSERR;
      break;
    }
read_again:
    errno = 0;
    res = read (reload_pipe[0],
                &c,
                1);
    if ((res < 0) && (EINTR != errno))
    {
      GNUNET_break (0);
      ret = GNUNET_SYSERR;
      break;
    }
    if (EINTR == errno)
      goto read_again;
    switch (c)
    {
    case SIGUSR1:
      /* reload internal key state, we do this in the loop */
      break;
    case SIGTERM:
    case SIGINT:
      /* terminate */
      ret = GNUNET_OK;
      break;
    case SIGHUP:
      /* restart updated binary */
      ret = GNUNET_NO;
      break;
#if HAVE_DEVELOPER
    case SIGCHLD:
      /* running in test-mode, test finished, terminate */
      ret = GNUNET_OK;
      break;
#endif
    default:
      /* unexpected character */
      GNUNET_break (0);
      break;
    }
  }
  GNUNET_SIGNAL_handler_uninstall (sigusr1);
  GNUNET_SIGNAL_handler_uninstall (sigterm);
  GNUNET_SIGNAL_handler_uninstall (sigint);
  GNUNET_SIGNAL_handler_uninstall (sighup);
  GNUNET_SIGNAL_handler_uninstall (sigchld);
  GNUNET_break (0 == close (reload_pipe[0]));
  GNUNET_break (0 == close (reload_pipe[1]));
  return ret;
}


/**
 * Finally release #internal_key_state.
 */
void
TEH_KS_free ()
{
  if (NULL != internal_key_state)
  {
    struct TEH_KS_StateHandle *ks = internal_key_state;

    internal_key_state = NULL;
    TEH_KS_release (ks);
  }
}


/**
 * Sign the message in @a purpose with the exchange's signing key.
 *
 * FIXME:
 * - Change API to return status code and do not assert on TEH_KS_acquire()
 *   failures, instead allow caller to handle it (i.e. by returning
 *   #TALER_EC_EXCHANGE_BAD_CONFIGURATION to application).
 *
 * @param purpose the message to sign
 * @param[out] pub set to the current public signing key of the exchange
 * @param[out] sig signature over purpose using current signing key
 * @return #GNUNET_OK on success, #GNUNET_SYSERR if we lack key material
 */
int
TEH_KS_sign (const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
             struct TALER_ExchangePublicKeyP *pub,
             struct TALER_ExchangeSignatureP *sig)

{
  struct TEH_KS_StateHandle *key_state;

  key_state = TEH_KS_acquire (GNUNET_TIME_absolute_get ());
  if (NULL == key_state)
  {
    /* This *can* happen if the exchange's keys are
       not properly maintained. */
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _ ("Cannot sign request, no valid keys available\n"));
    return GNUNET_SYSERR;
  }
  *pub = key_state->current_sign_key_issue.issue.signkey_pub;
  GNUNET_assert (GNUNET_OK ==
                 GNUNET_CRYPTO_eddsa_sign (
                   &key_state->current_sign_key_issue.signkey_priv.eddsa_priv,
                   purpose,
                   &sig->eddsa_signature));
  TEH_KS_release (key_state);
  return GNUNET_OK;
}


/**
 * Comparator used for a binary search for @a key in the
 * `struct KeysResponseData` array.
 *
 * @param key pointer to a `struct GNUNET_TIME_Absolute`
 * @param value pointer to a `struct KeysResponseData` array entry
 * @return 0 if time matches, -1 if key is smaller, 1 if key is larger
 */
static int
krd_search_comparator (const void *key,
                       const void *value)
{
  const struct GNUNET_TIME_Absolute *kd = key;
  const struct KeysResponseData *krd = value;

  if (kd->abs_value_us > krd->cherry_pick_date.abs_value_us)
    return 1;
  if (kd->abs_value_us < krd->cherry_pick_date.abs_value_us)
    return -1;
  return 0;
}


/**
 * Function to call to handle the request by sending
 * back static data from the @a rh.
 *
 * @param rh context of the handler
 * @param connection the MHD connection to handle
 * @param[in,out] connection_cls the connection's closure (can be updated)
 * @param upload_data upload data
 * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
 * @return MHD result code
 */
int
TEH_KS_handler_keys (struct TEH_RequestHandler *rh,
                     struct MHD_Connection *connection,
                     void **connection_cls,
                     const char *upload_data,
                     size_t *upload_data_size)
{
  struct TEH_KS_StateHandle *key_state;
  int ret;
  const char *have_cherrypick;
  const char *have_fakenow;
  struct GNUNET_TIME_Absolute last_issue_date;
  struct GNUNET_TIME_Absolute now;
  const struct KeysResponseData *krd;

  (void) connection_cls;
  (void) upload_data;
  (void) upload_data_size;
  have_cherrypick = MHD_lookup_connection_value (connection,
                                                 MHD_GET_ARGUMENT_KIND,
                                                 "last_issue_date");
  if (NULL != have_cherrypick)
  {
    unsigned long long cherrypickn;

    if (1 !=
        sscanf (have_cherrypick,
                "%llu",
                &cherrypickn))
    {
      GNUNET_break_op (0);
      return TALER_MHD_reply_with_error (connection,
                                         MHD_HTTP_BAD_REQUEST,
                                         TALER_EC_KEYS_HAVE_NOT_NUMERIC,
                                         "last_issue_date");
    }
    last_issue_date.abs_value_us = (uint64_t) cherrypickn * 1000000LLU;
  }
  else
  {
    last_issue_date.abs_value_us = 0LLU;
  }
  now = GNUNET_TIME_absolute_get ();
  have_fakenow = MHD_lookup_connection_value (connection,
                                              MHD_GET_ARGUMENT_KIND,
                                              "now");
  if (NULL != have_fakenow)
  {
    unsigned long long fakenown;

    if (1 !=
        sscanf (have_fakenow,
                "%llu",
                &fakenown))
    {
      GNUNET_break_op (0);
      return TALER_MHD_reply_with_error (connection,
                                         MHD_HTTP_BAD_REQUEST,
                                         TALER_EC_KEYS_HAVE_NOT_NUMERIC,
                                         "now");
    }
    now.abs_value_us = (uint64_t) fakenown * 1000000LLU;
  }

  key_state = TEH_KS_acquire (now);
  if (NULL == key_state)
  {
    TALER_LOG_ERROR ("Lacking keys to operate\n");
    return TALER_MHD_reply_with_error (connection,
                                       MHD_HTTP_INTERNAL_SERVER_ERROR,
                                       TALER_EC_EXCHANGE_BAD_CONFIGURATION,
                                       "no keys");
  }
  krd = bsearch (&last_issue_date,
                 key_state->krd_array,
                 key_state->krd_array_length,
                 sizeof (struct KeysResponseData),
                 &krd_search_comparator);

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Filtering /keys by cherry pick date %s found entry %u/%u\n",
              GNUNET_STRINGS_absolute_time_to_string (last_issue_date),
              (unsigned int) (krd - key_state->krd_array),
              key_state->krd_array_length);
  if ( (NULL == krd) &&
       (key_state->krd_array_length > 0) )
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Client provided invalid cherry picking timestamp %s, returning full response\n",
                GNUNET_STRINGS_absolute_time_to_string (last_issue_date));
    krd = &key_state->krd_array[0];
  }
  if (NULL == krd)
  {
    /* FIXME: should return 500 response instead... */
    GNUNET_break (0);
    return MHD_NO;
  }
  ret = MHD_queue_response (connection,
                            rh->response_code,
                            (MHD_YES == TALER_MHD_can_compress (connection))
                            ? krd->response_compressed
                            : krd->response_uncompressed);
  TEH_KS_release (key_state);
  return ret;
}


/* end of taler-exchange-httpd_keystate.c */