Skip to content

Commit

Permalink
rustls: add pkcs8 and sec1 keys support
Browse files Browse the repository at this point in the history
  • Loading branch information
blackbeam committed May 20, 2024
1 parent 60ce011 commit 4f5f0d4
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 deletions.
81 changes: 76 additions & 5 deletions src/conn/opts/rustls_opts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(feature = "rustls-tls")]

use rustls::pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs1KeyDer};
use rustls_pemfile::{certs, rsa_private_keys};
use rustls_pemfile::{certs, ec_private_keys, pkcs8_private_keys, rsa_private_keys};

use std::{borrow::Cow, path::Path};

Expand Down Expand Up @@ -70,13 +70,84 @@ impl ClientIdentity {
}

let mut priv_key = None;
for key in rsa_private_keys(&mut &*key_data).into_iter().take(1) {

for key in rsa_private_keys(&mut &*key_data).take(1) {
priv_key = Some(PrivateKeyDer::Pkcs1(key?.clone_key()));
}

let priv_key =
priv_key.unwrap_or_else(|| PrivateKeyDer::Pkcs1(PrivatePkcs1KeyDer::from(key_data)));
if priv_key.is_none() {
for key in pkcs8_private_keys(&mut &*key_data).take(1) {
priv_key = Some(PrivateKeyDer::Pkcs8(key?.clone_key()))
}
}

if priv_key.is_none() {
for key in ec_private_keys(&mut &*key_data).take(1) {
priv_key = Some(PrivateKeyDer::Sec1(key?.clone_key()))
}
}

if let Some(priv_key) = priv_key {
return Ok((cert_chain, dbg!(priv_key)));
}

match PrivateKeyDer::try_from(key_data.as_slice()) {
Ok(key) => Ok((cert_chain, key.clone_key())),
Err(_) => Ok((
cert_chain,
PrivateKeyDer::Pkcs1(PrivatePkcs1KeyDer::from(key_data)),
)),
}
}
}

#[cfg(test)]
mod tests {
use std::path::Path;

use rustls::pki_types::PrivateKeyDer;

use crate::ClientIdentity;

#[test]
fn load_pkcs1() {
let (_certs, key_pem) = ClientIdentity::new(
Path::new("tests/client.crt"),
Path::new("tests/client-key.pem"),
)
.load()
.unwrap();
assert!(matches!(key_pem, PrivateKeyDer::Pkcs1(_)));

let (_certs, key_der) = ClientIdentity::new(
Path::new("tests/client.crt"),
Path::new("tests/client-key.pem"),
)
.load()
.unwrap();
assert!(matches!(key_der, PrivateKeyDer::Pkcs1(_)));

assert_eq!(key_der, key_pem);
}

#[test]
fn load_pkcs8() {
let (_certs, key_der) = ClientIdentity::new(
Path::new("tests/client.crt"),
Path::new("tests/client-key.pkcs8.der"),
)
.load()
.unwrap();
assert!(matches!(key_der, PrivateKeyDer::Pkcs8(_)));

let (_certs, key_pem) = ClientIdentity::new(
Path::new("tests/client.crt"),
Path::new("tests/client-key.pkcs8.pem"),
)
.load()
.unwrap();
assert!(matches!(key_pem, PrivateKeyDer::Pkcs8(_)));

Ok((cert_chain, priv_key))
assert_eq!(key_der, key_pem);
}
}
Binary file added tests/client-key.der
Binary file not shown.
Binary file added tests/client-key.pkcs8.der
Binary file not shown.
28 changes: 28 additions & 0 deletions tests/client-key.pkcs8.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCgcITynCAG+50L
jeUPc/5BuqUfrouuNvUwnyuRL7gkgVJooi9XfYch5caGTehpDoIaj4TydKJOg/ju
z0a82TzoAYUum8xtBVKWI/GUQauaJVw/LKyYL0WYdoRbrxdApIzG3B/j80MLWCwj
CdPiN+bMXxUtpNAIsP0o+v6j/BpGXdwcvc/WoyeC61iwaayWljlf1Fb0LQDCQ//5
1Jea5wVbsFq4r5/06Vc4qWhkrDXcFjRqNPevPoMn4sr+pgC8AzQWOXnCN9VEaqg5
M3POLGZsoup0B23y/NeaoBCYzL6DpbgLZmv6ChDmrmY+u89JxLi67CItvukJSo+B
r32vKJCLAgMBAAECggEAYlx5dY/4Jq/N2U6V90JiyANYxrKpGhbBfZyoBiveVise
zd1Y7uebmFGZw68x5G6rnwMMO+T7uO06CZb0REVfDNIZx7hmvRP9TOUe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jkYvOEg+rt4CDQhdahn4CQ
Nt8w3vB4qGR07wWFu4V/DA==
-----END PRIVATE KEY-----

0 comments on commit 4f5f0d4

Please sign in to comment.