summaryrefslogtreecommitdiffstats
path: root/src/keys.rs
blob: cb21bb487acf0e9f32690cfb652dfe49510cd8ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use super::openpgp;
use super::openpgp::Packet;

/// This represents a transferable public key (see [RFC 4880, section
/// 11.1](https://tools.ietf.org/html/rfc4880#section-11.1)).
#[derive(Debug)]
pub struct TPK {
    userids: Vec<UserIDBinding>,
    subkeys: Vec<SubkeyBinding>,
}

#[derive(Debug)]
pub struct SubkeyBinding {
    subkey: openpgp::Key,
    signatures: Vec<openpgp::Signature>,
}

#[derive(Debug)]
pub struct UserIDBinding {
    userid: openpgp::UserID,
    signatures: Vec<openpgp::Signature>,
}

#[derive(Debug)]
enum States {
    Start,
    TPK,
    UserID(UserIDBinding),
    Subkey(SubkeyBinding),
    End,
}

impl TPK {
    /// Return the first transferable public key found in `m`.
    pub fn from_message(m: openpgp::Message) -> Option<Self> {
        let mut state = States::Start;
        let mut tpk = TPK { userids: vec![], subkeys: vec![] };
        for p in m.into_iter() {
            state = match state {
                States::Start => {
                    /* Find the first public key packet.  */
                    match p {
                        Packet::PublicKey(pk) => {
                            tpk.subkeys.push(SubkeyBinding{subkey: pk, signatures: vec![]});
                            States::TPK
                        },
                        _ => States::Start,
                    }
                },
                States::TPK => {
                    /* Find user id, or subkey packets.  */
                    match p {
                        Packet::PublicKey(_pk) => {
                            States::End
                        },
                        Packet::UserID(uid) => {
                            States::UserID(UserIDBinding{userid: uid, signatures: vec![]})
                        },
                        Packet::PublicSubkey(key) => {
                            States::Subkey(SubkeyBinding{subkey: key, signatures: vec![]})
                        },
                        _ => States::TPK,
                    }
                },
                States::UserID(mut u) => {
                    /* Find signature packets.  */
                    match p {
                        Packet::PublicKey(_pk) => {
                            States::End
                        },
                        Packet::UserID(uid) => {
                            tpk.userids.push(u);
                            States::UserID(UserIDBinding{userid: uid, signatures: vec![]})
                        },
                        Packet::PublicSubkey(key) => {
                            tpk.userids.push(u);
                            States::Subkey(SubkeyBinding{subkey: key, signatures: vec![]})
                        },
                        Packet::Signature(sig) => {
                            u.signatures.push(sig);
                            States::UserID(u)
                        },
                        _ => States::UserID(u),
                    }
                },
                States::Subkey(mut s) => {
                    /* Find signature packets.  */
                    match p {
                        Packet::PublicKey(_pk) => {
                            States::End
                        },
                        Packet::UserID(uid) => {
                            tpk.subkeys.push(s);
                            States::UserID(UserIDBinding{userid: uid, signatures: vec![]})
                        },
                        Packet::PublicSubkey(key) => {
                            tpk.subkeys.push(s);
                            States::Subkey(SubkeyBinding{subkey: key, signatures: vec![]})
                        },
                        Packet::Signature(sig) => {
                            s.signatures.push(sig);
                            States::Subkey(s)
                        },
                        _ => States::Subkey(s),
                    }
                },
                States::End => break,
            };
        }

        match state {
            States::UserID(u) => {
                tpk.userids.push(u);
                Some(tpk)
            },
            States::Subkey(s) => {
                tpk.subkeys.push(s);
                Some(tpk)
            },
            States::End => Some(tpk),
            _ => None,
        }.and_then(|tpk| tpk.canonicalize())
    }

    fn canonicalize(self) -> Option<Self> {
        // Sanity checks.

        // - One or more User ID packets.
        if self.userids.len() == 0 {
            return None;
        }

        // - After each Subkey packet, one Signature packet.
        for subkey in self.subkeys.iter().skip(1) {
            if subkey.signatures.len() == 0 {
                return None;
            }
        }

        // XXX Do some canonicalization.

        Some(self)
    }
}

#[cfg(test)]
mod test {
    use super::TPK;
    use super::openpgp;

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

    #[test]
    fn broken() {
        let m = openpgp::Message::from_bytes(bytes!("testy-broken-no-pk.pgp")).unwrap();
        let tpk = TPK::from_message(m);
        assert!(tpk.is_none());

        let m = openpgp::Message::from_bytes(bytes!("testy-broken-no-uid.pgp")).unwrap();
        let tpk = TPK::from_message(m);
        assert!(tpk.is_none());

        let m = openpgp::Message::from_bytes(bytes!("testy-broken-no-sig-on-subkey.pgp")).unwrap();
        let tpk = TPK::from_message(m);
        assert!(tpk.is_none());
    }

    #[test]
    fn basics() {
        let m = openpgp::Message::from_bytes(bytes!("testy.pgp")).unwrap();
        let tpk = TPK::from_message(m).unwrap();
        //println!("{:?}", tpk);

        assert_eq!(tpk.userids.len(), 1, "number of userids");
        // XXX .value is private
        //assert_eq!(tpk.userids[0].userid.value, "Testy McTestface <testy@example.org>");
        assert_eq!(tpk.subkeys.len(), 2, "number of subkeys");

        let m = openpgp::Message::from_bytes(bytes!("testy-no-subkey.pgp")).unwrap();
        let tpk = TPK::from_message(m).unwrap();

        assert_eq!(tpk.userids.len(), 1, "number of userids");
        assert_eq!(tpk.subkeys.len(), 1, "number of subkeys");
    }
}