22
33use {
44 crypto:: OpensslCrypto ,
5- dpe:: commands:: { self , CertifyKeyCmd , CertifyKeyFlags , CommandHdr } ,
5+ dpe:: commands:: { self , CertifyKeyCmd , CertifyKeyFlags , DeriveChildCmd , DeriveChildFlags , CommandHdr } ,
66 dpe:: context:: ContextHandle ,
77 dpe:: dpe_instance:: { DpeEnv , DpeTypes } ,
88 dpe:: response:: Response ,
1111 platform:: default:: DefaultPlatform ,
1212 zerocopy:: AsBytes ,
1313} ;
14+ use std:: env;
1415
1516pub struct TestTypes { }
1617
@@ -19,21 +20,41 @@ impl DpeTypes for TestTypes {
1920 type Platform < ' a > = DefaultPlatform ;
2021}
2122
22- fn main ( ) {
23- let support = Support :: AUTO_INIT | Support :: X509 ;
24-
25- let mut env = DpeEnv :: < TestTypes > {
26- crypto : OpensslCrypto :: new ( ) ,
27- platform : DefaultPlatform ,
23+ // Call DeriveChild on the default context so the generated cert will have a
24+ // TcbInfo populated.
25+ fn add_tcb_info ( dpe : & mut DpeInstance , env : & mut DpeEnv < TestTypes > , data : & [ u8 ; DPE_PROFILE . get_hash_size ( ) ] , tci_type : u32 ) {
26+ let cmd = DeriveChildCmd {
27+ handle : ContextHandle :: default ( ) ,
28+ data : * data,
29+ flags : DeriveChildFlags :: INPUT_ALLOW_X509 | DeriveChildFlags :: MAKE_DEFAULT ,
30+ tci_type,
31+ target_locality : 0 , // Unused since flag isn't set
2832 } ;
33+ let cmd_body = cmd. as_bytes ( ) . to_vec ( ) ;
34+ let cmd_hdr = CommandHdr :: new_for_test ( dpe:: commands:: Command :: DERIVE_CHILD )
35+ . as_bytes ( )
36+ . to_vec ( ) ;
37+ let mut command = cmd_hdr;
38+ command. extend ( cmd_body) ;
2939
30- let mut dpe = DpeInstance :: new ( & mut env, support) . unwrap ( ) ;
40+ let resp = dpe
41+ . execute_serialized_command ( env, 0 , & command)
42+ . unwrap ( ) ;
43+
44+ let _ = match resp {
45+ // Expect CertifyKey response return an error in all other cases.
46+ Response :: DeriveChild ( res) => res,
47+ Response :: Error ( res) => panic ! ( "Error response {}" , res. status) ,
48+ _ => panic ! ( "Unexpected Response" ) ,
49+ } ;
50+ }
3151
52+ fn certify_key ( dpe : & mut DpeInstance , env : & mut DpeEnv < TestTypes > , format : u32 ) -> Vec < u8 > {
3253 let certify_key_cmd: CertifyKeyCmd = commands:: CertifyKeyCmd {
3354 handle : ContextHandle :: default ( ) ,
3455 flags : CertifyKeyFlags :: empty ( ) ,
3556 label : [ 0 ; DPE_PROFILE . get_hash_size ( ) ] ,
36- format : commands :: CertifyKeyCmd :: FORMAT_X509 ,
57+ format,
3758 } ;
3859 let cmd_body = certify_key_cmd. as_bytes ( ) . to_vec ( ) ;
3960 let cmd_hdr = CommandHdr :: new_for_test ( dpe:: commands:: Command :: CERTIFY_KEY )
@@ -43,7 +64,7 @@ fn main() {
4364 command. extend ( cmd_body) ;
4465
4566 let resp = dpe
46- . execute_serialized_command ( & mut env, 0 , & command)
67+ . execute_serialized_command ( env, 0 , & command)
4768 . unwrap ( ) ;
4869
4970 let certify_key_response = match resp {
@@ -53,9 +74,38 @@ fn main() {
5374 _ => panic ! ( "Unexpected Response" ) ,
5475 } ;
5576
77+ certify_key_response. cert [ ..certify_key_response. cert_size as usize ] . to_vec ( )
78+ }
79+
80+ fn main ( ) {
81+ let args: Vec < String > = env:: args ( ) . collect ( ) ;
82+ let ( format, format_str) = if args. len ( ) > 1 {
83+ let arg = & args[ 1 ] ;
84+ if arg == "csr" {
85+ ( commands:: CertifyKeyCmd :: FORMAT_CSR , "PKCS7" )
86+ } else if arg == "x509" {
87+ ( commands:: CertifyKeyCmd :: FORMAT_X509 , "CERTIFICATE" )
88+ } else {
89+ panic ! ( "Unsupported format {}" , arg)
90+ }
91+ } else {
92+ ( commands:: CertifyKeyCmd :: FORMAT_X509 , "CERTIFICATE" )
93+ } ;
94+ let support = Support :: AUTO_INIT | Support :: X509 | Support :: CSR ;
95+
96+ let mut env = DpeEnv :: < TestTypes > {
97+ crypto : OpensslCrypto :: new ( ) ,
98+ platform : DefaultPlatform ,
99+ } ;
100+
101+ let mut dpe = DpeInstance :: new ( & mut env, support) . unwrap ( ) ;
102+
103+ add_tcb_info ( & mut dpe, & mut env, & [ 0 ; DPE_PROFILE . get_hash_size ( ) ] , u32:: from_be_bytes ( * b"TEST" ) ) ;
104+ let cert = certify_key ( & mut dpe, & mut env, format) ;
105+
56106 let pem = Pem :: new (
57- "CERTIFICATE" ,
58- & certify_key_response . cert [ ..certify_key_response . cert_size as usize ] ,
107+ format_str ,
108+ cert,
59109 ) ;
60110
61111 print ! (
0 commit comments