Skip to content
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

fix(tls): encode der to pem #1399

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions tonic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ axum = {version = "0.6.9", default_features = false, optional = true}

# rustls
async-stream = { version = "0.3", optional = true }
pem = { version = "2.0.1", optional = true }
rustls-pemfile = { version = "1.0", optional = true }
rustls-native-certs = { version = "0.6.1", optional = true }
tokio-rustls = { version = "0.24.0", optional = true }
Expand Down
13 changes: 12 additions & 1 deletion tonic/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,23 @@ impl<T> Request<T> {
/// TLS enabled connections.
#[cfg(feature = "transport")]
#[cfg_attr(docsrs, doc(cfg(feature = "transport")))]
pub fn peer_certs(&self) -> Option<Arc<Vec<Certificate>>> {
pub fn peer_certs(&self) -> Option<Vec<Certificate>> {
#[cfg(feature = "tls")]
{
self.extensions()
.get::<TlsConnectInfo<TcpConnectInfo>>()
.and_then(|i| i.peer_certs())
.map(|der_certs| {
der_certs
.iter()
.map(|der_cert| {
pem::Pem::new("CERTIFICATE", der_cert.as_ref())
.to_string()
.into_bytes()
})
.map(|pem_cert| Certificate::from_pem(pem_cert))
.collect()
})
}

#[cfg(not(feature = "tls"))]
Expand Down
7 changes: 3 additions & 4 deletions tonic/src/transport/server/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use hyper::server::conn::AddrStream;
use std::net::SocketAddr;
use tokio::net::TcpStream;

#[cfg(feature = "tls")]
use crate::transport::Certificate;
#[cfg(feature = "tls")]
use std::sync::Arc;
#[cfg(feature = "tls")]
use tokio_rustls::rustls::Certificate;
#[cfg(feature = "tls")]
use tokio_rustls::server::TlsStream;

/// Trait that connected IO resources implement and use to produce info about the connection.
Expand Down Expand Up @@ -126,8 +126,7 @@ where
let inner = inner.connect_info();

let certs = if let Some(certs) = session.peer_certificates() {
let certs = certs.iter().map(Certificate::from_pem).collect();
Some(Arc::new(certs))
Some(Arc::new(certs.to_vec()))
} else {
None
};
Expand Down