certutils 0.1.0

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
use std::net::IpAddr;
use std::time::SystemTime;

use crate::certificate::system_time_to_timestamp;
use crate::{ffi, Certificate, Error, Result, SubjectAltName};

/// The intended use of the certificate being verified.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum VerificationPurpose {
    /// Validates a chain without endpoint key usage or EKU checks.
    ///
    /// This is not sufficient for TLS authentication. The caller is responsible
    /// for deciding whether the certificate is authorized for its intended use.
    ChainOnly,
    /// Validates a non-CA TLS server certificate and its chain's applicable
    /// key usage and server-authentication EKU restrictions.
    ///
    /// A concrete DNS name or IP address must be supplied with
    /// [`VerificationOptions::subject`].
    TlsServer,
    /// Validates a non-CA TLS client certificate and its chain's applicable
    /// key usage and client-authentication EKU restrictions.
    ///
    /// Name matching is optional; client authorization remains the caller's
    /// responsibility.
    TlsClient,
}

/// Options applied while validating a certificate chain.
///
/// Purpose must be selected explicitly; there is no default authentication policy.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct VerificationOptions {
    purpose: VerificationPurpose,
    time: Option<SystemTime>,
    subject: Option<SubjectAltName>,
}

impl VerificationOptions {
    /// Creates options for an explicit purpose at the current time.
    ///
    /// TLS server verification also requires [`Self::subject`].
    pub fn new(purpose: VerificationPurpose) -> Self {
        Self {
            purpose,
            time: None,
            subject: None,
        }
    }

    /// Overrides the verification time.
    pub fn at_time(mut self, time: SystemTime) -> Self {
        self.time = Some(time);
        self
    }

    /// Requires the end-entity certificate to match a DNS name or IP address.
    ///
    /// DNS reference names must be concrete, without wildcards; IP addresses
    /// must use [`SubjectAltName::Ip`]. Matching uses SANs only, never the common
    /// name, and permits only whole-label certificate wildcards.
    pub fn subject(mut self, subject: SubjectAltName) -> Self {
        self.subject = Some(subject);
        self
    }
}

/// Verifies certificate chains against explicit trust anchors.
///
/// This does not perform a TLS handshake, revocation or certificate-transparency
/// checks, fetch certificates, or load system trust roots. TLS applications must
/// also enforce their protocol and authorization requirements.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CertificateVerifier {
    trust_anchors: Vec<Certificate>,
}

impl CertificateVerifier {
    /// Creates a verifier from explicit trust anchors.
    pub fn new(trust_anchors: impl IntoIterator<Item = Certificate>) -> Self {
        Self {
            trust_anchors: trust_anchors.into_iter().collect(),
        }
    }

    /// Verifies an end-entity certificate and any supplied intermediates.
    pub fn verify(
        &self,
        end_entity: &Certificate,
        intermediates: &[Certificate],
        options: &VerificationOptions,
    ) -> Result<()> {
        if options.purpose == VerificationPurpose::TlsServer && options.subject.is_none() {
            return Err(Error::InvalidInput {
                field: "verification subject",
                reason: "TLS server verification requires a DNS name or IP address".to_owned(),
            });
        }
        if let Some(SubjectAltName::Dns(name)) = &options.subject {
            if name.as_str().contains('*') || name.as_str().parse::<IpAddr>().is_ok() {
                return Err(Error::InvalidInput {
                    field: "verification subject",
                    reason: "use a concrete DNS name, or the IP variant for an IP address"
                        .to_owned(),
                });
            }
        }
        let end_entity = end_entity.to_ffi()?;
        let intermediates = intermediates
            .iter()
            .map(Certificate::to_ffi)
            .collect::<Result<Vec<_>>>()?;
        let trust_anchors = self
            .trust_anchors
            .iter()
            .map(Certificate::to_ffi)
            .collect::<Result<Vec<_>>>()?;
        let time = options.time.map(system_time_to_timestamp).transpose()?;
        let subject = options.subject.as_ref().map(|subject| match subject {
            SubjectAltName::Dns(name) => ffi::verify::VerificationSubject::Dns(name.as_str()),
            SubjectAltName::Ip(address) => ffi::verify::VerificationSubject::Ip(*address),
        });

        let purpose = match options.purpose {
            VerificationPurpose::ChainOnly => ffi::verify::Purpose::ChainOnly,
            VerificationPurpose::TlsServer => ffi::verify::Purpose::TlsServer,
            VerificationPurpose::TlsClient => ffi::verify::Purpose::TlsClient,
        };
        ffi::verify::verify_chain(
            &end_entity,
            &intermediates,
            &trust_anchors,
            time,
            subject,
            purpose,
        )
        .map_err(map_verification_error)
    }
}

fn map_verification_error(error: ffi::Error) -> Error {
    match error {
        ffi::Error::Verification => Error::Verification,
        ffi::Error::InvalidEncoding => Error::InvalidEncoding {
            encoding: "X.509 certificate",
        },
        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::Allocation | ffi::Error::Crypto => Error::Crypto,
    }
}

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

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

    #[test]
    fn enforces_verification_purposes() {
        let validity = Validity::new(
            UNIX_EPOCH + Duration::from_secs(1_700_000_000),
            UNIX_EPOCH + Duration::from_secs(1_800_000_000),
        )
        .unwrap();
        let issuer_key = PrivateKey::generate().unwrap();
        let issuer = Certificate::builder(
            DistinguishedName::builder()
                .common_name("Purpose test CA")
                .build()
                .unwrap(),
            validity,
            CertificateProfile::CertificateAuthority { path_length: None },
        )
        .self_signed(&issuer_key)
        .unwrap();
        let verifier = CertificateVerifier::new([issuer.clone()]);
        let key = PrivateKey::generate().unwrap();
        let name = crate::SubjectAltName::Dns(DnsName::new("service.example.com").unwrap());
        for (profile, server, client) in [
            (CertificateProfile::Server, true, false),
            (CertificateProfile::Client, false, true),
            (CertificateProfile::ServerAndClient, true, true),
            (
                CertificateProfile::CertificateAuthority {
                    path_length: Some(0),
                },
                false,
                false,
            ),
        ] {
            let certificate = Certificate::builder(
                DistinguishedName::builder()
                    .common_name("Endpoint")
                    .build()
                    .unwrap(),
                validity,
                profile,
            )
            .subject_alt_name(name.clone())
            .signed_by(&key.public_key().unwrap(), &issuer, &issuer_key)
            .unwrap();
            for (purpose, expected) in [
                (VerificationPurpose::ChainOnly, true),
                (VerificationPurpose::TlsServer, server),
                (VerificationPurpose::TlsClient, client),
            ] {
                let options = VerificationOptions::new(purpose)
                    .at_time(UNIX_EPOCH + Duration::from_secs(1_750_000_000))
                    .subject(name.clone());
                let result = verifier.verify(&certificate, &[], &options);
                if expected {
                    result.unwrap();
                } else {
                    assert!(
                        matches!(result, Err(crate::Error::Verification)),
                        "{profile:?} must be rejected for {purpose:?}"
                    );
                }
            }
            for (purpose, expected) in [
                (VerificationPurpose::ChainOnly, true),
                (VerificationPurpose::TlsClient, client),
            ] {
                let options = VerificationOptions::new(purpose)
                    .at_time(UNIX_EPOCH + Duration::from_secs(1_750_000_000));
                let result = verifier.verify(&certificate, &[], &options);
                if expected {
                    result.unwrap();
                } else {
                    assert!(
                        matches!(result, Err(crate::Error::Verification)),
                        "{profile:?} must be rejected for {purpose:?} without a subject"
                    );
                }
            }
        }
    }

    fn test_server_chain() -> (CertificateVerifier, Certificate, VerificationOptions) {
        let validity = Validity::new(
            UNIX_EPOCH + Duration::from_secs(1_700_000_000),
            UNIX_EPOCH + Duration::from_secs(1_800_000_000),
        )
        .unwrap();
        let ca_key = PrivateKey::generate().unwrap();
        let ca_subject = DistinguishedName::builder()
            .common_name("Test CA")
            .build()
            .unwrap();
        let ca = Certificate::builder(
            ca_subject,
            validity,
            CertificateProfile::CertificateAuthority {
                path_length: Some(0),
            },
        )
        .self_signed(&ca_key)
        .unwrap();

        let leaf_key = PrivateKey::generate().unwrap();
        let leaf = Certificate::builder(
            DistinguishedName::builder()
                .common_name("localhost")
                .build()
                .unwrap(),
            validity,
            CertificateProfile::Server,
        )
        .dns_name(DnsName::new("localhost").unwrap())
        .signed_by(&leaf_key.public_key().unwrap(), &ca, &ca_key)
        .unwrap();
        let options = VerificationOptions::new(VerificationPurpose::TlsServer)
            .at_time(UNIX_EPOCH + Duration::from_secs(1_750_000_000))
            .subject(crate::SubjectAltName::Dns(
                DnsName::new("localhost").unwrap(),
            ));

        let verifier = CertificateVerifier::new([ca]);
        verifier.verify(&leaf, &[], &options).unwrap();
        (verifier, leaf, options)
    }

    #[test]
    fn verifies_only_trusted_matching_current_certificates() {
        let (verifier, leaf, options) = test_server_chain();
        let validity = leaf.validity().unwrap();
        for time in [
            validity.not_before(),
            validity.not_after() - Duration::from_secs(1),
        ] {
            verifier
                .verify(&leaf, &[], &options.clone().at_time(time))
                .unwrap();
        }
        for time in [
            validity.not_before() - Duration::from_secs(1),
            validity.not_after() + Duration::from_secs(1),
        ] {
            assert!(
                matches!(
                    verifier.verify(&leaf, &[], &options.clone().at_time(time)),
                    Err(crate::Error::Verification)
                ),
                "certificate must be rejected at {time:?}"
            );
        }
        let wrong_name = options.clone().subject(crate::SubjectAltName::Dns(
            DnsName::new("other.example").unwrap(),
        ));
        assert!(matches!(
            verifier.verify(&leaf, &[], &wrong_name),
            Err(crate::Error::Verification)
        ));
        assert!(matches!(
            CertificateVerifier::new([]).verify(&leaf, &[], &options),
            Err(crate::Error::Verification)
        ));

        let other_ca = Certificate::builder(
            DistinguishedName::builder()
                .common_name("Test CA")
                .build()
                .unwrap(),
            validity,
            CertificateProfile::CertificateAuthority {
                path_length: Some(0),
            },
        )
        .generate_self_signed()
        .unwrap();
        assert!(matches!(
            CertificateVerifier::new([other_ca.certificate().clone()]).verify(&leaf, &[], &options),
            Err(crate::Error::Verification)
        ));
    }

    #[test]
    fn rejects_invalid_server_reference_identities() {
        let (verifier, leaf, _) = test_server_chain();
        let options = VerificationOptions::new(VerificationPurpose::TlsServer)
            .at_time(UNIX_EPOCH + Duration::from_secs(1_750_000_000));
        for options in [
            options.clone(),
            options.clone().subject(crate::SubjectAltName::Dns(
                DnsName::new("*.example.com").unwrap(),
            )),
            options.subject(crate::SubjectAltName::Dns(
                DnsName::new("127.0.0.1").unwrap(),
            )),
        ] {
            assert!(matches!(
                verifier.verify(&leaf, &[], &options),
                Err(crate::Error::InvalidInput {
                    field: "verification subject",
                    ..
                })
            ));
        }
    }

    #[test]
    fn verifies_a_certificate_signed_by_an_rsa_issuer() {
        let validity = Validity::new(
            UNIX_EPOCH + Duration::from_secs(1_700_000_000),
            UNIX_EPOCH + Duration::from_secs(1_800_000_000),
        )
        .unwrap();
        let ca_key = PrivateKey::generate_rsa(RsaKeySize::Bits2048).unwrap();
        let ca = Certificate::builder(
            DistinguishedName::builder()
                .common_name("RSA Test CA")
                .build()
                .unwrap(),
            validity,
            CertificateProfile::CertificateAuthority { path_length: None },
        )
        .self_signed(&ca_key)
        .unwrap();
        let leaf_key = PrivateKey::generate().unwrap();
        let leaf = Certificate::builder(
            DistinguishedName::builder()
                .common_name("localhost")
                .build()
                .unwrap(),
            validity,
            CertificateProfile::Server,
        )
        .dns_name(DnsName::new("localhost").unwrap())
        .signed_by(&leaf_key.public_key().unwrap(), &ca, &ca_key)
        .unwrap();
        let options = VerificationOptions::new(VerificationPurpose::TlsServer)
            .at_time(UNIX_EPOCH + Duration::from_secs(1_750_000_000))
            .subject(crate::SubjectAltName::Dns(
                DnsName::new("localhost").unwrap(),
            ));

        CertificateVerifier::new([ca])
            .verify(&leaf, &[], &options)
            .unwrap();
    }
}