Skip to content

Commit a5b54f5

Browse files
tindzkcpu
authored andcommitted
key_pair: add function to retrieve signature algorithm
Currently, the `alg` field is restricted to the crate's scope. Provide a public function to access it.
1 parent 31403a4 commit a5b54f5

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/key_pair.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ impl KeyPair {
5656
pub fn from_der(der: &[u8]) -> Result<Self, RcgenError> {
5757
Ok(der.try_into()?)
5858
}
59+
/// Returns the key pair's signature algorithm
60+
pub fn algorithm(&self) -> &'static SignatureAlgorithm {
61+
self.alg
62+
}
5963
/// Parses the key pair from the ASCII PEM format
6064
#[cfg(feature = "pem")]
6165
pub fn from_pem(pem_str: &str) -> Result<Self, RcgenError> {
@@ -93,7 +97,7 @@ impl KeyPair {
9397
/// Usually, calling this function is not neccessary and you can just call
9498
/// [`from_der`](Self::from_der) instead. That function will try to figure
9599
/// out a fitting [`SignatureAlgorithm`] for the given
96-
/// key pair. However sometimes multiple signature algorithms fit for the
100+
/// key pair. However, sometimes multiple signature algorithms fit for the
97101
/// same der key. In that instance, you can use this function to precisely
98102
/// specify the `SignatureAlgorithm`.
99103
pub fn from_der_and_sign_algo(
@@ -173,7 +177,7 @@ pub trait RemoteKeyPair {
173177
/// Signs `msg` using the selected algorithm
174178
fn sign(&self, msg: &[u8]) -> Result<Vec<u8>, RcgenError>;
175179

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

@@ -364,3 +368,21 @@ pub(crate) trait PublicKeyData {
364368
})
365369
}
366370
}
371+
372+
#[cfg(test)]
373+
mod test {
374+
use super::*;
375+
376+
use ring::rand::SystemRandom;
377+
use ring::signature::{EcdsaKeyPair, ECDSA_P256_SHA256_FIXED_SIGNING};
378+
379+
#[test]
380+
fn test_algorithm() {
381+
let rng = SystemRandom::new();
382+
let pkcs8 = EcdsaKeyPair::generate_pkcs8(&ECDSA_P256_SHA256_FIXED_SIGNING, &rng).unwrap();
383+
let der = pkcs8.as_ref().to_vec();
384+
385+
let key_pair = KeyPair::from_der(&der).unwrap();
386+
assert_eq!(key_pair.algorithm(), &PKCS_ECDSA_P256_SHA256);
387+
}
388+
}

0 commit comments

Comments
 (0)