fixed #1077 - handle non-standard PKCS8 EC private key PEMs

This commit is contained in:
Eugene 2024-10-04 17:18:43 +02:00
parent bb285ccc60
commit 38bdbade69
No known key found for this signature in database
GPG key ID: 5896FCBBDD1CF4F4
3 changed files with 13 additions and 0 deletions

1
Cargo.lock generated
View file

@ -5762,6 +5762,7 @@ dependencies = [
name = "warpgate-common"
version = "0.10.2"
dependencies = [
"aho-corasick",
"anyhow",
"argon2",
"async-trait",

View file

@ -43,3 +43,4 @@ warpgate-sso = { version = "*", path = "../warpgate-sso" }
rustls = { version = "0.23", features = ["ring"], default-features = false}
rustls-pemfile = "1.0"
webpki = "0.22"
aho-corasick = "1.1.3"

View file

@ -1,6 +1,7 @@
use std::path::Path;
use std::sync::Arc;
use aho_corasick::AhoCorasick;
use poem::listener::RustlsCertificate;
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
use rustls::sign::{CertifiedKey, SigningKey};
@ -58,6 +59,16 @@ impl TlsPrivateKey {
}
pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, RustlsSetupError> {
let bytes = {
// https://github.com/rustls/rustls/issues/767
let ac = AhoCorasick::new(&[b"EC PRIVATE KEY"]).expect("EC PK AhoCorasick");
let mut new_bytes = vec![];
ac.replace_all_with_bytes(&bytes, &mut new_bytes, |_, _, dst| {
dst.extend_from_slice(b"PRIVATE KEY");
true
});
new_bytes
};
let mut key = rustls_pemfile::pkcs8_private_keys(&mut bytes.as_slice())?
.drain(..)
.next()