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
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/peerconnection/rtc_certificate_generator.h"
#include <string>
#include <utility>
#include "base/functional/bind.h"
#include "base/task/single_thread_task_runner.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/modules/peerconnection/peer_connection_dependency_factory.h"
#include "third_party/blink/renderer/platform/wtf/thread_safe_ref_counted.h"
#include "third_party/webrtc/api/scoped_refptr.h"
#include "third_party/webrtc/rtc_base/rtc_certificate.h"
#include "third_party/webrtc/rtc_base/rtc_certificate_generator.h"
namespace blink {
namespace {
// A certificate generation request spawned by
// |GenerateCertificateWithOptionalExpiration|. This
// is handled by a separate class so that reference counting can keep the
// request alive independently of the |RTCCertificateGenerator| that spawned it.
class RTCCertificateGeneratorRequest
: public WTF::ThreadSafeRefCounted<RTCCertificateGeneratorRequest> {
public:
RTCCertificateGeneratorRequest(
const scoped_refptr<base::SingleThreadTaskRunner>& main_thread,
const scoped_refptr<base::SingleThreadTaskRunner>& worker_thread)
: main_thread_(main_thread), worker_thread_(worker_thread) {
DCHECK(main_thread_);
DCHECK(worker_thread_);
}
void GenerateCertificateAsync(
const webrtc::KeyParams& key_params,
const std::optional<uint64_t>& expires_ms,
blink::RTCCertificateCallback completion_callback) {
DCHECK(main_thread_->BelongsToCurrentThread());
DCHECK(completion_callback);
worker_thread_->PostTask(
FROM_HERE,
base::BindOnce(
&RTCCertificateGeneratorRequest::GenerateCertificateOnWorkerThread,
this, key_params, expires_ms, std::move(completion_callback)));
}
private:
friend class WTF::ThreadSafeRefCounted<RTCCertificateGeneratorRequest>;
~RTCCertificateGeneratorRequest() {}
void GenerateCertificateOnWorkerThread(
const webrtc::KeyParams key_params,
const std::optional<uint64_t> expires_ms,
blink::RTCCertificateCallback completion_callback) {
DCHECK(worker_thread_->BelongsToCurrentThread());
webrtc::scoped_refptr<webrtc::RTCCertificate> certificate =
webrtc::RTCCertificateGenerator::GenerateCertificate(key_params,
expires_ms);
main_thread_->PostTask(
FROM_HERE,
base::BindOnce(&RTCCertificateGeneratorRequest::DoCallbackOnMainThread,
this, std::move(completion_callback), certificate));
}
void DoCallbackOnMainThread(
blink::RTCCertificateCallback completion_callback,
webrtc::scoped_refptr<webrtc::RTCCertificate> certificate) {
DCHECK(main_thread_->BelongsToCurrentThread());
DCHECK(completion_callback);
std::move(completion_callback).Run(std::move(certificate));
}
// The main thread is the renderer thread.
const scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
// The WebRTC worker thread.
const scoped_refptr<base::SingleThreadTaskRunner> worker_thread_;
};
void GenerateCertificateWithOptionalExpiration(
const webrtc::KeyParams& key_params,
const std::optional<uint64_t>& expires_ms,
blink::RTCCertificateCallback completion_callback,
ExecutionContext& context,
scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
DCHECK(key_params.IsValid());
if (context.IsContextDestroyed()) {
// If the context is destroyed we won't be able to access the
// PeerConnectionDependencyFactory. Reject the promise by returning a null
// certificate.
std::move(completion_callback).Run(nullptr);
return;
}
auto& pc_dependency_factory =
blink::PeerConnectionDependencyFactory::From(context);
pc_dependency_factory.EnsureInitialized();
scoped_refptr<RTCCertificateGeneratorRequest> request =
base::MakeRefCounted<RTCCertificateGeneratorRequest>(
task_runner, pc_dependency_factory.GetWebRtcNetworkTaskRunner());
request->GenerateCertificateAsync(key_params, expires_ms,
std::move(completion_callback));
}
} // namespace
void RTCCertificateGenerator::GenerateCertificate(
const webrtc::KeyParams& key_params,
blink::RTCCertificateCallback completion_callback,
ExecutionContext& context,
scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
GenerateCertificateWithOptionalExpiration(key_params, std::nullopt,
std::move(completion_callback),
context, task_runner);
}
void RTCCertificateGenerator::GenerateCertificateWithExpiration(
const webrtc::KeyParams& key_params,
uint64_t expires_ms,
blink::RTCCertificateCallback completion_callback,
ExecutionContext& context,
scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
GenerateCertificateWithOptionalExpiration(key_params, expires_ms,
std::move(completion_callback),
context, task_runner);
}
bool RTCCertificateGenerator::IsSupportedKeyParams(
const webrtc::KeyParams& key_params) {
return key_params.IsValid();
}
webrtc::scoped_refptr<webrtc::RTCCertificate> RTCCertificateGenerator::FromPEM(
String pem_private_key,
String pem_certificate) {
webrtc::scoped_refptr<webrtc::RTCCertificate> certificate =
webrtc::RTCCertificate::FromPEM(webrtc::RTCCertificatePEM(
pem_private_key.Utf8(), pem_certificate.Utf8()));
if (!certificate)
return nullptr;
return certificate;
}
} // namespace blink
|