diff --git a/src/providers/trusted_service/context/mod.rs b/src/providers/trusted_service/context/mod.rs index 303d3df9..aa5ed498 100644 --- a/src/providers/trusted_service/context/mod.rs +++ b/src/providers/trusted_service/context/mod.rs @@ -4,7 +4,7 @@ use error::{Error, WrapperError}; use log::{error, info, trace}; use prost::Message; use std::convert::{TryFrom, TryInto}; -use std::ffi::{c_void, CString}; +use std::ffi::CString; use std::io::{self}; use std::ptr::null_mut; use std::slice; @@ -61,9 +61,8 @@ mod ts_protobuf; /// is required from the caller. #[derive(Debug)] pub struct Context { - rpc_caller: *mut rpc_caller, + rpc_caller_session: *mut rpc_caller_session, service_context: *mut service_context, - rpc_session_handle: *mut c_void, call_mutex: Mutex<()>, } @@ -75,33 +74,20 @@ impl Context { unsafe { service_locator_init() }; info!("Obtaining a crypto Trusted Service context."); - let mut status = 0; let service_name = CString::new("sn:trustedfirmware.org:crypto-protobuf:0").unwrap(); - let service_context = unsafe { service_locator_query(service_name.as_ptr(), &mut status) }; + let service_context = unsafe { service_locator_query(service_name.as_ptr()) }; if service_context.is_null() { - error!("Locating crypto Trusted Service failed, status: {}", status); + error!("Locating crypto Trusted Service failed"); return Err(io::Error::new( io::ErrorKind::Other, "Failed to obtain a Trusted Service context", ) .into()); - } else if status != 0 { - return Err(io::Error::new( - io::ErrorKind::Other, - format!( - "Failed to connect to Trusted Service; status code: {}", - status - ), - ) - .into()); } info!("Starting crypto Trusted Service context"); - let mut rpc_caller = null_mut(); - let rpc_session_handle = unsafe { - service_context_open(service_context, TS_RPC_ENCODING_PROTOBUF, &mut rpc_caller) - }; - if rpc_caller.is_null() || rpc_session_handle.is_null() { + let rpc_caller_session = unsafe { service_context_open(service_context) }; + if rpc_caller_session.is_null() { return Err(io::Error::new( io::ErrorKind::Other, "Failed to start Trusted Service context", @@ -109,9 +95,8 @@ impl Context { .into()); } let ctx = Context { - rpc_caller, + rpc_caller_session, service_context, - rpc_session_handle, call_mutex: Mutex::new(()), }; @@ -129,8 +114,19 @@ impl Context { trace!("Beginning call to Trusted Service"); let mut buf_out = null_mut(); - let call_handle = - unsafe { rpc_caller_begin(self.rpc_caller, &mut buf_out, req.encoded_len()) }; + // The response buffer length is set to 4096 as a common buffer length + // for all operations. In case of the session memory policy being "alloc_for_session" + // which is dependant on the platform, this value doesnt impact but for + // platforms with memory policy "alloc_for_each_call" the buffer length should be + // sufficient enough to hold the entire response. + let call_handle = unsafe { + rpc_caller_session_begin( + self.rpc_caller_session, + &mut buf_out, + req.encoded_len(), + 4096, + ) + }; if call_handle.is_null() { error!("Call handle was null"); return Err(WrapperError::CallHandleNull.into()); @@ -140,7 +136,9 @@ impl Context { } let mut buf_out = unsafe { slice::from_raw_parts_mut(buf_out, req.encoded_len()) }; req.encode(&mut buf_out).map_err(|e| { - unsafe { rpc_caller_end(self.rpc_caller, call_handle) }; + unsafe { + let _ = rpc_caller_session_end(call_handle); + }; format_error!("Failed to serialize Protobuf request", e); WrapperError::FailedPbConversion })?; @@ -151,13 +149,12 @@ impl Context { let mut resp_buf = null_mut(); let mut resp_buf_size = 0; let status = unsafe { - rpc_caller_invoke( - self.rpc_caller, + rpc_caller_session_invoke( call_handle, i32::from(req.opcode()).try_into().unwrap(), - &mut opstatus, &mut resp_buf, &mut resp_buf_size, + &mut opstatus, ) }; Error::from_status_opstatus( @@ -165,16 +162,23 @@ impl Context { i32::try_from(opstatus).map_err(|_| Error::Wrapper(WrapperError::InvalidOpStatus))?, ) .map_err(|e| { - unsafe { rpc_caller_end(self.rpc_caller, call_handle) }; + unsafe { + let _ = rpc_caller_session_end(call_handle); + }; e })?; let resp_buf = unsafe { slice::from_raw_parts_mut(resp_buf, resp_buf_size) }; resp.merge(&*resp_buf).map_err(|e| { - unsafe { rpc_caller_end(self.rpc_caller, call_handle) }; + unsafe { + let _ = rpc_caller_session_end(call_handle); + }; format_error!("Failed to serialize Protobuf request", e); WrapperError::FailedPbConversion })?; - unsafe { rpc_caller_end(self.rpc_caller, call_handle) }; + unsafe { + let status = rpc_caller_session_end(call_handle); + Error::from_status_opstatus(status, 0)?; + }; Ok(resp) } @@ -182,7 +186,7 @@ impl Context { impl Drop for Context { fn drop(&mut self) { - unsafe { service_context_close(self.service_context, self.rpc_session_handle) }; + unsafe { service_context_close(self.service_context, self.rpc_caller_session) }; unsafe { service_context_relinquish(self.service_context) }; }