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 bool indicating whether the attributes of an asset are required to be encrypted. 143 RequireAttrEncrypted = DataType::Bool as isize | 0x47, 144 145 /// A tag whose value is a byte array indicating the group id an asset belongs to. 146 GroupId = DataType::Bytes as isize | 0x48, 147 148 /// A tag whose value is a 32-bit unsigned integer indicating the type of Asset encapsulation. 149 WrapType = DataType::Number as isize | 0x49, 150 151 /// A tag whose value is a 32-bit unsigned integer indicating the specific user id. 152 UserId = DataType::Number as isize | 0x100, 153 } 154 } 155 156 /// A type that indicates the secret or attribute value of an Asset tag. 157 #[derive(Clone)] 158 #[derive(Debug)] 159 #[derive(Eq, Hash, PartialEq)] 160 #[repr(C)] 161 pub enum Value { 162 /// Asset attribute value, whose data type is bool. 163 Bool(bool), 164 165 /// Asset attribute value, whose data type is number. 166 Number(u32), 167 168 /// Asset attribute value, whose data type is byte array. 169 Bytes(Vec<u8>), 170 } 171 172 impl Drop for Value { drop(&mut self)173 fn drop(&mut self) { 174 if let Value::Bytes(bytes) = self { 175 bytes.fill(0); 176 } 177 } 178 } 179 180 /// A Map type containing tag-value pairs that describe the attributes of an Asset. 181 pub type AssetMap = HashMap<Tag, Value>; 182 183 impl_enum_trait! { 184 /// An enum type containing the Asset error codes. 185 #[derive(Clone, Copy)] 186 #[derive(Debug)] 187 #[derive(Eq, Hash, PartialEq)] 188 pub enum ErrCode { 189 /// The error code indicates that the caller doesn't have the permission. 190 PermissionDenied = 201, 191 192 /// The error code indicates that the caller is not system application. 193 NotSystemApplication = 202, 194 195 /// The error code indicates that the argument is invalid. 196 InvalidArgument = 401, 197 198 /// The error code indicates that the ASSET service is unavailable. 199 ServiceUnavailable = 24000001, 200 201 /// The error code indicates that the queried Asset can not be found. 202 NotFound = 24000002, 203 204 /// The error code indicates that the Asset already exists. 205 Duplicated = 24000003, 206 207 /// The error code indicates that the access to Asset is denied. 208 AccessDenied = 24000004, 209 210 /// The error code indicates that the screen lock status mismatches. 211 StatusMismatch = 24000005, 212 213 /// The error code indicates insufficient memory. 214 OutOfMemory = 24000006, 215 216 /// The error code indicates that the Asset is corrupted. 217 DataCorrupted = 24000007, 218 219 /// The error code indicates that the database operation is failed. 220 DatabaseError = 24000008, 221 222 /// The error code indicates that the cryptography operation is failed. 223 CryptoError = 24000009, 224 225 /// The error code indicates that the ipc communication is abnormal. 226 IpcError = 24000010, 227 228 /// The error code indicates that the operation of calling Bundle Manager Service is failed. 229 BmsError = 24000011, 230 231 /// The error code indicates that the operation of calling OS Account Service is failed. 232 AccountError = 24000012, 233 234 /// The error code indicates that the operation of calling Access Token Service is failed. 235 AccessTokenError = 24000013, 236 237 /// The error code indicates that the operation of file is failed. 238 FileOperationError = 24000014, 239 240 /// The error code indicates that the operation of getting system time failed. 241 GetSystemTimeError = 24000015, 242 243 /// The error code indicates that the cache exceeds the limit. 244 LimitExceeded = 24000016, 245 246 /// The error code indicates that the capability is not supported. 247 Unsupported = 24000017, 248 } 249 } 250 251 /// A struct containing the Asset result code and error message. 252 #[derive(Debug)] 253 pub struct AssetError { 254 /// Error code for error occurred. 255 pub code: ErrCode, 256 257 /// Error message for error occurred. 258 pub msg: String, 259 } 260 261 /// Alias of the Asset result type. 262 pub type Result<T> = std::result::Result<T, AssetError>; 263 264 impl_enum_trait! { 265 /// An enum type indicates when the Asset is accessible. 266 #[repr(C)] 267 #[derive(Debug)] 268 #[derive(Clone, Copy)] 269 #[derive(PartialEq, Eq)] 270 #[derive(Default)] 271 pub enum Accessibility { 272 /// The secret value in the Asset can only be accessed after the device power on. 273 DevicePowerOn = 0, 274 275 /// The secret value in the Asset can only be accessed after the device is first unlocked. 276 #[default] 277 DeviceFirstUnlocked = 1, 278 279 /// The secret value in the Asset can only be accessed while the device is unlocked. 280 DeviceUnlocked = 2, 281 } 282 } 283 284 impl_enum_trait! { 285 /// An enum type indicates the user authentication type for Asset access control. 286 #[derive(Debug)] 287 #[derive(Clone, Copy)] 288 #[derive(PartialEq, Eq)] 289 #[derive(Default)] 290 pub enum AuthType { 291 /// The access to an Asset doesn't require user authentication. 292 #[default] 293 None = 0x00, 294 295 /// The access to an Asset requires user authentication using either PIN/pattern/password or biometric traits. 296 Any = 0xFF, 297 } 298 } 299 300 impl_enum_trait! { 301 /// An enum type indicates the type of Asset synchronization. 302 #[derive(Debug)] 303 #[derive(Clone, Copy)] 304 #[derive(PartialEq, Eq)] 305 #[derive(Default)] 306 pub enum SyncType { 307 /// An Asset with this attribute value is never allowed to be transferred out. 308 #[default] 309 Never = 0, 310 311 /// An Asset with this attribute value can only be restored to the device from which it was transferred out. 312 ThisDevice = 1 << 0, 313 314 /// An Asset with this attribute value can only be transferred out to a trusted device (user authorized). 315 TrustedDevice = 1 << 1, 316 317 /// An Asset with this attribute value can only be transferred out to a trusted device (user authorized). 318 TrustedAccount = 1 << 2, 319 } 320 } 321 322 impl_enum_trait! { 323 /// An enum type indicates the type of Asset synchronization. 324 #[derive(Debug)] 325 #[derive(Clone, Copy)] 326 #[derive(PartialEq, Eq)] 327 #[derive(Default)] 328 pub enum WrapType { 329 /// An Asset with this attribute value is never allowed to be wrapped up. 330 #[default] 331 Never = 0, 332 333 /// An Asset with this attribute value can only be wrapped or unwrapped on devices logged in with trusted accounts. 334 TrustedAccount = 1, 335 } 336 } 337 338 impl_enum_trait! { 339 /// An enum type indicates the strategy for conflict resolution when handling duplicated Asset alias. 340 #[derive(Default)] 341 pub enum ConflictResolution { 342 /// Directly overwrite an Asset with duplicated alias when a conflict is detected. 343 Overwrite = 0, 344 345 /// Throw an error so that the caller can take measures when a conflict is detected. 346 #[default] 347 ThrowError = 1, 348 } 349 } 350 351 impl_enum_trait! { 352 /// An enum type indicates the return type of the queried Asset. 353 #[derive(Debug)] 354 #[derive(Clone, Copy)] 355 #[derive(PartialEq, Eq)] 356 #[derive(Default)] 357 pub enum LocalStatus { 358 /// Specify that the return data should contain both secret value and attributes. 359 #[default] 360 Local = 0, 361 362 /// Specify that the return data contains only attributes. 363 Cloud = 1 << 0, 364 } 365 } 366 367 impl_enum_trait! { 368 /// An enum type indicates the return type of the queried Asset. 369 #[derive(Debug)] 370 #[derive(Clone, Copy)] 371 #[derive(PartialEq, Eq)] 372 #[derive(Default)] 373 pub enum SyncStatus { 374 /// Specify that the return data should contain both secret value and attributes. 375 #[default] 376 NoNeedSync = 0, 377 378 /// Specify that the return data contains only attributes. 379 SyncAdd = 1 << 0, 380 381 /// Specify that the return data contains only attributes. 382 SyncDel = 1 << 1, 383 384 /// Specify that the return data contains only attributes. 385 SyncUpdate = 1 << 2, 386 } 387 } 388 389 impl_enum_trait! { 390 /// An enum type indicates the return type of the queried Asset. 391 #[derive(Default)] 392 pub enum ReturnType { 393 /// Specify that the return data should contain both secret value and attributes. 394 All = 0, 395 396 /// Specify that the return data contains only attributes. 397 #[default] 398 Attributes = 1, 399 } 400 } 401 402 impl_enum_trait! { 403 /// An enum type indicates the return type of the queried Asset. 404 #[derive(Default)] 405 pub enum OperationType { 406 /// Trigger Sync. 407 #[default] 408 NeedSync = 0, 409 410 /// Logout to clean cloud flag. 411 NeedLogout = 1, 412 413 /// Delete cloud data. 414 NeedDeleteCloudData = 2, 415 } 416 } 417 418 /// Expended abililty for HashMap. 419 pub trait Extension<K> { 420 /// Insert an attribute into the collection. insert_attr(&mut self, key: K, value: impl Conversion)421 fn insert_attr(&mut self, key: K, value: impl Conversion); 422 423 /// Get an attribute of bool type from the collection. get_bool_attr(&self, key: &K) -> Result<bool>424 fn get_bool_attr(&self, key: &K) -> Result<bool>; 425 426 /// Get an attribute of enum type from the collection. get_enum_attr<T: TryFrom<u32, Error = AssetError>>(&self, key: &K) -> Result<T>427 fn get_enum_attr<T: TryFrom<u32, Error = AssetError>>(&self, key: &K) -> Result<T>; 428 429 /// Get an attribute of number type from the collection. get_num_attr(&self, key: &K) -> Result<u32>430 fn get_num_attr(&self, key: &K) -> Result<u32>; 431 432 /// Get an attribute of bytes type from the collection. get_bytes_attr(&self, key: &K) -> Result<&Vec<u8>>433 fn get_bytes_attr(&self, key: &K) -> Result<&Vec<u8>>; 434 } 435 436 /// Conversion between a specific type and the Asset Value type. 437 pub trait Conversion { 438 /// Get the data type of Asset Enum type. data_type(&self) -> DataType439 fn data_type(&self) -> DataType; 440 441 /// Convert the Asset Enum type to the Value variant. into_value(self) -> Value442 fn into_value(self) -> Value; 443 } 444