summaryrefslogtreecommitdiffstats
path: root/tools/gpg-pair-tool.c
blob: 4054f5d19ad65a18b73d67ed6ded388adf28ab95 (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
/* gpg-pair-tool.c - The tool to run the pairing protocol.
 * Copyright (C) 2018 g10 Code GmbH
 *
 * This file is part of GnuPG.
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This file 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

/* Protocol:
 *
 *    Initiator             Responder
 *       |                     |
 *       |    COMMIT           |
 *       |-------------------->|
 *       |                     |
 *       |    DHPART1          |
 *       |<--------------------|
 *       |                     |
 *       |    DHPART2          |
 *       |-------------------->|
 *       |                     |
 *       |    CONFIRM          |
 *       |<--------------------|
 *       |                     |
 *
 * The initiator creates a keypar (PKi,SKi) and sends this COMMIT
 * message to the responder:
 *
 *   7 byte Magic, value: "GPG-pa1"
 *   1 byte MessageType, value 1 (COMMIT)
 *   8 byte SessionId, value: 8 random bytes
 *   1 byte Realm, value 1
 *   2 byte reserved, value 0
 *   5 byte ExpireTime, value: seconds since Epoch as an unsigned int.
 *  32 byte Hash(PKi)
 *
 * The initiator also needs to locally store the sessionid, the realm,
 * the expiration time, the keypair and a hash of the entire message
 * sent.
 *
 * The responder checks that the received message has not expired and
 * stores sessionid, realm, expiretime and the Hash(PKi).  The
 * Responder then creates and locally stores its own keypair (PKr,SKr)
 * and sends the DHPART1 message back:
 *
 *   7 byte Magic, value: "GPG-pa1"
 *   1 byte MessageType, value 2 (DHPART1)
 *   8 byte SessionId from COMMIT message
 *  32 byte PKr
 *  32 byte Hash(Hash(COMMIT) || DHPART1[0..47])
 *
 * Note that Hash(COMMIT) is the hash over the entire received COMMIT
 * message.  DHPART1[0..47] are the first 48 bytes of the created
 * DHPART1 message.
 *
 * The Initiator receives the DHPART1 message and checks that the hash
 * matches.  Although this hash is easily malleable it is later in the
 * protocol used to assert the integrity of all messages.  The
 * Initiator then computes the shared master secret from its SKi and
 * the received PKr.  Using this master secret several keys are
 * derived:
 *
 *  - HMACi-key using the label "GPG-pa1-HMACi-key".
 *  - SYMx-key using the label "GPG-pa1-SYMx-key"
 *
 * For details on the KDF see the implementation of the function kdf.
 * The master secret is stored securily in the local state.  The
 * DHPART2 message is then created and send to the Responder:
 *
 *   7 byte Magic, value: "GPG-pa1"
 *   1 byte MessageType, value 3 (DHPART2)
 *   8 byte SessionId from COMMIT message
 *  32 byte PKi
 *  32 byte MAC(HMACi-key, Hash(DHPART1) || DHPART2[0..47] || SYMx-key)
 *
 * The Responder receives the DHPART2 message and checks that the hash
 * of the received PKi matches the Hash(PKi) value as received earlier
 * with the COMMIT message.  The Responder now also computes the
 * shared master secret from its SKr and the received PKi and derives
 * the keys:
 *
 *  - HMACi-key using the label "GPG-pa1-HMACi-key".
 *  - HMACr-key using the label "GPG-pa1-HMACr-key".
 *  - SYMx-key using the label "GPG-pa1-SYMx-key"
 *  - SAS using the label "GPG-pa1-SAS"
 *
 * With these keys the MAC from the received DHPART2 message is
 * checked.  On success a SAS is displayed to the user and a CONFIRM
 * message send back:
 *
 *   7 byte Magic, value: "GPG-pa1"
 *   1 byte MessageType, value 4 (CONFIRM)
 *   8 byte SessionId from COMMIT message
 *  32 byte MAC(HMACr-key, Hash(DHPART2) || CONFIRM[0..15] || SYMx-key)
 *
 * The Initiator receives this CONFIRM message, gets the master shared
 * secrey from its local state and derives the keys.  It checks the
 * the MAC in the received CONFIRM message and ask the user to enter
 * the SAS as displayed by the responder.  Iff the SAS matches the
 * master key is flagged as confirmed and the Initiator may now use a
 * derived key to send encrypted data to the Responder.
 *
 * In case the Responder also needs to send encrypted data we need to
 * introduce another final message to tell the responder that the
 * Initiator validated the SAS.
 *
 * TODO:  Encrypt the state files using a key stored in gpg-agent's cache.
 *
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <stdarg.h>

#include "../common/util.h"
#include "../common/status.h"
#include "../common/i18n.h"
#include "../common/sysutils.h"
#include "../common/init.h"
#include "../common/name-value.h"

/* Constants to identify the commands and options. */
enum cmd_and_opt_values
  {
    aNull       = 0,

    oQuiet      = 'q',
    oVerbose	= 'v',
    oOutput     = 'o',
    oArmor      = 'a',

    aInitiate   = 400,
    aRespond    = 401,
    aGet        = 402,
    aCleanup    = 403,

    oDebug      = 500,
    oStatusFD,
    oHomedir,
    oSAS,

    oDummy
  };


/* The list of commands and options. */
static gpgrt_opt_t opts[] = {
  ARGPARSE_group (300, ("@Commands:\n ")),

  ARGPARSE_c (aInitiate, "initiate", N_("initiate a pairing request")),
  ARGPARSE_c (aRespond,  "respond", N_("respond to a pairing request")),
  ARGPARSE_c (aGet,      "get",     N_("return the keys")),
  ARGPARSE_c (aCleanup,  "cleanup", N_("remove expired states etc.")),

  ARGPARSE_group (301, ("@\nOptions:\n ")),

  ARGPARSE_s_n (oVerbose, "verbose", N_("verbose")),
  ARGPARSE_s_n (oQuiet,	"quiet", N_("be somewhat more quiet")),
  ARGPARSE_s_n (oArmor, "armor", N_("create ascii armored output")),
  ARGPARSE_s_s (oSAS, "sas", N_("|SAS|the SAS as shown by the peer")),
  ARGPARSE_s_s (oDebug, "debug", "@"),
  ARGPARSE_s_s (oOutput, "output", N_("|FILE|write the request to FILE")),
  ARGPARSE_s_i (oStatusFD, "status-fd", N_("|FD|write status info to this FD")),

  ARGPARSE_s_s (oHomedir, "homedir", "@"),

  ARGPARSE_end ()
};


/* We keep all global options in the structure OPT.  */
static struct
{
  int verbose;
  unsigned int debug;
  int quiet;
  int armor;
  const char *output;
  estream_t statusfp;
  unsigned int ttl;
  const char *sas;
} opt;


/* Debug values and macros.  */
#define DBG_MESSAGE_VALUE     2 /* Debug the messages.  */
#define DBG_CRYPTO_VALUE      4	/* Debug low level crypto.  */
#define DBG_MEMORY_VALUE     32	/* Debug memory allocation stuff.  */

#define DBG_MESSAGE  (opt.debug & DBG_MESSAGE_VALUE)
#define DBG_CRYPTO   (opt.debug & DBG_CRYPTO_VALUE)


/* The list of supported debug flags.  */
static struct debug_flags_s debug_flags [] =
  {
    { DBG_MESSAGE_VALUE, "message"  },
    { DBG_CRYPTO_VALUE , "crypto"  },
    { DBG_MEMORY_VALUE , "memory"  },
    { 0, NULL }
  };


/* The directory name below the cache dir to store paring states.  */
#define PAIRING_STATE_DIR  "state"

/* Message types.  */
#define MSG_TYPE_COMMIT  1
#define MSG_TYPE_DHPART1 2
#define MSG_TYPE_DHPART2 3
#define MSG_TYPE_CONFIRM 4


/* Realm values.  */
#define REALM_STANDARD  1




/* Local prototypes.  */
static void wrong_args (const char *text) GPGRT_ATTR_NORETURN;
static void xnvc_set_printf (nvc_t nvc, const char *name, const char *format,
                             ...) GPGRT_ATTR_PRINTF(3,4);
static void *hash_data (void *result, size_t resultsize,
                        ...) GPGRT_ATTR_SENTINEL(0);
static void *hmac_data (void *result, size_t resultsize,
                        const unsigned char *key, size_t keylen,
                        ...) GPGRT_ATTR_SENTINEL(0);


static gpg_error_t command_initiate (void);
static gpg_error_t command_respond (void);
static gpg_error_t command_cleanup (void);
static gpg_error_t command_get (const char *sessionidstr);




/* Print usage information and provide strings for help. */
static const char *
my_strusage( int level )
{
  const char *p;

  switch (level)
    {
    case 9:  p = "LGPL-2.1-or-later"; break;
    case 11: p = "gpg-pair-tool"; break;
    case 12: p = "@GNUPG@"; break;
    case 13: p = VERSION; break;
    case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break;
    case 17: p = PRINTABLE_OS_NAME; break;
    case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break;

    case 1:
    case 40:
      p = ("Usage: gpg-pair-tool [command] [options] [args] (-h for help)");
      break;
    case 41:
      p = ("Syntax: gpg-pair-tool [command] [options] [args]\n"
           "Client to run the pairing protocol\n");
      break;

    default: p = NULL; break;
    }
  return p;
}


static void
wrong_args (const char *text)
{
  es_fprintf (es_stderr, _("usage: %s [options] %s\n"),
              gpgrt_strusage (11), text);
  exit (2);
}


/* Set the status FD.  */
static void
set_status_fd (int fd)
{
  static int last_fd = -1;

  if (fd != -1 && last_fd == fd)
    return;

  if (opt.statusfp && opt.statusfp != es_stdout && opt.statusfp != es_stderr)
    es_fclose (opt.statusfp);
  opt.statusfp = NULL;
  if (fd == -1)
    return;

  if (fd == 1)
    opt.statusfp = es_stdout;
  else if (fd == 2)
    opt.statusfp = es_stderr;
  else
    opt.statusfp = es_fdopen (fd, "w");
  if (!opt.statusfp)
    {
      log_fatal ("can't open fd %d for status output: %s\n",
                 fd, gpg_strerror (gpg_error_from_syserror ()));
    }
  last_fd = fd;
}


/* Write a status line with code NO followed by the output of the
 * printf style FORMAT.  The caller needs to make sure that LFs and
 * CRs are not printed.  */
static void
write_status (int no, const char *format, ...)
{
  va_list arg_ptr;

  if (!opt.statusfp)
    return;  /* Not enabled.  */

  es_fputs ("[GNUPG:] ", opt.statusfp);
  es_fputs (get_status_string (no), opt.statusfp);
  if (format)
    {
      es_putc (' ', opt.statusfp);
      va_start (arg_ptr, format);
      es_vfprintf (opt.statusfp, format, arg_ptr);
      va_end (arg_ptr);
    }
  es_putc ('\n', opt.statusfp);
}



/* gpg-pair-tool main. */
int
main (int argc, char **argv)
{
  gpg_error_t err;
  gpgrt_argparse_t pargs = { &argc, &argv };
  enum cmd_and_opt_values cmd = 0;

  opt.ttl = 8*3600; /* Default to 8 hours.  */

  gnupg_reopen_std ("gpg-pair-tool");
  gpgrt_set_strusage (my_strusage);
  log_set_prefix ("gpg-pair-tool", GPGRT_LOG_WITH_PREFIX);

  /* Make sure that our subsystems are ready.  */
  i18n_init();
  init_common_subsystems (&argc, &argv);

  /* Parse the command line. */
  while (gpgrt_argparse (NULL, &pargs, opts))
    {
      switch (pargs.r_opt)
        {
	case oQuiet:     opt.quiet = 1; break;
        case oVerbose:   opt.verbose++; break;
        case oArmor:     opt.armor = 1; break;

        case oDebug:
          if (parse_debug_flag (pargs.r.ret_str, &opt.debug, debug_flags))
            {
              pargs.r_opt = ARGPARSE_INVALID_ARG;
              pargs.err   = ARGPARSE_PRINT_ERROR;
            }
          break;

        case oOutput:
          opt.output = pargs.r.ret_str;
          break;

        case oStatusFD:
          set_status_fd (translate_sys2libc_fd_int (pargs.r.ret_int, 1));
          break;

        case oHomedir:
          gnupg_set_homedir (pargs.r.ret_str);
          break;

        case oSAS:
          opt.sas = pargs.r.ret_str;
          break;

	case aInitiate:
	case aRespond:
	case aGet:
	case aCleanup:
          if (cmd && cmd != pargs.r_opt)
            log_error (_("conflicting commands\n"));
          else
            cmd = pargs.r_opt;
          break;

        default: pargs.err = ARGPARSE_PRINT_WARNING; break;
	}
    }
  gpgrt_argparse (NULL, &pargs, NULL);  /* Release internal state.  */

  /* Print a warning if an argument looks like an option.  */
  if (!opt.quiet && !(pargs.flags & ARGPARSE_FLAG_STOP_SEEN))
    {
      int i;

      for (i=0; i < argc; i++)
        if (argv[i][0] == '-' && argv[i][1] == '-')
          log_info (("NOTE: '%s' is not considered an option\n"), argv[i]);
    }
  gpgrt_argparse (NULL, &pargs, NULL);  /* Free internal memory.  */

  if (opt.sas)
    {
      if (strlen (opt.sas) != 11
          || !digitp (opt.sas+0) || !digitp (opt.sas+1) || !digitp (opt.sas+2)
          || opt.sas[3] != '-'
          || !digitp (opt.sas+4) || !digitp (opt.sas+5) || !digitp (opt.sas+6)
          || opt.sas[7] != '-'
          || !digitp (opt.sas+8) || !digitp (opt.sas+9) || !digitp (opt.sas+10))
        log_error ("invalid formatted SAS\n");
    }

  /* Stop if any error, inclduing ARGPARSE_PRINT_WARNING, occurred.  */
  if (log_get_errorcount (0))
    exit (2);

  if (DBG_CRYPTO)
    gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1|2);


  /* Now run the requested command.  */
  switch (cmd)
    {
    case aInitiate:
      if (argc)
        wrong_args ("--initiate");
      err = command_initiate ();
      break;

    case aRespond:
      if (argc)
        wrong_args ("--respond");
      err = command_respond ();
      break;

    case aGet:
      if (argc > 1)
        wrong_args ("--respond [sessionid]");
      err = command_get (argc? *argv:NULL);
      break;

    case aCleanup:
      if (argc)
        wrong_args ("--cleanup");
      err = command_cleanup ();
      break;

    default:
      gpgrt_usage (1);
      err = 0;
      break;
    }

  if (err)
    write_status (STATUS_FAILURE, "- %u", err);
  else if (log_get_errorcount (0))
    write_status (STATUS_FAILURE, "- %u", GPG_ERR_GENERAL);
  else
    write_status (STATUS_SUCCESS, NULL);
  return log_get_errorcount (0)? 1:0;
}



/* Wrapper around nvc_new which terminates in the error case.  */
static nvc_t
xnvc_new (void)
{
  nvc_t c = nvc_new ();
  if (!c)
    log_fatal ("error creating NVC object: %s\n",
               gpg_strerror (gpg_error_from_syserror ()));
  return c;
}

/* Wrapper around nvc_set which terminates in the error case.  */
static void
xnvc_set (nvc_t nvc, const char *name, const char *value)
{
  gpg_error_t err = nvc_set (nvc, name, value);
  if (err)
    log_fatal ("error updating NVC object: %s\n", gpg_strerror (err));
}

/* Call vnc_set with (BUFFER, BUFLEN) converted to a hex string as
 * value.  Terminates in the error case.  */
static void
xnvc_set_hex (nvc_t nvc, const char *name, const void *buffer, size_t buflen)
{
  char *hex;

  hex = bin2hex (buffer, buflen, NULL);
  if (!hex)
    xoutofcore ();
  strlwr (hex);
  xnvc_set (nvc, name, hex);
  xfree (hex);
}

/* Call nvc_set with a value created from the string generated using
 * the printf style FORMAT.  Terminates in the error case.  */
static void
xnvc_set_printf (nvc_t nvc, const char *name, const char *format, ...)
{
  va_list arg_ptr;
  char *buffer;

  va_start (arg_ptr, format);
  if (gpgrt_vasprintf (&buffer, format, arg_ptr) < 0)
    log_fatal ("estream_asprintf failed: %s\n",
               gpg_strerror (gpg_error_from_syserror ()));
  va_end (arg_ptr);
  xnvc_set (nvc, name, buffer);
  xfree (buffer);
}


/* Return the string for the first entry in NVC with NAME.  If NAME is
 * missing, an empty string is returned.  The returned string is a
 * pointer into NVC.  */
static const char *
xnvc_get_string (nvc_t nvc, const char *name)
{
  nve_t item;

  if (!nvc)
    return "";
  item = nvc_lookup (nvc, name);
  if (!item)
    return "";
  return nve_value (item);
}



/* Return a string for MSGTYPE.  */
const char *
msgtypestr (int msgtype)
{
  switch (msgtype)
    {
    case MSG_TYPE_COMMIT:  return "Commit";
    case MSG_TYPE_DHPART1: return "DHPart1";
    case MSG_TYPE_DHPART2: return "DHPart2";
    case MSG_TYPE_CONFIRM: return "Confirm";
    }
  return "?";
}


/* Private to {get,set}_session_id().  */
static struct {
  int initialized;
  unsigned char sessid[8];
} session_id;


/* Return the 8 octet session.  */
static unsigned char *
get_session_id (void)
{
  if (!session_id.initialized)
    {
      session_id.initialized = 1;
      gcry_create_nonce (session_id.sessid, sizeof session_id.sessid);
    }

  return session_id.sessid;
}

static void
set_session_id (const void *sessid, size_t len)
{
  log_assert (!session_id.initialized);
  if (len > sizeof session_id.sessid)
    len = sizeof session_id.sessid;
  memcpy (session_id.sessid, sessid, len);
  if (len < sizeof session_id.sessid)
    memset (session_id.sessid+len, 0, sizeof session_id.sessid - len);
  session_id.initialized = 1;
}

/* Return a string with the hexified session id.  */
static const char *
get_session_id_hex (void)
{
  static char hexstr[16+1];

  bin2hex (get_session_id (), 8, hexstr);
  strlwr (hexstr);
  return hexstr;
}


/* Return a fixed string with the directory used to store the state of
 * pairings.  On error a diagnostic is printed but the file name is
 * returned anyway.  It is expected that the expected failure of the
 * following open is responsible for error handling.  */
static const char *
get_pairing_statedir (void)
{
  static char *fname;
  gpg_error_t err = 0;
  char *tmpstr;
  struct stat statbuf;

  if (fname)
    return fname;

  fname = make_filename (gnupg_homedir (), GNUPG_CACHE_DIR, NULL);
  if (gnupg_stat (fname, &statbuf) && errno == ENOENT)
    {
      if (gnupg_mkdir (fname, "-rwx"))
        {
          err = gpg_error_from_syserror ();
          log_error (_("can't create directory '%s': %s\n"),
                     fname, gpg_strerror (err) );
        }
      else if (!opt.quiet)
        log_info (_("directory '%s' created\n"), fname);
    }

  tmpstr = make_filename (fname, PAIRING_STATE_DIR, NULL);
  xfree (fname);
  fname = tmpstr;
  if (gnupg_stat (fname, &statbuf) && errno == ENOENT)
    {
      if (gnupg_mkdir (fname, "-rwx"))
        {
          if (!err)
            {
              err = gpg_error_from_syserror ();
              log_error (_("can't create directory '%s': %s\n"),
                         fname, gpg_strerror (err) );
            }
        }
      else if (!opt.quiet)
        log_info (_("directory '%s' created\n"), fname);
    }

  return fname;
}


/* Open the pairing state file.  SESSIONID is a 8 byte buffer with the
 * session-id.  If CREATE_FLAG is set the file is created and will
 * always return a valid stream.  If CREATE_FLAG is not set the file
 * is opened for reading and writing.  If the file does not exist NULL
 * is return; in all other error cases the process is terminated.  If
 * R_FNAME is not NULL the name of the file is stored there and the
 * caller needs to free it.  */
static estream_t
open_pairing_state (const unsigned char *sessionid, int create_flag,
                    char **r_fname)
{
  gpg_error_t err;
  char *fname, *tmpstr;
  estream_t fp;

  /* The filename is the session id with a "pa1" suffix.  Note that
   * the state dir may eventually be used for other purposes as well
   * and thus the suffix identifies that the file belongs to this
   * tool.  We use lowercase file names for no real reason.  */
  tmpstr = bin2hex (sessionid, 8, NULL);
  if (!tmpstr)
    xoutofcore ();
  strlwr (tmpstr);
  fname = xstrconcat (tmpstr, ".pa1", NULL);
  xfree (tmpstr);
  tmpstr = make_filename (get_pairing_statedir (), fname, NULL);
  xfree (fname);
  fname = tmpstr;

  fp = es_fopen (fname, create_flag? "wbx,mode=-rw": "rb+,mode=-rw");
  if (!fp)
    {
      err = gpg_error_from_syserror ();
      if (create_flag)
        {
          /* We should always be able to create a file.  Also we use a
           * 64 bit session id, it is theoretically possible that such
           * a session already exists.  However, that is rare enough
           * and thus the fatal error message should still be  okay.  */
          log_fatal ("can't create '%s': %s\n", fname, gpg_strerror (err));
        }
      else if (gpg_err_code (err) == GPG_ERR_ENOENT)
        {
          /* That is an expected error; return NULL.  */
        }
      else
        {
          log_fatal ("can't open '%s': %s\n", fname, gpg_strerror (err));
        }
    }

  if (r_fname)
    *r_fname = fname;
  else
    xfree (fname);

  return fp;
}


/* Write the state to a possible new state file.  */
static void
write_state (nvc_t state, int create_flag)
{
  gpg_error_t err;
  char *fname = NULL;
  estream_t fp;

  fp = open_pairing_state (get_session_id (), create_flag, &fname);
  log_assert (fp);

  err = nvc_write (state, fp);
  if (err)
    {
      es_fclose  (fp);
      gnupg_remove (fname);
      log_fatal ("error writing '%s': %s\n", fname, gpg_strerror (err));
    }

  /* If we did not create the file, we need to truncate the file.  */
  if (!create_flag && ftruncate (es_fileno (fp), es_ftello (fp)))
    {
      err = gpg_error_from_syserror ();
      log_fatal ("error truncating '%s': %s\n", fname, gpg_strerror (err));
    }
  if (es_ferror (fp) || es_fclose (fp))
    {
      err = gpg_error_from_syserror ();
      es_fclose  (fp);
      gnupg_remove (fname);
      log_fatal ("error writing '%s': %s\n", fname, gpg_strerror (err));
    }
}


/* Read the state into a newly allocated state object and store that
 * at R_STATE. If no state is available GPG_ERR_NOT_FOUND is returned
 * and as with all errors NULL is tored at R_STATE.  SESSIONID is an
 * input with the 8 session id.  */
static gpg_error_t
read_state (nvc_t *r_state)
{
  gpg_error_t err;
  char *fname = NULL;
  estream_t fp;
  nvc_t state = NULL;
  nve_t item;
  const char *value;
  unsigned long expire;

  *r_state = NULL;

  fp = open_pairing_state (get_session_id (), 0, &fname);
  if (!fp)
    return gpg_error (GPG_ERR_NOT_FOUND);

  err = nvc_parse (&state, NULL, fp);
  if (err)
    {
      log_info ("failed to parse state file '%s': %s\n",
                fname, gpg_strerror (err));
      goto leave;
    }

  /* Check whether the state already expired.  */
  item = nvc_lookup (state, "Expires:");
  if (!item)
    {
      log_info ("invalid state file '%s': %s\n",
                fname, "field 'expire' not found");
      goto leave;
    }
  value = nve_value (item);
  if (!value || !(expire = strtoul (value, NULL, 10)))
    {
      log_info ("invalid state file '%s': %s\n",
                fname, "field 'expire' has an invalid value");
      goto leave;
    }
  if (expire <= gnupg_get_time ())
    {
      es_fclose (fp);
      fp = NULL;
      if (gnupg_remove (fname))
        {
          err = gpg_error_from_syserror ();
          log_info ("failed to delete state file '%s': %s\n",
                    fname, gpg_strerror (err));
        }
      else if (opt.verbose)
        log_info ("state file '%s' deleted\n", fname);
      err = gpg_error (GPG_ERR_NOT_FOUND);
      goto leave;
    }

  *r_state = state;
  state = NULL;

 leave:
  nvc_release (state);
  es_fclose (fp);
  return err;
}


/* Send (MSG,MSGLEN) to the output device.  */
static void
send_message (const unsigned char *msg, size_t msglen)
{
  gpg_error_t err;

  if (opt.verbose)
    log_info ("session %s: sending %s message\n",
              get_session_id_hex (), msgtypestr (msg[7]));

  if (DBG_MESSAGE)
    log_printhex (msg, msglen, "send msg(%s):", msgtypestr (msg[7]));

  /* FIXME: For now only stdout.  */
  if (opt.armor)
    {
      gpgrt_b64state_t state;

      state = gpgrt_b64enc_start (es_stdout, "");
      if (!state)
        log_fatal ("error setting up base64 encoder: %s\n",
                   gpg_strerror (gpg_error_from_syserror ()));
      err = gpgrt_b64enc_write (state, msg, msglen);
      if (!err)
        err = gpgrt_b64enc_finish (state);
      if (err)
        log_fatal ("error writing base64 to stdout: %s\n", gpg_strerror (err));
    }
  else
    {
      if (es_fwrite (msg, msglen, 1, es_stdout) != 1)
        log_fatal ("error writing to stdout: %s\n",
                   gpg_strerror (gpg_error_from_syserror ()));
    }
  es_fputc ('\n', es_stdout);
}


/* Read a message from stdin and store it at the address (R_MSG,
 * R_MSGLEN).  This function detects armoring and removes it.  On
 * error NULL is stored at R_MSG, a diagnostic printed and an error
 * code returned.  The returned message has a proper message type and
 * an appropriate length.  The message type is stored at R_MSGTYPE and
 * if a state is available it is stored at R_STATE.  */
static gpg_error_t
read_message (unsigned char **r_msg, size_t *r_msglen, int *r_msgtype,
              nvc_t *r_state)
{
  gpg_error_t err;
  unsigned char msg[128];  /* max msg size is 80 but 107 with base64.  */
  size_t msglen;
  size_t reqlen;

  *r_msg = NULL;
  *r_state = NULL;

  es_setvbuf (es_stdin, NULL, _IONBF, 0);
  es_set_binary (es_stdin);

  if (es_read (es_stdin, msg, sizeof msg, &msglen))
    {
      err = gpg_error_from_syserror ();
      log_error ("error reading from message: %s\n", gpg_strerror (err));
      return err;
    }

  if (msglen > 4 && !memcmp (msg, "R1BH", 4))
    {
      /* This is base64 of the first 3 bytes.  */
      gpgrt_b64state_t state = gpgrt_b64dec_start (NULL);
      if (!state)
        log_fatal ("error setting up base64 decoder: %s\n",
                   gpg_strerror (gpg_error_from_syserror ()));
      err = gpgrt_b64dec_proc (state, msg, msglen, &msglen);
      gpgrt_b64dec_finish (state);
      if (err)
        {
          log_error ("error decoding message: %s\n", gpg_strerror (err));
          return err;
        }
    }

  if (msglen < 16 || memcmp (msg, "GPG-pa1", 7))
    {
      log_error ("error parsing message: %s\n",
                 msglen? "invalid header":"empty message");
      return gpg_error (GPG_ERR_INV_RESPONSE);
    }
  switch (msg[7])
    {
    case MSG_TYPE_COMMIT:  reqlen = 56; break;
    case MSG_TYPE_DHPART1: reqlen = 80; break;
    case MSG_TYPE_DHPART2: reqlen = 80; break;
    case MSG_TYPE_CONFIRM: reqlen = 48; break;

    default:
      log_error ("error parsing message: %s\n", "invalid message type");
      return gpg_error (GPG_ERR_INV_RESPONSE);
    }
  if (msglen < reqlen)
    {
      log_error ("error parsing message: %s\n", "message too short");
      return gpg_error (GPG_ERR_INV_RESPONSE);
    }

  if (DBG_MESSAGE)
    log_printhex (msg, msglen, "recv msg(%s):", msgtypestr (msg[7]));

  /* Note that we ignore any garbage at the end of a message.  */
  msglen = reqlen;

  set_session_id (msg+8, 8);

  if (opt.verbose)
    log_info ("session %s: received %s message\n",
              get_session_id_hex (), msgtypestr (msg[7]));

  /* Read the state.  */
  err = read_state (r_state);
  if (err && gpg_err_code (err) != GPG_ERR_NOT_FOUND)
    return err;

  *r_msg = xmalloc (msglen);
  memcpy (*r_msg, msg, msglen);
  *r_msglen = msglen;
  *r_msgtype = msg[7];
  return err;
}


/* Display the Short Authentication String (SAS). If WAIT is true the
 * function waits until the user has entered the SAS as seen at the
 * peer.
 *
 * To construct the SAS we take the 4 most significant octets of HASH,
 * interpret them as a 32 bit big endian unsigned integer, divide that
 * integer by 10^9 and take the remainder.  The remainder is displayed
 * as 3 groups of 3 decimal digits delimited by a hyphens.  This gives
 * a search space of close to 2^30 and is still easy to compare.
 */
static gpg_error_t
display_sas (const unsigned char *hash, size_t hashlen, int wait)
{
  gpg_error_t err = 0;
  unsigned long sas = 0;
  char sasbuf[12];

  log_assert (hashlen >= 4);

  sas |= (unsigned long)hash[20] << 24;
  sas |= (unsigned long)hash[21] << 16;
  sas |= (unsigned long)hash[22] <<  8;
  sas |= (unsigned long)hash[23];
  sas %= 1000000000ul;
  snprintf (sasbuf, sizeof sasbuf, "%09lu", sas);
  memmove (sasbuf+8, sasbuf+6, 3);
  memmove (sasbuf+4, sasbuf+3, 3);
  sasbuf[3] = sasbuf[7] = '-';
  sasbuf[11] = 0;

  if (wait)
    log_info ("Please check the SAS:\n");
  else
    log_info ("Please note the SAS:\n");
  log_info ("\n");
  log_info ("   %s\n", sasbuf);
  log_info ("\n");

  if (wait)
    {
      if (!opt.sas || strcmp (sasbuf, opt.sas))
        err = gpg_error (GPG_ERR_NOT_CONFIRMED);
      else
        log_info ("SAS confirmed\n");
    }

  if (err)
    log_info ("checking SAS failed: %s\n", gpg_strerror (err));
  return err;
}



static gpg_error_t
create_dh_keypair (unsigned char *dh_secret, size_t dh_secret_len,
                   unsigned char *dh_public, size_t dh_public_len)
{
  gpg_error_t err;
  unsigned char *p;

  /* We need a temporary buffer for the public key.  Check the length
   * for the later memcpy.  */
  if (dh_public_len < 32 || dh_secret_len < 32)
    return gpg_error (GPG_ERR_BUFFER_TOO_SHORT);

  if (gcry_ecc_get_algo_keylen (GCRY_ECC_CURVE25519) > dh_public_len)
    return gpg_error (GPG_ERR_BUFFER_TOO_SHORT);

  p = gcry_random_bytes (32, GCRY_VERY_STRONG_RANDOM);
  if (!p)
    return gpg_error_from_syserror ();

  memcpy (dh_secret, p, 32);
  xfree (p);

  err = gcry_ecc_mul_point (GCRY_ECC_CURVE25519, dh_public, dh_secret, NULL);
  if (err)
    return err;

  if (DBG_CRYPTO)
    {
      log_printhex (dh_secret, 32, "DH secret:");
      log_printhex (dh_public, 32, "DH public:");
    }

  return 0;
}


/* SHA256 the data given as varargs tuples of (const void*, size_t)
 * and store the result in RESULT.  The end of the list is indicated
 * by a NULL element in a tuple.  RESULTLEN gives the length of the
 * RESULT buffer which must be at least 32.  Note that the second item
 * of the tuple is the length and it is a size_t.  */
static void *
hash_data (void *result, size_t resultsize, ...)
{
  va_list arg_ptr;
  gpg_error_t err;
  gcry_md_hd_t hd;
  const void *data;
  size_t datalen;

  log_assert (resultsize >= 32);

  err = gcry_md_open (&hd, GCRY_MD_SHA256, 0);
  if (err)
    log_fatal ("error creating a Hash handle: %s\n", gpg_strerror (err));
  /* log_printhex ("", 0, "Hash-256:"); */

  va_start (arg_ptr, resultsize);
  while ((data = va_arg (arg_ptr, const void *)))
    {
      datalen = va_arg (arg_ptr, size_t);
      /* log_printhex (data, datalen, "    data:"); */
      gcry_md_write (hd, data, datalen);
    }
  va_end (arg_ptr);

  memcpy (result, gcry_md_read (hd, 0), 32);
  /* log_printhex (result, 32, "  result:"); */

  gcry_md_close (hd);

  return result;
}


/* HMAC-SHA256 the data given as varargs tuples of (const void*,
 * size_t) using (KEYLEN,KEY) and store the result in RESULT.  The end
 * of the list is indicated by a NULL element in a tuple.  RESULTLEN
 * gives the length of the RESULT buffer which must be at least 32.
 * Note that the second item of the tuple is the length and it is a
 * size_t.  */
static void *
hmac_data (void *result, size_t resultsize,
           const unsigned char *key, size_t keylen, ...)
{
  va_list arg_ptr;
  gpg_error_t err;
  gcry_mac_hd_t hd;
  const void *data;
  size_t datalen;

  log_assert (resultsize >= 32);

  err = gcry_mac_open (&hd, GCRY_MAC_HMAC_SHA256, 0, NULL);
  if (err)
    log_fatal ("error creating a MAC handle: %s\n", gpg_strerror (err));
  err = gcry_mac_setkey (hd, key, keylen);
  if (err)
    log_fatal ("error setting the MAC key: %s\n", gpg_strerror (err));
  /* log_printhex (key, keylen, "HMAC-key:"); */

  va_start (arg_ptr, keylen);
  while ((data = va_arg (arg_ptr, const void *)))
    {
      datalen = va_arg (arg_ptr, size_t);
      /* log_printhex (data, datalen, "    data:"); */
      err = gcry_mac_write (hd, data, datalen);
      if (err)
        log_fatal ("error writing to the MAC handle: %s\n", gpg_strerror (err));
    }
  va_end (arg_ptr);

  err = gcry_mac_read (hd, result, &resultsize);
  if (err || resultsize != 32)
    log_fatal ("error reading MAC value: %s\n", gpg_strerror (err));
  /* log_printhex (result, resultsize, "  result:"); */

  gcry_mac_close (hd);

  return result;
}


/* Key derivation function:
 *
 * FIXME(doc)
 */
static void
kdf (unsigned char *result, size_t resultlen,
     const unsigned char *master, size_t masterlen,
     const unsigned char *sessionid, size_t sessionidlen,
     const unsigned char *expire, size_t expirelen,
     const char *label)
{
  log_assert (masterlen == 32 && sessionidlen == 8 && expirelen == 5);
  log_assert (*label);
  log_assert (resultlen == 32);

  hmac_data (result, resultlen, master, masterlen,
             "\x00\x00\x00\x01", (size_t)4,      /* Counter=1*/
             label, strlen (label) + 1,          /* Label, 0x00 */
             sessionid, sessionidlen,            /* Context */
             expire, expirelen,                  /* Context */
             "\x00\x00\x01\x00", (size_t)4,      /* L=256 */
             NULL);
}


static gpg_error_t
compute_master_secret (unsigned char *master, size_t masterlen,
                       const unsigned char *sk_a, size_t sk_a_len,
                       const unsigned char *pk_b, size_t pk_b_len)
{
  gpg_error_t err;

  log_assert (masterlen == 32);
  log_assert (sk_a_len == 32);
  log_assert (pk_b_len == 32);

  err = gcry_ecc_mul_point (GCRY_ECC_CURVE25519, master, sk_a, pk_b);
  if (err)
    log_error ("error computing DH: %s\n", gpg_strerror (err));

  return err;
}


/* We are the Initiator: Create the commit message.  This function
 * sends the COMMIT message and writes STATE. */
static gpg_error_t
make_msg_commit (nvc_t state)
{
  gpg_error_t err;
  uint64_t now, expire;
  unsigned char secret[32];
  unsigned char public[32];
  unsigned char *newmsg;
  size_t newmsglen;
  unsigned char tmphash[32];

  err = create_dh_keypair (secret, sizeof secret, public, sizeof public );
  if (err)
    log_error ("creating DH keypair failed: %s\n", gpg_strerror (err));

  now = gnupg_get_time ();
  expire = now + opt.ttl;

  newmsglen = 7+1+8+1+2+5+32;
  newmsg = xmalloc (newmsglen);
  memcpy (newmsg+0, "GPG-pa1", 7);
  newmsg[7] = MSG_TYPE_COMMIT;
  memcpy (newmsg+8, get_session_id (), 8);
  newmsg[16] = REALM_STANDARD;
  newmsg[17] = 0;
  newmsg[18] = 0;
  newmsg[19] = expire >> 32;
  newmsg[20] = expire >> 24;
  newmsg[21] = expire >> 16;
  newmsg[22] = expire >> 8;
  newmsg[23] = expire;
  gcry_md_hash_buffer (GCRY_MD_SHA256, newmsg+24, public, 32);

  /* Create the state file.  */
  xnvc_set (state, "State:", "Commit-sent");
  xnvc_set_printf (state, "Created:", "%llu", (unsigned long long)now);
  xnvc_set_printf (state, "Expires:", "%llu", (unsigned long long)expire);
  xnvc_set_hex (state, "DH-PKi:", public, 32);
  xnvc_set_hex (state, "DH-SKi:", secret, 32);
  gcry_md_hash_buffer (GCRY_MD_SHA256, tmphash, newmsg, newmsglen);
  xnvc_set_hex (state, "Hash-Commit:", tmphash, 32);

  /* Write the state.  Note that we need to create it.  The state
   * updating should in theory be done atomically with send_message.
   * However, we can't assure that the message will actually be
   * delivered and thus it doesn't matter whether we have an already
   * update state when we later fail in send_message.  */
  write_state (state, 1);

  /* Write the message.  */
  send_message (newmsg, newmsglen);

   xfree (newmsg);
  return err;
}


/* We are the Responder: Process a commit message in (MSG,MSGLEN)
 * which has already been validated to have a correct header and
 * message type.  Sends the DHPart1 message and writes STATE.  */
static gpg_error_t
proc_msg_commit (nvc_t state, const unsigned char *msg, size_t msglen)
{
  gpg_error_t err;
  uint64_t now, expire;
  unsigned char tmphash[32];
  unsigned char secret[32];
  unsigned char public[32];
  unsigned char *newmsg = NULL;
  size_t newmsglen;

  log_assert (msglen >= 56);
  now = gnupg_get_time ();

  /* Check that the message has not expired.  */
  expire  = (uint64_t)msg[19] << 32;
  expire |= (uint64_t)msg[20] << 24;
  expire |= (uint64_t)msg[21] << 16;
  expire |= (uint64_t)msg[22] <<  8;
  expire |= (uint64_t)msg[23];
  if (expire < now)
    {
      log_error ("received %s message is too old\n",
                 msgtypestr (MSG_TYPE_COMMIT));
      err = gpg_error (GPG_ERR_TOO_OLD);
      goto leave;
    }

  /* Create the response.  */
  err = create_dh_keypair (secret, sizeof secret, public, sizeof public );
  if (err)
    {
      log_error ("creating DH keypair failed: %s\n", gpg_strerror (err));
      goto leave;
    }

  newmsglen = 7+1+8+32+32;
  newmsg = xmalloc (newmsglen);
  memcpy (newmsg+0, "GPG-pa1", 7);
  newmsg[7] = MSG_TYPE_DHPART1;
  memcpy (newmsg+8, msg + 8, 8);   /* SessionID.  */
  memcpy (newmsg+16, public, 32);  /* PKr */
  /* Hash(Hash(Commit) || DHPart1[0..47]) */
  gcry_md_hash_buffer (GCRY_MD_SHA256, tmphash, msg, msglen);
  hash_data (newmsg+48, 32,
             tmphash, sizeof tmphash,
             newmsg, (size_t)48,
             NULL);

  /* Update the state.  */
  xnvc_set (state, "State:", "DHPart1-sent");
  xnvc_set_printf (state, "Created:", "%llu", (unsigned long long)now);
  xnvc_set_printf (state, "Expires:", "%llu", (unsigned long long)expire);
  xnvc_set_hex (state, "Hash-PKi:", msg+24, 32);
  xnvc_set_hex (state, "DH-PKr:", public, 32);
  xnvc_set_hex (state, "DH-SKr:", secret, 32);
  gcry_md_hash_buffer (GCRY_MD_SHA256, tmphash, newmsg, newmsglen);
  xnvc_set_hex (state, "Hash-DHPart1:", tmphash, 32);

  /* Write the state.  Note that we need to create it. */
  write_state (state, 1);

  /* Write the message.  */
  send_message (newmsg, newmsglen);

 leave:
  xfree (newmsg);
  return err;
}


/* We are the Initiator: Process a DHPART1 message in (MSG,MSGLEN)
 * which has already been validated to have a correct header and
 * message type.  Sends the DHPart2 message and writes STATE.  */
static gpg_error_t
proc_msg_dhpart1 (nvc_t state, const unsigned char *msg, size_t msglen)
{
  gpg_error_t err;
  unsigned char hash[32];
  unsigned char tmphash[32];
  unsigned char pki[32];
  unsigned char pkr[32];
  unsigned char ski[32];
  unsigned char master[32];
  uint64_t expire;
  unsigned char expirebuf[5];
  unsigned char hmacikey[32];
  unsigned char symxkey[32];
  unsigned char *newmsg = NULL;
  size_t newmsglen;

  log_assert (msglen >= 80);

  /* Check that the message includes the Hash(Commit). */
  if (hex2bin (xnvc_get_string (state, "Hash-Commit:"), hash, sizeof hash) < 0)
    {
      err = gpg_error (GPG_ERR_INV_VALUE);
      log_error ("no or garbled 'Hash-Commit' in our state file\n");
      goto leave;
    }
  hash_data (tmphash, 32,
             hash, sizeof hash,
             msg, (size_t)48,
             NULL);
  if (memcmp (msg+48, tmphash, 32))
    {
      err = gpg_error (GPG_ERR_BAD_DATA);
      log_error ("manipulation of received %s message detected: %s\n",
                 msgtypestr (MSG_TYPE_DHPART1), "Bad Hash");
      goto leave;
    }
  /* Check that the received PKr is different from our PKi and copy
   * PKr into PKR.  */
  if (hex2bin (xnvc_get_string (state, "DH-PKi:"), pki, sizeof pki) < 0)
    {
      err = gpg_error (GPG_ERR_INV_VALUE);
      log_error ("no or garbled 'DH-PKi' in our state file\n");
      goto leave;
    }
  if (!memcmp (msg+16, pki, 32))
    {
      /* This can only happen if the state file leaked to the
       * responder.  */
      err = gpg_error (GPG_ERR_BAD_DATA);
      log_error ("received our own public key PKi instead of PKr\n");
      goto leave;
    }
  memcpy (pkr, msg+16, 32);

  /* Put the expire value into a buffer.  */
  expire = string_to_u64 (xnvc_get_string (state, "Expires:"));
  if (!expire)
    {
      err = gpg_error (GPG_ERR_INV_VALUE);
      log_error ("no 'Expire' in our state file\n");
      goto leave;
    }
  expirebuf[0] = expire >> 32;
  expirebuf[1] = expire >> 24;
  expirebuf[2] = expire >> 16;
  expirebuf[3] = expire >> 8;
  expirebuf[4] = expire;

  /* Get our secret from the state.  */
  if (hex2bin (xnvc_get_string (state, "DH-SKi:"), ski, sizeof ski) < 0)
    {
      err = gpg_error (GPG_ERR_INV_VALUE);
      log_error ("no or garbled 'DH-SKi' in our state file\n");
      goto leave;
    }

  /* Compute the shared secrets.  */
  err = compute_master_secret (master, sizeof master,
                               ski, sizeof ski, pkr, sizeof pkr);
  if (err)
    {
      log_error ("creating DH keypair failed: %s\n", gpg_strerror (err));
      goto leave;
    }

  kdf (hmacikey, sizeof hmacikey,
       master, sizeof master, msg+8, 8, expirebuf, sizeof expirebuf,
       "GPG-pa1-HMACi-key");
  kdf (symxkey, sizeof symxkey,
       master, sizeof master, msg+8, 8, expirebuf, sizeof expirebuf,
       "GPG-pa1-SYMx-key");


  /* Create the response.  */
  newmsglen = 7+1+8+32+32;
  newmsg = xmalloc (newmsglen);
  memcpy (newmsg+0, "GPG-pa1", 7);
  newmsg[7] = MSG_TYPE_DHPART2;
  memcpy (newmsg+8, msg + 8, 8); /* SessionID.  */
  memcpy (newmsg+16, pki, 32);   /* PKi */
  /* MAC(HMACi-key, Hash(DHPART1) || DHPART2[0..47] || SYMx-key) */
  gcry_md_hash_buffer (GCRY_MD_SHA256, tmphash, msg, msglen);
  hmac_data (newmsg+48, 32, hmacikey, sizeof hmacikey,
             tmphash, sizeof tmphash,
             newmsg, (size_t)48,
             symxkey, sizeof symxkey,
             NULL);

  /* Update the state.  */
  xnvc_set (state, "State:", "DHPart2-sent");
  xnvc_set_hex (state, "DH-Master:", master, sizeof master);
  gcry_md_hash_buffer (GCRY_MD_SHA256, tmphash, newmsg, newmsglen);
  xnvc_set_hex (state, "Hash-DHPart2:", tmphash, 32);

  /* Write the state.  */
  write_state (state, 0);

  /* Write the message.  */
  send_message (newmsg, newmsglen);

 leave:
  xfree (newmsg);
  return err;
}


/* We are the Responder: Process a DHPART2 message in (MSG,MSGLEN)
 * which has already been validated to have a correct header and
 * message type.  Sends the CONFIRM message and writes STATE.  */
static gpg_error_t
proc_msg_dhpart2 (nvc_t state, const unsigned char *msg, size_t msglen)
{
  gpg_error_t err;
  unsigned char hash[32];
  unsigned char tmphash[32];
  uint64_t expire;
  unsigned char expirebuf[5];
  unsigned char pki[32];
  unsigned char pkr[32];
  unsigned char skr[32];
  unsigned char master[32];
  unsigned char hmacikey[32];
  unsigned char hmacrkey[32];
  unsigned char symxkey[32];
  unsigned char sas[32];
  unsigned char *newmsg = NULL;
  size_t newmsglen;

  log_assert (msglen >= 80);

  /* Check that the PKi in the message matches the Hash(Pki) received
   * with the Commit message. */
  memcpy (pki, msg + 16, 32);
  gcry_md_hash_buffer (GCRY_MD_SHA256, hash, pki, 32);
  if (hex2bin (xnvc_get_string (state, "Hash-PKi:"),
               tmphash, sizeof tmphash) < 0)
    {
      err = gpg_error (GPG_ERR_INV_VALUE);
      log_error ("no or garbled 'Hash-PKi' in our state file\n");
      goto leave;
    }
  if (memcmp (hash, tmphash, 32))
    {
      err = gpg_error (GPG_ERR_BAD_DATA);
      log_error ("Initiator sent a different key in %s than announced in %s\n",
                 msgtypestr (MSG_TYPE_DHPART2),
                 msgtypestr (MSG_TYPE_COMMIT));
      goto leave;
    }
  /* Check that the received PKi is different from our PKr.  */
  if (hex2bin (xnvc_get_string (state, "DH-PKr:"), pkr, sizeof pkr) < 0)
    {
      err = gpg_error (GPG_ERR_INV_VALUE);
      log_error ("no or garbled 'DH-PKr' in our state file\n");
      goto leave;
    }
  if (!memcmp (pkr, pki, 32))
    {
      err = gpg_error (GPG_ERR_BAD_DATA);
      log_error ("Initiator sent our own PKr back\n");
      goto leave;
    }

  /* Put the expire value into a buffer.  */
  expire = string_to_u64 (xnvc_get_string (state, "Expires:"));
  if (!expire)
    {
      err = gpg_error (GPG_ERR_INV_VALUE);
      log_error ("no 'Expire' in our state file\n");
      goto leave;
    }
  expirebuf[0] = expire >> 32;
  expirebuf[1] = expire >> 24;
  expirebuf[2] = expire >> 16;
  expirebuf[3] = expire >> 8;
  expirebuf[4] = expire;

  /* Get our secret from the state.  */
  if (hex2bin (xnvc_get_string (state, "DH-SKr:"), skr, sizeof skr) < 0)
    {
      err = gpg_error (GPG_ERR_INV_VALUE);
      log_error ("no or garbled 'DH-SKr' in our state file\n");
      goto leave;
    }

  /* Compute the shared secrets.  */
  err = compute_master_secret (master, sizeof master,
                               skr, sizeof skr, pki, sizeof pki);
  if (err)
    {
      log_error ("creating DH keypair failed: %s\n", gpg_strerror (err));
      goto leave;
    }

  kdf (hmacikey, sizeof hmacikey,
       master, sizeof master, msg+8, 8, expirebuf, sizeof expirebuf,
       "GPG-pa1-HMACi-key");
  kdf (hmacrkey, sizeof hmacrkey,
       master, sizeof master, msg+8, 8, expirebuf, sizeof expirebuf,
       "GPG-pa1-HMACr-key");
  kdf (symxkey, sizeof symxkey,
       master, sizeof master, msg+8, 8, expirebuf, sizeof expirebuf,
       "GPG-pa1-SYMx-key");
  kdf (sas, sizeof sas,
       master, sizeof master, msg+8, 8, expirebuf, sizeof expirebuf,
       "GPG-pa1-SAS");

  /* Check the MAC from the message which is
   *   MAC(HMACi-key, Hash(DHPART1) || DHPART2[0..47] || SYMx-key).
   * For that we need to fetch the stored hash from the state.  */
  if (hex2bin (xnvc_get_string (state, "Hash-DHPart1:"),
               tmphash, sizeof tmphash) < 0)
    {
      err = gpg_error (GPG_ERR_INV_VALUE);
      log_error ("no or garbled 'Hash-DHPart1' in our state file\n");
      goto leave;
    }
  hmac_data (hash, 32, hmacikey, sizeof hmacikey,
             tmphash, sizeof tmphash,
             msg, 48,
             symxkey, sizeof symxkey,
             NULL);
  if (memcmp (msg+48, hash, 32))
    {
      err = gpg_error (GPG_ERR_BAD_DATA);
      log_error ("manipulation of received %s message detected: %s\n",
                 msgtypestr (MSG_TYPE_DHPART2), "Bad MAC");
      goto leave;
    }

  /* Create the response.  */
  newmsglen = 7+1+8+32;
  newmsg = xmalloc (newmsglen);
  memcpy (newmsg+0, "GPG-pa1", 7);
  newmsg[7] = MSG_TYPE_CONFIRM;
  memcpy (newmsg+8, msg + 8, 8); /* SessionID.  */
  /* MAC(HMACr-key, Hash(DHPART2) || CONFIRM[0..15] || SYMx-key) */
  gcry_md_hash_buffer (GCRY_MD_SHA256, tmphash, msg, msglen);
  hmac_data (newmsg+16, 32, hmacrkey, sizeof hmacrkey,
             tmphash, sizeof tmphash,
             newmsg, (size_t)16,
             symxkey, sizeof symxkey,
             NULL);

  /* Update the state.  */
  xnvc_set (state, "State:", "Confirm-sent");
  xnvc_set_hex (state, "DH-Master:", master, sizeof master);

  /* Write the state.  */
  write_state (state, 0);

  /* Write the message.  */
  send_message (newmsg, newmsglen);

  display_sas (sas, sizeof sas, 0);


 leave:
  xfree (newmsg);
  return err;
}


/* We are the Initiator: Process a CONFIRM message in (MSG,MSGLEN)
 * which has already been validated to have a correct header and
 * message type.  Does not send anything back.  */
static gpg_error_t
proc_msg_confirm (nvc_t state, const unsigned char *msg, size_t msglen)
{
  gpg_error_t err;
  unsigned char hash[32];
  unsigned char tmphash[32];
  unsigned char master[32];
  uint64_t expire;
  unsigned char expirebuf[5];
  unsigned char hmacrkey[32];
  unsigned char symxkey[32];
  unsigned char sas[32];

  log_assert (msglen >= 48);

  /* Put the expire value into a buffer.  */
  expire = string_to_u64 (xnvc_get_string (state, "Expires:"));
  if (!expire)
    {
      err = gpg_error (GPG_ERR_INV_VALUE);
      log_error ("no 'Expire' in our state file\n");
      goto leave;
    }
  expirebuf[0] = expire >> 32;
  expirebuf[1] = expire >> 24;
  expirebuf[2] = expire >> 16;
  expirebuf[3] = expire >> 8;
  expirebuf[4] = expire;

  /* Get the master secret.  */
  if (hex2bin (xnvc_get_string (state, "DH-Master:"),master,sizeof master) < 0)
    {
      err = gpg_error (GPG_ERR_INV_VALUE);
      log_error ("no or garbled 'DH-Master' in our state file\n");
      goto leave;
    }

  kdf (hmacrkey, sizeof hmacrkey,
       master, sizeof master, msg+8, 8, expirebuf, sizeof expirebuf,
       "GPG-pa1-HMACr-key");
  kdf (symxkey, sizeof symxkey,
       master, sizeof master, msg+8, 8, expirebuf, sizeof expirebuf,
       "GPG-pa1-SYMx-key");
  kdf (sas, sizeof sas,
       master, sizeof master, msg+8, 8, expirebuf, sizeof expirebuf,
       "GPG-pa1-SAS");

  /* Check the MAC from the message which is */
  /*   MAC(HMACr-key, Hash(DHPART2) || CONFIRM[0..15] || SYMx-key). */
  if (hex2bin (xnvc_get_string (state, "Hash-DHPart2:"),
               tmphash, sizeof tmphash) < 0)
    {
      err = gpg_error (GPG_ERR_INV_VALUE);
      log_error ("no or garbled 'Hash-DHPart2' in our state file\n");
      goto leave;
    }
  hmac_data (hash, 32, hmacrkey, sizeof hmacrkey,
             tmphash, sizeof tmphash,
             msg, (size_t)16,
             symxkey, sizeof symxkey,
             NULL);
  if (!memcmp (msg+48, hash, 32))
    {
      err = gpg_error (GPG_ERR_BAD_DATA);
      log_error ("manipulation of received %s message detected: %s\n",
                 msgtypestr (MSG_TYPE_CONFIRM), "Bad MAC");
      goto leave;
    }


  err = display_sas (sas, sizeof sas, 1);
  if (err)
    goto leave;

  /* Update the state.  */
  xnvc_set (state, "State:", "Confirmed");

  /* Write the state.  */
  write_state (state, 0);

 leave:
  return err;
}



/* Expire old state files.  This loops over all state files and remove
 * those which are expired.  */
static void
expire_old_states (void)
{
  gpg_error_t err = 0;
  const char *dirname;
  gnupg_dir_t dir = NULL;
  gnupg_dirent_t dir_entry;
  char *fname = NULL;
  estream_t fp = NULL;
  nvc_t nvc = NULL;
  nve_t item;
  const char *value;
  unsigned long expire;
  unsigned long now = gnupg_get_time ();

  dirname = get_pairing_statedir ();
  dir = gnupg_opendir (dirname);
  if (!dir)
    {
      err = gpg_error_from_syserror ();
      goto leave;
    }

  while ((dir_entry = gnupg_readdir (dir)))
    {
      if (strlen (dir_entry->d_name) != 16+4
          || strcmp (dir_entry->d_name + 16, ".pa1"))
        continue;

      xfree (fname);
      fname = make_filename (dirname, dir_entry->d_name, NULL);
      es_fclose (fp);
      fp = es_fopen (fname, "rb");
      if (!fp)
        {
          err = gpg_error_from_syserror ();
          if (gpg_err_code (err) != GPG_ERR_ENOENT)
            log_info ("failed to open state file '%s': %s\n",
                      fname, gpg_strerror (err));
          continue;
        }
      nvc_release (nvc);

      /* NB.: The following is similar to code in read_state.  */
      err = nvc_parse (&nvc, NULL, fp);
      if (err)
        {
          log_info ("failed to parse state file '%s': %s\n",
                    fname, gpg_strerror (err));
          continue; /* Skip */
        }
      item = nvc_lookup (nvc, "Expires:");
      if (!item)
        {
          log_info ("invalid state file '%s': %s\n",
                    fname, "field 'expire' not found");
          continue; /* Skip */
        }
      value = nve_value (item);
      if (!value || !(expire = strtoul (value, NULL, 10)))
        {
          log_info ("invalid state file '%s': %s\n",
                    fname, "field 'expire' has an invalid value");
          continue; /* Skip */
        }

      if (expire <= now)
        {
          es_fclose (fp);
          fp = NULL;
          if (gnupg_remove (fname))
            {
              err = gpg_error_from_syserror ();
              log_info ("failed to delete state file '%s': %s\n",
                        fname, gpg_strerror (err));
            }
          else if (opt.verbose)
            log_info ("state file '%s' deleted\n", fname);
        }
    }

 leave:
  if (err)
    log_error ("expiring old states in '%s' failed: %s\n",
               dirname, gpg_strerror (err));
  gnupg_closedir (dir);
  es_fclose (fp);
  xfree (fname);
}



/* Initiate a pairing.  The output needs to be conveyed to the
 * peer  */
static gpg_error_t
command_initiate (void)
{
  gpg_error_t err;
  nvc_t state;

  state = xnvc_new ();
  xnvc_set (state, "Version:", "GPG-pa1");
  xnvc_set_hex (state, "Session:", get_session_id (), 8);
  xnvc_set (state, "Role:", "Initiator");

  err = make_msg_commit (state);

  nvc_release (state);
  return err;
}



/* Helper for command_respond().  */
static gpg_error_t
expect_state (int msgtype, const char *statestr, const char *expected)
{
  if (strcmp (statestr, expected))
    {
      log_error ("received %s message in %s state (should be %s)\n",
                 msgtypestr (msgtype), statestr, expected);
      return gpg_error (GPG_ERR_INV_RESPONSE);
    }
  return 0;
}

/* Respond to a pairing intiation.  This is used by the peer and later
 * by the original responder.  Depending on the state the output needs
 * to be conveyed to the peer.  */
static gpg_error_t
command_respond (void)
{
  gpg_error_t err;
  unsigned char *msg;
  size_t msglen = 0; /* In case that read_message returns an error.  */
  int msgtype = 0;   /* ditto.  */
  nvc_t state;
  const char *rolestr;
  const char *statestr;

  err = read_message (&msg, &msglen, &msgtype, &state);
  if (err && gpg_err_code (err) != GPG_ERR_NOT_FOUND)
    goto leave;
  rolestr = xnvc_get_string (state, "Role:");
  statestr = xnvc_get_string (state, "State:");
  if (DBG_MESSAGE)
    {
      if (!state)
        log_debug ("no state available\n");
      else
        log_debug ("we are %s, our current state is %s\n", rolestr, statestr);
      log_debug ("got message of type %s (%d)\n",
                 msgtypestr (msgtype), msgtype);
    }

  if (!state)
    {
      if (msgtype == MSG_TYPE_COMMIT)
        {
          state = xnvc_new ();
          xnvc_set (state, "Version:", "GPG-pa1");
          xnvc_set_hex (state, "Session:", get_session_id (), 8);
          xnvc_set (state, "Role:", "Responder");
          err = proc_msg_commit (state, msg, msglen);
        }
      else
        {
          log_error ("%s message expected but got %s\n",
                     msgtypestr (MSG_TYPE_COMMIT), msgtypestr (msgtype));
          if (msgtype == MSG_TYPE_DHPART1)
            log_info ("the pairing probably took too long and timed out\n");
          err = gpg_error (GPG_ERR_INV_RESPONSE);
          goto leave;
        }
    }
  else if (!strcmp (rolestr, "Initiator"))
    {
      if (msgtype == MSG_TYPE_DHPART1)
        {
          if (!(err = expect_state (msgtype, statestr, "Commit-sent")))
            err = proc_msg_dhpart1 (state, msg, msglen);
        }
      else if (msgtype == MSG_TYPE_CONFIRM)
        {
          if (!(err = expect_state (msgtype, statestr, "DHPart2-sent")))
            err = proc_msg_confirm (state, msg, msglen);
        }
      else
        {
          log_error ("%s message not expected by Initiator\n",
                     msgtypestr (msgtype));
          err = gpg_error (GPG_ERR_INV_RESPONSE);
          goto leave;
        }
    }
  else if (!strcmp (rolestr, "Responder"))
    {
      if (msgtype == MSG_TYPE_DHPART2)
        {
          if (!(err = expect_state (msgtype, statestr, "DHPart1-sent")))
            err = proc_msg_dhpart2 (state, msg, msglen);
        }
      else
        {
          log_error ("%s message not expected by Responder\n",
                     msgtypestr (msgtype));
          err = gpg_error (GPG_ERR_INV_RESPONSE);
          goto leave;
        }
    }
  else
    log_fatal ("invalid role '%s' in state file\n", rolestr);


 leave:
  xfree (msg);
  nvc_release (state);
  return err;
}



/* Return the keys for SESSIONIDSTR or the last one if it is NULL.
 * Two keys are returned: The first is the one for sending encrypted
 * data and the second one for decrypting received data.  The keys are
 * always returned hex encoded and both are terminated by a LF. */
static gpg_error_t
command_get (const char *sessionidstr)
{
  gpg_error_t err;
  unsigned char sessid[8];
  nvc_t state;

  if (!sessionidstr)
    {
      log_error ("calling without session-id is not yet implemented\n");
      err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
      goto leave;
    }
  if (hex2bin (sessionidstr, sessid, sizeof sessid) < 0)
    {
      err = gpg_error (GPG_ERR_INV_VALUE);
      log_error ("invalid session id given\n");
      goto leave;
    }
  set_session_id (sessid, sizeof sessid);
  err = read_state (&state);
  if (err)
    {
      log_error ("reading state of session %s failed: %s\n",
                 sessionidstr, gpg_strerror (err));
      goto leave;
    }

 leave:
  return err;
}



/* Cleanup command.  */
static gpg_error_t
command_cleanup (void)
{
  expire_old_states ();
  return 0;
}