blob: b30e176c4528c882c5251617a761b7ada9af4e1f [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
Henrik Kjellanderc0362762017-06-29 08:03:04 +020011#ifndef WEBRTC_RTC_BASE_SSLADAPTER_H_
12#define WEBRTC_RTC_BASE_SSLADAPTER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
kjellandere96c45b2017-06-30 10:45:21 -070014#include "webrtc/rtc_base/asyncsocket.h"
15#include "webrtc/rtc_base/sslstreamadapter.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017namespace rtc {
18
Justin Uberti1d445502017-08-14 17:04:34 -070019class SSLAdapter;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020020
Justin Uberti1d445502017-08-14 17:04:34 -070021// 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.
26class 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 // Creates a new SSL adapter, but from a shared context.
32 virtual SSLAdapter* CreateAdapter(AsyncSocket* socket) = 0;
33
34 static SSLAdapterFactory* Create();
35};
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.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020042class SSLAdapter : public AsyncSocketAdapter {
43 public:
Justin Uberti1d445502017-08-14 17:04:34 -070044 explicit SSLAdapter(AsyncSocket* socket) : AsyncSocketAdapter(socket) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020045
Justin Uberti1d445502017-08-14 17:04:34 -070046 // Methods that control server certificate verification, used in unit tests.
47 // Do not call these methods in production code.
48 // TODO(juberti): Remove the opportunistic encryption mechanism in
49 // BasicPacketSocketFactory that uses this function.
Diogo Real1dca9d52017-08-29 12:18:32 -070050 virtual void SetIgnoreBadCert(bool ignore) = 0;
51 virtual void SetAlpnProtocols(const std::vector<std::string>& protos) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020052
53 // Do DTLS or TLS (default is TLS, if unspecified)
54 virtual void SetMode(SSLMode mode) = 0;
55
Steve Anton786de702017-08-17 15:15:46 -070056 // Set the certificate this socket will present to incoming clients.
57 virtual void SetIdentity(SSLIdentity* identity) = 0;
58
59 // Choose whether the socket acts as a server socket or client socket.
60 virtual void SetRole(SSLRole role) = 0;
61
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062 // StartSSL returns 0 if successful.
63 // If StartSSL is called while the socket is closed or connecting, the SSL
64 // negotiation will begin as soon as the socket connects.
Justin Uberti1d445502017-08-14 17:04:34 -070065 // TODO(juberti): Remove |restartable|.
66 virtual int StartSSL(const char* hostname, bool restartable = false) = 0;
67
68 // When an SSLAdapterFactory is used, an SSLAdapter may be used to resume
69 // a previous SSL session, which results in an abbreviated handshake.
70 // This method, if called after SSL has been established for this adapter,
71 // indicates whether the current session is a resumption of a previous
72 // session.
73 virtual bool IsResumedSession() = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020074
75 // Create the default SSL adapter for this platform. On failure, returns null
76 // and deletes |socket|. Otherwise, the returned SSLAdapter takes ownership
77 // of |socket|.
78 static SSLAdapter* Create(AsyncSocket* socket);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020079};
80
81///////////////////////////////////////////////////////////////////////////////
82
83typedef bool (*VerificationCallback)(void* cert);
84
85// Call this on the main thread, before using SSL.
86// Call CleanupSSLThread when finished with SSL.
87bool InitializeSSL(VerificationCallback callback = nullptr);
88
89// Call to initialize additional threads.
90bool InitializeSSLThread();
91
92// Call to cleanup additional threads, and also the main thread.
93bool CleanupSSL();
94
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020095} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096
Henrik Kjellanderc0362762017-06-29 08:03:04 +020097#endif // WEBRTC_RTC_BASE_SSLADAPTER_H_