Commit 74ae2d6b authored by Naomi Musgrave's avatar Naomi Musgrave Committed by Commit Bot

[Nearby] fetch the certificate when handling an outgoing advertisement.

Fetch the certificate from the CertificateManager, to pass to CreateShareTarget.
This is a follow-on from a comment on I1b1ed2252baa59e68f4daa1e772a51e9cf32eff4.

Bug: 1084644
Change-Id: Ie42ae628517716cc64e7d57011079497e803f260
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2346390
Commit-Queue: Naomi Musgrave <nmusgrave@chromium.org>
Commit-Queue: Alex Chau <alexchau@chromium.org>
Reviewed-by: default avatarAlex Chau <alexchau@chromium.org>
Reviewed-by: default avatarRichard Knoll <knollr@chromium.org>
Auto-Submit: Naomi Musgrave <nmusgrave@chromium.org>
Cr-Commit-Position: refs/heads/master@{#797286}
parent 677aac8f
...@@ -565,12 +565,32 @@ void NearbySharingServiceImpl::OnOutgoingAdvertisementDecoded( ...@@ -565,12 +565,32 @@ void NearbySharingServiceImpl::OnOutgoingAdvertisementDecoded(
return; return;
} }
// Once get the advertisement, first thing to do is to decrypt its device name // Once get the advertisement, the first thing to do is decrypt the
// based on its visibility and create a ShareTarget to represent this remote // certificate.
// device. std::vector<uint8_t> encrypted_metadata = advertisement->encrypted_metadata;
// TODO(nmusgrave): Use valid certificate from certificate manager. std::vector<uint8_t> salt = advertisement->salt;
GetCertificateManager()->GetDecryptedPublicCertificate(
std::move(encrypted_metadata), std::move(salt),
base::BindOnce(&NearbySharingServiceImpl::OnOutgoingDecryptedCertificate,
weak_ptr_factory_.GetWeakPtr(), endpoint_id,
std::move(advertisement)));
}
void NearbySharingServiceImpl::OnOutgoingDecryptedCertificate(
const std::string& endpoint_id,
sharing::mojom::AdvertisementPtr advertisement,
base::Optional<NearbyShareDecryptedPublicCertificate> certificate) {
// Check again for this endpoint id, to avoid race conditions.
if (outgoing_share_target_map_.find(endpoint_id) !=
outgoing_share_target_map_.end()) {
return;
}
// The certificate provides the device name, in order to create a ShareTarget
// to represent this remote device.
base::Optional<ShareTarget> share_target = CreateShareTarget( base::Optional<ShareTarget> share_target = CreateShareTarget(
endpoint_id, std::move(advertisement), /*certificate=*/base::nullopt, endpoint_id, std::move(advertisement), std::move(certificate),
/*is_incoming=*/false); /*is_incoming=*/false);
if (!share_target) { if (!share_target) {
NS_LOG(VERBOSE) << __func__ NS_LOG(VERBOSE) << __func__
......
...@@ -141,6 +141,10 @@ class NearbySharingServiceImpl ...@@ -141,6 +141,10 @@ class NearbySharingServiceImpl
void OnOutgoingAdvertisementDecoded( void OnOutgoingAdvertisementDecoded(
const std::string& endpoint_id, const std::string& endpoint_id,
sharing::mojom::AdvertisementPtr advertisement); sharing::mojom::AdvertisementPtr advertisement);
void OnOutgoingDecryptedCertificate(
const std::string& endpoint_id,
sharing::mojom::AdvertisementPtr advertisement,
base::Optional<NearbyShareDecryptedPublicCertificate> certificate);
bool IsBluetoothPresent() const; bool IsBluetoothPresent() const;
bool IsBluetoothPowered() const; bool IsBluetoothPowered() const;
bool HasAvailableConnectionMediums(); bool HasAvailableConnectionMediums();
......
...@@ -714,6 +714,65 @@ TEST_F(NearbySharingServiceImplTest, ...@@ -714,6 +714,65 @@ TEST_F(NearbySharingServiceImplTest,
// Ensure decoder parses a valid endpoint advertisement. // Ensure decoder parses a valid endpoint advertisement.
SetUpAdvertisementDecoder(kValidV1EndpointInfo, SetUpAdvertisementDecoder(kValidV1EndpointInfo,
/*return_empty_advertisement=*/false); /*return_empty_advertisement=*/false);
SetUpCertificateManager(/*return_empty_certificate=*/false);
// Start discovering, to ensure a discovery listener is registered.
base::RunLoop run_loop;
MockTransferUpdateCallback transfer_callback;
NiceMock<MockShareTargetDiscoveredCallback> discovery_callback;
EXPECT_EQ(
NearbySharingService::StatusCodes::kOk,
service_->RegisterSendSurface(&transfer_callback, &discovery_callback,
SendSurfaceState::kForeground));
EXPECT_TRUE(fake_nearby_connections_manager_->IsDiscovering());
// Discover a new endpoint, with fields set up a valid certificate.
EXPECT_CALL(discovery_callback, OnShareTargetDiscovered)
.WillOnce([&run_loop](ShareTarget share_target) {
EXPECT_FALSE(share_target.is_incoming);
EXPECT_TRUE(share_target.is_known);
EXPECT_FALSE(share_target.has_attachments());
EXPECT_EQ(kDeviceName, share_target.device_name);
EXPECT_EQ(GURL(kTestMetadataIconUrl), share_target.image_url);
EXPECT_EQ(nearby_share::mojom::ShareTargetType::kUnknown,
share_target.type);
EXPECT_TRUE(share_target.device_id);
EXPECT_NE(kEndpointId, share_target.device_id);
EXPECT_EQ(kTestMetadataFullName, share_target.full_name);
run_loop.Quit();
});
fake_nearby_connections_manager_->OnEndpointFound(
kEndpointId,
location::nearby::connections::mojom::DiscoveredEndpointInfo::New(
kValidV1EndpointInfo, kServiceId));
run_loop.Run();
// Register another send surface, which will automatically catch up discovered
// endpoints.
base::RunLoop run_loop2;
MockTransferUpdateCallback transfer_callback2;
NiceMock<MockShareTargetDiscoveredCallback> discovery_callback2;
EXPECT_CALL(discovery_callback2, OnShareTargetDiscovered)
.WillOnce([&run_loop2](ShareTarget share_target) {
EXPECT_EQ(kDeviceName, share_target.device_name);
run_loop2.Quit();
});
EXPECT_EQ(
NearbySharingService::StatusCodes::kOk,
service_->RegisterSendSurface(&transfer_callback2, &discovery_callback2,
SendSurfaceState::kForeground));
run_loop2.Run();
}
TEST_F(NearbySharingServiceImplTest, RegisterSendSurfaceEmptyCertificate) {
ui::ScopedSetIdleState unlocked(ui::IDLE_STATE_IDLE);
SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI);
// Ensure decoder parses a valid endpoint advertisement.
SetUpAdvertisementDecoder(kValidV1EndpointInfo,
/*return_empty_advertisement=*/false);
SetUpCertificateManager(/*return_empty_certificate=*/true);
// Start discovering, to ensure a discovery listener is registered. // Start discovering, to ensure a discovery listener is registered.
base::RunLoop run_loop; base::RunLoop run_loop;
...@@ -725,10 +784,19 @@ TEST_F(NearbySharingServiceImplTest, ...@@ -725,10 +784,19 @@ TEST_F(NearbySharingServiceImplTest,
SendSurfaceState::kForeground)); SendSurfaceState::kForeground));
EXPECT_TRUE(fake_nearby_connections_manager_->IsDiscovering()); EXPECT_TRUE(fake_nearby_connections_manager_->IsDiscovering());
// Discover a new endpoint. // Discover a new endpoint, with fields set up a valid certificate.
EXPECT_CALL(discovery_callback, OnShareTargetDiscovered) EXPECT_CALL(discovery_callback, OnShareTargetDiscovered)
.WillOnce([&run_loop](ShareTarget share_target) { .WillOnce([&run_loop](ShareTarget share_target) {
EXPECT_FALSE(share_target.is_incoming);
EXPECT_FALSE(share_target.is_known);
EXPECT_FALSE(share_target.has_attachments());
EXPECT_EQ(kDeviceName, share_target.device_name); EXPECT_EQ(kDeviceName, share_target.device_name);
EXPECT_FALSE(share_target.image_url);
EXPECT_EQ(nearby_share::mojom::ShareTargetType::kUnknown,
share_target.type);
EXPECT_TRUE(share_target.device_id);
EXPECT_EQ(kEndpointId, share_target.device_id);
EXPECT_FALSE(share_target.full_name);
run_loop.Quit(); run_loop.Quit();
}); });
fake_nearby_connections_manager_->OnEndpointFound( fake_nearby_connections_manager_->OnEndpointFound(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment