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
//! A Rust implementation of the Stateless OpenPGP Interface.
//!
//! This crate defines an interface that is the Rust equivalent of the
//! [draft 08] of the Stateless OpenPGP Command Line Interface.  Note
//! that you need an concrete implementation of this interface (such
//! as [`sequoia-sop`]) in order to use it.
//!
//!   [draft 08]: https://www.ietf.org/archive/id/draft-dkg-openpgp-stateless-cli-08.html
//!   [`sequoia-sop`]: https://docs.rs/sequoia-sop
//!
//! # Examples
//!
//! Given a reference to a [`SOP`] implementation, which is the main
//! entry point for every SOP operation, generate keys, extract certs,
//! sign, verify, encrypt, and decrypt:
//!
//! ```rust
//! # use sop::*; use std::io::Cursor;
//! # fn sop_examples<'s, S: SOP<'s> + 's>(sop: &'s S) -> Result<()> {
//! let alice_sec = sop.generate_key()?
//!     .userid("Alice Lovelace <alice@openpgp.example>")
//!     .generate()?;
//! let alice_pgp = sop.extract_cert()?
//!     .keys(&alice_sec)?;
//!
//! let bob_sec = sop.generate_key()?
//!     .userid("Bob Babbage <bob@openpgp.example>")
//!     .generate()?;
//! let bob_pgp = sop.extract_cert()?
//!     .keys(&bob_sec)?;
//!
//! let statement = b"Hello World :)";
//! let mut data = Cursor::new(&statement);
//! let (_micalg, signature) = sop.sign()?
//!     .mode(ops::SignAs::Text)
//!     .keys(&alice_sec)?
//!     .data(&mut data)?;
//!
//! let verifications = sop.verify()?
//!     .certs(&alice_pgp)?
//!     .signatures(&signature)?
//!     .data(&mut Cursor::new(&statement))?;
//! assert_eq!(verifications.len(), 1);
//!
//! let mut statement_cur = Cursor::new(&statement);
//! let (_session_key, ciphertext) = sop.encrypt()?
//!     .sign_with_keys(&alice_sec)?
//!     .with_certs(&bob_pgp)?
//!     .plaintext(&mut statement_cur)?
//!     .to_vec()?;
//!
//! let mut ciphertext_cur = Cursor::new(&ciphertext);
//! let (_, plaintext) = sop.decrypt()?
//!     .with_keys(&bob_sec)?
//!     .ciphertext(&mut ciphertext_cur)?
//!     .to_vec()?;
//! assert_eq!(&plaintext, statement);
//! # Ok(()) }
//! ```
//!
//! The above snippet is the equivalent of the following SOP command
//! line example from the SOP spec:
//!
//! ```sh
//! $ sop generate-key "Alice Lovelace <alice@openpgp.example>" > alice.sec
//! $ sop extract-cert < alice.sec > alice.pgp
//!
//! $ sop sign --as=text alice.sec < statement.txt > statement.txt.asc
//! $ sop verify announcement.txt.asc alice.pgp < announcement.txt
//!
//! $ sop encrypt --sign-with=alice.sec bob.pgp < msg.eml > encrypted.asc
//! $ sop decrypt alice.sec < ciphertext.asc > cleartext.out
//! ```

use std::{
    fmt,
    io,
};

pub mod ops;
use ops::*;
pub mod errors;
use errors::*;
pub mod plumbing;

#[cfg(feature = "cli")]
pub mod cli;

/// Loads objects like certs and keys.
pub trait Load<'s, S: SOP<'s>> {
    /// Loads objects like certs and keys from the given reader.
    fn from_reader(sop: &'s S, source: &mut (dyn io::Read + Send + Sync))
                   -> Result<Self>
    where Self: Sized;

    /// Loads objects like certs and keys from the given byte slice.
    fn from_bytes(sop: &'s S, source: &[u8]) -> Result<Self>
    where
        Self: Sized,
    {
        Self::from_reader(sop, &mut io::Cursor::new(source))
    }
}

/// Saves objects like certs and keys.
pub trait Save {
    /// Saves objects like certs and keys, writing them to the given writer.
    fn to_writer(&self, armored: bool, sink: &mut (dyn io::Write + Send + Sync))
                 -> Result<()>;

    /// Saves objects like certs and keys, writing them to a vector.
    fn to_vec(&self, armored: bool) -> Result<Vec<u8>> {
        let mut sink = vec![];
        self.to_writer(armored, &mut sink)?;
        Ok(sink)
    }
}

/// Main entry point to the Stateless OpenPGP Interface.
pub trait SOP<'s>: Sized {
    /// Secret keys.
    type Keys: Load<'s, Self> + Save + plumbing::SopRef<'s, Self>;

    /// Public keys.
    type Certs: Load<'s, Self> + Save + plumbing::SopRef<'s, Self>;

    /// Signatures.
    type Sigs: Load<'s, Self> + Save + plumbing::SopRef<'s, Self>;

    /// Gets SOP version information.
    ///
    /// The default implementation returns the version of the spec
    /// that this framework supports.  This should be fine for most
    /// implementations.  However, implementations may chose to
    /// override this function to return a more nuanced response.
    fn spec_version(&'s self) -> &'static str {
        "~draft-dkg-openpgp-stateless-cli-08"
    }

    /// Gets version information.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use sop::*; use std::io::Cursor;
    /// # fn sop_examples<'s, S: SOP<'s> + 's>(sop: &'s S) -> Result<()> {
    /// // Prints the name of the SOP implementation.
    /// println!("{}", sop.version()?.frontend()?);
    ///
    /// // Prints the name of the underlying OpenPGP implementation.
    /// println!("{}", sop.version()?.backend()?);
    ///
    /// // Prints extended version information.
    /// println!("{}", sop.version()?.extended()?);
    /// # Ok(()) }
    /// ```
    fn version(&'s self) -> Result<Box<dyn Version + 's>>;

    /// Generates a Secret Key.
    ///
    /// Customize the operation using the builder [`GenerateKey`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use sop::*; use std::io::Cursor;
    /// # fn sop_examples<'s, S: SOP<'s> + 's>(sop: &'s S) -> Result<()> {
    /// let alice_sec = sop.generate_key()?
    ///     .userid("Alice Lovelace <alice@openpgp.example>")
    ///     .generate()?;
    /// # Ok(()) }
    /// ```
    fn generate_key(&'s self)
                    -> Result<Box<dyn GenerateKey<Self, Self::Keys> + 's>>;

    /// Updates a key's password.
    ///
    /// Customize the operation using the builder [`ChangeKeyPassword`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use sop::*; use std::io::Cursor; use std::fs::File;
    /// # fn sop_examples<'s, S, Certs, Keys>(sop: &'s S) -> Result<()>
    /// # where
    /// #     S: SOP<'s, Certs = Certs, Keys = Keys>,
    /// #     Certs: Load<'s, S> + Save,
    /// #     Keys: Load<'s, S> + Save,
    /// # {
    /// let alice_secret =
    ///     Keys::from_reader(sop, &mut File::open("alice.secret")?)?;
    ///
    /// let alice_updated_secret = sop.change_key_password()?
    ///     .old_key_password(Password::new_unchecked(b"hunter2".to_vec()))?
    ///     .new_key_password(Password::new(b"jaeger2".to_vec())?)?
    ///     .keys(&alice_secret)?;
    /// # Ok(()) }
    /// ```
    fn change_key_password(&'s self)
                           -> Result<Box<dyn ChangeKeyPassword<Self, Self::Keys> + 's>>;

    /// Creates a Revocation Certificate.
    ///
    /// Customize the operation using the builder [`RevokeKey`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use sop::*; use std::io::Cursor; use std::fs::File;
    /// # fn sop_examples<'s, S, Certs, Keys>(sop: &'s S) -> Result<()>
    /// # where
    /// #     S: SOP<'s, Certs = Certs, Keys = Keys>,
    /// #     Certs: Load<'s, S> + Save,
    /// #     Keys: Load<'s, S> + Save,
    /// # {
    /// let alice_secret =
    ///     Keys::from_reader(sop, &mut File::open("alice.secret")?)?;
    ///
    /// let alice_revoked = sop.revoke_key()?
    ///     .with_key_password(Password::new_unchecked(b"hunter2".to_vec()))?
    ///     .keys(&alice_secret)?;
    /// # Ok(()) }
    /// ```
    fn revoke_key(&'s self)
                  -> Result<Box<dyn RevokeKey<Self, Self::Certs, Self::Keys> + 's>>;

    /// Extracts a Certificate from a Secret Key.
    ///
    /// Customize the operation using the builder [`ExtractCert`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use sop::*; use std::io::Cursor; use std::fs::File;
    /// # fn sop_examples<'s, S, Certs, Keys>(sop: &'s S) -> Result<()>
    /// # where
    /// #     S: SOP<'s, Certs = Certs, Keys = Keys>,
    /// #     Certs: Load<'s, S> + Save,
    /// #     Keys: Load<'s, S> + Save,
    /// # {
    /// let alice_secret =
    ///     Keys::from_reader(sop, &mut File::open("alice.secret")?)?;
    ///
    /// let alice_public = sop.extract_cert()?
    ///     .keys(&alice_secret)?;
    /// # Ok(()) }
    /// ```
    fn extract_cert(&'s self) -> Result<Box<
            dyn ExtractCert<Self, Self::Certs, Self::Keys> + 's>>;

    /// Creates Detached Signatures.
    ///
    /// Customize the operation using the builder [`Sign`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use sop::*; use std::io::Cursor; use std::fs::File;
    /// # fn sop_examples<'s, S, Certs, Keys>(sop: &'s S) -> Result<()>
    /// # where
    /// #     S: SOP<'s, Certs = Certs, Keys = Keys>,
    /// #     Certs: Load<'s, S> + Save,
    /// #     Keys: Load<'s, S> + Save,
    /// # {
    /// let alice_secret =
    ///     Keys::from_reader(sop, &mut File::open("alice.secret")?)?;
    ///
    /// let (_micalg, sig) = sop.sign()?
    ///     .keys(&alice_secret)?
    ///     .data(&mut Cursor::new(&b"Hello World :)"))?;
    /// # Ok(()) }
    /// ```
    fn sign(&'s self)
            -> Result<Box<dyn Sign<Self, Self::Keys, Self::Sigs> + 's>>;

    /// Verifies Detached Signatures.
    ///
    /// Customize the operation using the builder [`Verify`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use sop::*; use std::io::Cursor; use std::fs::File;
    /// # fn sop_examples<'s, S, Certs, Keys, Sigs>(sop: &'s S) -> Result<()>
    /// # where
    /// #     S: SOP<'s, Certs = Certs, Keys = Keys, Sigs = Sigs>,
    /// #     Certs: Load<'s, S> + Save,
    /// #     Keys: Load<'s, S> + Save,
    /// #     Sigs: Load<'s, S> + Save,
    /// # {
    /// let alice_public =
    ///     Certs::from_reader(sop, &mut File::open("alice.public")?)?;
    /// let sig =
    ///     Sigs::from_reader(sop, &mut File::open("data.asc")?)?;
    ///
    /// let verifications = sop.verify()?
    ///     .certs(&alice_public)?
    ///     .signatures(&sig)?
    ///     .data(&mut Cursor::new(&b"Hello World :)"))?;
    /// let valid_signatures = ! verifications.is_empty();
    /// # Ok(()) }
    /// ```
    fn verify(&'s self)
              -> Result<Box<dyn Verify<Self, Self::Certs, Self::Sigs> + 's>>;

    /// Encrypts a Message.
    ///
    /// Customize the operation using the builder [`Encrypt`].
    ///
    /// # Examples
    ///
    /// Encrypts a message for Bob, and signs it using Alice's key.
    ///
    /// ```rust
    /// # use sop::*; use std::io::Cursor; use std::fs::File;
    /// # fn sop_examples<'s, S, Certs, Keys>(sop: &'s S) -> Result<()>
    /// # where
    /// #     S: SOP<'s, Certs = Certs, Keys = Keys>,
    /// #     Certs: Load<'s, S> + Save,
    /// #     Keys: Load<'s, S> + Save,
    /// # {
    /// let alice_secret =
    ///     Keys::from_reader(sop, &mut File::open("alice.secret")?)?;
    /// let bob_public =
    ///     Certs::from_reader(sop, &mut File::open("bob.public")?)?;
    ///
    /// let (_session_key, ciphertext) = sop.encrypt()?
    ///     .sign_with_keys(&alice_secret)?
    ///     .with_certs(&bob_public)?
    ///     .plaintext(&mut Cursor::new(&b"Hello World :)"))?
    ///     .to_vec()?;
    /// # Ok(()) }
    /// ```
    fn encrypt(&'s self)
               -> Result<Box<dyn Encrypt<Self, Self::Certs, Self::Keys> + 's>>;

    /// Decrypts a Message.
    ///
    /// Customize the operation using the builder [`Decrypt`].
    ///
    /// # Examples
    ///
    /// Decrypts a message encrypted for Bob, and verifies Alice's
    /// signature on it.
    ///
    /// ```rust
    /// # use sop::*; use std::io::Cursor; use std::fs::File;
    /// # fn sop_examples<'s, S, Certs, Keys>(sop: &'s S) -> Result<()>
    /// # where
    /// #     S: SOP<'s, Certs = Certs, Keys = Keys>,
    /// #     Certs: Load<'s, S> + Save,
    /// #     Keys: Load<'s, S> + Save,
    /// # {
    /// let alice_public =
    ///     Certs::from_reader(sop, &mut File::open("alice.public")?)?;
    /// let bob_secret =
    ///     Keys::from_reader(sop, &mut File::open("bob.secret")?)?;
    ///
    /// let ((_session_key, verifications), plaintext) = sop.decrypt()?
    ///     .verify_with_certs(&alice_public)?
    ///     .with_keys(&bob_secret)?
    ///     .ciphertext(&mut File::open("ciphertext.pgp")?)?
    ///     .to_vec()?;
    /// let valid_signatures = ! verifications.is_empty();
    /// # Ok(()) }
    /// ```
    fn decrypt(&'s self)
               -> Result<Box<dyn Decrypt<Self, Self::Certs, Self::Keys> + 's>>;

    /// Converts binary OpenPGP data to ASCII.
    ///
    /// By default, SOP operations emit ASCII-Armored data.  But,
    /// occasionally it can be useful to explicitly armor data.
    ///
    /// Customize the operation using the builder [`Armor`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use sop::*; use std::io::Cursor; use std::fs::File;
    /// # fn sop_examples<'s, S: SOP<'s> + 's>(sop: &'s S) -> Result<()> {
    /// let (_, alice_secret_asc) = sop.armor()?
    ///     .data(&mut File::open("alice.secret.bin")?)?
    ///     .to_vec()?;
    /// assert!(alice_secret_asc.starts_with(b"-----BEGIN PGP PRIVATE KEY BLOCK-----"));
    /// # Ok(()) }
    /// ```
    fn armor(&'s self) -> Result<Box<dyn Armor + 's>>;

    /// Converts ASCII OpenPGP data to binary.
    ///
    /// By default, SOP operations emit ASCII-Armored data, but this
    /// behavior can be changed at export time.  Nevertheless,
    /// occasionally it can be useful to explicitly dearmor data.
    ///
    /// Customize the operation using the builder [`Dearmor`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use sop::*; use std::io::Cursor; use std::fs::File;
    /// # fn sop_examples<'s, S: SOP<'s> + 's>(sop: &'s S) -> Result<()> {
    /// let (_, alice_secret_bin) = sop.dearmor()?
    ///     .data(&mut File::open("alice.secret.asc")?)?
    ///     .to_vec()?;
    /// assert!(! alice_secret_bin.starts_with(b"-----BEGIN PGP PRIVATE KEY BLOCK-----"));
    /// # Ok(()) }
    /// ```
    fn dearmor(&'s self) -> Result<Box<dyn Dearmor + 's>>;

    /// Splits Signatures from an Inline-Signed Message.
    ///
    /// Note: The signatures are not verified, this merely transforms
    /// an inline-signed message into a detached signature, which in
    /// turn can be verified using [`SOP::verify`].
    ///
    /// Customize the operation using the builder [`InlineDetach`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use sop::*; use std::io::Cursor; use std::fs::File;
    /// # fn sop_examples<'s, S: SOP<'s> + 's>(sop: &'s S) -> Result<()> {
    /// let (signatures, data) = sop.inline_detach()?
    ///     .message(&mut File::open("inline-signed.pgp")?)?
    ///     .to_vec()?;
    /// # Ok(()) }
    /// ```
    fn inline_detach(&'s self)
                     -> Result<Box<dyn InlineDetach<Self::Sigs> + 's>>;

    /// Verifies an Inline-Signed Message.
    ///
    /// Customize the operation using the builder [`InlineVerify`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use sop::*; use std::io::Cursor; use std::fs::File;
    /// # fn sop_examples<'s, S, Certs, Keys>(sop: &'s S) -> Result<()>
    /// # where
    /// #     S: SOP<'s, Certs = Certs, Keys = Keys>,
    /// #     Certs: Load<'s, S> + Save,
    /// #     Keys: Load<'s, S> + Save,
    /// # {
    /// let alice_public =
    ///     Certs::from_reader(sop, &mut File::open("alice.public")?)?;
    ///
    /// let (verifications, data) = sop.inline_verify()?
    ///     .certs(&alice_public)?
    ///     .message(&mut File::open("inline-signed.pgp")?)?
    ///     .to_vec()?;
    /// let valid_signatures = ! verifications.is_empty();
    /// # Ok(()) }
    /// ```
    fn inline_verify(&'s self)
                     -> Result<Box<dyn InlineVerify<Self, Self::Certs> + 's>>;

    /// Creates an Inline-Signed Message.
    ///
    /// Customize the operation using the builder [`InlineSign`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use sop::*; use std::io::Cursor; use std::fs::File;
    /// # fn sop_examples<'s, S, Certs, Keys>(sop: &'s S) -> Result<()>
    /// # where
    /// #     S: SOP<'s, Certs = Certs, Keys = Keys>,
    /// #     Certs: Load<'s, S> + Save,
    /// #     Keys: Load<'s, S> + Save,
    /// # {
    /// let alice_secret =
    ///     Keys::from_reader(sop, &mut File::open("alice.secret")?)?;
    ///
    /// let (inline_signed_asc) = sop.inline_sign()?
    ///     .keys(&alice_secret)?
    ///     .data(&mut Cursor::new(&b"Hello World :)"))?
    ///     .to_vec()?;
    /// # Ok(()) }
    /// ```
    fn inline_sign(&'s self)
                   -> Result<Box<dyn InlineSign<Self, Self::Keys> + 's>>;
}

/// A password.
///
/// See [Passwords are Human-Readable] in the SOP spec.
///
///   [Passwords are Human-Readable]: https://www.ietf.org/archive/id/draft-dkg-openpgp-stateless-cli-08.html#name-passwords-are-human-readabl
pub struct Password(Box<[u8]>);

impl Password {
    /// Returns a `Password` that is guaranteed to be human-readable.
    ///
    /// Use this function when you get a password from the user to
    /// generate an artifact with (see [Generating Material with
    /// Human-Readable Passwords]).
    ///
    /// [Generating Material with Human-Readable Passwords]: https://www.ietf.org/archive/id/draft-dkg-openpgp-stateless-cli-08.html#name-generating-material-with-hu
    pub fn new(password: Vec<u8>) -> Result<Password> {
        // Securely erase the password.
        fn securely_erase(mut p: Vec<u8>) {
            unsafe {
                memsec::memzero(p.as_mut_ptr(), p.len());
            }
        }

        let mut s = String::from_utf8(password)
            .map_err(|e| {
                securely_erase(e.into_bytes());
                Error::PasswordNotHumanReadable
            })?;

        // Check for leading whitespace.
        if s.trim_start().len() != s.len() {
            securely_erase(s.into_bytes());
            return Err(Error::PasswordNotHumanReadable);
        }

        // Trim trailing whitespace.
        s.truncate(s.trim_end().len());

        // Check for odd whitespace.
        if s.chars().any(|c| c.is_whitespace() && c != ' ') {
            securely_erase(s.into_bytes());
            return Err(Error::PasswordNotHumanReadable);
        }

        // XXX: Check that the password is in Unicode Normal Form C,
        // but I don't think that is possible with Rust's stdlib.

        Ok(Password(s.into_bytes().into()))
    }

    /// Returns a `Password` without further checking.
    ///
    /// Use this function when you get a password from the user that
    /// is used when consuming an artifact (see [Consuming
    /// Password-protected Material]).
    ///
    /// [Consuming Password-protected Material]: https://www.ietf.org/archive/id/draft-dkg-openpgp-stateless-cli-08.html#name-consuming-password-protecte
    pub fn new_unchecked(password: Vec<u8>) -> Password {
        Password(password.into())
    }
}

impl plumbing::PasswordsAreHumanReadable for Password {
    fn normalized(&self) -> &[u8] {
        // First, let's hope it is UTF-8.
        if let Ok(p) = std::str::from_utf8(&self.0) {
            p.trim_end().as_bytes()
        } else {
            // As a best effort for now, drop ASCII-whitespace from
            // the end.
            let mut p = &self.0[..];
            while ! p.is_empty() && p[p.len() - 1].is_ascii_whitespace() {
                p = &p[..p.len() - 1];
            }
            p
        }
    }

    fn variants(&self) -> Box<dyn Iterator<Item = &[u8]> + '_> {
        Box::new(std::iter::once(self.normalized())
            .filter_map(move |normalized| {
                if normalized.len() < self.0.len() {
                    Some(normalized)
                } else {
                    None
                }
            })
            .chain(std::iter::once(&self.0[..])))
    }
}

impl Drop for Password {
    fn drop(&mut self) {
        unsafe {
            memsec::memzero(self.0.as_mut_ptr(), self.0.len());
        }
    }
}

/// A session key.
pub struct SessionKey {
    algorithm: u8,
    key: Box<[u8]>,
}

impl SessionKey {
    /// Creates a new session key object.
    pub fn new<A, K>(algorithm: A, key: K) -> Result<Self>
    where A: Into<u8>,
          K: AsRef<[u8]>,
    {
        // XXX: Maybe sanity check key lengths.
        Ok(SessionKey {
            algorithm: algorithm.into(),
            key: key.as_ref().to_vec().into(),
        })
    }

    /// Returns the symmetric algorithm octet.
    pub fn algorithm(&self) -> u8 {
        self.algorithm
    }

    /// Returns the session key.
    pub fn key(&self) -> &[u8] {
        &self.key
    }
}

impl fmt::Display for SessionKey {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}:", self.algorithm)?;
        for b in &self.key[..] {
            write!(f, "{:02X}", b)?
        }
        Ok(())
    }
}

impl Drop for SessionKey {
    fn drop(&mut self) {
        unsafe {
            memsec::memzero(self.key.as_mut_ptr(), self.key.len());
        }
    }
}

impl std::str::FromStr for SessionKey {
    type Err = ParseError;
    fn from_str(sk: &str) -> ParseResult<Self> {
        // The SOP format is:
        //
        //   <decimal-cipher-octet> ":" <hex-session-key>
        //
        // We most likely will change the first field, so we split
        // from the end of the string using `rsplit`, which puts the
        // last segment first.  This is rather unexpected.  Reverse
        // it.
        let fields = sk.rsplit(':').rev().collect::<Vec<_>>();

        if fields.len() != 2 {
            return Err(ParseError(format!(
                "Expected two colon-separated fields, got {:?}",
                fields)));
        }

        let algo: u8 = fields[0].parse().map_err(
            |e| ParseError(format!("Failed to parse algorithm: {}", e)))?;
        let sk = from_hex(&fields[1], true)?;
        Self::new(algo, sk).map_err(
            |e| ParseError(format!("Bad session key: {}", e)))
    }
}

/// A helpful function for converting a hexadecimal string to binary.
/// This function skips whitespace if `pretty` is set.
fn from_hex(hex: &str, pretty: bool) -> ParseResult<Vec<u8>> {
    const BAD: u8 = 255u8;
    const X: u8 = 'x' as u8;

    let mut nibbles = hex.chars().filter_map(|x| {
        match x {
            '0' => Some(0u8),
            '1' => Some(1u8),
            '2' => Some(2u8),
            '3' => Some(3u8),
            '4' => Some(4u8),
            '5' => Some(5u8),
            '6' => Some(6u8),
            '7' => Some(7u8),
            '8' => Some(8u8),
            '9' => Some(9u8),
            'a' | 'A' => Some(10u8),
            'b' | 'B' => Some(11u8),
            'c' | 'C' => Some(12u8),
            'd' | 'D' => Some(13u8),
            'e' | 'E' => Some(14u8),
            'f' | 'F' => Some(15u8),
            'x' | 'X' if pretty => Some(X),
            _ if pretty && x.is_whitespace() => None,
            _ => Some(BAD),
        }
    }).collect::<Vec<u8>>();

    if pretty && nibbles.len() >= 2 && nibbles[0] == 0 && nibbles[1] == X {
        // Drop '0x' prefix.
        nibbles.remove(0);
        nibbles.remove(0);
    }

    if nibbles.iter().any(|&b| b == BAD || b == X) {
        // Not a hex character.
        return Err(ParseError("Invalid characters".into()));
    }

    // We need an even number of nibbles.
    if nibbles.len() % 2 != 0 {
        return Err(ParseError("Odd number of nibbles".into()));
    }

    let bytes = nibbles.chunks(2).map(|nibbles| {
        (nibbles[0] << 4) | nibbles[1]
    }).collect::<Vec<u8>>();

    Ok(bytes)
}

/// Result specialization.
pub type Result<T> = std::result::Result<T, Error>;

/// Convenience alias.
type ParseResult<T> = std::result::Result<T, ParseError>;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn session_key_roundtrip() -> Result<()> {
        for algo in &[9, 13] {
            let sk = SessionKey::new(
                *algo,
                &[0xE1, 0x48, 0x97, 0x81, 0xAA, 0x22, 0xE1, 0xBF,
                  0x6E, 0x3E, 0x61, 0x74, 0x8C, 0x8D, 0x3F, 0x35,
                  0x50, 0x7C, 0x80, 0x9E, 0x95, 0x64, 0x86, 0x87,
                  0xC7, 0xE4, 0xB9, 0xAF, 0x86, 0x17, 0xD3, 0xAE])?;
            let sk_s = sk.to_string();
            let sk_p: SessionKey = sk_s.parse().unwrap();
            assert_eq!(sk.algorithm(), sk_p.algorithm());
            assert_eq!(sk.key(), sk_p.key());
        }
        Ok(())
    }

    #[test]
    fn sign_as_roundtrip() -> Result<()> {
        use SignAs::*;
        for a in &[Text, Binary] {
            let s = a.to_string();
            let b: SignAs = s.parse().unwrap();
            assert_eq!(a, &b);
        }
        Ok(())
    }

    #[test]
    fn encrypt_as_roundtrip() -> Result<()> {
        use EncryptAs::*;
        for a in &[Text, Binary] {
            let s = a.to_string();
            let b: EncryptAs = s.parse().unwrap();
            assert_eq!(a, &b);
        }
        Ok(())
    }

    #[test]
    fn armor_label_roundtrip() -> Result<()> {
        use ArmorLabel::*;
        for a in &[Auto, Sig, Key, Cert, Message] {
            let s = a.to_string();
            let b: ArmorLabel = s.parse().unwrap();
            assert_eq!(a, &b);
        }
        Ok(())
    }
}