optee_teec/session.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
use libc;
use optee_teec_sys as raw;
use std::ptr;
use crate::Param;
use crate::{Context, Error, Operation, Result, Uuid};
/// Session login methods.
#[derive(Copy, Clone)]
pub enum ConnectionMethods {
/// No login data is provided.
LoginPublic,
/// Login data about the user running the Client Application process is provided.
LoginUser,
/// Login data about the group running the Client Application process is provided.
LoginGroup,
/// Login data about the running Client Application itself is provided.
LoginApplication,
/// Login data about the user and the running Client Application itself is provided.
LoginUserApplication,
/// Login data about the group and the running Client Application itself is provided.
LoginGroupApplication,
}
/// Represents a connection between a client application and a trusted application.
pub struct Session {
raw: raw::TEEC_Session,
}
impl Session {
/// Initializes a TEE session object with specified context and uuid.
pub fn new<A: Param, B: Param, C: Param, D: Param>(
context: &mut Context,
uuid: Uuid,
operation: Option<&mut Operation<A, B, C, D>>,
) -> Result<Self> {
let mut raw_session = raw::TEEC_Session {
ctx: context.as_mut_raw_ptr(),
session_id: 0,
};
let mut err_origin: libc::uint32_t = 0;
let raw_operation = match operation {
Some(o) => o.as_mut_raw_ptr(),
None => ptr::null_mut() as *mut raw::TEEC_Operation,
};
unsafe {
match raw::TEEC_OpenSession(
context.as_mut_raw_ptr(),
&mut raw_session,
uuid.as_raw_ptr(),
ConnectionMethods::LoginPublic as u32,
ptr::null() as *const libc::c_void,
raw_operation,
&mut err_origin,
) {
raw::TEEC_SUCCESS => Ok(Self { raw: raw_session }),
code => Err(Error::from_raw_error(code)),
}
}
}
/// Converts a TEE client context to a raw pointer.
pub fn as_mut_raw_ptr(&mut self) -> *mut raw::TEEC_Session {
&mut self.raw
}
/// Invokes a command with an operation with this session.
pub fn invoke_command<A: Param, B: Param, C: Param, D: Param>(
&mut self,
command_id: u32,
operation: &mut Operation<A, B, C, D>,
) -> Result<()> {
let mut err_origin: libc::uint32_t = 0;
unsafe {
match raw::TEEC_InvokeCommand(
&mut self.raw,
command_id,
operation.as_mut_raw_ptr(),
&mut err_origin,
) {
raw::TEEC_SUCCESS => Ok(()),
code => Err(Error::from_raw_error(code)),
}
}
}
}
impl Drop for Session {
fn drop(&mut self) {
unsafe {
raw::TEEC_CloseSession(&mut self.raw);
}
}
}