lers 0.4.0

An async, user-friendly Let's Encrypt/ACMEv2 library written in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
use crate::{
    account::Account,
    error::{Error, Result},
    order::Order,
    responses::{Identifier, RevocationReason},
    Directory,
};
use base64::engine::{general_purpose::URL_SAFE_NO_PAD as BASE64, Engine};
use chrono::{DateTime, Utc};
use futures::future;
use openssl::{
    ec::{EcGroup, EcKey},
    hash::MessageDigest,
    nid::Nid,
    pkey::{PKey, Private},
    x509::X509,
};
use tracing::{info, instrument, Level, Span};

/// Used to configure the ordering of a certificate
pub struct CertificateBuilder<'a> {
    account: &'a Account,
    identifiers: Vec<Identifier>,
    not_before: Option<DateTime<Utc>>,
    not_after: Option<DateTime<Utc>>,
    private_key: Option<PKey<Private>>,
}

impl<'a> CertificateBuilder<'a> {
    pub(crate) fn new(account: &'a Account) -> CertificateBuilder<'a> {
        CertificateBuilder {
            account,
            // We know there'll be at least 1 identifier in the order
            identifiers: Vec::with_capacity(1),
            not_before: None,
            not_after: None,
            private_key: None,
        }
    }

    /// Add a domain (DNS identifier) to the certificate.
    ///
    /// All certificates must have at least one domain associated with them.
    pub fn add_domain<S: Into<String>>(mut self, domain: S) -> Self {
        self.identifiers.push(Identifier::Dns(domain.into()));
        self
    }

    /// When the certificate should expire.
    ///
    /// This may not be supported by all ACME servers, namely
    /// [Let's Encrypt](https://github.com/letsencrypt/boulder/blob/main/docs/acme-divergences.md#section-74).
    pub fn expiration(mut self, at: DateTime<Utc>) -> Self {
        self.not_after = Some(at);
        self
    }

    /// When the certificate should start being valid.
    ///
    /// This may not be supported by all ACME servers, namely
    /// [Let's Encrypt](https://github.com/letsencrypt/boulder/blob/main/docs/acme-divergences.md#section-74).
    pub fn not_before(mut self, at: DateTime<Utc>) -> Self {
        self.not_before = Some(at);
        self
    }

    /// Set the private key for certificate.
    pub fn private_key(mut self, private_key: PKey<Private>) -> Self {
        self.private_key = Some(private_key);
        self
    }

    /// Obtain the certificate
    #[instrument(
        level = Level::INFO,
        name = "CertificateBuilder::obtain",
        err,
        skip_all,
        fields(
            order.id,
            self.account.id,
            ?self.identifiers,
            ?self.not_before,
            ?self.not_after,
        ),
    )]
    pub async fn obtain(self) -> Result<Certificate> {
        if self.identifiers.is_empty() {
            return Err(Error::MissingIdentifiers);
        }

        let mut order = Order::create(
            self.account,
            self.identifiers,
            self.not_before,
            self.not_after,
        )
        .await?;
        Span::current().record("order.id", order.id());

        info!("solving order authorization(s)");
        let authorizations = order.authorizations().await?;
        future::try_join_all(authorizations.iter().map(|a| a.solve())).await?;

        info!("waiting for order to be ready...");
        order.wait_ready().await?;

        let private_key = match self.private_key {
            Some(key) => key,
            None => {
                let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1)?;
                let ec = EcKey::generate(&group)?;
                PKey::from_ec_key(ec)?
            }
        };

        info!("finalizing order...");
        order.finalize(&private_key).await?;

        order.wait_done().await?;

        info!("order completed, downloading certificate...");
        let chain = order.download().await?;

        Ok(Certificate { chain, private_key })
    }
}

/// An issued certificate by the ACME server
#[derive(Debug)]
pub struct Certificate {
    chain: Vec<X509>,
    pub(crate) private_key: PKey<Private>,
}

impl Certificate {
    /// Load a certificate from an exported chain and private key
    pub fn from_chain_and_private_key(chain: Format<'_>, private_key: Format<'_>) -> Result<Self> {
        Ok(Certificate {
            chain: chain.try_into()?,
            private_key: private_key.try_into()?,
        })
    }

    /// Create a certificate from an already parsed chain and private key
    pub fn from_raw_chain_and_private_key(chain: Vec<X509>, private_key: PKey<Private>) -> Self {
        Certificate { chain, private_key }
    }

    /// Export the private key in PEM PKCS#8 format
    pub fn private_key_to_pem(&self) -> Result<Vec<u8>> {
        Ok(self.private_key.private_key_to_pem_pkcs8()?)
    }

    /// Export the private key in DER format
    pub fn private_key_to_der(&self) -> Result<Vec<u8>> {
        Ok(self.private_key.private_key_to_der()?)
    }

    /// Export the issued certificate in PEM format
    ///
    /// **NOTE**: this does NOT export the full certificate chain, use
    /// [`Certificate::fullchain_to_pem`] for that.
    pub fn to_pem(&self) -> Result<Vec<u8>> {
        Ok(self.chain.first().unwrap().to_pem()?)
    }

    /// Export the full certificate chain in PEM format
    pub fn fullchain_to_pem(&self) -> Result<Vec<u8>> {
        let mut result = Vec::new();
        for certificate in &self.chain {
            result.extend(certificate.to_pem()?);
        }
        Ok(result)
    }

    /// Export the issued certificate in DER format
    ///
    /// **NOTE**: this does NOT export the full certificate chain, use
    /// [`Certificate::fullchain_to_der`] for that.
    pub fn to_der(&self) -> Result<Vec<u8>> {
        Ok(self.chain.first().unwrap().to_der()?)
    }

    /// Export the full certificate chain in DER format
    pub fn fullchain_to_der(&self) -> Result<Vec<u8>> {
        let mut result = Vec::new();
        for certificate in &self.chain {
            result.extend(certificate.to_der()?);
        }
        Ok(result)
    }

    /// Get a reference to the underlying [`openssl::x509::X509`] instance for the certificate.
    pub fn x509(&self) -> &X509 {
        self.chain.first().unwrap()
    }

    /// Get a reference to the full [`openssl::x509::X509`] chain for the certificate.
    pub fn x509_chain(&self) -> &[X509] {
        self.chain.as_slice()
    }

    /// Calculate the SHA256 digest of the leaf certificate in hex format
    pub fn digest(&self) -> String {
        let digest = self
            .x509()
            .digest(MessageDigest::sha256())
            .expect("digest should always succeed");
        hex::encode(digest)
    }

    /// Revoke this certificate.
    #[instrument(
        level = Level::INFO,
        name = "Certificate::revoke",
        err,
        skip_all,
        fields(self = %self.digest())
    )]
    pub async fn revoke(&self, directory: &Directory) -> Result<()> {
        let der = BASE64.encode(self.to_der()?);
        directory
            .api()
            .revoke_certificate(der, None, &self.private_key, None)
            .await
    }

    /// Revoke this certificate with a reason.
    #[instrument(
        level = Level::INFO,
        name = "Certificate::revoke_with_reason",
        err,
        skip_all,
        fields(self = %self.digest())
    )]
    pub async fn revoke_with_reason(
        &self,
        directory: &Directory,
        reason: RevocationReason,
    ) -> Result<()> {
        let der = BASE64.encode(self.to_der()?);
        directory
            .api()
            .revoke_certificate(der, Some(reason), &self.private_key, None)
            .await
    }
}

/// The possible formats a certificate/private key can be loaded from.
///
/// When loading a certificate, full certificate chains can only be loaded from [`Format::Pem`].
#[derive(Debug)]
pub enum Format<'d> {
    /// Bytes of a PEM encoded x509 certificate or private key
    Pem(&'d [u8]),
    /// Bytes of a DER encoded x509 certificate or private key
    Der(&'d [u8]),
}

impl<'d> TryInto<Vec<X509>> for Format<'d> {
    type Error = openssl::error::ErrorStack;

    fn try_into(self) -> std::result::Result<Vec<X509>, Self::Error> {
        match self {
            Self::Pem(pem) => X509::stack_from_pem(pem),
            Self::Der(der) => Ok(vec![X509::from_der(der)?]),
        }
    }
}

impl<'d> TryInto<PKey<Private>> for Format<'d> {
    type Error = openssl::error::ErrorStack;

    fn try_into(self) -> std::result::Result<PKey<Private>, Self::Error> {
        match self {
            Self::Pem(pem) => PKey::private_key_from_pem(pem),
            Self::Der(der) => PKey::private_key_from_der(der),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        responses::{ErrorType, RevocationReason},
        test::{account, directory, directory_with_dns01_solver, directory_with_http01_solver},
        Error,
    };
    use openssl::{
        pkey::{PKey, Private},
        x509::X509,
    };
    use test_log::test;

    macro_rules! check_subjects {
        ($cert:expr => $($name:expr),+ $(,)?) => {
            {
                let expected = {
                    let mut set = std::collections::HashSet::new();
                    $( set.insert($name.to_owned()); )+
                    set
                };
                let names = $cert
                    .subject_alt_names()
                    .unwrap()
                    .iter()
                    .map(|n| n.dnsname().unwrap().to_owned())
                    .collect::<std::collections::HashSet<_>>();
                assert_eq!(names, expected);
            }
        };
    }

    fn check_key(cert: &X509, key: &PKey<Private>) {
        let cert_key = cert.public_key().unwrap();
        assert!(key.public_eq(&cert_key));
    }

    /// Check that the issuer for a certificate matches the provided issuer
    fn check_issuer(cert: &X509, issuer: &X509) {
        assert_eq!(
            cert.issuer_name()
                .entries()
                .next()
                .unwrap()
                .data()
                .as_utf8()
                .unwrap()
                .to_string(),
            issuer
                .subject_name()
                .entries()
                .next()
                .unwrap()
                .data()
                .as_utf8()
                .unwrap()
                .to_string()
        );
    }

    #[test(tokio::test)]
    async fn obtain_no_identifiers() {
        let directory = directory().await;
        let account = account(directory).await;

        let error = account.certificate().obtain().await.unwrap_err();
        assert!(matches!(error, Error::MissingIdentifiers));
    }

    #[test(tokio::test)]
    async fn obtain_missing_solvers() {
        let directory = directory().await;
        let account = account(directory).await;

        let error = account
            .certificate()
            .add_domain("domain.com")
            .obtain()
            .await
            .unwrap_err();
        assert!(matches!(error, Error::MissingSolver));
    }

    #[test(tokio::test)]
    async fn obtain_blocked_domain() {
        let directory = directory().await;
        let account = account(directory).await;

        let error = account
            .certificate()
            .add_domain("blocked-domain.example")
            .obtain()
            .await
            .unwrap_err();

        let Error::Server(error) = error else { panic!("expected Error::Server") };
        assert_eq!(error.type_, ErrorType::RejectedIdentifier);
        assert_eq!(error.status.unwrap(), 400);
        assert!(error.detail.unwrap().contains("blocked-domain.example"));
    }

    #[test(tokio::test)]
    async fn obtain_single_domain() {
        let directory = directory_with_http01_solver().await;
        let account = account(directory).await;

        let certificate = account
            .certificate()
            .add_domain("single.com")
            .obtain()
            .await
            .unwrap();

        assert_eq!(certificate.chain.len(), 2);
        let issued = certificate.chain.first().unwrap();
        let issuer = certificate.chain.last().unwrap();

        check_subjects!(issued => "single.com");
        check_issuer(issued, issuer);
        check_key(issued, &certificate.private_key);
    }

    #[test(tokio::test)]
    async fn obtain_multiple_domains() {
        let directory = directory_with_http01_solver().await;
        let account = account(directory).await;

        let certificate = account
            .certificate()
            .add_domain("one.multiple.com")
            .add_domain("two.multiple.com")
            .add_domain("three.multiple.com")
            .obtain()
            .await
            .unwrap();

        assert_eq!(certificate.chain.len(), 2);
        let issued = certificate.chain.first().unwrap();
        let issuer = certificate.chain.last().unwrap();

        check_subjects!(issued => "one.multiple.com", "two.multiple.com", "three.multiple.com");
        check_issuer(issued, issuer);
        check_key(issued, &certificate.private_key);
    }

    #[test(tokio::test)]
    async fn obtain_wildcard() {
        let directory = directory_with_dns01_solver().await;
        let account = account(directory).await;

        let certificate = account
            .certificate()
            .add_domain("*.wildcard.com")
            .obtain()
            .await
            .unwrap();

        assert_eq!(certificate.chain.len(), 2);
        let issued = certificate.chain.first().unwrap();
        let issuer = certificate.chain.last().unwrap();

        check_subjects!(issued => "*.wildcard.com");
        check_issuer(issued, issuer);
        check_key(issued, &certificate.private_key);
    }

    #[test(tokio::test)]
    async fn obtain_wildcard_without_dns01() {
        let directory = directory_with_http01_solver().await;
        let account = account(directory).await;

        let error = account
            .certificate()
            .add_domain("*.failure.wildcard.com")
            .obtain()
            .await
            .unwrap_err();
        assert!(matches!(error, Error::MissingSolver));
    }

    #[test(tokio::test)]
    async fn obtain_and_revoke_from_account() {
        let directory = directory_with_http01_solver().await;
        let account = account(directory).await;

        let certificate = account
            .certificate()
            .add_domain("revoke.com")
            .obtain()
            .await
            .unwrap();

        account
            .revoke_certificate(certificate.x509())
            .await
            .unwrap();
    }

    #[test(tokio::test)]
    async fn obtain_and_revoke_with_reason_from_account() {
        let directory = directory_with_http01_solver().await;
        let account = account(directory).await;

        let certificate = account
            .certificate()
            .add_domain("reason.revoke.com")
            .obtain()
            .await
            .unwrap();

        account
            .revoke_certificate_with_reason(certificate.x509(), RevocationReason::Superseded)
            .await
            .unwrap();
    }

    #[test(tokio::test)]
    async fn obtain_and_revoke_from_certificate() {
        let directory = directory_with_http01_solver().await;
        let account = account(directory.clone()).await;

        let certificate = account
            .certificate()
            .add_domain("reason.revoke.com")
            .obtain()
            .await
            .unwrap();

        certificate.revoke(&directory).await.unwrap();
    }

    #[test(tokio::test)]
    async fn obtain_and_revoke_with_reason_from_certificate() {
        let directory = directory_with_http01_solver().await;
        let account = account(directory.clone()).await;

        let certificate = account
            .certificate()
            .add_domain("reason.revoke.com")
            .obtain()
            .await
            .unwrap();

        certificate
            .revoke_with_reason(&directory, RevocationReason::Superseded)
            .await
            .unwrap();
    }

    #[test(tokio::test)]
    async fn obtain_and_renew_single_domain() {
        let directory = directory_with_http01_solver().await;
        let account = account(directory).await;

        let certificate = account
            .certificate()
            .add_domain("renew.me")
            .obtain()
            .await
            .unwrap();

        account.renew_certificate(certificate).await.unwrap();
    }

    #[test(tokio::test)]
    async fn obtain_and_renew_multiple_domains() {
        let directory = directory_with_http01_solver().await;
        let account = account(directory).await;

        let certificate = account
            .certificate()
            .add_domain("one.renew.me")
            .add_domain("two.renew.me")
            .add_domain("three.renew.me")
            .obtain()
            .await
            .unwrap();

        account.renew_certificate(certificate).await.unwrap();
    }

    #[test(tokio::test)]
    async fn obtain_and_renew_wildcard_domain() {
        let directory = directory_with_dns01_solver().await;
        let account = account(directory).await;

        let certificate = account
            .certificate()
            .add_domain("*.renew.me")
            .obtain()
            .await
            .unwrap();

        account.renew_certificate(certificate).await.unwrap();
    }
}