diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2018-07-04 15:52:52 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2018-07-04 15:52:52 +0200 |
commit | c68015af076f4ddd31813dcbc62056fa6376a517 (patch) | |
tree | cc0ce531a1fa6a4a6d1bf6c838ff5f56dca12797 | |
parent | dff6bc3e86c4cacfe9153d46e7be2b6484e086b9 (diff) |
openpgp: Use broken-down time as key creation time.
-rw-r--r-- | openpgp/src/hash.rs | 11 | ||||
-rw-r--r-- | openpgp/src/key.rs | 7 | ||||
-rw-r--r-- | openpgp/src/lib.rs | 2 | ||||
-rw-r--r-- | openpgp/src/parse/parse.rs | 4 | ||||
-rw-r--r-- | openpgp/src/serialize/mod.rs | 3 | ||||
-rw-r--r-- | openpgp/src/subpacket.rs | 9 | ||||
-rw-r--r-- | openpgp/src/tpk.rs | 11 |
7 files changed, 29 insertions, 18 deletions
diff --git a/openpgp/src/hash.rs b/openpgp/src/hash.rs index 91717345..a39f2b5c 100644 --- a/openpgp/src/hash.rs +++ b/openpgp/src/hash.rs @@ -7,6 +7,7 @@ use Key; use Signature; use Error; use Result; +use conversions::Time; use nettle::Hash; @@ -117,10 +118,12 @@ impl Key { header.push(4); // Creation time. - header.push(((self.creation_time >> 24) & 0xFF) as u8); - header.push(((self.creation_time >> 16) & 0xFF) as u8); - header.push(((self.creation_time >> 8) & 0xFF) as u8); - header.push((self.creation_time & 0xFF) as u8); + let creation_time = self.creation_time.to_pgp() + .unwrap_or(0); + header.push((creation_time >> 24) as u8); + header.push((creation_time >> 16) as u8); + header.push((creation_time >> 8) as u8); + header.push((creation_time >> 0) as u8); // Algorithm. header.push(self.pk_algo.into()); diff --git a/openpgp/src/key.rs b/openpgp/src/key.rs index 1c054203..86205d4e 100644 --- a/openpgp/src/key.rs +++ b/openpgp/src/key.rs @@ -1,4 +1,5 @@ use std::fmt; +use time; use mpis::MPIs; use Tag; @@ -14,7 +15,7 @@ impl fmt::Debug for Key { f.debug_struct("Key") .field("fingerprint", &self.fingerprint()) .field("version", &self.version) - .field("creation_time", &self.creation_time) + .field("creation_time", &format!("{}", self.creation_time.rfc3339())) .field("pk_algo", &self.pk_algo) .field("mpis", &self.mpis) .field("secret", &self.secret) @@ -35,7 +36,7 @@ impl Key { Key { common: Default::default(), version: 4, - creation_time: 0, + creation_time: time::now(), pk_algo: PublicKeyAlgorithm::Unknown(0), mpis: MPIs::empty(), secret: None, @@ -46,7 +47,7 @@ impl Key { /// /// A Unix timestamp is the number of seconds since the Unix /// epoch. - pub fn creation_time(mut self, timestamp: u32) -> Self { + pub fn creation_time(mut self, timestamp: time::Tm) -> Self { self.creation_time = timestamp; self } diff --git a/openpgp/src/lib.rs b/openpgp/src/lib.rs index 72fa4911..84dc6847 100644 --- a/openpgp/src/lib.rs +++ b/openpgp/src/lib.rs @@ -321,7 +321,7 @@ pub struct Key { /// Version of the key packet. Must be 4. pub version: u8, /// When the key was created. - pub creation_time: u32, + pub creation_time: time::Tm, /// Public key algorithm of this signature. pub pk_algo: PublicKeyAlgorithm, /// Public key MPIs. Must be a *PublicKey variant. diff --git a/openpgp/src/parse/parse.rs b/openpgp/src/parse/parse.rs index ab4eb537..1669f9f9 100644 --- a/openpgp/src/parse/parse.rs +++ b/openpgp/src/parse/parse.rs @@ -7,6 +7,7 @@ use std::str; use std::mem; use std::fmt; use std::path::Path; +use time; use nettle::Hash; @@ -43,6 +44,7 @@ use constants::{ PublicKeyAlgorithm, SymmetricAlgorithm, }; +use conversions::Time; use mpis::{MPI, MPIs}; use symmetric::{Decryptor, BufferedReaderDecryptor}; @@ -1040,7 +1042,7 @@ impl Key { let key = Key { common: Default::default(), version: version, - creation_time: creation_time, + creation_time: time::Tm::from_pgp(creation_time), pk_algo: pk_algo, mpis: mpis, secret: secret, diff --git a/openpgp/src/serialize/mod.rs b/openpgp/src/serialize/mod.rs index ac97f150..89f7f6cd 100644 --- a/openpgp/src/serialize/mod.rs +++ b/openpgp/src/serialize/mod.rs @@ -22,6 +22,7 @@ use s2k::S2K; use subpacket::{ Subpacket, SubpacketValue, SubpacketLengthTrait, }; +use conversions::Time; // Whether to trace the modules execution (on stderr). const TRACE : bool = false; @@ -642,7 +643,7 @@ impl SerializeKey for Key { non-version 4 packets.".into()).into()); } write_byte(o, self.version)?; - write_be_u32(o, self.creation_time)?; + write_be_u32(o, self.creation_time.to_pgp()?)?; write_byte(o, self.pk_algo.into())?; self.mpis.serialize(o)?; diff --git a/openpgp/src/subpacket.rs b/openpgp/src/subpacket.rs index 84d34ad9..c9787018 100644 --- a/openpgp/src/subpacket.rs +++ b/openpgp/src/subpacket.rs @@ -1584,7 +1584,7 @@ impl Signature { pub fn key_expired_at(&self, key: &Key, tm: time::Tm) -> bool { match self.key_expiration_time() { Some(e) => - ((key.creation_time + e) as i64) <= tm.to_timespec().sec, + key.creation_time + time::Duration::seconds(e as i64) <= tm, None => false, // No expiration time, does not expire. } @@ -2214,7 +2214,7 @@ fn accessors() { sig.set_revocable(false).unwrap(); assert_eq!(sig.revocable(), Some(false)); - let key = ::Key::new().creation_time(now.to_timespec().sec as u32); + let key = ::Key::new().creation_time(now); sig.set_key_expiration_time(Some(five_minutes)).unwrap(); assert_eq!(sig.key_expiration_time(), Some(five_minutes.num_seconds() as u32)); @@ -2342,6 +2342,7 @@ fn subpacket_test_1 () { #[test] fn subpacket_test_2() { + use conversions::Time; use PacketPile; // Test # Subpacket @@ -2418,9 +2419,9 @@ fn subpacket_test_2() { // Check key expiration. assert!(! sig.key_expired_at(key, time::at_utc(time::Timespec::new( - key.creation_time as i64 + 63072000 - 1, 0)))); + key.creation_time.to_pgp().unwrap() as i64 + 63072000 - 1, 0)))); assert!(sig.key_expired_at(key, time::at_utc(time::Timespec::new( - key.creation_time as i64 + 63072000, 0)))); + key.creation_time.to_pgp().unwrap() as i64 + 63072000, 0)))); assert_eq!(sig.preferred_symmetric_algorithms(), Some(&[9, 8, 7, 2][..])); diff --git a/openpgp/src/tpk.rs b/openpgp/src/tpk.rs index 3f284bd7..f37e10e0 100644 --- a/openpgp/src/tpk.rs +++ b/openpgp/src/tpk.rs @@ -1447,6 +1447,7 @@ mod test { #[test] fn broken() { + use conversions::Time; for i in 0..2 { let tpk = parse_tpk(bytes!("testy-broken-no-pk.pgp"), i == 0); @@ -1465,7 +1466,7 @@ mod test { let tpk = parse_tpk(bytes!("testy-broken-no-sig-on-subkey.pgp"), i == 0).unwrap(); eprintln!("{:?}", tpk); - assert_eq!(tpk.primary.creation_time, 1511355130); + assert_eq!(tpk.primary.creation_time.to_pgp().unwrap(), 1511355130); assert_eq!(tpk.userids.len(), 1); assert_eq!(tpk.userids[0].userid.value, &b"Testy McTestface <testy@example.org>"[..]); @@ -1479,10 +1480,11 @@ mod test { #[test] fn basics() { + use conversions::Time; for i in 0..2 { let tpk = parse_tpk(bytes!("testy.pgp"), i == 0).unwrap(); - assert_eq!(tpk.primary.creation_time, 1511355130); + assert_eq!(tpk.primary.creation_time.to_pgp().unwrap(), 1511355130); assert_eq!(tpk.fingerprint().to_hex(), "3E8877C877274692975189F5D03F6F865226FE8B"); @@ -1496,13 +1498,14 @@ mod test { assert_eq!(tpk.user_attributes.len(), 0); assert_eq!(tpk.subkeys.len(), 1, "number of subkeys"); - assert_eq!(tpk.subkeys[0].subkey.creation_time, 1511355130); + assert_eq!(tpk.subkeys[0].subkey.creation_time.to_pgp().unwrap(), + 1511355130); assert_eq!(tpk.subkeys[0].selfsigs[0].hash_prefix, [ 0xb7, 0xb9 ]); let tpk = parse_tpk(bytes!("testy-no-subkey.pgp"), i == 0).unwrap(); - assert_eq!(tpk.primary.creation_time, 1511355130); + assert_eq!(tpk.primary.creation_time.to_pgp().unwrap(), 1511355130); assert_eq!(tpk.fingerprint().to_hex(), "3E8877C877274692975189F5D03F6F865226FE8B"); |