From cdd6870af2c4cb6f2b2be747ce872c81840a3d49 Mon Sep 17 00:00:00 2001 From: hai007 Date: Mon, 24 Jun 2024 12:14:07 -0700 Subject: [PATCH] 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 --- .../advertisement_decoder_new_format_test.cc | 4 +++ .../advertisement_decoder_rust_impl.cc | 33 ++++++++++++++++--- .../advertisement_decoder_rust_impl.h | 11 +++++-- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/presence/implementation/advertisement_decoder_new_format_test.cc b/presence/implementation/advertisement_decoder_new_format_test.cc index 6225111a..ef78c828 100644 --- a/presence/implementation/advertisement_decoder_new_format_test.cc +++ b/presence/implementation/advertisement_decoder_new_format_test.cc @@ -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> credentials; credentials[IdentityType::IDENTITY_TYPE_PRIVATE_GROUP].push_back( @@ -98,6 +99,9 @@ TEST(AdvertisementDecoderImpl, DecodeEncryptedAdvertisement) { absl::StatusOr 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, diff --git a/presence/implementation/advertisement_decoder_rust_impl.cc b/presence/implementation/advertisement_decoder_rust_impl.cc index cbe2b5f7..0f7227b3 100644 --- a/presence/implementation/advertisement_decoder_rust_impl.cc +++ b/presence/implementation/advertisement_decoder_rust_impl.cc @@ -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(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 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 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 key_seed_array; diff --git a/presence/implementation/advertisement_decoder_rust_impl.h b/presence/implementation/advertisement_decoder_rust_impl.h index 9599b4a6..b2444e7d 100644 --- a/presence/implementation/advertisement_decoder_rust_impl.h +++ b/presence/implementation/advertisement_decoder_rust_impl.h @@ -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>* credentials_map) - : cred_book_(InitializeCredentialBook(credentials_map)) {} + : cred_book_(InitializeCredentialBook(credentials_map)), + private_credentials_( + (*credentials_map) + [internal::IdentityType::IDENTITY_TYPE_PRIVATE_GROUP]) {} absl::StatusOr 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