xref: /aosp_15_r20/external/webrtc/rtc_base/ssl_adapter.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef RTC_BASE_SSL_ADAPTER_H_
12*d9f75844SAndroid Build Coastguard Worker #define RTC_BASE_SSL_ADAPTER_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <string>
15*d9f75844SAndroid Build Coastguard Worker #include <vector>
16*d9f75844SAndroid Build Coastguard Worker 
17*d9f75844SAndroid Build Coastguard Worker #include "absl/strings/string_view.h"
18*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/async_socket.h"
19*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/ssl_certificate.h"
20*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/ssl_identity.h"
21*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/ssl_stream_adapter.h"
22*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system/rtc_export.h"
23*d9f75844SAndroid Build Coastguard Worker 
24*d9f75844SAndroid Build Coastguard Worker namespace rtc {
25*d9f75844SAndroid Build Coastguard Worker 
26*d9f75844SAndroid Build Coastguard Worker class SSLAdapter;
27*d9f75844SAndroid Build Coastguard Worker 
28*d9f75844SAndroid Build Coastguard Worker // Class for creating SSL adapters with shared state, e.g., a session cache,
29*d9f75844SAndroid Build Coastguard Worker // which allows clients to resume SSL sessions to previously-contacted hosts.
30*d9f75844SAndroid Build Coastguard Worker // Clients should create the factory using Create(), set up the factory as
31*d9f75844SAndroid Build Coastguard Worker // needed using SetMode, and then call CreateAdapter to create adapters when
32*d9f75844SAndroid Build Coastguard Worker // needed.
33*d9f75844SAndroid Build Coastguard Worker class SSLAdapterFactory {
34*d9f75844SAndroid Build Coastguard Worker  public:
~SSLAdapterFactory()35*d9f75844SAndroid Build Coastguard Worker   virtual ~SSLAdapterFactory() {}
36*d9f75844SAndroid Build Coastguard Worker 
37*d9f75844SAndroid Build Coastguard Worker   // Specifies whether TLS or DTLS is to be used for the SSL adapters.
38*d9f75844SAndroid Build Coastguard Worker   virtual void SetMode(SSLMode mode) = 0;
39*d9f75844SAndroid Build Coastguard Worker 
40*d9f75844SAndroid Build Coastguard Worker   // Specify a custom certificate verifier for SSL.
41*d9f75844SAndroid Build Coastguard Worker   virtual void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) = 0;
42*d9f75844SAndroid Build Coastguard Worker 
43*d9f75844SAndroid Build Coastguard Worker   // Set the certificate this socket will present to incoming clients.
44*d9f75844SAndroid Build Coastguard Worker   // Takes ownership of `identity`.
45*d9f75844SAndroid Build Coastguard Worker   virtual void SetIdentity(std::unique_ptr<SSLIdentity> identity) = 0;
46*d9f75844SAndroid Build Coastguard Worker 
47*d9f75844SAndroid Build Coastguard Worker   // Choose whether the socket acts as a server socket or client socket.
48*d9f75844SAndroid Build Coastguard Worker   virtual void SetRole(SSLRole role) = 0;
49*d9f75844SAndroid Build Coastguard Worker 
50*d9f75844SAndroid Build Coastguard Worker   // Methods that control server certificate verification, used in unit tests.
51*d9f75844SAndroid Build Coastguard Worker   // Do not call these methods in production code.
52*d9f75844SAndroid Build Coastguard Worker   virtual void SetIgnoreBadCert(bool ignore) = 0;
53*d9f75844SAndroid Build Coastguard Worker 
54*d9f75844SAndroid Build Coastguard Worker   // Creates a new SSL adapter, but from a shared context.
55*d9f75844SAndroid Build Coastguard Worker   virtual SSLAdapter* CreateAdapter(Socket* socket) = 0;
56*d9f75844SAndroid Build Coastguard Worker 
57*d9f75844SAndroid Build Coastguard Worker   static std::unique_ptr<SSLAdapterFactory> Create();
58*d9f75844SAndroid Build Coastguard Worker };
59*d9f75844SAndroid Build Coastguard Worker 
60*d9f75844SAndroid Build Coastguard Worker // Class that abstracts a client-to-server SSL session. It can be created
61*d9f75844SAndroid Build Coastguard Worker // standalone, via SSLAdapter::Create, or through a factory as described above,
62*d9f75844SAndroid Build Coastguard Worker // in which case it will share state with other SSLAdapters created from the
63*d9f75844SAndroid Build Coastguard Worker // same factory.
64*d9f75844SAndroid Build Coastguard Worker // After creation, call StartSSL to initiate the SSL handshake to the server.
65*d9f75844SAndroid Build Coastguard Worker class SSLAdapter : public AsyncSocketAdapter {
66*d9f75844SAndroid Build Coastguard Worker  public:
SSLAdapter(Socket * socket)67*d9f75844SAndroid Build Coastguard Worker   explicit SSLAdapter(Socket* socket) : AsyncSocketAdapter(socket) {}
68*d9f75844SAndroid Build Coastguard Worker 
69*d9f75844SAndroid Build Coastguard Worker   // Methods that control server certificate verification, used in unit tests.
70*d9f75844SAndroid Build Coastguard Worker   // Do not call these methods in production code.
71*d9f75844SAndroid Build Coastguard Worker   // TODO(juberti): Remove the opportunistic encryption mechanism in
72*d9f75844SAndroid Build Coastguard Worker   // BasicPacketSocketFactory that uses this function.
73*d9f75844SAndroid Build Coastguard Worker   virtual void SetIgnoreBadCert(bool ignore) = 0;
74*d9f75844SAndroid Build Coastguard Worker 
75*d9f75844SAndroid Build Coastguard Worker   virtual void SetAlpnProtocols(const std::vector<std::string>& protos) = 0;
76*d9f75844SAndroid Build Coastguard Worker   virtual void SetEllipticCurves(const std::vector<std::string>& curves) = 0;
77*d9f75844SAndroid Build Coastguard Worker 
78*d9f75844SAndroid Build Coastguard Worker   // Do DTLS or TLS (default is TLS, if unspecified)
79*d9f75844SAndroid Build Coastguard Worker   virtual void SetMode(SSLMode mode) = 0;
80*d9f75844SAndroid Build Coastguard Worker   // Specify a custom certificate verifier for SSL.
81*d9f75844SAndroid Build Coastguard Worker   virtual void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) = 0;
82*d9f75844SAndroid Build Coastguard Worker 
83*d9f75844SAndroid Build Coastguard Worker   // Set the certificate this socket will present to incoming clients.
84*d9f75844SAndroid Build Coastguard Worker   // Takes ownership of `identity`.
85*d9f75844SAndroid Build Coastguard Worker   virtual void SetIdentity(std::unique_ptr<SSLIdentity> identity) = 0;
86*d9f75844SAndroid Build Coastguard Worker 
87*d9f75844SAndroid Build Coastguard Worker   // Choose whether the socket acts as a server socket or client socket.
88*d9f75844SAndroid Build Coastguard Worker   virtual void SetRole(SSLRole role) = 0;
89*d9f75844SAndroid Build Coastguard Worker 
90*d9f75844SAndroid Build Coastguard Worker   // StartSSL returns 0 if successful.
91*d9f75844SAndroid Build Coastguard Worker   // If StartSSL is called while the socket is closed or connecting, the SSL
92*d9f75844SAndroid Build Coastguard Worker   // negotiation will begin as soon as the socket connects.
93*d9f75844SAndroid Build Coastguard Worker   virtual int StartSSL(absl::string_view hostname) = 0;
94*d9f75844SAndroid Build Coastguard Worker 
95*d9f75844SAndroid Build Coastguard Worker   // When an SSLAdapterFactory is used, an SSLAdapter may be used to resume
96*d9f75844SAndroid Build Coastguard Worker   // a previous SSL session, which results in an abbreviated handshake.
97*d9f75844SAndroid Build Coastguard Worker   // This method, if called after SSL has been established for this adapter,
98*d9f75844SAndroid Build Coastguard Worker   // indicates whether the current session is a resumption of a previous
99*d9f75844SAndroid Build Coastguard Worker   // session.
100*d9f75844SAndroid Build Coastguard Worker   virtual bool IsResumedSession() = 0;
101*d9f75844SAndroid Build Coastguard Worker 
102*d9f75844SAndroid Build Coastguard Worker   // Create the default SSL adapter for this platform. On failure, returns null
103*d9f75844SAndroid Build Coastguard Worker   // and deletes `socket`. Otherwise, the returned SSLAdapter takes ownership
104*d9f75844SAndroid Build Coastguard Worker   // of `socket`.
105*d9f75844SAndroid Build Coastguard Worker   static SSLAdapter* Create(Socket* socket);
106*d9f75844SAndroid Build Coastguard Worker 
107*d9f75844SAndroid Build Coastguard Worker  private:
108*d9f75844SAndroid Build Coastguard Worker   // Not supported.
Listen(int backlog)109*d9f75844SAndroid Build Coastguard Worker   int Listen(int backlog) override { RTC_CHECK(false); }
Accept(SocketAddress * paddr)110*d9f75844SAndroid Build Coastguard Worker   Socket* Accept(SocketAddress* paddr) override { RTC_CHECK(false); }
111*d9f75844SAndroid Build Coastguard Worker };
112*d9f75844SAndroid Build Coastguard Worker 
113*d9f75844SAndroid Build Coastguard Worker ///////////////////////////////////////////////////////////////////////////////
114*d9f75844SAndroid Build Coastguard Worker 
115*d9f75844SAndroid Build Coastguard Worker // Call this on the main thread, before using SSL.
116*d9f75844SAndroid Build Coastguard Worker // Call CleanupSSL when finished with SSL.
117*d9f75844SAndroid Build Coastguard Worker RTC_EXPORT bool InitializeSSL();
118*d9f75844SAndroid Build Coastguard Worker 
119*d9f75844SAndroid Build Coastguard Worker // Call to cleanup additional threads, and also the main thread.
120*d9f75844SAndroid Build Coastguard Worker RTC_EXPORT bool CleanupSSL();
121*d9f75844SAndroid Build Coastguard Worker 
122*d9f75844SAndroid Build Coastguard Worker }  // namespace rtc
123*d9f75844SAndroid Build Coastguard Worker 
124*d9f75844SAndroid Build Coastguard Worker #endif  // RTC_BASE_SSL_ADAPTER_H_
125