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
//! This library is designed to integrate non-PGP generated and verified keys
//! and signatures with channels that expect PGP data. It specifically only
//! supports the ed25519 signature scheme.
//!
//! Sometimes you want to be able to sign data, and the only reasonable channel
//! to transmit signatures and public keys available to you expects them to be
//! PGP formatted. If you don't want to use a heavyweight dependency like gpg,
//! this library supports only the minimal necessary components of the PGP
//! format to transmit your keys and signatures.
#![deny(missing_docs, missing_debug_implementations)]

extern crate base64;
extern crate byteorder;
extern crate digest;
extern crate sha1;
extern crate typenum;

#[macro_use] extern crate failure;
#[macro_use] extern crate bitflags;

#[cfg(feature = "dalek")]
extern crate ed25519_dalek as dalek;

mod ascii_armor;
mod packet;

mod key;
mod sig;

pub use key::PgpKey;
pub use sig::{PgpSig, SubPacket, SigType};

/// An OpenPGP public key fingerprint.
pub type Fingerprint = [u8; 20];
/// An ed25519 signature.
pub type Signature = [u8; 64];

bitflags! {
    /// The key flags assigned to this key.
    pub struct KeyFlags: u8 {
        /// No key flags.
        const NONE              = 0x00;
        /// The Certify flag.
        const CERTIFY           = 0x01;
        /// The Sign flag.
        const SIGN              = 0x02;
        /// The Encrypt Communication flag.
        const ENCRYPT_COMS      = 0x04;
        /// The Encrypt Storage flag.
        const ENCRYPT_STORAGE   = 0x08;
        /// The Authentication flag.
        const AUTHENTICATION    = 0x20;
    }
}

/// An error returned while attempting to parse a PGP signature or public key.
#[derive(Fail, Debug)]
pub enum PgpError {
    /// Invalid ASCII armor format
    #[fail(display = "Invalid ASCII armor format")]
    InvalidAsciiArmor,
    /// Packet header incorrectly formatted
    #[fail(display = "Packet header incorrectly formatted")]
    InvalidPacketHeader,
    /// Unsupported packet length format
    #[fail(display = "Unsupported packet length format")]
    UnsupportedPacketLength,
    /// Unsupported form of signature packet
    #[fail(display = "Unsupported form of signature packet")]
    UnsupportedSignaturePacket,
    /// First hashed subpacket of signature must be the key fingerprint
    #[fail(display = "First hashed subpacket of signature must be the key fingerprint")]
    MissingFingerprintSubpacket,
    /// Unsupported form of public key packet
    #[fail(display = "Unsupported form of public key packet")]
    UnsupportedPublicKeyPacket,
}

// Helper for writing base64 data
struct Base64<'a>(&'a [u8]);

impl<'a> std::fmt::Debug for Base64<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str(&base64::encode(self.0))
    }
}