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).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) -> Result<raw::TEEC_Context>49     pub fn new_raw(fd: libc::c_int, reg_mem: bool) -> Result<raw::TEEC_Context> {
50         let mut raw_ctx = raw::TEEC_Context { fd, reg_mem };
51         unsafe {
52             match raw::TEEC_InitializeContext(ptr::null_mut() as *mut libc::c_char, &mut raw_ctx) {
53                 raw::TEEC_SUCCESS => Ok(raw_ctx),
54                 code => Err(Error::from_raw_error(code)),
55             }
56         }
57     }
58 
59     /// Converts a TEE client context to a raw pointer.
60     ///
61     /// # Examples
62     ///
63     /// ```
64     /// let mut ctx = Context::new().unwrap();
65     /// let mut raw_ptr: *mut optee_teec_sys::TEEC_Context = ctx.as_mut_raw_ptr();
66     /// ```
as_mut_raw_ptr(&mut self) -> *mut raw::TEEC_Context67     pub fn as_mut_raw_ptr(&mut self) -> *mut raw::TEEC_Context {
68         &mut self.raw
69     }
70 
71     /// Opens a new session with the specified trusted application.
72     ///
73     /// The target trusted application is specified by `uuid`.
74     ///
75     /// # Examples
76     ///
77     /// ```
78     /// let mut ctx = Context::new().unwrap();
79     /// let uuid = Uuid::parse_str("8abcf200-2450-11e4-abe2-0002a5d5c51b").unwrap();
80     /// let session = ctx.open_session(uuid).unwrap();
81     /// ```
open_session(&mut self, uuid: Uuid) -> Result<Session>82     pub fn open_session(&mut self, uuid: Uuid) -> Result<Session> {
83         Session::new(
84             self,
85             uuid,
86             None::<&mut Operation<ParamNone, ParamNone, ParamNone, ParamNone>>,
87         )
88     }
89 
90     /// Opens a new session with the specified trusted application, pass some
91     /// parameters to TA by an operation.
92     ///
93     /// The target trusted application is specified by `uuid`.
94     ///
95     /// # Examples
96     ///
97     /// ```
98     /// let mut ctx = Context::new().unwrap();
99     /// let uuid = Uuid::parse_str("8abcf200-2450-11e4-abe2-0002a5d5c51b").unwrap();
100     /// let p0 = ParamValue(42, 0, ParamType::ValueInout);
101     /// let mut operation = Operation::new(0, p0, ParamNone, ParamNone, ParamNone);
102     /// let session = ctx.open_session_with_operation(uuid, operation).unwrap();
103     /// ```
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>104     pub fn open_session_with_operation<A: Param, B: Param, C: Param, D: Param>(
105         &mut self,
106         uuid: Uuid,
107         operation: &mut Operation<A, B, C, D>,
108     ) -> Result<Session> {
109         Session::new(self, uuid, Some(operation))
110     }
111 }
112 
113 impl Drop for Context {
drop(&mut self)114     fn drop(&mut self) {
115         unsafe {
116             raw::TEEC_FinalizeContext(&mut self.raw);
117         }
118     }
119 }
120