Skip to content

Commit 96c6810

Browse files
ccsr: trust a server certificate that exactly matches the configured CA
With rustls, webpki rejects a self-signed `CA:TRUE` certificate that is both the schema registry's server certificate and the connection's `SSL CERTIFICATE AUTHORITY`, reporting `CaUsedAsEndEntity`. OpenSSL accepted it. When a schema registry client has configured root certificates, build the rustls configuration in mz-ccsr and hand it to reqwest as a preconfigured TLS backend. Its verifier accepts a server certificate that is byte-for-byte identical to a configured root, which is equivalent to pinning that certificate, and otherwise defers to `rustls-platform-verifier` with the same native and extra roots that reqwest uses by default. Handshake signatures are always checked by the inner verifier. The client identity moves into the rustls configuration, because reqwest ignores its own identity setting with a preconfigured backend. Clients without configured roots keep reqwest's default TLS path. A CN-only leaf without a subjectAltName is still rejected. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 46fd9da commit 96c6810

6 files changed

Lines changed: 579 additions & 40 deletions

File tree

‎Cargo.lock‎

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ rlimit = "0.11.0"
478478
rocksdb = { version = "0.25.0", default-features = false, features = ["lz4", "snappy", "zstd"] }
479479
ropey = "1.6.1"
480480
rustls = "0.23.38"
481+
rustls-platform-verifier = "0.7.0"
481482
rpassword = "7.5.1"
482483
rusqlite = { version = "0.40.2", features = ["bundled"] }
483484
ryu = "1.0.23"
@@ -566,6 +567,7 @@ uuid = "1.19.0"
566567
version-compare = "0.2.1"
567568
walkdir = "2.5.0"
568569
which = "8"
570+
x509-cert = { version = "0.2.5", default-features = false }
569571
yansi = "1.0.1"
570572
zeroize = { version = "1.8.2", features = ["derive", "serde"] }
571573
zip = { version = "8.6.0", default-features = false, features = ["deflate-flate2"] }

‎src/ccsr/Cargo.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@ proptest.workspace = true
1616
zeroize.workspace = true
1717
proptest-derive.workspace = true
1818
rustls.workspace = true
19+
rustls-platform-verifier.workspace = true
1920
serde.workspace = true
2021
thiserror.workspace = true
2122
url = { workspace = true, features = ["serde"] }
23+
x509-cert.workspace = true
2224

2325
[dev-dependencies]
2426
hyper.workspace = true
2527
hyper-util.workspace = true
2628
mz-ore = { path = "../ore", features = ["async", "test"] }
2729
openssl.workspace = true
30+
tokio-openssl.workspace = true
2831
serde_json.workspace = true
2932
tokio.workspace = true
3033
tracing.workspace = true

‎src/ccsr/src/config.rs‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ impl ClientConfig {
6363
/// Adds a trusted root TLS certificate.
6464
///
6565
/// Certificates in the system's certificate store are trusted by default.
66+
/// A server certificate identical to `cert` is trusted even if it is not
67+
/// a valid end-entity certificate, for example a self-signed CA. Its name
68+
/// and validity period are still checked, and its name may match any
69+
/// subject common name if it has no DNS or IP subjectAltName.
6670
pub fn add_root_certificate(mut self, cert: Certificate) -> ClientConfig {
6771
self.root_certs.push(cert);
6872
self
@@ -106,11 +110,13 @@ impl ClientConfig {
106110
pub fn build(self) -> Result<Client, anyhow::Error> {
107111
let mut builder = reqwest::ClientBuilder::new();
108112

109-
for root_cert in self.root_certs {
110-
builder = builder.add_root_certificate(root_cert.into());
111-
}
112-
113-
if let Some(ident) = self.identity {
113+
// NOTE: A preconfigured TLS backend makes reqwest ignore all of its TLS
114+
// builder settings (roots, identity, ALPN, SNI, TLS versions, CRLs), so
115+
// any such setting must go into `rustls_config` instead.
116+
if !self.root_certs.is_empty() {
117+
let tls = crate::tls::rustls_config(&self.root_certs, self.identity.as_ref())?;
118+
builder = builder.tls_backend_preconfigured(tls);
119+
} else if let Some(ident) = self.identity {
114120
builder = builder.identity(ident.into());
115121
}
116122

‎src/ccsr/src/tls.rs‎

Lines changed: 199 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,18 @@
1212
use std::fmt;
1313
use std::sync::Arc;
1414

15+
use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier};
16+
use rustls::client::verify_server_name;
1517
use rustls::pki_types::pem::PemObject;
16-
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
18+
use rustls::pki_types::{CertificateDer, PrivateKeyDer, ServerName, UnixTime};
19+
use rustls::server::ParsedCertificate;
20+
use rustls::{CertificateError, DigitallySignedStruct, SignatureScheme};
1721
use serde::{Deserialize, Serialize};
22+
use x509_cert::der::asn1::{Ia5StringRef, PrintableStringRef, Utf8StringRef};
23+
use x509_cert::der::oid::db::rfc4519;
24+
use x509_cert::der::{Decode, Tag, Tagged};
25+
use x509_cert::ext::pkix::SubjectAltName;
26+
use x509_cert::ext::pkix::name::GeneralName;
1827
use zeroize::{Zeroize, Zeroizing};
1928

2029
/// An error constructing a [`Certificate`] or [`Identity`].
@@ -30,6 +39,8 @@ pub enum TlsError {
3039
Certificate(rustls::CertificateError),
3140
#[error("invalid TLS identity: {0}")]
3241
Identity(rustls::Error),
42+
#[error("invalid TLS configuration: {0}")]
43+
Config(rustls::Error),
3344
#[error(transparent)]
3445
Reqwest(#[from] reqwest::Error),
3546
}
@@ -75,16 +86,7 @@ impl Identity {
7586
pem.push(b'\n');
7687
pem.extend_from_slice(cert);
7788

78-
// Mirror `reqwest::Identity::from_pem`, which uses the last private
79-
// key in the buffer.
80-
let mut keys = PrivateKeyDer::pem_slice_iter(&pem).collect::<Result<Vec<_>, _>>()?;
81-
let key = keys.pop().ok_or(TlsError::NoPrivateKey)?;
82-
keys.iter_mut().for_each(Zeroize::zeroize);
83-
let certs = CertificateDer::pem_slice_iter(&pem).collect::<Result<Vec<_>, _>>()?;
84-
if certs.is_empty() {
85-
return Err(TlsError::NoCertificate);
86-
}
87-
89+
let (certs, key) = parse_identity_pem(&pem)?;
8890
// reqwest only checks that the key matches the certificate when the
8991
// client is built, so check here to report the error up front.
9092
let provider = rustls::crypto::aws_lc_rs::default_provider();
@@ -97,6 +99,192 @@ impl Identity {
9799
}
98100
}
99101

102+
/// Splits an identity PEM buffer into its certificate chain and private key.
103+
fn parse_identity_pem(
104+
pem: &[u8],
105+
) -> Result<(Vec<CertificateDer<'static>>, PrivateKeyDer<'static>), TlsError> {
106+
// Mirror `reqwest::Identity::from_pem`, which uses the last private key in
107+
// the buffer.
108+
let mut keys = PrivateKeyDer::pem_slice_iter(pem).collect::<Result<Vec<_>, _>>()?;
109+
let key = keys.pop().ok_or(TlsError::NoPrivateKey)?;
110+
keys.iter_mut().for_each(Zeroize::zeroize);
111+
let certs = CertificateDer::pem_slice_iter(pem).collect::<Result<Vec<_>, _>>()?;
112+
if certs.is_empty() {
113+
return Err(TlsError::NoCertificate);
114+
}
115+
Ok((certs, key))
116+
}
117+
118+
/// Builds the rustls configuration for a client that trusts `roots` in
119+
/// addition to the platform's trust store, and that presents `identity`, if
120+
/// any, for client authentication.
121+
///
122+
/// Server certificates are verified by `rustls-platform-verifier`, as reqwest
123+
/// does by default, with the [`ExactRootMatch`] fallback.
124+
pub(crate) fn rustls_config(
125+
roots: &[Certificate],
126+
identity: Option<&Identity>,
127+
) -> Result<rustls::ClientConfig, TlsError> {
128+
let provider = Arc::new(rustls::crypto::aws_lc_rs::default_provider());
129+
let roots: Vec<_> = roots
130+
.iter()
131+
.map(|cert| CertificateDer::from(cert.der.clone()))
132+
.collect();
133+
let inner = rustls_platform_verifier::Verifier::new_with_extra_roots(
134+
roots.clone(),
135+
Arc::clone(&provider),
136+
)
137+
.map_err(TlsError::Config)?;
138+
let builder = rustls::ClientConfig::builder_with_provider(provider)
139+
.with_safe_default_protocol_versions()
140+
.map_err(TlsError::Config)?
141+
.dangerous()
142+
.with_custom_certificate_verifier(Arc::new(ExactRootMatch { inner, roots }));
143+
let mut config = match identity {
144+
Some(identity) => {
145+
let (certs, key) = parse_identity_pem(&identity.pem)?;
146+
builder
147+
.with_client_auth_cert(certs, key)
148+
.map_err(TlsError::Identity)?
149+
}
150+
None => builder.with_no_client_auth(),
151+
};
152+
// reqwest only sets ALPN on TLS configurations it builds itself. This
153+
// mirrors its choice while the workspace enables reqwest's `http2` feature.
154+
config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
155+
Ok(config)
156+
}
157+
158+
/// A server certificate verifier that accepts a server certificate that is
159+
/// byte-for-byte identical to one of `roots`, and otherwise defers to `inner`.
160+
///
161+
/// An exact match must still be within its validity period and valid for the
162+
/// server name, but skips the chain, basic constraints and key usage checks.
163+
/// This keeps a self-signed `CA:TRUE` certificate working when it is supplied
164+
/// as its own certificate authority, which webpki rejects as
165+
/// `CaUsedAsEndEntity`. The name check falls back to the subject common names
166+
/// when an exact match has no DNS or IP subjectAltName, see
167+
/// [`common_name_matches`]. Every other certificate is checked against
168+
/// subjectAltNames only. An exact match is decided without consulting
169+
/// `inner`. Handshake signatures are always verified by `inner`.
170+
#[derive(Debug)]
171+
struct ExactRootMatch {
172+
inner: rustls_platform_verifier::Verifier,
173+
roots: Vec<CertificateDer<'static>>,
174+
}
175+
176+
/// Returns whether `server_name` is a DNS name equal, ignoring ASCII case, to
177+
/// any subject common name of `cert`, and `cert` has no DNS or IP
178+
/// subjectAltName.
179+
///
180+
/// This is stricter than OpenSSL's fallback, which also applies when only IP
181+
/// subjectAltNames are present, matches wildcard common names, and decodes
182+
/// string types other than UTF8String, PrintableString and IA5String.
183+
fn common_name_matches(cert: &x509_cert::Certificate, server_name: &ServerName<'_>) -> bool {
184+
let ServerName::DnsName(name) = server_name else {
185+
return false;
186+
};
187+
let tbs = &cert.tbs_certificate;
188+
// An undecodable subjectAltName extension counts as present.
189+
let has_san = tbs.filter::<SubjectAltName>().any(|san| match san {
190+
Ok((_, SubjectAltName(names))) => names
191+
.iter()
192+
.any(|name| matches!(name, GeneralName::DnsName(_) | GeneralName::IpAddress(_))),
193+
Err(_) => true,
194+
});
195+
if has_san {
196+
return false;
197+
}
198+
tbs.subject
199+
.0
200+
.iter()
201+
.flat_map(|rdn| rdn.0.iter())
202+
.filter(|atv| atv.oid == rfc4519::CN)
203+
.any(|cn| {
204+
let cn = match cn.value.tag() {
205+
Tag::Utf8String => cn
206+
.value
207+
.decode_as::<Utf8StringRef<'_>>()
208+
.map(|s| s.as_str().to_owned()),
209+
Tag::PrintableString => cn
210+
.value
211+
.decode_as::<PrintableStringRef<'_>>()
212+
.map(|s| s.as_str().to_owned()),
213+
Tag::Ia5String => cn
214+
.value
215+
.decode_as::<Ia5StringRef<'_>>()
216+
.map(|s| s.as_str().to_owned()),
217+
_ => return false,
218+
};
219+
cn.is_ok_and(|cn| cn.eq_ignore_ascii_case(name.as_ref()))
220+
})
221+
}
222+
223+
impl ServerCertVerifier for ExactRootMatch {
224+
fn verify_server_cert(
225+
&self,
226+
end_entity: &CertificateDer<'_>,
227+
intermediates: &[CertificateDer<'_>],
228+
server_name: &ServerName<'_>,
229+
ocsp_response: &[u8],
230+
now: UnixTime,
231+
) -> Result<ServerCertVerified, rustls::Error> {
232+
// Checked before `inner`, which would reject a `CA:TRUE` certificate and
233+
// logs every rejection at error level.
234+
if self
235+
.roots
236+
.iter()
237+
.any(|root| root.as_ref() == end_entity.as_ref())
238+
{
239+
let cert = x509_cert::Certificate::from_der(end_entity)
240+
.map_err(|_| rustls::Error::InvalidCertificate(CertificateError::BadEncoding))?;
241+
let validity = &cert.tbs_certificate.validity;
242+
let now = now.as_secs();
243+
if now < validity.not_before.to_unix_duration().as_secs() {
244+
return Err(rustls::Error::InvalidCertificate(
245+
CertificateError::NotValidYet,
246+
));
247+
}
248+
if now > validity.not_after.to_unix_duration().as_secs() {
249+
return Err(rustls::Error::InvalidCertificate(CertificateError::Expired));
250+
}
251+
// Checks subjectAltNames only, not basic constraints, so a
252+
// `CA:TRUE` certificate passes. Errors match `inner`'s on Linux.
253+
let parsed = ParsedCertificate::try_from(end_entity)?;
254+
match verify_server_name(&parsed, server_name) {
255+
Ok(()) => {}
256+
Err(_) if common_name_matches(&cert, server_name) => {}
257+
Err(e) => return Err(e),
258+
}
259+
return Ok(ServerCertVerified::assertion());
260+
}
261+
self.inner
262+
.verify_server_cert(end_entity, intermediates, server_name, ocsp_response, now)
263+
}
264+
265+
fn verify_tls12_signature(
266+
&self,
267+
message: &[u8],
268+
cert: &CertificateDer<'_>,
269+
dss: &DigitallySignedStruct,
270+
) -> Result<HandshakeSignatureValid, rustls::Error> {
271+
self.inner.verify_tls12_signature(message, cert, dss)
272+
}
273+
274+
fn verify_tls13_signature(
275+
&self,
276+
message: &[u8],
277+
cert: &CertificateDer<'_>,
278+
dss: &DigitallySignedStruct,
279+
) -> Result<HandshakeSignatureValid, rustls::Error> {
280+
self.inner.verify_tls13_signature(message, cert, dss)
281+
}
282+
283+
fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
284+
self.inner.supported_verify_schemes()
285+
}
286+
}
287+
100288
impl From<Identity> for reqwest::Identity {
101289
fn from(id: Identity) -> Self {
102290
reqwest::Identity::from_pem(&id.pem).expect("known to be a valid identity")

0 commit comments

Comments
 (0)