1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 //! This module defines asset-related data structures. 17 18 use std::collections::HashMap; 19 20 mod extension; 21 #[macro_use] 22 pub mod macros; 23 24 impl_enum_trait! { 25 /// An enum type containing the data type definitions for Asset attribute value. 26 #[derive(Eq, PartialEq)] 27 pub enum DataType { 28 /// The data type of Asset attribute value is bool. 29 Bool = 1 << 28, 30 31 /// The data type of Asset attribute value is uint32. 32 Number = 2 << 28, 33 34 /// The data type of Asset attribute value is byte array. 35 Bytes = 3 << 28, 36 } 37 } 38 39 impl_tag_trait! { 40 /// An emum type that indicates the tag of the asset attribute. 41 #[derive(Clone, Copy)] 42 #[derive(Debug)] 43 #[derive(Eq, Hash, PartialEq)] 44 pub enum Tag { 45 /// A tag whose value is a byte array indicating the sensitive user data such as passwords and tokens. 46 Secret = DataType::Bytes as isize | 0x01, 47 48 /// A tag whose value is a byte array identifying an Asset. 49 Alias = DataType::Bytes as isize | 0x02, 50 51 /// A tag whose value is a 32-bit unsigned integer indicating when the Asset can be accessed. 52 Accessibility = DataType::Number as isize | 0x03, 53 54 /// A tag whose value is a bool indicating whether a screen lock password is set for the device. 55 RequirePasswordSet = DataType::Bool as isize | 0x04, 56 57 /// A tag whose value is a 32-bit unsigned integer indicating 58 /// the user authentication type for Asset access control. 59 AuthType = DataType::Number as isize | 0x05, 60 61 /// A tag whose value is a 32-bit unsigned integer indicating 62 /// the validity period in seconds of user authentication. 63 AuthValidityPeriod = DataType::Number as isize | 0x06, 64 65 /// A tag whose value is a byte array indicating the authentication challenge for anti-replay protection. 66 AuthChallenge = DataType::Bytes as isize | 0x07, 67 68 /// A tag whose value is a byte array indicating the authentication token after a user is verified. 69 AuthToken = DataType::Bytes as isize | 0x08, 70 71 /// A tag whose value is a 32-bit unsigned integer indicating the type of Asset synchronization. 72 SyncType = DataType::Number as isize | 0x10, 73 74 /// A tag whose value is a bool indicating whether Asset is stored persistently. 75 IsPersistent = DataType::Bool as isize | 0x11, 76 77 /// A tag whose value is a byte array indicating the first user-defined Asset data label (not allow to update). 78 DataLabelCritical1 = DataType::Bytes as isize | 0x20, 79 80 /// A tag whose value is a byte array indicating the second user-defined Asset data label (not allow to update). 81 DataLabelCritical2 = DataType::Bytes as isize | 0x21, 82 83 /// A tag whose value is a byte array indicating the third user-defined Asset data label (not allow to update). 84 DataLabelCritical3 = DataType::Bytes as isize | 0x22, 85 86 /// A tag whose value is a byte array indicating the fourth user-defined Asset data label (not allow to update). 87 DataLabelCritical4 = DataType::Bytes as isize | 0x23, 88 89 /// A tag whose value is a byte array indicating the first user-defined Asset data label (allow to update). 90 DataLabelNormal1 = DataType::Bytes as isize | 0x30, 91 92 /// A tag whose value is a byte array indicating the second user-defined Asset data label (allow to update). 93 DataLabelNormal2 = DataType::Bytes as isize | 0x31, 94 95 /// A tag whose value is a byte array indicating the third user-defined Asset data label (allow to update). 96 DataLabelNormal3 = DataType::Bytes as isize | 0x32, 97 98 /// A tag whose value is a byte array indicating the fourth user-defined Asset data label (allow to update). 99 DataLabelNormal4 = DataType::Bytes as isize | 0x33, 100 101 /// A local tag whose value is a byte array indicating 102 /// the first user-defined Asset data label (allow to update). 103 /// The information of a local tag will not be synchronized. 104 DataLabelNormalLocal1 = DataType::Bytes as isize | 0x34, 105 106 /// A local tag whose value is a byte array indicating 107 /// the second user-defined Asset data label (allow to update). 108 /// The information of a local tag will not be synchronized. 109 DataLabelNormalLocal2 = DataType::Bytes as isize | 0x35, 110 111 /// A local tag whose value is a byte array indicating 112 /// the third user-defined Asset data label (allow to update). 113 /// The information of a local tag will not be synchronized. 114 DataLabelNormalLocal3 = DataType::Bytes as isize | 0x36, 115 116 /// A local tag whose value is a byte array indicating 117 /// the fourth user-defined Asset data label (allow to update). 118 /// The information of a local tag will not be synchronized. 119 DataLabelNormalLocal4 = DataType::Bytes as isize | 0x37, 120 121 /// A tag whose value is a 32-bit unsigned integer indicating the return type of the queried Asset. 122 ReturnType = DataType::Number as isize | 0x40, 123 124 /// A tag whose value is a 32-bit unsigned integer indicating the maximum number of returned Assets in a query. 125 ReturnLimit = DataType::Number as isize | 0x41, 126 127 /// A tag whose value is a 32-bit unsigned integer indicating the offset of return data in batch query. 128 ReturnOffset = DataType::Number as isize | 0x42, 129 130 /// A tag whose value is a 32-bit unsigned integer indicating how the query results are sorted. 131 ReturnOrderedBy = DataType::Number as isize | 0x43, 132 133 /// A tag whose value is a 32-bit unsigned integer indicating the strategy for resolving Asset conflicts. 134 ConflictResolution = DataType::Number as isize | 0x44, 135 136 /// A tag whose value is a byte array indicating the update time of an Asset. 137 UpdateTime = DataType::Bytes as isize | 0x45, 138 139 /// A tag whose value is a byte array indicating the update time of an Asset. 140 OperationType = DataType::Number as isize | 0x46, 141 142 /// A tag whose value is a 32-bit unsigned integer indicating the specific user id. 143 UserId = DataType::Number as isize | 0x47, 144 } 145 } 146 147 /// A type that indicates the secret or attribute value of an Asset tag. 148 #[derive(Clone)] 149 #[derive(Debug)] 150 #[derive(Eq, Hash, PartialEq)] 151 #[repr(C)] 152 pub enum Value { 153 /// Asset attribute value, whose data type is bool. 154 Bool(bool), 155 156 /// Asset attribute value, whose data type is number. 157 Number(u32), 158 159 /// Asset attribute value, whose data type is byte array. 160 Bytes(Vec<u8>), 161 } 162 163 impl Drop for Value { drop(&mut self)164 fn drop(&mut self) { 165 if let Value::Bytes(bytes) = self { 166 bytes.fill(0); 167 } 168 } 169 } 170 171 /// A Map type containing tag-value pairs that describe the attributes of an Asset. 172 pub type AssetMap = HashMap<Tag, Value>; 173 174 impl_enum_trait! { 175 /// An enum type containing the Asset error codes. 176 #[derive(Clone, Copy)] 177 #[derive(Debug)] 178 #[derive(Eq, Hash, PartialEq)] 179 pub enum ErrCode { 180 /// The error code indicates that the caller doesn't have the permission. 181 PermissionDenied = 201, 182 183 /// The error code indicates that the caller is not system application. 184 NotSystemApplication = 202, 185 186 /// The error code indicates that the argument is invalid. 187 InvalidArgument = 401, 188 189 /// The error code indicates that the ASSET service is unavailable. 190 ServiceUnavailable = 24000001, 191 192 /// The error code indicates that the queried Asset can not be found. 193 NotFound = 24000002, 194 195 /// The error code indicates that the Asset already exists. 196 Duplicated = 24000003, 197 198 /// The error code indicates that the access to Asset is denied. 199 AccessDenied = 24000004, 200 201 /// The error code indicates that the screen lock status mismatches. 202 StatusMismatch = 24000005, 203 204 /// The error code indicates insufficient memory. 205 OutOfMemory = 24000006, 206 207 /// The error code indicates that the Asset is corrupted. 208 DataCorrupted = 24000007, 209 210 /// The error code indicates that the database operation is failed. 211 DatabaseError = 24000008, 212 213 /// The error code indicates that the cryptography operation is failed. 214 CryptoError = 24000009, 215 216 /// The error code indicates that the ipc communication is abnormal. 217 IpcError = 24000010, 218 219 /// The error code indicates that the operation of calling Bundle Manager Service is failed. 220 BmsError = 24000011, 221 222 /// The error code indicates that the operation of calling OS Account Service is failed. 223 AccountError = 24000012, 224 225 /// The error code indicates that the operation of calling Access Token Service is failed. 226 AccessTokenError = 24000013, 227 228 /// The error code indicates that the operation of file is failed. 229 FileOperationError = 24000014, 230 231 /// The error code indicates that the operation of getting system time failed. 232 GetSystemTimeError = 24000015, 233 234 /// The error code indicates that the cache exceeds the limit. 235 LimitExceeded = 24000016, 236 237 /// The error code indicates that the capability is not supported. 238 Unsupported = 24000017, 239 } 240 } 241 242 /// A struct containing the Asset result code and error message. 243 #[derive(Debug)] 244 pub struct AssetError { 245 /// Error code for error occurred. 246 pub code: ErrCode, 247 248 /// Error message for error occurred. 249 pub msg: String, 250 } 251 252 /// Alias of the Asset result type. 253 pub type Result<T> = std::result::Result<T, AssetError>; 254 255 impl_enum_trait! { 256 /// An enum type indicates when the Asset is accessible. 257 #[repr(C)] 258 #[derive(Debug)] 259 #[derive(Clone, Copy)] 260 #[derive(PartialEq, Eq)] 261 #[derive(Default)] 262 pub enum Accessibility { 263 /// The secret value in the Asset can only be accessed after the device power on. 264 DevicePowerOn = 0, 265 266 /// The secret value in the Asset can only be accessed after the device is first unlocked. 267 #[default] 268 DeviceFirstUnlocked = 1, 269 270 /// The secret value in the Asset can only be accessed while the device is unlocked. 271 DeviceUnlocked = 2, 272 } 273 } 274 275 impl_enum_trait! { 276 /// An enum type indicates the user authentication type for Asset access control. 277 #[derive(Debug)] 278 #[derive(Clone, Copy)] 279 #[derive(PartialEq, Eq)] 280 #[derive(Default)] 281 pub enum AuthType { 282 /// The access to an Asset doesn't require user authentication. 283 #[default] 284 None = 0x00, 285 286 /// The access to an Asset requires user authentication using either PIN/pattern/password or biometric traits. 287 Any = 0xFF, 288 } 289 } 290 291 impl_enum_trait! { 292 /// An enum type indicates the type of Asset synchronization. 293 #[derive(Debug)] 294 #[derive(Clone, Copy)] 295 #[derive(PartialEq, Eq)] 296 #[derive(Default)] 297 pub enum SyncType { 298 /// An Asset with this attribute value is never allowed to be transferred out. 299 #[default] 300 Never = 0, 301 302 /// An Asset with this attribute value can only be restored to the device from which it was transferred out. 303 ThisDevice = 1 << 0, 304 305 /// An Asset with this attribute value can only be transferred out to a trusted device (user authorized). 306 TrustedDevice = 1 << 1, 307 308 /// An Asset with this attribute value can only be transferred out to a trusted device (user authorized). 309 TrustedAccount = 1 << 2, 310 } 311 } 312 313 impl_enum_trait! { 314 /// An enum type indicates the strategy for conflict resolution when handling duplicated Asset alias. 315 #[derive(Default)] 316 pub enum ConflictResolution { 317 /// Directly overwrite an Asset with duplicated alias when a conflict is detected. 318 Overwrite = 0, 319 320 /// Throw an error so that the caller can take measures when a conflict is detected. 321 #[default] 322 ThrowError = 1, 323 } 324 } 325 326 impl_enum_trait! { 327 /// An enum type indicates the return type of the queried Asset. 328 #[derive(Debug)] 329 #[derive(Clone, Copy)] 330 #[derive(PartialEq, Eq)] 331 #[derive(Default)] 332 pub enum LocalStatus { 333 /// Specify that the return data should contain both secret value and attributes. 334 #[default] 335 Local = 0, 336 337 /// Specify that the return data contains only attributes. 338 Cloud = 1 << 0, 339 } 340 } 341 342 impl_enum_trait! { 343 /// An enum type indicates the return type of the queried Asset. 344 #[derive(Debug)] 345 #[derive(Clone, Copy)] 346 #[derive(PartialEq, Eq)] 347 #[derive(Default)] 348 pub enum SyncStatus { 349 /// Specify that the return data should contain both secret value and attributes. 350 #[default] 351 NoNeedSync = 0, 352 353 /// Specify that the return data contains only attributes. 354 SyncAdd = 1 << 0, 355 356 /// Specify that the return data contains only attributes. 357 SyncDel = 1 << 1, 358 359 /// Specify that the return data contains only attributes. 360 SyncUpdate = 1 << 2, 361 } 362 } 363 364 impl_enum_trait! { 365 /// An enum type indicates the return type of the queried Asset. 366 #[derive(Default)] 367 pub enum ReturnType { 368 /// Specify that the return data should contain both secret value and attributes. 369 All = 0, 370 371 /// Specify that the return data contains only attributes. 372 #[default] 373 Attributes = 1, 374 } 375 } 376 377 impl_enum_trait! { 378 /// An enum type indicates the return type of the queried Asset. 379 #[derive(Default)] 380 pub enum OperationType { 381 /// Trigger Sync. 382 #[default] 383 NeedSync = 0, 384 385 /// Logout to clean cloud flag. 386 NeedLogout = 1, 387 388 /// Delete cloud data. 389 NeedDeleteCloudData = 2, 390 } 391 } 392 393 /// Expended abililty for HashMap. 394 pub trait Extension<K> { 395 /// Insert an attribute into the collection. insert_attr(&mut self, key: K, value: impl Conversion)396 fn insert_attr(&mut self, key: K, value: impl Conversion); 397 398 /// Get an attribute of bool type from the collection. get_bool_attr(&self, key: &K) -> Result<bool>399 fn get_bool_attr(&self, key: &K) -> Result<bool>; 400 401 /// Get an attribute of enum type from the collection. get_enum_attr<T: TryFrom<u32, Error = AssetError>>(&self, key: &K) -> Result<T>402 fn get_enum_attr<T: TryFrom<u32, Error = AssetError>>(&self, key: &K) -> Result<T>; 403 404 /// Get an attribute of number type from the collection. get_num_attr(&self, key: &K) -> Result<u32>405 fn get_num_attr(&self, key: &K) -> Result<u32>; 406 407 /// Get an attribute of bytes type from the collection. get_bytes_attr(&self, key: &K) -> Result<&Vec<u8>>408 fn get_bytes_attr(&self, key: &K) -> Result<&Vec<u8>>; 409 } 410 411 /// Conversion between a specific type and the Asset Value type. 412 pub trait Conversion { 413 /// Get the data type of Asset Enum type. data_type(&self) -> DataType414 fn data_type(&self) -> DataType; 415 416 /// Convert the Asset Enum type to the Value variant. into_value(self) -> Value417 fn into_value(self) -> Value; 418 } 419