Skip to content

Commit

Permalink
Move get_signature_schema_from_offered to utils crate with better rus…
Browse files Browse the repository at this point in the history
…tdoc
  • Loading branch information
Taowyoo committed Dec 15, 2023
1 parent 3c8ff2c commit b092184
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 31 deletions.
31 changes: 1 addition & 30 deletions rustls-mbedcrypto-provider/src/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustls::{pki_types, SignatureScheme};
use std::sync::Mutex;
use utils::error::mbedtls_err_into_rustls_err;
use utils::hash::{buffer_for_hash_type, rustls_signature_scheme_to_mbedtls_hash_type};
use utils::pk::{pk_type_to_signature_algo, rustls_signature_scheme_to_mbedtls_pk_options};
use utils::pk::{get_signature_schema_from_offered, pk_type_to_signature_algo, rustls_signature_scheme_to_mbedtls_pk_options};

struct MbedTlsSigner(Arc<Mutex<mbedtls::pk::Pk>>, SignatureScheme);

Expand Down Expand Up @@ -154,35 +154,6 @@ impl rustls::sign::SigningKey for MbedTlsPkSigningKey {
}
}

/// Helper function to choose proper [`SignatureScheme`] based on given inputs
pub fn get_signature_schema_from_offered(
pk_type: mbedtls::pk::Type,
offered: &[SignatureScheme],
ec_signature_scheme: Option<SignatureScheme>,
rsa_scheme_prefer_order_list: &[SignatureScheme],
) -> Option<SignatureScheme> {
match pk_type {
mbedtls::pk::Type::Rsa | mbedtls::pk::Type::RsaAlt | mbedtls::pk::Type::RsassaPss => {
// choose a rsa schema
for scheme in rsa_scheme_prefer_order_list {
if offered.contains(scheme) {
return Some(*scheme);
}
}
None
}
mbedtls::pk::Type::Eckey | mbedtls::pk::Type::EckeyDh | mbedtls::pk::Type::Ecdsa => {
let scheme = ec_signature_scheme.expect("validated");
if offered.contains(&scheme) {
Some(scheme)
} else {
None
}
}
_ => None,
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
44 changes: 43 additions & 1 deletion rustls-mbedtls-provider-utils/src/pk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub fn rustls_signature_scheme_to_mbedtls_curve_id(signature_scheme: SignatureSc
}
}

/// Helper function to get [`rustls::SignatureAlgorithm`] from mbedtls [`mbedtls::pk::Type`]
/// Helper function to get [`rustls::SignatureAlgorithm`] from mbedtls [`Type`]
pub fn pk_type_to_signature_algo(pk_type: Type) -> Option<rustls::SignatureAlgorithm> {
use rustls::SignatureAlgorithm;
match pk_type {
Expand All @@ -85,6 +85,48 @@ pub fn pk_type_to_signature_algo(pk_type: Type) -> Option<rustls::SignatureAlgor
}
}

/// Helper function to choose proper `SignatureScheme` based on given inputs
///
/// # Arguments
///
/// * `pk_type` - The type of [`Pk`][1] used currently.
/// * `offered` - The list of offered signature schemes
/// * `ec_signature_scheme` - If current [`Pk`][1] is EC key, corresponding ec signature scheme should be provided
/// * `rsa_scheme_prefer_order_list` - Order list of preferred RSA signature schemes
///
/// # Returns
///
/// The chosen `SignatureScheme` if found, otherwise `None`
///
/// [1]: mbedtls::pk::Pk
pub fn get_signature_schema_from_offered(
pk_type: Type,
offered: &[SignatureScheme],
ec_signature_scheme: Option<SignatureScheme>,
rsa_scheme_prefer_order_list: &[SignatureScheme],
) -> Option<SignatureScheme> {
match pk_type {
Type::Rsa | Type::RsaAlt | Type::RsassaPss => {
// choose a rsa schema
for scheme in rsa_scheme_prefer_order_list {
if offered.contains(scheme) {
return Some(*scheme);
}

Check warning on line 114 in rustls-mbedtls-provider-utils/src/pk.rs

View check run for this annotation

Codecov / codecov/patch

rustls-mbedtls-provider-utils/src/pk.rs#L114

Added line #L114 was not covered by tests
}
None

Check warning on line 116 in rustls-mbedtls-provider-utils/src/pk.rs

View check run for this annotation

Codecov / codecov/patch

rustls-mbedtls-provider-utils/src/pk.rs#L116

Added line #L116 was not covered by tests
}
Type::Eckey | Type::EckeyDh | Type::Ecdsa => {
let scheme = ec_signature_scheme.expect("EC Pk should provides a valid ec_signature_scheme");
if offered.contains(&scheme) {
Some(scheme)
} else {
None
}
}
_ => None,

Check warning on line 126 in rustls-mbedtls-provider-utils/src/pk.rs

View check run for this annotation

Codecov / codecov/patch

rustls-mbedtls-provider-utils/src/pk.rs#L126

Added line #L126 was not covered by tests
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit b092184

Please sign in to comment.