u2f_command_constructor_unittest.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2018 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/u2f_command_constructor.h"
  5. #include <utility>
  6. #include "device/fido/ctap_get_assertion_request.h"
  7. #include "device/fido/ctap_make_credential_request.h"
  8. #include "device/fido/fido_constants.h"
  9. #include "device/fido/fido_parsing_utils.h"
  10. #include "device/fido/fido_test_data.h"
  11. #include "testing/gmock/include/gmock/gmock.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. namespace device {
  14. namespace {
  15. CtapMakeCredentialRequest ConstructMakeCredentialRequest() {
  16. PublicKeyCredentialRpEntity rp("acme.com");
  17. rp.name = "acme.com";
  18. PublicKeyCredentialUserEntity user(
  19. fido_parsing_utils::Materialize(test_data::kUserId));
  20. user.name = "johnpsmith@example.com";
  21. user.display_name = "John P. Smith";
  22. user.icon_url = GURL("https://pics.acme.com/00/p/aBjjjpqPb.png");
  23. return CtapMakeCredentialRequest(
  24. test_data::kClientDataJson, std::move(rp), std::move(user),
  25. PublicKeyCredentialParams(PublicKeyCredentialParams(
  26. std::vector<PublicKeyCredentialParams::CredentialInfo>(1))));
  27. }
  28. CtapGetAssertionRequest ConstructGetAssertionRequest() {
  29. return CtapGetAssertionRequest("acme.com", test_data::kClientDataJson);
  30. }
  31. } // namespace
  32. TEST(U2fCommandConstructorTest, TestCreateU2fRegisterCommand) {
  33. const auto register_command_without_individual_attestation =
  34. ConstructU2fRegisterCommand(test_data::kApplicationParameter,
  35. test_data::kChallengeParameter,
  36. false /* is_individual_attestation */);
  37. EXPECT_THAT(register_command_without_individual_attestation,
  38. ::testing::ElementsAreArray(test_data::kU2fRegisterCommandApdu));
  39. const auto register_command_with_individual_attestation =
  40. ConstructU2fRegisterCommand(test_data::kApplicationParameter,
  41. test_data::kChallengeParameter,
  42. true /* is_individual_attestation */);
  43. EXPECT_THAT(register_command_with_individual_attestation,
  44. ::testing::ElementsAreArray(
  45. test_data::kU2fRegisterCommandApduWithIndividualAttestation));
  46. }
  47. TEST(U2fCommandConstructorTest, TestConvertCtapMakeCredentialToU2fRegister) {
  48. const auto make_credential_param = ConstructMakeCredentialRequest();
  49. EXPECT_TRUE(IsConvertibleToU2fRegisterCommand(make_credential_param));
  50. const auto u2f_register_command =
  51. ConvertToU2fRegisterCommand(make_credential_param);
  52. ASSERT_TRUE(u2f_register_command);
  53. EXPECT_THAT(*u2f_register_command,
  54. ::testing::ElementsAreArray(test_data::kU2fRegisterCommandApdu));
  55. }
  56. TEST(U2fCommandConstructorTest, TestU2fRegisterCredentialAlgorithmRequirement) {
  57. PublicKeyCredentialRpEntity rp("acme.com");
  58. rp.name = "acme.com";
  59. PublicKeyCredentialUserEntity user(
  60. fido_parsing_utils::Materialize(test_data::kUserId));
  61. user.name = "johnpsmith@example.com";
  62. user.display_name = "John P. Smith";
  63. user.icon_url = GURL("https://pics.acme.com/00/p/aBjjjpqPb.png");
  64. CtapMakeCredentialRequest make_credential_param(
  65. test_data::kClientDataJson, std::move(rp), std::move(user),
  66. PublicKeyCredentialParams({{CredentialType::kPublicKey, -257}}));
  67. EXPECT_FALSE(IsConvertibleToU2fRegisterCommand(make_credential_param));
  68. }
  69. TEST(U2fCommandConstructorTest, TestU2fRegisterUserVerificationRequirement) {
  70. auto make_credential_param = ConstructMakeCredentialRequest();
  71. make_credential_param.user_verification =
  72. UserVerificationRequirement::kRequired;
  73. EXPECT_FALSE(IsConvertibleToU2fRegisterCommand(make_credential_param));
  74. }
  75. TEST(U2fCommandConstructorTest, TestU2fRegisterResidentKeyRequirement) {
  76. auto make_credential_param = ConstructMakeCredentialRequest();
  77. make_credential_param.resident_key_required = true;
  78. EXPECT_FALSE(IsConvertibleToU2fRegisterCommand(make_credential_param));
  79. }
  80. TEST(U2fCommandConstructorTest, TestCreateSignApduCommand) {
  81. const auto& encoded_sign = ConstructU2fSignCommand(
  82. test_data::kApplicationParameter, test_data::kChallengeParameter,
  83. test_data::kU2fSignKeyHandle);
  84. ASSERT_TRUE(encoded_sign);
  85. EXPECT_THAT(*encoded_sign,
  86. ::testing::ElementsAreArray(test_data::kU2fSignCommandApdu));
  87. }
  88. TEST(U2fCommandConstructorTest, TestConvertCtapGetAssertionToU2fSignRequest) {
  89. auto get_assertion_req = ConstructGetAssertionRequest();
  90. std::vector<PublicKeyCredentialDescriptor> allowed_list;
  91. allowed_list.push_back(PublicKeyCredentialDescriptor(
  92. CredentialType::kPublicKey,
  93. fido_parsing_utils::Materialize(test_data::kU2fSignKeyHandle)));
  94. get_assertion_req.allow_list = std::move(allowed_list);
  95. const auto u2f_sign_command = ConvertToU2fSignCommand(
  96. get_assertion_req, ApplicationParameterType::kPrimary,
  97. fido_parsing_utils::Materialize(test_data::kU2fSignKeyHandle));
  98. EXPECT_TRUE(IsConvertibleToU2fSignCommand(get_assertion_req));
  99. ASSERT_TRUE(u2f_sign_command);
  100. EXPECT_THAT(*u2f_sign_command,
  101. ::testing::ElementsAreArray(test_data::kU2fSignCommandApdu));
  102. }
  103. TEST(U2fCommandConstructorTest, TestU2fSignAllowListRequirement) {
  104. auto get_assertion_req = ConstructGetAssertionRequest();
  105. EXPECT_FALSE(IsConvertibleToU2fSignCommand(get_assertion_req));
  106. }
  107. TEST(U2fCommandConstructorTest, TestU2fSignUserVerificationRequirement) {
  108. auto get_assertion_req = ConstructGetAssertionRequest();
  109. std::vector<PublicKeyCredentialDescriptor> allowed_list;
  110. allowed_list.push_back(PublicKeyCredentialDescriptor(
  111. CredentialType::kPublicKey,
  112. fido_parsing_utils::Materialize(test_data::kU2fSignKeyHandle)));
  113. get_assertion_req.allow_list = std::move(allowed_list);
  114. get_assertion_req.user_verification = UserVerificationRequirement::kRequired;
  115. EXPECT_FALSE(IsConvertibleToU2fSignCommand(get_assertion_req));
  116. }
  117. TEST(U2fCommandConstructorTest, TestCreateSignWithIncorrectKeyHandle) {
  118. std::array<uint8_t, kU2fApplicationParamLength> application_parameter = {
  119. 0x01};
  120. std::array<uint8_t, kU2fChallengeParamLength> challenge_parameter = {0x02};
  121. std::vector<uint8_t> key_handle(kMaxKeyHandleLength, 0xff);
  122. const auto valid_sign_command = ConstructU2fSignCommand(
  123. application_parameter, challenge_parameter, key_handle);
  124. ASSERT_TRUE(valid_sign_command);
  125. key_handle.push_back(0xff);
  126. const auto invalid_sign_command = ConstructU2fSignCommand(
  127. application_parameter, challenge_parameter, key_handle);
  128. ASSERT_FALSE(invalid_sign_command);
  129. }
  130. } // namespace device