-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): add a "helper" feature for TlsIdentityCtx
This snippet is useful for server implementations, as long as the server must be configured with a tokio-rustls TlsAcceptor, and not directly with certificate paths for examples. Signed-off-by: Marc-André Lureau <[email protected]>
- Loading branch information
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use std::{fs::File, io::BufReader, path::Path, sync::Arc}; | ||
|
||
use anyhow::Context; | ||
use rustls_pemfile::{certs, pkcs8_private_keys}; | ||
use tokio_rustls::{rustls, TlsAcceptor}; | ||
|
||
pub struct TlsIdentityCtx { | ||
pub cert: rustls::pki_types::CertificateDer<'static>, | ||
pub priv_key: rustls::pki_types::PrivateKeyDer<'static>, | ||
pub pub_key: Vec<u8>, | ||
} | ||
|
||
impl TlsIdentityCtx { | ||
pub fn init_from_paths(cert_path: &Path, key_path: &Path) -> anyhow::Result<Self> { | ||
use x509_cert::der::Decode as _; | ||
|
||
let cert = certs(&mut BufReader::new(File::open(cert_path)?)) | ||
.next() | ||
.context("no certificate")??; | ||
|
||
let pub_key = { | ||
let cert = x509_cert::Certificate::from_der(&cert).map_err(std::io::Error::other)?; | ||
cert.tbs_certificate | ||
.subject_public_key_info | ||
.subject_public_key | ||
.as_bytes() | ||
.ok_or_else(|| std::io::Error::other("subject public key BIT STRING is not aligned"))? | ||
.to_owned() | ||
}; | ||
|
||
let priv_key = pkcs8_private_keys(&mut BufReader::new(File::open(key_path)?)) | ||
.next() | ||
.context("no private key")? | ||
.map(rustls::pki_types::PrivateKeyDer::from)?; | ||
|
||
Ok(Self { | ||
cert, | ||
priv_key, | ||
pub_key, | ||
}) | ||
} | ||
|
||
pub fn make_acceptor(&self) -> anyhow::Result<TlsAcceptor> { | ||
let mut server_config = rustls::ServerConfig::builder() | ||
.with_no_client_auth() | ||
.with_single_cert(vec![self.cert.clone()], self.priv_key.clone_key()) | ||
.context("bad certificate/key")?; | ||
|
||
// This adds support for the SSLKEYLOGFILE env variable (https://wiki.wireshark.org/TLS#using-the-pre-master-secret) | ||
server_config.key_log = Arc::new(rustls::KeyLogFile::new()); | ||
|
||
Ok(TlsAcceptor::from(Arc::new(server_config))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters