@@ -10,7 +10,7 @@ use std::ptr::null_mut;
10
10
use std:: slice;
11
11
use std:: sync:: Mutex ;
12
12
use ts_binding:: * ;
13
- use ts_protobuf:: Opcode ;
13
+ use ts_protobuf:: { CloseKeyIn , GetOpcode , OpenKeyIn , OpenKeyOut , SetHandle } ;
14
14
15
15
#[ allow(
16
16
non_snake_case,
@@ -30,6 +30,7 @@ pub mod ts_binding {
30
30
include ! ( concat!( env!( "OUT_DIR" ) , "/ts_bindings.rs" ) ) ;
31
31
}
32
32
33
+ mod asym_sign;
33
34
mod key_management;
34
35
35
36
#[ allow(
@@ -48,6 +49,59 @@ mod key_management;
48
49
) ]
49
50
mod ts_protobuf {
50
51
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 ) ;
51
105
}
52
106
53
107
// TODO:
@@ -65,15 +119,27 @@ pub struct Context {
65
119
66
120
impl Context {
67
121
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." ) ;
69
127
let mut status = 0 ;
70
128
let service_context = unsafe {
71
129
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 ( ) ,
73
133
& mut status,
74
134
)
75
135
} ;
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 {
77
143
return Err ( Error :: new (
78
144
ErrorKind :: Other ,
79
145
format ! (
@@ -104,15 +170,14 @@ impl Context {
104
170
105
171
fn send_request < T : Message + Default > (
106
172
& self ,
107
- req : & impl Message ,
108
- opcode : Opcode ,
109
- rpc_cl : * mut rpc_caller ,
173
+ req : & ( impl Message + GetOpcode ) ,
110
174
) -> Result < T , PsaError > {
111
175
let _mutex_guard = self . call_mutex . try_lock ( ) . expect ( "Call mutex poisoned" ) ;
112
176
info ! ( "Beginning call to Trusted Service" ) ;
113
177
114
178
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 ( ) ) } ;
116
181
if call_handle == null_mut ( ) {
117
182
error ! ( "Call handle was null" ) ;
118
183
return Err ( PsaError :: CommunicationFailure ) ;
@@ -122,7 +187,7 @@ impl Context {
122
187
}
123
188
let mut buf_out = unsafe { slice:: from_raw_parts_mut ( buf_out, req. encoded_len ( ) ) } ;
124
189
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) } ;
126
191
format_error ! ( "Failed to serialize Protobuf request" , e) ;
127
192
PsaError :: CommunicationFailure
128
193
} ) ?;
@@ -134,39 +199,55 @@ impl Context {
134
199
let mut resp_buf_size = 0 ;
135
200
let status = unsafe {
136
201
rpc_caller_invoke (
137
- rpc_cl ,
202
+ self . rpc_caller ,
138
203
call_handle,
139
- i32:: from ( opcode) . try_into ( ) . unwrap ( ) ,
204
+ i32:: from ( req . opcode ( ) ) . try_into ( ) . unwrap ( ) ,
140
205
& mut opstatus,
141
206
& mut resp_buf,
142
207
& mut resp_buf_size,
143
208
)
144
209
} ;
145
210
if status != 0 || opstatus != 0 {
146
- unsafe { rpc_caller_end ( rpc_cl , call_handle) } ;
211
+ unsafe { rpc_caller_end ( self . rpc_caller , call_handle) } ;
147
212
error ! (
148
213
"Error on call invocation: status = {}, opstatus = {}" ,
149
- status, opstatus as i32
214
+ status, opstatus
150
215
) ;
151
- Status :: from ( opstatus as i32 ) . to_result ( ) ?;
216
+ Status :: from ( opstatus) . to_result ( ) ?;
152
217
}
153
218
let resp_buf = unsafe { slice:: from_raw_parts_mut ( resp_buf, resp_buf_size) } ;
154
219
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) } ;
156
221
format_error ! ( "Failed to serialize Protobuf request" , e) ;
157
222
PsaError :: CommunicationFailure
158
223
} ) ?;
159
- unsafe { rpc_caller_end ( rpc_cl , call_handle) } ;
224
+ unsafe { rpc_caller_end ( self . rpc_caller , call_handle) } ;
160
225
161
226
Ok ( resp)
162
227
}
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
+ }
163
244
}
164
245
165
246
impl Drop for Context {
166
247
fn drop ( & mut self ) {
167
248
unsafe { service_context_close ( self . service_context , self . rpc_session_handle ) } ;
168
249
169
- unsafe { service_locator_relinquish ( self . service_context ) } ;
250
+ unsafe { service_context_relinquish ( self . service_context ) } ;
170
251
}
171
252
}
172
253
0 commit comments