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
//! For storing OpenPGP Certificates.
//!
//! The key store stores OpenPGP Certificates ("Certs") using an
//! arbitrary label.  Stored keys are automatically updated from
//! remote sources.  This ensures that updates like new subkeys and
//! revocations are discovered in a timely manner.
//!
//! # Security considerations
//!
//! Storing public keys potentially leaks communication partners.
//! Protecting against adversaries inspecting the local storage is out
//! of scope for Sequoia.  Please take the necessary precautions.
//!
//! Sequoia updates keys in compliance with the [network policy] used
//! to create the mapping.
//!
//! [network policy]: sequoia_net::Policy
//!
//! # Examples
//!
//! ```
//! # use sequoia_openpgp as openpgp;
//! # use openpgp::Fingerprint;
//! # use sequoia_ipc::{Context, IPCPolicy};
//! # use sequoia_net as net;
//! # use sequoia_store::*;
//! # fn main() { f().unwrap(); }
//! # fn f() -> Result<()> {
//! # let ctx = Context::configure()
//! #     .ipc_policy(IPCPolicy::Internal)
//! #     .ephemeral().build()?;
//! let mapping = Mapping::open(&ctx, net::Policy::Offline,
//!                             REALM_CONTACTS, "default")?;
//!
//! let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb");
//! let binding = mapping.add("Mister B.", &fp)?;
//!
//! println!("Binding {:?}", binding.stats()?);
//! // prints:
//! // Binding Stats {
//! //     created: Some(Timespec { tv_sec: 1513704042, tv_nsec: 0 }),
//! //     updated: None,
//! //     encryption: Stamps { count: 0, first: None, last: None },
//! //     verification: Stamps { count: 0, first: None, last: None }
//! // }
//! # Ok(())
//! # }
//! ```

#![doc(html_favicon_url = "https://docs.sequoia-pgp.org/favicon.png")]
#![doc(html_logo_url = "https://docs.sequoia-pgp.org/logo.svg")]
#![warn(missing_docs)]

#[macro_use]
extern crate capnp_rpc;

use std::cell::RefCell;
use std::fmt;
use std::future::Future;
use std::rc::Rc;
use std::time;

use capnp::capability::Promise;
use capnp_rpc::rpc_twoparty_capnp::Side;

use sequoia_openpgp as openpgp;

use crate::openpgp::Fingerprint;
use crate::openpgp::KeyID;
use crate::openpgp::Cert;
use crate::openpgp::parse::Parse;
use crate::openpgp::serialize::Serialize;
use sequoia_ipc::Context;
use sequoia_ipc as ipc;
use sequoia_net as net;

#[allow(dead_code)] mod store_protocol_capnp;
use crate::store_protocol_capnp::node;

/// Macros managing requests and responses.
#[macro_use] mod macros;

pub(crate) mod backend;

/// Single-threaded Tokio runtime that drives the Cap'n Proto `RpcSystem`.
struct RpcRuntime {
    runtime: tokio::runtime::Runtime,
    rpc_task: tokio::task::LocalSet,
}

impl RpcRuntime {
    /// Blocks until a `fut` is finished, concurrently driving the RPC system.
    fn block_on<F: Future>(&mut self, fut: F) -> F::Output {
        self.rpc_task.block_on(&mut self.runtime, fut)
    }
}

/// Returns the service descriptor.
#[doc(hidden)]
pub fn descriptor(c: &Context) -> ipc::Descriptor {
    ipc::Descriptor::new(
        c,
        c.home().join("public-key-store.cookie"),
        c.lib().join("sequoia-public-key-store"),
        backend::factory,
    )
}

/// Keys used for communications.
pub const REALM_CONTACTS: &str =
    "org.sequoia-pgp.contacts";

/// Keys used for signing software updates.
pub const REALM_SOFTWARE_UPDATES: &str =
    "org.sequoia-pgp.software-updates";

/// The common key pool.
pub struct Store {
}

impl Store {
    /// Establishes a connection to the backend.
    fn connect(c: &Context) -> Result<(RpcRuntime, node::Client)> {
        let descriptor = descriptor(c);

        let rt = tokio::runtime::Builder::new()
            .basic_scheduler()
            .enable_io()
            .enable_time()
            .build()?;
        // Need to enter Tokio context due to Tokio TcpStream creation binding
        // eagerly to an I/O reactor
        let mut rpc_system = rt.enter(|| descriptor.connect())?;

        let client: node::Client = rpc_system.bootstrap(Side::Server);

        // Since RpcSystem is explicitly `!Send`, we need to spawn it on
        // a `LocalSet` that needs to be exclusively driven by `RpcRuntime`
        let rpc_task = tokio::task::LocalSet::new();
        rpc_task.spawn_local(rpc_system);

        Ok((RpcRuntime { runtime: rt, rpc_task }, client))
    }

    /// Imports a key into the common key pool.
    ///
    /// # Examples
    ///
    /// ```
    /// # use sequoia_openpgp as openpgp;
    /// # use openpgp::Cert;
    /// # use openpgp::parse::Parse;
    /// # use sequoia_ipc::{Context, IPCPolicy};
    /// # use sequoia_store::{Store, Result};
    /// # fn main() { f().unwrap(); }
    /// # fn f() -> Result<()> {
    /// # let ctx = Context::configure()
    /// #     .ipc_policy(IPCPolicy::Internal)
    /// #     .ephemeral().build()?;
    /// # let cert = Cert::from_bytes(
    /// #     &include_bytes!("../../openpgp/tests/data/keys/testy.pgp")[..]).unwrap();
    /// let key = Store::import(&ctx, &cert)?;
    /// assert_eq!(key.cert()?.fingerprint(), cert.fingerprint());
    /// # Ok(())
    /// # }
    /// ```
    pub fn import(c: &Context, cert: &Cert) -> Result<Key> {
        let mut blob = vec![];
        cert.serialize(&mut blob)?;

        let (mut core, client) = Store::connect(c)?;
        let mut request = client.import_request();
        request.get().set_key(&blob);
        let key = make_request!(&mut core, request)?;
        Ok(Key::new(Rc::new(RefCell::new(core)), key))
    }

    /// Looks up a key in the common key pool.
    ///
    /// # Examples
    ///
    /// ```
    /// # use sequoia_openpgp as openpgp;
    /// # use openpgp::Cert;
    /// # use openpgp::parse::Parse;
    /// # use sequoia_ipc::{Context, IPCPolicy};
    /// # use sequoia_store::{Store, Result};
    /// # fn main() { f().unwrap(); }
    /// # fn f() -> Result<()> {
    /// # let ctx = Context::configure()
    /// #     .ipc_policy(IPCPolicy::Internal)
    /// #     .ephemeral().build()?;
    /// # let cert = Cert::from_bytes(
    /// #     &include_bytes!("../../openpgp/tests/data/keys/testy.pgp")[..]).unwrap();
    /// Store::import(&ctx, &cert)?;
    /// let key = Store::lookup(&ctx, &cert.fingerprint())?;
    /// assert_eq!(key.cert()?.fingerprint(), cert.fingerprint());
    /// # Ok(())
    /// # }
    /// ```
    pub fn lookup(c: &Context, fp: &Fingerprint) -> Result<Key> {
        let (mut core, client) = Store::connect(c)?;
        let mut request = client.lookup_by_fingerprint_request();
        let fp = format!("{:X}", fp);
        request.get().set_fingerprint(&fp);
        let key = make_request!(&mut core, request)?;
        Ok(Key::new(Rc::new(RefCell::new(core)), key))
    }

    /// Looks up a key in the common key pool by KeyID.
    ///
    /// # Examples
    ///
    /// ```
    /// # use sequoia_openpgp as openpgp;
    /// # use openpgp::Cert;
    /// # use openpgp::parse::Parse;
    /// # use sequoia_ipc::{Context, IPCPolicy};
    /// # use sequoia_store::{Store, Result};
    /// # fn main() { f().unwrap(); }
    /// # fn f() -> Result<()> {
    /// # let ctx = Context::configure()
    /// #     .ipc_policy(IPCPolicy::Internal)
    /// #     .ephemeral().build()?;
    /// # let cert = Cert::from_bytes(
    /// #     &include_bytes!("../../openpgp/tests/data/keys/testy.pgp")[..]).unwrap();
    /// Store::import(&ctx, &cert)?;
    /// let key = Store::lookup_by_keyid(&ctx, &cert.fingerprint().into())?;
    /// assert_eq!(key.cert()?.fingerprint(), cert.fingerprint());
    /// # Ok(())
    /// # }
    /// ```
    pub fn lookup_by_keyid(c: &Context, keyid: &KeyID) -> Result<Key> {
        let (mut core, client) = Store::connect(c)?;
        let mut request = client.lookup_by_keyid_request();
        request.get().set_keyid(keyid.as_u64()?);
        let key = make_request!(&mut core, request)?;
        Ok(Key::new(Rc::new(RefCell::new(core)), key))
    }

    /// Looks up a key in the common key pool by (Sub)KeyID.
    ///
    /// The KeyID may also reference a subkey.
    ///
    /// # Examples
    ///
    /// ```
    /// # use sequoia_openpgp as openpgp;
    /// # use openpgp::{Cert, KeyID};
    /// # use openpgp::parse::Parse;
    /// # use sequoia_ipc::{Context, IPCPolicy};
    /// # use sequoia_store::{Store, Result};
    /// # fn main() { f().unwrap(); }
    /// # fn f() -> Result<()> {
    /// # let ctx = Context::configure()
    /// #     .ipc_policy(IPCPolicy::Internal)
    /// #     .ephemeral().build()?;
    /// # let cert = Cert::from_bytes(
    /// #     &include_bytes!("../../openpgp/tests/data/keys/neal.pgp")[..])
    /// #     .unwrap();
    /// Store::import(&ctx, &cert)?;
    ///
    /// // Lookup by the primary key's KeyID.
    /// let key = Store::lookup_by_subkeyid(&ctx, &"AACB3243630052D9".parse()?)?;
    /// assert_eq!(key.cert()?.fingerprint(), cert.fingerprint());
    ///
    /// // Lookup by the signing subkey's KeyID.
    /// let key = Store::lookup_by_subkeyid(&ctx, &"7223B56678E02528".parse()?)?;
    /// assert_eq!(key.cert()?.fingerprint(), cert.fingerprint());
    ///
    /// // Lookup by the encryption subkey's KeyID.
    /// let key = Store::lookup_by_subkeyid(&ctx, &"C2B819056C652598".parse()?)?;
    /// assert_eq!(key.cert()?.fingerprint(), cert.fingerprint());
    ///
    /// // Lookup by the authentication subkey's KeyID.
    /// let key = Store::lookup_by_subkeyid(&ctx, &"A3506AFB820ABD08".parse()?)?;
    /// assert_eq!(key.cert()?.fingerprint(), cert.fingerprint());
    /// # Ok(())
    /// # }
    /// ```
    pub fn lookup_by_subkeyid(c: &Context, keyid: &KeyID) -> Result<Key> {
        let (mut core, client) = Store::connect(c)?;
        let mut request = client.lookup_by_subkeyid_request();
        request.get().set_keyid(keyid.as_u64()?);
        let key = make_request!(&mut core, request)?;
        Ok(Key::new(Rc::new(RefCell::new(core)), key))
    }

    /// Lists all keys in the common key pool.
    pub fn list_keys(c: &Context) -> Result<KeyIter> {
        let (mut core, client) = Store::connect(c)?;
        let request = client.iter_keys_request();
        let iter = make_request!(&mut core, request)?;
        Ok(KeyIter{core: Rc::new(RefCell::new(core)), iter})
    }

    /// Lists all log entries.
    pub fn server_log(c: &Context) -> Result<LogIter> {
        let (mut core, client) = Store::connect(c)?;
        let request = client.log_request();
        let iter = make_request!(&mut core, request)?;
        Ok(LogIter{core: Rc::new(RefCell::new(core)), iter})
    }
}

/// A public key store.
pub struct Mapping {
    name: String,
    core: Rc<RefCell<RpcRuntime>>,
    mapping: node::mapping::Client,
}

impl fmt::Debug for Mapping {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Mapping {{ name: {} }}", self.name)
    }
}

impl Mapping {
    /// Opens a mapping.
    ///
    /// Opens a mapping with the given name.  If the mapping does not
    /// exist, it is created.  Mappings are handles for objects
    /// maintained by a background service.  The background service
    /// associates state with this name.
    ///
    /// The store updates Certs in compliance with the network policy
    /// of the context that created the mapping in the first place.
    /// Opening the mapping with a different network policy is
    /// forbidden.
    pub fn open(c: &Context, p: net::Policy, realm: &str, name: &str)
                -> Result<Self> {
        let (mut core, client) = Store::connect(c)?;

        let mut request = client.open_request();
        request.get().set_realm(realm);
        request.get().set_network_policy(p.into());
        request.get().set_ephemeral(c.ephemeral());
        request.get().set_name(name);

        let mapping = make_request!(&mut core, request)?;
        Ok(Self::new(Rc::new(RefCell::new(core)), name, mapping))
    }

    fn new(core: Rc<RefCell<RpcRuntime>>, name: &str, mapping: node::mapping::Client) -> Self {
        Mapping{core, name: name.into(), mapping}
    }

    /// Lists all mappings with the given prefix.
    pub fn list(c: &Context, realm_prefix: &str) -> Result<MappingIter> {
        let (mut core, client) = Store::connect(c)?;
        let mut request = client.iter_request();
        request.get().set_realm_prefix(realm_prefix);
        let iter = make_request!(&mut core, request)?;
        Ok(MappingIter{core: Rc::new(RefCell::new(core)), iter})
    }

    /// Adds a key identified by fingerprint to the mapping.
    ///
    /// # Examples
    ///
    /// ```
    /// # use sequoia_openpgp as openpgp;
    /// # use openpgp::Fingerprint;
    /// # use sequoia_ipc::{Context, IPCPolicy};
    /// # use sequoia_net as net;
    /// # use sequoia_store::*;
    /// # fn main() { f().unwrap(); }
    /// # fn f() -> Result<()> {
    /// # let ctx = Context::configure()
    /// #     .ipc_policy(IPCPolicy::Internal)
    /// #     .ephemeral().build()?;
    /// let mapping = Mapping::open(&ctx, net::Policy::Offline,
    ///                             REALM_CONTACTS, "default")?;
    /// let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb");
    /// mapping.add("Mister B.", &fp)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn add(&self, label: &str, fingerprint: &Fingerprint) -> Result<Binding> {
        let mut request = self.mapping.add_request();
        request.get().set_label(label);
        request.get().set_fingerprint(format!("{:X}", fingerprint).as_ref());
        let binding = make_request!(self.core.borrow_mut(), request)?;
        Ok(Binding::new(self.core.clone(), Some(label), binding))
    }

    /// Imports a key into the mapping.
    ///
    /// # Examples
    ///
    /// ```
    /// # use sequoia_openpgp as openpgp;
    /// # use openpgp::Cert;
    /// # use openpgp::parse::Parse;
    /// # use sequoia_ipc::{Context, IPCPolicy};
    /// # use sequoia_net as net;
    /// # use sequoia_store::*;
    /// # fn main() { f().unwrap(); }
    /// # fn f() -> Result<()> {
    /// # let ctx = Context::configure()
    /// #     .ipc_policy(IPCPolicy::Internal)
    /// #     .ephemeral().build()?;
    /// # let cert = Cert::from_bytes(
    /// #     &include_bytes!("../../openpgp/tests/data/keys/testy.pgp")[..]).unwrap();
    /// let mapping = Mapping::open(&ctx, net::Policy::Offline,
    ///                             REALM_CONTACTS, "default")?;
    /// mapping.import("Testy McTestface", &cert)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn import(&self, label: &str, cert: &Cert) -> Result<Cert> {
        let fingerprint = cert.fingerprint();
        let mut request = self.mapping.add_request();
        request.get().set_label(label);
        request.get().set_fingerprint(format!("{:X}", fingerprint).as_ref());
        let binding = make_request!(self.core.borrow_mut(), request)?;
        let binding = Binding::new(self.core.clone(), Some(label), binding);
        binding.import(cert)
    }

    /// Returns the binding for the given label.
    ///
    /// # Examples
    ///
    /// ```
    /// # use sequoia_openpgp as openpgp;
    /// # use openpgp::Fingerprint;
    /// # use sequoia_ipc::{Context, IPCPolicy};
    /// # use sequoia_net as net;
    /// # use sequoia_store::*;
    /// # fn main() { f().unwrap(); }
    /// # fn f() -> Result<()> {
    /// # let ctx = Context::configure()
    /// #     .ipc_policy(IPCPolicy::Internal)
    /// #     .ephemeral().build()?;
    /// let mapping = Mapping::open(&ctx, net::Policy::Offline,
    ///                             REALM_CONTACTS, "default")?;
    /// let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb");
    /// mapping.add("Mister B.", &fp)?;
    /// drop(mapping);
    /// // ...
    /// let mapping = Mapping::open(&ctx, net::Policy::Offline,
    ///                             REALM_CONTACTS, "default")?;
    /// let binding = mapping.lookup("Mister B.")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn lookup(&self, label: &str) -> Result<Binding> {
        let mut request = self.mapping.lookup_request();
        request.get().set_label(label);
        let binding = make_request!(self.core.borrow_mut(), request)?;
        Ok(Binding::new(self.core.clone(), Some(label), binding))
    }

    /// Looks up a key by (Sub)KeyID.
    ///
    /// The KeyID may also reference a subkey.
    ///
    /// # Examples
    ///
    /// ```
    /// # use sequoia_openpgp as openpgp;
    /// # use openpgp::{Cert, KeyID};
    /// # use openpgp::parse::Parse;
    /// # use sequoia_ipc::{Context, IPCPolicy};
    /// # use sequoia_net as net;
    /// # use sequoia_store::*;
    /// # fn main() { f().unwrap(); }
    /// # fn f() -> Result<()> {
    /// # let ctx = Context::configure()
    /// #     .ipc_policy(IPCPolicy::Internal)
    /// #     .ephemeral().build()?;
    /// # let cert = Cert::from_bytes(
    /// #     &include_bytes!("../../openpgp/tests/data/keys/emmelie-dorothea-dina-samantha-awina-ed25519.pgp")[..])
    /// #     .unwrap();
    /// let mapping = Mapping::open(&ctx, net::Policy::Offline,
    ///                             REALM_CONTACTS, "default")?;
    /// mapping.import("Emmelie", &cert)?;
    ///
    /// // Lookup by the primary key's KeyID.
    /// let cert_ = mapping.lookup_by_subkeyid(&"069C0C348DD82C19".parse()?)?
    ///     .cert()?;
    /// assert_eq!(cert, cert_);
    ///
    /// // Lookup by the subkey's KeyID.
    /// let cert_ = mapping.lookup_by_subkeyid(&"22E3FAFE96B56C32".parse()?)?
    ///     .cert()?;
    /// assert_eq!(cert, cert_);
    /// # Ok(())
    /// # }
    /// ```
    pub fn lookup_by_subkeyid(&self, keyid: &KeyID) -> Result<Binding> {
        let mut request = self.mapping.lookup_by_subkeyid_request();
        request.get().set_keyid(keyid.as_u64()?);
        let binding = make_request!(self.core.borrow_mut(), request)?;
        let mut binding = Binding::new(self.core.clone(), None, binding);
        binding.label = binding.label().ok();
        Ok(binding)
    }

    /// Deletes this mapping.
    ///
    /// # Examples
    ///
    /// ```
    /// # use sequoia_openpgp as openpgp;
    /// # use openpgp::Fingerprint;
    /// # use sequoia_ipc::{Context, IPCPolicy};
    /// # use sequoia_net as net;
    /// # use sequoia_store::*;
    /// # fn main() { f().unwrap(); }
    /// # fn f() -> Result<()> {
    /// # let ctx = Context::configure()
    /// #     .ipc_policy(IPCPolicy::Internal)
    /// #     .ephemeral().build()?;
    /// let mapping = Mapping::open(&ctx, net::Policy::Offline,
    ///                             REALM_CONTACTS, "default")?;
    /// let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb");
    /// mapping.add("Mister B.", &fp)?;
    /// mapping.delete()?;
    /// // ...
    /// let mapping = Mapping::open(&ctx, net::Policy::Offline,
    ///                             REALM_CONTACTS, "default")?;
    /// let binding = mapping.lookup("Mister B.");
    /// assert!(binding.is_err()); // not found
    /// # Ok(())
    /// # }
    /// ```
    pub fn delete(self) -> Result<()> {
        let request = self.mapping.delete_request();
        make_request_map!(self.core.borrow_mut(), request, |_| Ok(()))
    }

    /// Lists all bindings.
    pub fn iter(&self) -> Result<BundleIter> {
        let request = self.mapping.iter_request();
        let iter = make_request!(self.core.borrow_mut(), request)?;
        Ok(BundleIter{core: self.core.clone(), iter})
    }

    /// Lists all log entries related to this mapping.
    pub fn log(&self) -> Result<LogIter> {
        let request = self.mapping.log_request();
        let iter = make_request!(self.core.borrow_mut(), request)?;
        Ok(LogIter{core: self.core.clone(), iter})
    }
}

/// Makes a stats request and parses the result.
macro_rules! make_stats_request {
    ( $core: expr, $request: expr ) => {{
        make_request_map!(
            $core, $request,
            |s: node::stats::Reader| Ok(Stats{
                created: from_unix(s.get_created()),
                updated: from_unix(s.get_updated()),
                encryption: Stamps::new(
                    s.get_encryption_count(),
                    from_unix(s.get_encryption_first()),
                    from_unix(s.get_encryption_last())),
                verification: Stamps::new(
                    s.get_verification_count(),
                    from_unix(s.get_verification_first()),
                    from_unix(s.get_verification_last())),
            }))
    }}
}

/// Represents an entry in a Mapping.
///
/// Mappings map labels to Certs.  A `Binding` represents a pair in this
/// relation.  We make this explicit because we associate metadata
/// with these pairs.
pub struct Binding {
    label: Option<String>,
    core: Rc<RefCell<RpcRuntime>>,
    binding: node::binding::Client,
}

impl fmt::Debug for Binding {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Binding {{ label: {:?} }}", self.label)
    }
}

impl Binding {
    fn new(core: Rc<RefCell<RpcRuntime>>,
           label: Option<&str>,
           binding: node::binding::Client) -> Self {
        Binding{label: label.map(|l| l.into()), core, binding}
    }

    /// Returns stats for this binding.
    ///
    /// # Examples
    ///
    /// ```
    /// # use sequoia_openpgp as openpgp;
    /// # use openpgp::Fingerprint;
    /// # use sequoia_ipc::{Context, IPCPolicy};
    /// # use sequoia_net as net;
    /// # use sequoia_store::*;
    /// # fn main() { f().unwrap(); }
    /// # fn f() -> Result<()> {
    /// # let ctx = Context::configure()
    /// #     .ipc_policy(IPCPolicy::Internal)
    /// #     .ephemeral().build()?;
    /// let mapping = Mapping::open(&ctx, net::Policy::Offline,
    ///                             REALM_CONTACTS, "default")?;
    ///
    /// let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb");
    /// let binding = mapping.add("Mister B.", &fp)?;
    ///
    /// println!("Binding {:?}", binding.stats()?);
    /// // prints:
    /// // Binding Stats {
    /// //     created: Some(Timespec { tv_sec: 1513704042, tv_nsec: 0 }),
    /// //     updated: None,
    /// //     encryption: Stamps { count: 0, first: None, last: None },
    /// //     verification: Stamps { count: 0, first: None, last: None }
    /// // }
    /// # Ok(())
    /// # }
    /// ```
    pub fn stats(&self) -> Result<Stats> {
        make_stats_request!(self.core.borrow_mut(),
                            self.binding.stats_request())
    }

    /// Returns the `Key` of this binding.
    pub fn key(&self) -> Result<Key> {
        make_request_map!(self.core.borrow_mut(),
                          self.binding.key_request(),
                          |cert| Ok(Key::new(self.core.clone(), cert)))
    }

    /// Returns the `Cert` of this binding.
    ///
    /// A shortcut for `self.key()?.cert()`.
    pub fn cert(&self) -> Result<Cert> {
        self.key()?.cert()
    }

    /// Updates this binding with the given Cert.
    ///
    /// If the new key `cert` matches the current key, i.e. they have
    /// the same fingerprint, both keys are merged and normalized.
    /// The returned key contains all packets known to Sequoia, and
    /// should be used instead of `cert`.
    ///
    /// If the new key does not match the current key, and it does not
    /// carry a valid signature from the current key, an
    /// `Error::Conflict` is returned, and you have to resolve the
    /// conflict, either by ignoring the new key, or by using
    /// `Binding::rotate` to force a rotation.
    ///
    /// # Examples
    ///
    /// ```
    /// # use sequoia_openpgp as openpgp;
    /// # use openpgp::Cert;
    /// # use openpgp::parse::Parse;
    /// # use sequoia_ipc::{Context, IPCPolicy};
    /// # use sequoia_net as net;
    /// # use sequoia_store::*;
    /// # fn main() { f().unwrap(); }
    /// # fn f() -> Result<()> {
    /// # let ctx = Context::configure()
    /// #     .ipc_policy(IPCPolicy::Internal)
    /// #     .ephemeral().build()?;
    /// # let old = Cert::from_bytes(
    /// #     &include_bytes!("../../openpgp/tests/data/keys/testy.pgp")[..]).unwrap();
    /// # let new = Cert::from_bytes(
    /// #     &include_bytes!("../../openpgp/tests/data/keys/testy-new.pgp")[..]).unwrap();
    /// let mapping = Mapping::open(&ctx, net::Policy::Offline,
    ///                             REALM_CONTACTS, "default")?;
    /// mapping.import("Testy McTestface", &old)?;
    /// // later...
    /// let binding = mapping.lookup("Testy McTestface")?;
    /// let r = binding.import(&new);
    /// assert!(r.is_err()); // Conflict!
    /// # Ok(())
    /// # }
    /// ```
    pub fn import(&self, cert: &Cert) -> Result<Cert> {
        let mut blob = vec![];
        cert.serialize(&mut blob)?;
        let mut request = self.binding.import_request();
        request.get().set_force(false);
        request.get().set_key(&blob);
        make_request_map!(
            self.core.borrow_mut(),
            request,
            |data| Cert::from_bytes(data))
    }

    /// Forces a keyrotation to the given Cert.
    ///
    /// The current key is replaced with the new key `cert`, even if
    /// they do not have the same fingerprint.  If a key with the same
    /// fingerprint as `cert` is already in the store, is merged with
    /// `cert` and normalized.  The returned key contains all packets
    /// known to Sequoia, and should be used instead of `cert`.
    ///
    /// Use this function to resolve conflicts returned from
    /// `Binding::import`.  Make sure that you have authenticated
    /// `cert` properly.  How to do that depends on your thread model.
    /// You could simply ask Alice to call her communication partner
    /// Bob and confirm that he rotated his keys.
    ///
    /// # Examples
    ///
    /// ```
    /// # use sequoia_openpgp as openpgp;
    /// # use openpgp::Cert;
    /// # use openpgp::parse::Parse;
    /// # use sequoia_ipc::{Context, IPCPolicy};
    /// # use sequoia_net as net;
    /// # use sequoia_store::*;
    /// # fn main() { f().unwrap(); }
    /// # fn f() -> Result<()> {
    /// # let ctx = Context::configure()
    /// #     .ipc_policy(IPCPolicy::Internal)
    /// #     .ephemeral().build()?;
    /// # let old = Cert::from_bytes(
    /// #     &include_bytes!("../../openpgp/tests/data/keys/testy.pgp")[..]).unwrap();
    /// # let new = Cert::from_bytes(
    /// #     &include_bytes!("../../openpgp/tests/data/keys/testy-new.pgp")[..]).unwrap();
    /// let mapping = Mapping::open(&ctx, net::Policy::Offline,
    ///                             REALM_CONTACTS, "default")?;
    /// mapping.import("Testy McTestface", &old)?;
    /// // later...
    /// let binding = mapping.lookup("Testy McTestface")?;
    /// let r = binding.import(&new);
    /// assert!(r.is_err()); // Conflict!
    /// let r = binding.rotate(&new)?;
    /// assert_eq!(new.fingerprint(), r.fingerprint());
    /// # Ok(())
    /// # }
    /// ```
    pub fn rotate(&self, cert: &Cert) -> Result<Cert> {
        let mut blob = vec![];
        cert.serialize(&mut blob)?;
        let mut request = self.binding.import_request();
        request.get().set_force(true);
        request.get().set_key(&blob);
        make_request_map!(
            self.core.borrow_mut(),
            request,
            |data| Cert::from_bytes(data))
    }

    /// Deletes this binding.
    ///
    /// # Examples
    ///
    /// ```
    /// # use sequoia_openpgp as openpgp;
    /// # use openpgp::Fingerprint;
    /// # use sequoia_ipc::{Context, IPCPolicy};
    /// # use sequoia_net as net;
    /// # use sequoia_store::*;
    /// # fn main() { f().unwrap(); }
    /// # fn f() -> Result<()> {
    /// # let ctx = Context::configure()
    /// #     .ipc_policy(IPCPolicy::Internal)
    /// #     .ephemeral().build()?;
    /// let mapping = Mapping::open(&ctx, net::Policy::Offline,
    ///                             REALM_CONTACTS, "default")?;
    /// let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb");
    /// let binding = mapping.add("Mister B.", &fp)?;
    /// binding.delete()?;
    /// let binding = mapping.lookup("Mister B.");
    /// assert!(binding.is_err()); // not found
    /// # Ok(())
    /// # }
    /// ```
    pub fn delete(self) -> Result<()> {
        let request = self.binding.delete_request();
        make_request_map!(self.core.borrow_mut(), request, |_| Ok(()))
    }

    fn register_encryption(&self) -> Result<Stats> {
        #![allow(dead_code)]     // XXX use
        make_stats_request!(
            self.core.borrow_mut(),
            self.binding.register_encryption_request())
    }

    fn register_verification(&self) -> Result<Stats> {
        #![allow(dead_code)]     // XXX use
        make_stats_request!(
            self.core.borrow_mut(),
            self.binding.register_verification_request())
    }

    /// Lists all log entries related to this binding.
    pub fn log(&self) -> Result<LogIter> {
        let request = self.binding.log_request();
        let iter = make_request!(self.core.borrow_mut(), request)?;
        Ok(LogIter{core: self.core.clone(), iter})
    }

    /// Gets this binding's label.
    pub fn label(&self) -> Result<String> {
        if let Some(ref label) = self.label {
            return Ok(label.clone());
        }

        let request = self.binding.label_request();
        make_request_map!(self.core.borrow_mut(),
                          request,
                          |l: &str| Ok(l.into()))
    }
}

/// Represents a key in the store.
///
/// A `Key` is a handle to a stored Cert.  We make this explicit
/// because we associate metadata with Certs.
pub struct Key {
    core: Rc<RefCell<RpcRuntime>>,
    key: node::key::Client,
}

impl fmt::Debug for Key {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Key {{ }}")
    }
}

impl Key {
    fn new(core: Rc<RefCell<RpcRuntime>>, key: node::key::Client) -> Self {
        Key{core, key}
    }

    /// Returns the Cert.
    pub fn cert(&self) -> Result<Cert> {
        make_request_map!(self.core.borrow_mut(),
                          self.key.cert_request(),
                          |cert| Cert::from_bytes(cert))
    }

    /// Returns stats for this key.
    pub fn stats(&self) -> Result<Stats> {
        make_stats_request!(self.core.borrow_mut(),
                            self.key.stats_request())
    }

    /// Updates this stored key with the given Cert.
    ///
    /// If the new key `cert` matches the current key, i.e. they have
    /// the same fingerprint, both keys are merged and normalized.
    /// The returned key contains all packets known to Sequoia, and
    /// should be used instead of `cert`.
    ///
    /// If the new key does not match the current key,
    /// `Error::Conflict` is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// # use sequoia_openpgp as openpgp;
    /// # use openpgp::Fingerprint;
    /// # use openpgp::Cert;
    /// # use openpgp::parse::Parse;
    /// # use sequoia_ipc::{Context, IPCPolicy};
    /// # use sequoia_net as net;
    /// # use sequoia_store::*;
    /// # fn main() { f().unwrap(); }
    /// # fn f() -> Result<()> {
    /// # let ctx = Context::configure()
    /// #     .ipc_policy(IPCPolicy::Internal)
    /// #     .ephemeral().build()?;
    /// # let old = Cert::from_bytes(
    /// #     &include_bytes!("../../openpgp/tests/data/keys/testy.pgp")[..]).unwrap();
    /// # let new = Cert::from_bytes(
    /// #     &include_bytes!("../../openpgp/tests/data/keys/testy-new.pgp")[..]).unwrap();
    /// let mapping = Mapping::open(&ctx, net::Policy::Offline,
    ///                             REALM_CONTACTS, "default")?;
    /// let fp = "3E8877C877274692975189F5D03F6F865226FE8B".parse().unwrap();
    /// let binding = mapping.add("Testy McTestface", &fp)?;
    /// let key = binding.key()?;
    /// let r = key.import(&old)?;
    /// assert_eq!(r.fingerprint(), old.fingerprint());
    /// let r = key.import(&new);
    /// assert!(r.is_err()); // conflict
    /// # Ok(())
    /// # }
    /// ```
    pub fn import(&self, cert: &Cert) -> Result<Cert> {
        let mut blob = vec![];
        cert.serialize(&mut blob)?;
        let mut request = self.key.import_request();
        request.get().set_key(&blob);
        make_request_map!(
            self.core.borrow_mut(),
            request,
            |data| Cert::from_bytes(data))
    }

    /// Lists all log entries related to this key.
    pub fn log(&self) -> Result<LogIter> {
        let request = self.key.log_request();
        let iter = make_request!(self.core.borrow_mut(), request)?;
        Ok(LogIter{core: self.core.clone(), iter})
    }
}


/// Returns `t` as time::SystemTime.
fn from_unix(t: i64) -> Option<time::SystemTime> {
    if t == 0 {
        None
    } else {
        // XXX: Backend and frontend should really communicate
        // unsigned timestamps.
        Some(time::UNIX_EPOCH + time::Duration::new(t as u64, 0))
    }
}

/// Statistics about bindings and stored keys.
///
/// We collect some data about binginds and stored keys.  This
/// information can be used to make informed decisions about key
/// transitions.
#[derive(Debug)]
pub struct Stats {
    /// Records the time this item was created.
    pub created: Option<time::SystemTime>,

    /// Records the time this item was last updated.
    pub updated: Option<time::SystemTime>,

    /// Records counters and timestamps of encryptions.
    pub encryption: Stamps,

    /// Records counters and timestamps of verifications.
    pub verification: Stamps,
}

/// Represents a log entry.
#[derive(Debug)]
pub struct Log {
    /// Records the time of the entry.
    pub timestamp: time::SystemTime,

    /// Relates the entry to a mapping.
    pub mapping: Option<Mapping>,

    /// Relates the entry to a binding.
    pub binding: Option<Binding>,

    /// Relates the entry to a key.
    pub key: Option<Key>,

    /// Relates the entry to some object.
    ///
    /// This is a human-readable description of what this log entry is
    /// mainly concerned with.
    pub slug: String,

    /// Holds the result of the operation.
    ///
    /// This is either `Ok(Message)`, or `Err((Message, Error))`.
    pub status: ::std::result::Result<String, (String, String)>,
}

impl Log {
    fn new(timestamp: i64,
           mapping: Option<Mapping>, binding: Option<Binding>, key: Option<Key>,
           slug: &str, message: &str, error: Option<&str>)
           -> Option<Self> {
        let timestamp = from_unix(timestamp)?;

        Some(Log{
            timestamp,
            mapping,
            binding,
            key,
            slug: slug.into(),
            status: if let Some(error) = error {
                Err((message.into(), error.into()))
            } else {
                Ok(message.into())
            },
        })
    }

    /// Returns the message without context.
    pub fn short(&self) -> String {
        match self.status {
            Ok(ref m) => m.clone(),
            Err((ref m, ref e)) => format!("{}: {}", m, e),
        }
    }

    /// Returns the message with some context.
    pub fn string(&self) -> String {
        match self.status {
            Ok(ref m) => format!("{}: {}", self.slug, m),
            Err((ref m, ref e)) => format!("{}: {}: {}", self.slug, m, e),
        }
    }
}

/// Counter and timestamps.
#[derive(Debug)]
pub struct Stamps {
    /// Counts how many times this has been used.
    pub count: usize,

    /// Records the time when this has been used first.
    pub first:  Option<time::SystemTime>,

    /// Records the time when this has been used last.
    pub last: Option<time::SystemTime>,
}

impl Stamps {
    fn new(count: i64,
           first: Option<time::SystemTime>,
           last: Option<time::SystemTime>) -> Self {
        Stamps {
            count: count as usize,
            first,
            last,
        }
    }
}

/* Iterators.  */

/// Iterates over mappings.
pub struct MappingIter {
    core: Rc<RefCell<RpcRuntime>>,
    iter: node::mapping_iter::Client,
}

impl Iterator for MappingIter {
    type Item = (String, String, net::Policy, Mapping);

    fn next(&mut self) -> Option<Self::Item> {
        let request = self.iter.next_request();
        let doit = || {
            make_request_map!(
                self.core.borrow_mut(), request,
                |r: node::mapping_iter::item::Reader|
                Ok((
                    r.get_realm()?.into(),
                    r.get_name()?.into(),
                    r.get_network_policy()?.into(),
                    Mapping::new(self.core.clone(), r.get_name()?, r.get_mapping()?))))
        };
        doit().ok()
    }
}

/// Iterates over bindings in a mapping.
pub struct BundleIter {
    core: Rc<RefCell<RpcRuntime>>,
    iter: node::binding_iter::Client,
}

impl Iterator for BundleIter {
    type Item = (String, openpgp::Fingerprint, Binding);

    fn next(&mut self) -> Option<Self::Item> {
        let request = self.iter.next_request();
        let doit = || {
            make_request_map!(
                self.core.borrow_mut(), request,
                |r: node::binding_iter::item::Reader|
                Ok((String::from(r.get_label()?),
                    r.get_fingerprint().unwrap().parse()?,
                    Binding::new(self.core.clone(), Some(r.get_label()?),
                                 r.get_binding()?))))
        };
        doit().ok()
    }
}

/// Iterates over keys in the common key pool.
pub struct KeyIter {
    core: Rc<RefCell<RpcRuntime>>,
    iter: node::key_iter::Client,
}

impl Iterator for KeyIter {
    type Item = (openpgp::Fingerprint, Key);

    fn next(&mut self) -> Option<Self::Item> {
        let request = self.iter.next_request();
        let doit = || {
            make_request_map!(
                self.core.borrow_mut(), request,
                |r: node::key_iter::item::Reader|
                Ok((r.get_fingerprint().unwrap().parse()?,
                    Key::new(self.core.clone(), r.get_key()?))))
        };
        doit().ok()
    }
}

/// Iterates over logs.
pub struct LogIter {
    core: Rc<RefCell<RpcRuntime>>,
    iter: node::log_iter::Client,
}

impl Iterator for LogIter {
    type Item = Log;

    fn next(&mut self) -> Option<Self::Item> {
        let request = self.iter.next_request();
        let doit = || {
            make_request_map!(
                self.core.borrow_mut(), request,
                |r: node::log_iter::entry::Reader|
                Log::new(r.get_timestamp(),
                         r.get_mapping().ok().map(
                             |cap| Mapping::new(self.core.clone(), &"", cap)),
                         r.get_binding().ok().map(
                             |cap| Binding::new(self.core.clone(), None, cap)),
                         r.get_key().ok().map(
                             |cap| Key::new(self.core.clone(), cap)),
                         r.get_slug()?,
                         r.get_message()?,
                         if r.has_error() {
                             r.get_error().ok()
                         } else {
                             None
                         }).ok_or_else(|| Error::StoreError.into()))
        };
        doit().ok()
    }
}

/* Error handling.  */

/// Results for sequoia-store.
pub type Result<T> = ::std::result::Result<T, anyhow::Error>;


// Converts from backend errors.
impl From<node::Error> for anyhow::Error {
    fn from(error: node::Error) -> Self {
        match error {
            node::Error::Unspecified => Error::StoreError.into(),
            node::Error::NotFound => Error::NotFound.into(),
            node::Error::Conflict => Error::Conflict.into(),
            node::Error::SystemError => Error::StoreError.into(),
            node::Error::MalformedCert => Error::MalformedCert.into(),
            node::Error::MalformedFingerprint =>
                Error::MalformedFingerprint.into(),
            node::Error::NetworkPolicyViolationOffline =>
                net::Error::PolicyViolation(net::Policy::Offline).into(),
            node::Error::NetworkPolicyViolationAnonymized =>
                net::Error::PolicyViolation(net::Policy::Anonymized).into(),
            node::Error::NetworkPolicyViolationEncrypted =>
                net::Error::PolicyViolation(net::Policy::Encrypted).into(),
            node::Error::NetworkPolicyViolationInsecure =>
                net::Error::PolicyViolation(net::Policy::Insecure).into(),
        }
    }
}

#[derive(thiserror::Error, Debug)]
/// Errors returned from the store.
pub enum Error {
    /// A requested key was not found.
    #[error("Key not found")]
    NotFound,
    /// The new key is in conflict with the current key.
    #[error("New key conflicts with the current key")]
    Conflict,
    /// This is a catch-all for unspecified backend errors, and should
    /// go away soon.
    #[error("Unspecified store error")]
    StoreError,
    /// A protocol error occurred.
    #[error("Unspecified protocol error")]
    ProtocolError,
    /// A Cert is malformed.
    #[error("Malformed Cert")]
    MalformedCert,
    /// A fingerprint is malformed.
    #[error("Malformed fingerprint")]
    MalformedFingerprint,
    /// A `capnp::Error` occurred.
    #[error("Internal RPC error")]
    RpcError(#[from] capnp::Error),
}

impl From<capnp::NotInSchema> for Error {
    fn from(_: capnp::NotInSchema) -> Self {
        Error::ProtocolError
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::openpgp::parse::Parse;

    // Like assert!, but checks a pattern.
    //
    //   assert_match!(Some(_) = x);
    //
    // Note: For modules to see this macro, we need to define it before we
    // declare the modules.
    #[allow(unused_macros)]
    macro_rules! assert_match {
        ( $error: pat = $expr:expr, $fmt:expr, $($pargs:expr),* ) => {{
            let x = $expr;
            if let $error = x {
                /* Pass.  */
            } else {
                let extra = format!($fmt, $($pargs),*);
                panic!("Expected {}, got {:?}{}{}",
                       stringify!($error), x,
                       if $fmt.len() > 0 { ": " } else { "." }, extra);
            }
        }};
        ( $error: pat = $expr: expr, $fmt:expr ) => {
            assert_match!($error = $expr, $fmt, );
        };
        ( $error: pat = $expr: expr ) => {
            assert_match!($error = $expr, "");
        };
    }

    macro_rules! bytes {
        ( $x:expr ) => { include_bytes!(concat!("../../openpgp/tests/data/keys/", $x)) };
    }

    const P: net::Policy = net::Policy::Offline;

    #[test]
    fn mapping_network_policy_mismatch() {
        let ctx = ipc::Context::configure()
            .ephemeral()
            .ipc_policy(ipc::IPCPolicy::Internal)
            .build().unwrap();
        // Create mapping.
        Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap();

        let ctx2 = ipc::Context::configure()
            .home(ctx.home())
            .ipc_policy(ipc::IPCPolicy::Internal)
            .build().unwrap();
        let mapping = Mapping::open(&ctx2, net::Policy::Encrypted,
                                    REALM_CONTACTS, "default");
        assert_match!(net::Error::PolicyViolation(_)
                      = mapping.err().unwrap().downcast::<net::Error>().unwrap());
    }

    #[test]
    fn import_key() {
        let ctx = ipc::Context::configure()
            .ephemeral()
            .ipc_policy(ipc::IPCPolicy::Internal)
            .build().unwrap();
        let mapping = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap();
        let cert = Cert::from_bytes(&bytes!("testy.pgp")[..]).unwrap();
        mapping.import("Mr. McTestface", &cert).unwrap();
        let binding = mapping.lookup("Mr. McTestface").unwrap();
        let cert_retrieved = binding.cert().unwrap();
        assert_eq!(cert.fingerprint(), cert_retrieved.fingerprint());
    }

    #[test]
    fn key_not_found() {
        let ctx = ipc::Context::configure()
            .ephemeral()
            .ipc_policy(ipc::IPCPolicy::Internal)
            .build().unwrap();
        let mapping = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap();
        let r = mapping.lookup("I do not exist");
        assert_match!(Error::NotFound
                      = r.err().unwrap().downcast::<Error>().unwrap());
    }

    #[test]
    fn add_then_import_wrong_key() {
        let ctx = ipc::Context::configure()
            .ephemeral()
            .ipc_policy(ipc::IPCPolicy::Internal)
            .build().unwrap();
        let mapping = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap();
        let cert = Cert::from_bytes(&bytes!("testy.pgp")[..]).unwrap();
        let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb");
        let binding = mapping.add("Mister B.", &fp).unwrap();
        let r = binding.import(&cert);
        assert_match!(Error::Conflict
                      = r.err().unwrap().downcast::<Error>().unwrap());
    }

    #[test]
    fn add_then_add_different_key() {
        let ctx = ipc::Context::configure()
            .ephemeral()
            .ipc_policy(ipc::IPCPolicy::Internal)
            .build().unwrap();
        let mapping = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap();
        let b = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb");
        mapping.add("Mister B.", &b).unwrap();
        let c = Fingerprint::from_bytes(b"cccccccccccccccccccc");
        assert_match!(Error::Conflict
                      = mapping.add("Mister B.", &c)
                      .err().unwrap().downcast::<Error>().unwrap());
    }

    #[test]
    fn delete_mapping_twice() {
        let ctx = ipc::Context::configure()
            .ephemeral()
            .ipc_policy(ipc::IPCPolicy::Internal)
            .build().unwrap();
        let s0 = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap();
        let s1 = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap();
        s0.delete().unwrap();
        s1.delete().unwrap();
    }

    #[test]
    fn delete_mapping_then_use() {
        let ctx = ipc::Context::configure()
            .ephemeral()
            .ipc_policy(ipc::IPCPolicy::Internal)
            .build().unwrap();
        let s0 = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap();
        let s1 = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap();
        s0.delete().unwrap();
        let binding = s1.lookup("Foobarbaz");
        assert_match!(Error::NotFound
                      = binding.err().unwrap().downcast::<Error>().unwrap());
        let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb");
        let binding = s1.add("Mister B.", &fp);
        assert_match!(Error::NotFound
                      = binding.err().unwrap().downcast::<Error>().unwrap());
    }

    #[test]
    fn delete_binding_twice() {
        let ctx = ipc::Context::configure()
            .ephemeral()
            .ipc_policy(ipc::IPCPolicy::Internal)
            .build().unwrap();
        let mapping = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap();
        let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb");
        let b0 = mapping.add("Mister B.", &fp).unwrap();
        let b1 = mapping.lookup("Mister B.").unwrap();
        b0.delete().unwrap();
        b1.delete().unwrap();
    }

    #[test]
    fn delete_binding_then_use() {
        let ctx = ipc::Context::configure()
            .ephemeral()
            .ipc_policy(ipc::IPCPolicy::Internal)
            .build().unwrap();
        let mapping = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap();
        let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb");
        let b0 = mapping.add("Mister B.", &fp).unwrap();
        let b1 = mapping.lookup("Mister B.").unwrap();
        b0.delete().unwrap();
        assert_match!(Error::NotFound
                      = b1.stats().err().unwrap().downcast::<Error>().unwrap());
        assert_match!(Error::NotFound
                      = b1.key().err().unwrap().downcast::<Error>().unwrap());
    }

    fn make_some_mappings() -> ipc::Context {
        let ctx0 = ipc::Context::configure()
            .ephemeral()
            .ipc_policy(ipc::IPCPolicy::Internal)
            .build().unwrap();
        let mapping = Mapping::open(&ctx0, P, REALM_CONTACTS, "default").unwrap();
        let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb");
        mapping.add("Mister B.", &fp).unwrap();
        mapping.add("B4", &fp).unwrap();

        Mapping::open(&ctx0, P, REALM_CONTACTS, "another mapping").unwrap();

        let ctx1 = ipc::Context::configure()
            .home(ctx0.home())
            .ipc_policy(ipc::IPCPolicy::Internal)
            .build().unwrap();
        let mapping =
            Mapping::open(&ctx1, P, REALM_SOFTWARE_UPDATES, "default").unwrap();
        let fp = Fingerprint::from_bytes(b"cccccccccccccccccccc");
        mapping.add("Mister C.", &fp).unwrap();

        ctx0
    }

    #[test]
    fn stats() {
        let ctx = make_some_mappings();
        let mapping = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap();
        let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb");
        let binding = mapping.add("Mister B.", &fp).unwrap();

        let stats0 = binding.stats().unwrap();
        assert_match!(Some(_) = stats0.created);
        assert_match!(None = stats0.updated);
        assert_eq!(stats0.encryption.count, 0);
        assert_match!(None = stats0.encryption.first);
        assert_match!(None = stats0.encryption.last);
        assert_eq!(stats0.verification.count, 0);
        assert_match!(None = stats0.verification.first);
        assert_match!(None = stats0.verification.last);

        binding.register_encryption().unwrap();
        binding.register_encryption().unwrap();
        binding.register_verification().unwrap();

        let stats1 = binding.stats().unwrap();
        assert_match!(Some(_) = stats1.created);
        assert_eq!(stats0.created, stats1.created);
        assert_match!(None = stats1.updated);
        assert_eq!(stats1.encryption.count, 2);
        assert_match!(Some(_) = stats1.encryption.first);
        assert_match!(Some(_) = stats1.encryption.last);
        assert!(stats1.encryption.first <= stats1.encryption.last);
        assert_eq!(stats1.verification.count, 1);
        assert_match!(Some(_) = stats1.verification.first);
        assert_match!(Some(_) = stats1.verification.last);
        assert_eq!(stats1.verification.first, stats1.verification.last);
    }


    #[test]
    fn mapping_iterator() {
        let ctx = make_some_mappings();
        let mut iter = Mapping::list(&ctx, REALM_CONTACTS).unwrap();
        let (realm, name, network_policy, mapping) = iter.next().unwrap();
        assert_eq!(realm, REALM_CONTACTS);
        assert_eq!(name, "default");
        assert_eq!(network_policy, net::Policy::Offline);
        let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb");
        mapping.add("Mister B.", &fp).unwrap();
        let (realm, name, network_policy, mapping) = iter.next().unwrap();
        assert_eq!(realm, REALM_CONTACTS);
        assert_eq!(name, "another mapping");
        assert_eq!(network_policy, net::Policy::Offline);
        mapping.add("Mister B.", &fp).unwrap();
        assert!(iter.next().is_none());
    }

    #[test]
    fn binding_iterator() {
        let ctx = make_some_mappings();
        let mapping = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap();
        let mut iter = mapping.iter().unwrap();
        let (label, fingerprint, binding) = iter.next().unwrap();
        let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb");
        assert_eq!(label, "Mister B.");
        assert_eq!(fingerprint, fp);
        binding.stats().unwrap();
        let (label, fingerprint, binding) = iter.next().unwrap();
        assert_eq!(label, "B4");
        assert_eq!(fingerprint, fp);
        binding.stats().unwrap();
        assert!(iter.next().is_none());
    }

    #[test]
    fn key_iterator() {
        let ctx = make_some_mappings();
        let mut iter = Store::list_keys(&ctx).unwrap();
        let (fingerprint, key) = iter.next().unwrap();
        assert_eq!(fingerprint, Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb"));
        key.stats().unwrap();
        let (fingerprint, key) = iter.next().unwrap();
        assert_eq!(fingerprint, Fingerprint::from_bytes(b"cccccccccccccccccccc"));
        key.stats().unwrap();
        assert!(iter.next().is_none());
    }
}