Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ webpki-roots = "0.26"
pem = "3.0.3"
thiserror = "2"
x509-parser = "0.16"
chrono = { version = "0.4.24", default-features = false, features = ["clock"] }
time = "0.3.34"
async-trait = "0.1.53"
async-io = "2.3.0"
tokio = { version= "1.20.1", optional= true }
Expand Down
11 changes: 4 additions & 7 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::acceptor::AcmeAcceptor;
use crate::acme::{Account, AcmeError, Auth, AuthStatus, Directory, Identifier, Order, OrderStatus, ACME_TLS_ALPN_NAME};
use crate::{any_ecdsa_type, crypto_provider, AcmeConfig, Incoming, ResolvesServerCertAcme};
use async_io::Timer;
use chrono::{DateTime, TimeZone, Utc};
use core::fmt;
use futures::future::try_join_all;
use futures::prelude::*;
Expand All @@ -20,6 +19,7 @@ use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;
use thiserror::Error;
use time::OffsetDateTime;
use x509_parser::parse_x509_certificate;

#[allow(clippy::type_complexity)]
Expand Down Expand Up @@ -191,7 +191,7 @@ impl<EC: 'static + Debug, EA: 'static + Debug> AcmeState<EC, EA> {
wait: None,
}
}
fn parse_cert(pem: &[u8]) -> Result<(CertifiedKey, [DateTime<Utc>; 2]), CertParseError> {
fn parse_cert(pem: &[u8]) -> Result<(CertifiedKey, [OffsetDateTime; 2]), CertParseError> {
let mut pems = pem::parse_many(pem)?;
if pems.len() < 2 {
return Err(CertParseError::TooFewPem(pems.len()));
Expand All @@ -204,7 +204,7 @@ impl<EC: 'static + Debug, EA: 'static + Debug> AcmeState<EC, EA> {
let validity = match parse_x509_certificate(&cert_chain[0]) {
Ok((_, cert)) => {
let validity = cert.validity();
[validity.not_before, validity.not_after].map(|t| Utc.timestamp_opt(t.timestamp(), 0).earliest().unwrap())
[validity.not_before, validity.not_after].map(|t| t.to_datetime())
}
Err(err) => return Err(CertParseError::X509(err)),
};
Expand All @@ -224,10 +224,7 @@ impl<EC: 'static + Debug, EA: 'static + Debug> AcmeState<EC, EA> {
}
};
self.resolver.set_cert(Arc::new(cert));
let wait_duration = (validity[1] - (validity[1] - validity[0]) / 3 - Utc::now())
.max(chrono::Duration::zero())
.to_std()
.unwrap_or_default();
let wait_duration = (validity[1] - (validity[1] - validity[0]) / 3i32 - OffsetDateTime::now_utc()).unsigned_abs();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this a bit safer and more obvious while we are at it. Something like this:

let [valid_from, valid_until] = validity;
let total_valid_duration = valid_until.saturating_sub(valid_from).max(Duration::ZERO);
let renew_at = valid_until.saturating_sub(total_valid_duration/3);
let wait_duration = renew_at.saturating_sub(OffsetDateTime::now_utc());
self.wait = Some(Timer::after(wait_duration.abs()));

Copy link
Author

@Rapptz Rapptz Dec 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, this doesn't actually work because OffsetDateTime::saturating_sub takes a Duration but both arguments are OffsetDateTime. The only way to get dt - dt to produce a Duration without using Sub seems to be either reimplementing the logic within Sub or just working with "instants" (i.e. converting the dt to nanoseconds since the UNIX epoch) and doing the subtraction that way. Note that this approach does not produce a Duration but an i128 instead.

Another consideration that might make it easier to work with is whether nanosecond precision is necessary, since by using the regular second precision unix_timestamp might be easier than dealing with an i128.

Copy link
Owner

@FlorianUekermann FlorianUekermann Jan 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working with i64 (seconds) for timestamps and durations seems fine.
It's a bit annoying that time doesn't provide any safe "time between" function... If lhs > rhs and the offsets are the same lhs - rhs may be safe. But I find that difficult to confirm looking at the implementation.

The more I think about it, it seems like it may actually be easiest not to use time here, convert to u64 immediately and use saturating operations on that.

self.wait = Some(Timer::after(wait_duration));
if cached {
return Ok(EventOk::DeployedCachedCert);
Expand Down
Loading