Skip to content

Add context to signature-related errors #357

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

Merged
merged 3 commits into from
Jun 20, 2025
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,6 @@ codegen-units = 1
name = "benchmarks"
path = "benches/benchmark.rs"
harness = false

[package.metadata.cargo-semver-checks.lints]
enum_variant_marked_deprecated = "warn"
135 changes: 99 additions & 36 deletions src/alg_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
use std::prelude::v1::*;

use base64::{Engine as _, engine::general_purpose};
use pki_types::alg_id;

use crate::error::{DerTypeId, Error};
use crate::error::{DerTypeId, Error, UnsupportedSignatureAlgorithmForPublicKeyContext};
use crate::verify_cert::Budget;
use crate::{der, signed_data};

use super::{
INVALID_SIGNATURE_FOR_RSA_KEY, OK_IF_POINT_COMPRESSION_SUPPORTED, OK_IF_RSA_AVAILABLE,
SUPPORTED_ALGORITHMS_IN_TESTS, UNSUPPORTED_ECDSA_SHA512_SIGNATURE,
UNSUPPORTED_SIGNATURE_ALGORITHM_FOR_RSA_KEY,
OK_IF_POINT_COMPRESSION_SUPPORTED, SUPPORTED_ALGORITHMS_IN_TESTS, invalid_rsa_signature,
maybe_rsa, unsupported, unsupported_for_ecdsa, unsupported_for_rsa,
};

macro_rules! test_file_bytes {
Expand Down Expand Up @@ -113,7 +113,10 @@ fn test_ecdsa_prime256v1_sha512_spki_params_null() {
test_verify_signed_data(test_file_bytes!(
"ecdsa-prime256v1-sha512-spki-params-null.pem"
)),
Err(UNSUPPORTED_ECDSA_SHA512_SIGNATURE)
Err(unsupported_for_ecdsa(
&alg_id::ECDSA_SHA512,
include_bytes!("../tests/signatures/alg-id-ecpublickey-params-null.der")
))
);
}

Expand All @@ -135,7 +138,10 @@ fn test_ecdsa_prime256v1_sha512_using_ecdh_key() {
test_verify_signed_data(test_file_bytes!(
"ecdsa-prime256v1-sha512-using-ecdh-key.pem"
)),
Err(UNSUPPORTED_ECDSA_SHA512_SIGNATURE)
Err(unsupported_for_ecdsa(
&alg_id::ECDSA_SHA512,
include_bytes!("../tests/signatures/alg-ecdh-secp256r1.der")
))
);
}

Expand All @@ -147,7 +153,10 @@ fn test_ecdsa_prime256v1_sha512_using_ecmqv_key() {
test_verify_signed_data(test_file_bytes!(
"ecdsa-prime256v1-sha512-using-ecmqv-key.pem"
)),
Err(UNSUPPORTED_ECDSA_SHA512_SIGNATURE)
Err(unsupported_for_ecdsa(
&alg_id::ECDSA_SHA512,
include_bytes!("../tests/signatures/alg-ecmqv-secp256r1.der")
))
);
}

Expand All @@ -156,8 +165,11 @@ fn test_ecdsa_prime256v1_sha512_using_rsa_algorithm() {
assert_eq!(
test_verify_signed_data(test_file_bytes!(
"ecdsa-prime256v1-sha512-using-rsa-algorithm.pem"
)),
Err(UNSUPPORTED_SIGNATURE_ALGORITHM_FOR_RSA_KEY)
),),
Err(unsupported_for_rsa(
&alg_id::RSA_PKCS1_SHA512,
&alg_id::ECDSA_P256,
))
);
}

Expand All @@ -169,7 +181,10 @@ fn test_ecdsa_prime256v1_sha512_wrong_signature_format() {
test_verify_signed_data(test_file_bytes!(
"ecdsa-prime256v1-sha512-wrong-signature-format.pem"
)),
Err(UNSUPPORTED_ECDSA_SHA512_SIGNATURE)
Err(unsupported_for_ecdsa(
&alg_id::ECDSA_SHA512,
&alg_id::ECDSA_P256,
))
);
}

Expand All @@ -178,7 +193,10 @@ fn test_ecdsa_prime256v1_sha512_wrong_signature_format() {
fn test_ecdsa_prime256v1_sha512() {
assert_eq!(
test_verify_signed_data(test_file_bytes!("ecdsa-prime256v1-sha512.pem")),
Err(UNSUPPORTED_ECDSA_SHA512_SIGNATURE)
Err(unsupported_for_ecdsa(
&alg_id::ECDSA_SHA512,
&alg_id::ECDSA_P256,
))
);
}

Expand All @@ -204,7 +222,14 @@ fn test_ecdsa_secp384r1_sha256() {
fn test_ecdsa_using_rsa_key() {
assert_eq!(
test_verify_signed_data(test_file_bytes!("ecdsa-using-rsa-key.pem")),
Err(Error::UnsupportedSignatureAlgorithmForPublicKey)
Err(Error::UnsupportedSignatureAlgorithmForPublicKeyContext(
UnsupportedSignatureAlgorithmForPublicKeyContext {
#[cfg(feature = "alloc")]
signature_algorithm_id: alg_id::ECDSA_SHA256.as_ref().to_vec(),
#[cfg(feature = "alloc")]
public_key_algorithm_id: alg_id::RSA_ENCRYPTION.as_ref().to_vec(),
}
))
);
}

Expand All @@ -228,7 +253,9 @@ fn test_rsa_pkcs1_sha1_bad_key_der_null() {
fn test_rsa_pkcs1_sha1_key_params_absent() {
assert_eq!(
test_verify_signed_data(test_file_bytes!("rsa-pkcs1-sha1-key-params-absent.pem")),
Err(Error::UnsupportedSignatureAlgorithm)
Err(unsupported(include_bytes!(
"../tests/signatures/alg-rsae-sha1.der"
)))
);
}

Expand All @@ -238,23 +265,27 @@ fn test_rsa_pkcs1_sha1_using_pss_key_no_params() {
test_verify_signed_data(test_file_bytes!(
"rsa-pkcs1-sha1-using-pss-key-no-params.pem"
)),
Err(Error::UnsupportedSignatureAlgorithm)
Err(unsupported(include_bytes!(
"../tests/signatures/alg-rsae-sha1.der"
)))
);
}

#[test]
fn test_rsa_pkcs1_sha1_wrong_algorithm() {
assert_eq!(
test_verify_signed_data(test_file_bytes!("rsa-pkcs1-sha1-wrong-algorithm.pem")),
Err(INVALID_SIGNATURE_FOR_RSA_KEY)
Err(invalid_rsa_signature())
);
}

#[test]
fn test_rsa_pkcs1_sha1() {
assert_eq!(
test_verify_signed_data(test_file_bytes!("rsa-pkcs1-sha1.pem")),
Err(Error::UnsupportedSignatureAlgorithm)
Err(unsupported(include_bytes!(
"../tests/signatures/alg-rsae-sha1.der"
)))
);
}

Expand All @@ -267,7 +298,7 @@ fn test_rsa_pkcs1_sha1() {
fn test_rsa_pkcs1_sha256() {
assert_eq!(
test_verify_signed_data(test_file_bytes!("rsa-pkcs1-sha256.pem")),
Err(INVALID_SIGNATURE_FOR_RSA_KEY)
Err(invalid_rsa_signature())
);
}

Expand All @@ -285,7 +316,10 @@ fn test_rsa_pkcs1_sha256_spki_non_null_params() {
test_verify_signed_data(test_file_bytes!(
"rsa-pkcs1-sha256-spki-non-null-params.pem"
)),
Err(UNSUPPORTED_SIGNATURE_ALGORITHM_FOR_RSA_KEY)
Err(unsupported_for_rsa(
&alg_id::RSA_PKCS1_SHA256,
include_bytes!("../tests/signatures/alg-rsae-bad-params.der")
))
);
}

Expand All @@ -295,15 +329,25 @@ fn test_rsa_pkcs1_sha256_using_ecdsa_algorithm() {
test_verify_signed_data(test_file_bytes!(
"rsa-pkcs1-sha256-using-ecdsa-algorithm.pem"
)),
Err(Error::UnsupportedSignatureAlgorithmForPublicKey)
Err(Error::UnsupportedSignatureAlgorithmForPublicKeyContext(
UnsupportedSignatureAlgorithmForPublicKeyContext {
#[cfg(feature = "alloc")]
signature_algorithm_id: alg_id::ECDSA_SHA256.as_ref().to_vec(),
#[cfg(feature = "alloc")]
public_key_algorithm_id: alg_id::RSA_ENCRYPTION.as_ref().to_vec(),
}
))
);
}

#[test]
fn test_rsa_pkcs1_sha256_using_id_ea_rsa() {
assert_eq!(
test_verify_signed_data(test_file_bytes!("rsa-pkcs1-sha256-using-id-ea-rsa.pem")),
Err(UNSUPPORTED_SIGNATURE_ALGORITHM_FOR_RSA_KEY)
Err(unsupported_for_rsa(
&alg_id::RSA_PKCS1_SHA256,
include_bytes!("../tests/signatures/alg-rsa-null-params.der")
))
);
}

Expand All @@ -315,7 +359,9 @@ fn test_rsa_pss_sha1_salt20_using_pss_key_no_params() {
test_verify_signed_data(test_file_bytes!(
"rsa-pss-sha1-salt20-using-pss-key-no-params.pem"
)),
Err(Error::UnsupportedSignatureAlgorithm)
Err(unsupported(include_bytes!(
"../tests/signatures/alg-rsapss-defaults.der"
)))
);
}

Expand All @@ -325,30 +371,38 @@ fn test_rsa_pss_sha1_salt20_using_pss_key_with_null_params() {
test_verify_signed_data(test_file_bytes!(
"rsa-pss-sha1-salt20-using-pss-key-with-null-params.pem"
)),
Err(Error::UnsupportedSignatureAlgorithm)
Err(unsupported(include_bytes!(
"../tests/signatures/alg-rsapss-defaults.der"
)))
);
}
#[test]
fn test_rsa_pss_sha1_salt20() {
assert_eq!(
test_verify_signed_data(test_file_bytes!("rsa-pss-sha1-salt20.pem")),
Err(Error::UnsupportedSignatureAlgorithm)
Err(unsupported(include_bytes!(
"../tests/signatures/alg-rsapss-defaults.der"
)))
);
}

#[test]
fn test_rsa_pss_sha1_wrong_salt() {
assert_eq!(
test_verify_signed_data(test_file_bytes!("rsa-pss-sha1-wrong-salt.pem")),
Err(Error::UnsupportedSignatureAlgorithm)
Err(unsupported(include_bytes!(
"../tests/signatures/alg-rsapss-salt23.der"
)))
);
}

#[test]
fn test_rsa_pss_sha256_mgf1_sha512_salt33() {
assert_eq!(
test_verify_signed_data(test_file_bytes!("rsa-pss-sha256-mgf1-sha512-salt33.pem")),
Err(Error::UnsupportedSignatureAlgorithm)
Err(unsupported(include_bytes!(
"../tests/signatures/alg-rsapss-sha256-mgf1-sha512-salt33.der"
)))
);
}

Expand All @@ -358,7 +412,9 @@ fn test_rsa_pss_sha256_salt10_using_pss_key_with_params() {
test_verify_signed_data(test_file_bytes!(
"rsa-pss-sha256-salt10-using-pss-key-with-params.pem"
)),
Err(Error::UnsupportedSignatureAlgorithm)
Err(unsupported(include_bytes!(
"../tests/signatures/alg-rsapss-sha256-mgf1-sha256-salt10.der"
)))
);
}
#[test]
Expand All @@ -367,15 +423,19 @@ fn test_rsa_pss_sha256_salt10_using_pss_key_with_wrong_params() {
test_verify_signed_data(test_file_bytes!(
"rsa-pss-sha256-salt10-using-pss-key-with-wrong-params.pem"
)),
Err(Error::UnsupportedSignatureAlgorithm)
Err(unsupported(include_bytes!(
"../tests/signatures/alg-rsapss-sha256-mgf1-sha256-salt10.der"
)))
);
}

#[test]
fn test_rsa_pss_sha256_salt10() {
assert_eq!(
test_verify_signed_data(test_file_bytes!("rsa-pss-sha256-salt10.pem")),
Err(Error::UnsupportedSignatureAlgorithm)
Err(unsupported(include_bytes!(
"../tests/signatures/alg-rsapss-sha256-mgf1-sha256-salt10.der"
)))
);
}

Expand All @@ -385,23 +445,23 @@ fn test_rsa_pss_sha256_salt10() {
fn test_rsa_pss_sha256_salt32() {
assert_eq!(
test_verify_signed_data(test_file_bytes!("ours/rsa-pss-sha256-salt32.pem")),
OK_IF_RSA_AVAILABLE
maybe_rsa()
);
}

#[test]
fn test_rsa_pss_sha384_salt48() {
assert_eq!(
test_verify_signed_data(test_file_bytes!("ours/rsa-pss-sha384-salt48.pem")),
OK_IF_RSA_AVAILABLE
maybe_rsa()
);
}

#[test]
fn test_rsa_pss_sha512_salt64() {
assert_eq!(
test_verify_signed_data(test_file_bytes!("ours/rsa-pss-sha512-salt64.pem")),
OK_IF_RSA_AVAILABLE
maybe_rsa()
);
}

Expand All @@ -411,7 +471,7 @@ fn test_rsa_pss_sha256_salt32_corrupted_data() {
test_verify_signed_data(test_file_bytes!(
"ours/rsa-pss-sha256-salt32-corrupted-data.pem"
)),
Err(INVALID_SIGNATURE_FOR_RSA_KEY)
Err(invalid_rsa_signature())
);
}

Expand All @@ -421,7 +481,7 @@ fn test_rsa_pss_sha384_salt48_corrupted_data() {
test_verify_signed_data(test_file_bytes!(
"ours/rsa-pss-sha384-salt48-corrupted-data.pem"
)),
Err(INVALID_SIGNATURE_FOR_RSA_KEY)
Err(invalid_rsa_signature())
);
}

Expand All @@ -431,23 +491,26 @@ fn test_rsa_pss_sha512_salt64_corrupted_data() {
test_verify_signed_data(test_file_bytes!(
"ours/rsa-pss-sha512-salt64-corrupted-data.pem"
)),
Err(INVALID_SIGNATURE_FOR_RSA_KEY)
Err(invalid_rsa_signature())
);
}

#[test]
fn test_rsa_using_ec_key() {
assert_eq!(
test_verify_signed_data(test_file_bytes!("rsa-using-ec-key.pem")),
Err(UNSUPPORTED_SIGNATURE_ALGORITHM_FOR_RSA_KEY)
Err(unsupported_for_rsa(
&alg_id::RSA_PKCS1_SHA256,
&alg_id::ECDSA_P256
))
);
}

#[test]
fn test_rsa2048_pkcs1_sha512() {
assert_eq!(
test_verify_signed_data(test_file_bytes!("rsa2048-pkcs1-sha512.pem")),
OK_IF_RSA_AVAILABLE
maybe_rsa()
);
}

Expand Down
Loading