@@ -36,6 +36,7 @@ use crate::{
36
36
37
37
use log:: error;
38
38
use serde:: { Deserialize , Serialize } ;
39
+ use std:: collections:: HashMap ;
39
40
use std:: convert:: { TryFrom , TryInto } ;
40
41
use zeroize:: Zeroize ;
41
42
@@ -532,10 +533,10 @@ impl TransientKeyContext {
532
533
#[ derive( Debug ) ]
533
534
pub struct TransientKeyContextBuilder {
534
535
tcti_name_conf : TctiNameConf ,
535
- hierarchy : Hierarchy ,
536
536
root_key_size : u16 , // TODO: replace with root key PUBLIC definition
537
537
root_key_auth_size : usize ,
538
- hierarchy_auth : Vec < u8 > ,
538
+ root_hierarchy : Hierarchy ,
539
+ hierarchy_auth : HashMap < Hierarchy , Vec < u8 > > ,
539
540
default_context_cipher : SymmetricDefinitionObject ,
540
541
session_hash_alg : HashingAlgorithm ,
541
542
}
@@ -545,10 +546,10 @@ impl TransientKeyContextBuilder {
545
546
pub fn new ( ) -> Self {
546
547
TransientKeyContextBuilder {
547
548
tcti_name_conf : TctiNameConf :: Device ( Default :: default ( ) ) ,
548
- hierarchy : Hierarchy :: Owner ,
549
+ root_hierarchy : Hierarchy :: Owner ,
549
550
root_key_size : 2048 ,
550
551
root_key_auth_size : 32 ,
551
- hierarchy_auth : Vec :: new ( ) ,
552
+ hierarchy_auth : HashMap :: new ( ) ,
552
553
default_context_cipher : SymmetricDefinitionObject :: AES_256_CFB ,
553
554
session_hash_alg : HashingAlgorithm :: Sha256 ,
554
555
}
@@ -560,9 +561,15 @@ impl TransientKeyContextBuilder {
560
561
self
561
562
}
562
563
564
+ /// Set the auth values for any hierarchies that will be used
565
+ pub fn with_hierarchy_auth ( mut self , hierarchy : Hierarchy , auth : Vec < u8 > ) -> Self {
566
+ let _ = self . hierarchy_auth . insert ( hierarchy, auth) ;
567
+ self
568
+ }
569
+
563
570
/// Define which hierarchy will be used for the keys being managed.
564
- pub fn with_hierarchy ( mut self , hierarchy : Hierarchy ) -> Self {
565
- self . hierarchy = hierarchy;
571
+ pub fn with_root_hierarchy ( mut self , hierarchy : Hierarchy ) -> Self {
572
+ self . root_hierarchy = hierarchy;
566
573
self
567
574
}
568
575
@@ -578,12 +585,6 @@ impl TransientKeyContextBuilder {
578
585
self
579
586
}
580
587
581
- /// Input the authentication value of the working hierarchy.
582
- pub fn with_hierarchy_auth ( mut self , hierarchy_auth : Vec < u8 > ) -> Self {
583
- self . hierarchy_auth = hierarchy_auth;
584
- self
585
- }
586
-
587
588
/// Define the cipher to be used within this context as a default.
588
589
///
589
590
/// Currently this default is used for:
@@ -624,7 +625,7 @@ impl TransientKeyContextBuilder {
624
625
/// `Context::set_handle_auth`
625
626
/// * if the root key authentication size is given greater than 32 or if the root key size is
626
627
/// not 1024, 2048, 3072 or 4096, a `InvalidParam` wrapper error is returned
627
- pub fn build ( self ) -> Result < TransientKeyContext > {
628
+ pub fn build ( mut self ) -> Result < TransientKeyContext > {
628
629
if self . root_key_auth_size > 32 {
629
630
return Err ( Error :: local_error ( ErrorKind :: WrongParamSize ) ) ;
630
631
}
@@ -640,9 +641,9 @@ impl TransientKeyContextBuilder {
640
641
None
641
642
} ;
642
643
643
- if ! self . hierarchy_auth . is_empty ( ) {
644
- let auth_hierarchy = Auth :: try_from ( self . hierarchy_auth ) ?;
645
- context. tr_set_auth ( self . hierarchy . into ( ) , & auth_hierarchy) ?;
644
+ for ( hierarchy , auth ) in self . hierarchy_auth . drain ( ) {
645
+ let auth_hierarchy = Auth :: try_from ( auth ) ?;
646
+ context. tr_set_auth ( hierarchy. into ( ) , & auth_hierarchy) ?;
646
647
}
647
648
648
649
let session = context
@@ -669,7 +670,7 @@ impl TransientKeyContextBuilder {
669
670
670
671
let root_key_handle = context
671
672
. create_primary (
672
- self . hierarchy ,
673
+ self . root_hierarchy ,
673
674
& create_restricted_decryption_rsa_public (
674
675
self . default_context_cipher ,
675
676
root_key_rsa_key_bits,
0 commit comments