blob: 4b8b9c74e0f8f94dfa6d94ae76c12f9d364a2e46 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_SSL_ADAPTER_H_
12#define RTC_BASE_SSL_ADAPTER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070014#include <string>
15#include <vector>
16
Ali Tofigh2ab914c2022-04-13 12:55:15 +020017#include "absl/strings/string_view.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/async_socket.h"
19#include "rtc_base/ssl_certificate.h"
20#include "rtc_base/ssl_identity.h"
21#include "rtc_base/ssl_stream_adapter.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020022#include "rtc_base/system/rtc_export.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024namespace rtc {
25
Justin Uberti1d445502017-08-14 17:04:34 -070026class SSLAdapter;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020027
Justin Uberti1d445502017-08-14 17:04:34 -070028// Class for creating SSL adapters with shared state, e.g., a session cache,
29// which allows clients to resume SSL sessions to previously-contacted hosts.
30// Clients should create the factory using Create(), set up the factory as
31// needed using SetMode, and then call CreateAdapter to create adapters when
32// needed.
33class SSLAdapterFactory {
34 public:
35 virtual ~SSLAdapterFactory() {}
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070036
Justin Uberti1d445502017-08-14 17:04:34 -070037 // Specifies whether TLS or DTLS is to be used for the SSL adapters.
38 virtual void SetMode(SSLMode mode) = 0;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070039
40 // Specify a custom certificate verifier for SSL.
41 virtual void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) = 0;
42
Niels Möllerac9a2882021-10-20 15:25:09 +020043 // Set the certificate this socket will present to incoming clients.
44 // Takes ownership of `identity`.
45 virtual void SetIdentity(std::unique_ptr<SSLIdentity> identity) = 0;
46
47 // Choose whether the socket acts as a server socket or client socket.
48 virtual void SetRole(SSLRole role) = 0;
49
50 // Methods that control server certificate verification, used in unit tests.
51 // Do not call these methods in production code.
52 virtual void SetIgnoreBadCert(bool ignore) = 0;
53
Justin Uberti1d445502017-08-14 17:04:34 -070054 // Creates a new SSL adapter, but from a shared context.
Niels Möllerd0b88792021-08-12 10:32:30 +020055 virtual SSLAdapter* CreateAdapter(Socket* socket) = 0;
Justin Uberti1d445502017-08-14 17:04:34 -070056
Niels Möllerac9a2882021-10-20 15:25:09 +020057 static std::unique_ptr<SSLAdapterFactory> Create();
Justin Uberti1d445502017-08-14 17:04:34 -070058};
59
60// Class that abstracts a client-to-server SSL session. It can be created
61// standalone, via SSLAdapter::Create, or through a factory as described above,
62// in which case it will share state with other SSLAdapters created from the
63// same factory.
64// After creation, call StartSSL to initiate the SSL handshake to the server.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020065class SSLAdapter : public AsyncSocketAdapter {
66 public:
Niels Möllerd0b88792021-08-12 10:32:30 +020067 explicit SSLAdapter(Socket* socket) : AsyncSocketAdapter(socket) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068
Sergey Silkin9c147dd2018-09-12 10:45:38 +000069 // Methods that control server certificate verification, used in unit tests.
70 // Do not call these methods in production code.
71 // TODO(juberti): Remove the opportunistic encryption mechanism in
72 // BasicPacketSocketFactory that uses this function.
73 virtual void SetIgnoreBadCert(bool ignore) = 0;
74
75 virtual void SetAlpnProtocols(const std::vector<std::string>& protos) = 0;
76 virtual void SetEllipticCurves(const std::vector<std::string>& curves) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020077
78 // Do DTLS or TLS (default is TLS, if unspecified)
79 virtual void SetMode(SSLMode mode) = 0;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070080 // Specify a custom certificate verifier for SSL.
81 virtual void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020082
Steve Anton786de702017-08-17 15:15:46 -070083 // Set the certificate this socket will present to incoming clients.
Artem Titov96e3b992021-07-26 16:03:14 +020084 // Takes ownership of `identity`.
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010085 virtual void SetIdentity(std::unique_ptr<SSLIdentity> identity) = 0;
Steve Anton786de702017-08-17 15:15:46 -070086
87 // Choose whether the socket acts as a server socket or client socket.
88 virtual void SetRole(SSLRole role) = 0;
89
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020090 // StartSSL returns 0 if successful.
91 // If StartSSL is called while the socket is closed or connecting, the SSL
92 // negotiation will begin as soon as the socket connects.
Ali Tofigh2ab914c2022-04-13 12:55:15 +020093 virtual int StartSSL(absl::string_view hostname) = 0;
Justin Uberti1d445502017-08-14 17:04:34 -070094
95 // When an SSLAdapterFactory is used, an SSLAdapter may be used to resume
96 // a previous SSL session, which results in an abbreviated handshake.
97 // This method, if called after SSL has been established for this adapter,
98 // indicates whether the current session is a resumption of a previous
99 // session.
100 virtual bool IsResumedSession() = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200101
102 // Create the default SSL adapter for this platform. On failure, returns null
Artem Titov96e3b992021-07-26 16:03:14 +0200103 // and deletes `socket`. Otherwise, the returned SSLAdapter takes ownership
104 // of `socket`.
Niels Möllerd0b88792021-08-12 10:32:30 +0200105 static SSLAdapter* Create(Socket* socket);
Niels Möllerac9a2882021-10-20 15:25:09 +0200106
107 private:
108 // Not supported.
109 int Listen(int backlog) override { RTC_CHECK(false); }
110 Socket* Accept(SocketAddress* paddr) override { RTC_CHECK(false); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200111};
112
113///////////////////////////////////////////////////////////////////////////////
114
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200115// Call this on the main thread, before using SSL.
Jiawei Oueb0df082018-02-02 14:51:18 -0800116// Call CleanupSSL when finished with SSL.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200117RTC_EXPORT bool InitializeSSL();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200118
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200119// Call to cleanup additional threads, and also the main thread.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200120RTC_EXPORT bool CleanupSSL();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200121
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200122} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123
Steve Anton10542f22019-01-11 09:11:00 -0800124#endif // RTC_BASE_SSL_ADAPTER_H_