Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ECC Key Configuration with rustls #743

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions rumqttd/src/server/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ impl TLSAcceptor {
.collect();

// Get private key
let key = first_private_key_in_pemfile(key_path)
.map_err(|_| Error::InvalidServerKey(key_path.clone()))?;
let key = first_private_key_in_pemfile(key_path)?;

(certs, key)
};
Expand Down Expand Up @@ -228,29 +227,24 @@ fn first_private_key_in_pemfile(key_path: &String) -> Result<PrivateKey, Error>
let key_file = File::open(key_path);
let key_file = key_file.map_err(|_| Error::ServerKeyNotFound(key_path.clone()))?;

// Let's iterate over the keys with read_one/read_all
let rd = &mut BufReader::new(key_file);

// keep reading Items one by one to find a Key, return error if none found.
loop {
match rustls_pemfile::read_one(rd) {
Ok(maybe_inner) => match maybe_inner {
Some(item) => match item {
// If we find a private key, return it
Item::ECKey(key) | Item::RSAKey(key) | Item::PKCS8Key(key) => {
return Ok(PrivateKey(key))
}
// Otherwise, continue
_ => {}
},
None => {
error!("No private key found in {:?}", key_path);
return Err(Error::InvalidServerKey(key_path.clone()));
}
},
Err(err) => {
error!("Error reading key file: {:?}", err);
let item = rustls_pemfile::read_one(rd).map_err(|err| {
error!("Error reading key file: {:?}", err);
Error::InvalidServerKey(key_path.clone())
})?;
swanandx marked this conversation as resolved.
Show resolved Hide resolved

match item {
Some(Item::ECKey(key) | Item::RSAKey(key) | Item::PKCS8Key(key)) => {
return Ok(PrivateKey(key));
}
None => {
error!("No private key found in {:?}", key_path);
return Err(Error::InvalidServerKey(key_path.clone()));
}
_ => {}
}
}
}