6
6
//! for their Parsec operations.
7
7
use super :: Provide ;
8
8
use crate :: authenticators:: ApplicationIdentity ;
9
- use crate :: key_info_managers:: KeyInfoManagerClient ;
9
+ use crate :: key_info_managers:: { KeyIdentity , KeyInfoManagerClient } ;
10
10
use crate :: providers:: crypto_capability:: CanDoCrypto ;
11
11
use crate :: providers:: ProviderIdentity ;
12
12
use derivative:: Derivative ;
@@ -416,7 +416,8 @@ impl ProviderBuilder {
416
416
builder = builder. with_hierarchy_auth ( Hierarchy :: Endorsement , endorsement_auth) ;
417
417
self . endorsement_hierarchy_auth . zeroize ( ) ;
418
418
}
419
- Ok ( Provider :: new (
419
+
420
+ let built_provider = Provider :: new (
420
421
self . provider_name . ok_or_else ( || {
421
422
std:: io:: Error :: new ( ErrorKind :: InvalidData , "missing provider name" )
422
423
} ) ?,
@@ -427,6 +428,58 @@ impl ProviderBuilder {
427
428
format_error ! ( "Error creating TSS Transient Object Context" , e) ;
428
429
std:: io:: Error :: new ( ErrorKind :: InvalidData , "failed initializing TSS context" )
429
430
} ) ?,
430
- ) )
431
+ ) ;
432
+
433
+ // Get the root key from the key store
434
+ let root_key_identity = KeyIdentity :: new (
435
+ ApplicationIdentity :: new_internal ( ) ,
436
+ built_provider. provider_identity . clone ( ) ,
437
+ String :: from ( "RootKeyTPM" ) ,
438
+ ) ;
439
+ let key_is_stored = match built_provider
440
+ . key_info_store
441
+ . does_not_exist ( & root_key_identity)
442
+ {
443
+ Ok ( ( ) ) => false ,
444
+ Err ( ResponseStatus :: PsaErrorAlreadyExists ) => true ,
445
+ Err ( e) => Err ( e) . map_err ( |e| {
446
+ format_error ! ( "Failure accessing Key Info Manager" , e) ;
447
+ std:: io:: Error :: new ( ErrorKind :: InvalidData , "Key existence check failed" )
448
+ } ) ?,
449
+ } ;
450
+
451
+ if key_is_stored {
452
+ let stored_root_key_name: Vec < u8 > = built_provider
453
+ . key_info_store
454
+ . get_key_id ( & root_key_identity)
455
+ . map_err ( |e| {
456
+ format_error ! ( "Error getting Key Identities from the Key Info Store" , e) ;
457
+ std:: io:: Error :: new ( ErrorKind :: InvalidData , "failed getting Key Identities" )
458
+ } ) ?;
459
+ // Check if the stored public part coincides with the one in the context
460
+ let mut esapi_context = built_provider
461
+ . esapi_context
462
+ . lock ( )
463
+ . expect ( "ESAPI Context lock poisoned" ) ;
464
+
465
+ let root_key_name = esapi_context. get_root_key_name ( ) . map_err ( |e| {
466
+ format_error ! ( "Error getting the Root Key's name" , e) ;
467
+ std:: io:: Error :: new (
468
+ ErrorKind :: InvalidData ,
469
+ "failed getting Root Key's Name" ,
470
+ )
471
+ } ) ?;
472
+
473
+ if root_key_name. value ( ) . to_vec ( ) != stored_root_key_name {
474
+ let e = std:: io:: Error :: new (
475
+ ErrorKind :: InvalidData ,
476
+ "Obtained Root Key name does not coincide with the stored one" ,
477
+ ) ;
478
+ format_error ! ( "Error when verifying the Root Key's Name" , e) ;
479
+ return Err ( e) ;
480
+ }
481
+ }
482
+
483
+ Ok ( built_provider)
431
484
}
432
485
}
0 commit comments