mirror of
https://github.com/eycorsican/leaf.git
synced 2026-12-06 08:02:36 +00:00
fix(config): read an inline private key as a key, not as a filename
A certificate and its key are configured the same way and were read two different ways. `certificate` was recognised inline -- if it contains "-----BEGIN", it is the PEM itself -- and `certificateKey` never was: it went straight to the path branch, so an inline key became a filename made of PEM, joined onto the asset directory. What the operator saw was "no private keys found" about a key that was right there in the configuration. It affected the quic and tls settings on both sides, all four with their own copy of the same twelve lines. There is one resolver now, used for both halves of every keypair, and three tests that say which of the three shapes -- inline, absolute, relative -- goes where. Found by an end-to-end matrix that configures every stack from one self-signed certificate held in memory: with `rawCertificateKey` it worked, with `certificateKey` no quic node would start. Also: the quic inbound now reads a SEC1 key -- "BEGIN EC PRIVATE KEY", what openssl hands out for an elliptic curve by default. The tls inbound has always taken one. That path has no test here; the harness's key is PKCS#8, and the asymmetry is plain enough between the two files.
This commit is contained in:
1 parent
d2fcffbd76
commit
63dceed479
2 files changed
+84
-82
No files matched your search
+67
-80
@@ -529,6 +529,28 @@ fn is_inline_certificate(certificate: &str) -> bool {
|
||||
certificate.contains("-----BEGIN")
|
||||
}
|
||||
|
||||
/// Resolves a certificate or a private key, either of which may be given
|
||||
/// inline or as a path.
|
||||
///
|
||||
/// The two halves of a keypair are configured the same way and have to be read
|
||||
/// the same way. They were not: a certificate was recognised inline and a key
|
||||
/// never was, so an inline key became a path under the asset directory made of
|
||||
/// PEM, and what the operator saw was "no private keys found" about a key that
|
||||
/// was right there in the configuration.
|
||||
fn resolve_certificate(value: &str) -> String {
|
||||
if is_inline_certificate(value) {
|
||||
return value.to_string();
|
||||
}
|
||||
let path = Path::new(value);
|
||||
if path.is_absolute() {
|
||||
return path.to_string_lossy().to_string();
|
||||
}
|
||||
Path::new(&*crate::option::ASSET_LOCATION)
|
||||
.join(path)
|
||||
.to_string_lossy()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn validate_non_empty_str(value: &str, field_name: &str, protocol: &str) -> Result<()> {
|
||||
if value.trim().is_empty() {
|
||||
return Err(anyhow::anyhow!(
|
||||
@@ -846,30 +868,12 @@ pub fn to_internal(mut config: Config) -> Result<internal::Config> {
|
||||
if let Some(ext_raw_certificate) = &ext_settings.raw_certificate {
|
||||
settings.certificate = ext_raw_certificate.join("\n");
|
||||
} else if let Some(ext_certificate) = &ext_settings.certificate {
|
||||
if is_inline_certificate(ext_certificate) {
|
||||
settings.certificate = ext_certificate.clone();
|
||||
} else {
|
||||
let cert = Path::new(&ext_certificate);
|
||||
if cert.is_absolute() {
|
||||
settings.certificate = cert.to_string_lossy().to_string();
|
||||
} else {
|
||||
let asset_loc = Path::new(&*crate::option::ASSET_LOCATION);
|
||||
let path = asset_loc.join(cert).to_string_lossy().to_string();
|
||||
settings.certificate = path;
|
||||
}
|
||||
}
|
||||
settings.certificate = resolve_certificate(ext_certificate);
|
||||
}
|
||||
if let Some(ext_raw_certificate_key) = &ext_settings.raw_certificate_key {
|
||||
settings.certificate_key = ext_raw_certificate_key.join("\n");
|
||||
} else if let Some(ext_certificate_key) = &ext_settings.certificate_key {
|
||||
let key = Path::new(&ext_certificate_key);
|
||||
if key.is_absolute() {
|
||||
settings.certificate_key = key.to_string_lossy().to_string();
|
||||
} else {
|
||||
let asset_loc = Path::new(&*crate::option::ASSET_LOCATION);
|
||||
let path = asset_loc.join(key).to_string_lossy().to_string();
|
||||
settings.certificate_key = path;
|
||||
}
|
||||
settings.certificate_key = resolve_certificate(ext_certificate_key);
|
||||
}
|
||||
if let Some(ext_alpns) = &ext_settings.alpn {
|
||||
for ext_alpn in ext_alpns {
|
||||
@@ -906,30 +910,12 @@ pub fn to_internal(mut config: Config) -> Result<internal::Config> {
|
||||
if let Some(ext_raw_certificate) = &ext_settings.raw_certificate {
|
||||
settings.certificate = ext_raw_certificate.join("\n");
|
||||
} else if let Some(ext_certificate) = &ext_settings.certificate {
|
||||
if is_inline_certificate(ext_certificate) {
|
||||
settings.certificate = ext_certificate.clone();
|
||||
} else {
|
||||
let cert = Path::new(&ext_certificate);
|
||||
if cert.is_absolute() {
|
||||
settings.certificate = cert.to_string_lossy().to_string();
|
||||
} else {
|
||||
let asset_loc = Path::new(&*crate::option::ASSET_LOCATION);
|
||||
let path = asset_loc.join(cert).to_string_lossy().to_string();
|
||||
settings.certificate = path;
|
||||
}
|
||||
}
|
||||
settings.certificate = resolve_certificate(ext_certificate);
|
||||
}
|
||||
if let Some(ext_raw_certificate_key) = &ext_settings.raw_certificate_key {
|
||||
settings.certificate_key = ext_raw_certificate_key.join("\n");
|
||||
} else if let Some(ext_certificate_key) = &ext_settings.certificate_key {
|
||||
let key = Path::new(&ext_certificate_key);
|
||||
if key.is_absolute() {
|
||||
settings.certificate_key = key.to_string_lossy().to_string();
|
||||
} else {
|
||||
let asset_loc = Path::new(&*crate::option::ASSET_LOCATION);
|
||||
let path = asset_loc.join(key).to_string_lossy().to_string();
|
||||
settings.certificate_key = path;
|
||||
}
|
||||
settings.certificate_key = resolve_certificate(ext_certificate_key);
|
||||
}
|
||||
if let Some(ext_ech_config) = &ext_settings.ech_config {
|
||||
settings.ech_config = ext_ech_config.clone();
|
||||
@@ -1182,30 +1168,12 @@ pub fn to_internal(mut config: Config) -> Result<internal::Config> {
|
||||
if let Some(ext_raw_certificate) = &ext_settings.raw_certificate {
|
||||
settings.certificate = ext_raw_certificate.join("\n");
|
||||
} else if let Some(ext_certificate) = &ext_settings.certificate {
|
||||
if is_inline_certificate(ext_certificate) {
|
||||
settings.certificate = ext_certificate.clone();
|
||||
} else {
|
||||
let cert = Path::new(&ext_certificate);
|
||||
if cert.is_absolute() {
|
||||
settings.certificate = cert.to_string_lossy().to_string();
|
||||
} else {
|
||||
let asset_loc = Path::new(&*crate::option::ASSET_LOCATION);
|
||||
let path = asset_loc.join(cert).to_string_lossy().to_string();
|
||||
settings.certificate = path;
|
||||
}
|
||||
}
|
||||
settings.certificate = resolve_certificate(ext_certificate);
|
||||
}
|
||||
if let Some(ext_raw_certificate_key) = &ext_settings.raw_certificate_key {
|
||||
settings.certificate_key = ext_raw_certificate_key.join("\n");
|
||||
} else if let Some(ext_certificate_key) = &ext_settings.certificate_key {
|
||||
let key = Path::new(&ext_certificate_key);
|
||||
if key.is_absolute() {
|
||||
settings.certificate_key = key.to_string_lossy().to_string();
|
||||
} else {
|
||||
let asset_loc = Path::new(&*crate::option::ASSET_LOCATION);
|
||||
let path = asset_loc.join(key).to_string_lossy().to_string();
|
||||
settings.certificate_key = path;
|
||||
}
|
||||
settings.certificate_key = resolve_certificate(ext_certificate_key);
|
||||
}
|
||||
if let Some(ext_insecure) = ext_settings.insecure {
|
||||
settings.insecure = ext_insecure;
|
||||
@@ -1369,30 +1337,12 @@ pub fn to_internal(mut config: Config) -> Result<internal::Config> {
|
||||
if let Some(ext_raw_certificate) = &ext_settings.raw_certificate {
|
||||
settings.certificate = ext_raw_certificate.join("\n");
|
||||
} else if let Some(ext_certificate) = &ext_settings.certificate {
|
||||
if is_inline_certificate(ext_certificate) {
|
||||
settings.certificate = ext_certificate.clone();
|
||||
} else {
|
||||
let cert = Path::new(&ext_certificate);
|
||||
if cert.is_absolute() {
|
||||
settings.certificate = cert.to_string_lossy().to_string();
|
||||
} else {
|
||||
let asset_loc = Path::new(&*crate::option::ASSET_LOCATION);
|
||||
let path = asset_loc.join(cert).to_string_lossy().to_string();
|
||||
settings.certificate = path;
|
||||
}
|
||||
}
|
||||
settings.certificate = resolve_certificate(ext_certificate);
|
||||
}
|
||||
if let Some(ext_raw_certificate_key) = &ext_settings.raw_certificate_key {
|
||||
settings.certificate_key = ext_raw_certificate_key.join("\n");
|
||||
} else if let Some(ext_certificate_key) = &ext_settings.certificate_key {
|
||||
let key = Path::new(&ext_certificate_key);
|
||||
if key.is_absolute() {
|
||||
settings.certificate_key = key.to_string_lossy().to_string();
|
||||
} else {
|
||||
let asset_loc = Path::new(&*crate::option::ASSET_LOCATION);
|
||||
let path = asset_loc.join(key).to_string_lossy().to_string();
|
||||
settings.certificate_key = path;
|
||||
}
|
||||
settings.certificate_key = resolve_certificate(ext_certificate_key);
|
||||
}
|
||||
if let Some(ext_alpns) = &ext_settings.alpn {
|
||||
settings.alpn = ext_alpns.clone();
|
||||
@@ -1627,3 +1577,40 @@ pub fn to_internal(mut config: Config) -> Result<internal::Config> {
|
||||
config.dns = protobuf::MessageField::some(dns);
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const INLINE_KEY: &str = "-----BEGIN PRIVATE KEY-----\nMIGH\n-----END PRIVATE KEY-----\n";
|
||||
|
||||
/// Both halves of a keypair are configured the same way and have to be
|
||||
/// read the same way. The key was not: it was always taken for a path, so
|
||||
/// an inline one became a filename made of PEM under the asset directory,
|
||||
/// and what the operator saw was "no private keys found" about a key that
|
||||
/// was right there in the configuration.
|
||||
#[test]
|
||||
fn an_inline_key_is_not_mistaken_for_a_path() {
|
||||
assert_eq!(resolve_certificate(INLINE_KEY), INLINE_KEY);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_absolute_path_is_left_alone() {
|
||||
assert_eq!(
|
||||
resolve_certificate("/etc/leaf/cert.pem"),
|
||||
"/etc/leaf/cert.pem"
|
||||
);
|
||||
}
|
||||
|
||||
/// A relative path is still resolved against the asset directory, which is
|
||||
/// what makes `"certificate": "cert.pem"` work in a config file.
|
||||
#[test]
|
||||
fn a_relative_path_is_resolved_against_the_asset_directory() {
|
||||
let resolved = resolve_certificate("cert.pem");
|
||||
assert!(
|
||||
resolved.ends_with("cert.pem") && resolved != "cert.pem",
|
||||
"expected a path under the asset directory, got {}",
|
||||
resolved
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ use futures::stream::Stream;
|
||||
use futures::task::{Context, Poll};
|
||||
use quinn::{RecvStream, SendStream};
|
||||
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
|
||||
use rustls_pemfile::{certs, pkcs8_private_keys, rsa_private_keys};
|
||||
use rustls_pemfile::{certs, ec_private_keys, pkcs8_private_keys, rsa_private_keys};
|
||||
use tokio::sync::mpsc::{channel, Receiver, Sender};
|
||||
use tokio::time::{timeout, Duration};
|
||||
use tracing::{debug, trace, warn};
|
||||
@@ -95,7 +95,22 @@ impl Handler {
|
||||
match rsa.into_iter().next() {
|
||||
Some(x) => PrivateKeyDer::Pkcs1(x),
|
||||
None => {
|
||||
return Err(anyhow!("no private keys found",));
|
||||
// A SEC1 key -- "BEGIN EC PRIVATE KEY", what
|
||||
// openssl and rcgen hand out for an elliptic curve
|
||||
// by default. The TLS inbound has always taken
|
||||
// one; without this the same key file works there
|
||||
// and fails here with "no private keys found",
|
||||
// which says nothing about what is wrong with it.
|
||||
let ec = ec_private_keys(&mut io::BufReader::new(&*key))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
match ec.into_iter().next() {
|
||||
Some(x) => PrivateKeyDer::Sec1(x),
|
||||
None => {
|
||||
return Err(anyhow!(
|
||||
"no private key found: expected a PKCS#8, PKCS#1 or SEC1 key"
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user