blob: 4843d264fcccd366d480f6438614103b9ea9785b [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_SSLADAPTER_H_
12#define RTC_BASE_SSLADAPTER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070014#include <string>
15#include <vector>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/asyncsocket.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070018#include "rtc_base/sslcertificate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/sslstreamadapter.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020021namespace rtc {
22
Justin Uberti1d445502017-08-14 17:04:34 -070023class SSLAdapter;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024
Justin Uberti1d445502017-08-14 17:04:34 -070025// Class for creating SSL adapters with shared state, e.g., a session cache,
26// which allows clients to resume SSL sessions to previously-contacted hosts.
27// Clients should create the factory using Create(), set up the factory as
28// needed using SetMode, and then call CreateAdapter to create adapters when
29// needed.
30class SSLAdapterFactory {
31 public:
32 virtual ~SSLAdapterFactory() {}
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070033
Justin Uberti1d445502017-08-14 17:04:34 -070034 // Specifies whether TLS or DTLS is to be used for the SSL adapters.
35 virtual void SetMode(SSLMode mode) = 0;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070036
37 // Specify a custom certificate verifier for SSL.
38 virtual void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) = 0;
39
Justin Uberti1d445502017-08-14 17:04:34 -070040 // Creates a new SSL adapter, but from a shared context.
41 virtual SSLAdapter* CreateAdapter(AsyncSocket* socket) = 0;
42
43 static SSLAdapterFactory* Create();
44};
45
46// Class that abstracts a client-to-server SSL session. It can be created
47// standalone, via SSLAdapter::Create, or through a factory as described above,
48// in which case it will share state with other SSLAdapters created from the
49// same factory.
50// After creation, call StartSSL to initiate the SSL handshake to the server.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020051class SSLAdapter : public AsyncSocketAdapter {
52 public:
Justin Uberti1d445502017-08-14 17:04:34 -070053 explicit SSLAdapter(AsyncSocket* socket) : AsyncSocketAdapter(socket) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054
Sergey Silkin9c147dd2018-09-12 10:45:38 +000055 // Methods that control server certificate verification, used in unit tests.
56 // Do not call these methods in production code.
57 // TODO(juberti): Remove the opportunistic encryption mechanism in
58 // BasicPacketSocketFactory that uses this function.
59 virtual void SetIgnoreBadCert(bool ignore) = 0;
60
61 virtual void SetAlpnProtocols(const std::vector<std::string>& protos) = 0;
62 virtual void SetEllipticCurves(const std::vector<std::string>& curves) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020063
64 // Do DTLS or TLS (default is TLS, if unspecified)
65 virtual void SetMode(SSLMode mode) = 0;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070066 // Specify a custom certificate verifier for SSL.
67 virtual void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068
Steve Anton786de702017-08-17 15:15:46 -070069 // Set the certificate this socket will present to incoming clients.
70 virtual void SetIdentity(SSLIdentity* identity) = 0;
71
72 // Choose whether the socket acts as a server socket or client socket.
73 virtual void SetRole(SSLRole role) = 0;
74
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020075 // StartSSL returns 0 if successful.
76 // If StartSSL is called while the socket is closed or connecting, the SSL
77 // negotiation will begin as soon as the socket connects.
Justin Uberti1d445502017-08-14 17:04:34 -070078 // TODO(juberti): Remove |restartable|.
79 virtual int StartSSL(const char* hostname, bool restartable = false) = 0;
80
81 // When an SSLAdapterFactory is used, an SSLAdapter may be used to resume
82 // a previous SSL session, which results in an abbreviated handshake.
83 // This method, if called after SSL has been established for this adapter,
84 // indicates whether the current session is a resumption of a previous
85 // session.
86 virtual bool IsResumedSession() = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020087
88 // Create the default SSL adapter for this platform. On failure, returns null
89 // and deletes |socket|. Otherwise, the returned SSLAdapter takes ownership
90 // of |socket|.
91 static SSLAdapter* Create(AsyncSocket* socket);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020092};
93
94///////////////////////////////////////////////////////////////////////////////
95
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020096// Call this on the main thread, before using SSL.
Jiawei Oueb0df082018-02-02 14:51:18 -080097// Call CleanupSSL when finished with SSL.
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070098bool InitializeSSL();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020099
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200100// Call to cleanup additional threads, and also the main thread.
101bool CleanupSSL();
102
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200103} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000104
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200105#endif // RTC_BASE_SSLADAPTER_H_