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