2
2
3
3
use {
4
4
crypto:: OpensslCrypto ,
5
- dpe:: commands:: { self , CertifyKeyCmd , CertifyKeyFlags , CommandHdr } ,
5
+ dpe:: commands:: { self , CertifyKeyCmd , CertifyKeyFlags , DeriveChildCmd , DeriveChildFlags , CommandHdr } ,
6
6
dpe:: context:: ContextHandle ,
7
7
dpe:: dpe_instance:: { DpeEnv , DpeTypes } ,
8
8
dpe:: response:: Response ,
11
11
platform:: default:: DefaultPlatform ,
12
12
zerocopy:: AsBytes ,
13
13
} ;
14
+ use std:: env;
14
15
15
16
pub struct TestTypes { }
16
17
@@ -19,21 +20,41 @@ impl DpeTypes for TestTypes {
19
20
type Platform < ' a > = DefaultPlatform ;
20
21
}
21
22
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
28
32
} ;
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) ;
29
39
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
+ }
31
51
52
+ fn certify_key ( dpe : & mut DpeInstance , env : & mut DpeEnv < TestTypes > , format : u32 ) -> Vec < u8 > {
32
53
let certify_key_cmd: CertifyKeyCmd = commands:: CertifyKeyCmd {
33
54
handle : ContextHandle :: default ( ) ,
34
55
flags : CertifyKeyFlags :: empty ( ) ,
35
56
label : [ 0 ; DPE_PROFILE . get_hash_size ( ) ] ,
36
- format : commands :: CertifyKeyCmd :: FORMAT_X509 ,
57
+ format,
37
58
} ;
38
59
let cmd_body = certify_key_cmd. as_bytes ( ) . to_vec ( ) ;
39
60
let cmd_hdr = CommandHdr :: new_for_test ( dpe:: commands:: Command :: CERTIFY_KEY )
@@ -43,7 +64,7 @@ fn main() {
43
64
command. extend ( cmd_body) ;
44
65
45
66
let resp = dpe
46
- . execute_serialized_command ( & mut env, 0 , & command)
67
+ . execute_serialized_command ( env, 0 , & command)
47
68
. unwrap ( ) ;
48
69
49
70
let certify_key_response = match resp {
@@ -53,9 +74,38 @@ fn main() {
53
74
_ => panic ! ( "Unexpected Response" ) ,
54
75
} ;
55
76
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
+
56
106
let pem = Pem :: new (
57
- "CERTIFICATE" ,
58
- & certify_key_response . cert [ ..certify_key_response . cert_size as usize ] ,
107
+ format_str ,
108
+ cert,
59
109
) ;
60
110
61
111
print ! (
0 commit comments