1 /* 2 * Copyright (c) 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 #include "account_iam_info.h" 17 18 #include "account_log_wrapper.h" 19 20 namespace OHOS { 21 namespace AccountSA { 22 WriteRemoteAuthParam(Parcel & parcel) const23bool AuthParam::WriteRemoteAuthParam(Parcel& parcel) const 24 { 25 bool hasValue = (remoteAuthParam != std::nullopt); 26 if (!parcel.WriteBool(hasValue)) { 27 ACCOUNT_LOGE("Write RemoteAuthParam exist failed."); 28 return false; 29 } 30 if (!hasValue) { 31 return true; 32 } 33 hasValue = (remoteAuthParam.value().verifierNetworkId != std::nullopt); 34 if (!parcel.WriteBool(hasValue)) { 35 ACCOUNT_LOGE("Write verifierNetworkId exist failed."); 36 return false; 37 } 38 if (hasValue) { 39 if (!parcel.WriteString(remoteAuthParam.value().verifierNetworkId.value())) { 40 ACCOUNT_LOGE("Write verifierNetworkId failed."); 41 return false; 42 } 43 } 44 hasValue = (remoteAuthParam.value().collectorNetworkId != std::nullopt); 45 if (!parcel.WriteBool(hasValue)) { 46 ACCOUNT_LOGE("Write collectorNetworkId exist failed."); 47 return false; 48 } 49 if (hasValue) { 50 if (!parcel.WriteString(remoteAuthParam.value().collectorNetworkId.value())) { 51 ACCOUNT_LOGE("Write collectorNetworkId failed."); 52 return false; 53 } 54 } 55 hasValue = (remoteAuthParam.value().collectorTokenId != std::nullopt); 56 if (!parcel.WriteBool(hasValue)) { 57 ACCOUNT_LOGE("Write collectorTokenId exist failed."); 58 return false; 59 } 60 if (hasValue) { 61 if (!parcel.WriteUint32(remoteAuthParam.value().collectorTokenId.value())) { 62 ACCOUNT_LOGE("Write collectorTokenId failed."); 63 return false; 64 } 65 } 66 return true; 67 } 68 Marshalling(Parcel & parcel) const69bool AuthParam::Marshalling(Parcel& parcel) const 70 { 71 if (!parcel.WriteInt32(userId)) { 72 ACCOUNT_LOGE("Failed to write userId!"); 73 return false; 74 } 75 if (!parcel.WriteUInt8Vector(challenge)) { 76 ACCOUNT_LOGE("Failed to write challenge"); 77 return false; 78 } 79 if (!parcel.WriteInt32(authType)) { 80 ACCOUNT_LOGE("Failed to write authType"); 81 return false; 82 } 83 if (!parcel.WriteUint32(authTrustLevel)) { 84 ACCOUNT_LOGE("Failed to write authTrustLevel"); 85 return false; 86 } 87 if (!parcel.WriteInt32(static_cast<int32_t>(authIntent))) { 88 ACCOUNT_LOGE("Failed to write authIntent"); 89 return false; 90 } 91 if (!WriteRemoteAuthParam(parcel)) { 92 ACCOUNT_LOGE("Failed to write remoteAuthParam"); 93 return false; 94 } 95 return true; 96 } 97 Unmarshalling(Parcel & parcel)98AuthParam* AuthParam::Unmarshalling(Parcel& parcel) 99 { 100 AuthParam* info = new (std::nothrow) AuthParam(); 101 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) { 102 ACCOUNT_LOGW("Read from parcel failed"); 103 delete info; 104 info = nullptr; 105 } 106 return info; 107 } 108 ReadFromParcel(Parcel & parcel)109bool AuthParam::ReadFromParcel(Parcel& parcel) 110 { 111 if (!parcel.ReadInt32(userId)) { 112 ACCOUNT_LOGE("Failed to read userId"); 113 return false; 114 } 115 if (!parcel.ReadUInt8Vector(&challenge)) { 116 ACCOUNT_LOGE("Failed to read challenge"); 117 return false; 118 } 119 int32_t authTypeNum; 120 if (!parcel.ReadInt32(authTypeNum)) { 121 ACCOUNT_LOGE("Failed to read authType for AuthUser"); 122 return false; 123 } 124 authType = static_cast<AuthType>(authTypeNum); 125 uint32_t authTrustLevelNum; 126 if (!parcel.ReadUint32(authTrustLevelNum)) { 127 ACCOUNT_LOGE("Failed to read authTrustLevel for AuthUser"); 128 return false; 129 } 130 authTrustLevel = static_cast<AuthTrustLevel>(authTrustLevelNum); 131 int32_t authIntentNum = 0; 132 if (!parcel.ReadInt32(authIntentNum)) { 133 ACCOUNT_LOGE("Failed to read authIntent for AuthUser"); 134 return false; 135 } 136 authIntent = static_cast<AuthIntent>(authIntentNum); 137 if (!ReadRemoteAuthParam(parcel)) { 138 ACCOUNT_LOGE("Failed to read remoteAuthParam for AuthUser"); 139 return false; 140 } 141 return true; 142 } 143 ReadRemoteAuthParam(Parcel & parcel)144bool AuthParam::ReadRemoteAuthParam(Parcel& parcel) 145 { 146 bool hasValue = false; 147 if (!parcel.ReadBool(hasValue)) { 148 ACCOUNT_LOGE("Read RemoteAuthParam exist failed."); 149 return false; 150 } 151 if (!hasValue) { 152 return true; 153 } 154 remoteAuthParam = RemoteAuthParam(); 155 if (!parcel.ReadBool(hasValue)) { 156 ACCOUNT_LOGE("Read verifierNetworkId exist failed."); 157 return false; 158 } 159 if (hasValue) { 160 std::string networkId; 161 if (!parcel.ReadString(networkId)) { 162 ACCOUNT_LOGE("Read verifierNetworkId failed."); 163 return false; 164 } 165 remoteAuthParam.value().verifierNetworkId = networkId; 166 } 167 if (!parcel.ReadBool(hasValue)) { 168 ACCOUNT_LOGE("Read collectorNetworkId exist failed."); 169 return false; 170 } 171 if (hasValue) { 172 std::string collectorNetworkId; 173 if (!parcel.ReadString(collectorNetworkId)) { 174 ACCOUNT_LOGE("Read collectorNetworkId failed."); 175 return false; 176 } 177 remoteAuthParam.value().collectorNetworkId = collectorNetworkId; 178 } 179 if (!parcel.ReadBool(hasValue)) { 180 ACCOUNT_LOGE("Read collectorTokenId exist failed."); 181 return false; 182 } 183 if (hasValue) { 184 uint32_t tokenId; 185 if (!parcel.ReadUint32(tokenId)) { 186 ACCOUNT_LOGE("Read collectorTokenId failed."); 187 return false; 188 } 189 remoteAuthParam.value().collectorTokenId = tokenId; 190 } 191 return true; 192 } 193 Marshalling(Parcel & parcel) const194bool CredentialInfoIam::Marshalling(Parcel& parcel) const 195 { 196 if (!parcel.WriteUint64(credentialInfo.credentialId)) { 197 ACCOUNT_LOGE("Write credentialId fail"); 198 return false; 199 } 200 if (!parcel.WriteInt32(credentialInfo.authType)) { 201 ACCOUNT_LOGE("Write authType fail"); 202 return false; 203 } 204 PinSubType pinType = credentialInfo.pinType.value_or(PinSubType::PIN_MAX); 205 if (!parcel.WriteInt32(pinType)) { 206 ACCOUNT_LOGE("Write authSubType fail"); 207 return false; 208 } 209 if (!parcel.WriteUint64(credentialInfo.templateId)) { 210 ACCOUNT_LOGE("Write templateId fail"); 211 return false; 212 } 213 if (!parcel.WriteBool(credentialInfo.isAbandoned)) { 214 ACCOUNT_LOGE("Write isAbandoned fail"); 215 return false; 216 } 217 if (!parcel.WriteInt64(credentialInfo.validityPeriod)) { 218 ACCOUNT_LOGE("Write validityPeriod fail"); 219 return false; 220 } 221 return true; 222 } 223 Unmarshalling(Parcel & parcel)224CredentialInfoIam* CredentialInfoIam::Unmarshalling(Parcel& parcel) 225 { 226 CredentialInfoIam* info = new (std::nothrow) CredentialInfoIam(); 227 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) { 228 ACCOUNT_LOGW("Read from parcel failed"); 229 delete info; 230 info = nullptr; 231 } 232 return info; 233 } 234 ReadFromParcel(Parcel & parcel)235bool CredentialInfoIam::ReadFromParcel(Parcel& parcel) 236 { 237 int32_t authType = 0; 238 int32_t pinType = 0; 239 if (!parcel.ReadUint64(credentialInfo.credentialId)) { 240 ACCOUNT_LOGE("Failed to read credentialId"); 241 return false; 242 } 243 if (!parcel.ReadInt32(authType)) { 244 ACCOUNT_LOGE("Failed to read authType"); 245 return false; 246 } 247 if (!parcel.ReadInt32(pinType)) { 248 ACCOUNT_LOGE("Failed to read pinSubType"); 249 return false; 250 } 251 if (!parcel.ReadUint64(credentialInfo.templateId)) { 252 ACCOUNT_LOGE("Failed to read templateId"); 253 return false; 254 } 255 if (!parcel.ReadBool(credentialInfo.isAbandoned)) { 256 ACCOUNT_LOGE("Failed to read isAbandoned"); 257 return false; 258 } 259 if (!parcel.ReadInt64(credentialInfo.validityPeriod)) { 260 ACCOUNT_LOGE("Failed to read validityPeriod"); 261 return false; 262 } 263 credentialInfo.authType = static_cast<AuthType>(authType); 264 credentialInfo.pinType = static_cast<PinSubType>(pinType); 265 return true; 266 } 267 ConvertToCredentialInfoIamList(const std::vector<CredentialInfo> & infoList)268std::vector<CredentialInfoIam> ConvertToCredentialInfoIamList(const std::vector<CredentialInfo>& infoList) 269 { 270 std::vector<CredentialInfoIam> infoIamList; 271 for (const auto& item : infoList) { 272 CredentialInfoIam credentialInfoIam; 273 credentialInfoIam.credentialInfo = item; 274 infoIamList.emplace_back(credentialInfoIam); 275 } 276 return infoIamList; 277 } 278 ConvertToCredentialInfoList(const std::vector<CredentialInfoIam> & infoIamList)279std::vector<CredentialInfo> ConvertToCredentialInfoList(const std::vector<CredentialInfoIam>& infoIamList) 280 { 281 std::vector<CredentialInfo> infoList; 282 for (const auto& item : infoIamList) { 283 infoList.emplace_back(item.credentialInfo); 284 } 285 return infoList; 286 } 287 Marshalling(Parcel & parcel) const288bool CredentialParametersIam::Marshalling(Parcel& parcel) const 289 { 290 if (!parcel.WriteInt32(credentialParameters.authType)) { 291 ACCOUNT_LOGE("Failed to write authType"); 292 return false; 293 } 294 PinSubType pinType = credentialParameters.pinType.value_or(PinSubType::PIN_MAX); 295 if (!parcel.WriteInt32(pinType)) { 296 ACCOUNT_LOGE("Failed to write pinType"); 297 return false; 298 } 299 if (!parcel.WriteUInt8Vector(credentialParameters.token)) { 300 ACCOUNT_LOGE("Failed to write token"); 301 return false; 302 } 303 return true; 304 } 305 Unmarshalling(Parcel & parcel)306CredentialParametersIam* CredentialParametersIam::Unmarshalling(Parcel& parcel) 307 { 308 CredentialParametersIam* info = new (std::nothrow) CredentialParametersIam(); 309 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) { 310 ACCOUNT_LOGW("Read from parcel failed"); 311 delete info; 312 info = nullptr; 313 } 314 return info; 315 } 316 ReadFromParcel(Parcel & parcel)317bool CredentialParametersIam::ReadFromParcel(Parcel& parcel) 318 { 319 int32_t authType; 320 if (!parcel.ReadInt32(authType)) { 321 ACCOUNT_LOGE("Failed to read authType"); 322 return false; 323 } 324 int32_t authSubType; 325 if (!parcel.ReadInt32(authSubType)) { 326 ACCOUNT_LOGE("Failed to read authSubType"); 327 return false; 328 } 329 if (!parcel.ReadUInt8Vector(&credentialParameters.token)) { 330 ACCOUNT_LOGE("Failed to read token"); 331 return false; 332 } 333 credentialParameters.authType = static_cast<AuthType>(authType); 334 credentialParameters.pinType = static_cast<PinSubType>(authSubType); 335 return true; 336 } 337 Marshalling(Parcel & parcel) const338bool GetPropertyRequestIam::Marshalling(Parcel& parcel) const 339 { 340 if (!parcel.WriteInt32(getPropertyRequest.authType)) { 341 ACCOUNT_LOGE("Failed to write authType for GetProperty"); 342 return false; 343 } 344 std::vector<uint32_t> attrKeys; 345 std::transform(getPropertyRequest.keys.begin(), getPropertyRequest.keys.end(), std::back_inserter(attrKeys), 346 [](const auto& key) { return static_cast<uint32_t>(key); }); 347 348 if (!parcel.WriteUInt32Vector(attrKeys)) { 349 ACCOUNT_LOGE("Failed to write keys"); 350 return false; 351 } 352 return true; 353 } 354 Unmarshalling(Parcel & parcel)355GetPropertyRequestIam* GetPropertyRequestIam::Unmarshalling(Parcel& parcel) 356 { 357 GetPropertyRequestIam* info = new (std::nothrow) GetPropertyRequestIam(); 358 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) { 359 ACCOUNT_LOGW("Read from parcel failed"); 360 delete info; 361 info = nullptr; 362 } 363 return info; 364 } 365 ReadFromParcel(Parcel & parcel)366bool GetPropertyRequestIam::ReadFromParcel(Parcel& parcel) 367 { 368 int32_t authType; 369 if (!parcel.ReadInt32(authType)) { 370 ACCOUNT_LOGE("Failed to read authType"); 371 return false; 372 } 373 std::vector<uint32_t> keys; 374 if (!parcel.ReadUInt32Vector(&keys)) { 375 ACCOUNT_LOGE("Failed to read attribute keys"); 376 return false; 377 } 378 379 getPropertyRequest.authType = static_cast<AuthType>(authType); 380 for (auto& key : keys) { 381 getPropertyRequest.keys.push_back(static_cast<Attributes::AttributeKey>(key)); 382 } 383 384 return true; 385 } 386 Marshalling(Parcel & parcel) const387bool SetPropertyRequestIam::Marshalling(Parcel& parcel) const 388 { 389 if (!parcel.WriteInt32(setPropertyRequest.authType)) { 390 ACCOUNT_LOGE("Failed to write authType for SetProperty"); 391 return false; 392 } 393 auto buffer = setPropertyRequest.attrs.Serialize(); 394 if (!parcel.WriteUInt8Vector(buffer)) { 395 ACCOUNT_LOGE("Failed to write attributes"); 396 return false; 397 } 398 return true; 399 } 400 Unmarshalling(Parcel & parcel)401SetPropertyRequestIam* SetPropertyRequestIam::Unmarshalling(Parcel& parcel) 402 { 403 SetPropertyRequestIam* info = new (std::nothrow) SetPropertyRequestIam(); 404 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) { 405 ACCOUNT_LOGW("Read from parcel failed"); 406 delete info; 407 info = nullptr; 408 } 409 return info; 410 } 411 ReadFromParcel(Parcel & parcel)412bool SetPropertyRequestIam::ReadFromParcel(Parcel& parcel) 413 { 414 int32_t authType; 415 if (!parcel.ReadInt32(authType)) { 416 ACCOUNT_LOGE("Failed to read authType"); 417 return false; 418 } 419 420 std::vector<uint8_t> attr; 421 if (!parcel.ReadUInt8Vector(&attr)) { 422 ACCOUNT_LOGE("Failed to read attributes"); 423 return false; 424 } 425 426 setPropertyRequest.authType = static_cast<AuthType>(authType); 427 setPropertyRequest.attrs = Attributes(attr); 428 429 return true; 430 } 431 } // namespace AccountSA 432 } // namespace OHOS 433 434