diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-06-28 13:59:48 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-06-28 13:59:48 +0200 |
commit | 1525eec1426a6a34b84809179d784e5cee3e8bfa (patch) | |
tree | 1e73534e95d14f776085df7ca6154057b8108712 /openpgp/src/parse | |
parent | f214d55b7061e9d6006f915ee8fbedf29ddf6078 (diff) |
openpgp: Make struct MPI opaque.
Diffstat (limited to 'openpgp/src/parse')
-rw-r--r-- | openpgp/src/parse/mpis.rs | 20 | ||||
-rw-r--r-- | openpgp/src/parse/parse.rs | 29 |
2 files changed, 23 insertions, 26 deletions
diff --git a/openpgp/src/parse/mpis.rs b/openpgp/src/parse/mpis.rs index 49c7e5f4..3b78755b 100644 --- a/openpgp/src/parse/mpis.rs +++ b/openpgp/src/parse/mpis.rs @@ -478,12 +478,12 @@ fn mpis_parse_test() { //assert_eq!(mpis.serialized_len(), 6); match &mpis { &mpis::PublicKey::RSA{ ref n, ref e } => { - assert_eq!(n.bits, 1); - assert_eq!(n.value[0], 1); - assert_eq!(n.value.len(), 1); - assert_eq!(e.bits, 2); - assert_eq!(e.value[0], 2); - assert_eq!(e.value.len(), 1); + assert_eq!(n.bits(), 1); + assert_eq!(n.value()[0], 1); + assert_eq!(n.value().len(), 1); + assert_eq!(e.bits(), 2); + assert_eq!(e.value()[0], 2); + assert_eq!(e.value().len(), 1); } _ => assert!(false), @@ -500,10 +500,10 @@ fn mpis_parse_test() { // The number 511. let mpi = MPI::from_bytes(b"\x00\x09\x01\xff").unwrap(); - assert_eq!(mpi.value.len(), 2); - assert_eq!(mpi.bits, 9); - assert_eq!(mpi.value[0], 1); - assert_eq!(mpi.value[1], 0xff); + assert_eq!(mpi.value().len(), 2); + assert_eq!(mpi.bits(), 9); + assert_eq!(mpi.value()[0], 1); + assert_eq!(mpi.value()[1], 0xff); // The number 1, incorrectly encoded (the length should be 1, // not 2). diff --git a/openpgp/src/parse/parse.rs b/openpgp/src/parse/parse.rs index 8df103e8..5ff6d599 100644 --- a/openpgp/src/parse/parse.rs +++ b/openpgp/src/parse/parse.rs @@ -1961,7 +1961,7 @@ impl MPI { if bits == 0 { // Now consume the data. php.parse_be_u16(name_len).expect("worked before"); - return Ok(MPI{ bits: 0, value: vec![].into_boxed_slice()}); + return Ok(vec![].into()); } let bytes = (bits + 7) / 8; @@ -1996,10 +1996,7 @@ impl MPI { // Now consume the data. php.parse_be_u16(name_len).expect("worked before"); php.parse_bytes(name, bytes).expect("worked before"); - Ok(MPI{ - bits: bits, - value: value.into_boxed_slice() - }) + Ok(value.into()) } /// Dissects this MPI describing a point into the individual @@ -2019,19 +2016,19 @@ impl MPI { ed25519::ED25519_KEY_SIZE); // This curve uses a custom compression format which // only contains the X coordinate. - if self.value.len() != 1 + curve25519::CURVE25519_SIZE { + if self.value().len() != 1 + curve25519::CURVE25519_SIZE { return Err(Error::MalformedPacket( format!("Bad size of Curve25519 key: {} expected: {}", - self.value.len(), + self.value().len(), 1 + curve25519::CURVE25519_SIZE)).into()); } - if self.value[0] != 0x40 { + if self.value().get(0).map(|&b| b != 0x40).unwrap_or(true) { return Err(Error::MalformedPacket( "Bad encoding of Curve25519 key".into()).into()); } - Ok((&self.value[1..], &[])) + Ok((&self.value()[1..], &[])) }, _ => { @@ -2045,20 +2042,20 @@ impl MPI { + (2 // (x, y) * coordinate_length); - if self.value.len() != expected_length { + if self.value().len() != expected_length { return Err(Error::InvalidArgument( format!("Invalid length of MPI: {} (expected {})", - self.value.len(), expected_length)).into()); + self.value().len(), expected_length)).into()); } - if self.value[0] != 0x04 { + if self.value().get(0).map(|&b| b != 0x04).unwrap_or(true) { return Err(Error::InvalidArgument( - format!("Bad prefix: {:x} (expected 0x04)", self.value[0])) - .into()); + format!("Bad prefix: {:?} (expected Some(0x04))", + self.value().get(0))).into()); } - Ok((&self.value[1..1 + coordinate_length], - &self.value[1 + coordinate_length..])) + Ok((&self.value()[1..1 + coordinate_length], + &self.value()[1 + coordinate_length..])) }, } } |