@@ -542,9 +542,11 @@ mod tests {
542
542
tests:: { Data , DataView } ,
543
543
KeyStore ,
544
544
} ,
545
- traits:: tests:: { TestIds , TestSigningKey , TestSymmKey } ,
546
- CompositeEncryptable , CryptoError , Decryptable , SignatureAlgorithm , SigningKey ,
547
- SigningNamespace , SymmetricCryptoKey ,
545
+ traits:: tests:: { TestAsymmKey , TestIds , TestSigningKey , TestSymmKey } ,
546
+ AsymmetricCryptoKey , AsymmetricPublicCryptoKey , CompositeEncryptable , CoseKeyBytes ,
547
+ CoseSerializable , Decryptable , KeyDecryptable , Pkcs8PrivateKeyBytes ,
548
+ PublicKeyEncryptionAlgorithm , SignatureAlgorithm , SignedPublicKey , SigningKey ,
549
+ SigningNamespace , SpkiPublicKeyBytes , SymmetricCryptoKey ,
548
550
} ;
549
551
550
552
#[ test]
@@ -734,4 +736,83 @@ mod tests {
734
736
& SigningNamespace :: ExampleNamespace
735
737
) )
736
738
}
739
+
740
+ #[ test]
741
+ fn test_account_key_rotation ( ) {
742
+ let store: KeyStore < TestIds > = KeyStore :: default ( ) ;
743
+ let mut ctx = store. context_mut ( ) ;
744
+
745
+ // Generate a new user key
746
+ let new_user_key = SymmetricCryptoKey :: make_xchacha20_poly1305_key ( ) ;
747
+ let current_user_private_key_id = TestAsymmKey :: A ( 0 ) ;
748
+ let current_user_signing_key_id = TestSigningKey :: A ( 0 ) ;
749
+
750
+ // Make the keys
751
+ ctx. generate_symmetric_key ( TestSymmKey :: A ( 0 ) ) . unwrap ( ) ;
752
+ ctx. make_signing_key ( current_user_signing_key_id) . unwrap ( ) ;
753
+ ctx. set_asymmetric_key (
754
+ current_user_private_key_id,
755
+ AsymmetricCryptoKey :: make ( PublicKeyEncryptionAlgorithm :: RsaOaepSha1 ) ,
756
+ )
757
+ . unwrap ( ) ;
758
+
759
+ // Get the rotated account keys
760
+ let rotated_keys = ctx
761
+ . dangerous_get_v2_rotated_account_keys (
762
+ new_user_key,
763
+ current_user_private_key_id,
764
+ current_user_signing_key_id,
765
+ )
766
+ . unwrap ( ) ;
767
+
768
+ let user_key = ctx. get_symmetric_key ( TestSymmKey :: A ( 0 ) ) . unwrap ( ) ;
769
+
770
+ // Public/Private key
771
+ assert_eq ! (
772
+ AsymmetricPublicCryptoKey :: from_der( & rotated_keys. public_key) . unwrap( ) ,
773
+ ctx. get_asymmetric_key( current_user_private_key_id)
774
+ . unwrap( )
775
+ . to_public_key( ) ,
776
+ ) ;
777
+ let decrypted_private_key: Vec < u8 > =
778
+ rotated_keys. private_key . decrypt_with_key ( user_key) . unwrap ( ) ;
779
+ let private_key =
780
+ AsymmetricCryptoKey :: from_der ( & Pkcs8PrivateKeyBytes :: from ( decrypted_private_key) )
781
+ . unwrap ( ) ;
782
+ assert_eq ! (
783
+ private_key. to_der( ) . unwrap( ) ,
784
+ ctx. get_asymmetric_key( current_user_private_key_id)
785
+ . unwrap( )
786
+ . to_der( )
787
+ . unwrap( )
788
+ ) ;
789
+
790
+ // Signing Key
791
+ let decrypted_signing_key: Vec < u8 > =
792
+ rotated_keys. signing_key . decrypt_with_key ( user_key) . unwrap ( ) ;
793
+ let signing_key =
794
+ SigningKey :: from_cose ( & CoseKeyBytes :: from ( decrypted_signing_key) ) . unwrap ( ) ;
795
+ assert_eq ! (
796
+ signing_key. to_cose( ) ,
797
+ ctx. get_signing_key( current_user_signing_key_id)
798
+ . unwrap( )
799
+ . to_cose( ) ,
800
+ ) ;
801
+
802
+ // Signed Public Key
803
+ let signed_public_key = SignedPublicKey :: try_from ( rotated_keys. signed_public_key ) . unwrap ( ) ;
804
+ let unwrapped_key = signed_public_key
805
+ . verify_and_unwrap (
806
+ & ctx. get_signing_key ( current_user_signing_key_id)
807
+ . unwrap ( )
808
+ . to_verifying_key ( ) ,
809
+ )
810
+ . unwrap ( ) ;
811
+ assert_eq ! (
812
+ unwrapped_key,
813
+ ctx. get_asymmetric_key( current_user_private_key_id)
814
+ . unwrap( )
815
+ . to_public_key( )
816
+ ) ;
817
+ }
737
818
}
0 commit comments