1use alloc::vec::Vec;
2use core::fmt;
3
4use pki_types::{
5 CertificateDer, ServerName, SignatureVerificationAlgorithm, SubjectPublicKeyInfoDer, UnixTime,
6};
7
8use super::anchors::RootCertStore;
9use super::pki_error;
10use crate::enums::SignatureScheme;
11use crate::error::{Error, PeerMisbehaved};
12use crate::verify::{DigitallySignedStruct, HandshakeSignatureValid};
13
14#[allow(dead_code)]
26pub fn verify_server_cert_signed_by_trust_anchor(
27 cert: &ParsedCertificate<'_>,
28 roots: &RootCertStore,
29 intermediates: &[CertificateDer<'_>],
30 now: UnixTime,
31 supported_algs: &[&dyn SignatureVerificationAlgorithm],
32) -> Result<(), Error> {
33 verify_server_cert_signed_by_trust_anchor_impl(
34 cert,
35 roots,
36 intermediates,
37 None, now,
39 supported_algs,
40 )
41}
42
43pub fn verify_server_name(
48 cert: &ParsedCertificate<'_>,
49 server_name: &ServerName<'_>,
50) -> Result<(), Error> {
51 cert.0
52 .verify_is_valid_for_subject_name(server_name)
53 .map_err(pki_error)
54}
55
56#[derive(Clone, Copy)]
59#[allow(unreachable_pub)]
60pub struct WebPkiSupportedAlgorithms {
61 pub all: &'static [&'static dyn SignatureVerificationAlgorithm],
67
68 pub mapping: &'static [(
80 SignatureScheme,
81 &'static [&'static dyn SignatureVerificationAlgorithm],
82 )],
83}
84
85impl WebPkiSupportedAlgorithms {
86 pub fn supported_schemes(&self) -> Vec<SignatureScheme> {
88 self.mapping
89 .iter()
90 .map(|item| item.0)
91 .collect()
92 }
93
94 fn convert_scheme(
96 &self,
97 scheme: SignatureScheme,
98 ) -> Result<&[&'static dyn SignatureVerificationAlgorithm], Error> {
99 self.mapping
100 .iter()
101 .filter_map(|item| if item.0 == scheme { Some(item.1) } else { None })
102 .next()
103 .ok_or_else(|| PeerMisbehaved::SignedHandshakeWithUnadvertisedSigScheme.into())
104 }
105
106 #[cfg(unstable_api_not_supported)] pub fn fips(&self) -> bool {
109 self.all.iter().all(|alg| alg.fips())
110 && self
111 .mapping
112 .iter()
113 .all(|item| item.1.iter().all(|alg| alg.fips()))
114 }
115}
116
117impl fmt::Debug for WebPkiSupportedAlgorithms {
118 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119 write!(f, "WebPkiSupportedAlgorithms {{ all: [ .. ], mapping: ")?;
120 f.debug_list()
121 .entries(self.mapping.iter().map(|item| item.0))
122 .finish()?;
123 write!(f, " }}")
124 }
125}
126
127pub struct ParsedCertificate<'a>(pub(crate) webpki::EndEntityCert<'a>);
131
132impl ParsedCertificate<'_> {
133 pub fn subject_public_key_info(&self) -> SubjectPublicKeyInfoDer<'static> {
135 self.0.subject_public_key_info()
136 }
137}
138
139impl<'a> TryFrom<&'a CertificateDer<'a>> for ParsedCertificate<'a> {
140 type Error = Error;
141 fn try_from(value: &'a CertificateDer<'a>) -> Result<Self, Self::Error> {
142 webpki::EndEntityCert::try_from(value)
143 .map_err(pki_error)
144 .map(ParsedCertificate)
145 }
146}
147
148pub fn verify_tls12_signature(
157 message: &[u8],
158 cert: &CertificateDer<'_>,
159 dss: &DigitallySignedStruct,
160 supported_schemes: &WebPkiSupportedAlgorithms,
161) -> Result<HandshakeSignatureValid, Error> {
162 let possible_algs = supported_schemes.convert_scheme(dss.scheme)?;
163 let cert = webpki::EndEntityCert::try_from(cert).map_err(pki_error)?;
164
165 for alg in possible_algs {
166 match cert.verify_signature(*alg, message, dss.signature()) {
167 Err(webpki::Error::UnsupportedSignatureAlgorithmForPublicKey) => continue,
168 Err(e) => return Err(pki_error(e)),
169 Ok(()) => return Ok(HandshakeSignatureValid::assertion()),
170 }
171 }
172
173 Err(pki_error(
174 webpki::Error::UnsupportedSignatureAlgorithmForPublicKey,
175 ))
176}
177
178pub fn verify_tls13_signature(
185 msg: &[u8],
186 cert: &CertificateDer<'_>,
187 dss: &DigitallySignedStruct,
188 supported_schemes: &WebPkiSupportedAlgorithms,
189) -> Result<HandshakeSignatureValid, Error> {
190 if !dss.scheme.supported_in_tls13() {
191 return Err(PeerMisbehaved::SignedHandshakeWithUnadvertisedSigScheme.into());
192 }
193
194 let alg = supported_schemes.convert_scheme(dss.scheme)?[0];
195
196 let cert = webpki::EndEntityCert::try_from(cert).map_err(pki_error)?;
197
198 cert.verify_signature(alg, msg, dss.signature())
199 .map_err(pki_error)
200 .map(|_| HandshakeSignatureValid::assertion())
201}
202
203pub fn verify_tls13_signature_with_raw_key(
206 msg: &[u8],
207 spki: &SubjectPublicKeyInfoDer<'_>,
208 dss: &DigitallySignedStruct,
209 supported_schemes: &WebPkiSupportedAlgorithms,
210) -> Result<HandshakeSignatureValid, Error> {
211 if !dss.scheme.supported_in_tls13() {
212 return Err(PeerMisbehaved::SignedHandshakeWithUnadvertisedSigScheme.into());
213 }
214
215 let raw_key = webpki::RawPublicKeyEntity::try_from(spki).map_err(pki_error)?;
216 let alg = supported_schemes.convert_scheme(dss.scheme)?[0];
217
218 raw_key
219 .verify_signature(alg, msg, dss.signature())
220 .map_err(pki_error)
221 .map(|_| HandshakeSignatureValid::assertion())
222}
223
224pub(crate) fn verify_server_cert_signed_by_trust_anchor_impl(
238 cert: &ParsedCertificate<'_>,
239 roots: &RootCertStore,
240 intermediates: &[CertificateDer<'_>],
241 revocation: Option<webpki::RevocationOptions<'_>>,
242 now: UnixTime,
243 supported_algs: &[&dyn SignatureVerificationAlgorithm],
244) -> Result<(), Error> {
245 let result = cert.0.verify_for_usage(
246 supported_algs,
247 &roots.roots,
248 intermediates,
249 now,
250 webpki::KeyUsage::server_auth(),
251 revocation,
252 None,
253 );
254 match result {
255 Ok(_) => Ok(()),
256 Err(e) => Err(pki_error(e)),
257 }
258}
259
260#[cfg(test)]
261mod tests {
262 use std::format;
263
264 use super::*;
265
266 #[test]
267 fn certificate_debug() {
268 assert_eq!(
269 "CertificateDer(0x6162)",
270 format!("{:?}", CertificateDer::from(b"ab".to_vec()))
271 );
272 }
273
274 #[cfg(feature = "ring")]
275 #[test]
276 fn webpki_supported_algorithms_is_debug() {
277 assert_eq!(
278 "WebPkiSupportedAlgorithms { all: [ .. ], mapping: [ECDSA_NISTP384_SHA384, ECDSA_NISTP256_SHA256, ED25519, RSA_PSS_SHA512, RSA_PSS_SHA384, RSA_PSS_SHA256, RSA_PKCS1_SHA512, RSA_PKCS1_SHA384, RSA_PKCS1_SHA256] }",
279 format!("{:?}", crate::crypto::ring::default_provider().signature_verification_algorithms)
280 );
281 }
282}