Skip to content

Commit 716bd3d

Browse files
committed
Add ECDSA signature verification for curve P521
1 parent b7dcc93 commit 716bd3d

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

assets/p521-selfsigned.der

605 Bytes
Binary file not shown.

src/verify.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use crate::prelude::*;
22
use crate::signature_algorithm::RsaSsaPssParams;
33
use asn1_rs::{Any, BitString, DerParser};
44
use oid_registry::{
5-
OID_EC_P256, OID_NIST_EC_P384, OID_NIST_HASH_SHA256, OID_NIST_HASH_SHA384,
5+
OID_EC_P256, OID_NIST_EC_P384, OID_NIST_EC_P521, OID_NIST_HASH_SHA256, OID_NIST_HASH_SHA384,
66
OID_NIST_HASH_SHA512, OID_PKCS1_RSASSAPSS, OID_PKCS1_SHA1WITHRSA, OID_PKCS1_SHA256WITHRSA,
77
OID_PKCS1_SHA384WITHRSA, OID_PKCS1_SHA512WITHRSA, OID_SHA1_WITH_RSA, OID_SIG_ECDSA_WITH_SHA256,
8-
OID_SIG_ECDSA_WITH_SHA384, OID_SIG_ED25519,
8+
OID_SIG_ECDSA_WITH_SHA384, OID_SIG_ECDSA_WITH_SHA512, OID_SIG_ED25519,
99
};
1010

1111
// Since the `signature` object is similar in ring and in aws-lc-rs, we just use simple logic
@@ -53,6 +53,9 @@ pub fn verify_signature(
5353
} else if *signature_algorithm == OID_SIG_ECDSA_WITH_SHA384 {
5454
get_ec_curve_sha(&public_key.algorithm, 384)
5555
.ok_or(X509Error::SignatureUnsupportedAlgorithm)?
56+
} else if *signature_algorithm == OID_SIG_ECDSA_WITH_SHA512 {
57+
get_ec_curve_sha(&public_key.algorithm, 512)
58+
.ok_or(X509Error::SignatureUnsupportedAlgorithm)?
5659
} else if *signature_algorithm == OID_SIG_ED25519 {
5760
&signature::ED25519
5861
} else {
@@ -76,7 +79,6 @@ fn get_ec_curve_sha(
7679
sha_len: usize,
7780
) -> Option<&'static dyn signature::VerificationAlgorithm> {
7881
let curve_oid = pubkey_alg.parameters.as_ref()?.as_oid().ok()?;
79-
// let curve_oid = pubkey_alg.parameters.as_ref()?.as_oid().ok()?;
8082
if curve_oid == OID_EC_P256 {
8183
match sha_len {
8284
256 => Some(&signature::ECDSA_P256_SHA256_ASN1),
@@ -89,6 +91,13 @@ fn get_ec_curve_sha(
8991
384 => Some(&signature::ECDSA_P384_SHA384_ASN1),
9092
_ => None,
9193
}
94+
} else if curve_oid == OID_NIST_EC_P521 {
95+
match sha_len {
96+
256 => Some(&signature::ECDSA_P521_SHA256_ASN1),
97+
384 => Some(&signature::ECDSA_P521_SHA384_ASN1),
98+
512 => Some(&signature::ECDSA_P521_SHA512_ASN1),
99+
_ => None,
100+
}
92101
} else {
93102
None
94103
}

tests/verify.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,15 @@ fn test_signature_verification_rsa_pss_sha512() {
6767
eprintln!("Verification: {res:?}");
6868
assert!(res.is_ok());
6969
}
70+
71+
static P521_SELF_SIGNED_DER: &[u8] =
72+
include_bytes!("../assets/p521-selfsigned.der");
73+
74+
#[test]
75+
fn test_signature_verification_p521() {
76+
let (_, x509_ca) =
77+
parse_x509_certificate(P521_SELF_SIGNED_DER).expect("could not parse certificate");
78+
let res = x509_ca.verify_signature(None);
79+
eprintln!("Verification: {res:?}");
80+
assert!(res.is_ok());
81+
}

0 commit comments

Comments
 (0)