@@ -56,6 +56,10 @@ impl KeyPair {
56
56
pub fn from_der ( der : & [ u8 ] ) -> Result < Self , RcgenError > {
57
57
Ok ( der. try_into ( ) ?)
58
58
}
59
+ /// Returns the key pair's signature algorithm
60
+ pub fn algorithm ( & self ) -> & ' static SignatureAlgorithm {
61
+ self . alg
62
+ }
59
63
/// Parses the key pair from the ASCII PEM format
60
64
#[ cfg( feature = "pem" ) ]
61
65
pub fn from_pem ( pem_str : & str ) -> Result < Self , RcgenError > {
@@ -93,7 +97,7 @@ impl KeyPair {
93
97
/// Usually, calling this function is not neccessary and you can just call
94
98
/// [`from_der`](Self::from_der) instead. That function will try to figure
95
99
/// 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
97
101
/// same der key. In that instance, you can use this function to precisely
98
102
/// specify the `SignatureAlgorithm`.
99
103
pub fn from_der_and_sign_algo (
@@ -173,7 +177,7 @@ pub trait RemoteKeyPair {
173
177
/// Signs `msg` using the selected algorithm
174
178
fn sign ( & self , msg : & [ u8 ] ) -> Result < Vec < u8 > , RcgenError > ;
175
179
176
- /// Reveals which algorithm will be used when you call `sign()`
180
+ /// Reveals the algorithm to be used when calling `sign()`
177
181
fn algorithm ( & self ) -> & ' static SignatureAlgorithm ;
178
182
}
179
183
@@ -364,3 +368,21 @@ pub(crate) trait PublicKeyData {
364
368
} )
365
369
}
366
370
}
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