@@ -39,8 +39,6 @@ pub(crate) enum KeyPairKind {
39
39
/// A RSA key pair
40
40
#[ cfg( feature = "crypto" ) ]
41
41
Rsa ( RsaKeyPair , & ' static dyn RsaEncoding ) ,
42
- /// A remote key pair
43
- Remote ( Box < dyn SigningKey + Send + Sync > ) ,
44
42
}
45
43
46
44
impl fmt:: Debug for KeyPairKind {
@@ -52,7 +50,6 @@ impl fmt::Debug for KeyPairKind {
52
50
Self :: Ed ( key_pair) => write ! ( f, "{:?}" , key_pair) ,
53
51
#[ cfg( feature = "crypto" ) ]
54
52
Self :: Rsa ( key_pair, _) => write ! ( f, "{:?}" , key_pair) ,
55
- Self :: Remote ( _) => write ! ( f, "Box<dyn RemotePrivateKey>" ) ,
56
53
}
57
54
}
58
55
}
@@ -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
///
@@ -399,38 +387,6 @@ impl KeyPair {
399
387
std:: iter:: once ( self . alg )
400
388
}
401
389
402
- pub ( crate ) fn sign ( & self , msg : & [ u8 ] , writer : DERWriter ) -> Result < ( ) , Error > {
403
- match & self . kind {
404
- #[ cfg( feature = "crypto" ) ]
405
- KeyPairKind :: Ec ( kp) => {
406
- let system_random = SystemRandom :: new ( ) ;
407
- let signature = kp. sign ( & system_random, msg) . _err ( ) ?;
408
- let sig = & signature. as_ref ( ) ;
409
- writer. write_bitvec_bytes ( sig, & sig. len ( ) * 8 ) ;
410
- } ,
411
- #[ cfg( feature = "crypto" ) ]
412
- KeyPairKind :: Ed ( kp) => {
413
- let signature = kp. sign ( msg) ;
414
- let sig = & signature. as_ref ( ) ;
415
- writer. write_bitvec_bytes ( sig, & sig. len ( ) * 8 ) ;
416
- } ,
417
- #[ cfg( feature = "crypto" ) ]
418
- KeyPairKind :: Rsa ( kp, padding_alg) => {
419
- let system_random = SystemRandom :: new ( ) ;
420
- let mut signature = vec ! [ 0 ; rsa_key_pair_public_modulus_len( kp) ] ;
421
- kp. sign ( * padding_alg, & system_random, msg, & mut signature)
422
- . _err ( ) ?;
423
- let sig = & signature. as_ref ( ) ;
424
- writer. write_bitvec_bytes ( sig, & sig. len ( ) * 8 ) ;
425
- } ,
426
- KeyPairKind :: Remote ( kp) => {
427
- let signature = kp. sign ( msg) ?;
428
- writer. write_bitvec_bytes ( & signature, & signature. len ( ) * 8 ) ;
429
- } ,
430
- }
431
- Ok ( ( ) )
432
- }
433
-
434
390
/// Return the key pair's public key in DER format
435
391
///
436
392
/// The key is formatted according to the SubjectPublicKeyInfo struct of
@@ -454,11 +410,6 @@ impl KeyPair {
454
410
///
455
411
/// Panics if called on a remote key pair.
456
412
pub fn serialize_der ( & self ) -> Vec < u8 > {
457
- #[ cfg_attr( not( feature = "crypto" ) , allow( irrefutable_let_patterns) ) ]
458
- if let KeyPairKind :: Remote ( _) = self . kind {
459
- panic ! ( "Serializing a remote key pair is not supported" )
460
- }
461
-
462
413
self . serialized_der . clone ( )
463
414
}
464
415
@@ -467,24 +418,9 @@ impl KeyPair {
467
418
///
468
419
/// Panics if called on a remote key pair.
469
420
pub fn serialized_der ( & self ) -> & [ u8 ] {
470
- #[ cfg_attr( not( feature = "crypto" ) , allow( irrefutable_let_patterns) ) ]
471
- if let KeyPairKind :: Remote ( _) = self . kind {
472
- panic ! ( "Serializing a remote key pair is not supported" )
473
- }
474
-
475
421
& self . serialized_der
476
422
}
477
423
478
- /// Access the remote key pair if it is a remote one
479
- pub fn as_remote ( & self ) -> Option < & ( dyn SigningKey + Send + Sync ) > {
480
- #[ cfg_attr( not( feature = "crypto" ) , allow( irrefutable_let_patterns) ) ]
481
- if let KeyPairKind :: Remote ( remote) = & self . kind {
482
- Some ( remote. as_ref ( ) )
483
- } else {
484
- None
485
- }
486
- }
487
-
488
424
/// Serializes the key pair (including the private key) in PKCS#8 format in PEM
489
425
#[ cfg( feature = "pem" ) ]
490
426
pub fn serialize_pem ( & self ) -> String {
@@ -494,6 +430,29 @@ impl KeyPair {
494
430
}
495
431
}
496
432
433
+ impl SigningKey for KeyPair {
434
+ fn sign ( & self , msg : & [ u8 ] ) -> Result < Vec < u8 > , Error > {
435
+ Ok ( match & self . kind {
436
+ #[ cfg( feature = "crypto" ) ]
437
+ KeyPairKind :: Ec ( kp) => {
438
+ let system_random = SystemRandom :: new ( ) ;
439
+ let signature = kp. sign ( & system_random, msg) . _err ( ) ?;
440
+ signature. as_ref ( ) . to_owned ( )
441
+ } ,
442
+ #[ cfg( feature = "crypto" ) ]
443
+ KeyPairKind :: Ed ( kp) => kp. sign ( msg) . as_ref ( ) . to_owned ( ) ,
444
+ #[ cfg( feature = "crypto" ) ]
445
+ KeyPairKind :: Rsa ( kp, padding_alg) => {
446
+ let system_random = SystemRandom :: new ( ) ;
447
+ let mut signature = vec ! [ 0 ; rsa_key_pair_public_modulus_len( kp) ] ;
448
+ kp. sign ( * padding_alg, & system_random, msg, & mut signature)
449
+ . _err ( ) ?;
450
+ signature
451
+ } ,
452
+ } )
453
+ }
454
+ }
455
+
497
456
impl PublicKeyData for KeyPair {
498
457
fn public_key_der ( & self ) -> & [ u8 ] {
499
458
match & self . kind {
@@ -503,7 +462,6 @@ impl PublicKeyData for KeyPair {
503
462
KeyPairKind :: Ed ( kp) => kp. public_key ( ) . as_ref ( ) ,
504
463
#[ cfg( feature = "crypto" ) ]
505
464
KeyPairKind :: Rsa ( kp, _) => kp. public_key ( ) . as_ref ( ) ,
506
- KeyPairKind :: Remote ( kp) => kp. public_key_der ( ) ,
507
465
}
508
466
}
509
467
@@ -635,7 +593,7 @@ pub enum RsaKeySize {
635
593
}
636
594
637
595
pub ( crate ) fn sign_der (
638
- key : & KeyPair ,
596
+ key : & impl SigningKey ,
639
597
f : impl FnOnce ( & mut DERWriterSeq < ' _ > ) -> Result < ( ) , Error > ,
640
598
) -> Result < Vec < u8 > , Error > {
641
599
yasna:: try_construct_der ( |writer| {
@@ -644,10 +602,12 @@ pub(crate) fn sign_der(
644
602
writer. next ( ) . write_der ( & data) ;
645
603
646
604
// Write signatureAlgorithm
647
- key. alg . write_alg_ident ( writer. next ( ) ) ;
605
+ key. algorithm ( ) . write_alg_ident ( writer. next ( ) ) ;
648
606
649
607
// Write signature
650
- key. sign ( & data, writer. next ( ) ) ?;
608
+ let sig = key. sign ( & data) ?;
609
+ let writer = writer. next ( ) ;
610
+ writer. write_bitvec_bytes ( & sig, sig. len ( ) * 8 ) ;
651
611
652
612
Ok ( ( ) )
653
613
} )
0 commit comments