Skip to main content

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(
31    elided_lifetimes_in_paths,
32    unnameable_types,
33    unreachable_pub,
34    clippy::use_self
35)]
36#![deny(missing_docs, clippy::as_conversions)]
37#![allow(
38    clippy::len_without_is_empty,
39    clippy::manual_let_else,
40    clippy::new_without_default,
41    clippy::single_match,
42    clippy::single_match_else,
43    clippy::type_complexity,
44    clippy::upper_case_acronyms
45)]
46// Enable documentation for all features on docs.rs
47#![cfg_attr(webpki_docsrs, feature(doc_cfg))]
48
49#[cfg(any(feature = "std", test))]
50extern crate std;
51
52#[cfg(any(test, feature = "alloc"))]
53#[cfg_attr(test, macro_use)]
54extern crate alloc;
55
56#[macro_use]
57mod der;
58
59#[cfg(feature = "aws-lc-rs")]
60mod aws_lc_rs_algs;
61mod cert;
62mod end_entity;
63mod error;
64#[cfg(feature = "ring")]
65mod ring_algs;
66mod rpk_entity;
67/// Processing of certificate transparency SCTs.
68pub mod sct;
69mod signed_data;
70mod subject_name;
71mod time;
72mod trust_anchor;
73
74mod crl;
75mod verify_cert;
76mod x509;
77
78#[cfg(test)]
79pub(crate) mod test_utils;
80
81pub use {
82    cert::Cert,
83    crl::{
84        BorrowedCertRevocationList, BorrowedRevokedCert, CertRevocationList, CrlsRequired,
85        ExpirationPolicy, RevocationCheckDepth, RevocationOptions, RevocationOptionsBuilder,
86        RevocationReason, UnknownStatusPolicy,
87    },
88    der::DerIterator,
89    end_entity::EndEntityCert,
90    error::{
91        DerTypeId, Error, InvalidNameContext, UnsupportedSignatureAlgorithmContext,
92        UnsupportedSignatureAlgorithmForPublicKeyContext,
93    },
94    rpk_entity::RawPublicKeyEntity,
95    trust_anchor::anchor_from_trusted_cert,
96    verify_cert::{
97        ExtendedKeyUsageValidator, IntermediateIterator, KeyPurposeId, KeyPurposeIdIter, KeyUsage,
98        RequiredEkuNotFoundContext, VerifiedPath,
99    },
100};
101
102#[cfg(feature = "alloc")]
103pub use trust_anchor::spki_for_anchor;
104
105#[cfg(feature = "alloc")]
106pub use crl::{OwnedCertRevocationList, OwnedRevokedCert};
107
108#[cfg(feature = "ring")]
109/// Signature verification algorithm implementations using the *ring* crypto library.
110pub mod ring {
111    pub use super::ring_algs::{
112        ECDSA_P256_SHA256, ECDSA_P256_SHA384, ECDSA_P384_SHA256, ECDSA_P384_SHA384, ED25519,
113    };
114
115    #[cfg(feature = "alloc")]
116    pub use super::ring_algs::{
117        RSA_PKCS1_2048_8192_SHA256, RSA_PKCS1_2048_8192_SHA256_ABSENT_PARAMS,
118        RSA_PKCS1_2048_8192_SHA384, RSA_PKCS1_2048_8192_SHA384_ABSENT_PARAMS,
119        RSA_PKCS1_2048_8192_SHA512, RSA_PKCS1_2048_8192_SHA512_ABSENT_PARAMS,
120        RSA_PKCS1_3072_8192_SHA384, RSA_PSS_2048_8192_SHA256_LEGACY_KEY,
121        RSA_PSS_2048_8192_SHA384_LEGACY_KEY, RSA_PSS_2048_8192_SHA512_LEGACY_KEY,
122    };
123}
124
125#[cfg(feature = "aws-lc-rs")]
126/// Signature verification algorithm implementations using the aws-lc-rs crypto library.
127pub mod aws_lc_rs {
128    pub use super::aws_lc_rs_algs::{
129        ECDSA_P256_SHA256, ECDSA_P256_SHA384, ECDSA_P256_SHA512, ECDSA_P384_SHA256,
130        ECDSA_P384_SHA384, ECDSA_P384_SHA512, ECDSA_P521_SHA256, ECDSA_P521_SHA384,
131        ECDSA_P521_SHA512, ED25519, ML_DSA_44, ML_DSA_65, ML_DSA_87, RSA_PKCS1_2048_8192_SHA256,
132        RSA_PKCS1_2048_8192_SHA256_ABSENT_PARAMS, RSA_PKCS1_2048_8192_SHA384,
133        RSA_PKCS1_2048_8192_SHA384_ABSENT_PARAMS, RSA_PKCS1_2048_8192_SHA512,
134        RSA_PKCS1_2048_8192_SHA512_ABSENT_PARAMS, RSA_PKCS1_3072_8192_SHA384,
135        RSA_PSS_2048_8192_SHA256_LEGACY_KEY, RSA_PSS_2048_8192_SHA384_LEGACY_KEY,
136        RSA_PSS_2048_8192_SHA512_LEGACY_KEY,
137    };
138}
139
140/// An array of all the verification algorithms exported by this crate.
141///
142/// This will be empty if the crate is built without the `ring` and `aws-lc-rs` features.
143pub static ALL_VERIFICATION_ALGS: &[&dyn pki_types::SignatureVerificationAlgorithm] = &[
144    #[cfg(feature = "ring")]
145    ring::ECDSA_P256_SHA256,
146    #[cfg(feature = "ring")]
147    ring::ECDSA_P256_SHA384,
148    #[cfg(feature = "ring")]
149    ring::ECDSA_P384_SHA256,
150    #[cfg(feature = "ring")]
151    ring::ECDSA_P384_SHA384,
152    #[cfg(feature = "ring")]
153    ring::ED25519,
154    #[cfg(all(feature = "ring", feature = "alloc"))]
155    ring::RSA_PKCS1_2048_8192_SHA256,
156    #[cfg(all(feature = "ring", feature = "alloc"))]
157    ring::RSA_PKCS1_2048_8192_SHA384,
158    #[cfg(all(feature = "ring", feature = "alloc"))]
159    ring::RSA_PKCS1_2048_8192_SHA512,
160    #[cfg(all(feature = "ring", feature = "alloc"))]
161    ring::RSA_PKCS1_2048_8192_SHA256_ABSENT_PARAMS,
162    #[cfg(all(feature = "ring", feature = "alloc"))]
163    ring::RSA_PKCS1_2048_8192_SHA384_ABSENT_PARAMS,
164    #[cfg(all(feature = "ring", feature = "alloc"))]
165    ring::RSA_PKCS1_2048_8192_SHA512_ABSENT_PARAMS,
166    #[cfg(all(feature = "ring", feature = "alloc"))]
167    ring::RSA_PKCS1_3072_8192_SHA384,
168    #[cfg(all(feature = "ring", feature = "alloc"))]
169    ring::RSA_PSS_2048_8192_SHA256_LEGACY_KEY,
170    #[cfg(all(feature = "ring", feature = "alloc"))]
171    ring::RSA_PSS_2048_8192_SHA384_LEGACY_KEY,
172    #[cfg(all(feature = "ring", feature = "alloc"))]
173    ring::RSA_PSS_2048_8192_SHA512_LEGACY_KEY,
174    #[cfg(feature = "aws-lc-rs")]
175    aws_lc_rs::ECDSA_P256_SHA256,
176    #[cfg(feature = "aws-lc-rs")]
177    aws_lc_rs::ECDSA_P256_SHA384,
178    #[cfg(feature = "aws-lc-rs")]
179    aws_lc_rs::ECDSA_P256_SHA512,
180    #[cfg(feature = "aws-lc-rs")]
181    aws_lc_rs::ECDSA_P384_SHA256,
182    #[cfg(feature = "aws-lc-rs")]
183    aws_lc_rs::ECDSA_P384_SHA384,
184    #[cfg(feature = "aws-lc-rs")]
185    aws_lc_rs::ECDSA_P384_SHA512,
186    #[cfg(feature = "aws-lc-rs")]
187    aws_lc_rs::ECDSA_P521_SHA256,
188    #[cfg(feature = "aws-lc-rs")]
189    aws_lc_rs::ECDSA_P521_SHA384,
190    #[cfg(feature = "aws-lc-rs")]
191    aws_lc_rs::ECDSA_P521_SHA512,
192    #[cfg(feature = "aws-lc-rs")]
193    aws_lc_rs::ED25519,
194    #[cfg(feature = "aws-lc-rs")]
195    aws_lc_rs::RSA_PKCS1_2048_8192_SHA256,
196    #[cfg(feature = "aws-lc-rs")]
197    aws_lc_rs::RSA_PKCS1_2048_8192_SHA384,
198    #[cfg(feature = "aws-lc-rs")]
199    aws_lc_rs::RSA_PKCS1_2048_8192_SHA512,
200    #[cfg(feature = "aws-lc-rs")]
201    aws_lc_rs::RSA_PKCS1_2048_8192_SHA256_ABSENT_PARAMS,
202    #[cfg(feature = "aws-lc-rs")]
203    aws_lc_rs::RSA_PKCS1_2048_8192_SHA384_ABSENT_PARAMS,
204    #[cfg(feature = "aws-lc-rs")]
205    aws_lc_rs::RSA_PKCS1_2048_8192_SHA512_ABSENT_PARAMS,
206    #[cfg(feature = "aws-lc-rs")]
207    aws_lc_rs::RSA_PKCS1_3072_8192_SHA384,
208    #[cfg(feature = "aws-lc-rs")]
209    aws_lc_rs::RSA_PSS_2048_8192_SHA256_LEGACY_KEY,
210    #[cfg(feature = "aws-lc-rs")]
211    aws_lc_rs::RSA_PSS_2048_8192_SHA384_LEGACY_KEY,
212    #[cfg(feature = "aws-lc-rs")]
213    aws_lc_rs::RSA_PSS_2048_8192_SHA512_LEGACY_KEY,
214    #[cfg(feature = "aws-lc-rs")]
215    aws_lc_rs::ML_DSA_44,
216    #[cfg(feature = "aws-lc-rs")]
217    aws_lc_rs::ML_DSA_65,
218    #[cfg(feature = "aws-lc-rs")]
219    aws_lc_rs::ML_DSA_87,
220];
221
222fn public_values_eq(a: untrusted::Input<'_>, b: untrusted::Input<'_>) -> bool {
223    a.as_slice_less_safe() == b.as_slice_less_safe()
224}