-
Notifications
You must be signed in to change notification settings - Fork 12
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
[RSDK-9433] - Store TLS certs in DER format to save space #360
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ use std::str::FromStr; | |
use std::{convert::Infallible, error::Error, fmt::Debug, rc::Rc, sync::Mutex}; | ||
|
||
use hyper::Uri; | ||
use rustls_pki_types::pem::PemObject; | ||
use rustls_pki_types::{CertificateDer, PrivateKeyDer}; | ||
|
||
use crate::{common::grpc::ServerError, proto::app::v1::RobotConfig}; | ||
|
||
|
@@ -90,12 +92,18 @@ pub struct TlsCertificate { | |
pub(crate) private_key: Vec<u8>, | ||
} | ||
|
||
impl From<CertificateResponse> for TlsCertificate { | ||
fn from(resp: CertificateResponse) -> Self { | ||
Self { | ||
certificate: resp.tls_certificate.into_bytes(), | ||
private_key: resp.tls_private_key.into_bytes(), | ||
} | ||
impl TryFrom<CertificateResponse> for TlsCertificate { | ||
type Error = rustls_pki_types::pem::Error; | ||
fn try_from(resp: CertificateResponse) -> Result<Self, Self::Error> { | ||
// we convert the certificate and private key from PEM to DER format to save space | ||
let private_key_bytes = resp.tls_private_key.into_bytes(); | ||
let private_key = PrivateKeyDer::from_pem_slice(&private_key_bytes[0..])?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this range should just work with |
||
let cert_bytes = resp.tls_certificate.into_bytes(); | ||
let cert = CertificateDer::from_pem_slice(&cert_bytes[0..])?; | ||
Ok(Self { | ||
certificate: cert.to_vec(), | ||
private_key: private_key.secret_der().to_vec(), | ||
}) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,7 +83,7 @@ impl Esp32ServerConfig { | |
cacert_bytes: 0, | ||
}, | ||
__bindgen_anon_3: crate::esp32::esp_idf_svc::sys::esp_tls_cfg_server__bindgen_ty_3 { | ||
// This is the server certificates in the PEM format | ||
// This is the server certificates in the DER format | ||
servercert_buf: srv_cert.as_ptr(), | ||
}, | ||
__bindgen_anon_4: crate::esp32::esp_idf_svc::sys::esp_tls_cfg_server__bindgen_ty_4 { | ||
|
@@ -439,23 +439,23 @@ impl<IO> From<Esp32ClientTlsStream<IO>> for Esp32TlsStream<IO> { | |
|
||
#[derive(Default)] | ||
pub struct Esp32H2Connector { | ||
srv_cert: Option<CString>, | ||
srv_key: Option<CString>, | ||
srv_cert: Option<Vec<u8>>, | ||
srv_key: Option<Vec<u8>>, | ||
} | ||
|
||
impl ViamH2Connector for Esp32H2Connector { | ||
fn set_server_certificates(&mut self, srv_cert: Vec<u8>, srv_key: Vec<u8>) { | ||
let _ = self.srv_cert.replace(CString::new(srv_cert).unwrap()); | ||
let _ = self.srv_key.replace(CString::new(srv_key).unwrap()); | ||
let _ = self.srv_cert.replace(srv_cert); | ||
let _ = self.srv_key.replace(srv_key); | ||
Comment on lines
+448
to
+449
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
} | ||
fn accept_connection( | ||
&self, | ||
connection: Async<TcpStream>, | ||
) -> Result<std::pin::Pin<Box<dyn IntoHttp2Stream>>, std::io::Error> { | ||
if self.srv_cert.is_some() && self.srv_key.is_some() { | ||
let cfg = Esp32ServerConfig::new( | ||
self.srv_cert.as_ref().unwrap().to_bytes_with_nul(), | ||
self.srv_key.as_ref().unwrap().to_bytes_with_nul(), | ||
&self.srv_cert.as_ref().unwrap(), | ||
&self.srv_key.as_ref().unwrap(), | ||
Comment on lines
+457
to
+458
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
); | ||
let conn = Esp32Accept::new(connection, cfg)?; | ||
Ok(Box::pin(Esp32StreamAcceptor(conn))) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,23 +28,15 @@ impl ViamH2Connector for NativeH2Connector { | |
connection: Async<TcpStream>, | ||
) -> Result<std::pin::Pin<Box<dyn IntoHttp2Stream>>, std::io::Error> { | ||
if self.srv_cert.is_some() && self.srv_key.is_some() { | ||
let cert_chain = rustls_pemfile::certs(&mut BufReader::new( | ||
self.srv_cert.as_ref().unwrap().as_slice(), | ||
)) | ||
.map(|c| rustls::Certificate(c.unwrap().to_vec())) | ||
.collect(); | ||
let priv_keys = rustls_pemfile::private_key(&mut BufReader::new( | ||
self.srv_key.as_ref().unwrap().as_slice(), | ||
)) | ||
.unwrap() | ||
.map(|k| rustls::PrivateKey(k.secret_der().to_vec())); | ||
let priv_keys = rustls::PrivateKey(self.srv_key.clone().unwrap()); | ||
let cert_chain = vec![rustls::Certificate(self.srv_cert.clone().unwrap())]; | ||
Comment on lines
+31
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here with unwraps |
||
let mut cfg = ServerConfig::builder() | ||
.with_safe_default_cipher_suites() | ||
.with_safe_default_kx_groups() | ||
.with_protocol_versions(&[&rustls::version::TLS12]) | ||
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))? | ||
.with_no_client_auth() | ||
.with_single_cert(cert_chain, priv_keys.unwrap()) | ||
.with_single_cert(cert_chain, priv_keys) | ||
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))?; | ||
cfg.alpn_protocols = vec!["h2".as_bytes().to_vec()]; | ||
Ok(Box::pin(NativeStreamAcceptor( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: join these