Skip to content

Commit

Permalink
wip: tls test
Browse files Browse the repository at this point in the history
  • Loading branch information
Alvenix authored and SergioBenitez committed Mar 28, 2024
1 parent 9c747ae commit 7ba1f84
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 16 deletions.
1 change: 1 addition & 0 deletions examples/tls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ publish = false

[dependencies]
rocket = { path = "../../core/lib", features = ["tls", "mtls", "secrets", "http3-preview"] }
rustls = { version = "0.23", features = ["aws_lc_rs"] }
yansi = "1.0.1"
4 changes: 4 additions & 0 deletions examples/tls/Rocket.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ key = "private/ecdsa_nistp256_sha256_key_pkcs8.pem"
certs = "private/ecdsa_nistp384_sha384_cert.pem"
key = "private/ecdsa_nistp384_sha384_key_pkcs8.pem"

[ecdsa_nistp521_sha512_pkcs8.tls]
certs = "private/ecdsa_nistp521_sha512_cert.pem"
key = "private/ecdsa_nistp521_sha512_key_pkcs8.pem"

[ecdsa_nistp256_sha256_sec1.tls]
certs = "private/ecdsa_nistp256_sha256_cert.pem"
key = "private/ecdsa_nistp256_sha256_key_sec1.pem"
Expand Down
Binary file added examples/tls/private/ecdsa_nistp521_sha512.p12
Binary file not shown.
23 changes: 23 additions & 0 deletions examples/tls/private/ecdsa_nistp521_sha512_cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-----BEGIN CERTIFICATE-----
MIID5DCCAcygAwIBAgIUZ2n0Lhg+9cVPCOtK7Ov1D5n58GIwDQYJKoZIhvcNAQEN
BQAwRzELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMRIwEAYDVQQKDAlSb2NrZXQg
Q0ExFzAVBgNVBAMMDlJvY2tldCBSb290IENBMB4XDTI0MDMyNzExMDYzMFoXDTM0
MDMyNTExMDYzMFowPzELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMQ8wDQYDVQQK
DAZSb2Nr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rW+rGKg==
-----END CERTIFICATE-----
8 changes: 8 additions & 0 deletions examples/tls/private/ecdsa_nistp521_sha512_key_pkcs8.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-----BEGIN PRIVATE KEY-----
MIHuAgEAMBAGByqGSM49AgEGBSuBBAAjBIHWMIHTAgEBBEIA3a6vdhcCDC8p/8yG
eDASmWqQdkmgU9kaqqgmBh2gcLhTxxtjwAy8BxBy4UmRFo8VJtFUDLXt8ZR2vzTc
zKybjrmhgYkDgYYABAGF7cnY2iUdwbAujVRDw/MMSKG8yjtNOFo8ihbqZEHLkD4O
Aw8y04poaEsh96IWmF9/fDAUkoMaOQNc029Ewf2JdQGC4j+klpmX3o0XcmbkUEja
zSB9OzCCHXvpfFQ9A1Um/fmXo+XnreLEYHFYMSM30gotZlc0grbW8GoKb2VvxwRB
IA==
-----END PRIVATE KEY-----
24 changes: 24 additions & 0 deletions examples/tls/private/gen_certs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,39 @@ function gen_ecdsa_nistp384_sha384() {
rm ca_cert.srl server.csr ecdsa_nistp384_sha384_key.pem
}

function gen_ecdsa_nistp521_sha512() {
gen_ca_if_non_existent

openssl ecparam -out ecdsa_nistp521_sha512_key.pem -name secp521r1 -genkey

# Convert to pkcs8 format supported by rustls
openssl pkcs8 -topk8 -nocrypt -in ecdsa_nistp521_sha512_key.pem \
-out ecdsa_nistp521_sha512_key_pkcs8.pem

openssl req -new -nodes -sha512 -key ecdsa_nistp521_sha512_key_pkcs8.pem \
-subj "${SUBJECT}" -out server.csr

openssl x509 -req -sha512 -extfile <(printf "subjectAltName=${ALT}") -days 3650 \
-CA ca_cert.pem -CAkey ca_key.pem -CAcreateserial \
-in server.csr -out ecdsa_nistp521_sha512_cert.pem

openssl pkcs12 -export -password pass:rocket -in ecdsa_nistp521_sha512_cert.pem \
-inkey ecdsa_nistp521_sha512_key_pkcs8.pem -out ecdsa_nistp521_sha512.p12

rm ca_cert.srl server.csr ecdsa_nistp521_sha512_key.pem
}

case $1 in
ed25519) gen_ed25519 ;;
rsa_sha256) gen_rsa_sha256 ;;
ecdsa_nistp256_sha256) gen_ecdsa_nistp256_sha256 ;;
ecdsa_nistp384_sha384) gen_ecdsa_nistp384_sha384 ;;
ecdsa_nistp521_sha512) gen_ecdsa_nistp521_sha512 ;;
*)
gen_ed25519
gen_rsa_sha256
gen_ecdsa_nistp256_sha256
gen_ecdsa_nistp384_sha384
gen_ecdsa_nistp521_sha512
;;
esac
42 changes: 26 additions & 16 deletions examples/tls/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ fn insecure_cookies() {
fn hello_world() {
use rocket::listener::DefaultListener;
use rocket::config::{Config, SecretKey};
use rustls::crypto::aws_lc_rs;

let profiles = [
let mut profiles = vec![
"rsa_sha256",
"ecdsa_nistp256_sha256_pkcs8",
"ecdsa_nistp384_sha384_pkcs8",
Expand All @@ -79,20 +80,29 @@ fn hello_world() {
"ed25519",
];

for profile in profiles {
let config = Config {
secret_key: SecretKey::generate().unwrap(),
..Config::debug_default()
};

let figment = Config::figment().merge(config).select(profile);
let client = Client::tracked_secure(super::rocket().configure(figment)).unwrap();
let response = client.get("/").dispatch();
assert_eq!(response.into_string().unwrap(), "Hello, world!");

let figment = client.rocket().figment();
let listener: DefaultListener = figment.extract().unwrap();
assert_eq!(figment.profile(), profile);
listener.tls.as_ref().unwrap().validate().expect("valid TLS config");
for use_aws_lc in [false, true] {
if use_aws_lc {
let crypto_provider = aws_lc_rs::default_provider();
crypto_provider.install_default().unwrap();

profiles.push("ecdsa_nistp521_sha512_pkcs8");
}

for profile in &profiles {
let config = Config {
secret_key: SecretKey::generate().unwrap(),
..Config::debug_default()
};

let figment = Config::figment().merge(config).select(profile);
let client = Client::tracked_secure(super::rocket().configure(figment)).unwrap();
let response = client.get("/").dispatch();
assert_eq!(response.into_string().unwrap(), "Hello, world!");

let figment = client.rocket().figment();
let listener: DefaultListener = figment.extract().unwrap();
assert_eq!(figment.profile(), profile);
listener.tls.as_ref().unwrap().validate().expect("valid TLS config");
}
}
}

0 comments on commit 7ba1f84

Please sign in to comment.