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

Bump ureq release to use more recent rustls #208

Merged
merged 2 commits into from
Aug 27, 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
76 changes: 47 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions pkcs11/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ serde = { version = "1", features = ["derive"], default-features = false }
serde_yaml = "0.9.22"
serde_json = { default-features = false, version = "1.0.64" }
lazy_static = "1.4.0"
nethsm-sdk-rs = { version = "1" }
rustls = { version = "0.21", features = [
"dangerous_configuration",
], default-features = false }
rustls-native-certs = "0.6"
nethsm-sdk-rs = { version = "1.0" }
rustls = { version = "0.23", default-features = false }
rustls-native-certs = "0.7"
base64ct = { version = "1.6", default-features = false }
hex = "0.4"
der = { version = "0.7", default-features = false }
Expand All @@ -39,5 +37,8 @@ rayon = "1.8.0"
syslog = "6.1.0"
thiserror = "1.0.63"

# Needed to prevent breaking change with rustls updates
ureq = { version = "2.10", default-features = false }

[dev-dependencies]
hex-literal = "0.4.1"
1 change: 1 addition & 0 deletions pkcs11/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl<T> From<apis::Error<T>> for ApiError {
}),
}),
apis::Error::StringParse(e) => ApiError::StringParse(e),
// apis::Error::Multipart { field: _, error } => ApiError::Io(error),
}
}
}
Expand Down
129 changes: 106 additions & 23 deletions pkcs11/src/config/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use super::{
};
use log::{debug, error, info, trace};
use nethsm_sdk_rs::ureq;
use rustls::client::ServerCertVerifier;
use rustls::{
client::danger::ServerCertVerifier,
crypto::{verify_tls12_signature, verify_tls13_signature, CryptoProvider},
};
use sha2::Digest;

const DEFAULT_USER_AGENT: &str = concat!("pkcs11-rs/", env!("CARGO_PKG_VERSION"));
Expand Down Expand Up @@ -57,53 +60,131 @@ pub fn initialize_with_configs(
}

pub fn initialize() -> Result<Device, InitializationError> {
rustls::crypto::ring::default_provider()
.install_default()
.unwrap();
initialize_with_configs(config_files())
}

struct DangerIgnoreVerifier {}
#[derive(Debug)]
struct DangerIgnoreVerifier;

impl ServerCertVerifier for DangerIgnoreVerifier {
fn verify_server_cert(
&self,
_end_entity: &rustls::Certificate,
_intermediates: &[rustls::Certificate],
_server_name: &rustls::ServerName,
_scts: &mut dyn Iterator<Item = &[u8]>,
_end_entity: &rustls::pki_types::CertificateDer<'_>,
_intermediates: &[rustls::pki_types::CertificateDer<'_>],
_server_name: &rustls::pki_types::ServerName<'_>,
_ocsp_response: &[u8],
_now: std::time::SystemTime,
) -> Result<rustls::client::ServerCertVerified, rustls::Error> {
// always accept the certificate
Ok(rustls::client::ServerCertVerified::assertion())
_now: rustls::pki_types::UnixTime,
) -> Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
Ok(rustls::client::danger::ServerCertVerified::assertion())
}

fn verify_tls12_signature(
&self,
message: &[u8],
cert: &rustls::pki_types::CertificateDer<'_>,
dss: &rustls::DigitallySignedStruct,
) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
let default_provider = CryptoProvider::get_default().unwrap();
verify_tls12_signature(
message,
cert,
dss,
&default_provider.signature_verification_algorithms,
)
}

fn verify_tls13_signature(
&self,
message: &[u8],
cert: &rustls::pki_types::CertificateDer<'_>,
dss: &rustls::DigitallySignedStruct,
) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
let default_provider = CryptoProvider::get_default().unwrap();
verify_tls13_signature(
message,
cert,
dss,
&default_provider.signature_verification_algorithms,
)
}

fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
let default_provider = CryptoProvider::get_default().unwrap();

default_provider
.signature_verification_algorithms
.supported_schemes()
}
}

#[derive(Debug)]
struct FingerprintVerifier {
fingerprints: Vec<Vec<u8>>,
}

impl ServerCertVerifier for FingerprintVerifier {
fn verify_server_cert(
&self,
end_entity: &rustls::Certificate,
_intermediates: &[rustls::Certificate],
_server_name: &rustls::ServerName,
_scts: &mut dyn Iterator<Item = &[u8]>,
end_entity: &rustls::pki_types::CertificateDer<'_>,
_intermediates: &[rustls::pki_types::CertificateDer<'_>],
_server_name: &rustls::pki_types::ServerName<'_>,
_ocsp_response: &[u8],
_now: std::time::SystemTime,
) -> Result<rustls::client::ServerCertVerified, rustls::Error> {
_now: rustls::pki_types::UnixTime,
) -> Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
let mut hasher = sha2::Sha256::new();
hasher.update(&end_entity.0);
hasher.update(end_entity.as_ref());
let result = hasher.finalize();
for fingerprint in self.fingerprints.iter() {
if fingerprint == &result[..] {
for fingerprint in &self.fingerprints {
if fingerprint == &*result {
trace!("Certificate fingerprint matches");
return Ok(rustls::client::ServerCertVerified::assertion());
return Ok(rustls::client::danger::ServerCertVerified::assertion());
}
}
Err(rustls::Error::General(
"Could not verify certificate fingerprint".to_string(),
))
}

fn verify_tls12_signature(
&self,
message: &[u8],
cert: &rustls::pki_types::CertificateDer<'_>,
dss: &rustls::DigitallySignedStruct,
) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
let default_provider = CryptoProvider::get_default().unwrap();
verify_tls12_signature(
message,
cert,
dss,
&default_provider.signature_verification_algorithms,
)
}

fn verify_tls13_signature(
&self,
message: &[u8],
cert: &rustls::pki_types::CertificateDer<'_>,
dss: &rustls::DigitallySignedStruct,
) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
let default_provider = CryptoProvider::get_default().unwrap();
verify_tls13_signature(
message,
cert,
dss,
&default_provider.signature_verification_algorithms,
)
}

fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
let default_provider = CryptoProvider::get_default().unwrap();

default_provider
.signature_verification_algorithms
.supported_schemes()
}
}

fn slot_from_config(slot: &SlotConfig) -> Result<Slot, InitializationError> {
Expand All @@ -123,11 +204,12 @@ fn slot_from_config(slot: &SlotConfig) -> Result<Slot, InitializationError> {
);

for instance in slot.instances.iter() {
let tls_conf = rustls::ClientConfig::builder().with_safe_defaults();
let tls_conf = rustls::ClientConfig::builder();

let tls_conf = if instance.danger_insecure_cert {
tls_conf
.with_custom_certificate_verifier(Arc::new(DangerIgnoreVerifier {}))
.dangerous()
.with_custom_certificate_verifier(Arc::new(DangerIgnoreVerifier))
.with_no_client_auth()
} else if !instance.sha256_fingerprints.is_empty() {
let fingerprints = instance
Expand All @@ -136,6 +218,7 @@ fn slot_from_config(slot: &SlotConfig) -> Result<Slot, InitializationError> {
.map(|f| f.value.clone())
.collect();
tls_conf
.dangerous()
.with_custom_certificate_verifier(Arc::new(FingerprintVerifier { fingerprints }))
.with_no_client_auth()
} else {
Expand All @@ -145,7 +228,7 @@ fn slot_from_config(slot: &SlotConfig) -> Result<Slot, InitializationError> {
InitializationError::NoCerts
})?;

let (added, failed) = roots.add_parsable_certificates(&native_certs);
let (added, failed) = roots.add_parsable_certificates(native_certs);
// panic!("{:?}", (added, failed));
debug!("Added {added} certifcates and failed to parse {failed} certificates");

Expand Down
Loading