Skip to content

WIP: Add support for Certificate verification with CRL #2186

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion examples/src/tls/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let cert = std::fs::read_to_string(data_dir.join("tls/server.pem"))?;
let key = std::fs::read_to_string(data_dir.join("tls/server.key"))?;

let identity = Identity::from_pem(cert, key);
let identity = Identity::from_pem(cert, key, None);

let addr = "[::1]:50051".parse().unwrap();
let server = EchoServer::default();
Expand Down
2 changes: 1 addition & 1 deletion examples/src/tls_client_auth/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let server_root_ca_cert = Certificate::from_pem(server_root_ca_cert);
let client_cert = std::fs::read_to_string(data_dir.join("tls/client1.pem"))?;
let client_key = std::fs::read_to_string(data_dir.join("tls/client1.key"))?;
let client_identity = Identity::from_pem(client_cert, client_key);
let client_identity = Identity::from_pem(client_cert, client_key, None);

let tls = ClientTlsConfig::new()
.domain_name("localhost")
Expand Down
2 changes: 1 addition & 1 deletion examples/src/tls_client_auth/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let data_dir = std::path::PathBuf::from_iter([std::env!("CARGO_MANIFEST_DIR"), "data"]);
let cert = std::fs::read_to_string(data_dir.join("tls/server.pem"))?;
let key = std::fs::read_to_string(data_dir.join("tls/server.key"))?;
let server_identity = Identity::from_pem(cert, key);
let server_identity = Identity::from_pem(cert, key, None);

let client_ca_cert = std::fs::read_to_string(data_dir.join("tls/client_ca.pem"))?;
let client_ca_cert = Certificate::from_pem(client_ca_cert);
Expand Down
2 changes: 1 addition & 1 deletion interop/src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
if matches.use_tls {
let cert = std::fs::read_to_string("interop/data/server1.pem")?;
let key = std::fs::read_to_string("interop/data/server1.key")?;
let identity = Identity::from_pem(cert, key);
let identity = Identity::from_pem(cert, key, None);

builder = builder.tls_config(ServerTlsConfig::new().identity(identity))?;
}
Expand Down
3 changes: 2 additions & 1 deletion tonic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ deflate = ["dep:flate2"]
zstd = ["dep:zstd"]
default = ["router", "transport", "codegen", "prost"]
prost = ["dep:prost"]
_tls-any = ["dep:tokio-rustls", "dep:tokio", "tokio?/rt", "tokio?/macros"] # Internal. Please choose one of `tls-ring` or `tls-aws-lc`
_tls-any = ["dep:rustls-pki-types", "dep:tokio-rustls", "dep:tokio", "tokio?/rt", "tokio?/macros"] # Internal. Please choose one of `tls-ring` or `tls-aws-lc`
tls-ring = ["_tls-any", "tokio-rustls/ring"]
tls-aws-lc = ["_tls-any", "tokio-rustls/aws-lc-rs"]
tls-native-roots = ["_tls-any", "channel", "dep:rustls-native-certs"]
Expand Down Expand Up @@ -89,6 +89,7 @@ axum = {version = "0.8", default-features = false, optional = true}

# rustls
rustls-native-certs = { version = "0.8", optional = true }
rustls-pki-types = { version = "1.11", optional = true }
tokio-rustls = { version = "0.26.1", default-features = false, features = ["logging", "tls12"], optional = true }
webpki-roots = { version = "0.26", optional = true }

Expand Down
8 changes: 6 additions & 2 deletions tonic/src/transport/server/service/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ impl TlsAcceptor {
Some(cert) => {
let mut roots = RootCertStore::empty();
roots.add_parsable_certificates(convert_certificate_to_pki_types(cert)?);
let mut _builder = WebPkiClientVerifier::builder(roots.into());
if let Some(crls) = &identity.crls {
_builder = _builder.with_crls(crls.clone());
}
let verifier = if client_auth_optional {
WebPkiClientVerifier::builder(roots.into()).allow_unauthenticated()
_builder.allow_unauthenticated()
} else {
WebPkiClientVerifier::builder(roots.into())
_builder
}
.build()?;
builder.with_client_cert_verifier(verifier)
Expand Down
6 changes: 4 additions & 2 deletions tonic/src/transport/tls.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rustls_pki_types::CertificateRevocationListDer;
/// Represents a X509 certificate.
#[derive(Debug, Clone)]
pub struct Certificate {
Expand All @@ -9,6 +10,7 @@ pub struct Certificate {
pub struct Identity {
pub(crate) cert: Certificate,
pub(crate) key: Vec<u8>,
pub(crate) crls: Option<Vec<CertificateRevocationListDer<'static>>>,
}

impl Certificate {
Expand Down Expand Up @@ -52,9 +54,9 @@ impl Identity {
/// Parse a PEM encoded certificate and private key.
///
/// The provided cert must contain at least one PEM encoded certificate.
pub fn from_pem(cert: impl AsRef<[u8]>, key: impl AsRef<[u8]>) -> Self {
pub fn from_pem(cert: impl AsRef<[u8]>, key: impl AsRef<[u8]>, crls: Option<Vec<CertificateRevocationListDer<'static>>>) -> Self {
let cert = Certificate::from_pem(cert);
let key = key.as_ref().into();
Self { cert, key }
Self { cert, crls, key }
}
}