Change private/public credentials from struct to proto.

PiperOrigin-RevId: 464678736
This commit is contained in:
hai007
2022-08-01 19:16:05 -07:00
committed by Copybara-Service
parent 778c77eacd
commit a732616a97
12 changed files with 139 additions and 152 deletions
+1 -3
View File
@@ -73,16 +73,14 @@ cc_library(
"//third_party/nearby/presence/implementation:__pkg__",
],
deps = [
"//connections/clients/windows:types",
"//connections/implementation/proto:offline_wire_formats_cc_proto",
"//internal/platform:base",
"//internal/platform:cancellation_flag",
"//internal/platform:uuid",
"//third_party/nearby/presence:credential",
"//third_party/nearby/presence/proto:credential_cc_proto",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:optional",
],
)
@@ -21,16 +21,16 @@
#include <vector>
#include "internal/platform/exception.h"
#include "third_party/nearby/presence/credential.h"
#include "third_party/nearby/presence/presence_identity.h"
#include "third_party/nearby/presence/proto/credential.pb.h"
namespace location {
namespace nearby {
namespace api {
using ::nearby::presence::PresenceIdentity;
using ::nearby::presence::PrivateCredential;
using ::nearby::presence::PublicCredential;
using ::nearby::presence::proto::PrivateCredential;
using ::nearby::presence::proto::PublicCredential;
enum class CredentialOperationStatus {
kFailed = 0,
+3 -1
View File
@@ -58,6 +58,7 @@ cc_library(
deps = [
":credential",
"//internal/platform:base",
"//third_party/nearby/presence/proto:credential_cc_proto",
"@com_google_absl//absl/strings",
"@com_google_glog//:glog",
],
@@ -136,13 +137,13 @@ cc_library(
name = "credential",
srcs = ["presence_identity.cc"],
hdrs = [
"credential.h",
"presence_identity.h",
],
visibility = [
"//third_party/nearby:__subpackages__",
],
deps = [
"//third_party/nearby/presence/proto:credential_cc_proto",
"//third_party/nearby/presence/proto:device_metadata_cc_proto",
],
)
@@ -241,6 +242,7 @@ cc_test(
":credential",
"//internal/platform:uuid",
"//internal/platform/implementation/g3", # build_cleaner: keep
"//third_party/nearby/presence/proto:credential_cc_proto",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_googletest//:gtest_main",
],
-113
View File
@@ -1,113 +0,0 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_PRESENCE_CREDENTIAL_H_
#define THIRD_PARTY_NEARBY_PRESENCE_CREDENTIAL_H_
#include <set>
#include <string>
#include <vector>
#include "third_party/nearby/presence/presence_identity.h"
#include "third_party/nearby/presence/proto/device_metadata.pb.h"
namespace nearby {
namespace presence {
struct PrivateCredential {
PresenceIdentity::IdentityType identity_type;
// The unique id of (and hashed based on) a pair of secret
// key (PrivateCredential.verification_key) and X509Certificate's public
// key (PublicCredential.verification_key).
std::vector<uint8_t> secret_id;
// The aes key to encrypt personal fields in public certificates.
// A bytes representation of a Secret Key owned by contact, to decrypt the
// encrypted DeviceMetadata bytes stored within the advertisement.
std::vector<uint8_t> authenticity_key;
// Bytes representation of a public key of X509Certificate, used in
// handshake during contact verification phase.
std::vector<uint8_t> verification_key;
// The time in millis from epoch when this credential becomes effective.
int64_t start_time;
// The time in millis from epoch when this credential expires.
int64_t end_time;
// The set of 2-byte salts already used to encrypt the metadata key.
std::set<std::vector<uint8_t>> consumed_salts;
// The aes key to encrypt DeviceMetadata in public credential.
std::vector<uint8_t> metadata_encryption_key;
proto::DeviceMetadata device_metadata;
};
inline bool operator==(const PrivateCredential& a, const PrivateCredential& b) {
return a.identity_type == b.identity_type && a.secret_id == b.secret_id &&
a.start_time == b.start_time && a.end_time == b.end_time &&
a.consumed_salts == b.consumed_salts &&
a.metadata_encryption_key == b.metadata_encryption_key &&
a.authenticity_key == b.authenticity_key &&
a.verification_key == b.verification_key &&
a.device_metadata.SerializeAsString() ==
b.device_metadata.SerializeAsString();
}
struct PublicCredential {
PresenceIdentity::IdentityType identity_type;
// The unique id of (and hashed based on) a pair of secret
// key (PrivateCredential.verification_key) and X509Certificate's public
// key (PublicCredential.verification_key).
std::vector<uint8_t> secret_id;
// Bytes representation of a Secret Key owned by contact, to decrypt the
// metadata_key stored within the advertisement.
std::vector<uint8_t> authenticity_key;
// Bytes representation of a public key of X509Certificate, used in
// handshake during contact verification phase.
std::vector<uint8_t> verification_key;
// The time in millis from epoch when this credential becomes effective.
int64_t start_time;
// The time in millis from epoch when this credential expires.
int64_t end_time;
// The encrypted DeviceMetadata in bytes, contains personal information of the
// device/user who created this certificate. Needs to be decrypted into bytes,
// and converted back to DeviceMetadata instance to access fields.
std::vector<uint8_t> encrypted_metadata_bytes;
// The tag for verifying metadata_encryption_key.
std::vector<uint8_t> metadata_encryption_key_tag;
};
inline bool operator==(const PublicCredential& a, const PublicCredential& b) {
return a.identity_type == b.identity_type && a.secret_id == b.secret_id &&
a.verification_key == b.verification_key &&
a.authenticity_key == b.authenticity_key &&
a.start_time == b.start_time && a.end_time == b.end_time &&
a.encrypted_metadata_bytes == b.encrypted_metadata_bytes &&
a.metadata_encryption_key_tag == b.metadata_encryption_key_tag;
}
} // namespace presence
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_PRESENCE_CREDENTIAL_H_
+26 -24
View File
@@ -15,17 +15,20 @@
#include <cstdlib>
#include <vector>
#include "internal/platform/uuid.h"
#include "third_party/nearby/presence/credential.h"
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "third_party/nearby/presence/presence_identity.h"
#include "internal/platform/uuid.h"
#include "third_party/nearby/presence/proto/credential.pb.h"
namespace nearby {
namespace presence {
namespace {
using ::nearby::presence::proto::PrivateCredential;
using ::nearby::presence::proto::PublicCredential;
using ::nearby::presence::proto::IdentityType::IDENTITY_TYPE_PROVISIONED;
using ::nearby::presence::proto::IdentityType::IDENTITY_TYPE_PUBLIC;
using ::protobuf_matchers::EqualsProto;
TEST(CredentialsTest, NoDefaultConstructor) {
EXPECT_FALSE(std::is_trivially_constructible<PrivateCredential>::value);
@@ -35,43 +38,42 @@ TEST(CredentialsTest, NoDefaultConstructor) {
TEST(CredentialsTest, InitPublicCredential) {
PublicCredential pc1 = {};
PublicCredential pc2 = {};
EXPECT_EQ(pc1, pc2);
pc1.identity_type = PresenceIdentity::IdentityType::kPublic;
EXPECT_FALSE(pc1 == pc2);
pc2.identity_type = PresenceIdentity::IdentityType::kPublic;
EXPECT_EQ(pc1, pc2);
EXPECT_THAT(pc1, EqualsProto(pc2));
pc1.set_identity_type(IDENTITY_TYPE_PUBLIC);
EXPECT_THAT(pc1, ::testing::Not(EqualsProto(pc2)));
pc2.set_identity_type(IDENTITY_TYPE_PUBLIC);
EXPECT_THAT(pc1, EqualsProto(pc2));
}
TEST(CredentialsTest, InitPrivateCredential) {
PrivateCredential pc1 = {};
PrivateCredential pc2 = {};
EXPECT_EQ(pc1, pc2);
pc1.identity_type = PresenceIdentity::IdentityType::kPublic;
EXPECT_FALSE(pc1 == pc2);
pc2.identity_type = PresenceIdentity::IdentityType::kPublic;
EXPECT_EQ(pc1, pc2);
EXPECT_THAT(pc1, EqualsProto(pc2));
pc1.set_identity_type(IDENTITY_TYPE_PUBLIC);
EXPECT_THAT(pc1, ::testing::Not(EqualsProto(pc2)));
pc2.set_identity_type(IDENTITY_TYPE_PUBLIC);
EXPECT_THAT(pc1, EqualsProto(pc2));
}
TEST(CredentialsTest, CopyPrivateCredential) {
PrivateCredential pc1 = {};
pc1.identity_type = PresenceIdentity::IdentityType::kProvisioned;
std::vector<uint8_t> salts = {};
salts.push_back(15);
pc1.consumed_salts.insert(salts.begin(), salts.end());
pc1.device_metadata.set_device_name("Android Phone");
pc1.device_metadata.set_device_type(proto::DeviceMetadata::PHONE);
pc1.set_identity_type(IDENTITY_TYPE_PROVISIONED);
auto salts = pc1.mutable_consumed_salts();
salts->insert(std::pair<int32, bool>(15, true));
pc1.mutable_device_metadata()->set_device_name("Android Phone");
pc1.mutable_device_metadata()->set_device_type(proto::DeviceMetadata::PHONE);
PrivateCredential pc1_copy = {pc1};
EXPECT_EQ(pc1, pc1_copy);
EXPECT_THAT(pc1, EqualsProto(pc1_copy));
}
TEST(CredentialsTest, CopyPublicCredential) {
PublicCredential pc1 = {};
pc1.identity_type = PresenceIdentity::IdentityType::kProvisioned;
pc1.set_identity_type(IDENTITY_TYPE_PROVISIONED);
for (const uint8_t byte : location::nearby::Uuid().data()) {
pc1.secret_id.emplace_back(byte);
pc1.mutable_secret_id()->push_back(byte);
}
PublicCredential pc1_copy = {pc1};
EXPECT_EQ(pc1, pc1_copy);
EXPECT_THAT(pc1, EqualsProto(pc1_copy));
}
} // namespace
} // namespace presence
+1
View File
@@ -33,6 +33,7 @@ cc_library(
"//internal/platform/implementation:comm",
"//third_party/nearby/presence:credential",
"//third_party/nearby/presence/implementation/mediums",
"//third_party/nearby/presence/proto:credential_cc_proto",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
+4 -3
View File
@@ -22,15 +22,16 @@
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "internal/platform/implementation/credential_storage.h"
#include "third_party/nearby/presence/credential.h"
#include "third_party/nearby/presence/presence_identity.h"
#include "third_party/nearby/presence/proto/credential.pb.h"
namespace nearby {
namespace presence {
using ::nearby::presence::PresenceIdentity;
struct GenerateCredentialsCallback {
std::function<void(std::vector<PublicCredential>)> credentials_generated_cb;
std::function<void(std::vector<proto::PublicCredential>)>
credentials_generated_cb;
};
struct UpdateRemotePublicCredentialsCallback {
@@ -62,7 +63,7 @@ class CredentialManager {
// Update remote public credentials.
virtual void UpdateRemotePublicCredentials(
std::string account_name,
std::vector<PublicCredential> remote_public_creds,
std::vector<proto::PublicCredential> remote_public_creds,
UpdateRemotePublicCredentialsCallback credentials_updated_cb) = 0;
// Used to fetch private creds when broadcasting.
@@ -22,9 +22,9 @@
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "internal/platform/credential_storage.h"
#include "third_party/nearby/presence/credential.h"
#include "third_party/nearby/presence/implementation/credential_manager.h"
#include "third_party/nearby/presence/presence_identity.h"
#include "third_party/nearby/presence/proto/credential.pb.h"
namespace nearby {
namespace presence {
@@ -40,7 +40,7 @@ class CredentialManagerImpl : public CredentialManager {
void UpdateRemotePublicCredentials(
std::string account_name,
std::vector<PublicCredential> remote_public_creds,
std::vector<proto::PublicCredential> remote_public_creds,
UpdateRemotePublicCredentialsCallback credentials_updated_cb) override{};
void GetPrivateCredentials(
+14
View File
@@ -10,3 +10,17 @@ cc_proto_library(
],
deps = [":device_metadata_proto"],
)
proto_library(
name = "credential_proto",
srcs = ["credential.proto"],
deps = [":device_metadata_proto"],
)
cc_proto_library(
name = "credential_cc_proto",
visibility = [
"//third_party/nearby:__subpackages__",
],
deps = [":credential_proto"],
)
+82
View File
@@ -0,0 +1,82 @@
syntax = "proto2";
package nearby.presence.proto;
import "third_party/nearby/presence/proto/device_metadata.proto";
option cc_api_version = 2;
option java_api_version = 2;
option java_package = "com.google.nearby.presence";
enum IdentityType {
IDENTITY_TYPE_UNSPECIFIED = 0;
IDENTITY_TYPE_PRIVATE = 1;
IDENTITY_TYPE_TRUSTED = 2;
IDENTITY_TYPE_PUBLIC = 3;
IDENTITY_TYPE_PROVISIONED = 4;
}
// A proto to store the local device's private credential.
message PrivateCredential {
optional IdentityType identity_type = 1;
// The unique id of (and hashed based on) a pair of secret
// key (PrivateCredential.verification_key) and X509Certificate's public
// key (PublicCredential.verification_key).
optional bytes secret_id = 2;
// The aes key to encrypt personal fields in public certificates.
// A bytes representation of a Secret Key owned by contact, to decrypt the
// encrypted DeviceMetadata bytes stored within the advertisement.
optional bytes authenticity_key = 3;
// Bytes representation of a private key of X509Certificate, used in
// handshake during contact verification phase.
optional bytes verification_key = 4;
// The time in millis from epoch when this credential becomes effective.
optional uint64 start_time_millis = 5;
// The time in millis from epoch when this credential expires.
optional uint64 end_time_millis = 6;
// The set of 2-byte salts already used to encrypt the metadata key.
map<uint32, bool> consumed_salts = 7;
// The aes key to encrypt DeviceMetadata in public credential.
optional bytes metadata_encryption_key = 8;
// The device metadata relates to this private credential.
optional DeviceMetadata device_metadata = 9;
}
message PublicCredential {
optional IdentityType identity_type = 1;
// The unique id of (and hashed based on) a pair of secret
// key (PrivateCredential.verification_key) and X509Certificate's public
// key (PublicCredential.verification_key).
optional bytes secret_id = 2;
// Bytes representation of a Secret Key owned by contact, to decrypt the
// metadata_key stored within the advertisement.
optional bytes authenticity_key = 3;
// Bytes representation of a public key of X509Certificate, used in
// handshake during contact verification phase.
optional bytes verification_key = 4;
// The time in millis from epoch when this credential becomes effective.
optional uint64 start_time_millis = 5;
// The time in millis from epoch when this credential expires.
optional uint64 end_time_millis = 6;
// The encrypted DeviceMetadata in bytes, contains personal information of the
// device/user who created this certificate. Needs to be decrypted into bytes,
// and converted back to DeviceMetadata instance to access fields.
optional bytes encrypted_metadata_bytes = 7;
// The tag for verifying metadata_encryption_key.
optional bytes metadata_encryption_key_tag = 8;
}
+1 -1
View File
@@ -19,7 +19,7 @@ message DeviceMetadata {
// The Bluetooth MAC address of the device which created the certificate.
optional string bluetooth_mac_address = 5;
/** The version of the frame. */
/** The types of the device. */
enum DeviceType {
UNSPECIFIED = 0;
PHONE = 1;
+2 -2
View File
@@ -18,10 +18,10 @@
#include <string>
#include <vector>
#include "third_party/nearby/presence/credential.h"
#include "third_party/nearby/presence/data_element.h"
#include "third_party/nearby/presence/power_mode.h"
#include "third_party/nearby/presence/presence_identity.h"
#include "third_party/nearby/presence/proto/credential.pb.h"
namespace nearby {
namespace presence {
@@ -58,7 +58,7 @@ struct LegacyPresenceScanFilter : public ScanFilter {
// Android T needs clients to provide remote public credentials in scan
// requests.
std::vector<PublicCredential> remote_public_credentials;
std::vector<proto::PublicCredential> remote_public_credentials;
// A list of presence actions for matching. Matching condition is met as
// long as theres one or more equal actions between Scan actions and