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
use std::io::{BufRead, Read};

use buffer_redux::BufReader;

use crate::{
    armor::{self, BlockType, Dearmor},
    cleartext::CleartextSignedMessage,
    errors::Result,
    Deserializable, Message, SignedPublicKey, SignedSecretKey, StandaloneSignature,
};

/// A flexible representation of what can be represented in an armor file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Any {
    Cleartext(CleartextSignedMessage),
    PublicKey(SignedPublicKey),
    SecretKey(SignedSecretKey),
    Message(Message),
    Signature(StandaloneSignature),
}

impl Any {
    /// Parse armored ascii data.
    pub fn from_armor(bytes: impl Read) -> Result<(Self, armor::Headers)> {
        Self::from_armor_buf(BufReader::new(bytes))
    }

    /// Parse a single armor encoded composition.
    pub fn from_string(input: &str) -> Result<(Self, armor::Headers)> {
        Self::from_armor_buf(input.as_bytes())
    }

    /// Parse armored ascii data.
    pub fn from_armor_buf<R: BufRead>(input: R) -> Result<(Self, armor::Headers)> {
        let dearmor = armor::Dearmor::new(input);
        let (typ, headers, has_leading_data, rest) = dearmor.read_only_header()?;

        match typ {
            // Standard PGP types
            BlockType::PublicKey => {
                let dearmor = Dearmor::after_header(typ, headers.clone(), rest);
                let key = SignedPublicKey::from_bytes(dearmor)?;
                Ok((Self::PublicKey(key), headers))
            }
            BlockType::PrivateKey => {
                let dearmor = Dearmor::after_header(typ, headers.clone(), rest);
                let key = SignedSecretKey::from_bytes(dearmor)?;
                Ok((Self::SecretKey(key), headers))
            }
            BlockType::Message => {
                let dearmor = Dearmor::after_header(typ, headers.clone(), rest);
                let msg = Message::from_bytes(dearmor)?;
                Ok((Self::Message(msg), headers))
            }
            BlockType::Signature => {
                let dearmor = Dearmor::after_header(typ, headers.clone(), rest);
                let sig = StandaloneSignature::from_bytes(dearmor)?;
                Ok((Self::Signature(sig), headers))
            }
            BlockType::CleartextMessage => {
                ensure!(
                    !has_leading_data,
                    "must not have leading data for a cleartext message"
                );
                let (sig, headers) =
                    CleartextSignedMessage::from_armor_after_header(rest, headers)?;
                Ok((Self::Cleartext(sig), headers))
            }
            _ => unimplemented_err!("unsupported block type: {}", typ),
        }
    }
}