@@ -893,6 +893,56 @@ fn ro_rw_session_test() -> TestResult {
893893 Ok ( ( ) )
894894}
895895
896+ #[ test]
897+ #[ serial]
898+ fn session_copy_object ( ) -> TestResult {
899+ let aes128_template = [
900+ Attribute :: Class ( ObjectClass :: SECRET_KEY ) ,
901+ Attribute :: KeyType ( KeyType :: AES ) ,
902+ Attribute :: Encrypt ( true ) ,
903+ Attribute :: Token ( false ) ,
904+ Attribute :: Private ( true ) ,
905+ Attribute :: Sensitive ( true ) ,
906+ Attribute :: Extractable ( false ) ,
907+ Attribute :: ValueLen ( 16 . into ( ) ) ,
908+ Attribute :: Label ( "original" . as_bytes ( ) . to_vec ( ) ) ,
909+ ] ;
910+
911+ let copy_template = vec ! [ Attribute :: Label ( "copy" . as_bytes( ) . to_vec( ) ) ] ;
912+
913+ let insecure_copy_template = vec ! [ Attribute :: Extractable ( true ) ] ;
914+
915+ let ( pkcs11, slot) = init_pins ( ) ;
916+
917+ // open a session
918+ let rw_session = pkcs11. open_rw_session ( slot) ?;
919+
920+ // log in the session
921+ rw_session. login ( UserType :: User , Some ( & AuthPin :: new ( USER_PIN . into ( ) ) ) ) ?;
922+
923+ // create a key object
924+ let object = rw_session. generate_key ( & Mechanism :: AesKeyGen , & aes128_template) ?;
925+
926+ // copy the object without a template
927+ let copy = rw_session. copy_object ( object, & [ ] ) ?;
928+ rw_session. destroy_object ( copy) ?;
929+
930+ // copy the object with a template
931+ let copy = rw_session. copy_object ( object, & copy_template) ?;
932+ rw_session. destroy_object ( copy) ?;
933+
934+ // try the copy with the insecure template. It should fail. Returning CKR_OK is considered a failure.
935+ rw_session
936+ . copy_object ( object, & insecure_copy_template)
937+ . unwrap_err ( ) ;
938+
939+ // delete keys
940+ rw_session. destroy_object ( object) ?;
941+ rw_session. logout ( ) ?;
942+
943+ Ok ( ( ) )
944+ }
945+
896946#[ test]
897947#[ serial]
898948fn aes_cbc_encrypt ( ) -> TestResult {
0 commit comments