blob: 240899f55149564d80b860b4401d97376bbc07ce [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
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/async_socket.h"
18#include "rtc_base/ssl_certificate.h"
19#include "rtc_base/ssl_identity.h"
20#include "rtc_base/ssl_stream_adapter.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022namespace rtc {
23
Justin Uberti1d445502017-08-14 17:04:34 -070024class SSLAdapter;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025
Justin Uberti1d445502017-08-14 17:04:34 -070026// Class for creating SSL adapters with shared state, e.g., a session cache,
27// which allows clients to resume SSL sessions to previously-contacted hosts.
28// Clients should create the factory using Create(), set up the factory as
29// needed using SetMode, and then call CreateAdapter to create adapters when
30// needed.
31class SSLAdapterFactory {
32 public:
33 virtual ~SSLAdapterFactory() {}
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070034
Justin Uberti1d445502017-08-14 17:04:34 -070035 // Specifies whether TLS or DTLS is to be used for the SSL adapters.
36 virtual void SetMode(SSLMode mode) = 0;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070037
38 // Specify a custom certificate verifier for SSL.
39 virtual void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) = 0;
40
Justin Uberti1d445502017-08-14 17:04:34 -070041 // Creates a new SSL adapter, but from a shared context.
42 virtual SSLAdapter* CreateAdapter(AsyncSocket* socket) = 0;
43
44 static SSLAdapterFactory* Create();
45};
46
47// Class that abstracts a client-to-server SSL session. It can be created
48// standalone, via SSLAdapter::Create, or through a factory as described above,
49// in which case it will share state with other SSLAdapters created from the
50// same factory.
51// After creation, call StartSSL to initiate the SSL handshake to the server.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020052class SSLAdapter : public AsyncSocketAdapter {
53 public:
Justin Uberti1d445502017-08-14 17:04:34 -070054 explicit SSLAdapter(AsyncSocket* socket) : AsyncSocketAdapter(socket) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055
Sergey Silkin9c147dd2018-09-12 10:45:38 +000056 // Methods that control server certificate verification, used in unit tests.
57 // Do not call these methods in production code.
58 // TODO(juberti): Remove the opportunistic encryption mechanism in
59 // BasicPacketSocketFactory that uses this function.
60 virtual void SetIgnoreBadCert(bool ignore) = 0;
61
62 virtual void SetAlpnProtocols(const std::vector<std::string>& protos) = 0;
63 virtual void SetEllipticCurves(const std::vector<std::string>& curves) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020064
65 // Do DTLS or TLS (default is TLS, if unspecified)
66 virtual void SetMode(SSLMode mode) = 0;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070067 // Specify a custom certificate verifier for SSL.
68 virtual void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020069
Steve Anton786de702017-08-17 15:15:46 -070070 // Set the certificate this socket will present to incoming clients.
71 virtual void SetIdentity(SSLIdentity* identity) = 0;
72
73 // Choose whether the socket acts as a server socket or client socket.
74 virtual void SetRole(SSLRole role) = 0;
75
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020076 // StartSSL returns 0 if successful.
77 // If StartSSL is called while the socket is closed or connecting, the SSL
78 // negotiation will begin as soon as the socket connects.
Justin Uberti1d445502017-08-14 17:04:34 -070079 // TODO(juberti): Remove |restartable|.
80 virtual int StartSSL(const char* hostname, bool restartable = false) = 0;
81
82 // When an SSLAdapterFactory is used, an SSLAdapter may be used to resume
83 // a previous SSL session, which results in an abbreviated handshake.
84 // This method, if called after SSL has been established for this adapter,
85 // indicates whether the current session is a resumption of a previous
86 // session.
87 virtual bool IsResumedSession() = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020088
89 // Create the default SSL adapter for this platform. On failure, returns null
90 // and deletes |socket|. Otherwise, the returned SSLAdapter takes ownership
91 // of |socket|.
92 static SSLAdapter* Create(AsyncSocket* socket);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020093};
94
95///////////////////////////////////////////////////////////////////////////////
96
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020097// Call this on the main thread, before using SSL.
Jiawei Oueb0df082018-02-02 14:51:18 -080098// Call CleanupSSL when finished with SSL.
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070099bool InitializeSSL();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200100
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200101// Call to cleanup additional threads, and also the main thread.
102bool CleanupSSL();
103
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200104} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105
Steve Anton10542f22019-01-11 09:11:00 -0800106#endif // RTC_BASE_SSL_ADAPTER_H_