Use credential_id to correlate matched credential back to original

Upon succesfully dectypting an advertisements, the decoding library returns the id of the credential which was used to decrypt. We then use this id to correlate the decoded adv back to the original shared_credential which was used to decrypt it and save this and propagate it in the result returned to the caller

PiperOrigin-RevId: 646179759
This commit is contained in:
hai007
2024-06-24 12:16:34 -07:00
committed by Copybara-Service
parent 0ad19f3ce9
commit cdd6870af2
3 changed files with 41 additions and 7 deletions
@@ -89,6 +89,7 @@ TEST(AdvertisementDecoderImpl, DecodeEncryptedAdvertisement) {
public_credential.set_key_seed(seed.AsStringView());
public_credential.set_metadata_encryption_key_tag_v0(
known_mac.AsStringView());
public_credential.set_id(12345678);
absl::flat_hash_map<IdentityType, std::vector<internal::SharedCredential>>
credentials;
credentials[IdentityType::IDENTITY_TYPE_PRIVATE_GROUP].push_back(
@@ -98,6 +99,9 @@ TEST(AdvertisementDecoderImpl, DecodeEncryptedAdvertisement) {
absl::StatusOr<Advertisement> result =
decoder.DecodeAdvertisement(absl::HexStringToBytes(V0AdvEncryptedBytes));
ASSERT_OK(result);
EXPECT_EQ(result->public_credential.value().id(), public_credential.id());
EXPECT_EQ(result->public_credential.value().key_seed(),
public_credential.key_seed());
EXPECT_EQ(result->identity_type, IdentityType::IDENTITY_TYPE_PRIVATE_GROUP);
EXPECT_EQ(result->version, 0);
EXPECT_THAT(result->data_elements,
@@ -92,8 +92,22 @@ internal::IdentityType GetIdentityType(
}
}
absl::StatusOr<::nearby::internal::SharedCredential> FindById(
std::vector<::nearby::internal::SharedCredential> private_credentials,
uint64_t id) {
auto cred =
std::find_if(private_credentials.begin(), private_credentials.end(),
[&id](const auto& x) { return x.id() == id; });
if (cred == private_credentials.end()) {
return absl::NotFoundError("No credential found with id: " +
std::to_string(id));
}
return *cred;
}
absl::Status ProcessLegibleV0Adv(
nearby_protocol::LegibleDeserializedV0Advertisement legible_adv,
std::vector<::nearby::internal::SharedCredential> private_credentials,
Advertisement& advertisement) {
advertisement.identity_type = GetIdentityType(legible_adv.GetIdentityKind());
@@ -108,6 +122,10 @@ absl::Status ProcessLegibleV0Adv(
if (!cred_details.ok()) {
return cred_details.status();
}
advertisement.public_credential =
FindById(private_credentials, cred_details->cred_id);
// TODO(b/333126765): update salt to use unsigned char * to remove cast
std::string salt(reinterpret_cast<char const*>(cred_details->salt), 2);
advertisement.data_elements.push_back(DataElement(0x00, salt));
@@ -128,10 +146,13 @@ absl::Status ProcessLegibleV0Adv(
}
absl::Status ProcessV0Advertisement(
nearby_protocol::DeserializedV0Advertisement result, Advertisement& adv) {
nearby_protocol::DeserializedV0Advertisement result,
std::vector<::nearby::internal::SharedCredential> private_credentials,
Advertisement& adv) {
switch (result.GetKind()) {
case nearby_protocol::DeserializedV0AdvertisementKind::Legible:
return ProcessLegibleV0Adv(result.IntoLegible(), adv);
return ProcessLegibleV0Adv(result.IntoLegible(), private_credentials,
adv);
break;
case nearby_protocol::DeserializedV0AdvertisementKind::
NoMatchingCredentials: {
@@ -167,8 +188,9 @@ absl::StatusOr<Advertisement> AdvertisementDecoderImpl::DecodeAdvertisement(
}
case np_ffi::internal::DeserializeAdvertisementResultKind::V0: {
decoded_advertisement.version = 0;
auto result = ProcessV0Advertisement(deserialize_result.IntoV0(),
decoded_advertisement);
auto result =
ProcessV0Advertisement(deserialize_result.IntoV0(),
private_credentials_, decoded_advertisement);
if (!result.ok()) {
return result;
}
@@ -196,7 +218,8 @@ AdvertisementDecoderImpl::InitializeCredentialBook(
std::vector<uint8_t> metadata_bytes(
credential.encrypted_metadata_bytes_v0().begin(),
credential.encrypted_metadata_bytes_v0().end());
nearby_protocol::MatchedCredentialData matched_cred(0, metadata_bytes);
nearby_protocol::MatchedCredentialData matched_cred(credential.id(),
metadata_bytes);
auto key_seed = credential.key_seed();
std::array<uint8_t, 32> key_seed_array;
@@ -29,13 +29,19 @@ namespace presence {
// Implements the Rust backed parsing and decrypting of advertisement bytes
class AdvertisementDecoderImpl : public AdvertisementDecoder {
public:
AdvertisementDecoderImpl() : cred_book_(InitializeCredentialBook(nullptr)) {}
AdvertisementDecoderImpl()
: cred_book_(InitializeCredentialBook(nullptr)),
private_credentials_(
std::vector<::nearby::internal::SharedCredential>()) {}
explicit AdvertisementDecoderImpl(
absl::flat_hash_map<nearby::internal::IdentityType,
std::vector<internal::SharedCredential>>*
credentials_map)
: cred_book_(InitializeCredentialBook(credentials_map)) {}
: cred_book_(InitializeCredentialBook(credentials_map)),
private_credentials_(
(*credentials_map)
[internal::IdentityType::IDENTITY_TYPE_PRIVATE_GROUP]) {}
absl::StatusOr<Advertisement> DecodeAdvertisement(
absl::string_view advertisement) override;
@@ -46,6 +52,7 @@ class AdvertisementDecoderImpl : public AdvertisementDecoder {
std::vector<::nearby::internal::SharedCredential>>*
credentials_map);
nearby_protocol::CredentialBook cred_book_;
std::vector<::nearby::internal::SharedCredential> private_credentials_;
};
} // namespace presence