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