diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-05-31 17:23:57 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-06-01 12:55:08 +0200 |
commit | ccb2f52269f1673f8e7f9e1643dac9bb488524f6 (patch) | |
tree | ce408149d77cd934c413501fbdbc3ec8efbf30be /openpgp/src/parse | |
parent | c1c5d8a9bfdb04a054ac87a9e4419764553106a2 (diff) |
openpgp: Expose functions to parse MPIs.
Diffstat (limited to 'openpgp/src/parse')
-rw-r--r-- | openpgp/src/parse/mpis.rs | 62 | ||||
-rw-r--r-- | openpgp/src/parse/parse.rs | 8 |
2 files changed, 28 insertions, 42 deletions
diff --git a/openpgp/src/parse/mpis.rs b/openpgp/src/parse/mpis.rs index 76164be0..49c7e5f4 100644 --- a/openpgp/src/parse/mpis.rs +++ b/openpgp/src/parse/mpis.rs @@ -21,8 +21,7 @@ impl mpis::PublicKey { /// See [Section 3.2 of RFC 4880] for details. /// /// [Section 3.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-3.2 - #[cfg(test)] - pub fn parse_naked<T: AsRef<[u8]>>( + pub fn parse<T: AsRef<[u8]>>( algo: PublicKeyAlgorithm, buf: T) -> Result<Self> { @@ -32,7 +31,7 @@ impl mpis::PublicKey { let bio = buffered_reader::Generic::with_cookie( cur, None, Cookie::default()); let mut php = PacketHeaderParser::new_naked(Box::new(bio)); - Self::parse(algo, &mut php) + Self::_parse(algo, &mut php) } /// Parses a set of OpenPGP MPIs representing a public key. @@ -40,7 +39,7 @@ impl mpis::PublicKey { /// See [Section 3.2 of RFC 4880] for details. /// /// [Section 3.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-3.2 - pub(crate) fn parse<'a>(algo: PublicKeyAlgorithm, + pub(crate) fn _parse<'a>(algo: PublicKeyAlgorithm, php: &mut PacketHeaderParser<'a>) -> Result<Self> { @@ -160,7 +159,7 @@ impl mpis::SecretKey { let bio = buffered_reader::Generic::with_cookie( cur, None, Cookie::default()); let mut php = PacketHeaderParser::new_naked(Box::new(bio)); - let mpis = Self::parse(algo, &mut php)?; + let mpis = Self::_parse(algo, &mut php)?; // read expected sha1 hash of the mpis let their_chksum = php.parse_bytes("checksum", 20)?; @@ -187,9 +186,8 @@ impl mpis::SecretKey { /// See [Section 3.2 of RFC 4880] for details. /// /// [Section 3.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-3.2 - #[cfg(test)] - pub fn parse_naked<T: AsRef<[u8]>>(algo: PublicKeyAlgorithm, buf: T) - -> Result<Self> + pub fn parse<T: AsRef<[u8]>>(algo: PublicKeyAlgorithm, buf: T) + -> Result<Self> { use std::io::Cursor; @@ -197,7 +195,7 @@ impl mpis::SecretKey { let bio = buffered_reader::Generic::with_cookie( cur, None, Cookie::default()); let mut php = PacketHeaderParser::new_naked(Box::new(bio)); - Self::parse(algo, &mut php) + Self::_parse(algo, &mut php) } /// Parses a set of OpenPGP MPIs representing a secret key. @@ -205,9 +203,9 @@ impl mpis::SecretKey { /// See [Section 3.2 of RFC 4880] for details. /// /// [Section 3.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-3.2 - pub(crate) fn parse<'a>(algo: PublicKeyAlgorithm, - php: &mut PacketHeaderParser<'a>) - -> Result<Self> + pub(crate) fn _parse<'a>(algo: PublicKeyAlgorithm, + php: &mut PacketHeaderParser<'a>) + -> Result<Self> { use PublicKeyAlgorithm::*; @@ -286,16 +284,15 @@ impl mpis::Ciphertext { /// See [Section 3.2 of RFC 4880] for details. /// /// [Section 3.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-3.2 - #[cfg(test)] - pub fn parse_naked<T: AsRef<[u8]>>(algo: PublicKeyAlgorithm, buf: T) - -> Result<Self> { + pub fn parse<T: AsRef<[u8]>>(algo: PublicKeyAlgorithm, buf: T) + -> Result<Self> { use std::io::Cursor; let cur = Cursor::new(buf); let bio = buffered_reader::Generic::with_cookie( cur, None, Cookie::default()); let mut php = PacketHeaderParser::new_naked(Box::new(bio)); - Self::parse(algo, &mut php) + Self::_parse(algo, &mut php) } /// Parses a set of OpenPGP MPIs representing a ciphertext. @@ -304,9 +301,9 @@ impl mpis::Ciphertext { /// See [Section 3.2 of RFC 4880] for details. /// /// [Section 3.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-3.2 - pub(crate) fn parse<'a>(algo: PublicKeyAlgorithm, - php: &mut PacketHeaderParser<'a>) - -> Result<Self> { + pub(crate) fn _parse<'a>(algo: PublicKeyAlgorithm, + php: &mut PacketHeaderParser<'a>) + -> Result<Self> { use PublicKeyAlgorithm::*; #[allow(deprecated)] @@ -368,16 +365,15 @@ impl mpis::Signature { /// See [Section 3.2 of RFC 4880] for details. /// /// [Section 3.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-3.2 - #[cfg(test)] - pub fn parse_naked<T: AsRef<[u8]>>(algo: PublicKeyAlgorithm, buf: T) - -> Result<Self> { + pub fn parse<T: AsRef<[u8]>>(algo: PublicKeyAlgorithm, buf: T) + -> Result<Self> { use std::io::Cursor; let cur = Cursor::new(buf); let bio = buffered_reader::Generic::with_cookie( cur, None, Cookie::default()); let mut php = PacketHeaderParser::new_naked(Box::new(bio)); - Self::parse(algo, &mut php) + Self::_parse(algo, &mut php) } /// Parses a set of OpenPGP MPIs representing a signature. @@ -386,9 +382,9 @@ impl mpis::Signature { /// See [Section 3.2 of RFC 4880] for details. /// /// [Section 3.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-3.2 - pub(crate) fn parse<'a>(algo: PublicKeyAlgorithm, - php: &mut PacketHeaderParser<'a>) - -> Result<Self> { + pub(crate) fn _parse<'a>(algo: PublicKeyAlgorithm, + php: &mut PacketHeaderParser<'a>) + -> Result<Self> { use PublicKeyAlgorithm::*; #[allow(deprecated)] @@ -472,17 +468,12 @@ impl mpis::Signature { #[test] fn mpis_parse_test() { use super::Parse; - use std::io::Cursor; use PublicKeyAlgorithm::*; // Dummy RSA public key. { let buf = b"\x00\x01\x01\x00\x02\x02".to_vec(); - let cur = Cursor::new(buf); - let bio = buffered_reader::Generic::with_cookie( - cur, None, Cookie::default()); - let mut parser = PacketHeaderParser::new_naked(Box::new(bio)); - let mpis = mpis::PublicKey::parse(RSAEncryptSign, &mut parser).unwrap(); + let mpis = mpis::PublicKey::parse(RSAEncryptSign, buf).unwrap(); //assert_eq!(mpis.serialized_len(), 6); match &mpis { @@ -502,12 +493,7 @@ fn mpis_parse_test() { // The number 2. { let buf = b"\x00\x02\x02".to_vec(); - let cur = Cursor::new(buf); - let bio = buffered_reader::Generic::with_cookie( - cur, None, Cookie::default()); - let mut parser = PacketHeaderParser::new_naked(Box::new(bio)); - let mpis = mpis::Ciphertext::parse(RSAEncryptSign, &mut parser) - .unwrap(); + let mpis = mpis::Ciphertext::parse(RSAEncryptSign, buf).unwrap(); assert_eq!(mpis.serialized_len(), 3); } diff --git a/openpgp/src/parse/parse.rs b/openpgp/src/parse/parse.rs index ea51af0b..7486e493 100644 --- a/openpgp/src/parse/parse.rs +++ b/openpgp/src/parse/parse.rs @@ -899,7 +899,7 @@ impl Signature4 { return php.fail("not a signature algorithm"); } let mpis = php_try!( - crypto::mpis::Signature::parse(pk_algo, &mut php)); + crypto::mpis::Signature::_parse(pk_algo, &mut php)); let hash_algo = hash_algo.into(); let mut pp = php.ok(Packet::Signature(Signature4::new( @@ -1333,14 +1333,14 @@ impl Key4 { let creation_time = php_try!(php.parse_be_u32("creation_time")); let pk_algo: PublicKeyAlgorithm = php_try!(php.parse_u8("pk_algo")).into(); - let mpis = php_try!(PublicKey::parse(pk_algo, &mut php)); + let mpis = php_try!(PublicKey::_parse(pk_algo, &mut php)); let secret = if tag == Tag::SecretKey || tag == Tag::SecretSubkey { let s2k_usage = php_try!(php.parse_u8("s2k_usage")); let sec = match s2k_usage { // Unencrypted 0 => { let sec = php_try!( - crypto::mpis::SecretKey::parse(pk_algo, &mut php)); + crypto::mpis::SecretKey::_parse(pk_algo, &mut php)); let their_chksum = php_try!(php.parse_be_u16("checksum")); let mut cur = Cursor::new(Vec::default()); @@ -2082,7 +2082,7 @@ impl PKESK3 { if ! pk_algo.can_encrypt() { return php.fail("not an encryption algorithm"); } - let mpis = crypto::mpis::Ciphertext::parse(pk_algo, &mut php)?; + let mpis = crypto::mpis::Ciphertext::_parse(pk_algo, &mut php)?; let pkesk = php_try!(PKESK3::new(KeyID::from_bytes(&keyid), pk_algo, mpis)); |