webpki/
lib.rs

1// Copyright 2015 Brian Smith.
2//
3// Permission to use, copy, modify, and/or distribute this software for any
4// purpose with or without fee is hereby granted, provided that the above
5// copyright notice and this permission notice appear in all copies.
6//
7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
10// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15//! webpki: Web PKI X.509 Certificate Validation.
16//!
17//! See `EndEntityCert`'s documentation for a description of the certificate
18//! processing steps necessary for a TLS connection.
19//!
20//! # Features
21//!
22//! | Feature | Description |
23//! | ------- | ----------- |
24//! | `alloc` | Enable features that require use of the heap. Currently all RSA signature algorithms require this feature. |
25//! | `std` | Enable features that require libstd. Implies `alloc`. |
26//! | `ring` | Enable use of the *ring* crate for cryptography. |
27//! | `aws-lc-rs` | Enable use of the aws-lc-rs crate for cryptography. Previously this feature was named `aws_lc_rs`. |
28
29#![no_std]
30#![warn(elided_lifetimes_in_paths, unreachable_pub, clippy::use_self)]
31#![deny(missing_docs, clippy::as_conversions)]
32#![allow(
33    clippy::len_without_is_empty,
34    clippy::manual_let_else,
35    clippy::new_without_default,
36    clippy::single_match,
37    clippy::single_match_else,
38    clippy::type_complexity,
39    clippy::upper_case_acronyms
40)]
41// Enable documentation for all features on docs.rs
42#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
43
44#[cfg(any(feature = "std", test))]
45extern crate std;
46
47#[cfg(any(test, feature = "alloc"))]
48#[cfg_attr(test, macro_use)]
49extern crate alloc;
50
51#[macro_use]
52mod der;
53
54#[cfg(feature = "aws-lc-rs")]
55mod aws_lc_rs_algs;
56mod cert;
57mod end_entity;
58mod error;
59#[cfg(feature = "ring")]
60mod ring_algs;
61mod rpk_entity;
62mod signed_data;
63mod subject_name;
64mod time;
65mod trust_anchor;
66
67mod crl;
68mod verify_cert;
69mod x509;
70
71#[cfg(test)]
72pub(crate) mod test_utils;
73
74pub use {
75    cert::Cert,
76    crl::{
77        BorrowedCertRevocationList, BorrowedRevokedCert, CertRevocationList, ExpirationPolicy,
78        RevocationCheckDepth, RevocationOptions, RevocationOptionsBuilder, RevocationReason,
79        UnknownStatusPolicy,
80    },
81    end_entity::EndEntityCert,
82    error::{
83        DerTypeId, Error, InvalidNameContext, UnsupportedSignatureAlgorithmContext,
84        UnsupportedSignatureAlgorithmForPublicKeyContext,
85    },
86    rpk_entity::RawPublicKeyEntity,
87    trust_anchor::anchor_from_trusted_cert,
88    verify_cert::{
89        ExtendedKeyUsageValidator, KeyPurposeId, KeyPurposeIdIter, KeyUsage,
90        RequiredEkuNotFoundContext, VerifiedPath,
91    },
92};
93
94#[cfg(feature = "alloc")]
95pub use crl::{OwnedCertRevocationList, OwnedRevokedCert};
96
97#[cfg(feature = "ring")]
98/// Signature verification algorithm implementations using the *ring* crypto library.
99pub mod ring {
100    pub use super::ring_algs::{
101        ECDSA_P256_SHA256, ECDSA_P256_SHA384, ECDSA_P384_SHA256, ECDSA_P384_SHA384, ED25519,
102    };
103
104    #[cfg(feature = "alloc")]
105    pub use super::ring_algs::{
106        RSA_PKCS1_2048_8192_SHA256, RSA_PKCS1_2048_8192_SHA256_ABSENT_PARAMS,
107        RSA_PKCS1_2048_8192_SHA384, RSA_PKCS1_2048_8192_SHA384_ABSENT_PARAMS,
108        RSA_PKCS1_2048_8192_SHA512, RSA_PKCS1_2048_8192_SHA512_ABSENT_PARAMS,
109        RSA_PKCS1_3072_8192_SHA384, RSA_PSS_2048_8192_SHA256_LEGACY_KEY,
110        RSA_PSS_2048_8192_SHA384_LEGACY_KEY, RSA_PSS_2048_8192_SHA512_LEGACY_KEY,
111    };
112}
113
114#[cfg(feature = "aws-lc-rs")]
115/// Signature verification algorithm implementations using the aws-lc-rs crypto library.
116pub mod aws_lc_rs {
117    pub use super::aws_lc_rs_algs::{
118        ECDSA_P256_SHA256, ECDSA_P256_SHA384, ECDSA_P256_SHA512, ECDSA_P384_SHA256,
119        ECDSA_P384_SHA384, ECDSA_P384_SHA512, ECDSA_P521_SHA256, ECDSA_P521_SHA384,
120        ECDSA_P521_SHA512, ED25519, RSA_PKCS1_2048_8192_SHA256,
121        RSA_PKCS1_2048_8192_SHA256_ABSENT_PARAMS, RSA_PKCS1_2048_8192_SHA384,
122        RSA_PKCS1_2048_8192_SHA384_ABSENT_PARAMS, RSA_PKCS1_2048_8192_SHA512,
123        RSA_PKCS1_2048_8192_SHA512_ABSENT_PARAMS, RSA_PKCS1_3072_8192_SHA384,
124        RSA_PSS_2048_8192_SHA256_LEGACY_KEY, RSA_PSS_2048_8192_SHA384_LEGACY_KEY,
125        RSA_PSS_2048_8192_SHA512_LEGACY_KEY,
126    };
127    #[cfg(all(feature = "aws-lc-rs-unstable", not(feature = "aws-lc-rs-fips")))]
128    pub use super::aws_lc_rs_algs::{ML_DSA_44, ML_DSA_65, ML_DSA_87};
129}
130
131/// An array of all the verification algorithms exported by this crate.
132///
133/// This will be empty if the crate is built without the `ring` and `aws-lc-rs` features.
134pub static ALL_VERIFICATION_ALGS: &[&dyn pki_types::SignatureVerificationAlgorithm] = &[
135    #[cfg(feature = "ring")]
136    ring::ECDSA_P256_SHA256,
137    #[cfg(feature = "ring")]
138    ring::ECDSA_P256_SHA384,
139    #[cfg(feature = "ring")]
140    ring::ECDSA_P384_SHA256,
141    #[cfg(feature = "ring")]
142    ring::ECDSA_P384_SHA384,
143    #[cfg(feature = "ring")]
144    ring::ED25519,
145    #[cfg(all(feature = "ring", feature = "alloc"))]
146    ring::RSA_PKCS1_2048_8192_SHA256,
147    #[cfg(all(feature = "ring", feature = "alloc"))]
148    ring::RSA_PKCS1_2048_8192_SHA384,
149    #[cfg(all(feature = "ring", feature = "alloc"))]
150    ring::RSA_PKCS1_2048_8192_SHA512,
151    #[cfg(all(feature = "ring", feature = "alloc"))]
152    ring::RSA_PKCS1_2048_8192_SHA256_ABSENT_PARAMS,
153    #[cfg(all(feature = "ring", feature = "alloc"))]
154    ring::RSA_PKCS1_2048_8192_SHA384_ABSENT_PARAMS,
155    #[cfg(all(feature = "ring", feature = "alloc"))]
156    ring::RSA_PKCS1_2048_8192_SHA512_ABSENT_PARAMS,
157    #[cfg(all(feature = "ring", feature = "alloc"))]
158    ring::RSA_PKCS1_3072_8192_SHA384,
159    #[cfg(all(feature = "ring", feature = "alloc"))]
160    ring::RSA_PSS_2048_8192_SHA256_LEGACY_KEY,
161    #[cfg(all(feature = "ring", feature = "alloc"))]
162    ring::RSA_PSS_2048_8192_SHA384_LEGACY_KEY,
163    #[cfg(all(feature = "ring", feature = "alloc"))]
164    ring::RSA_PSS_2048_8192_SHA512_LEGACY_KEY,
165    #[cfg(feature = "aws-lc-rs")]
166    aws_lc_rs::ECDSA_P256_SHA256,
167    #[cfg(feature = "aws-lc-rs")]
168    aws_lc_rs::ECDSA_P256_SHA384,
169    #[cfg(feature = "aws-lc-rs")]
170    aws_lc_rs::ECDSA_P256_SHA512,
171    #[cfg(feature = "aws-lc-rs")]
172    aws_lc_rs::ECDSA_P384_SHA256,
173    #[cfg(feature = "aws-lc-rs")]
174    aws_lc_rs::ECDSA_P384_SHA384,
175    #[cfg(feature = "aws-lc-rs")]
176    aws_lc_rs::ECDSA_P384_SHA512,
177    #[cfg(feature = "aws-lc-rs")]
178    aws_lc_rs::ECDSA_P521_SHA256,
179    #[cfg(feature = "aws-lc-rs")]
180    aws_lc_rs::ECDSA_P521_SHA384,
181    #[cfg(feature = "aws-lc-rs")]
182    aws_lc_rs::ECDSA_P521_SHA512,
183    #[cfg(feature = "aws-lc-rs")]
184    aws_lc_rs::ED25519,
185    #[cfg(feature = "aws-lc-rs")]
186    aws_lc_rs::RSA_PKCS1_2048_8192_SHA256,
187    #[cfg(feature = "aws-lc-rs")]
188    aws_lc_rs::RSA_PKCS1_2048_8192_SHA384,
189    #[cfg(feature = "aws-lc-rs")]
190    aws_lc_rs::RSA_PKCS1_2048_8192_SHA512,
191    #[cfg(feature = "aws-lc-rs")]
192    aws_lc_rs::RSA_PKCS1_2048_8192_SHA256_ABSENT_PARAMS,
193    #[cfg(feature = "aws-lc-rs")]
194    aws_lc_rs::RSA_PKCS1_2048_8192_SHA384_ABSENT_PARAMS,
195    #[cfg(feature = "aws-lc-rs")]
196    aws_lc_rs::RSA_PKCS1_2048_8192_SHA512_ABSENT_PARAMS,
197    #[cfg(feature = "aws-lc-rs")]
198    aws_lc_rs::RSA_PKCS1_3072_8192_SHA384,
199    #[cfg(feature = "aws-lc-rs")]
200    aws_lc_rs::RSA_PSS_2048_8192_SHA256_LEGACY_KEY,
201    #[cfg(feature = "aws-lc-rs")]
202    aws_lc_rs::RSA_PSS_2048_8192_SHA384_LEGACY_KEY,
203    #[cfg(feature = "aws-lc-rs")]
204    aws_lc_rs::RSA_PSS_2048_8192_SHA512_LEGACY_KEY,
205    #[cfg(all(feature = "aws-lc-rs-unstable", not(feature = "aws-lc-rs-fips")))]
206    aws_lc_rs::ML_DSA_44,
207    #[cfg(all(feature = "aws-lc-rs-unstable", not(feature = "aws-lc-rs-fips")))]
208    aws_lc_rs::ML_DSA_65,
209    #[cfg(all(feature = "aws-lc-rs-unstable", not(feature = "aws-lc-rs-fips")))]
210    aws_lc_rs::ML_DSA_87,
211];
212
213fn public_values_eq(a: untrusted::Input<'_>, b: untrusted::Input<'_>) -> bool {
214    a.as_slice_less_safe() == b.as_slice_less_safe()
215}