Skip to content

Commit e618585

Browse files
committed
examples: prefer pemfile::private_key
Using `rustls_pemfile::rsa_private_keys` is overly specific, limiting use to just RSA keys with PKCS#1 encoding. This commit switches to using `rustls_pemfile::private_key` which is tailor made for loading one private key in any of the supported formats.
1 parent 941387a commit e618585

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

examples/server.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use std::fs::File;
2-
use std::io::{self, BufReader};
2+
use std::io::{self, BufReader, ErrorKind};
33
use std::net::ToSocketAddrs;
44
use std::path::{Path, PathBuf};
55
use std::sync::Arc;
66

77
use argh::FromArgs;
88
use pki_types::{CertificateDer, PrivateKeyDer};
9-
use rustls_pemfile::{certs, rsa_private_keys};
9+
use rustls_pemfile::{certs, private_key};
1010
use tokio::io::{copy, sink, split, AsyncWriteExt};
1111
use tokio::net::TcpListener;
1212
use tokio_rustls::{rustls, TlsAcceptor};
@@ -35,11 +35,13 @@ fn load_certs(path: &Path) -> io::Result<Vec<CertificateDer<'static>>> {
3535
certs(&mut BufReader::new(File::open(path)?)).collect()
3636
}
3737

38-
fn load_keys(path: &Path) -> io::Result<PrivateKeyDer<'static>> {
39-
rsa_private_keys(&mut BufReader::new(File::open(path)?))
40-
.next()
38+
fn load_key(path: &Path) -> io::Result<PrivateKeyDer<'static>> {
39+
Ok(private_key(&mut BufReader::new(File::open(path)?))
4140
.unwrap()
42-
.map(Into::into)
41+
.ok_or(io::Error::new(
42+
ErrorKind::Other,
43+
"no private key found".to_string(),
44+
))?)
4345
}
4446

4547
#[tokio::main]
@@ -52,7 +54,7 @@ async fn main() -> io::Result<()> {
5254
.next()
5355
.ok_or_else(|| io::Error::from(io::ErrorKind::AddrNotAvailable))?;
5456
let certs = load_certs(&options.cert)?;
55-
let key = load_keys(&options.key)?;
57+
let key = load_key(&options.key)?;
5658
let flag_echo = options.echo_mode;
5759

5860
let config = rustls::ServerConfig::builder()

0 commit comments

Comments
 (0)