blob: 739890e2b8dd3db8d04e57477c235c639f8bf1df [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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "rtc_base/rtc_certificate_generator.h"
Henrik Boströmda3a1da2016-04-15 17:55:21 +020012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <time.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Henrik Boströmda3a1da2016-04-15 17:55:21 +020015#include <algorithm>
jbauch555604a2016-04-26 03:13:22 -070016#include <memory>
Yves Gerey988cc082018-10-23 12:03:01 +020017#include <utility>
Henrik Boströmda3a1da2016-04-15 17:55:21 +020018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "rtc_base/location.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/message_handler.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/ssl_identity.h"
Henrik Boströmda3a1da2016-04-15 17:55:21 +020023
24namespace rtc {
25
26namespace {
27
28// A certificates' subject and issuer name.
29const char kIdentityName[] = "WebRTC";
agrieve26622d32017-08-08 10:48:15 -070030const uint64_t kYearInSeconds = 365 * 24 * 60 * 60;
Henrik Boströmda3a1da2016-04-15 17:55:21 +020031
Henrik Boströmda3a1da2016-04-15 17:55:21 +020032} // namespace
33
34// static
Yves Gerey665174f2018-06-19 15:03:05 +020035scoped_refptr<RTCCertificate> RTCCertificateGenerator::GenerateCertificate(
Henrik Boströmda3a1da2016-04-15 17:55:21 +020036 const KeyParams& key_params,
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020037 const absl::optional<uint64_t>& expires_ms) {
Benjamin Wright6c6c9df2018-10-25 01:16:26 -070038 if (!key_params.IsValid()) {
Henrik Boströmda3a1da2016-04-15 17:55:21 +020039 return nullptr;
Benjamin Wright6c6c9df2018-10-25 01:16:26 -070040 }
41
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010042 std::unique_ptr<SSLIdentity> identity;
Henrik Boströmda3a1da2016-04-15 17:55:21 +020043 if (!expires_ms) {
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010044 identity = SSLIdentity::Create(kIdentityName, key_params);
Henrik Boströmda3a1da2016-04-15 17:55:21 +020045 } else {
46 uint64_t expires_s = *expires_ms / 1000;
47 // Limit the expiration time to something reasonable (a year). This was
48 // somewhat arbitrarily chosen. It also ensures that the value is not too
Artem Titov96e3b992021-07-26 16:03:14 +020049 // large for the unspecified `time_t`.
Henrik Boströmda3a1da2016-04-15 17:55:21 +020050 expires_s = std::min(expires_s, kYearInSeconds);
Artem Titov96e3b992021-07-26 16:03:14 +020051 // TODO(torbjorng): Stop using `time_t`, its type is unspecified. It it safe
Henrik Boströmda3a1da2016-04-15 17:55:21 +020052 // to assume it can hold up to a year's worth of seconds (and more), but
Artem Titovcfea2182021-08-10 01:22:31 +020053 // `SSLIdentity::Create` should stop relying on `time_t`.
Henrik Boströmda3a1da2016-04-15 17:55:21 +020054 // See bugs.webrtc.org/5720.
55 time_t cert_lifetime_s = static_cast<time_t>(expires_s);
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010056 identity = SSLIdentity::Create(kIdentityName, key_params, cert_lifetime_s);
Henrik Boströmda3a1da2016-04-15 17:55:21 +020057 }
Benjamin Wright6c6c9df2018-10-25 01:16:26 -070058 if (!identity) {
Henrik Boströmda3a1da2016-04-15 17:55:21 +020059 return nullptr;
Benjamin Wright6c6c9df2018-10-25 01:16:26 -070060 }
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010061 return RTCCertificate::Create(std::move(identity));
Henrik Boströmda3a1da2016-04-15 17:55:21 +020062}
63
Yves Gerey665174f2018-06-19 15:03:05 +020064RTCCertificateGenerator::RTCCertificateGenerator(Thread* signaling_thread,
65 Thread* worker_thread)
66 : signaling_thread_(signaling_thread), worker_thread_(worker_thread) {
Henrik Boströmda3a1da2016-04-15 17:55:21 +020067 RTC_DCHECK(signaling_thread_);
68 RTC_DCHECK(worker_thread_);
69}
70
71void RTCCertificateGenerator::GenerateCertificateAsync(
72 const KeyParams& key_params,
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020073 const absl::optional<uint64_t>& expires_ms,
Danil Chapovalovb7da8162022-08-22 16:39:34 +020074 RTCCertificateGenerator::Callback callback) {
Henrik Boströmda3a1da2016-04-15 17:55:21 +020075 RTC_DCHECK(signaling_thread_->IsCurrent());
76 RTC_DCHECK(callback);
77
Henrik Boström2deee4b2022-01-20 11:58:05 +010078 worker_thread_->PostTask([key_params, expires_ms,
79 signaling_thread = signaling_thread_,
Danil Chapovalovb7da8162022-08-22 16:39:34 +020080 cb = std::move(callback)]() mutable {
Tomas Gunnarsson295570e2020-09-12 15:47:36 +020081 scoped_refptr<RTCCertificate> certificate =
82 RTCCertificateGenerator::GenerateCertificate(key_params, expires_ms);
83 signaling_thread->PostTask(
Danil Chapovalovb7da8162022-08-22 16:39:34 +020084 [cert = std::move(certificate), cb = std::move(cb)]() mutable {
85 std::move(cb)(std::move(cert));
Tomas Gunnarsson295570e2020-09-12 15:47:36 +020086 });
87 });
Henrik Boströmda3a1da2016-04-15 17:55:21 +020088}
89
90} // namespace rtc