@@ -10,7 +10,7 @@ use std::ptr::null_mut;
1010use std:: slice;
1111use std:: sync:: Mutex ;
1212use ts_binding:: * ;
13- use ts_protobuf:: Opcode ;
13+ use ts_protobuf:: { CloseKeyIn , GetOpcode , OpenKeyIn , OpenKeyOut , SetHandle } ;
1414
1515#[ allow(
1616 non_snake_case,
@@ -30,6 +30,7 @@ pub mod ts_binding {
3030 include ! ( concat!( env!( "OUT_DIR" ) , "/ts_bindings.rs" ) ) ;
3131}
3232
33+ mod asym_sign;
3334mod key_management;
3435
3536#[ allow(
@@ -48,6 +49,59 @@ mod key_management;
4849) ]
4950mod ts_protobuf {
5051 include ! ( concat!( env!( "OUT_DIR" ) , "/ts_crypto.rs" ) ) ;
52+
53+ pub trait GetOpcode {
54+ fn opcode ( & self ) -> Opcode ;
55+ }
56+
57+ macro_rules! opcode_impl {
58+ ( $type: ty, $opcode: ident) => {
59+ impl GetOpcode for $type {
60+ fn opcode( & self ) -> Opcode {
61+ Opcode :: $opcode
62+ }
63+ }
64+ } ;
65+
66+ ( $type_in: ty, $type_out: ty, $opcode: ident) => {
67+ impl GetOpcode for $type_in {
68+ fn opcode( & self ) -> Opcode {
69+ Opcode :: $opcode
70+ }
71+ }
72+
73+ impl GetOpcode for $type_out {
74+ fn opcode( & self ) -> Opcode {
75+ Opcode :: $opcode
76+ }
77+ }
78+ } ;
79+ }
80+
81+ opcode_impl ! ( OpenKeyIn , OpenKeyOut , OpenKey ) ;
82+ opcode_impl ! ( CloseKeyIn , CloseKey ) ;
83+ opcode_impl ! ( GenerateKeyIn , GenerateKeyOut , GenerateKey ) ;
84+ opcode_impl ! ( DestroyKeyIn , DestroyKeyOut , DestroyKey ) ;
85+ opcode_impl ! ( SignHashIn , SignHashOut , SignHash ) ;
86+ opcode_impl ! ( VerifyHashIn , VerifyHashOut , VerifyHash ) ;
87+
88+ pub trait SetHandle {
89+ fn set_handle ( & mut self , handle : u32 ) ;
90+ }
91+
92+ macro_rules! set_handle_impl {
93+ ( $type: ty) => {
94+ impl SetHandle for $type {
95+ fn set_handle( & mut self , handle: u32 ) {
96+ self . handle = handle;
97+ }
98+ }
99+ } ;
100+ }
101+
102+ set_handle_impl ! ( DestroyKeyIn ) ;
103+ set_handle_impl ! ( SignHashIn ) ;
104+ set_handle_impl ! ( VerifyHashIn ) ;
51105}
52106
53107// TODO:
@@ -65,15 +119,27 @@ pub struct Context {
65119
66120impl Context {
67121 pub fn connect ( ) -> anyhow:: Result < Self > {
68- info ! ( "Querying for crypto Trusted Services" ) ;
122+ // Initialise service locator. Can be called multiple times,
123+ // but *must* be called at least once.
124+ unsafe { service_locator_init ( ) } ;
125+
126+ info ! ( "Obtaining a crypto Trusted Service context." ) ;
69127 let mut status = 0 ;
70128 let service_context = unsafe {
71129 service_locator_query (
72- CString :: new ( "sn:tf.org:crypto:0" ) . unwrap ( ) . into_raw ( ) ,
130+ CString :: new ( "sn:trustedfirmware.org:crypto:0" )
131+ . unwrap ( )
132+ . into_raw ( ) ,
73133 & mut status,
74134 )
75135 } ;
76- if service_context == null_mut ( ) || status != 0 {
136+ if service_context == null_mut ( ) {
137+ return Err ( Error :: new (
138+ ErrorKind :: Other ,
139+ "Failed to obtain a Trusted Service context" ,
140+ )
141+ . into ( ) ) ;
142+ } else if status != 0 {
77143 return Err ( Error :: new (
78144 ErrorKind :: Other ,
79145 format ! (
@@ -104,15 +170,14 @@ impl Context {
104170
105171 fn send_request < T : Message + Default > (
106172 & self ,
107- req : & impl Message ,
108- opcode : Opcode ,
109- rpc_cl : * mut rpc_caller ,
173+ req : & ( impl Message + GetOpcode ) ,
110174 ) -> Result < T , PsaError > {
111175 let _mutex_guard = self . call_mutex . try_lock ( ) . expect ( "Call mutex poisoned" ) ;
112176 info ! ( "Beginning call to Trusted Service" ) ;
113177
114178 let mut buf_out = null_mut ( ) ;
115- let call_handle = unsafe { rpc_caller_begin ( rpc_cl, & mut buf_out, req. encoded_len ( ) ) } ;
179+ let call_handle =
180+ unsafe { rpc_caller_begin ( self . rpc_caller , & mut buf_out, req. encoded_len ( ) ) } ;
116181 if call_handle == null_mut ( ) {
117182 error ! ( "Call handle was null" ) ;
118183 return Err ( PsaError :: CommunicationFailure ) ;
@@ -122,7 +187,7 @@ impl Context {
122187 }
123188 let mut buf_out = unsafe { slice:: from_raw_parts_mut ( buf_out, req. encoded_len ( ) ) } ;
124189 req. encode ( & mut buf_out) . map_err ( |e| {
125- unsafe { rpc_caller_end ( rpc_cl , call_handle) } ;
190+ unsafe { rpc_caller_end ( self . rpc_caller , call_handle) } ;
126191 format_error ! ( "Failed to serialize Protobuf request" , e) ;
127192 PsaError :: CommunicationFailure
128193 } ) ?;
@@ -134,39 +199,55 @@ impl Context {
134199 let mut resp_buf_size = 0 ;
135200 let status = unsafe {
136201 rpc_caller_invoke (
137- rpc_cl ,
202+ self . rpc_caller ,
138203 call_handle,
139- i32:: from ( opcode) . try_into ( ) . unwrap ( ) ,
204+ i32:: from ( req . opcode ( ) ) . try_into ( ) . unwrap ( ) ,
140205 & mut opstatus,
141206 & mut resp_buf,
142207 & mut resp_buf_size,
143208 )
144209 } ;
145210 if status != 0 || opstatus != 0 {
146- unsafe { rpc_caller_end ( rpc_cl , call_handle) } ;
211+ unsafe { rpc_caller_end ( self . rpc_caller , call_handle) } ;
147212 error ! (
148213 "Error on call invocation: status = {}, opstatus = {}" ,
149- status, opstatus as i32
214+ status, opstatus
150215 ) ;
151- Status :: from ( opstatus as i32 ) . to_result ( ) ?;
216+ Status :: from ( opstatus) . to_result ( ) ?;
152217 }
153218 let resp_buf = unsafe { slice:: from_raw_parts_mut ( resp_buf, resp_buf_size) } ;
154219 resp. merge ( & * resp_buf) . map_err ( |e| {
155- unsafe { rpc_caller_end ( rpc_cl , call_handle) } ;
220+ unsafe { rpc_caller_end ( self . rpc_caller , call_handle) } ;
156221 format_error ! ( "Failed to serialize Protobuf request" , e) ;
157222 PsaError :: CommunicationFailure
158223 } ) ?;
159- unsafe { rpc_caller_end ( rpc_cl , call_handle) } ;
224+ unsafe { rpc_caller_end ( self . rpc_caller , call_handle) } ;
160225
161226 Ok ( resp)
162227 }
228+
229+ fn send_request_with_key < T : Message + Default > (
230+ & self ,
231+ mut req : impl Message + GetOpcode + SetHandle ,
232+ key_id : u32 ,
233+ ) -> Result < T , PsaError > {
234+ let proto_req = OpenKeyIn { id : key_id } ;
235+ let OpenKeyOut { handle } = self . send_request ( & proto_req) ?;
236+ req. set_handle ( handle) ;
237+ let res = self . send_request ( & req) ;
238+ let proto_req = CloseKeyIn { handle } ;
239+ let res_close = self . send_request ( & proto_req) ;
240+ let res = res?;
241+ res_close?;
242+ Ok ( res)
243+ }
163244}
164245
165246impl Drop for Context {
166247 fn drop ( & mut self ) {
167248 unsafe { service_context_close ( self . service_context , self . rpc_session_handle ) } ;
168249
169- unsafe { service_locator_relinquish ( self . service_context ) } ;
250+ unsafe { service_context_relinquish ( self . service_context ) } ;
170251 }
171252}
172253
0 commit comments