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
use std::io::prelude::*;
use crate::errors::*;

pub mod armor;
pub mod errors;
mod encoding;
pub mod packet;
pub use packet::Tag;
pub mod pubkey;
pub mod signature;
pub use signature::Signature;


pub struct Parser<R: Read> {
    r: R,
    max_alloc: Option<usize>,
}

impl<R: Read> Parser<R> {
    pub fn new(r: R) -> Parser<R> {
        Parser {
            r,
            max_alloc: None,
        }
    }

    pub fn with_max_alloc(r: R, max_alloc: usize) -> Parser<R> {
        Parser {
            r,
            max_alloc: Some(max_alloc),
        }
    }

    pub fn inner(&self) -> &R {
        &self.r
    }
}

impl<R: Read> Iterator for Parser<R> {
    type Item = (Tag, Vec<u8>);

    fn next(&mut self) -> Option<Self::Item> {
        let mut packet_body = Vec::new();
        let tag = packet::read(&mut self.r, &mut packet_body, &self.max_alloc);
        debug!("Received tag: {:?}", tag);

        match tag {
            Ok(tag) => Some((tag, packet_body)),
            _ => None,
        }
    }
}


#[cfg(test)]
mod tests {
    use armor::read_armored;
    use std::io::BufReader;
    use super::*;

    #[test]
    fn extract_userid_from_pubkey() {
        let key = include_bytes!("../data/hans_acker.asc");
        let key = read_armored(&mut BufReader::new(&key[..])).expect("read_armored");

        for (tag, body) in Parser::new(key.as_slice()) {
            println!("{:?}: {:?}", tag, body);
            if tag == Tag::UserID {
                let body = String::from_utf8(body).expect("invalid utf8");
                println!("UserID: {:?}", body);
                assert_eq!("Hans Acker (example comment) <hans.acker@example.com>", body);
                return;
            }
        }

        unreachable!("UserID wasn't found");
    }

    #[test]
    fn extract_userid_from_pubkey_freebsd() {
        let key = include_bytes!("../data/freebsd.asc");
        let key = read_armored(&mut BufReader::new(&key[..])).expect("read_armored");

        for (tag, body) in Parser::new(key.as_slice()) {
            // println!("{:?}: {:?}", tag, body);
            if tag == Tag::UserID {
                let body = String::from_utf8(body).expect("invalid utf8");
                println!("UserID: {:?}", body);
                assert_eq!("FreeBSD Security Officer <security-officer@FreeBSD.org>", body);
                return;
            }
        }

        unreachable!("UserID wasn't found");
    }

    #[test]
    fn extract_userid_from_pubkey_freebsd_with_alloc_limit() {
        let key = include_bytes!("../data/freebsd.asc");
        let key = read_armored(&mut BufReader::new(&key[..])).expect("read_armored");

        for (tag, body) in Parser::with_max_alloc(key.as_slice(), 1024 * 1024) {
            // println!("{:?}: {:?}", tag, body);
            if tag == Tag::UserID {
                let body = String::from_utf8(body).expect("invalid utf8");
                println!("UserID: {:?}", body);
                assert_eq!("FreeBSD Security Officer <security-officer@FreeBSD.org>", body);
                return;
            }
        }

        unreachable!("UserID wasn't found");
    }

    #[test]
    fn extract_userid_from_pubkey_freebsd_with_tiny_alloc_limit() {
        let key = include_bytes!("../data/freebsd.asc");
        let key = read_armored(&mut BufReader::new(&key[..])).expect("read_armored");

        for (_tag, _body) in Parser::with_max_alloc(key.as_slice(), 3) {
            unreachable!("max alloc didn't work");
        }
    }
}