rtc_certificate_generator.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #ifndef RTC_BASE_RTC_CERTIFICATE_GENERATOR_H_
  11. #define RTC_BASE_RTC_CERTIFICATE_GENERATOR_H_
  12. #include <stdint.h>
  13. #include "absl/types/optional.h"
  14. #include "api/scoped_refptr.h"
  15. #include "rtc_base/ref_count.h"
  16. #include "rtc_base/rtc_certificate.h"
  17. #include "rtc_base/ssl_identity.h"
  18. #include "rtc_base/system/rtc_export.h"
  19. #include "rtc_base/thread.h"
  20. namespace rtc {
  21. // See |RTCCertificateGeneratorInterface::GenerateCertificateAsync|.
  22. class RTCCertificateGeneratorCallback : public RefCountInterface {
  23. public:
  24. virtual void OnSuccess(const scoped_refptr<RTCCertificate>& certificate) = 0;
  25. virtual void OnFailure() = 0;
  26. protected:
  27. ~RTCCertificateGeneratorCallback() override {}
  28. };
  29. // Generates |RTCCertificate|s.
  30. // See |RTCCertificateGenerator| for the WebRTC repo's implementation.
  31. class RTCCertificateGeneratorInterface {
  32. public:
  33. virtual ~RTCCertificateGeneratorInterface() {}
  34. // Generates a certificate asynchronously on the worker thread.
  35. // Must be called on the signaling thread. The |callback| is invoked with the
  36. // result on the signaling thread. |exipres_ms| optionally specifies for how
  37. // long we want the certificate to be valid, but the implementation may choose
  38. // its own restrictions on the expiration time.
  39. virtual void GenerateCertificateAsync(
  40. const KeyParams& key_params,
  41. const absl::optional<uint64_t>& expires_ms,
  42. const scoped_refptr<RTCCertificateGeneratorCallback>& callback) = 0;
  43. };
  44. // Standard implementation of |RTCCertificateGeneratorInterface|.
  45. // The static function |GenerateCertificate| generates a certificate on the
  46. // current thread. The |RTCCertificateGenerator| instance generates certificates
  47. // asynchronously on the worker thread with |GenerateCertificateAsync|.
  48. class RTC_EXPORT RTCCertificateGenerator
  49. : public RTCCertificateGeneratorInterface {
  50. public:
  51. // Generates a certificate on the current thread. Returns null on failure.
  52. // If |expires_ms| is specified, the certificate will expire in approximately
  53. // that many milliseconds from now. |expires_ms| is limited to a year, a
  54. // larger value than that is clamped down to a year. If |expires_ms| is not
  55. // specified, a default expiration time is used.
  56. static scoped_refptr<RTCCertificate> GenerateCertificate(
  57. const KeyParams& key_params,
  58. const absl::optional<uint64_t>& expires_ms);
  59. RTCCertificateGenerator(Thread* signaling_thread, Thread* worker_thread);
  60. ~RTCCertificateGenerator() override {}
  61. // |RTCCertificateGeneratorInterface| overrides.
  62. // If |expires_ms| is specified, the certificate will expire in approximately
  63. // that many milliseconds from now. |expires_ms| is limited to a year, a
  64. // larger value than that is clamped down to a year. If |expires_ms| is not
  65. // specified, a default expiration time is used.
  66. void GenerateCertificateAsync(
  67. const KeyParams& key_params,
  68. const absl::optional<uint64_t>& expires_ms,
  69. const scoped_refptr<RTCCertificateGeneratorCallback>& callback) override;
  70. private:
  71. Thread* const signaling_thread_;
  72. Thread* const worker_thread_;
  73. };
  74. } // namespace rtc
  75. #endif // RTC_BASE_RTC_CERTIFICATE_GENERATOR_H_