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 core::fmt;
19 use hex;
20 use optee_teec_sys as raw;
21 use uuid as uuid_crate;
22 use uuid_crate::parser::ParseError;
23 use uuid_crate::BytesError;
24 
25 /// A Universally Unique Resource Identifier (UUID) type as defined in RFC4122.
26 /// The value is used to identify a trusted application.
27 pub struct Uuid {
28     raw: raw::TEEC_UUID,
29 }
30 
31 impl Uuid {
32     /// Parses a Uuid from a string of hexadecimal digits with optional hyphens.
33     ///
34     /// # Examples
35     ///
36     /// ```
37     /// let uuid = Uuid::parse_str("8abcf200-2450-11e4-abe2-0002a5d5c51b").unwrap();
38     /// ```
parse_str(input: &str) -> Result<Uuid, ParseError>39     pub fn parse_str(input: &str) -> Result<Uuid, ParseError> {
40         let uuid = uuid_crate::Uuid::parse_str(input)?;
41         let (time_low, time_mid, time_hi_and_version, clock_seq_and_node) = uuid.as_fields();
42         Ok(Self::new_raw(
43             time_low,
44             time_mid,
45             time_hi_and_version,
46             *clock_seq_and_node,
47         ))
48     }
49 
50     /// Creates a `Uuid` using the supplied big-endian bytes.
51     ///
52     /// # Examples
53     ///
54     /// ```
55     /// let bytes: [u8; 16] = [70, 235, 208, 238, 14, 109, 67, 201, 185, 13, 204, 195, 90, 145, 63, 62,];
56     /// let uuid = Uuid::from_bytes(bytes);
57     /// ```
from_bytes(bytes: [u8; 16]) -> Uuid58     pub fn from_bytes(bytes: [u8; 16]) -> Uuid {
59         let uuid = uuid_crate::Uuid::from_bytes(bytes);
60         let (time_low, time_mid, time_hi_and_version, clock_seq_and_node) = uuid.as_fields();
61         Self::new_raw(time_low, time_mid, time_hi_and_version, *clock_seq_and_node)
62     }
63 
64     /// Creates a `Uuid` using a slice of supplied big-endian bytes.
65     ///
66     /// # Examples
67     ///
68     /// ```
69     /// let bytes: &[u8; 16] = &[70, 235, 208, 238, 14, 109, 67, 201, 185, 13, 204, 195, 90, 145, 63, 62,];
70     /// let uuid = Uuid::from_slice(bytes);
71     /// ```
from_slice(b: &[u8]) -> Result<Uuid, BytesError>72     pub fn from_slice(b: &[u8]) -> Result<Uuid, BytesError> {
73         let uuid = uuid_crate::Uuid::from_slice(b)?;
74         let (time_low, time_mid, time_hi_and_version, clock_seq_and_node) = uuid.as_fields();
75         Ok(Self::new_raw(
76             time_low,
77             time_mid,
78             time_hi_and_version,
79             *clock_seq_and_node,
80         ))
81     }
82 
83     /// Crates a raw TEE client uuid object with specified parameters.
new_raw( time_low: u32, time_mid: u16, time_hi_and_version: u16, clock_seq_and_nod: [u8; 8], ) -> Uuid84     pub fn new_raw(
85         time_low: u32,
86         time_mid: u16,
87         time_hi_and_version: u16,
88         clock_seq_and_nod: [u8; 8],
89     ) -> Uuid {
90         let raw_uuid = raw::TEEC_UUID {
91             timeLow: time_low,
92             timeMid: time_mid,
93             timeHiAndVersion: time_hi_and_version,
94             clockSeqAndNode: clock_seq_and_nod,
95         };
96         Self { raw: raw_uuid }
97     }
98 
99     /// Converts a uuid to a const raw `TEEC_UUID` pointer.
as_raw_ptr(&self) -> *const raw::TEEC_UUID100     pub fn as_raw_ptr(&self) -> *const raw::TEEC_UUID {
101         &self.raw
102     }
103 }
104 
105 impl fmt::Display for Uuid {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result106     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
107         write!(
108             f,
109             "{:x}-{:x}-{:x}-{}",
110             self.raw.timeLow,
111             self.raw.timeMid,
112             self.raw.timeHiAndVersion,
113             hex::encode(self.raw.clockSeqAndNode)
114         )
115     }
116 }
117