Skip to content

Commit 4220081

Browse files
authored
Fix regression in EcdsaKeyPair::from_private_key_der (#686)
* Fix regression in EcdsaKeyPair::from_private_key_der * Satisfy clippy
1 parent fa33226 commit 4220081

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

aws-lc-fips-sys/builder/cmake_builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ impl CmakeBuilder {
113113

114114
if let Some(cc) = option_env!("AWS_LC_FIPS_SYS_CC") {
115115
env::set_var("CC", cc);
116-
emit_warning(&format!("Setting CC: {}", cc));
116+
emit_warning(&format!("Setting CC: {cc}"));
117117
}
118118
if let Some(cxx) = option_env!("AWS_LC_FIPS_SYS_CXX") {
119119
env::set_var("CXX", cxx);
120-
emit_warning(&format!("Setting CXX: {}", cxx));
120+
emit_warning(&format!("Setting CXX: {cxx}"));
121121
}
122122

123123
let cc_build = cc::Build::new();

aws-lc-rs/src/ec/key_pair.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
// Modifications copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
44
// SPDX-License-Identifier: Apache-2.0 OR ISC
55

6+
use crate::aws_lc::{EVP_DigestSign, EVP_DigestSignInit, EVP_PKEY, EVP_PKEY_EC};
67
use core::fmt;
78
use core::fmt::{Debug, Formatter};
89
use core::mem::MaybeUninit;
910
use core::ptr::{null, null_mut};
1011

11-
use crate::aws_lc::{EVP_DigestSign, EVP_DigestSignInit, EVP_PKEY_cmp, EVP_PKEY, EVP_PKEY_EC};
12-
1312
use crate::digest::digest_ctx::DigestContext;
1413
use crate::ec::evp_key_generate;
1514
use crate::ec::signature::{EcdsaSignatureFormat, EcdsaSigningAlgorithm, PublicKey};
@@ -162,8 +161,8 @@ impl EcdsaKeyPair {
162161
) -> Result<Self, KeyRejected> {
163162
let priv_evp_pkey = parse_sec1_private_bn(private_key, alg.id.nid())?;
164163
let pub_evp_pkey = parse_sec1_public_point(public_key, alg.id.nid())?;
165-
// EVP_PKEY_cmp only compare params and public key
166-
if 1 != unsafe { EVP_PKEY_cmp(*priv_evp_pkey.as_const(), *pub_evp_pkey.as_const()) } {
164+
// EVP_PKEY_cmp only compares params and public key
165+
if !priv_evp_pkey.eq(&pub_evp_pkey) {
167166
return Err(KeyRejected::inconsistent_components());
168167
}
169168

@@ -187,7 +186,8 @@ impl EcdsaKeyPair {
187186
alg: &'static EcdsaSigningAlgorithm,
188187
private_key: &[u8],
189188
) -> Result<Self, KeyRejected> {
190-
let evp_pkey = parse_rfc5915_private_key(private_key, alg.id.nid())?;
189+
let evp_pkey = LcPtr::<EVP_PKEY>::parse_rfc5208_private_key(private_key, EVP_PKEY_EC)
190+
.or(parse_rfc5915_private_key(private_key, alg.id.nid()))?;
191191

192192
Ok(Self::new(alg, evp_pkey)?)
193193
}
@@ -320,3 +320,32 @@ impl AsDer<EcPrivateKeyRfc5915Der<'static>> for PrivateKey<'_> {
320320
Ok(EcPrivateKeyRfc5915Der::new(bytes))
321321
}
322322
}
323+
324+
#[cfg(test)]
325+
mod tests {
326+
use crate::encoding::AsDer;
327+
use crate::signature::{EcdsaKeyPair, ECDSA_P256_SHA256_FIXED_SIGNING};
328+
329+
#[test]
330+
fn test_from_private_key_der() {
331+
let key_pair = EcdsaKeyPair::generate(&ECDSA_P256_SHA256_FIXED_SIGNING).unwrap();
332+
333+
let bytes_5208 = key_pair.to_pkcs8v1().unwrap();
334+
let bytes_5915 = key_pair.private_key().as_der().unwrap();
335+
336+
let key_pair_5208 = EcdsaKeyPair::from_private_key_der(
337+
&ECDSA_P256_SHA256_FIXED_SIGNING,
338+
bytes_5208.as_ref(),
339+
)
340+
.unwrap();
341+
let key_pair_5915 = EcdsaKeyPair::from_private_key_der(
342+
&ECDSA_P256_SHA256_FIXED_SIGNING,
343+
bytes_5915.as_ref(),
344+
)
345+
.unwrap();
346+
347+
assert_eq!(key_pair.evp_pkey, key_pair_5208.evp_pkey);
348+
assert_eq!(key_pair.evp_pkey, key_pair_5915.evp_pkey);
349+
assert_eq!(key_pair_5208.evp_pkey, key_pair_5915.evp_pkey);
350+
}
351+
}

aws-lc-rs/src/evp_pkey.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0 OR ISC
33

44
use crate::aws_lc::{
5-
EVP_PKEY_CTX_new, EVP_PKEY_bits, EVP_PKEY_get0_EC_KEY, EVP_PKEY_get0_RSA,
5+
EVP_PKEY_CTX_new, EVP_PKEY_bits, EVP_PKEY_cmp, EVP_PKEY_get0_EC_KEY, EVP_PKEY_get0_RSA,
66
EVP_PKEY_get_raw_private_key, EVP_PKEY_get_raw_public_key, EVP_PKEY_id,
77
EVP_PKEY_new_raw_private_key, EVP_PKEY_new_raw_public_key, EVP_PKEY_size, EVP_PKEY_up_ref,
88
EVP_marshal_private_key, EVP_marshal_private_key_v2, EVP_marshal_public_key,
@@ -18,6 +18,14 @@ use crate::ptr::{ConstPointer, LcPtr};
1818
use std::os::raw::c_int;
1919
use std::ptr::null_mut;
2020

21+
impl PartialEq<Self> for LcPtr<EVP_PKEY> {
22+
/// Only compares params and public key
23+
fn eq(&self, other: &Self) -> bool {
24+
// EVP_PKEY_cmp only compares params and public key
25+
1 == unsafe { EVP_PKEY_cmp(*self.as_const(), *other.as_const()) }
26+
}
27+
}
28+
2129
impl LcPtr<EVP_PKEY> {
2230
pub(crate) fn validate_as_ed25519(&self) -> Result<(), KeyRejected> {
2331
const ED25519_KEY_TYPE: c_int = aws_lc::EVP_PKEY_ED25519;
@@ -95,7 +103,7 @@ impl LcPtr<EVP_PKEY> {
95103
let mut cbb = LcCBB::new(self.key_size_bytes() * 5);
96104
if 1 != unsafe { EVP_marshal_public_key(cbb.as_mut_ptr(), *self.as_const()) } {
97105
return Err(Unspecified);
98-
};
106+
}
99107
cbb.into_vec()
100108
}
101109

0 commit comments

Comments
 (0)