@@ -526,3 +526,103 @@ impl ProviderBuilder {
526
526
Ok ( built_provider)
527
527
}
528
528
}
529
+
530
+ #[ cfg( test) ]
531
+ mod test {
532
+ use crate :: key_info_managers:: { KeyIdentity , KeyInfoManagerFactory } ;
533
+ use crate :: providers:: tpm:: ROOT_KEY_SIZE ;
534
+ use crate :: providers:: tpm:: { Provider , ProviderBuilder } ;
535
+ use crate :: providers:: ApplicationIdentity ;
536
+ use crate :: providers:: ProviderIdentity ;
537
+ use crate :: utils:: config:: { KeyInfoManagerConfig , KeyInfoManagerType } ;
538
+ use parsec_interface:: operations:: psa_algorithm:: Algorithm ;
539
+ use parsec_interface:: operations:: psa_key_attributes:: {
540
+ Attributes , Lifetime , Policy , Type , UsageFlags ,
541
+ } ;
542
+ use parsec_interface:: requests:: AuthType ;
543
+
544
+ #[ test]
545
+ fn test_root_key_check ( ) {
546
+ let tcti = "mssim:host=127.0.0.1,port=2321" ;
547
+ let owner_hierarchy_auth = "hex:74706d5f70617373" ;
548
+ let endorsement_hierarchy_auth = "str:endorsement_pass" . to_string ( ) ;
549
+
550
+ let provider_identity =
551
+ ProviderIdentity :: new ( Provider :: PROVIDER_UUID . to_string ( ) , "Tpm" . to_string ( ) ) ;
552
+ let kim_config = KeyInfoManagerConfig {
553
+ name : "sqlite-manager" . to_string ( ) ,
554
+ manager_type : KeyInfoManagerType :: SQLite ,
555
+ store_path : None ,
556
+ sqlite_db_path : Some (
557
+ "./kim-mappings/sqlite/sqlite-key-info-manager.sqlite3" . to_string ( ) ,
558
+ ) ,
559
+ } ;
560
+
561
+ let kim_factory = KeyInfoManagerFactory :: new ( & kim_config, AuthType :: NoAuth ) . unwrap ( ) ;
562
+ // Builds the tpm provider and inserts an internal key
563
+ {
564
+ let builder = ProviderBuilder :: new ( )
565
+ . with_key_info_store ( kim_factory. build_client ( provider_identity. clone ( ) ) )
566
+ . with_tcti ( tcti)
567
+ . with_provider_name ( "Tpm" . to_string ( ) )
568
+ . with_owner_hierarchy_auth ( owner_hierarchy_auth. to_string ( ) )
569
+ . with_endorsement_hierarchy_auth ( endorsement_hierarchy_auth. clone ( ) ) ;
570
+
571
+ unsafe {
572
+ let _ = builder. build ( ) . unwrap ( ) ;
573
+ }
574
+ }
575
+
576
+ // Builds the tpm provider, checking that the internally stored key matches
577
+ // the newly generated one.
578
+ // Then, it modifies the key information inside so that the next check fails.
579
+ {
580
+ let builder = ProviderBuilder :: new ( )
581
+ . with_key_info_store ( kim_factory. build_client ( provider_identity. clone ( ) ) )
582
+ . with_tcti ( tcti)
583
+ . with_provider_name ( "Tpm" . to_string ( ) )
584
+ . with_owner_hierarchy_auth ( owner_hierarchy_auth. to_string ( ) )
585
+ . with_endorsement_hierarchy_auth ( endorsement_hierarchy_auth. clone ( ) ) ;
586
+ // Reads the key and verifies the created one vs the stored one
587
+
588
+ let built_provider = unsafe { builder. build ( ) . unwrap ( ) } ;
589
+
590
+ // replace the key info from the internal key with some incorrect information
591
+ let root_key_identity = KeyIdentity :: new (
592
+ ApplicationIdentity :: new_internal ( ) ,
593
+ built_provider. provider_identity . clone ( ) ,
594
+ String :: from ( "RootKeyTPM" ) ,
595
+ ) ;
596
+ let attributes = Attributes {
597
+ lifetime : Lifetime :: Persistent ,
598
+ key_type : Type :: RsaPublicKey ,
599
+ bits : ROOT_KEY_SIZE as usize ,
600
+ policy : Policy {
601
+ // Internal key, usage_flags information is not relevant
602
+ usage_flags : UsageFlags :: default ( ) ,
603
+ // Internal key, permitted_algorithms information is not relevant
604
+ permitted_algorithms : Algorithm :: None ,
605
+ } ,
606
+ } ;
607
+
608
+ let test_material: Vec < u8 > = vec ! [ 1 , 2 ] ;
609
+
610
+ built_provider
611
+ . key_info_store
612
+ . replace_key_info ( root_key_identity, & test_material, attributes)
613
+ . unwrap ( ) ;
614
+ }
615
+
616
+ // Should fail as the newly replaced key information does not match
617
+ // the previously created one.
618
+ {
619
+ let builder = ProviderBuilder :: new ( )
620
+ . with_key_info_store ( kim_factory. build_client ( provider_identity. clone ( ) ) )
621
+ . with_tcti ( tcti)
622
+ . with_provider_name ( "Tpm" . to_string ( ) )
623
+ . with_owner_hierarchy_auth ( owner_hierarchy_auth. to_string ( ) )
624
+ . with_endorsement_hierarchy_auth ( endorsement_hierarchy_auth) ;
625
+ assert ! ( unsafe { builder. build( ) . is_err( ) } ) ;
626
+ }
627
+ }
628
+ }
0 commit comments