8000 Expose SingleCertAndKey server cert resolver by djc · Pull Request #2337 · rustls/rustls · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Expose SingleCertAndKey server cert resolver #2337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rustls/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustls"
version = "0.23.22"
version = "0.23.23"
edition = "2021"
rust-version = "1.71"
license = "Apache-2.0 OR ISC OR MIT"
Expand Down
11 changes: 3 additions & 8 deletions rustls/src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::builder::{ConfigBuilder, WantsVerifier};
use crate::client::{handy, ClientConfig, EchMode, ResolvesClientCert};
use crate::error::Error;
use crate::key_log::NoKeyLog;
use crate::msgs::handshake::CertificateChain;
use crate::sign::{CertifiedKey, SingleCertAndKey};
use crate::sync::Arc;
use crate::versions::TLS13;
use crate::webpki::{self, WebPkiServerVerifier};
Expand Down Expand Up @@ -148,13 +148,8 @@ impl ConfigBuilder<ClientConfig, WantsClientCert> {
cert_chain: Vec<CertificateDer<'static>>,
key_der: PrivateKeyDer<'static>,
) -> Result<ClientConfig, Error> {
let private_key = self
.provider
.key_provider
.load_private_key(key_der)?;
let resolver =
handy::AlwaysResolvesClientCert::new(private_key, CertificateChain(cert_chain))?;
Ok(self.with_client_cert_resolver(Arc::new(resolver)))
let certified_key = CertifiedKey::from_der(cert_chain, key_der, &self.provider)?;
Ok(self.with_client_cert_resolver(Arc::new(SingleCertAndKey::from(certified_key))))
}

/// Do not support client auth.
Expand Down
31 changes: 0 additions & 31 deletions rustls/src/client/handy.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use pki_types::ServerName;

use crate::enums::SignatureScheme;
use crate::error::Error;
use crate::msgs::handshake::CertificateChain;
use crate::msgs::persist;
use crate::sync::Arc;
use crate::{client, sign, NamedGroup};
Expand Down Expand Up @@ -211,35 +209,6 @@ impl client::ResolvesClientCert for FailResolveClientCert {
}
}

#[derive(Debug)]
pub(super) struct AlwaysResolvesClientCert(Arc<sign::CertifiedKey>);

impl AlwaysResolvesClientCert {
pub(super) fn new(
private_key: Arc<dyn sign::SigningKey>,
chain: CertificateChain<'static>,
) -> Result<Self, Error> {
Ok(Self(Arc::new(sign::CertifiedKey::new(
chain.0,
private_key,
))))
}
}

impl client::ResolvesClientCert for AlwaysResolvesClientCert {
fn resolve(
&self,
_root_hint_subjects: &[&[u8]],
_sigschemes: &[SignatureScheme],
) -> Option<Arc<sign::CertifiedKey>> {
Some(Arc::clone(&self.0))
}

fn has_certs(&self) -> bool {
true
}
}

/// An exemplar `ResolvesClientCert` implementation that always resolves to a single
/// [RFC 7250] raw public key.
///
Expand Down
65 changes: 63 additions & 2 deletions rustls/src/crypto/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
use alloc::vec::Vec;
use core::fmt::Debug;

use pki_types::{AlgorithmIdentifier, CertificateDer, SubjectPublicKeyInfoDer};
use pki_types::{AlgorithmIdentifier, CertificateDer, PrivateKeyDer, SubjectPublicKeyInfoDer};

use crate::client::ResolvesClientCert;
use crate::enums::{SignatureAlgorithm, SignatureScheme};
use crate::error::{Error, InconsistentKeys};
use crate::server::ParsedCertificate;
use crate::server::{ClientHello, ParsedCertificate, ResolvesServerCert};
use crate::sync::Arc;
use crate::x509;

use super::CryptoProvider;

/// An abstract signing key.
///
/// This interface is used by rustls to use a private signing key
Expand Down Expand Up @@ -85,6 +88,40 @@
fn scheme(&self) -> SignatureScheme;
}

/// Server certificate resolver which always resolves to the same certificate and key.
///
/// For use with [`ConfigBuilder::with_cert_resolver()`].
///
/// [`ConfigBuilder::with_cert_resolver()`]: crate::ConfigBuilder::with_cert_resolver
#[derive(Debug)]
pub struct SingleCertAndKey(Arc<CertifiedKey>);

impl From<CertifiedKey> for SingleCertAndKey {
fn from(certified_key: CertifiedKey) -> Self {
Self(Arc::new(certified_key))
}
}

impl ResolvesClientCert for SingleCertAndKey {
fn resolve(
&self,
_root_hint_subjects: &[&[u8]],
_sigschemes: &[SignatureScheme],
) -> Option<Arc<CertifiedKey>> {
Some(Arc::clone(&self.0))
}

fn has_certs(&self) -> bool {
true
}
}

impl ResolvesServerCert for SingleCertAndKey {
fn resolve(&self, _client_hello: ClientHello<'_>) -> Option<Arc<CertifiedKey>> {
Some(Arc::clone(&self.0))
}
}

/// A packaged-together certificate chain, matching `SigningKey` and
/// optional stapled OCSP response.
///
Expand All @@ -107,6 +144,30 @@
}

impl CertifiedKey {
/// Create a new `CertifiedKey` from a certificate chain and DER-encoded private key.
///
/// Attempt to parse the private key with the given [`CryptoProvider`]'s [`KeyProvider`] and
/// verify that it matches the public key in the first certificate of the `cert_chain`
/// if possible.
///
/// [`KeyProvider`]: crate::crypto::KeyProvider
pub fn from_der(
cert_chain: Vec<CertificateDer<'static>>,
key: PrivateKeyDer<'static>,
provider: &CryptoProvider,
) -> Result<Self, Error> {
let private_key = provider
.key_provider
.load_private_key(key)?;

let certified_key = Self::new(cert_chain, private_key);
match certified_key.keys_match() {
// Don't treat unknown consistency as an error
Ok(()) | Err(Error::InconsistentKeys(InconsistentKeys::Unknown)) => Ok(certified_key),
Err(err) => Err(err),

Check warning on line 167 in rustls/src/crypto/signer.rs

View check run for this annotation

Codecov / codecov/patch

rustls/src/crypto/signer.rs#L167

Added line #L167 was not covered by tests
}
}

/// Make a new CertifiedKey, with the given chain and key.
///
/// The cert chain must not be empty. The first certificate in the chain
Expand Down
2 changes: 1 addition & 1 deletion rustls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ pub mod pki_types {

/// Message signing interfaces.
pub mod sign {
pub use crate::crypto::signer::{CertifiedKey, Signer, SigningKey};
pub use crate::crypto::signer::{CertifiedKey, Signer, SigningKey, SingleCertAndKey};
}

/// APIs for implementing QUIC TLS
Expand Down
40 changes: 9 additions & 31 deletions rustls/src/server/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use core::marker::PhantomData;

use pki_types::{CertificateDer, PrivateKeyDer};

use super::{handy, ResolvesServerCert, ServerConfig};
use crate::builder::{ConfigBuilder, WantsVerifier};
use crate::error::Error;
use crate::server::{handy, ResolvesServerCert, ServerConfig};
use crate::sign::CertifiedKey;
use crate::sign::{CertifiedKey, SingleCertAndKey};
use crate::sync::Arc;
use crate::verify::{ClientCertVerifier, NoClientAuth};
use crate::{compress, versions, InconsistentKeys, NoKeyLog};
use crate::{compress, versions, NoKeyLog};

impl ConfigBuilder<ServerConfig, WantsVerifier> {
/// Choose how to verify client certificates.
Expand Down Expand Up @@ -67,20 +67,8 @@ impl ConfigBuilder<ServerConfig, WantsServerCert> {
cert_chain: Vec<CertificateDer<'static>>,
key_der: PrivateKeyDer<'static>,
) -> Result<ServerConfig, Error> {
let private_key = self
.provider
.key_provider
.load_private_key(key_der)?;

let certified_key = CertifiedKey::new(cert_chain, private_key);
match certified_key.keys_match() {
// Don't treat unknown consistency as an error
Ok(()) | Err(Error::InconsistentKeys(InconsistentKeys::Unknown)) => (),
Err(err) => return Err(err),
}

let resolver = handy::AlwaysResolvesChain::new(certified_key);
Ok(self.with_cert_resolver(Arc::new(resolver)))
let certified_key = CertifiedKey::from_der(cert_chain, key_der, self.crypto_provider())?;
Ok(self.with_cert_resolver(Arc::new(SingleCertAndKey::from(certified_key))))
}

/// Sets a single certificate chain, matching private key and optional OCSP
Expand All @@ -102,20 +90,10 @@ impl ConfigBuilder<ServerConfig, WantsServerCert> {
key_der: PrivateKeyDer<'static>,
ocsp: Vec<u8>,
) -> Result<ServerConfig, Error> {
let private_key = self
.provider
.key_provider
.load_private_key(key_der)?;

let certified_key = CertifiedKey::new(cert_chain, private_key);
match certified_key.keys_match() {
// Don't treat unknown consistency as an error
Ok(()) | Err(Error::InconsistentKeys(InconsistentKeys::Unknown)) => (),
Err(err) => return Err(err),
}

let resolver = handy::AlwaysResolvesChain::new_with_extras(certified_key, ocsp);
Ok(self.with_cert_resolver(Arc::new(resolver)))
let mut certified_key =
CertifiedKey::from_der(cert_chain, key_der, self.crypto_provider())?;
certified_key.ocsp = Some(ocsp);
Ok(self.with_cert_resolver(Arc::new(SingleCertAndKey::from(certified_key))))
}

/// Sets a custom [`ResolvesServerCert`].
Expand Down
33 changes: 0 additions & 33 deletions rustls/src/server/handy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,39 +166,6 @@ impl server::ProducesTickets for NeverProducesTickets {
}
}

/// Something which always resolves to the same cert chain.
#[derive(Debug)]
pub(super) struct AlwaysResolvesChain(Arc<sign::CertifiedKey>);

impl AlwaysResolvesChain {
/// Creates an `AlwaysResolvesChain`, using the supplied `CertifiedKey`.
pub(super) fn new(certified_key: sign::CertifiedKey) -> Self {
Self(Arc::new(certified_key))
}

/// Creates an `AlwaysResolvesChain`, using the supplied `CertifiedKey` and OCSP response.
///
/// If non-empty, the given OCSP response is attached.
pub(super) fn new_with_extras(certified_key: sign::CertifiedKey, ocsp: Vec<u8>) -> Self {
let mut r = Self::new(certified_key);

{
let cert = Arc::make_mut(&mut r.0);
if !ocsp.is_empty() {
cert.ocsp = Some(ocsp);
}
}

r
}
}

impl server::ResolvesServerCert for AlwaysResolvesChain {
fn resolve(&self, _client_hello: ClientHello<'_>) -> Option<Arc<sign::CertifiedKey>> {
Some(Arc::clone(&self.0))
}
}

/// An exemplar `ResolvesServerCert` implementation that always resolves to a single
/// [RFC 7250] raw public key.
///
Expand Down
Loading
Loading
0