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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
// 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/paired_key_verification_runner.h"
#include <iomanip>
#include <iostream>
#include "base/functional/bind.h"
#include "chrome/browser/nearby_sharing/certificates/common.h"
#include "chrome/browser/nearby_sharing/certificates/constants.h"
#include "chrome/browser/nearby_sharing/nearby_share_metrics.h"
#include "components/cross_device/logging/logging.h"
#include "third_party/nearby/sharing/proto/rpc_resources.pb.h"
#include "third_party/nearby/sharing/proto/wire_format.pb.h"
namespace {
std::ostream& operator<<(
std::ostream& out,
const PairedKeyVerificationRunner::PairedKeyVerificationResult& obj) {
out << static_cast<std::underlying_type<
PairedKeyVerificationRunner::PairedKeyVerificationResult>::type>(obj);
return out;
}
PairedKeyVerificationRunner::PairedKeyVerificationResult Convert(
sharing::mojom::PairedKeyResultFrame::Status status) {
switch (status) {
case sharing::mojom::PairedKeyResultFrame_Status::kUnknown:
return PairedKeyVerificationRunner::PairedKeyVerificationResult::kUnknown;
case sharing::mojom::PairedKeyResultFrame_Status::kSuccess:
return PairedKeyVerificationRunner::PairedKeyVerificationResult::kSuccess;
case sharing::mojom::PairedKeyResultFrame_Status::kFail:
return PairedKeyVerificationRunner::PairedKeyVerificationResult::kFail;
case sharing::mojom::PairedKeyResultFrame_Status::kUnable:
return PairedKeyVerificationRunner::PairedKeyVerificationResult::kUnable;
}
}
std::vector<uint8_t> PadPrefix(char prefix, std::vector<uint8_t> bytes) {
bytes.insert(bytes.begin(), prefix);
return bytes;
}
} // namespace
PairedKeyVerificationRunner::PairedKeyVerificationRunner(
const ShareTarget& share_target,
const std::string& endpoint_id,
const std::vector<uint8_t>& token,
NearbyConnection* connection,
const std::optional<NearbyShareDecryptedPublicCertificate>& certificate,
NearbyShareCertificateManager* certificate_manager,
nearby_share::mojom::Visibility visibility,
bool restrict_to_contacts,
IncomingFramesReader* frames_reader,
base::TimeDelta read_frame_timeout)
: share_target_(share_target),
endpoint_id_(endpoint_id),
raw_token_(token),
connection_(connection),
certificate_(certificate),
certificate_manager_(certificate_manager),
visibility_(visibility),
restrict_to_contacts_(restrict_to_contacts),
frames_reader_(frames_reader),
read_frame_timeout_(read_frame_timeout) {
DCHECK(connection);
DCHECK(certificate_manager);
DCHECK(frames_reader);
if (share_target.is_incoming) {
local_prefix_ = kNearbyShareReceiverVerificationPrefix;
remote_prefix_ = kNearbyShareSenderVerificationPrefix;
} else {
remote_prefix_ = kNearbyShareReceiverVerificationPrefix;
local_prefix_ = kNearbyShareSenderVerificationPrefix;
}
}
PairedKeyVerificationRunner::~PairedKeyVerificationRunner() = default;
void PairedKeyVerificationRunner::Run(
base::OnceCallback<void(PairedKeyVerificationResult)> callback) {
DCHECK(!callback_);
callback_ = std::move(callback);
SendPairedKeyEncryptionFrame();
frames_reader_->ReadFrame(
sharing::mojom::V1Frame::Tag::kPairedKeyEncryption,
base::BindOnce(
&PairedKeyVerificationRunner::OnReadPairedKeyEncryptionFrame,
weak_ptr_factory_.GetWeakPtr()),
read_frame_timeout_);
}
void PairedKeyVerificationRunner::OnReadPairedKeyEncryptionFrame(
std::optional<sharing::mojom::V1FramePtr> frame) {
if (!frame) {
CD_LOG(WARNING, Feature::NS)
<< __func__ << ": Failed to read remote paired key encrpytion";
RecordNearbySharePairedKeyVerificationError(
NearbySharePairedKeyVerificationError::kFailedToReadEncryptionFrame);
std::move(callback_).Run(PairedKeyVerificationResult::kFail);
return;
}
std::vector<PairedKeyVerificationResult> verification_results;
PairedKeyVerificationResult remote_public_certificate_result =
VerifyRemotePublicCertificate(*frame);
verification_results.push_back(remote_public_certificate_result);
CD_LOG(VERBOSE, Feature::NS)
<< __func__ << ": Remote public certificate verification result "
<< remote_public_certificate_result;
if (remote_public_certificate_result ==
PairedKeyVerificationResult::kSuccess) {
SendCertificateInfo();
} else if (restrict_to_contacts_) {
CD_LOG(VERBOSE, Feature::NS)
<< __func__
<< ": we are only allowing connections with contacts. "
"Rejecting connection from unknown ShareTarget - "
<< share_target_.id;
RecordNearbySharePairedKeyVerificationError(
NearbySharePairedKeyVerificationError::
kUnableToVerifyRemotePublicCertificateWhileRestrictedToContacts);
std::move(callback_).Run(PairedKeyVerificationResult::kFail);
return;
}
PairedKeyVerificationResult local_result =
VerifyPairedKeyEncryptionFrame(*frame);
verification_results.push_back(local_result);
CD_LOG(VERBOSE, Feature::NS)
<< __func__ << ": Paired key encryption verification result "
<< local_result;
SendPairedKeyResultFrame(local_result);
frames_reader_->ReadFrame(
sharing::mojom::V1Frame::Tag::kPairedKeyResult,
base::BindOnce(&PairedKeyVerificationRunner::OnReadPairedKeyResultFrame,
weak_ptr_factory_.GetWeakPtr(),
std::move(verification_results)),
read_frame_timeout_);
}
void PairedKeyVerificationRunner::OnReadPairedKeyResultFrame(
std::vector<PairedKeyVerificationResult> verification_results,
std::optional<sharing::mojom::V1FramePtr> frame) {
if (!frame) {
CD_LOG(WARNING, Feature::NS)
<< __func__ << ": Failed to read remote paired key result";
RecordNearbySharePairedKeyVerificationError(
NearbySharePairedKeyVerificationError::kFailedToReadResultFrame);
std::move(callback_).Run(PairedKeyVerificationResult::kFail);
return;
}
PairedKeyVerificationResult key_result =
Convert(frame.value()->get_paired_key_result()->status);
verification_results.push_back(key_result);
CD_LOG(VERBOSE, Feature::NS)
<< __func__ << ": Paired key result frame result " << key_result;
PairedKeyVerificationResult combined_result =
MergeResults(verification_results);
CD_LOG(VERBOSE, Feature::NS)
<< __func__ << ": Combined verification result " << combined_result;
std::move(callback_).Run(combined_result);
}
void PairedKeyVerificationRunner::SendPairedKeyResultFrame(
PairedKeyVerificationResult result) {
nearby::sharing::service::proto::Frame frame;
frame.set_version(nearby::sharing::service::proto::Frame::V1);
nearby::sharing::service::proto::V1Frame* v1_frame = frame.mutable_v1();
v1_frame->set_type(
nearby::sharing::service::proto::V1Frame::PAIRED_KEY_RESULT);
nearby::sharing::service::proto::PairedKeyResultFrame* result_frame =
v1_frame->mutable_paired_key_result();
switch (result) {
case PairedKeyVerificationResult::kUnable:
result_frame->set_status(
nearby::sharing::service::proto::PairedKeyResultFrame::UNABLE);
break;
case PairedKeyVerificationResult::kSuccess:
result_frame->set_status(
nearby::sharing::service::proto::PairedKeyResultFrame::SUCCESS);
break;
case PairedKeyVerificationResult::kFail:
result_frame->set_status(
nearby::sharing::service::proto::PairedKeyResultFrame::FAIL);
break;
case PairedKeyVerificationResult::kUnknown:
result_frame->set_status(
nearby::sharing::service::proto::PairedKeyResultFrame::UNKNOWN);
break;
}
std::vector<uint8_t> data(frame.ByteSizeLong());
frame.SerializeToArray(data.data(), frame.ByteSizeLong());
connection_->Write(std::move(data));
}
void PairedKeyVerificationRunner::SendCertificateInfo() {
// TODO(https://crbug.com/1114765): Update once the bug is resolved.
std::vector<nearby::sharing::proto::PublicCertificate> certificates;
if (certificates.empty()) {
return;
}
nearby::sharing::service::proto::Frame frame;
frame.set_version(nearby::sharing::service::proto::Frame::V1);
nearby::sharing::service::proto::V1Frame* v1_frame = frame.mutable_v1();
v1_frame->set_type(
nearby::sharing::service::proto::V1Frame::CERTIFICATE_INFO);
nearby::sharing::service::proto::CertificateInfoFrame* cert_frame =
v1_frame->mutable_certificate_info();
for (const auto& certificate : certificates) {
nearby::sharing::service::proto::PublicCertificate* cert =
cert_frame->add_public_certificate();
cert->set_secret_id(certificate.secret_id());
cert->set_authenticity_key(certificate.secret_key());
cert->set_public_key(certificate.public_key());
cert->set_start_time(certificate.start_time().seconds() * 1000);
cert->set_end_time(certificate.end_time().seconds() * 1000);
cert->set_encrypted_metadata_bytes(certificate.encrypted_metadata_bytes());
cert->set_metadata_encryption_key_tag(
certificate.metadata_encryption_key_tag());
}
std::vector<uint8_t> data(frame.ByteSizeLong());
frame.SerializeToArray(data.data(), frame.ByteSizeLong());
connection_->Write(std::move(data));
}
void PairedKeyVerificationRunner::SendPairedKeyEncryptionFrame() {
nearby::sharing::service::proto::Frame frame;
frame.set_version(nearby::sharing::service::proto::Frame::V1);
nearby::sharing::service::proto::V1Frame* v1_frame = frame.mutable_v1();
v1_frame->set_type(
nearby::sharing::service::proto::V1Frame::PAIRED_KEY_ENCRYPTION);
nearby::sharing::service::proto::PairedKeyEncryptionFrame* encryption_frame =
v1_frame->mutable_paired_key_encryption();
if (std::optional<std::vector<uint8_t>> signature =
certificate_manager_->SignWithPrivateCertificate(
visibility_, PadPrefix(local_prefix_, raw_token_));
signature && !signature->empty()) {
encryption_frame->set_signed_data(signature->data(), signature->size());
} else {
auto random_bytes =
GenerateRandomBytes<kNearbyShareNumBytesRandomSignature>();
encryption_frame->set_signed_data(random_bytes.data(), random_bytes.size());
}
{
std::array<uint8_t, kNearbyShareNumBytesAuthenticationTokenHash>
certificate_id_hash =
certificate_ ? certificate_->HashAuthenticationToken(raw_token_)
: GenerateRandomBytes<
kNearbyShareNumBytesAuthenticationTokenHash>();
encryption_frame->set_secret_id_hash(certificate_id_hash.data(),
certificate_id_hash.size());
}
std::vector<uint8_t> data(frame.ByteSizeLong());
frame.SerializeToArray(data.data(), frame.ByteSizeLong());
connection_->Write(std::move(data));
}
PairedKeyVerificationRunner::PairedKeyVerificationResult
PairedKeyVerificationRunner::VerifyRemotePublicCertificate(
const sharing::mojom::V1FramePtr& frame) {
if (std::optional<std::array<
uint8_t, kNearbyShareNumBytesAuthenticationTokenHash>> hash =
certificate_manager_->HashAuthenticationTokenWithPrivateCertificate(
visibility_, raw_token_);
hash && std::ranges::equal(
*hash, frame->get_paired_key_encryption()->secret_id_hash)) {
CD_LOG(VERBOSE, Feature::NS)
<< __func__ << ": Successfully verified remote public certificate.";
return PairedKeyVerificationResult::kSuccess;
}
CD_LOG(VERBOSE, Feature::NS)
<< __func__ << ": Unable to verify remote public certificate.";
return PairedKeyVerificationResult::kUnable;
}
PairedKeyVerificationRunner::PairedKeyVerificationResult
PairedKeyVerificationRunner::VerifyPairedKeyEncryptionFrame(
const sharing::mojom::V1FramePtr& frame) {
if (!certificate_) {
CD_LOG(VERBOSE, Feature::NS)
<< __func__
<< ": Unable to verify remote paired key encryption frame. "
"Certificate not found.";
return PairedKeyVerificationResult::kUnable;
}
if (!certificate_->VerifySignature(
PadPrefix(remote_prefix_, raw_token_),
frame->get_paired_key_encryption()->signed_data)) {
CD_LOG(VERBOSE, Feature::NS)
<< __func__
<< ": Unable to verify remote paired key encryption frame. "
"Signature verification failed.";
if (!frame->get_paired_key_encryption()->optional_signed_data) {
CD_LOG(VERBOSE, Feature::NS)
<< __func__ << ": No fallback signature to verify.";
RecordNearbySharePairedKeyVerificationError(
NearbySharePairedKeyVerificationError::kMissingOptionalSignature);
return PairedKeyVerificationResult::kFail;
}
CD_LOG(VERBOSE, Feature::NS)
<< __func__
<< ": Attempting to verify fallback signature for relaxed visibility.";
if (!certificate_->VerifySignature(
PadPrefix(remote_prefix_, raw_token_),
*frame->get_paired_key_encryption()->optional_signed_data)) {
CD_LOG(VERBOSE, Feature::NS)
<< __func__
<< ": Unable to verify remote paired key encryption frame. "
"Fallback signature verification failed.";
RecordNearbySharePairedKeyVerificationError(
NearbySharePairedKeyVerificationError::
kUnableToVerifyOptionalSignature);
return PairedKeyVerificationResult::kFail;
}
}
if (!share_target_.is_known) {
CD_LOG(VERBOSE, Feature::NS)
<< __func__
<< ": Unable to verify remote paired key encryption frame. "
"Remote side is not a known share target.";
return PairedKeyVerificationResult::kUnable;
}
CD_LOG(VERBOSE, Feature::NS)
<< __func__
<< ": Successfully verified remote paired key encryption frame.";
return PairedKeyVerificationResult::kSuccess;
}
PairedKeyVerificationRunner::PairedKeyVerificationResult
PairedKeyVerificationRunner::MergeResults(
const std::vector<PairedKeyVerificationResult>& results) {
bool all_success = true;
for (const auto& result : results) {
if (result == PairedKeyVerificationResult::kFail) {
return result;
}
if (result != PairedKeyVerificationResult::kSuccess) {
all_success = false;
}
}
return all_success ? PairedKeyVerificationResult::kSuccess
: PairedKeyVerificationResult::kUnable;
}
|