1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/nearby_sharing/certificates/nearby_share_decrypted_public_certificate.h"
#include <array>
#include <optional>
#include "chrome/browser/nearby_sharing/certificates/constants.h"
#include "chrome/browser/nearby_sharing/certificates/test_util.h"
#include "chromeos/ash/services/nearby/public/mojom/nearby_share_settings.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/nearby/sharing/proto/rpc_resources.pb.h"
namespace {
// The for_selected_contacts field of a public certificate proto is irrelevant
// for remote device certificates. Even if set, it is meaningless. It only has
// meaning for private certificates converted to public certificates and
// uploaded to the Nearby server.
const nearby_share::mojom::Visibility kTestPublicCertificateVisibility =
nearby_share::mojom::Visibility::kNoOne;
} // namespace
TEST(NearbyShareDecryptedPublicCertificateTest, Decrypt) {
nearby::sharing::proto::PublicCertificate proto_cert =
GetNearbyShareTestPublicCertificate(kTestPublicCertificateVisibility);
proto_cert.set_for_self_share(true);
std::optional<NearbyShareDecryptedPublicCertificate> cert =
NearbyShareDecryptedPublicCertificate::DecryptPublicCertificate(
proto_cert, GetNearbyShareTestEncryptedMetadataKey());
EXPECT_TRUE(cert);
EXPECT_EQ(
base::Time::FromSecondsSinceUnixEpoch(proto_cert.start_time().seconds()),
cert->not_before());
EXPECT_EQ(
base::Time::FromSecondsSinceUnixEpoch(proto_cert.end_time().seconds()),
cert->not_after());
EXPECT_EQ(std::vector<uint8_t>(proto_cert.secret_id().begin(),
proto_cert.secret_id().end()),
cert->id());
EXPECT_EQ(GetNearbyShareTestMetadata().SerializeAsString(),
cert->unencrypted_metadata().SerializeAsString());
EXPECT_EQ(proto_cert.for_self_share(), cert->for_self_share());
}
TEST(NearbyShareDecryptedPublicCertificateTest, Decrypt_IncorrectKeyFailure) {
// Input incorrect metadata encryption key.
EXPECT_FALSE(NearbyShareDecryptedPublicCertificate::DecryptPublicCertificate(
GetNearbyShareTestPublicCertificate(kTestPublicCertificateVisibility),
NearbyShareEncryptedMetadataKey(
std::array<uint8_t, kNearbyShareNumBytesMetadataEncryptionKeySalt>(),
std::array<uint8_t, kNearbyShareNumBytesMetadataEncryptionKey>())));
}
TEST(NearbyShareDecryptedPublicCertificateTest,
Decrypt_MetadataDecryptionFailure) {
// Use metadata that cannot be decrypted with the given key.
nearby::sharing::proto::PublicCertificate proto_cert =
GetNearbyShareTestPublicCertificate(kTestPublicCertificateVisibility);
proto_cert.set_encrypted_metadata_bytes("invalid metadata");
EXPECT_FALSE(NearbyShareDecryptedPublicCertificate::DecryptPublicCertificate(
proto_cert, GetNearbyShareTestEncryptedMetadataKey()));
}
TEST(NearbyShareDecryptedPublicCertificateTest, Decrypt_InvalidDataFailure) {
// Do not accept the input PublicCertificate because the validity period does
// not make sense.
nearby::sharing::proto::PublicCertificate proto_cert =
GetNearbyShareTestPublicCertificate(kTestPublicCertificateVisibility);
proto_cert.mutable_end_time()->set_seconds(proto_cert.start_time().seconds() -
1);
EXPECT_FALSE(NearbyShareDecryptedPublicCertificate::DecryptPublicCertificate(
proto_cert, GetNearbyShareTestEncryptedMetadataKey()));
}
TEST(NearbyShareDecryptedPublicCertificateTest, Verify) {
std::optional<NearbyShareDecryptedPublicCertificate> cert =
NearbyShareDecryptedPublicCertificate::DecryptPublicCertificate(
GetNearbyShareTestPublicCertificate(kTestPublicCertificateVisibility),
GetNearbyShareTestEncryptedMetadataKey());
EXPECT_TRUE(cert->VerifySignature(GetNearbyShareTestPayloadToSign(),
GetNearbyShareTestSampleSignature()));
}
TEST(NearbyShareDecryptedPublicCertificateTest, Verify_InitFailure) {
// Public key has invalid SubjectPublicKeyInfo format.
nearby::sharing::proto::PublicCertificate proto_cert =
GetNearbyShareTestPublicCertificate(kTestPublicCertificateVisibility);
proto_cert.set_public_key("invalid public key");
std::optional<NearbyShareDecryptedPublicCertificate> cert =
NearbyShareDecryptedPublicCertificate::DecryptPublicCertificate(
proto_cert, GetNearbyShareTestEncryptedMetadataKey());
ASSERT_TRUE(cert);
EXPECT_FALSE(cert->VerifySignature(GetNearbyShareTestPayloadToSign(),
GetNearbyShareTestSampleSignature()));
}
TEST(NearbyShareDecryptedPublicCertificateTest, Verify_WrongSignature) {
std::optional<NearbyShareDecryptedPublicCertificate> cert =
NearbyShareDecryptedPublicCertificate::DecryptPublicCertificate(
GetNearbyShareTestPublicCertificate(kTestPublicCertificateVisibility),
GetNearbyShareTestEncryptedMetadataKey());
EXPECT_FALSE(
cert->VerifySignature(GetNearbyShareTestPayloadToSign(),
/*signature=*/base::span<const uint8_t>()));
}
TEST(NearbyShareDecryptedPublicCertificateTest, HashAuthenticationToken) {
std::optional<NearbyShareDecryptedPublicCertificate> cert =
NearbyShareDecryptedPublicCertificate::DecryptPublicCertificate(
GetNearbyShareTestPublicCertificate(kTestPublicCertificateVisibility),
GetNearbyShareTestEncryptedMetadataKey());
EXPECT_EQ(GetNearbyShareTestPayloadHashUsingSecretKey(),
cert->HashAuthenticationToken(GetNearbyShareTestPayloadToSign()));
}
|