Skip to content

Commit

Permalink
Add serialization logic for GetAssertion response
Browse files Browse the repository at this point in the history
Add serialization logic for GetAsserion response needed to implement
virtual CTAP2 device.

Bug: 829413
Change-Id: I144a48bd99be2fa09231d76718ecf97bb9ad75b4
Reviewed-on: https://chromium-review.googlesource.com/1114286
Commit-Queue: Jun Choi <hongjunchoi@chromium.org>
Reviewed-by: Jan Wilken Dörrie <jdoerrie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#570689}
  • Loading branch information
Jun Choi authored and Commit Bot committed Jun 27, 2018
1 parent 736aa40 commit 0080859
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
22 changes: 22 additions & 0 deletions device/fido/authenticator_get_assertion_response.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <utility>

#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"

Expand Down Expand Up @@ -89,4 +91,24 @@ AuthenticatorGetAssertionResponse::SetNumCredentials(uint8_t num_credentials) {
return *this;
}

std::vector<uint8_t> 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
4 changes: 4 additions & 0 deletions device/fido/authenticator_get_assertion_response.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class COMPONENT_EXPORT(DEVICE_FIDO) AuthenticatorGetAssertionResponse
DISALLOW_COPY_AND_ASSIGN(AuthenticatorGetAssertionResponse);
};

COMPONENT_EXPORT(DEVICE_FIDO)
std::vector<uint8_t> GetSerializedCtapDeviceResponse(
const AuthenticatorGetAssertionResponse& response);

} // namespace device

#endif // DEVICE_FIDO_AUTHENTICATOR_GET_ASSERTION_RESPONSE_H_
46 changes: 46 additions & 0 deletions device/fido/ctap_response_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -586,4 +586,50 @@ TEST(CTAPResponseTest, TestSerializeMakeCredentialResponse) {
base::make_span(test_data::kTestMakeCredentialResponse).subspan(1)));
}

TEST(CTAPResponseTest, TestSerializeGetAssertionResponse) {
constexpr std::array<uint8_t, kRpIdHashLength> 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<uint8_t>(AuthenticatorData::Flag::kTestOfUserPresence),
std::array<uint8_t, kSignCounterLength>{
{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

0 comments on commit 0080859

Please sign in to comment.