public_key_credential_params.cc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "device/fido/public_key_credential_params.h"
  5. #include <utility>
  6. #include "base/numerics/safe_conversions.h"
  7. namespace device {
  8. bool PublicKeyCredentialParams::CredentialInfo::operator==(
  9. const CredentialInfo& other) const {
  10. return type == other.type && algorithm == other.algorithm;
  11. }
  12. // static
  13. absl::optional<PublicKeyCredentialParams>
  14. PublicKeyCredentialParams::CreateFromCBORValue(const cbor::Value& cbor_value) {
  15. if (!cbor_value.is_array())
  16. return absl::nullopt;
  17. std::vector<PublicKeyCredentialParams::CredentialInfo> credential_params;
  18. for (const auto& credential : cbor_value.GetArray()) {
  19. if (!credential.is_map() || credential.GetMap().size() != 2)
  20. return absl::nullopt;
  21. const auto& credential_map = credential.GetMap();
  22. const auto credential_type_it =
  23. credential_map.find(cbor::Value(kCredentialTypeMapKey));
  24. const auto algorithm_type_it =
  25. credential_map.find(cbor::Value(kCredentialAlgorithmMapKey));
  26. if (credential_type_it == credential_map.end() ||
  27. !credential_type_it->second.is_string() ||
  28. credential_type_it->second.GetString() != kPublicKey ||
  29. algorithm_type_it == credential_map.end() ||
  30. !algorithm_type_it->second.is_integer() ||
  31. !base::IsValueInRangeForNumericType<int32_t>(
  32. algorithm_type_it->second.GetInteger())) {
  33. return absl::nullopt;
  34. }
  35. credential_params.push_back(PublicKeyCredentialParams::CredentialInfo{
  36. CredentialType::kPublicKey,
  37. static_cast<int32_t>(algorithm_type_it->second.GetInteger())});
  38. }
  39. return PublicKeyCredentialParams(std::move(credential_params));
  40. }
  41. PublicKeyCredentialParams::PublicKeyCredentialParams(
  42. std::vector<CredentialInfo> credential_params)
  43. : public_key_credential_params_(std::move(credential_params)) {}
  44. PublicKeyCredentialParams::PublicKeyCredentialParams(
  45. const PublicKeyCredentialParams& other) = default;
  46. PublicKeyCredentialParams::PublicKeyCredentialParams(
  47. PublicKeyCredentialParams&& other) = default;
  48. PublicKeyCredentialParams& PublicKeyCredentialParams::operator=(
  49. const PublicKeyCredentialParams& other) = default;
  50. PublicKeyCredentialParams& PublicKeyCredentialParams::operator=(
  51. PublicKeyCredentialParams&& other) = default;
  52. PublicKeyCredentialParams::~PublicKeyCredentialParams() = default;
  53. cbor::Value AsCBOR(const PublicKeyCredentialParams& params) {
  54. cbor::Value::ArrayValue credential_param_array;
  55. credential_param_array.reserve(params.public_key_credential_params().size());
  56. for (const auto& credential : params.public_key_credential_params()) {
  57. cbor::Value::MapValue cbor_credential_map;
  58. cbor_credential_map.emplace(kCredentialTypeMapKey,
  59. CredentialTypeToString(credential.type));
  60. cbor_credential_map.emplace(kCredentialAlgorithmMapKey,
  61. credential.algorithm);
  62. credential_param_array.emplace_back(std::move(cbor_credential_map));
  63. }
  64. return cbor::Value(std::move(credential_param_array));
  65. }
  66. } // namespace device