blob: a881f1a369a43095e7b2ac05c57db8c001d22d5b [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#ifndef RTC_BASE_RTC_CERTIFICATE_GENERATOR_H_
12#define RTC_BASE_RTC_CERTIFICATE_GENERATOR_H_
Henrik Boströmda3a1da2016-04-15 17:55:21 +020013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stdint.h>
15
Danil Chapovalovb7da8162022-08-22 16:39:34 +020016#include "absl/functional/any_invocable.h"
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020017#include "absl/types/optional.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010018#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/rtc_certificate.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/ssl_identity.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020021#include "rtc_base/system/rtc_export.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/thread.h"
Henrik Boströmda3a1da2016-04-15 17:55:21 +020023
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024namespace rtc {
25
Artem Titov96e3b992021-07-26 16:03:14 +020026// Generates `RTCCertificate`s.
27// See `RTCCertificateGenerator` for the WebRTC repo's implementation.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020028class RTCCertificateGeneratorInterface {
29 public:
Danil Chapovalovb7da8162022-08-22 16:39:34 +020030 // Functor that will be called when certificate is generated asynchroniosly.
31 // Called with nullptr as the parameter on failure.
32 using Callback = absl::AnyInvocable<void(scoped_refptr<RTCCertificate>) &&>;
33
34 virtual ~RTCCertificateGeneratorInterface() = default;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020035
36 // Generates a certificate asynchronously on the worker thread.
Artem Titov96e3b992021-07-26 16:03:14 +020037 // Must be called on the signaling thread. The `callback` is invoked with the
38 // result on the signaling thread. `exipres_ms` optionally specifies for how
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020039 // long we want the certificate to be valid, but the implementation may choose
40 // its own restrictions on the expiration time.
41 virtual void GenerateCertificateAsync(
42 const KeyParams& key_params,
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020043 const absl::optional<uint64_t>& expires_ms,
Danil Chapovalovb7da8162022-08-22 16:39:34 +020044 Callback callback) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020045};
46
Artem Titov96e3b992021-07-26 16:03:14 +020047// Standard implementation of `RTCCertificateGeneratorInterface`.
48// The static function `GenerateCertificate` generates a certificate on the
49// current thread. The `RTCCertificateGenerator` instance generates certificates
50// asynchronously on the worker thread with `GenerateCertificateAsync`.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020051class RTC_EXPORT RTCCertificateGenerator
52 : public RTCCertificateGeneratorInterface {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020053 public:
54 // Generates a certificate on the current thread. Returns null on failure.
Artem Titov96e3b992021-07-26 16:03:14 +020055 // If `expires_ms` is specified, the certificate will expire in approximately
56 // that many milliseconds from now. `expires_ms` is limited to a year, a
57 // larger value than that is clamped down to a year. If `expires_ms` is not
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020058 // specified, a default expiration time is used.
59 static scoped_refptr<RTCCertificate> GenerateCertificate(
60 const KeyParams& key_params,
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020061 const absl::optional<uint64_t>& expires_ms);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062
63 RTCCertificateGenerator(Thread* signaling_thread, Thread* worker_thread);
64 ~RTCCertificateGenerator() override {}
65
Artem Titov96e3b992021-07-26 16:03:14 +020066 // `RTCCertificateGeneratorInterface` overrides.
67 // If `expires_ms` is specified, the certificate will expire in approximately
68 // that many milliseconds from now. `expires_ms` is limited to a year, a
69 // larger value than that is clamped down to a year. If `expires_ms` is not
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020070 // specified, a default expiration time is used.
Danil Chapovalovb7da8162022-08-22 16:39:34 +020071 void GenerateCertificateAsync(const KeyParams& key_params,
72 const absl::optional<uint64_t>& expires_ms,
73 Callback callback) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020074
75 private:
76 Thread* const signaling_thread_;
77 Thread* const worker_thread_;
78};
79
80} // namespace rtc
Henrik Boströmda3a1da2016-04-15 17:55:21 +020081
Steve Anton10542f22019-01-11 09:11:00 -080082#endif // RTC_BASE_RTC_CERTIFICATE_GENERATOR_H_