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