Skip to content

Commit 3457f92

Browse files
authoredJun 15, 2024··
feat(tls): Add ability to add multiple ca certificates (#1724)
* feat(tls): Add ability to add multiple ca certificates * feat(tls): Add method to add multiple ca certificates at once
1 parent 53cbd0e commit 3457f92

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed
 

‎tonic/src/transport/channel/tls.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::fmt;
1010
#[derive(Clone, Default)]
1111
pub struct ClientTlsConfig {
1212
domain: Option<String>,
13-
cert: Option<Certificate>,
13+
certs: Vec<Certificate>,
1414
identity: Option<Identity>,
1515
assume_http2: bool,
1616
}
@@ -19,7 +19,7 @@ impl fmt::Debug for ClientTlsConfig {
1919
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2020
f.debug_struct("ClientTlsConfig")
2121
.field("domain", &self.domain)
22-
.field("cert", &self.cert)
22+
.field("certs", &self.certs)
2323
.field("identity", &self.identity)
2424
.finish()
2525
}
@@ -30,7 +30,7 @@ impl ClientTlsConfig {
3030
pub fn new() -> Self {
3131
ClientTlsConfig {
3232
domain: None,
33-
cert: None,
33+
certs: Vec::new(),
3434
identity: None,
3535
assume_http2: false,
3636
}
@@ -46,10 +46,16 @@ impl ClientTlsConfig {
4646

4747
/// Sets the CA Certificate against which to verify the server's TLS certificate.
4848
pub fn ca_certificate(self, ca_certificate: Certificate) -> Self {
49-
ClientTlsConfig {
50-
cert: Some(ca_certificate),
51-
..self
52-
}
49+
let mut certs = self.certs;
50+
certs.push(ca_certificate);
51+
ClientTlsConfig { certs, ..self }
52+
}
53+
54+
/// Sets the multiple CA Certificates against which to verify the server's TLS certificate.
55+
pub fn ca_certificates(self, ca_certificates: impl IntoIterator<Item = Certificate>) -> Self {
56+
let mut certs = self.certs;
57+
certs.extend(ca_certificates);
58+
ClientTlsConfig { certs, ..self }
5359
}
5460

5561
/// Sets the client identity to present to the server.
@@ -75,7 +81,7 @@ impl ClientTlsConfig {
7581
None => uri.host().ok_or_else(Error::new_invalid_uri)?,
7682
};
7783
TlsConnector::new(
78-
self.cert.clone(),
84+
self.certs.clone(),
7985
self.identity.clone(),
8086
domain,
8187
self.assume_http2,

‎tonic/src/transport/service/connector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<C> Connector<C> {
6565
_ => return None,
6666
};
6767

68-
TlsConnector::new(None, None, host, self.assume_http2).ok()
68+
TlsConnector::new(Vec::new(), None, host, self.assume_http2).ok()
6969
}
7070
}
7171

‎tonic/src/transport/service/tls.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub(crate) struct TlsConnector {
3939

4040
impl TlsConnector {
4141
pub(crate) fn new(
42-
ca_cert: Option<Certificate>,
42+
ca_certs: Vec<Certificate>,
4343
identity: Option<Identity>,
4444
domain: &str,
4545
assume_http2: bool,
@@ -53,7 +53,7 @@ impl TlsConnector {
5353
#[cfg(feature = "tls-webpki-roots")]
5454
roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
5555

56-
if let Some(cert) = ca_cert {
56+
for cert in ca_certs {
5757
add_certs_from_pem(&mut Cursor::new(cert), &mut roots)?;
5858
}
5959

0 commit comments

Comments
 (0)
Please sign in to comment.