diff --git a/device/fido/authenticator_get_assertion_response.cc b/device/fido/authenticator_get_assertion_response.cc index 0cc45330036394..a02868d441375b 100644 --- a/device/fido/authenticator_get_assertion_response.cc +++ b/device/fido/authenticator_get_assertion_response.cc @@ -7,6 +7,8 @@ #include #include "base/optional.h" +#include "components/cbor/cbor_values.h" +#include "components/cbor/cbor_writer.h" #include "device/fido/authenticator_data.h" #include "device/fido/fido_parsing_utils.h" @@ -89,4 +91,24 @@ AuthenticatorGetAssertionResponse::SetNumCredentials(uint8_t num_credentials) { return *this; } +std::vector GetSerializedCtapDeviceResponse( + const AuthenticatorGetAssertionResponse& response) { + cbor::CBORValue::MapValue response_map; + if (response.credential()) + response_map.emplace(1, response.credential()->ConvertToCBOR()); + + response_map.emplace(2, response.auth_data().SerializeToByteArray()); + response_map.emplace(3, response.signature()); + + if (response.user_entity()) + response_map.emplace(4, response.user_entity()->ConvertToCBOR()); + + // Multiple account selection is not supported. + response_map.emplace(5, 1); + auto encoded_response = + cbor::CBORWriter::Write(cbor::CBORValue(std::move(response_map))); + DCHECK(encoded_response); + return *encoded_response; +} + } // namespace device diff --git a/device/fido/authenticator_get_assertion_response.h b/device/fido/authenticator_get_assertion_response.h index 4f60316b30c78a..72f492d80dec13 100644 --- a/device/fido/authenticator_get_assertion_response.h +++ b/device/fido/authenticator_get_assertion_response.h @@ -70,6 +70,10 @@ class COMPONENT_EXPORT(DEVICE_FIDO) AuthenticatorGetAssertionResponse DISALLOW_COPY_AND_ASSIGN(AuthenticatorGetAssertionResponse); }; +COMPONENT_EXPORT(DEVICE_FIDO) +std::vector GetSerializedCtapDeviceResponse( + const AuthenticatorGetAssertionResponse& response); + } // namespace device #endif // DEVICE_FIDO_AUTHENTICATOR_GET_ASSERTION_RESPONSE_H_ diff --git a/device/fido/ctap_response_unittest.cc b/device/fido/ctap_response_unittest.cc index 64f73fc8c6101f..2d8e23b544f4c8 100644 --- a/device/fido/ctap_response_unittest.cc +++ b/device/fido/ctap_response_unittest.cc @@ -586,4 +586,50 @@ TEST(CTAPResponseTest, TestSerializeMakeCredentialResponse) { base::make_span(test_data::kTestMakeCredentialResponse).subspan(1))); } +TEST(CTAPResponseTest, TestSerializeGetAssertionResponse) { + constexpr std::array kApplicationParameter = {{ + 0x62, 0x5d, 0xda, 0xdf, 0x74, 0x3f, 0x57, 0x27, 0xe6, 0x6b, 0xba, + 0x8c, 0x2e, 0x38, 0x79, 0x22, 0xd1, 0xaf, 0x43, 0xc5, 0x03, 0xd9, + 0x11, 0x4a, 0x8f, 0xba, 0x10, 0x4d, 0x84, 0xd0, 0x2b, 0xfa, + }}; + + constexpr uint8_t kUserId[] = { + 0x30, 0x82, 0x01, 0x93, 0x30, 0x82, 0x01, 0x38, 0xa0, 0x03, 0x02, + 0x01, 0x02, 0x30, 0x82, 0x01, 0x93, 0x30, 0x82, 0x01, 0x38, 0xa0, + 0x03, 0x02, 0x01, 0x02, 0x30, 0x82, 0x01, 0x93, 0x30, 0x82, + }; + + constexpr uint8_t kCredentialId[] = { + 0xf2, 0x20, 0x06, 0xde, 0x4f, 0x90, 0x5a, 0xf6, 0x8a, 0x43, 0x94, + 0x2f, 0x02, 0x4f, 0x2a, 0x5e, 0xce, 0x60, 0x3d, 0x9c, 0x6d, 0x4b, + 0x3d, 0xf8, 0xbe, 0x08, 0xed, 0x01, 0xfc, 0x44, 0x26, 0x46, 0xd0, + 0x34, 0x85, 0x8a, 0xc7, 0x5b, 0xed, 0x3f, 0xd5, 0x80, 0xbf, 0x98, + 0x08, 0xd9, 0x4f, 0xcb, 0xee, 0x82, 0xb9, 0xb2, 0xef, 0x66, 0x77, + 0xaf, 0x0a, 0xdc, 0xc3, 0x58, 0x52, 0xea, 0x6b, 0x9e, + }; + + AuthenticatorData authenticator_data( + kApplicationParameter, + base::strict_cast(AuthenticatorData::Flag::kTestOfUserPresence), + std::array{ + {0x00, 0x00, 0x00, 0x11}} /* signature_counter */, + base::nullopt /* attested_credential_data */); + AuthenticatorGetAssertionResponse response( + std::move(authenticator_data), + fido_parsing_utils::Materialize(test_data::kCtap2GetAssertionSignature)); + response.SetCredential({CredentialType::kPublicKey, + fido_parsing_utils::Materialize(kCredentialId)}); + PublicKeyCredentialUserEntity user(fido_parsing_utils::Materialize(kUserId)); + user.SetDisplayName("John P. Smith"); + user.SetUserName("johnpsmith@example.com"); + user.SetIconUrl(GURL("https://pics.acme.com/00/p/aBjjjpqPb.png")); + response.SetUserEntity(std::move(user)); + response.SetNumCredentials(1); + + EXPECT_THAT( + GetSerializedCtapDeviceResponse(response), + ::testing::ElementsAreArray( + base::make_span(test_data::kDeviceGetAssertionResponse).subspan(1))); +} + } // namespace device