Skip to content

Commit

Permalink
Fixes #1884 -- don't leave an error on the stack in public_eq
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Apr 16, 2023
1 parent 47abce4 commit 5e48158
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion openssl/src/pkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,11 @@ where
where
U: HasPublic,
{
unsafe { ffi::EVP_PKEY_cmp(self.as_ptr(), other.as_ptr()) == 1 }
let res = unsafe { ffi::EVP_PKEY_cmp(self.as_ptr(), other.as_ptr()) == 1 };
// Clear the stack. OpenSSL will put an error on the stack when the
// keys are different types in some situations.
let _ = ErrorStack::get();
res
}

/// Raw byte representation of a public key.
Expand Down Expand Up @@ -885,6 +889,7 @@ mod tests {
use crate::dh::Dh;
use crate::dsa::Dsa;
use crate::ec::EcKey;
use crate::error::Error;
use crate::nid::Nid;
use crate::rsa::Rsa;
use crate::symm::Cipher;
Expand Down Expand Up @@ -1168,4 +1173,17 @@ mod tests {
let key = PKey::ec_gen("prime256v1").unwrap();
assert!(key.ec_key().is_ok());
}

#[test]
fn test_public_eq() {
let rsa = Rsa::generate(2048).unwrap();
let pkey1 = PKey::from_rsa(rsa).unwrap();

let group = crate::ec::EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap();
let ec_key = EcKey::generate(&group).unwrap();
let pkey2 = PKey::from_ec_key(ec_key).unwrap();

assert!(!pkey1.public_eq(&pkey2));
assert!(Error::get().is_none());
}
}

0 comments on commit 5e48158

Please sign in to comment.