1 // Licensed to the Apache Software Foundation (ASF) under one 2 // or more contributor license agreements. See the NOTICE file 3 // distributed with this work for additional information 4 // regarding copyright ownership. The ASF licenses this file 5 // to you under the Apache License, Version 2.0 (the 6 // "License"); you may not use this file except in compliance 7 // with the License. You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 use crate::{Error, Operation, Result, Session, Uuid}; 19 use crate::{Param, ParamNone}; 20 use libc; 21 use optee_teec_sys as raw; 22 use std::ptr; 23 24 /// An abstraction of the logical connection between a client application and a 25 /// TEE. 26 pub struct Context { 27 raw: raw::TEEC_Context, 28 } 29 30 impl Context { 31 /// Creates a TEE client context object. 32 /// 33 /// # Examples 34 /// 35 /// ``` 36 /// let ctx = Context::new().unwrap(); 37 /// ``` new() -> Result<Context>38 pub fn new() -> Result<Context> { 39 Context::new_raw(0, true, false).map(|raw| Context { raw }) 40 } 41 42 /// Creates a raw TEE client context with implementation defined parameters. 43 /// 44 /// # Examples 45 /// 46 /// ``` 47 /// let raw_ctx: optee_teec_sys::TEEC_Context = Context::new_raw(0, true).unwrap(); 48 /// ``` new_raw(fd: libc::c_int, reg_mem: bool, memref_null: bool) -> Result<raw::TEEC_Context>49 pub fn new_raw(fd: libc::c_int, reg_mem: bool, memref_null: bool) -> Result<raw::TEEC_Context> { 50 let mut raw_ctx = raw::TEEC_Context { 51 fd, 52 reg_mem, 53 memref_null, 54 }; 55 unsafe { 56 match raw::TEEC_InitializeContext(ptr::null_mut() as *mut libc::c_char, &mut raw_ctx) { 57 raw::TEEC_SUCCESS => Ok(raw_ctx), 58 code => Err(Error::from_raw_error(code)), 59 } 60 } 61 } 62 63 /// Converts a TEE client context to a raw pointer. 64 /// 65 /// # Examples 66 /// 67 /// ``` 68 /// let mut ctx = Context::new().unwrap(); 69 /// let mut raw_ptr: *mut optee_teec_sys::TEEC_Context = ctx.as_mut_raw_ptr(); 70 /// ``` as_mut_raw_ptr(&mut self) -> *mut raw::TEEC_Context71 pub fn as_mut_raw_ptr(&mut self) -> *mut raw::TEEC_Context { 72 &mut self.raw 73 } 74 75 /// Opens a new session with the specified trusted application. 76 /// 77 /// The target trusted application is specified by `uuid`. 78 /// 79 /// # Examples 80 /// 81 /// ``` 82 /// let mut ctx = Context::new().unwrap(); 83 /// let uuid = Uuid::parse_str("8abcf200-2450-11e4-abe2-0002a5d5c51b").unwrap(); 84 /// let session = ctx.open_session(uuid).unwrap(); 85 /// ``` open_session(&mut self, uuid: Uuid) -> Result<Session>86 pub fn open_session(&mut self, uuid: Uuid) -> Result<Session> { 87 Session::new( 88 self, 89 uuid, 90 None::<&mut Operation<ParamNone, ParamNone, ParamNone, ParamNone>>, 91 ) 92 } 93 94 /// Opens a new session with the specified trusted application, pass some 95 /// parameters to TA by an operation. 96 /// 97 /// The target trusted application is specified by `uuid`. 98 /// 99 /// # Examples 100 /// 101 /// ``` 102 /// let mut ctx = Context::new().unwrap(); 103 /// let uuid = Uuid::parse_str("8abcf200-2450-11e4-abe2-0002a5d5c51b").unwrap(); 104 /// let p0 = ParamValue(42, 0, ParamType::ValueInout); 105 /// let mut operation = Operation::new(0, p0, ParamNone, ParamNone, ParamNone); 106 /// let session = ctx.open_session_with_operation(uuid, operation).unwrap(); 107 /// ``` open_session_with_operation<A: Param, B: Param, C: Param, D: Param>( &mut self, uuid: Uuid, operation: &mut Operation<A, B, C, D>, ) -> Result<Session>108 pub fn open_session_with_operation<A: Param, B: Param, C: Param, D: Param>( 109 &mut self, 110 uuid: Uuid, 111 operation: &mut Operation<A, B, C, D>, 112 ) -> Result<Session> { 113 Session::new(self, uuid, Some(operation)) 114 } 115 } 116 117 impl Drop for Context { drop(&mut self)118 fn drop(&mut self) { 119 unsafe { 120 raw::TEEC_FinalizeContext(&mut self.raw); 121 } 122 } 123 } 124