Henrik Boström | da3a1da | 2016-04-15 15:55:21 | [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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_RTC_CERTIFICATE_GENERATOR_H_ |
| 12 | #define RTC_BASE_RTC_CERTIFICATE_GENERATOR_H_ |
Henrik Boström | da3a1da | 2016-04-15 15:55:21 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | |
Danil Chapovalov | 0a1d189 | 2018-06-21 09:48:25 | [diff] [blame] | 16 | #include "absl/types/optional.h" |
Mirko Bonadei | d970807 | 2019-01-25 19:26:48 | [diff] [blame] | 17 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 18 | #include "rtc_base/ref_count.h" |
| 19 | #include "rtc_base/rtc_certificate.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 20 | #include "rtc_base/ssl_identity.h" |
Mirko Bonadei | 35214fc | 2019-09-23 12:54:28 | [diff] [blame] | 21 | #include "rtc_base/system/rtc_export.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 22 | #include "rtc_base/thread.h" |
Henrik Boström | da3a1da | 2016-04-15 15:55:21 | [diff] [blame] | 23 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 24 | namespace rtc { |
| 25 | |
| 26 | // See |RTCCertificateGeneratorInterface::GenerateCertificateAsync|. |
| 27 | class RTCCertificateGeneratorCallback : public RefCountInterface { |
| 28 | public: |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 29 | virtual void OnSuccess(const scoped_refptr<RTCCertificate>& certificate) = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 30 | virtual void OnFailure() = 0; |
| 31 | |
| 32 | protected: |
| 33 | ~RTCCertificateGeneratorCallback() override {} |
| 34 | }; |
| 35 | |
| 36 | // Generates |RTCCertificate|s. |
| 37 | // See |RTCCertificateGenerator| for the WebRTC repo's implementation. |
| 38 | class RTCCertificateGeneratorInterface { |
| 39 | public: |
| 40 | virtual ~RTCCertificateGeneratorInterface() {} |
| 41 | |
| 42 | // Generates a certificate asynchronously on the worker thread. |
| 43 | // Must be called on the signaling thread. The |callback| is invoked with the |
| 44 | // result on the signaling thread. |exipres_ms| optionally specifies for how |
| 45 | // long we want the certificate to be valid, but the implementation may choose |
| 46 | // its own restrictions on the expiration time. |
| 47 | virtual void GenerateCertificateAsync( |
| 48 | const KeyParams& key_params, |
Danil Chapovalov | 0a1d189 | 2018-06-21 09:48:25 | [diff] [blame] | 49 | const absl::optional<uint64_t>& expires_ms, |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 50 | const scoped_refptr<RTCCertificateGeneratorCallback>& callback) = 0; |
| 51 | }; |
| 52 | |
| 53 | // Standard implementation of |RTCCertificateGeneratorInterface|. |
| 54 | // The static function |GenerateCertificate| generates a certificate on the |
| 55 | // current thread. The |RTCCertificateGenerator| instance generates certificates |
| 56 | // asynchronously on the worker thread with |GenerateCertificateAsync|. |
Mirko Bonadei | 35214fc | 2019-09-23 12:54:28 | [diff] [blame] | 57 | class RTC_EXPORT RTCCertificateGenerator |
| 58 | : public RTCCertificateGeneratorInterface { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 59 | public: |
| 60 | // Generates a certificate on the current thread. Returns null on failure. |
| 61 | // If |expires_ms| is specified, the certificate will expire in approximately |
| 62 | // that many milliseconds from now. |expires_ms| is limited to a year, a |
| 63 | // larger value than that is clamped down to a year. If |expires_ms| is not |
| 64 | // specified, a default expiration time is used. |
| 65 | static scoped_refptr<RTCCertificate> GenerateCertificate( |
| 66 | const KeyParams& key_params, |
Danil Chapovalov | 0a1d189 | 2018-06-21 09:48:25 | [diff] [blame] | 67 | const absl::optional<uint64_t>& expires_ms); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 68 | |
| 69 | RTCCertificateGenerator(Thread* signaling_thread, Thread* worker_thread); |
| 70 | ~RTCCertificateGenerator() override {} |
| 71 | |
| 72 | // |RTCCertificateGeneratorInterface| overrides. |
| 73 | // If |expires_ms| is specified, the certificate will expire in approximately |
| 74 | // that many milliseconds from now. |expires_ms| is limited to a year, a |
| 75 | // larger value than that is clamped down to a year. If |expires_ms| is not |
| 76 | // specified, a default expiration time is used. |
| 77 | void GenerateCertificateAsync( |
| 78 | const KeyParams& key_params, |
Danil Chapovalov | 0a1d189 | 2018-06-21 09:48:25 | [diff] [blame] | 79 | const absl::optional<uint64_t>& expires_ms, |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 80 | const scoped_refptr<RTCCertificateGeneratorCallback>& callback) override; |
| 81 | |
| 82 | private: |
| 83 | Thread* const signaling_thread_; |
| 84 | Thread* const worker_thread_; |
| 85 | }; |
| 86 | |
| 87 | } // namespace rtc |
Henrik Boström | da3a1da | 2016-04-15 15:55:21 | [diff] [blame] | 88 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 89 | #endif // RTC_BASE_RTC_CERTIFICATE_GENERATOR_H_ |