blob: cacff61e35d13e6ef1dcaffd624fe6669af5846b [file] [log] [blame]
Henrik Boströmda3a1da2016-04-15 17:55:21 +02001/*
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/rtccertificategenerator.h"
Henrik Boströmda3a1da2016-04-15 17:55:21 +020012
13#include <algorithm>
jbauch555604a2016-04-26 03:13:22 -070014#include <memory>
Henrik Boströmda3a1da2016-04-15 17:55:21 +020015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
Niels Möller84255bb2017-10-06 13:43:23 +020017#include "rtc_base/refcountedobject.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/sslidentity.h"
Henrik Boströmda3a1da2016-04-15 17:55:21 +020019
20namespace rtc {
21
22namespace {
23
24// A certificates' subject and issuer name.
25const char kIdentityName[] = "WebRTC";
26
agrieve26622d32017-08-08 10:48:15 -070027const uint64_t kYearInSeconds = 365 * 24 * 60 * 60;
Henrik Boströmda3a1da2016-04-15 17:55:21 +020028
29enum {
30 MSG_GENERATE,
31 MSG_GENERATE_DONE,
32};
33
34// Helper class for generating certificates asynchronously; a single task
35// instance is responsible for a single asynchronous certificate generation
36// request. We are using a separate helper class so that a generation request
37// can outlive the |RTCCertificateGenerator| that spawned it.
38class RTCCertificateGenerationTask : public RefCountInterface,
39 public MessageHandler {
40 public:
41 RTCCertificateGenerationTask(
42 Thread* signaling_thread,
43 Thread* worker_thread,
44 const KeyParams& key_params,
45 const Optional<uint64_t>& expires_ms,
46 const scoped_refptr<RTCCertificateGeneratorCallback>& callback)
47 : signaling_thread_(signaling_thread),
48 worker_thread_(worker_thread),
49 key_params_(key_params),
50 expires_ms_(expires_ms),
51 callback_(callback) {
52 RTC_DCHECK(signaling_thread_);
53 RTC_DCHECK(worker_thread_);
54 RTC_DCHECK(callback_);
55 }
56 ~RTCCertificateGenerationTask() override {}
57
58 // Handles |MSG_GENERATE| and its follow-up |MSG_GENERATE_DONE|.
59 void OnMessage(Message* msg) override {
60 switch (msg->message_id) {
61 case MSG_GENERATE:
62 RTC_DCHECK(worker_thread_->IsCurrent());
63
64 // Perform the certificate generation work here on the worker thread.
65 certificate_ = RTCCertificateGenerator::GenerateCertificate(
66 key_params_, expires_ms_);
67
68 // Handle callbacks on signaling thread. Pass on the |msg->pdata|
69 // (which references |this| with ref counting) to that thread.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070070 signaling_thread_->Post(RTC_FROM_HERE, this, MSG_GENERATE_DONE,
71 msg->pdata);
Henrik Boströmda3a1da2016-04-15 17:55:21 +020072 break;
73 case MSG_GENERATE_DONE:
74 RTC_DCHECK(signaling_thread_->IsCurrent());
75
76 // Perform callback with result here on the signaling thread.
77 if (certificate_) {
78 callback_->OnSuccess(certificate_);
79 } else {
80 callback_->OnFailure();
81 }
82
83 // Destroy |msg->pdata| which references |this| with ref counting. This
84 // may result in |this| being deleted - do not touch member variables
85 // after this line.
86 delete msg->pdata;
87 return;
88 default:
89 RTC_NOTREACHED();
90 }
91 }
92
93 private:
94 Thread* const signaling_thread_;
95 Thread* const worker_thread_;
96 const KeyParams key_params_;
97 const Optional<uint64_t> expires_ms_;
98 const scoped_refptr<RTCCertificateGeneratorCallback> callback_;
99 scoped_refptr<RTCCertificate> certificate_;
100};
101
102} // namespace
103
104// static
105scoped_refptr<RTCCertificate>
106RTCCertificateGenerator::GenerateCertificate(
107 const KeyParams& key_params,
108 const Optional<uint64_t>& expires_ms) {
109 if (!key_params.IsValid())
110 return nullptr;
111 SSLIdentity* identity;
112 if (!expires_ms) {
113 identity = SSLIdentity::Generate(kIdentityName, key_params);
114 } else {
115 uint64_t expires_s = *expires_ms / 1000;
116 // Limit the expiration time to something reasonable (a year). This was
117 // somewhat arbitrarily chosen. It also ensures that the value is not too
118 // large for the unspecified |time_t|.
119 expires_s = std::min(expires_s, kYearInSeconds);
120 // TODO(torbjorng): Stop using |time_t|, its type is unspecified. It it safe
121 // to assume it can hold up to a year's worth of seconds (and more), but
122 // |SSLIdentity::Generate| should stop relying on |time_t|.
123 // See bugs.webrtc.org/5720.
124 time_t cert_lifetime_s = static_cast<time_t>(expires_s);
125 identity = SSLIdentity::GenerateWithExpiration(
126 kIdentityName, key_params, cert_lifetime_s);
127 }
128 if (!identity)
129 return nullptr;
jbauch555604a2016-04-26 03:13:22 -0700130 std::unique_ptr<SSLIdentity> identity_sptr(identity);
Henrik Boströmda3a1da2016-04-15 17:55:21 +0200131 return RTCCertificate::Create(std::move(identity_sptr));
132}
133
134RTCCertificateGenerator::RTCCertificateGenerator(
135 Thread* signaling_thread, Thread* worker_thread)
136 : signaling_thread_(signaling_thread),
137 worker_thread_(worker_thread) {
138 RTC_DCHECK(signaling_thread_);
139 RTC_DCHECK(worker_thread_);
140}
141
142void RTCCertificateGenerator::GenerateCertificateAsync(
143 const KeyParams& key_params,
144 const Optional<uint64_t>& expires_ms,
145 const scoped_refptr<RTCCertificateGeneratorCallback>& callback) {
146 RTC_DCHECK(signaling_thread_->IsCurrent());
147 RTC_DCHECK(callback);
148
149 // Create a new |RTCCertificateGenerationTask| for this generation request. It
150 // is reference counted and referenced by the message data, ensuring it lives
151 // until the task has completed (independent of |RTCCertificateGenerator|).
152 ScopedRefMessageData<RTCCertificateGenerationTask>* msg_data =
153 new ScopedRefMessageData<RTCCertificateGenerationTask>(
154 new RefCountedObject<RTCCertificateGenerationTask>(
155 signaling_thread_, worker_thread_, key_params, expires_ms,
156 callback));
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700157 worker_thread_->Post(RTC_FROM_HERE, msg_data->data().get(), MSG_GENERATE,
158 msg_data);
Henrik Boströmda3a1da2016-04-15 17:55:21 +0200159}
160
161} // namespace rtc