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

RUST-1122 Fix x509 auth for pkcs8 keys and Atlas free tier #532

Merged
merged 6 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ md-5 = "0.9.1"
os_info = { version = "3.0.1", default-features = false }
percent-encoding = "2.0.0"
rand = { version = "0.8.3", features = ["small_rng"] }
rustls-pemfile = "0.2.1"
serde_with = "1.3.1"
sha-1 = "0.9.4"
sha2 = "0.9.3"
Expand Down
10 changes: 7 additions & 3 deletions src/client/auth/x509.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,18 @@ pub(super) async fn authenticate_stream(
server_api: Option<&ServerApi>,
server_first: impl Into<Option<Document>>,
) -> Result<()> {
let server_response = match server_first.into() {
Some(server_first) => server_first,
let server_response: Document = match server_first.into() {
Some(_) => return Ok(()),
None => send_client_first(conn, credential, server_api)
.await?
.auth_response_body("MONGODB-X509")?,
};

if server_response.get_str("dbname") != Ok("$external") {
if server_response
.get("ok")
.and_then(crate::bson_util::get_int)
!= Some(1)
{
return Err(Error::authentication_error(
"MONGODB-X509",
"Authentication failed",
Expand Down
33 changes: 22 additions & 11 deletions src/client/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use rustls::{
ServerCertVerifier,
TLSError,
};
use rustls_pemfile::{read_one, Item};
use serde::{
de::{Error, Unexpected},
Deserialize,
Expand Down Expand Up @@ -846,22 +847,32 @@ impl TlsOptions {
};

file.seek(SeekFrom::Start(0))?;
patrickfreed marked this conversation as resolved.
Show resolved Hide resolved
let key = match pemfile::rsa_private_keys(&mut file) {
Ok(key) => key,
Err(()) => {
return Err(ErrorKind::InvalidTlsConfig {
message: format!(
"Unable to parse PEM-encoded RSA key from {}",
path.display()
),
let key = loop {
match read_one(&mut file) {
Ok(Some(Item::PKCS8Key(bytes))) | Ok(Some(Item::RSAKey(bytes))) => {
break rustls::PrivateKey(bytes)
}
Ok(Some(_)) => continue,
Ok(None) => {
return Err(ErrorKind::InvalidTlsConfig {
message: format!("No PEM-encoded keys in {}", path.display()),
}
.into())
}
Err(_) => {
return Err(ErrorKind::InvalidTlsConfig {
message: format!(
"Unable to parse PEM-encoded item from {}",
path.display()
),
}
.into())
}
.into())
}
};

// TODO: Get rid of unwrap.
config
.set_single_client_cert(certs, key.into_iter().next().unwrap())
.set_single_client_cert(certs, key)
.map_err(|e| ErrorKind::InvalidTlsConfig {
message: e.to_string(),
})?;
Expand Down