Skip to content

Commit d5e33b6

Browse files
committed
fixup! ACME: certificate issue and renewal implementation.
1 parent e9132b5 commit d5e33b6

File tree

4 files changed

+36
-28
lines changed

4 files changed

+36
-28
lines changed

src/acme.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use ngx::async_::sleep;
1212
use ngx::collections::Vec;
1313
use ngx::ngx_log_debug;
1414
use openssl::pkey::{PKey, PKeyRef, Private};
15-
use openssl::x509::{self, extension as x509_ext, X509Req, X509};
15+
use openssl::x509::{self, extension as x509_ext, X509Req};
1616

1717
use self::account_key::AccountKey;
1818
use self::types::{AuthorizationStatus, ChallengeKind, ChallengeStatus, OrderStatus};
@@ -31,7 +31,6 @@ static REPLAY_NONCE: http::HeaderName = http::HeaderName::from_static("replay-no
3131

3232
pub struct NewCertificateOutput {
3333
pub chain: Bytes,
34-
pub x509: Vec<X509>,
3534
pub pkey: PKey<Private>,
3635
}
3736

@@ -85,7 +84,13 @@ where
8584
Http: HttpClient,
8685
{
8786
pub fn new(http: Http, issuer: &'a Issuer, log: NonNull<nginx_sys::ngx_log_t>) -> Result<Self> {
88-
let key = AccountKey::new(issuer.pkey.as_ref().expect("account key"))?;
87+
let key = AccountKey::try_from(
88+
issuer
89+
.pkey
90+
.as_ref()
91+
.expect("checked during configuration load")
92+
.as_ref(),
93+
)?;
8994

9095
Ok(Self {
9196
issuer,
@@ -367,10 +372,7 @@ where
367372

368373
let chain = self.post(&certificate, b"").await?.into_body();
369374

370-
// FIXME: avoid reallocation from std::vec::Vec.
371-
let x509 = Vec::from_iter(X509::stack_from_pem(&chain)?);
372-
373-
Ok(NewCertificateOutput { chain, x509, pkey })
375+
Ok(NewCertificateOutput { chain, pkey })
374376
}
375377

376378
async fn do_authorization(

src/acme/account_key.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,6 @@ enum AccountKeyInner {
2828
}
2929

3030
impl AccountKey {
31-
pub fn new(key: &PKeyRef<Private>) -> Result<Self, AccountKeyError> {
32-
let inner = match key.id() {
33-
Id::EC => key.try_into().map(AccountKeyInner::ShaWithEcdsa),
34-
Id::RSA => key.try_into().map(AccountKeyInner::ShaWithRsa),
35-
id => Err(NewKeyError::Algorithm(id)),
36-
}?;
37-
38-
let thumbprint = match inner {
39-
AccountKeyInner::ShaWithEcdsa(ref key) => key.thumbprint(),
40-
AccountKeyInner::ShaWithRsa(ref key) => key.thumbprint(),
41-
}?;
42-
43-
Ok(Self { inner, thumbprint })
44-
}
45-
4631
pub fn thumbprint(&self) -> &[u8] {
4732
self.thumbprint.as_bytes()
4833
}
@@ -77,3 +62,22 @@ impl Serialize for AccountKey {
7762
}
7863
}
7964
}
65+
66+
impl TryFrom<&PKeyRef<Private>> for AccountKey {
67+
type Error = AccountKeyError;
68+
69+
fn try_from(value: &PKeyRef<Private>) -> Result<Self, Self::Error> {
70+
let inner = match value.id() {
71+
Id::EC => value.try_into().map(AccountKeyInner::ShaWithEcdsa),
72+
Id::RSA => value.try_into().map(AccountKeyInner::ShaWithRsa),
73+
id => Err(NewKeyError::Algorithm(id)),
74+
}?;
75+
76+
let thumbprint = match inner {
77+
AccountKeyInner::ShaWithEcdsa(ref key) => key.thumbprint(),
78+
AccountKeyInner::ShaWithRsa(ref key) => key.thumbprint(),
79+
}?;
80+
81+
Ok(Self { inner, thumbprint })
82+
}
83+
}

src/conf/order.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ where
5454
std::format!("{name}-{hash:x}", hash = hasher.finish())
5555
}
5656

57-
pub fn borrow<NewA>(&self, alloc: NewA) -> CertificateOrder<&str, NewA>
57+
pub fn to_str_order<NewA>(&self, alloc: NewA) -> CertificateOrder<&str, NewA>
5858
where
5959
NewA: Allocator + Clone,
6060
S: AsRef<[u8]>,

src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![no_std]
22
extern crate std;
33

4-
use core::ops::Deref;
54
use core::time::Duration;
65
use core::{cmp, ptr};
76

@@ -14,6 +13,7 @@ use ngx::core::Status;
1413
use ngx::http::{HttpModule, HttpModuleMainConf, HttpModuleServerConf};
1514
use ngx::log::ngx_cycle_log;
1615
use ngx::{ngx_log_debug, ngx_log_error};
16+
use openssl::x509::X509;
1717
use time::TimeRange;
1818
use zeroize::Zeroizing;
1919

@@ -256,15 +256,17 @@ async fn ngx_http_acme_update_certificates_for_issuer(
256256
let alloc = crate::util::OwnedPool::new(nginx_sys::NGX_DEFAULT_POOL_SIZE as _, log)
257257
.map_err(|_| AllocError)?;
258258

259-
let borrowed_order = order.borrow(alloc.deref());
260-
let res = client.new_certificate(&borrowed_order).await;
259+
// Acme client wants &str and we already validated that the identifiers are valid UTF-8.
260+
let str_order = order.to_str_order(&*alloc);
261+
let res = client.new_certificate(&str_order).await;
261262

262263
let cert_next = match res {
263264
Ok(ref val) => {
264265
let pkey = Zeroizing::new(val.pkey.private_key_to_pem_pkcs8()?);
266+
let x509 = X509::from_pem(&val.chain)?;
265267

266-
let valid = TimeRange::from_x509(&val.x509[0])
267-
.unwrap_or(TimeRange::new(Time::now(), Time::now()));
268+
let valid =
269+
TimeRange::from_x509(&x509).unwrap_or(TimeRange::new(Time::now(), Time::now()));
268270

269271
let next = match cert.write().set(&val.chain, &pkey, valid) {
270272
Ok(x) => x,

0 commit comments

Comments
 (0)