@@ -8,7 +8,9 @@ use ssh_key::{Algorithm, Cipher, KdfAlg, PrivateKey};
88#[ cfg( any( feature = "p256" , feature = "p384" , feature = "p521" ) ) ]
99use ssh_key:: EcdsaCurve ;
1010#[ cfg( feature = "alloc" ) ]
11- use ssh_key:: { LineEnding , PublicKey } ;
11+ use ssh_key:: LineEnding ;
12+ #[ cfg( any( feature = "alloc" , feature = "ed25519" ) ) ]
13+ use ssh_key:: PublicKey ;
1214#[ cfg( all( feature = "sha2" , feature = "std" ) ) ]
1315use std:: { io, path:: PathBuf , process} ;
1416
@@ -133,6 +135,16 @@ fn validate_dsa(key: PrivateKey) {
133135 assert ! ( key. kdf( ) . is_none( ) ) ;
134136
135137 let dsa_keypair = key. key_data ( ) . dsa ( ) . unwrap ( ) ;
138+ #[ cfg( feature = "dsa" ) ]
139+ {
140+ let native = dsa:: SigningKey :: try_from ( dsa_keypair. clone ( ) ) . unwrap ( ) ;
141+ check_native_public (
142+ ssh_key:: public:: DsaPublicKey :: try_from ( native. verifying_key ( ) ) . unwrap ( ) ,
143+ include_str ! ( "examples/id_dsa_1024.pub" ) ,
144+ ) ;
145+ let imported = ssh_key:: private:: DsaKeypair :: try_from ( native) . unwrap ( ) ;
146+ assert_eq ! ( PrivateKey :: from( imported) . key_data( ) , key. key_data( ) ) ;
147+ }
136148 assert_eq ! (
137149 & hex!(
138150 "dc3d89250ed9462114cb2c8d4816e3a511aaff1b06b0e01de17c1cb04e581bcab97176471d89fd7ca1817
@@ -348,6 +360,16 @@ fn validate_ed25519(key: PrivateKey) {
348360 assert ! ( key. kdf( ) . is_none( ) ) ;
349361
350362 let ed25519_keypair = key. key_data ( ) . ed25519 ( ) . unwrap ( ) ;
363+ #[ cfg( feature = "ed25519" ) ]
364+ {
365+ let native = ed25519_dalek:: SigningKey :: try_from ( ed25519_keypair. clone ( ) ) . unwrap ( ) ;
366+ check_native_public (
367+ ssh_key:: public:: Ed25519PublicKey :: from ( native. verifying_key ( ) ) ,
368+ include_str ! ( "examples/id_ed25519.pub" ) ,
369+ ) ;
370+ let imported = ssh_key:: private:: Ed25519Keypair :: from ( native) ;
371+ assert_eq ! ( PrivateKey :: from( imported) . key_data( ) , key. key_data( ) ) ;
372+ }
351373 assert_eq ! (
352374 & hex!( "b33eaef37ea2df7caa010defdea34e241f65f1b529a4f43ed14327f5c54aab62" ) ,
353375 ed25519_keypair. public. as_ref( ) ,
@@ -560,21 +582,27 @@ fn decode_rsa_4096_openssh() {
560582#[ test]
561583fn round_trip_rsa_3072 ( ) {
562584 let private_key = PrivateKey :: from_openssh ( OPENSSH_RSA_3072_EXAMPLE ) . unwrap ( ) ;
563- let rsa_keypair = match private_key. key_data ( ) {
564- ssh_key:: private:: KeypairData :: Rsa ( k) => k,
565- _ => unreachable ! ( ) ,
566- } ;
567-
568- let rsa_private_key = rsa:: PrivateKey :: try_from ( rsa_keypair) . unwrap ( ) ;
569- let rsa_keypair2 = ssh_key:: private:: RsaKeypair :: try_from ( & rsa_private_key) . unwrap ( ) ;
570- assert_eq ! ( rsa_keypair, & rsa_keypair2) ;
585+ let rsa_keypair = private_key. key_data ( ) . rsa ( ) . unwrap ( ) ;
586+ let native = rsa:: PrivateKey :: try_from ( rsa_keypair. clone ( ) ) . unwrap ( ) ;
587+ check_native_public (
588+ ssh_key:: public:: RsaPublicKey :: try_from ( native. to_public_key ( ) ) . unwrap ( ) ,
589+ include_str ! ( "examples/id_rsa_3072.pub" ) ,
590+ ) ;
591+ let imported = ssh_key:: private:: RsaKeypair :: try_from ( native) . unwrap ( ) ;
592+ assert_eq ! (
593+ PrivateKey :: from( imported) . key_data( ) ,
594+ private_key. key_data( )
595+ ) ;
571596}
572597
573598#[ cfg( feature = "alloc" ) ]
574599#[ test]
575600fn decode_security_key_private_fields ( ) {
576601 use encoding:: { Decode , Decoder , Encode } ;
577- use ssh_key:: private:: KeypairData ;
602+ use ssh_key:: {
603+ private:: { self , KeypairData } ,
604+ public:: KeyData ,
605+ } ;
578606
579607 for fixture in [
580608 include_str ! ( "examples/id_sk_ed25519.pub" ) ,
@@ -585,6 +613,25 @@ fn decode_security_key_private_fields() {
585613 let mut bytes = public. to_bytes ( ) . unwrap ( ) ;
586614 0x05u8 . encode ( & mut bytes) . unwrap ( ) ;
587615 b"handle" . as_slice ( ) . encode ( & mut bytes) . unwrap ( ) ;
616+
617+ // New handles have an empty reserved field. Imported keys can carry reserved data.
618+ let registered: PrivateKey = match public. key_data ( ) {
619+ KeyData :: SkEd25519 ( sk) => private:: SkEd25519 :: new ( sk. clone ( ) , 0x05 , b"handle" )
620+ . unwrap ( )
621+ . into ( ) ,
622+ #[ cfg( feature = "ecdsa" ) ]
623+ KeyData :: SkEcdsaSha2NistP256 ( sk) => {
624+ private:: SkEcdsaSha2NistP256 :: new ( sk. clone ( ) , 0x05 , b"handle" )
625+ . unwrap ( )
626+ . into ( )
627+ }
628+ _ => unreachable ! ( ) ,
629+ } ;
630+ let mut expected = bytes. clone ( ) ;
631+ b"" . as_slice ( ) . encode ( & mut expected) . unwrap ( ) ;
632+ assert_eq ! ( registered. key_data( ) . encode_vec( ) . unwrap( ) , expected) ;
633+ assert_eq ! ( registered. public_key( ) . key_data( ) , public. key_data( ) ) ;
634+
588635 b"reserved" . as_slice ( ) . encode ( & mut bytes) . unwrap ( ) ;
589636
590637 let mut reader = Decoder :: from_bytes ( & bytes) . unwrap ( ) ;
@@ -599,6 +646,7 @@ fn decode_security_key_private_fields() {
599646 assert_eq ! ( key_handle, b"handle" ) ;
600647 assert ! ( reader. is_finished( ) ) ;
601648 assert_eq ! ( keypair. encode_vec( ) . unwrap( ) , bytes) ;
649+ round_trip_keypair ( keypair, & public) ;
602650 }
603651}
604652
@@ -613,14 +661,17 @@ fn decode_custom_algorithm_openssh() {
613661 assert_eq ! ( KdfAlg :: None , key. kdf( ) . algorithm( ) ) ;
614662 assert ! ( key. kdf( ) . is_none( ) ) ;
615663
616- let opaque_keypair = key. key_data ( ) . other ( ) . unwrap ( ) ;
617- assert_eq ! (
618- & hex!( "888f24ee17adfed0091e67e485fb9844cfed6072cac1d06390e4005f5015b44f" ) ,
619- opaque_keypair. public. as_ref( ) ,
664+ let public = ssh_key:: public:: OpaquePublicKey :: new (
665+ hex ! ( "888f24ee17adfed0091e67e485fb9844cfed6072cac1d06390e4005f5015b44f" ) . to_vec ( ) ,
666+ Algorithm :: new ( "name@example.com" ) . unwrap ( ) ,
667+ ) ;
668+ let expected = ssh_key:: private:: OpaqueKeypair :: new (
669+ hex ! ( "986c953b4b5efb3285ff207c1ca5ee39a59047bc488fbc3b1ef036efc7575c75" ) . to_vec ( ) ,
670+ public,
620671 ) ;
621672 assert_eq ! (
622- & hex! ( "986c953b4b5efb3285ff207c1ca5ee39a59047bc488fbc3b1ef036efc7575c75" ) ,
623- opaque_keypair . private. as_ref ( ) ,
673+ key . key_data ( ) ,
674+ & ssh_key :: private:: KeypairData :: Other ( expected )
624675 ) ;
625676
626677 assert_eq ! ( key. comment( ) . as_bytes( ) , b"comment@example.com" ) ;
@@ -711,9 +762,24 @@ fn round_trip_test(private_key: &str) -> PrivateKey {
711762 let key2 = PrivateKey :: from_openssh ( & * pem) . unwrap ( ) ;
712763 assert_eq ! ( key, key2) ;
713764
765+ round_trip_keypair ( key. key_data ( ) . clone ( ) , key. public_key ( ) ) ;
714766 key
715767}
716768
769+ #[ cfg( feature = "alloc" ) ]
770+ fn round_trip_keypair ( keypair : ssh_key:: private:: KeypairData , public : & PublicKey ) {
771+ let key = PrivateKey :: new ( keypair, public. comment ( ) . clone ( ) ) . unwrap ( ) ;
772+ let decoded = PrivateKey :: from_bytes ( & key. to_bytes ( ) . unwrap ( ) ) . unwrap ( ) ;
773+ assert_eq ! ( decoded. public_key( ) , public) ;
774+ assert_eq ! ( decoded, key) ;
775+ }
776+
777+ #[ cfg( any( feature = "dsa" , feature = "ed25519" , feature = "rsa" ) ) ]
778+ fn check_native_public ( public : impl Into < PublicKey > , fixture : & str ) {
779+ let expected = PublicKey :: from_openssh ( fixture) . unwrap ( ) ;
780+ assert_eq ! ( public. into( ) . key_data( ) , expected. key_data( ) ) ;
781+ }
782+
717783/// Parse PEM encoded using `PrivateKey::to_openssh` using the `ssh-keygen` utility.
718784#[ cfg( all( feature = "sha2" , feature = "std" ) ) ]
719785fn encoding_integration_test ( private_key : PrivateKey ) {
0 commit comments