Skip to content

Commit 9830c18

Browse files
authored
Support EC and PKCS8 private keys (#316)
* Support EC and PKCS8 private keys * Use iter instead of infinite loop in `load_keys` fn
1 parent bf6efde commit 9830c18

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/tls.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Stream wrapper.
22

3-
use rustls_pemfile::{certs, rsa_private_keys};
3+
use rustls_pemfile::{certs, read_one, Item};
4+
use std::iter;
45
use std::path::Path;
56
use std::sync::Arc;
67
use tokio_rustls::rustls::{self, Certificate, PrivateKey};
@@ -17,9 +18,17 @@ pub fn load_certs(path: &Path) -> std::io::Result<Vec<Certificate>> {
1718
}
1819

1920
pub fn load_keys(path: &Path) -> std::io::Result<Vec<PrivateKey>> {
20-
rsa_private_keys(&mut std::io::BufReader::new(std::fs::File::open(path)?))
21-
.map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidInput, "invalid key"))
22-
.map(|mut keys| keys.drain(..).map(PrivateKey).collect())
21+
let mut rd = std::io::BufReader::new(std::fs::File::open(path)?);
22+
23+
iter::from_fn(|| read_one(&mut rd).transpose())
24+
.filter_map(|item| match item {
25+
Err(err) => Some(Err(err)),
26+
Ok(Item::RSAKey(key)) => Some(Ok(PrivateKey(key))),
27+
Ok(Item::ECKey(key)) => Some(Ok(PrivateKey(key))),
28+
Ok(Item::PKCS8Key(key)) => Some(Ok(PrivateKey(key))),
29+
_ => None,
30+
})
31+
.collect()
2332
}
2433

2534
pub struct Tls {

0 commit comments

Comments
 (0)