certutils 0.1.1

High-level X.509 certificate, CSR, and key utilities backed by AWS-LC
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
use std::net::IpAddr;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use crate::{
    ffi, DistinguishedName, DnsName, Error, PrivateKey, PublicKey, Result, SubjectAltName,
};

/// The validity interval of an X.509 certificate.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Validity {
    not_before: SystemTime,
    not_after: SystemTime,
}

impl Validity {
    /// Creates an interval after checking that its bounds are ordered.
    pub fn new(not_before: SystemTime, not_after: SystemTime) -> Result<Self> {
        let not_before = system_time_to_timestamp(not_before)?;
        let not_after = system_time_to_timestamp(not_after)?;
        if not_before > not_after {
            return Err(invalid_input(
                "validity",
                "not_before must not exceed not_after",
            ));
        }
        Ok(Self {
            not_before: timestamp_to_system_time(not_before)?,
            not_after: timestamp_to_system_time(not_after)?,
        })
    }

    /// Returns the inclusive lower validity bound.
    pub fn not_before(&self) -> SystemTime {
        self.not_before
    }

    /// Returns the inclusive upper validity bound.
    pub fn not_after(&self) -> SystemTime {
        self.not_after
    }
}

/// A positive X.509 certificate serial number.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct SerialNumber(Vec<u8>);

impl SerialNumber {
    /// Validates big-endian serial-number bytes.
    pub fn new(bytes: impl Into<Vec<u8>>) -> Result<Self> {
        let bytes = bytes.into();
        if bytes.is_empty() || bytes.len() > 20 {
            return Err(invalid_input("serial number", "must contain 1 to 20 bytes"));
        }
        if bytes.iter().all(|byte| *byte == 0) {
            return Err(invalid_input("serial number", "must be positive"));
        }
        if bytes.len() > 1 && bytes[0] == 0 {
            return Err(invalid_input(
                "serial number",
                "must not contain leading zero bytes",
            ));
        }
        if bytes.len() == 20 && bytes[0] & 0x80 != 0 {
            return Err(invalid_input(
                "serial number",
                "positive DER encoding must not exceed 20 bytes",
            ));
        }
        Ok(Self(bytes))
    }

    /// Borrows the unsigned, big-endian serial-number bytes.
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }
}

/// An opinionated certificate extension profile.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum CertificateProfile {
    /// An end-entity certificate for TLS servers.
    Server,
    /// An end-entity certificate for TLS clients.
    Client,
    /// An end-entity certificate for TLS servers and clients.
    ServerAndClient,
    /// A certificate authority, optionally constrained to a maximum path length.
    CertificateAuthority {
        /// The maximum number of subordinate CA certificates in a valid path.
        path_length: Option<u8>,
    },
}

/// An owned X.509 certificate.
///
/// Accessors such as [`Certificate::subject`] re-parse the certificate on
/// each call; use [`Certificate::details`] to read all supported fields in a
/// single pass.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Certificate {
    der: Vec<u8>,
}

/// A generated self-signed certificate and its private key.
///
/// This bundle is intended for ephemeral development identities. Persisted or
/// externally managed keys should use [`CertificateBuilder::self_signed`].
#[derive(Debug)]
pub struct SelfSignedCertificate {
    certificate: Certificate,
    private_key: PrivateKey,
}

impl SelfSignedCertificate {
    /// Borrows the generated certificate.
    pub fn certificate(&self) -> &Certificate {
        &self.certificate
    }

    /// Borrows the generated private key.
    pub fn private_key(&self) -> &PrivateKey {
        &self.private_key
    }

    /// Separates the bundle into its certificate and private key.
    pub fn into_parts(self) -> (Certificate, PrivateKey) {
        (self.certificate, self.private_key)
    }
}

impl Certificate {
    /// Starts a certificate builder with required fields.
    pub fn builder(
        subject: DistinguishedName,
        validity: Validity,
        profile: CertificateProfile,
    ) -> CertificateBuilder {
        CertificateBuilder::new(subject, validity, profile)
    }

    /// Parses and validates a DER-encoded certificate.
    ///
    /// Signature algorithms must be ECDSA with SHA-256 or SHA-384, or RSA
    /// PKCS#1 v1.5 with SHA-256 or SHA-384. The public key must satisfy the
    /// requirements of [`PublicKey::from_spki_der`]. Distinguished names and
    /// subject alternative names are validated when read.
    pub fn from_der(der: &[u8]) -> Result<Self> {
        let certificate = parse_certificate(der)?;
        let der = certificate.to_der().map_err(map_certificate_error)?;
        Ok(Self { der })
    }

    /// Parses and validates a PEM-encoded certificate.
    ///
    /// Applies the same validation as [`Certificate::from_der`].
    /// Requires one header-free `CERTIFICATE` block at the start of the input,
    /// followed only by optional ASCII whitespace.
    pub fn from_pem(pem: &str) -> Result<Self> {
        let certificate = ffi::Certificate::from_pem(pem).map_err(map_certificate_pem_error)?;
        let der = certificate.to_der().map_err(map_certificate_error)?;
        Ok(Self { der })
    }

    /// Borrows the canonical DER encoding.
    pub fn as_der(&self) -> &[u8] {
        &self.der
    }

    /// Encodes the certificate as PEM.
    pub fn to_pem(&self) -> Result<String> {
        parse_certificate(&self.der)?
            .to_pem()
            .map_err(map_certificate_pem_error)
    }

    /// Parses a PEM certificate chain bundle into one certificate per block.
    ///
    /// Each block must be a well-formed, header-free `CERTIFICATE` PEM,
    /// separated only by ASCII whitespace. At least one certificate is required,
    /// the first block must start at the beginning of the input, and only ASCII
    /// whitespace may follow the final block.
    /// Each certificate is validated as in [`Certificate::from_pem`].
    ///
    /// This matches the `application/pem-certificate-chain` body an ACME
    /// server returns from its certificate URL (leaf first, then
    /// intermediates); the ordering is preserved but not otherwise enforced.
    pub fn from_pem_chain(pem: &str) -> Result<Vec<Self>> {
        let certificates =
            ffi::Certificate::from_pem_chain(pem).map_err(map_certificate_pem_error)?;
        certificates.iter().map(certificate_from_ffi).collect()
    }

    /// Encodes a slice of certificates as a concatenated PEM chain bundle.
    ///
    /// The certificates appear in slice order; the result parses with
    /// [`Certificate::from_pem_chain`].
    pub fn certificates_to_pem(certificates: &[Certificate]) -> Result<String> {
        let mut output = String::new();
        for certificate in certificates {
            output.push_str(&certificate.to_pem()?);
        }
        Ok(output)
    }

    /// Returns the subject distinguished name.
    pub fn subject(&self) -> Result<DistinguishedName> {
        parse_certificate(&self.der)
            .and_then(|certificate| certificate.subject().map_err(map_certificate_error))
            .and_then(DistinguishedName::from_ffi)
    }

    /// Returns the issuer distinguished name.
    pub fn issuer(&self) -> Result<DistinguishedName> {
        parse_certificate(&self.der)
            .and_then(|certificate| certificate.issuer().map_err(map_certificate_error))
            .and_then(DistinguishedName::from_ffi)
    }

    /// Returns the certificate's subject alternative names.
    pub fn subject_alt_names(&self) -> Result<Vec<SubjectAltName>> {
        parse_certificate(&self.der)?
            .subject_alt_names()
            .map_err(map_certificate_error)?
            .into_iter()
            .map(SubjectAltName::from_ffi)
            .collect()
    }

    /// Returns the certificate's public key.
    pub fn public_key(&self) -> Result<PublicKey> {
        public_key_from_ffi(&parse_certificate(&self.der)?)
    }

    /// Returns the certificate validity interval.
    pub fn validity(&self) -> Result<Validity> {
        let (not_before, not_after) = parse_certificate(&self.der)?
            .validity()
            .map_err(map_certificate_error)?;
        Validity::new(
            timestamp_to_system_time(not_before)?,
            timestamp_to_system_time(not_after)?,
        )
    }

    /// Returns the certificate serial number.
    pub fn serial_number(&self) -> Result<SerialNumber> {
        serial_number_from_ffi(&parse_certificate(&self.der)?)
    }

    /// Parses the certificate once and returns all supported fields.
    ///
    /// Reading several fields from the returned snapshot is cheaper than
    /// calling the individual accessors, each of which re-parses the
    /// certificate.
    pub fn details(&self) -> Result<CertificateDetails> {
        let certificate = parse_certificate(&self.der)?;
        let subject = certificate.subject().map_err(map_certificate_error)?;
        let issuer = certificate.issuer().map_err(map_certificate_error)?;
        let subject_alt_names = certificate
            .subject_alt_names()
            .map_err(map_certificate_error)?
            .into_iter()
            .map(SubjectAltName::from_ffi)
            .collect::<Result<Vec<_>>>()?;
        let (not_before, not_after) = certificate.validity().map_err(map_certificate_error)?;
        Ok(CertificateDetails {
            subject: DistinguishedName::from_ffi(subject)?,
            issuer: DistinguishedName::from_ffi(issuer)?,
            subject_alt_names,
            public_key: public_key_from_ffi(&certificate)?,
            validity: Validity::new(
                timestamp_to_system_time(not_before)?,
                timestamp_to_system_time(not_after)?,
            )?,
            serial_number: serial_number_from_ffi(&certificate)?,
        })
    }

    /// Verifies the certificate signature using an issuer public key.
    pub fn verify_signature(&self, issuer_key: &PublicKey) -> Result<()> {
        let certificate = parse_certificate(&self.der)?;
        let issuer_key = issuer_key.to_ffi()?;
        certificate
            .verify_signature(&issuer_key)
            .map_err(map_certificate_error)
    }

    pub(crate) fn to_ffi(&self) -> Result<ffi::Certificate> {
        parse_certificate(&self.der)
    }
}

/// The supported fields of an X.509 certificate, parsed in one pass.
///
/// Returned by [`Certificate::details`].
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CertificateDetails {
    subject: DistinguishedName,
    issuer: DistinguishedName,
    subject_alt_names: Vec<SubjectAltName>,
    public_key: PublicKey,
    validity: Validity,
    serial_number: SerialNumber,
}

impl CertificateDetails {
    /// Returns the subject distinguished name.
    pub fn subject(&self) -> &DistinguishedName {
        &self.subject
    }

    /// Returns the issuer distinguished name.
    pub fn issuer(&self) -> &DistinguishedName {
        &self.issuer
    }

    /// Returns the certificate's subject alternative names.
    pub fn subject_alt_names(&self) -> &[SubjectAltName] {
        &self.subject_alt_names
    }

    /// Returns the certificate's public key.
    pub fn public_key(&self) -> &PublicKey {
        &self.public_key
    }

    /// Returns the certificate validity interval.
    pub fn validity(&self) -> Validity {
        self.validity
    }

    /// Returns the certificate serial number.
    pub fn serial_number(&self) -> &SerialNumber {
        &self.serial_number
    }
}

/// Builds a certificate using a constrained set of secure extension profiles.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CertificateBuilder {
    subject: DistinguishedName,
    validity: Validity,
    profile: CertificateProfile,
    serial_number: Option<SerialNumber>,
    subject_alt_names: Vec<SubjectAltName>,
}

impl CertificateBuilder {
    /// Creates a builder with required certificate fields.
    pub fn new(
        subject: DistinguishedName,
        validity: Validity,
        profile: CertificateProfile,
    ) -> Self {
        Self {
            subject,
            validity,
            profile,
            serial_number: None,
            subject_alt_names: Vec::new(),
        }
    }

    /// Uses a caller-provided serial number instead of generating one.
    pub fn serial_number(mut self, serial_number: SerialNumber) -> Self {
        self.serial_number = Some(serial_number);
        self
    }

    /// Appends a subject alternative name.
    pub fn subject_alt_name(mut self, name: SubjectAltName) -> Self {
        self.subject_alt_names.push(name);
        self
    }

    /// Appends a validated DNS subject alternative name.
    pub fn dns_name(self, name: DnsName) -> Self {
        self.subject_alt_name(SubjectAltName::Dns(name))
    }

    /// Appends an IP address subject alternative name.
    pub fn ip_address(self, address: IpAddr) -> Self {
        self.subject_alt_name(SubjectAltName::Ip(address))
    }

    /// Generates a private key and issues a self-signed certificate with it.
    pub fn generate_self_signed(self) -> Result<SelfSignedCertificate> {
        let private_key = PrivateKey::generate()?;
        let certificate = self.self_signed(&private_key)?;
        Ok(SelfSignedCertificate {
            certificate,
            private_key,
        })
    }

    /// Issues a self-signed certificate using a caller-provided private key.
    pub fn self_signed(self, key: &PrivateKey) -> Result<Certificate> {
        let subject = self.subject.to_ffi()?;
        let subject_alt_names = self.subject_alt_names_ffi();
        let params = self.ffi_params(&subject, &subject_alt_names)?;
        let certificate =
            ffi::Certificate::self_signed(params, key.as_ffi()).map_err(map_certificate_error)?;
        certificate_from_ffi(&certificate)
    }

    /// Issues a certificate for `subject_key` using a capable issuer certificate and key.
    ///
    /// The issuer must be a certificate authority, its key usage must permit
    /// certificate signing when present, and its path-length constraint must
    /// permit issuing a subordinate CA.
    ///
    /// The issuer may be generated in memory or loaded from an existing CA
    /// certificate and PKCS#8 private key with [`Certificate::from_pem`] and
    /// [`PrivateKey::from_pkcs8_pem`].
    pub fn signed_by(
        self,
        subject_key: &PublicKey,
        issuer: &Certificate,
        issuer_key: &PrivateKey,
    ) -> Result<Certificate> {
        if issuer.public_key()? != issuer_key.public_key()? {
            return Err(invalid_input(
                "issuer key",
                "does not match the issuer certificate",
            ));
        }
        let issuer = parse_certificate(issuer.as_der())?;
        let subordinate_ca = matches!(
            self.profile,
            CertificateProfile::CertificateAuthority { .. }
        );
        if !issuer.can_issue(subordinate_ca) {
            return Err(invalid_input(
                "issuer certificate",
                "is not authorized to issue the requested certificate",
            ));
        }
        let subject = self.subject.to_ffi()?;
        let subject_alt_names = self.subject_alt_names_ffi();
        let params = self.ffi_params(&subject, &subject_alt_names)?;
        let subject_key = subject_key.to_ffi()?;
        let certificate =
            ffi::Certificate::signed_by(params, &subject_key, &issuer, issuer_key.as_ffi())
                .map_err(map_certificate_error)?;
        certificate_from_ffi(&certificate)
    }

    fn subject_alt_names_ffi(&self) -> Vec<ffi::SubjectAlternativeName> {
        self.subject_alt_names
            .iter()
            .map(SubjectAltName::to_ffi)
            .collect()
    }

    fn ffi_params<'a>(
        &'a self,
        subject: &'a ffi::DistinguishedName,
        subject_alt_names: &'a [ffi::SubjectAlternativeName],
    ) -> Result<ffi::x509::CertificateParams<'a>> {
        if self.subject.entries().len() == 0 {
            if matches!(
                self.profile,
                CertificateProfile::CertificateAuthority { .. }
            ) {
                return Err(invalid_input(
                    "subject",
                    "certificate authorities require a non-empty subject",
                ));
            }
            if subject_alt_names.is_empty() {
                return Err(invalid_input(
                    "subject",
                    "an empty subject requires at least one subject alternative name",
                ));
            }
        }
        Ok(ffi::x509::CertificateParams {
            subject,
            not_before: system_time_to_timestamp(self.validity.not_before)?,
            not_after: system_time_to_timestamp(self.validity.not_after)?,
            serial_number: self.serial_number.as_ref().map(SerialNumber::as_bytes),
            subject_alt_names,
            profile: match self.profile {
                CertificateProfile::Server => ffi::x509::CertificateProfile::Server,
                CertificateProfile::Client => ffi::x509::CertificateProfile::Client,
                CertificateProfile::ServerAndClient => {
                    ffi::x509::CertificateProfile::ServerAndClient
                }
                CertificateProfile::CertificateAuthority { path_length } => {
                    ffi::x509::CertificateProfile::CertificateAuthority { path_length }
                }
            },
        })
    }
}

fn parse_certificate(der: &[u8]) -> Result<ffi::Certificate> {
    ffi::Certificate::from_der(der).map_err(map_certificate_error)
}

fn public_key_from_ffi(certificate: &ffi::Certificate) -> Result<PublicKey> {
    let key = certificate.public_key().map_err(map_certificate_error)?;
    let der = key.to_spki_der().map_err(map_certificate_error)?;
    PublicKey::from_spki_der(&der)
}

fn serial_number_from_ffi(certificate: &ffi::Certificate) -> Result<SerialNumber> {
    let bytes = certificate.serial_number().map_err(map_certificate_error)?;
    SerialNumber::new(bytes).map_err(|_| Error::InvalidEncoding {
        encoding: "X.509 certificate serial number",
    })
}

fn certificate_from_ffi(certificate: &ffi::Certificate) -> Result<Certificate> {
    certificate
        .to_der()
        .map(|der| Certificate { der })
        .map_err(map_certificate_error)
}

fn map_certificate_error(error: ffi::Error) -> Error {
    match error {
        ffi::Error::InvalidEncoding => Error::InvalidEncoding {
            encoding: "X.509 DER",
        },
        ffi::Error::UnsupportedAlgorithm => Error::Unsupported {
            feature: "signature algorithm",
        },
        ffi::Error::UnsupportedNameAttribute => Error::Unsupported {
            feature: "distinguished-name attribute",
        },
        ffi::Error::UnsupportedSubjectAltName => Error::Unsupported {
            feature: "subject alternative name",
        },
        ffi::Error::Verification => Error::Verification,
        ffi::Error::Allocation | ffi::Error::Crypto => Error::Crypto,
    }
}

fn map_certificate_pem_error(error: ffi::Error) -> Error {
    match error {
        ffi::Error::InvalidEncoding => Error::InvalidEncoding {
            encoding: "X.509 PEM",
        },
        error => map_certificate_error(error),
    }
}

pub(crate) fn system_time_to_timestamp(time: SystemTime) -> Result<i64> {
    let timestamp = match time.duration_since(UNIX_EPOCH) {
        Ok(duration) => i128::from(duration.as_secs()),
        Err(error) => {
            let duration = error.duration();
            -i128::from(duration.as_secs()) - i128::from(duration.subsec_nanos() != 0)
        }
    };
    i64::try_from(timestamp)
        .map_err(|_| invalid_input("validity", "time is outside the supported range"))
}

fn timestamp_to_system_time(timestamp: i64) -> Result<SystemTime> {
    let duration = Duration::from_secs(timestamp.unsigned_abs());
    let time = if timestamp >= 0 {
        UNIX_EPOCH.checked_add(duration)
    } else {
        UNIX_EPOCH.checked_sub(duration)
    };
    time.ok_or_else(|| invalid_input("validity", "time is outside the supported range"))
}

fn invalid_input(field: &'static str, reason: &str) -> Error {
    Error::InvalidInput {
        field,
        reason: reason.to_owned(),
    }
}

#[cfg(test)]
mod tests {
    use std::net::{IpAddr, Ipv4Addr};
    use std::time::{Duration, UNIX_EPOCH};

    use super::{Certificate, CertificateProfile, SerialNumber, Validity};
    use crate::{
        CertificateVerifier, DistinguishedName, DnsName, Error, PrivateKey, RsaKeySize,
        SubjectAltName, VerificationOptions, VerificationPurpose,
    };

    // Test-only CA generated with LibreSSL, an independent implementation.
    const OPENSSL_CA_CERTIFICATE: &str = "-----BEGIN CERTIFICATE-----\n\
MIIBYjCCAQigAwIBAgIJANWygVGoL0iVMAoGCCqGSM49BAMCMCQxIjAgBgNVBAMM\n\
GWNlcnR1dGlscyBJbnRlcm9wIFRlc3QgQ0EwHhcNMjYwOTE2MDIzMDAyWhcNMzYw\n\
OTEzMDIzMDAyWjAkMSIwIAYDVQQDDBljZXJ0dXRpbHMgSW50ZXJvcCBUZXN0IENB\n\
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEY+uYXt9+foU3q/GQIvTK/RgZ2TJc\n\
hUAKQLMZ+vJSa5FM5U93sA+rU5AS4lkQiQbX4/np4rqV9wSy+PYej0hSyaMjMCEw\n\
DwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMCAQYwCgYIKoZIzj0EAwIDSAAw\n\
RQIgT0fV1145TVtL5O8qUvk0WfyE/82t9XEKVUF8eKbS32wCIQDE4pbKuo5Ir51g\n\
dB2uVS88XY8IDP9e60omlOXI37JzoQ==\n\
-----END CERTIFICATE-----\n";

    // Test-only PKCS#8 key for `OPENSSL_CA_CERTIFICATE`.
    const OPENSSL_CA_PRIVATE_KEY: &str = "-----BEGIN PRIVATE KEY-----\n\
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg2mpOOCkX/OonrPFN\n\
nyw/slv7SLiVbvGtXn7x45f5EmehRANCAARj65he335+hTer8ZAi9Mr9GBnZMlyF\n\
QApAsxn68lJrkUzlT3ewD6tTkBLiWRCJBtfj+eniupX3BLL49h6PSFLJ\n\
-----END PRIVATE KEY-----\n";

    #[test]
    fn validates_validity_bounds() {
        assert!(matches!(
            Validity::new(UNIX_EPOCH + Duration::from_secs(2), UNIX_EPOCH),
            Err(Error::InvalidInput {
                field: "validity",
                ..
            })
        ));
        for (start, end) in [
            (UNIX_EPOCH, UNIX_EPOCH),
            (
                UNIX_EPOCH - Duration::from_secs(2),
                UNIX_EPOCH - Duration::from_secs(1),
            ),
            (
                UNIX_EPOCH - Duration::from_secs(1),
                UNIX_EPOCH + Duration::from_secs(1),
            ),
        ] {
            let validity = Validity::new(start, end).unwrap();
            assert_eq!(validity.not_before(), start);
            assert_eq!(validity.not_after(), end);
        }
    }

    #[test]
    fn validates_serial_number_encoding_boundaries() {
        for bytes in [
            vec![],
            vec![0],
            vec![0; 20],
            vec![0, 1],
            vec![1; 21],
            vec![0x80; 20],
        ] {
            assert!(matches!(
                SerialNumber::new(bytes),
                Err(Error::InvalidInput {
                    field: "serial number",
                    ..
                })
            ));
        }
        let key = PrivateKey::generate().unwrap();
        let builder = Certificate::builder(
            DistinguishedName::builder()
                .common_name("Serial test")
                .build()
                .unwrap(),
            Validity::new(UNIX_EPOCH, UNIX_EPOCH + Duration::from_secs(1)).unwrap(),
            CertificateProfile::Server,
        );
        for bytes in [vec![1], vec![0x80], vec![0xff; 19], vec![0x7f; 20]] {
            let serial = SerialNumber::new(bytes.clone()).unwrap();
            assert_eq!(serial.as_bytes(), bytes);
            let certificate = builder
                .clone()
                .serial_number(serial.clone())
                .self_signed(&key)
                .unwrap();
            let parsed = Certificate::from_der(certificate.as_der()).unwrap();
            assert_eq!(parsed.serial_number().unwrap(), serial);
            parsed.verify_signature(&key.public_key().unwrap()).unwrap();
        }
    }

    #[test]
    fn builds_and_reads_a_self_signed_certificate() {
        let subject = DistinguishedName::builder()
            .common_name("localhost")
            .build()
            .unwrap();
        let validity = Validity::new(
            UNIX_EPOCH + Duration::from_secs(1_700_000_000),
            UNIX_EPOCH + Duration::from_secs(1_800_000_000),
        )
        .unwrap();
        let identity = Certificate::builder(subject.clone(), validity, CertificateProfile::Server)
            .serial_number(SerialNumber::new([1]).unwrap())
            .dns_name(DnsName::new("localhost").unwrap())
            .generate_self_signed()
            .unwrap();
        let certificate = identity.certificate();

        certificate
            .verify_signature(&identity.private_key().public_key().unwrap())
            .unwrap();
        assert_eq!(certificate.subject().unwrap(), subject);
        assert_eq!(certificate.issuer().unwrap(), subject);
        assert_eq!(certificate.validity().unwrap(), validity);
        assert_eq!(certificate.serial_number().unwrap().as_bytes(), &[1]);
        let pem = certificate.to_pem().unwrap();
        assert_eq!(Certificate::from_pem(&pem).unwrap(), *certificate);
    }

    #[test]
    fn reads_all_fields_in_one_pass() {
        let subject = DistinguishedName::builder()
            .common_name("localhost")
            .build()
            .unwrap();
        let validity = Validity::new(
            UNIX_EPOCH + Duration::from_secs(1_700_000_000),
            UNIX_EPOCH + Duration::from_secs(1_800_000_000),
        )
        .unwrap();
        let names = vec![
            SubjectAltName::Dns(DnsName::new("localhost").unwrap()),
            SubjectAltName::Ip(IpAddr::V4(Ipv4Addr::LOCALHOST)),
        ];
        let identity = Certificate::builder(subject.clone(), validity, CertificateProfile::Server)
            .serial_number(SerialNumber::new([0x01, 0x02]).unwrap())
            .subject_alt_name(names[0].clone())
            .subject_alt_name(names[1].clone())
            .generate_self_signed()
            .unwrap();

        let details = identity.certificate().details().unwrap();
        assert_eq!(details.subject(), &subject);
        assert_eq!(details.issuer(), &subject);
        assert_eq!(details.subject_alt_names(), names.as_slice());
        assert_eq!(
            details.public_key(),
            &identity.private_key().public_key().unwrap()
        );
        assert_eq!(details.validity(), validity);
        assert_eq!(details.serial_number().as_bytes(), &[0x01, 0x02]);
    }

    #[test]
    fn issues_a_certificate_from_an_openssl_ca() {
        let issuer = Certificate::from_pem(OPENSSL_CA_CERTIFICATE).unwrap();
        let issuer_key = PrivateKey::from_pkcs8_pem(OPENSSL_CA_PRIVATE_KEY).unwrap();
        assert_eq!(
            issuer.public_key().unwrap(),
            issuer_key.public_key().unwrap()
        );

        let validity = Validity::new(
            UNIX_EPOCH + Duration::from_secs(1_700_000_000),
            UNIX_EPOCH + Duration::from_secs(1_800_000_000),
        )
        .unwrap();
        let leaf_key = PrivateKey::generate().unwrap();
        let builder = Certificate::builder(
            DistinguishedName::builder()
                .common_name("leaf.example")
                .build()
                .unwrap(),
            validity,
            CertificateProfile::Server,
        )
        .dns_name(DnsName::new("leaf.example").unwrap());
        assert!(matches!(
            builder
                .clone()
                .signed_by(&leaf_key.public_key().unwrap(), &issuer, &leaf_key),
            Err(Error::InvalidInput {
                field: "issuer key",
                ..
            })
        ));
        let leaf = builder
            .signed_by(&leaf_key.public_key().unwrap(), &issuer, &issuer_key)
            .unwrap();

        leaf.verify_signature(&issuer_key.public_key().unwrap())
            .unwrap();
        assert_eq!(leaf.issuer().unwrap(), issuer.subject().unwrap());

        let options = VerificationOptions::new(VerificationPurpose::TlsServer)
            .at_time(UNIX_EPOCH + Duration::from_secs(1_795_000_000))
            .subject(SubjectAltName::Dns(DnsName::new("leaf.example").unwrap()));
        CertificateVerifier::new([issuer])
            .verify(&leaf, &[], &options)
            .unwrap();
    }

    #[test]
    fn round_trips_a_pem_certificate_chain() {
        let ca = Certificate::from_pem(OPENSSL_CA_CERTIFICATE).unwrap();
        let ca_key = PrivateKey::from_pkcs8_pem(OPENSSL_CA_PRIVATE_KEY).unwrap();
        let validity = Validity::new(
            UNIX_EPOCH + Duration::from_secs(1_700_000_000),
            UNIX_EPOCH + Duration::from_secs(1_800_000_000),
        )
        .unwrap();
        let leaf_key = PrivateKey::generate().unwrap();
        let leaf = Certificate::builder(
            DistinguishedName::builder()
                .common_name("leaf.example")
                .build()
                .unwrap(),
            validity,
            CertificateProfile::Server,
        )
        .dns_name(DnsName::new("leaf.example").unwrap())
        .signed_by(&leaf_key.public_key().unwrap(), &ca, &ca_key)
        .unwrap();

        // Leaf first, then the CA, as in an ACME certificate download.
        let chain = Certificate::certificates_to_pem(&[leaf.clone(), ca.clone()]).unwrap();
        let parsed = Certificate::from_pem_chain(&chain).unwrap();
        assert_eq!(parsed, vec![leaf, ca]);

        // A single certificate is a valid one-element chain.
        assert_eq!(
            Certificate::from_pem_chain(OPENSSL_CA_CERTIFICATE).unwrap(),
            vec![Certificate::from_pem(OPENSSL_CA_CERTIFICATE).unwrap()]
        );

        // Empty, non-certificate, and truncated bundles are rejected.
        assert!(Certificate::from_pem_chain("").is_err());
        assert!(Certificate::from_pem_chain("not PEM").is_err());
        let mut truncated = chain.clone();
        truncated.truncate(truncated.len() - 10);
        assert!(Certificate::from_pem_chain(&truncated).is_err());
    }

    #[test]
    fn builds_and_reads_an_rsa_self_signed_certificate() {
        let key = PrivateKey::generate_rsa(RsaKeySize::Bits2048).unwrap();
        let subject = DistinguishedName::builder()
            .common_name("localhost")
            .build()
            .unwrap();
        let validity = Validity::new(
            UNIX_EPOCH + Duration::from_secs(1_700_000_000),
            UNIX_EPOCH + Duration::from_secs(1_800_000_000),
        )
        .unwrap();
        let certificate = Certificate::builder(subject, validity, CertificateProfile::Server)
            .dns_name(DnsName::new("localhost").unwrap())
            .self_signed(&key)
            .unwrap();

        let parsed = Certificate::from_der(certificate.as_der()).unwrap();
        assert_eq!(parsed.public_key().unwrap(), key.public_key().unwrap());
        parsed.verify_signature(&key.public_key().unwrap()).unwrap();
    }

    #[test]
    fn rejects_invalid_issuance_profiles() {
        let validity = Validity::new(
            UNIX_EPOCH + Duration::from_secs(1_700_000_000),
            UNIX_EPOCH + Duration::from_secs(1_800_000_000),
        )
        .unwrap();
        let empty_subject = DistinguishedName::builder().build().unwrap();
        assert!(matches!(
            Certificate::builder(empty_subject, validity, CertificateProfile::Server)
                .generate_self_signed(),
            Err(Error::InvalidInput {
                field: "subject",
                ..
            })
        ));
        assert!(matches!(
            Certificate::builder(
                DistinguishedName::builder().build().unwrap(),
                validity,
                CertificateProfile::CertificateAuthority { path_length: None },
            )
            .dns_name(DnsName::new("ca.example").unwrap())
            .generate_self_signed(),
            Err(Error::InvalidInput {
                field: "subject",
                ..
            })
        ));

        let issuer_key = PrivateKey::generate().unwrap();
        let non_ca = Certificate::builder(
            DistinguishedName::builder()
                .common_name("Not a CA")
                .build()
                .unwrap(),
            validity,
            CertificateProfile::Server,
        )
        .self_signed(&issuer_key)
        .unwrap();
        let subject_key = PrivateKey::generate().unwrap().public_key().unwrap();
        assert!(matches!(
            Certificate::builder(
                DistinguishedName::builder()
                    .common_name("Leaf")
                    .build()
                    .unwrap(),
                validity,
                CertificateProfile::Server,
            )
            .signed_by(&subject_key, &non_ca, &issuer_key),
            Err(Error::InvalidInput {
                field: "issuer certificate",
                ..
            })
        ));

        let ca = Certificate::builder(
            DistinguishedName::builder()
                .common_name("Path Length Zero CA")
                .build()
                .unwrap(),
            validity,
            CertificateProfile::CertificateAuthority {
                path_length: Some(0),
            },
        )
        .self_signed(&issuer_key)
        .unwrap();
        assert!(matches!(
            Certificate::builder(
                DistinguishedName::builder()
                    .common_name("Subordinate CA")
                    .build()
                    .unwrap(),
                validity,
                CertificateProfile::CertificateAuthority { path_length: None },
            )
            .signed_by(&subject_key, &ca, &issuer_key),
            Err(Error::InvalidInput {
                field: "issuer certificate",
                ..
            })
        ));
    }
}