summaryrefslogtreecommitdiffstats
path: root/openpgp/src/packet/key/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'openpgp/src/packet/key/mod.rs')
-rw-r--r--openpgp/src/packet/key/mod.rs75
1 files changed, 42 insertions, 33 deletions
diff --git a/openpgp/src/packet/key/mod.rs b/openpgp/src/packet/key/mod.rs
index 5cea7423..b6337ce0 100644
--- a/openpgp/src/packet/key/mod.rs
+++ b/openpgp/src/packet/key/mod.rs
@@ -52,7 +52,7 @@
use std::fmt;
use std::cmp::Ordering;
-use time;
+use std::time;
use crate::Error;
use crate::crypto::{self, mem::{self, Protected}, mpis, hash::Hash};
@@ -515,7 +515,7 @@ pub struct Key4<P, R>
/// CTB packet header fields.
pub(crate) common: packet::Common,
/// When the key was created.
- creation_time: time::Tm,
+ creation_time: time::SystemTime,
/// Public key algorithm of this signature.
pk_algo: PublicKeyAlgorithm,
/// Public key MPIs.
@@ -535,7 +535,7 @@ impl<P, R> fmt::Debug for Key4<P, R>
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Key4")
.field("fingerprint", &self.fingerprint())
- .field("creation_time", &format!("{}", self.creation_time.rfc3339()))
+ .field("creation_time", &self.creation_time)
.field("pk_algo", &self.pk_algo)
.field("mpis", &self.mpis)
.field("secret", &self.secret)
@@ -582,13 +582,14 @@ impl<P, R> Key4<P, R>
R: key::KeyRole,
{
/// Creates a new OpenPGP key packet.
- pub fn new(creation_time: time::Tm, pk_algo: PublicKeyAlgorithm,
- mpis: mpis::PublicKey, secret: Option<SecretKeyMaterial>)
- -> Result<Self>
+ pub fn new<T>(creation_time: T, pk_algo: PublicKeyAlgorithm,
+ mpis: mpis::PublicKey, secret: Option<SecretKeyMaterial>)
+ -> Result<Self>
+ where T: Into<time::SystemTime>
{
Ok(Key4 {
common: Default::default(),
- creation_time: creation_time,
+ creation_time: creation_time.into(),
pk_algo: pk_algo,
mpis: mpis,
secret: secret,
@@ -607,14 +608,15 @@ impl<P, R> Key4<P, R>
hash: H, sym: S, ctime: T)
-> Result<Self> where H: Into<Option<HashAlgorithm>>,
S: Into<Option<SymmetricAlgorithm>>,
- T: Into<Option<time::Tm>>
+ T: Into<Option<time::SystemTime>>
{
let mut point = Vec::from(public_key);
point.insert(0, 0x40);
Ok(Key4 {
common: Default::default(),
- creation_time: ctime.into().unwrap_or(time::now()),
+ creation_time: ctime.into()
+ .unwrap_or_else(|| time::SystemTime::now().canonicalize()),
pk_algo: PublicKeyAlgorithm::ECDH,
mpis: mpis::PublicKey::ECDH{
curve: Curve::Cv25519,
@@ -638,7 +640,7 @@ impl<P, R> Key4<P, R>
hash: H, sym: S, ctime: T)
-> Result<Self> where H: Into<Option<HashAlgorithm>>,
S: Into<Option<SymmetricAlgorithm>>,
- T: Into<Option<time::Tm>>
+ T: Into<Option<time::SystemTime>>
{
use nettle::curve25519::{self, CURVE25519_SIZE};
@@ -650,7 +652,8 @@ impl<P, R> Key4<P, R>
Ok(Key4 {
common: Default::default(),
- creation_time: ctime.into().unwrap_or(time::now()),
+ creation_time: ctime.into()
+ .unwrap_or_else(|| time::SystemTime::now().canonicalize()),
pk_algo: PublicKeyAlgorithm::ECDH,
mpis: mpis::PublicKey::ECDH{
curve: Curve::Cv25519,
@@ -673,14 +676,15 @@ impl<P, R> Key4<P, R>
/// will be used. The key will have it's creation date set to
/// `ctime` or the current time if `None` is given.
pub fn import_public_ed25519<T>(public_key: &[u8], ctime: T) -> Result<Self>
- where T: Into<Option<time::Tm>>
+ where T: Into<Option<time::SystemTime>>
{
let mut point = Vec::from(public_key);
point.insert(0, 0x40);
Ok(Key4 {
common: Default::default(),
- creation_time: ctime.into().unwrap_or(time::now()),
+ creation_time: ctime.into()
+ .unwrap_or_else(|| time::SystemTime::now().canonicalize()),
pk_algo: PublicKeyAlgorithm::EdDSA,
mpis: mpis::PublicKey::EdDSA{
curve: Curve::Ed25519,
@@ -699,7 +703,7 @@ impl<P, R> Key4<P, R>
/// will be used. The key will have it's creation date set to
/// `ctime` or the current time if `None` is given.
pub fn import_secret_ed25519<T>(private_key: &[u8], ctime: T)
- -> Result<Self> where T: Into<Option<time::Tm>>
+ -> Result<Self> where T: Into<Option<time::SystemTime>>
{
use nettle::ed25519::{self, ED25519_KEY_SIZE};
@@ -708,7 +712,8 @@ impl<P, R> Key4<P, R>
Ok(Key4 {
common: Default::default(),
- creation_time: ctime.into().unwrap_or(time::now()),
+ creation_time: ctime.into()
+ .unwrap_or_else(|| time::SystemTime::now().canonicalize()),
pk_algo: PublicKeyAlgorithm::EdDSA,
mpis: mpis::PublicKey::EdDSA{
curve: Curve::Ed25519,
@@ -728,11 +733,12 @@ impl<P, R> Key4<P, R>
/// have it's creation date set to `ctime` or the current time if `None`
/// is given.
pub fn import_public_rsa<T>(e: &[u8], n: &[u8], ctime: T)
- -> Result<Self> where T: Into<Option<time::Tm>>
+ -> Result<Self> where T: Into<Option<time::SystemTime>>
{
Ok(Key4 {
common: Default::default(),
- creation_time: ctime.into().unwrap_or(time::now()),
+ creation_time: ctime.into()
+ .unwrap_or_else(|| time::SystemTime::now().canonicalize()),
pk_algo: PublicKeyAlgorithm::RSAEncryptSign,
mpis: mpis::PublicKey::RSA {
e: mpis::MPI::new(e),
@@ -750,7 +756,7 @@ impl<P, R> Key4<P, R>
/// have it's creation date set to `ctime` or the current time if `None`
/// is given.
pub fn import_secret_rsa<T>(d: &[u8], p: &[u8], q: &[u8], ctime: T)
- -> Result<Self> where T: Into<Option<time::Tm>>
+ -> Result<Self> where T: Into<Option<time::SystemTime>>
{
use nettle::rsa;
@@ -760,7 +766,8 @@ impl<P, R> Key4<P, R>
Ok(Key4 {
common: Default::default(),
- creation_time: ctime.into().unwrap_or(time::now()),
+ creation_time: ctime.into()
+ .unwrap_or_else(|| time::SystemTime::now().canonicalize()),
pk_algo: PublicKeyAlgorithm::RSAEncryptSign,
mpis: mpis::PublicKey::RSA {
e: mpis::MPI::new(&key.e()[..]),
@@ -799,7 +806,7 @@ impl<P, R> Key4<P, R>
Ok(Key4 {
common: Default::default(),
- creation_time: time::now().canonicalize(),
+ creation_time: time::SystemTime::now().canonicalize(),
pk_algo: PublicKeyAlgorithm::RSAEncryptSign,
mpis: public_mpis,
secret: sec,
@@ -954,7 +961,7 @@ impl<P, R> Key4<P, R>
Ok(Key4 {
common: Default::default(),
- creation_time: time::now().canonicalize(),
+ creation_time: time::SystemTime::now().canonicalize(),
pk_algo: pk_algo,
mpis: mpis,
secret: secret,
@@ -964,12 +971,13 @@ impl<P, R> Key4<P, R>
}
/// Gets the key packet's creation time field.
- pub fn creation_time(&self) -> &time::Tm {
- &self.creation_time
+ pub fn creation_time(&self) -> time::SystemTime {
+ self.creation_time
}
/// Sets the key packet's creation time field.
- pub fn set_creation_time(&mut self, timestamp: time::Tm) -> time::Tm {
+ pub fn set_creation_time(&mut self, timestamp: time::SystemTime)
+ -> time::SystemTime {
::std::mem::replace(&mut self.creation_time, timestamp.canonicalize())
}
@@ -1408,10 +1416,10 @@ mod tests {
fn import_cv25519() {
use crate::crypto::{ecdh, mem, SessionKey};
use self::mpis::{MPI, Ciphertext};
- use time::{at, Timespec};
// X25519 key
- let ctime = at(Timespec::new(0x5c487129,0));
+ let ctime =
+ time::UNIX_EPOCH + time::Duration::new(0x5c487129, 0);
let public = b"\xed\x59\x0a\x15\x08\x95\xe9\x92\xd2\x2c\x14\x01\xb3\xe9\x3b\x7f\xff\xe6\x6f\x22\x65\xec\x69\xd9\xb8\xda\x24\x2c\x64\x84\x44\x11";
let key : key::SecretKey
= Key4::import_public_cv25519(&public[..],
@@ -1443,10 +1451,10 @@ mod tests {
fn import_cv25519_sec() {
use crate::crypto::ecdh;
use self::mpis::{MPI, Ciphertext};
- use time::{at, Timespec};
// X25519 key
- let ctime = at(Timespec::new(0x5c487129,0));
+ let ctime =
+ time::UNIX_EPOCH + time::Duration::new(0x5c487129, 0);
let public = b"\xed\x59\x0a\x15\x08\x95\xe9\x92\xd2\x2c\x14\x01\xb3\xe9\x3b\x7f\xff\xe6\x6f\x22\x65\xec\x69\xd9\xb8\xda\x24\x2c\x64\x84\x44\x11";
let secret = b"\xa0\x27\x13\x99\xc9\xe3\x2e\xd2\x47\xf6\xd6\x63\x9d\xe6\xec\xcb\x57\x0b\x92\xbb\x17\xfe\xb8\xf1\xc4\x1f\x06\x7c\x55\xfc\xdd\x58";
let key: key::PublicKey
@@ -1484,10 +1492,10 @@ mod tests {
fn import_rsa() {
use crate::crypto::SessionKey;
use self::mpis::{MPI, Ciphertext};
- use time::{at, Timespec};
// RSA key
- let ctime = at(Timespec::new(1548950502,0));
+ let ctime =
+ time::UNIX_EPOCH + time::Duration::new(1548950502, 0);
let d = b"\x14\xC4\x3A\x0C\x3A\x79\xA4\xF7\x63\x0D\x89\x93\x63\x8B\x56\x9C\x29\x2E\xCD\xCF\xBF\xB0\xEC\x66\x52\xC3\x70\x1B\x19\x21\x73\xDE\x8B\xAC\x0E\xF2\xE1\x28\x42\x66\x56\x55\x00\x3B\xFD\x50\xC4\x7C\xBC\x9D\xEB\x7D\xF4\x81\xFC\xC3\xBF\xF7\xFF\xD0\x41\x3E\x50\x3B\x5F\x5D\x5F\x56\x67\x5E\x00\xCE\xA4\x53\xB8\x59\xA0\x40\xC8\x96\x6D\x12\x09\x27\xBE\x1D\xF1\xC2\x68\xFC\xF0\x14\xD6\x52\x77\x07\xC8\x12\x36\x9C\x9A\x5C\xAF\x43\xCC\x95\x20\xBB\x0A\x44\x94\xDD\xB4\x4F\x45\x4E\x3A\x1A\x30\x0D\x66\x40\xAC\x68\xE8\xB0\xFD\xCD\x6C\x6B\x6C\xB5\xF7\xE4\x36\x95\xC2\x96\x98\xFD\xCA\x39\x6C\x1A\x2E\x55\xAD\xB6\xE0\xF8\x2C\xFF\xBC\xD3\x32\x15\x52\x39\xB3\x92\x35\xDB\x8B\x68\xAF\x2D\x4A\x6E\x64\xB8\x28\x63\xC4\x24\x94\x2D\xA9\xDB\x93\x56\xE3\xBC\xD0\xB6\x38\x84\x04\xA4\xC6\x18\x48\xFE\xB2\xF8\xE1\x60\x37\x52\x96\x41\xA5\x79\xF6\x3D\xB7\x2A\x71\x5B\x7A\x75\xBF\x7F\xA2\x5A\xC8\xA1\x38\xF2\x5A\xBD\x14\xFC\xAF\xB4\x54\x83\xA4\xBD\x49\xA2\x8B\x91\xB0\xE0\x4A\x1B\x21\x54\x07\x19\x70\x64\x7C\x3E\x9F\x8D\x8B\xE4\x70\xD1\xE7\xBE\x4E\x5C\xCE\xF1";
let p = b"\xC8\x32\xD1\x17\x41\x4D\x8F\x37\x09\x18\x32\x4C\x4C\xF4\xA2\x15\x27\x43\x3D\xBB\xB5\xF6\x1F\xCF\xD2\xE4\x43\x61\x07\x0E\x9E\x35\x1F\x0A\x5D\xFB\x3A\x45\x74\x61\x73\x73\x7B\x5F\x1F\x87\xFB\x54\x8D\xA8\x85\x3E\xB0\xB7\xC7\xF5\xC9\x13\x99\x8D\x40\xE6\xA6\xD0\x71\x3A\xE3\x2D\x4A\xC3\xA3\xFF\xF7\x72\x82\x14\x52\xA4\xBA\x63\x0E\x17\xCA\xCA\x18\xC4\x3A\x40\x79\xF1\x86\xB3\x10\x4B\x9F\xB2\xAE\x2E\x13\x38\x8D\x2C\xF9\x88\x4C\x25\x53\xEF\xF9\xD1\x8B\x1A\x7C\xE7\xF6\x4B\x73\x51\x31\xFA\x44\x1D\x36\x65\x71\xDA\xFC\x6F";
let q = b"\xCC\x30\xE9\xCC\xCB\x31\x28\xB5\x90\xFF\x06\x62\x42\x5B\x24\x0E\x00\xFE\xE2\x37\xC4\xAC\xBB\x3B\x8F\xF2\x0E\x3F\x78\xCF\x6B\x7C\xE8\x75\x57\x7C\x15\x9D\x1A\x66\xF2\x0A\xE5\xD3\x0B\xE7\x40\xF7\xE7\x00\xB6\x86\xB5\xD9\x20\x67\xE0\x4A\xC0\x90\xA4\x13\x4D\xC9\xB0\x12\xC5\xCD\x4C\xEB\xA1\x91\x2D\x43\x58\x6E\xB6\x75\xA0\x93\xF0\x5B\xC5\x31\xCA\xB7\xC6\x22\x0C\xD3\xEC\x84\xC5\x91\xA1\x5F\x2C\x8E\x07\x5D\xA1\x98\x67\xC5\x7A\x58\x16\x71\x3D\xED\x91\x03\x0D\xD4\x25\x07\x89\x9B\x33\x98\xA3\x70\xD9\xE7\xC8\x17\xA3\xD9";
@@ -1516,7 +1524,6 @@ mod tests {
#[test]
fn import_ed25519() {
- use time::{at, Timespec};
use crate::{Fingerprint, KeyID};
use crate::constants::SignatureType;
use crate::packet::signature::Signature4;
@@ -1524,7 +1531,8 @@ mod tests {
Subpacket, SubpacketValue, SubpacketArea};
// Ed25519 key
- let ctime = at(Timespec::new(1548249630,0));
+ let ctime =
+ time::UNIX_EPOCH + time::Duration::new(1548249630, 0);
let q = b"\x57\x15\x45\x1B\x68\xA5\x13\xA2\x20\x0F\x71\x9D\xE3\x05\x3B\xED\xA2\x21\xDE\x61\x5A\xF5\x67\x45\xBB\x97\x99\x43\x53\x59\x7C\x3F";
let key: key::PublicKey
= Key4::import_public_ed25519(q, ctime).unwrap().into();
@@ -1533,7 +1541,8 @@ mod tests {
let mut unhashed = SubpacketArea::empty();
let fpr = Fingerprint::from_hex("D81A 5DC0 DEBF EE5F 9AC8 20EB 6769 5DB9 920D 4FAC").unwrap();
let kid = KeyID::from_hex("6769 5DB9 920D 4FAC").unwrap();
- let ctime = at(Timespec::new(1549460479,0));
+ let ctime =
+ time::UNIX_EPOCH + time::Duration::new(1549460479, 0);
let r = b"\x5A\xF9\xC7\x42\x70\x24\x73\xFF\x7F\x27\xF9\x20\x9D\x20\x0F\xE3\x8F\x71\x3C\x5F\x97\xFD\x60\x80\x39\x29\xC2\x14\xFD\xC2\x4D\x70";
let s = b"\x6E\x68\x74\x11\x72\xF4\x9C\xE1\x99\x99\x1F\x67\xFC\x3A\x68\x33\xF9\x3F\x3A\xB9\x1A\xA5\x72\x4E\x78\xD4\x81\xCB\x7B\xA5\xE5\x0A";