Skip to main content

optee_utee/
identity.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::Uuid;
19use num_enum::{FromPrimitive, IntoPrimitive};
20use optee_utee_sys as raw;
21
22#[derive(Copy, Clone)]
23pub struct Identity {
24    raw: raw::TEE_Identity,
25}
26
27impl Identity {
28    /// Returns the login type of this client identity.
29    ///
30    /// The GlobalPlatform specification reserves implementation-defined login ranges,
31    /// so a login value outside the set named below is possible in principle. Such a
32    /// value is returned as [`LoginType::Unknown`] carrying the raw code instead of
33    /// aborting the TA, so a TA inspecting its callers must decide explicitly what to
34    /// do with a login it does not recognize — normally by rejecting the caller.
35    pub fn login_type(&self) -> LoginType {
36        LoginType::from(self.raw.login)
37    }
38
39    pub fn uuid(&self) -> Uuid {
40        Uuid::from(self.raw.uuid)
41    }
42}
43
44impl From<raw::TEE_Identity> for Identity {
45    fn from(raw: raw::TEE_Identity) -> Self {
46        Self { raw }
47    }
48}
49
50/// The login type of a client, as stamped by OP-TEE into `TEE_Identity::login`.
51///
52/// This is the Rust-side mirror of the `TEE_LOGIN_*` constants defined in the C
53/// header. The `Unknown(u32)` variant catches any login value this crate does not
54/// name, so the value always round-trips back through [`IntoPrimitive`].
55#[derive(Clone, Copy, Debug, Eq, PartialEq, FromPrimitive, IntoPrimitive, strum::Display)]
56#[repr(u32)]
57pub enum LoginType {
58    Public = raw::TEE_LOGIN_PUBLIC,
59    User = raw::TEE_LOGIN_USER,
60    Group = raw::TEE_LOGIN_GROUP,
61    Application = raw::TEE_LOGIN_APPLICATION,
62    ApplicationUser = raw::TEE_LOGIN_APPLICATION_USER,
63    ApplicationGroup = raw::TEE_LOGIN_APPLICATION_GROUP,
64    TrustedApp = raw::TEE_LOGIN_TRUSTED_APP,
65    #[num_enum(catch_all)]
66    Unknown(u32),
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    fn identity_with_login(login: u32) -> Identity {
74        Identity::from(raw::TEE_Identity {
75            login,
76            uuid: raw::TEE_UUID {
77                timeLow: 0,
78                timeMid: 0,
79                timeHiAndVersion: 0,
80                clockSeqAndNode: [0; 8],
81            },
82        })
83    }
84
85    /// Guarantees: the login values named by the GlobalPlatform specification still
86    /// map to their named variants.
87    #[test]
88    fn known_login_types_still_map() {
89        let known = [
90            (raw::TEE_LOGIN_PUBLIC, LoginType::Public),
91            (raw::TEE_LOGIN_USER, LoginType::User),
92            (raw::TEE_LOGIN_GROUP, LoginType::Group),
93            (raw::TEE_LOGIN_APPLICATION, LoginType::Application),
94            (raw::TEE_LOGIN_APPLICATION_USER, LoginType::ApplicationUser),
95            (
96                raw::TEE_LOGIN_APPLICATION_GROUP,
97                LoginType::ApplicationGroup,
98            ),
99            (raw::TEE_LOGIN_TRUSTED_APP, LoginType::TrustedApp),
100        ];
101        for (raw_login, expected) in known {
102            assert_eq!(identity_with_login(raw_login).login_type(), expected);
103        }
104    }
105
106    /// Guarantees: reading a login value outside the named set yields `Unknown`
107    /// instead of panicking, and the raw value round-trips.
108    #[test]
109    fn unnamed_login_types_do_not_panic() {
110        for raw_login in [0x0000_0003, 0x0000_0300, 0x8000_0000, u32::MAX] {
111            let login_type = identity_with_login(raw_login).login_type();
112            assert_eq!(login_type, LoginType::Unknown(raw_login));
113            assert_eq!(u32::from(login_type), raw_login);
114        }
115    }
116}