optee_teec/context.rs
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
18use crate::{
19 ConnectionMethods, Operation, Param, ParamNone, Result, Session, Uuid, raw, teec_check,
20};
21use std::{
22 ptr,
23 sync::{Arc, Mutex},
24};
25
26pub struct InnerContext(pub raw::TEEC_Context);
27
28impl Drop for InnerContext {
29 fn drop(&mut self) {
30 unsafe {
31 raw::TEEC_FinalizeContext(&mut self.0);
32 }
33 }
34}
35
36/// An abstraction of the logical connection between a client application and a
37/// TEE.
38pub struct Context {
39 // Sessions keep an `Arc` clone so the OP-TEE context outlives them. The
40 // mutex serializes access to the shared handle and makes it `Send + Sync`:
41 // `raw::TEEC_Context` contains only scalars, so `Arc<Mutex<InnerContext>>`
42 // gets both traits automatically, without any `unsafe impl`.
43 raw: Arc<Mutex<InnerContext>>,
44}
45
46impl Context {
47 /// Creates a TEE client context object.
48 ///
49 /// # Examples
50 ///
51 /// ``` no_run
52 /// # use optee_teec::Context;
53 /// # fn main() -> optee_teec::Result<()> {
54 /// let mut ctx = Context::new()?;
55 /// # Ok(())
56 /// # }
57 /// ```
58 pub fn new() -> Result<Context> {
59 // SAFETY:
60 // raw_ctx is a C struct(TEEC_Context), which zero value is valid.
61 let mut raw_ctx = unsafe { std::mem::zeroed() };
62 teec_check(unsafe { raw::TEEC_InitializeContext(ptr::null_mut(), &mut raw_ctx) })?;
63 Ok(Self {
64 raw: Arc::new(Mutex::new(InnerContext(raw_ctx))),
65 })
66 }
67
68 /// Opens a new session with the specified trusted application.
69 ///
70 /// The target trusted application is specified by `uuid`.
71 ///
72 /// # Examples
73 ///
74 /// ``` no_run
75 /// use optee_teec::{Context, ErrorKind, Uuid};
76 ///
77 /// fn main() -> optee_teec::Result<()> {
78 /// let mut ctx = Context::new()?;
79 /// let uuid = Uuid::parse_str("8abcf200-2450-11e4-abe2-0002a5d5c51b").map_err(|err| {
80 /// println!("bad uuid: {:?}", err);
81 /// ErrorKind::BadParameters
82 /// })?;
83 /// let session = ctx.open_session(uuid)?;
84 /// Ok(())
85 /// }
86 /// ```
87 pub fn open_session(&mut self, uuid: Uuid) -> Result<Session> {
88 self.open_session_with_login(uuid, ConnectionMethods::LoginPublic)
89 }
90
91 pub fn open_session_with_login(
92 &mut self,
93 uuid: Uuid,
94 login: ConnectionMethods,
95 ) -> Result<Session> {
96 Session::new(
97 self,
98 uuid,
99 login,
100 None::<&mut Operation<ParamNone, ParamNone, ParamNone, ParamNone>>,
101 )
102 }
103
104 /// Opens a new session with the specified trusted application, pass some
105 /// parameters to TA by an operation.
106 ///
107 /// The target trusted application is specified by `uuid`.
108 ///
109 /// # Examples
110 ///
111 /// ``` no_run
112 /// use optee_teec::{Context, ErrorKind, Operation, ParamNone, ParamType, ParamValue, Uuid};
113 ///
114 /// fn main() -> optee_teec::Result<()> {
115 /// let mut ctx = Context::new()?;
116 /// let uuid = Uuid::parse_str("8abcf200-2450-11e4-abe2-0002a5d5c51b").map_err(|err| {
117 /// println!("bad uuid: {:?}", err);
118 /// ErrorKind::BadParameters
119 /// })?;
120 /// let p0 = ParamValue::new(42, 0, ParamType::ValueInout);
121 /// let mut operation = Operation::new(0, p0, ParamNone, ParamNone, ParamNone);
122 /// let session = ctx.open_session_with_operation(uuid, &mut operation)?;
123 /// Ok(())
124 /// }
125 /// ```
126 pub fn open_session_with_operation<A: Param, B: Param, C: Param, D: Param>(
127 &mut self,
128 uuid: Uuid,
129 operation: &mut Operation<A, B, C, D>,
130 ) -> Result<Session> {
131 Session::new(self, uuid, ConnectionMethods::LoginPublic, Some(operation))
132 }
133}
134
135// Internal usage only
136impl Context {
137 // anyone who wants to access the inner_context must take this as mut.
138 pub(crate) fn inner_context(&mut self) -> Arc<Mutex<InnerContext>> {
139 self.raw.clone()
140 }
141}