1
+ #[ cfg( feature = "crypto" ) ]
1
2
use std:: fmt;
2
3
3
4
#[ cfg( feature = "pem" ) ]
@@ -29,30 +30,23 @@ use crate::{sign_algo::SignatureAlgorithm, Error};
29
30
30
31
/// A key pair variant
31
32
#[ allow( clippy:: large_enum_variant) ]
33
+ #[ cfg( feature = "crypto" ) ]
32
34
pub ( crate ) enum KeyPairKind {
33
35
/// A Ecdsa key pair
34
- #[ cfg( feature = "crypto" ) ]
35
36
Ec ( EcdsaKeyPair ) ,
36
37
/// A Ed25519 key pair
37
- #[ cfg( feature = "crypto" ) ]
38
38
Ed ( Ed25519KeyPair ) ,
39
39
/// A RSA key pair
40
- #[ cfg( feature = "crypto" ) ]
41
40
Rsa ( RsaKeyPair , & ' static dyn RsaEncoding ) ,
42
- /// A remote key pair
43
- Remote ( Box < dyn SigningKey + Send + Sync > ) ,
44
41
}
45
42
43
+ #[ cfg( feature = "crypto" ) ]
46
44
impl fmt:: Debug for KeyPairKind {
47
45
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
48
46
match self {
49
- #[ cfg( feature = "crypto" ) ]
50
47
Self :: Ec ( key_pair) => write ! ( f, "{:?}" , key_pair) ,
51
- #[ cfg( feature = "crypto" ) ]
52
48
Self :: Ed ( key_pair) => write ! ( f, "{:?}" , key_pair) ,
53
- #[ cfg( feature = "crypto" ) ]
54
49
Self :: Rsa ( key_pair, _) => write ! ( f, "{:?}" , key_pair) ,
55
- Self :: Remote ( _) => write ! ( f, "Box<dyn RemotePrivateKey>" ) ,
56
50
}
57
51
}
58
52
}
@@ -64,12 +58,14 @@ impl fmt::Debug for KeyPairKind {
64
58
/// `openssl genrsa` doesn't work. See ring's [documentation](ring::signature::RsaKeyPair::from_pkcs8)
65
59
/// for how to generate RSA keys in the wanted format
66
60
/// and conversion between the formats.
61
+ #[ cfg( feature = "crypto" ) ]
67
62
pub struct KeyPair {
68
63
pub ( crate ) kind : KeyPairKind ,
69
64
pub ( crate ) alg : & ' static SignatureAlgorithm ,
70
65
pub ( crate ) serialized_der : Vec < u8 > ,
71
66
}
72
67
68
+ #[ cfg( feature = "crypto" ) ]
73
69
impl fmt:: Debug for KeyPair {
74
70
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
75
71
f. debug_struct ( "KeyPair" )
@@ -80,6 +76,7 @@ impl fmt::Debug for KeyPair {
80
76
}
81
77
}
82
78
79
+ #[ cfg( feature = "crypto" ) ]
83
80
impl KeyPair {
84
81
/// Generate a new random [`PKCS_ECDSA_P256_SHA256`] key pair
85
82
#[ cfg( feature = "crypto" ) ]
@@ -187,15 +184,6 @@ impl KeyPair {
187
184
Self :: try_from ( private_key. contents ( ) )
188
185
}
189
186
190
- /// Obtains the key pair from a raw public key and a remote private key
191
- pub fn from_remote ( key_pair : Box < dyn SigningKey + Send + Sync > ) -> Result < Self , Error > {
192
- Ok ( Self {
193
- alg : key_pair. algorithm ( ) ,
194
- kind : KeyPairKind :: Remote ( key_pair) ,
195
- serialized_der : Vec :: new ( ) ,
196
- } )
197
- }
198
-
199
187
/// Obtains the key pair from a DER formatted key
200
188
/// using the specified [`SignatureAlgorithm`]
201
189
///
@@ -419,40 +407,16 @@ impl KeyPair {
419
407
}
420
408
421
409
/// Serializes the key pair (including the private key) in PKCS#8 format in DER
422
- ///
423
- /// Panics if called on a remote key pair.
424
410
pub fn serialize_der ( & self ) -> Vec < u8 > {
425
- #[ cfg_attr( not( feature = "crypto" ) , allow( irrefutable_let_patterns) ) ]
426
- if let KeyPairKind :: Remote ( _) = self . kind {
427
- panic ! ( "Serializing a remote key pair is not supported" )
428
- }
429
-
430
411
self . serialized_der . clone ( )
431
412
}
432
413
433
414
/// Returns a reference to the serialized key pair (including the private key)
434
415
/// in PKCS#8 format in DER
435
- ///
436
- /// Panics if called on a remote key pair.
437
416
pub fn serialized_der ( & self ) -> & [ u8 ] {
438
- #[ cfg_attr( not( feature = "crypto" ) , allow( irrefutable_let_patterns) ) ]
439
- if let KeyPairKind :: Remote ( _) = self . kind {
440
- panic ! ( "Serializing a remote key pair is not supported" )
441
- }
442
-
443
417
& self . serialized_der
444
418
}
445
419
446
- /// Access the remote key pair if it is a remote one
447
- pub fn as_remote ( & self ) -> Option < & ( dyn SigningKey + Send + Sync ) > {
448
- #[ cfg_attr( not( feature = "crypto" ) , allow( irrefutable_let_patterns) ) ]
449
- if let KeyPairKind :: Remote ( remote) = & self . kind {
450
- Some ( remote. as_ref ( ) )
451
- } else {
452
- None
453
- }
454
- }
455
-
456
420
/// Serializes the key pair (including the private key) in PKCS#8 format in PEM
457
421
#[ cfg( feature = "pem" ) ]
458
422
pub fn serialize_pem ( & self ) -> String {
@@ -479,21 +443,17 @@ impl SigningKey for KeyPair {
479
443
. _err ( ) ?;
480
444
signature
481
445
} ,
482
- KeyPairKind :: Remote ( kp) => kp. sign ( msg) ?,
483
446
} )
484
447
}
485
448
}
486
449
450
+ #[ cfg( feature = "crypto" ) ]
487
451
impl PublicKeyData for KeyPair {
488
452
fn der_bytes ( & self ) -> & [ u8 ] {
489
453
match & self . kind {
490
- #[ cfg( feature = "crypto" ) ]
491
454
KeyPairKind :: Ec ( kp) => kp. public_key ( ) . as_ref ( ) ,
492
- #[ cfg( feature = "crypto" ) ]
493
455
KeyPairKind :: Ed ( kp) => kp. public_key ( ) . as_ref ( ) ,
494
- #[ cfg( feature = "crypto" ) ]
495
456
KeyPairKind :: Rsa ( kp, _) => kp. public_key ( ) . as_ref ( ) ,
496
- KeyPairKind :: Remote ( kp) => kp. der_bytes ( ) ,
497
457
}
498
458
}
499
459
@@ -647,9 +607,6 @@ pub(crate) fn sign_der(
647
607
}
648
608
649
609
/// A key that can be used to sign messages
650
- ///
651
- /// Trait objects based on this trait can be passed to the [`KeyPair::from_remote`] function for generating certificates
652
- /// from a remote and raw private key, for example an HSM.
653
610
pub trait SigningKey : PublicKeyData {
654
611
/// Signs `msg` using the selected algorithm
655
612
fn sign ( & self , msg : & [ u8 ] ) -> Result < Vec < u8 > , Error > ;
0 commit comments