ssl_adapter.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright 2004 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_SSL_ADAPTER_H_
  11. #define RTC_BASE_SSL_ADAPTER_H_
  12. #include <string>
  13. #include <vector>
  14. #include "rtc_base/async_socket.h"
  15. #include "rtc_base/ssl_certificate.h"
  16. #include "rtc_base/ssl_identity.h"
  17. #include "rtc_base/ssl_stream_adapter.h"
  18. #include "rtc_base/system/rtc_export.h"
  19. namespace rtc {
  20. class SSLAdapter;
  21. // Class for creating SSL adapters with shared state, e.g., a session cache,
  22. // which allows clients to resume SSL sessions to previously-contacted hosts.
  23. // Clients should create the factory using Create(), set up the factory as
  24. // needed using SetMode, and then call CreateAdapter to create adapters when
  25. // needed.
  26. class SSLAdapterFactory {
  27. public:
  28. virtual ~SSLAdapterFactory() {}
  29. // Specifies whether TLS or DTLS is to be used for the SSL adapters.
  30. virtual void SetMode(SSLMode mode) = 0;
  31. // Specify a custom certificate verifier for SSL.
  32. virtual void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) = 0;
  33. // Creates a new SSL adapter, but from a shared context.
  34. virtual SSLAdapter* CreateAdapter(AsyncSocket* socket) = 0;
  35. static SSLAdapterFactory* Create();
  36. };
  37. // Class that abstracts a client-to-server SSL session. It can be created
  38. // standalone, via SSLAdapter::Create, or through a factory as described above,
  39. // in which case it will share state with other SSLAdapters created from the
  40. // same factory.
  41. // After creation, call StartSSL to initiate the SSL handshake to the server.
  42. class SSLAdapter : public AsyncSocketAdapter {
  43. public:
  44. explicit SSLAdapter(AsyncSocket* socket) : AsyncSocketAdapter(socket) {}
  45. // Methods that control server certificate verification, used in unit tests.
  46. // Do not call these methods in production code.
  47. // TODO(juberti): Remove the opportunistic encryption mechanism in
  48. // BasicPacketSocketFactory that uses this function.
  49. virtual void SetIgnoreBadCert(bool ignore) = 0;
  50. virtual void SetAlpnProtocols(const std::vector<std::string>& protos) = 0;
  51. virtual void SetEllipticCurves(const std::vector<std::string>& curves) = 0;
  52. // Do DTLS or TLS (default is TLS, if unspecified)
  53. virtual void SetMode(SSLMode mode) = 0;
  54. // Specify a custom certificate verifier for SSL.
  55. virtual void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) = 0;
  56. // Set the certificate this socket will present to incoming clients.
  57. // Takes ownership of |identity|.
  58. virtual void SetIdentity(std::unique_ptr<SSLIdentity> identity) = 0;
  59. // Choose whether the socket acts as a server socket or client socket.
  60. virtual void SetRole(SSLRole role) = 0;
  61. // StartSSL returns 0 if successful.
  62. // If StartSSL is called while the socket is closed or connecting, the SSL
  63. // negotiation will begin as soon as the socket connects.
  64. virtual int StartSSL(const char* hostname) = 0;
  65. // When an SSLAdapterFactory is used, an SSLAdapter may be used to resume
  66. // a previous SSL session, which results in an abbreviated handshake.
  67. // This method, if called after SSL has been established for this adapter,
  68. // indicates whether the current session is a resumption of a previous
  69. // session.
  70. virtual bool IsResumedSession() = 0;
  71. // Create the default SSL adapter for this platform. On failure, returns null
  72. // and deletes |socket|. Otherwise, the returned SSLAdapter takes ownership
  73. // of |socket|.
  74. static SSLAdapter* Create(AsyncSocket* socket);
  75. };
  76. ///////////////////////////////////////////////////////////////////////////////
  77. // Call this on the main thread, before using SSL.
  78. // Call CleanupSSL when finished with SSL.
  79. RTC_EXPORT bool InitializeSSL();
  80. // Call to cleanup additional threads, and also the main thread.
  81. RTC_EXPORT bool CleanupSSL();
  82. } // namespace rtc
  83. #endif // RTC_BASE_SSL_ADAPTER_H_