Skip to content

key_pair: add function to retrieve signature algorithm #158

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 1 commit into from
Sep 20, 2023
Merged
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
26 changes: 24 additions & 2 deletions src/key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ impl KeyPair {
pub fn from_der(der: &[u8]) -> Result<Self, RcgenError> {
Ok(der.try_into()?)
}
/// Returns the key pair's signature algorithm
pub fn algorithm(&self) -> &'static SignatureAlgorithm {
self.alg
}
/// Parses the key pair from the ASCII PEM format
#[cfg(feature = "pem")]
pub fn from_pem(pem_str: &str) -> Result<Self, RcgenError> {
Expand Down Expand Up @@ -93,7 +97,7 @@ impl KeyPair {
/// Usually, calling this function is not neccessary and you can just call
/// [`from_der`](Self::from_der) instead. That function will try to figure
/// out a fitting [`SignatureAlgorithm`] for the given
/// key pair. However sometimes multiple signature algorithms fit for the
/// key pair. However, sometimes multiple signature algorithms fit for the
/// same der key. In that instance, you can use this function to precisely
/// specify the `SignatureAlgorithm`.
pub fn from_der_and_sign_algo(
Expand Down Expand Up @@ -173,7 +177,7 @@ pub trait RemoteKeyPair {
/// Signs `msg` using the selected algorithm
fn sign(&self, msg: &[u8]) -> Result<Vec<u8>, RcgenError>;

/// Reveals which algorithm will be used when you call `sign()`
/// Reveals the algorithm to be used when calling `sign()`
fn algorithm(&self) -> &'static SignatureAlgorithm;
}

Expand Down Expand Up @@ -364,3 +368,21 @@ pub(crate) trait PublicKeyData {
})
}
}

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

use ring::rand::SystemRandom;
use ring::signature::{EcdsaKeyPair, ECDSA_P256_SHA256_FIXED_SIGNING};

#[test]
fn test_algorithm() {
let rng = SystemRandom::new();
let pkcs8 = EcdsaKeyPair::generate_pkcs8(&ECDSA_P256_SHA256_FIXED_SIGNING, &rng).unwrap();
let der = pkcs8.as_ref().to_vec();

let key_pair = KeyPair::from_der(&der).unwrap();
assert_eq!(key_pair.algorithm(), &PKCS_ECDSA_P256_SHA256);
}
}