blob: 8cabca46418dd1ee6cab8744cd965d231bee16e2 [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 Chapovalov0a1d1892018-06-21 11:48:25 +020016#include "absl/types/optional.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010017#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/ref_count.h"
19#include "rtc_base/rtc_certificate.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/ssl_identity.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/thread.h"
Henrik Boströmda3a1da2016-04-15 17:55:21 +020022
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023namespace rtc {
24
25// See |RTCCertificateGeneratorInterface::GenerateCertificateAsync|.
26class RTCCertificateGeneratorCallback : public RefCountInterface {
27 public:
Yves Gerey665174f2018-06-19 15:03:05 +020028 virtual void OnSuccess(const scoped_refptr<RTCCertificate>& certificate) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029 virtual void OnFailure() = 0;
30
31 protected:
32 ~RTCCertificateGeneratorCallback() override {}
33};
34
35// Generates |RTCCertificate|s.
36// See |RTCCertificateGenerator| for the WebRTC repo's implementation.
37class RTCCertificateGeneratorInterface {
38 public:
39 virtual ~RTCCertificateGeneratorInterface() {}
40
41 // Generates a certificate asynchronously on the worker thread.
42 // Must be called on the signaling thread. The |callback| is invoked with the
43 // result on the signaling thread. |exipres_ms| optionally specifies for how
44 // long we want the certificate to be valid, but the implementation may choose
45 // its own restrictions on the expiration time.
46 virtual void GenerateCertificateAsync(
47 const KeyParams& key_params,
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020048 const absl::optional<uint64_t>& expires_ms,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049 const scoped_refptr<RTCCertificateGeneratorCallback>& callback) = 0;
50};
51
52// Standard implementation of |RTCCertificateGeneratorInterface|.
53// The static function |GenerateCertificate| generates a certificate on the
54// current thread. The |RTCCertificateGenerator| instance generates certificates
55// asynchronously on the worker thread with |GenerateCertificateAsync|.
56class RTCCertificateGenerator : public RTCCertificateGeneratorInterface {
57 public:
58 // Generates a certificate on the current thread. Returns null on failure.
59 // If |expires_ms| is specified, the certificate will expire in approximately
60 // that many milliseconds from now. |expires_ms| is limited to a year, a
61 // larger value than that is clamped down to a year. If |expires_ms| is not
62 // specified, a default expiration time is used.
63 static scoped_refptr<RTCCertificate> GenerateCertificate(
64 const KeyParams& key_params,
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020065 const absl::optional<uint64_t>& expires_ms);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020066
67 RTCCertificateGenerator(Thread* signaling_thread, Thread* worker_thread);
68 ~RTCCertificateGenerator() override {}
69
70 // |RTCCertificateGeneratorInterface| overrides.
71 // If |expires_ms| is specified, the certificate will expire in approximately
72 // that many milliseconds from now. |expires_ms| is limited to a year, a
73 // larger value than that is clamped down to a year. If |expires_ms| is not
74 // specified, a default expiration time is used.
75 void GenerateCertificateAsync(
76 const KeyParams& key_params,
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020077 const absl::optional<uint64_t>& expires_ms,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020078 const scoped_refptr<RTCCertificateGeneratorCallback>& callback) override;
79
80 private:
81 Thread* const signaling_thread_;
82 Thread* const worker_thread_;
83};
84
85} // namespace rtc
Henrik Boströmda3a1da2016-04-15 17:55:21 +020086
Steve Anton10542f22019-01-11 09:11:00 -080087#endif // RTC_BASE_RTC_CERTIFICATE_GENERATOR_H_