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

Issue 354/support dynamic issuer in tls cert issuer logic #365

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ required-features = ["tcp"]
name = "tcp_listener_layers"
required-features = ["tcp"]

[[example]]
name = "tls_boring_dynamic_certs"
required-features = ["boring", "http-full"]

[[example]]
name = "tls_boring_termination"
required-features = ["boring", "haproxy", "http-full"]
Expand Down
28 changes: 28 additions & 0 deletions examples/assets/example.com.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN CERTIFICATE-----
MIIE3DCCAsSgAwIBAgIJAN4TBpLFs4VhMA0GCSqGSIb3DQEBCwUAMBYxFDASBgNV
BAMMC2V4YW1wbGUuY29tMB4XDTI0MTIwOTIwMDUxN1oXDTM0MTIwNzIwMDUxN1ow
FjEUMBIGA1UEAwwLZXhhbXBsZS5jb20wggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAw
ggIKAoICAQC14A6yHqrF+5+VPljtBd9vgjTQxBCqQ7Af/7cNlFtZjOKmXz0bOCfZ
RjaxNNjjveztFH+VhRpH/JyM7Qd7R0FX84IyH4Z9a58jgKW/l/YM1Q4Y50WGpM9S
k5p9Q8xTWIoZPrjvh6zV4PKef87LxxqoO9QXv34d5g7dsQLbSwJ93SeggH0E5e1V
vP1DW0kvu1BF6rsmF5eTyK/VNg/el9mGyMbcyhBKTpTyVT2FQYRFuZtHXHRnAocC
dv887c/TsYVDffTwv7peVoOotO0twKn0SMdtybiNJyDEdcgw2bFbQu7oV/95cBur
pxePzED31E64QI8emTvZ62L/c5QvP0OY3x2CSb5ctd6z7wWTJ8wkl7N8+y7Xgn1a
AAfki4rWk5qfWAO3BNZo/TGyiWeo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UzD12
-----END CERTIFICATE-----
52 changes: 52 additions & 0 deletions examples/assets/example.com.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-----BEGIN PRIVATE KEY-----
MIIJQQIBADANBgkqhkiG9w0BAQEFAASCCSswggknAgEAAoICAQC14A6yHqrF+5+V
PljtBd9vgjTQxBCqQ7Af/7cNlFtZjOKmXz0bOCfZRjaxNNjjveztFH+VhRpH/JyM
7Qd7R0FX84IyH4Z9a58jgKW/l/YM1Q4Y50WGpM9Sk5p9Q8xTWIoZPrjvh6zV4PKe
f87LxxqoO9QXv34d5g7dsQLbSwJ93SeggH0E5e1VvP1DW0kvu1BF6rsmF5eTyK/V
Ng/el9mGyMbcyhBKTpTyVT2FQYRFuZtHXHRnAocCdv887c/TsYVDffTwv7peVoOo
tO0twKn0SMdtybiNJyDEdcgw2bFbQu7oV/95cBurpxePzED31E64QI8emTvZ62L/
c5QvP0OY3x2CSb5ctd6z7wWTJ8wkl7N8+y7Xgn1aAAfki4rWk5qfWAO3BNZo/TGy
iWeoNttJ+NddfwI3+h6phK7X56vRhYSqwSnxWyQYlTJAnFQb7TMEP/k9ov2S9MzL
TURLLeNiiXjvkOxi+12HzhlTNgk3X49y9f8PLxkNw37TghunAl4OvA+LdslSayFM
mZbx9fm+6ZGkjcsDYnf1Mff+aoCkkUcAFg5DQFDkmvu08mJL2D+9I9OK/Yvn/qXW
jhVdWLJ/k5hrQmLIs1KbQtlGvvYeC7kHY3yBK+3wt/Cnx9qwhOPJcufuEChiMcVc
seGAZJhUT7gQM22v9jb9QZfhihGMpQIDAQABAoICAAEwEWiAYsUgO46R9aq2mxrz
Urz6StGR15id5ugjy+Tt0p0bih6fY0M7/idyHOh/2QhDVgH3+I/3yl3xPldDw917
LaJ+KjaveT6WHH7/0w+KZKks+XtDoEb9x8iBpNf2gBBiJHsL+8j5yxvV4dfa2QW4
Pk5ZZc10Lyyrd0VPXBjOVO/rZuWdSsuAjHbgJb42DvQl8ErzZH4GlX9i9v+RJ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JcR7X3T+SmKfm+2LY6SIPtMXpxGwJFsI96BSB1HqdCPuKC
SQvP1onQOUBQmL9jamsFj6h/3TKn
-----END PRIVATE KEY-----
29 changes: 29 additions & 0 deletions examples/assets/second_example.com.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-----BEGIN CERTIFICATE-----
MIIE+DCCAuCgAwIBAgIJAI/8Ipq5D4qSMA0GCSqGSIb3DQEBCwUAMB0xGzAZBgNV
BAMMEnNlY29uZC5leGFtcGxlLmNvbTAeFw0yNDEyMDkyMDA4MTFaFw0zNDEyMDcy
MDA4MTFaMB0xGzAZBgNVBAMMEnNlY29uZC5leGFtcGxlLmNvbTCCAiIwDQYJKoZI
hvcNAQEBBQADggIPADCCAgoCggIBAND3iExTF4OQSwNIwj154FM9QO486/RJG4oJ
5ptSykilzMpko0sH4ElSi7YRVaBmOnt73Hv7dRU6wUUkC/o1PeZG0DZEbCZSdH0F
UE2GPLCboPM7z1VYFLaXFsMzOP5m58sRGv7DT6Fwc/lrH5dW1bDSEj/MqruX9ghr
0ZwjmHpd9fq+mzI6Uj/mmJzNp2jfvxjUlcj0cFmi54RnZ4sNySZsjnEaq9P/vNZI
BcUiixB/LQ+3dJeWL+QeRgNGprmvHXhPaVY+vCfqaGJHbSamb4lJOxjmJMdtyZj3
9boG8BQ3ZO/NL8YbH9PSVcdgYPFEC+3FpGHMPwNjyiHWuiNFd5Sc7RQMCW5wmhxt
THT8pPLG4eAMIR6hNYQ4kVkIJQReU731DvIV/ZL1vHtMkLDQ2wz8e5cghyXPE279
t90aFmBvgxYUkEBdZbdbh4AfXppPqo+z07Gn+f4Xwm36vZHOxfYRqYDuyFAMzEvU
shR7l2k94am9oowmpkW5J9yMb3sPYyhmqrUcXIZBP3zLHalUXlBMlFoVqh+OC6Yf
E2BgBJmtwx+CHCwXgQ635pBOJBkdl4Auxm056fPkRMcCmNvnX+5QHgxzAUYaNvg3
6tLkuEegQpzYYx/tp8serktspZxoUz1wC7DhP9sVnkTz5Bf6wiLTF1Heefv99Qoc
sk4c87BVAgMBAAGjOzA5MDcGA1UdEQQwMC6CEnNlY29uZC5leGFtcGxlLmNvbYIS
c2Vjb25kLmV4YW1wbGUuY29thwQKAAABMA0GCSqGSIb3DQEBCwUAA4ICAQBfdZYx
PsAzS6RQeimf3JS/A6cxV6d9oLOUYRWiGZsQmQXWG8cO23In4proJGrMDTH1wOR8
WqYxZdSyVT1oJjHdQ/bIYzm/Qf3oLHfikjs0ERNN1c2Xcl1QNxa7fR+R0PEBEnQ3
cwM+562bSVeGKGAl/Ffb/ZRbBo7tyTavujt0/WmCRY0PUUxKZ6Ydq0TYjayw/iL5
eWgtOpGgNxb1P8vLmnXO1Ihq8rK2nwXjAfaotNU3clzcwOlmf82ek8ssirqYhaw0
lkrCbiIspCKU8q8A4i+WDosase/B9SSF6fRFXJ3j6+opByARFekpBiorvovO/zW7
pZsJ/3AISQtLdbTlXi+2+m2EzgEgje5JMZX7mnBPnpBiBZuYBUYy0As0ZgMXn8OC
vQ5b6wc5yckzjIxfGpvNR6t40xQuT2zI5oPd7mgWHX0zbWAxEFklKQLiXDy96nBP
DcLfh30ZxRRVhreMxuwylEsmoZqzyTFANsJ3e8QnEJovEBF0gSAlUPXSQXqjFhgV
4upYLDIus6ll2gJJZ+yaIm8GrX2V+m9q9OkxwrNjDyFMHP1BVYmPvBKgcJ45WR3R
6blrUW6IJinkzMjcRfFwh+yPTbTeuL6KRUvRRQ1DA6R83HNLOGOKFZkWnTpD7JqP
z7jPWvEet9dRSBu/WlegMkCTg4q4hfVVl7ygDw==
-----END CERTIFICATE-----
52 changes: 52 additions & 0 deletions examples/assets/second_example.com.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-----BEGIN PRIVATE KEY-----
MIIJQwIBADANBgkqhkiG9w0BAQEFAASCCS0wggkpAgEAAoICAQDQ94hMUxeDkEsD
SMI9eeBTPUDuPOv0SRuKCeabUspIpczKZKNLB+BJUou2EVWgZjp7e9x7+3UVOsF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E40xjAjhCyX
mLSKRnundSD82McZhLfJkSeTClA+dm3tGrCTJtTXyUNPVmEG0ftVLnOYaWTZ0fHG
fOsxImQPjKjINF94smzq4WoKIm26+m06pysXDCI8oPjOaeEtLK44ra08Wa68xnbc
7qoF9rJ2b1Ws4PP4XjYc1I8FZQl2Xs4pT6oX60MzBMq4HVGAkDwQFHwghGy9hA0U
6iTfXlj3qgbq9jNHupCfHpurwUJ0NWsz5TcwVz/RAoIBAQC+f6napjDta8Fd2frY
Op7d9u4GNPdNm3He47cUAWhGPjy5tl/A+Kl4WBUJgQNwP6SCox/rdwUDD7wXXV6r
g1frXczkEnOyFWAJV6eZAhXqtmmTyHuVnSq2vjDdf06GSV8YhCnPBeuycTbt6wEN
0kdAKBD6hsbNHYQawajdSHOnERuLxWyYrBprRjKm1+A7EHb10jBNzFAG10sGF41O
YsZZejnNhJyiaQq+YmJ/4XJ2wZ3RcYoXRGO5EXZR/+mIClN/6PU2ZGsABy3dAEzw
O0lfVid6EujI977tEY7T9gJH8lbAeHFUCUjxKE7o/GcB7bs7l4rMMYo99moepg+x
RZ4ZAoIBABhyys0StLGNXbCldq1c0yOYrgo4b7R8etViAnWVngdjdf0oYlzE5GeK
x5KdHCLx933QWpx5ebsaLlgnrY+r8efth+dFSseSP0u0rpY3WH0UqVOCC/NvKTQC
PQAmZKCf7v/NKKaoxqAHNFilZFr4WPy7EQLDfrNTd3IhLjYWHzTS5hHHnX+Ilvjd
5cwsCN4QEUgwBvB0sOAD2UrXSVYu+zFXgvWd27IyI3+ibCXE+jRu8z5quev06+A8
tbHPV6tIVqshz5FGhD3bKgDoyynDdWCklTKtovfdlMN/on7hsopJd3EmCo7+pnal
nzMywIv3nYFETPqCte37rv3CXyuap50=
-----END PRIVATE KEY-----
214 changes: 214 additions & 0 deletions examples/tls_boring_dynamic_certs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
//! This example demonstrates how to dynamically choose certificates for incomming requests
//!
//! # Run the example
//!
//! ```sh
//! cargo run --example tls_boring_dynamic_certs --features=boring,http-full
//! ```
//!
//! Test if the correct certificates are returned by making curl resolve example and second.example to
//! the localhost address on which we expose this service.
//!
//! Example certificate:
//! ```sh
//! curl -vik --resolve example:64801:127.0.0.1 https://example:64801
//! ```
//! Output
//! ```
//! * Server certificate:
//! * subject: CN=example.com
//! * start date: Dec 9 20:05:17 2024 GMT
//! * expire date: Dec 7 20:05:17 2034 GMT
//! * issuer: CN=example.com
//! * SSL certificate verify result: self signed certificate (18), continuing anyway.
//! ```
//!
//! Second example certificate:
//! ```sh
//! curl -vik --resolve second.example:64801:127.0.0.1 https://second.example:64801
//! ```
//! Output
//! ```
//! * Server certificate:
//! * subject: CN=second.example.com
//! * start date: Dec 9 20:08:11 2024 GMT
//! * expire date: Dec 7 20:08:11 2034 GMT
//! * issuer: CN=second.example.com
//! * SSL certificate verify result: self signed certificate (18), continuing anyway.
//! ```
//!
//! Fallback to to default (example certificate) if no matches are found:
//! ```sh
//! curl -vik https://127.0.0.1:64801
//! ```
//! Output
//! ```
//! * Server certificate:
//! * subject: CN=example.com
//! * start date: Dec 9 20:05:17 2024 GMT
//! * expire date: Dec 7 20:05:17 2034 GMT
//! * issuer: CN=example.com
//! * SSL certificate verify result: self signed certificate (18), continuing anyway.
//! ```

// these dependencies are re-exported by rama for your convenience,
// as to make it easy to use them and ensure that the versions remain compatible
// (given most do not have a stable release yet)

// rama provides everything out of the box to build a TLS termination proxy
use rama::{
error::OpaqueError,
graceful::Shutdown,
http::server::HttpServer,
http::{Request, Response},
layer::ConsumeErrLayer,
net::{
address::{Domain, Host},
tls::server::{ServerAuth, ServerConfig},
tls::{
client::ClientHello,
server::{DynamicCertIssuer, ServerAuthData, ServerCertIssuerData},
DataEncoding,
},
},
rt::Executor,
service::service_fn,
tcp::server::TcpListener,
tls::boring::server::{TlsAcceptorData, TlsAcceptorLayer},
Context, Layer,
};
use rama_http::IntoResponse;
use rama_net::tls::server::CacheKind;

// everything else is provided by the standard library, community crates or tokio
use std::{convert::Infallible, time::Duration};
use tracing::metadata::LevelFilter;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

#[tokio::main]
async fn main() {
tracing_subscriber::registry()
.with(fmt::layer())
.with(
EnvFilter::builder()
.with_default_directive(LevelFilter::DEBUG.into())
.from_env_lossy(),
)
.init();

let issuer = DynamicIssuer::new();

let tls_server_config = ServerConfig::new(ServerAuth::CertIssuer(ServerCertIssuerData {
kind: issuer.into(),
cache_kind: CacheKind::Disabled,
..Default::default()
}));

let acceptor_data = TlsAcceptorData::try_from(tls_server_config).expect("create acceptor data");

let shutdown = Shutdown::default();

// create http server
shutdown.spawn_task_fn(|guard| async {
let exec = Executor::graceful(guard.clone());
let http_service = HttpServer::auto(exec).service(service_fn(http_service));

let tcp_service = (
ConsumeErrLayer::default(),
TlsAcceptorLayer::new(acceptor_data),
)
.layer(http_service);

TcpListener::bind("127.0.0.1:64801")
.await
.expect("bind TCP Listener: http")
.serve_graceful(guard, tcp_service)
.await;
});

shutdown
.shutdown_with_limit(Duration::from_secs(3))
.await
.expect("graceful shutdown");
}

struct DynamicIssuer {
example_data: ServerAuthData,
second_example_data: ServerAuthData,
default_data: ServerAuthData,
}

impl DynamicIssuer {
fn new() -> Self {
Self {
example_data: example_self_signed_auth().expect("load example data"),
second_example_data: second_example_self_signed_auth()
.expect("load second example data"),
default_data: example_self_signed_auth().expect("load default data"),
}
}
}

impl DynamicCertIssuer for DynamicIssuer {
async fn issue_cert(
&self,
client_hello: ClientHello,
_server_name: Option<Host>,
) -> Result<ServerAuthData, OpaqueError> {
match client_hello.ext_server_name() {
Some(host) => match host {
rama_net::address::Host::Name(domain) => {
if domain == &Domain::from_static("example") {
return Ok(self.example_data.clone());
} else if domain == &Domain::from_static("second.example") {
return Ok(self.second_example_data.clone());
}
Ok(self.example_data.clone())
}
rama_net::address::Host::Address(_ip_addr) => Ok(self.default_data.clone()),
},
None => Ok(self.example_data.clone()),
}
}
}

pub fn example_self_signed_auth() -> Result<ServerAuthData, OpaqueError> {
Ok(ServerAuthData {
private_key: DataEncoding::Pem(
std::str::from_utf8(include_bytes!("./assets/example.com.key"))
soundofspace marked this conversation as resolved.
Show resolved Hide resolved
.expect("should decode")
.try_into()
.expect("should work"),
),
cert_chain: DataEncoding::Pem(
std::str::from_utf8(include_bytes!("./assets/example.com.crt"))
.expect("should decode")
.try_into()
.expect("should work"),
),
ocsp: None,
})
}

pub fn second_example_self_signed_auth() -> Result<ServerAuthData, OpaqueError> {
Ok(ServerAuthData {
private_key: DataEncoding::Pem(
include_str!("./assets/second_example.com.key")
.try_into()
.expect("should work"),
),
cert_chain: DataEncoding::Pem(
include_str!("./assets/second_example.com.crt")
.try_into()
.expect("should work"),
),
ocsp: None,
})
}

async fn http_service<S>(_ctx: Context<S>, _request: Request) -> Result<Response, Infallible> {
Ok(
"hello client, you were served by boring tls terminator proxy issuing a dynamic certificate"
.into_response(),
)
}
Loading
Loading