Henrik Boström | da3a1da | 2016-04-15 17:55:21 +0200 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/rtccertificategenerator.h" |
Henrik Boström | da3a1da | 2016-04-15 17:55:21 +0200 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 14 | #include <memory> |
Henrik Boström | da3a1da | 2016-04-15 17:55:21 +0200 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
Niels Möller | 84255bb | 2017-10-06 13:43:23 +0200 | [diff] [blame] | 17 | #include "rtc_base/refcountedobject.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/sslidentity.h" |
Henrik Boström | da3a1da | 2016-04-15 17:55:21 +0200 | [diff] [blame] | 19 | |
| 20 | namespace rtc { |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | // A certificates' subject and issuer name. |
| 25 | const char kIdentityName[] = "WebRTC"; |
| 26 | |
agrieve | 26622d3 | 2017-08-08 10:48:15 -0700 | [diff] [blame] | 27 | const uint64_t kYearInSeconds = 365 * 24 * 60 * 60; |
Henrik Boström | da3a1da | 2016-04-15 17:55:21 +0200 | [diff] [blame] | 28 | |
| 29 | enum { |
| 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. |
| 38 | class 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 Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 70 | signaling_thread_->Post(RTC_FROM_HERE, this, MSG_GENERATE_DONE, |
| 71 | msg->pdata); |
Henrik Boström | da3a1da | 2016-04-15 17:55:21 +0200 | [diff] [blame] | 72 | 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 |
| 105 | scoped_refptr<RTCCertificate> |
| 106 | RTCCertificateGenerator::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; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 130 | std::unique_ptr<SSLIdentity> identity_sptr(identity); |
Henrik Boström | da3a1da | 2016-04-15 17:55:21 +0200 | [diff] [blame] | 131 | return RTCCertificate::Create(std::move(identity_sptr)); |
| 132 | } |
| 133 | |
| 134 | RTCCertificateGenerator::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 | |
| 142 | void 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 Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 157 | worker_thread_->Post(RTC_FROM_HERE, msg_data->data().get(), MSG_GENERATE, |
| 158 | msg_data); |
Henrik Boström | da3a1da | 2016-04-15 17:55:21 +0200 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | } // namespace rtc |