diff options
author | Neal H. Walfield <neal@pep.foundation> | 2020-08-03 17:22:50 +0200 |
---|---|---|
committer | Neal H. Walfield <neal@pep.foundation> | 2020-08-03 17:35:41 +0200 |
commit | cb6b672a4dbb703cb92661bfa82b7089919a107b (patch) | |
tree | 62aeb4ebca67f058822a484a7fe3668d7dee8792 /tool/src | |
parent | ad9dac7d5a68f9a6dd2bcae67a405eb9f2756b58 (diff) |
openpgp: Change CertBuilder to use a relative expiration time.
- `CertBuilder::set_expiration_time` takes an absolute time.
- Most callers use a relative time.
- Internally, we need a relative time (that's what the Key
Expiration Time packet takes).
- Converting the absolute time to a relative time is error prone:
should it be relative to the creation time when called or when
`CertBuilder` is finalized?
- KISS: Change it to just take a relative time.
- To better reflect the new semantics, also change the name to
`CertBuilder::set_validity_period`.
Diffstat (limited to 'tool/src')
-rw-r--r-- | tool/src/commands/key.rs | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/tool/src/commands/key.rs b/tool/src/commands/key.rs index abd95433..dc78866f 100644 --- a/tool/src/commands/key.rs +++ b/tool/src/commands/key.rs @@ -33,22 +33,24 @@ pub fn generate(m: &ArgMatches, force: bool) -> Result<()> { // Expiration. match (m.value_of("expires"), m.value_of("expires-in")) { (None, None) => // Default expiration. - builder = builder.set_expiration_time( - Some(SystemTime::now() - + Duration::new(3 * SECONDS_IN_YEAR, 0))), + builder = builder.set_validity_period( + Some(Duration::new(3 * SECONDS_IN_YEAR, 0))), (Some(t), None) if t == "never" => - builder = builder.set_expiration_time(None), + builder = builder.set_validity_period(None), (Some(t), None) => { - let t = - crate::parse_iso8601(t, chrono::NaiveTime::from_hms(0, 0, 0))?; - builder = builder.set_expiration_time(Some(t.into())); + let now = builder.creation_time() + .unwrap_or_else(std::time::SystemTime::now); + let expiration = SystemTime::from( + crate::parse_iso8601(t, chrono::NaiveTime::from_hms(0, 0, 0))?); + let validity = expiration.duration_since(now)?; + builder = builder.set_creation_time(now) + .set_validity_period(validity); }, (None, Some(d)) if d == "never" => - builder = builder.set_expiration_time(None), + builder = builder.set_validity_period(None), (None, Some(d)) => { let d = parse_duration(d)?; - builder = builder.set_expiration_time( - Some(SystemTime::now() + d)); + builder = builder.set_validity_period(Some(d)); }, (Some(_), Some(_)) => unreachable!("conflicting args"), } |