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

[DRAFT] Fix #214: Separate user cert-chain from PKCS#8 key #215

Merged
merged 2 commits into from
Feb 5, 2024
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
15 changes: 13 additions & 2 deletions data/src/config/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl Server {
accept_invalid_certs: self.dangerously_accept_invalid_certs,
root_cert_path: self.root_cert_path.as_ref(),
client_cert_path: self.sasl.as_ref().and_then(Sasl::external_cert),
client_key_path: self.sasl.as_ref().and_then(Sasl::external_key),
}
} else {
connection::Security::Unsecured
Expand All @@ -97,8 +98,10 @@ pub enum Sasl {
password: String,
},
External {
/// The path to PEM encoded X509 certificate for external auth
/// The path to PEM encoded X509 user certificate for external auth
cert: PathBuf,
/// The path to PEM encoded PKCS#8 private key corresponding to the user certificate for external auth
key: Option<PathBuf>,
},
}

Expand All @@ -122,12 +125,20 @@ impl Sasl {
}

fn external_cert(&self) -> Option<&PathBuf> {
if let Self::External { cert } = self {
if let Self::External { cert, .. } = self {
Some(cert)
} else {
None
}
}

fn external_key(&self) -> Option<&PathBuf> {
if let Self::External { cert, key, .. } = self {
Some(key.as_ref().unwrap_or(cert))
} else {
None
}
}
}

fn default_use_tls() -> bool {
Expand Down
11 changes: 7 additions & 4 deletions irc/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub enum Security<'a> {
accept_invalid_certs: bool,
root_cert_path: Option<&'a PathBuf>,
client_cert_path: Option<&'a PathBuf>,
client_key_path: Option<&'a PathBuf>,
},
}

Expand All @@ -44,6 +45,7 @@ impl Connection {
accept_invalid_certs,
root_cert_path,
client_cert_path,
client_key_path,
} = config.security
{
let mut builder = native_tls::TlsConnector::builder();
Expand All @@ -55,10 +57,11 @@ impl Connection {
builder.add_root_certificate(cert);
}

if let Some(path) = client_cert_path {
let bytes = fs::read(path).await?;
let pkcs12_archive = Identity::from_pkcs8(&bytes, &bytes)?;
builder.identity(pkcs12_archive);
if let (Some(cert_path), Some(pkcs8_key_path)) = (client_cert_path, client_key_path) {
let cert_bytes = fs::read(cert_path).await?;
let pkcs8_key_bytes = fs::read(pkcs8_key_path).await?;
let identity = Identity::from_pkcs8(&cert_bytes, &pkcs8_key_bytes)?;
builder.identity(identity);
}

let tls = TlsConnector::from(builder.build()?)
Expand Down
Loading