1use iroh_base::{PublicKey, Signature};
7use rustls::{
8 CertificateError, DigitallySignedStruct, DistinguishedName, SignatureScheme,
9 SupportedProtocolVersion,
10 client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier},
11 crypto::{WebPkiSupportedAlgorithms, verify_tls13_signature_with_raw_key},
12 pki_types::CertificateDer as Certificate,
13 server::danger::{ClientCertVerified, ClientCertVerifier},
14};
15use webpki_types::SubjectPublicKeyInfoDer;
16
17pub(super) const PROTOCOL_VERSIONS: &[&SupportedProtocolVersion] = &[&rustls::version::TLS13];
19
20const ED25519_DALEK: Ed25519Dalek = Ed25519Dalek;
21const SUPPORTED_SIG_ALGS: WebPkiSupportedAlgorithms = WebPkiSupportedAlgorithms {
22 all: &[&ED25519_DALEK],
23 mapping: &[(SignatureScheme::ED25519, &[&ED25519_DALEK])],
24};
25
26#[derive(Default, Debug)]
30pub(super) struct ServerCertificateVerifier;
31
32impl ServerCertVerifier for ServerCertificateVerifier {
33 fn verify_server_cert(
34 &self,
35 end_entity: &Certificate,
36 intermediates: &[Certificate],
37 server_name: &rustls::pki_types::ServerName,
38 _ocsp_response: &[u8],
39 _now: rustls::pki_types::UnixTime,
40 ) -> Result<ServerCertVerified, rustls::Error> {
41 let rustls::pki_types::ServerName::DnsName(dns_name) = server_name else {
42 return Err(rustls::Error::UnsupportedNameType);
43 };
44 let Some(remote_peer_id) = super::name::decode(dns_name.as_ref()) else {
45 return Err(rustls::Error::InvalidCertificate(
46 CertificateError::NotValidForName,
47 ));
48 };
49
50 if !intermediates.is_empty() {
51 return Err(rustls::Error::InvalidCertificate(
52 CertificateError::UnknownIssuer,
53 ));
54 }
55
56 let end_entity_as_spki = SubjectPublicKeyInfoDer::from(end_entity.as_ref());
57 let remote_public_spki = rustls::sign::public_key_to_spki(
58 &webpki_types::alg_id::ED25519,
59 remote_peer_id.as_bytes(),
60 );
61
62 if remote_public_spki != end_entity_as_spki {
69 return Err(rustls::Error::InvalidCertificate(
70 CertificateError::UnknownIssuer,
71 ));
72 }
73
74 Ok(ServerCertVerified::assertion())
75 }
76
77 fn verify_tls12_signature(
78 &self,
79 _message: &[u8],
80 _cert: &Certificate,
81 _dss: &DigitallySignedStruct,
82 ) -> Result<HandshakeSignatureValid, rustls::Error> {
83 Err(rustls::Error::PeerIncompatible(
84 rustls::PeerIncompatible::Tls12NotOffered,
85 ))
86 }
87
88 fn verify_tls13_signature(
89 &self,
90 message: &[u8],
91 cert: &Certificate,
92 dss: &DigitallySignedStruct,
93 ) -> Result<HandshakeSignatureValid, rustls::Error> {
94 verify_tls13_signature_with_raw_key(
95 message,
96 &SubjectPublicKeyInfoDer::from(cert.as_ref()),
97 dss,
98 &SUPPORTED_SIG_ALGS,
99 )
100 }
101
102 fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
103 SUPPORTED_SIG_ALGS.supported_schemes()
104 }
105
106 fn requires_raw_public_keys(&self) -> bool {
107 true
108 }
109}
110
111#[derive(Default, Debug)]
115pub(super) struct ClientCertificateVerifier;
116
117impl ClientCertVerifier for ClientCertificateVerifier {
121 fn offer_client_auth(&self) -> bool {
122 true
123 }
124
125 fn verify_client_cert(
126 &self,
127 _end_entity: &Certificate,
128 intermediates: &[Certificate],
129 _now: rustls::pki_types::UnixTime,
130 ) -> Result<ClientCertVerified, rustls::Error> {
131 if !intermediates.is_empty() {
132 return Err(rustls::Error::InvalidCertificate(
133 CertificateError::UnknownIssuer,
134 ));
135 }
136
137 Ok(ClientCertVerified::assertion())
141 }
142
143 fn verify_tls12_signature(
144 &self,
145 _message: &[u8],
146 _cert: &Certificate,
147 _dss: &DigitallySignedStruct,
148 ) -> Result<HandshakeSignatureValid, rustls::Error> {
149 Err(rustls::Error::PeerIncompatible(
150 rustls::PeerIncompatible::Tls12NotOffered,
151 ))
152 }
153
154 fn verify_tls13_signature(
155 &self,
156 message: &[u8],
157 cert: &Certificate,
158 dss: &DigitallySignedStruct,
159 ) -> Result<HandshakeSignatureValid, rustls::Error> {
160 verify_tls13_signature_with_raw_key(
161 message,
162 &SubjectPublicKeyInfoDer::from(cert.as_ref()),
163 dss,
164 &SUPPORTED_SIG_ALGS,
165 )
166 }
167
168 fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
169 SUPPORTED_SIG_ALGS.supported_schemes()
170 }
171
172 fn root_hint_subjects(&self) -> &[DistinguishedName] {
173 &[][..]
174 }
175
176 fn requires_raw_public_keys(&self) -> bool {
177 true
178 }
179}
180
181#[derive(Debug)]
182struct Ed25519Dalek;
183
184impl webpki_types::SignatureVerificationAlgorithm for Ed25519Dalek {
185 fn verify_signature(
186 &self,
187 public_key: &[u8],
188 message: &[u8],
189 signature: &[u8],
190 ) -> Result<(), webpki_types::InvalidSignature> {
191 let public_key =
192 PublicKey::try_from(public_key).map_err(|_| webpki_types::InvalidSignature)?;
193 let signature =
194 Signature::try_from(signature).map_err(|_| webpki_types::InvalidSignature)?;
195 public_key
196 .verify(message, &signature)
197 .map_err(|_| webpki_types::InvalidSignature)
198 }
199
200 fn public_key_alg_id(&self) -> webpki_types::AlgorithmIdentifier {
201 webpki_types::alg_id::ED25519
202 }
203
204 fn signature_alg_id(&self) -> webpki_types::AlgorithmIdentifier {
205 webpki_types::alg_id::ED25519
206 }
207
208 fn fips(&self) -> bool {
209 false
210 }
211}