blob: 4b49efd25fb04be3d70f10ecc52c6e40742eec52 [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_OPENSSLADAPTER_H_
12#define WEBRTC_RTC_BASE_OPENSSLADAPTER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Justin Uberti1d445502017-08-14 17:04:34 -070014#include <map>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020015#include <string>
kjellandere96c45b2017-06-30 10:45:21 -070016#include "webrtc/rtc_base/buffer.h"
17#include "webrtc/rtc_base/messagehandler.h"
18#include "webrtc/rtc_base/messagequeue.h"
19#include "webrtc/rtc_base/ssladapter.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020021typedef struct ssl_st SSL;
22typedef struct ssl_ctx_st SSL_CTX;
23typedef struct x509_store_ctx_st X509_STORE_CTX;
Justin Uberti1d445502017-08-14 17:04:34 -070024typedef struct ssl_session_st SSL_SESSION;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020026namespace rtc {
27
Justin Uberti1d445502017-08-14 17:04:34 -070028class OpenSSLAdapterFactory;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029
30class OpenSSLAdapter : public SSLAdapter, public MessageHandler {
Justin Uberti1d445502017-08-14 17:04:34 -070031 public:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020032 static bool InitializeSSL(VerificationCallback callback);
33 static bool InitializeSSLThread();
34 static bool CleanupSSL();
35
Justin Uberti1d445502017-08-14 17:04:34 -070036 explicit OpenSSLAdapter(AsyncSocket* socket,
37 OpenSSLAdapterFactory* factory = nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020038 ~OpenSSLAdapter() override;
39
40 void SetMode(SSLMode mode) override;
41 int StartSSL(const char* hostname, bool restartable) override;
42 int Send(const void* pv, size_t cb) override;
43 int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override;
44 int Recv(void* pv, size_t cb, int64_t* timestamp) override;
45 int RecvFrom(void* pv,
46 size_t cb,
47 SocketAddress* paddr,
48 int64_t* timestamp) override;
49 int Close() override;
50
51 // Note that the socket returns ST_CONNECTING while SSL is being negotiated.
52 ConnState GetState() const override;
Justin Uberti1d445502017-08-14 17:04:34 -070053 bool IsResumedSession() override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054
Justin Uberti1d445502017-08-14 17:04:34 -070055 // Creates a new SSL_CTX object, configured for client-to-server usage
56 // with SSLMode |mode|, and if |enable_cache| is true, with support for
57 // storing successful sessions so that they can be later resumed.
58 // OpenSSLAdapterFactory will call this method to create its own internal
59 // SSL_CTX, and OpenSSLAdapter will also call this when used without a
60 // factory.
61 static SSL_CTX* CreateContext(SSLMode mode, bool enable_cache);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062
Justin Uberti1d445502017-08-14 17:04:34 -070063 protected:
64 void OnConnectEvent(AsyncSocket* socket) override;
65 void OnReadEvent(AsyncSocket* socket) override;
66 void OnWriteEvent(AsyncSocket* socket) override;
67 void OnCloseEvent(AsyncSocket* socket, int err) override;
68
69 private:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020070 enum SSLState {
71 SSL_NONE, SSL_WAIT, SSL_CONNECTING, SSL_CONNECTED, SSL_ERROR
72 };
73
74 enum { MSG_TIMEOUT };
75
76 int BeginSSL();
77 int ContinueSSL();
78 void Error(const char* context, int err, bool signal = true);
79 void Cleanup();
80
81 // Return value and arguments have the same meanings as for Send; |error| is
82 // an output parameter filled with the result of SSL_get_error.
83 int DoSslWrite(const void* pv, size_t cb, int* error);
84
85 void OnMessage(Message* msg) override;
86
87 static bool VerifyServerName(SSL* ssl, const char* host,
88 bool ignore_bad_cert);
89 bool SSLPostConnectionCheck(SSL* ssl, const char* host);
90#if !defined(NDEBUG)
Justin Uberti1d445502017-08-14 17:04:34 -070091 // In debug builds, logs info about the state of the SSL connection.
92 static void SSLInfoCallback(const SSL* ssl, int where, int ret);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020093#endif
94 static int SSLVerifyCallback(int ok, X509_STORE_CTX* store);
95 static VerificationCallback custom_verify_callback_;
96 friend class OpenSSLStreamAdapter; // for custom_verify_callback_;
97
Justin Uberti1d445502017-08-14 17:04:34 -070098 // If the SSL_CTX was created with |enable_cache| set to true, this callback
99 // will be called when a SSL session has been successfully established,
100 // to allow its SSL_SESSION* to be cached for later resumption.
101 static int NewSSLSessionCallback(SSL* ssl, SSL_SESSION* session);
102
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200103 static bool ConfigureTrustedRootCertificates(SSL_CTX* ctx);
Justin Uberti1d445502017-08-14 17:04:34 -0700104
105 // Parent object that maintains shared state.
106 // Can be null if state sharing is not needed.
107 OpenSSLAdapterFactory* factory_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200108
109 SSLState state_;
110 bool ssl_read_needs_write_;
111 bool ssl_write_needs_read_;
112 // If true, socket will retain SSL configuration after Close.
Justin Uberti1d445502017-08-14 17:04:34 -0700113 // TODO(juberti): Remove this unused flag.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200114 bool restartable_;
115
116 // This buffer is used if SSL_write fails with SSL_ERROR_WANT_WRITE, which
117 // means we need to keep retrying with *the same exact data* until it
118 // succeeds. Afterwards it will be cleared.
119 Buffer pending_data_;
120
121 SSL* ssl_;
122 SSL_CTX* ssl_ctx_;
123 std::string ssl_host_name_;
124 // Do DTLS or not
125 SSLMode ssl_mode_;
126
127 bool custom_verification_succeeded_;
128};
129
Justin Uberti1d445502017-08-14 17:04:34 -0700130class OpenSSLAdapterFactory : public SSLAdapterFactory {
131 public:
132 OpenSSLAdapterFactory();
133 ~OpenSSLAdapterFactory() override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200134
Justin Uberti1d445502017-08-14 17:04:34 -0700135 void SetMode(SSLMode mode) override;
136 OpenSSLAdapter* CreateAdapter(AsyncSocket* socket) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200137
Justin Uberti1d445502017-08-14 17:04:34 -0700138 static OpenSSLAdapterFactory* Create();
Henrik Kjellanderc0362762017-06-29 08:03:04 +0200139
Justin Uberti1d445502017-08-14 17:04:34 -0700140 private:
141 SSL_CTX* ssl_ctx() { return ssl_ctx_; }
142 // Looks up a session by hostname. The returned SSL_SESSION is not up_refed.
143 SSL_SESSION* LookupSession(const std::string& hostname);
144 // Adds a session to the cache, and up_refs it. Any existing session with the
145 // same hostname is replaced.
146 void AddSession(const std::string& hostname, SSL_SESSION* session);
147 friend class OpenSSLAdapter;
148
149 SSLMode ssl_mode_;
150 // Holds the shared SSL_CTX for all created adapters.
151 SSL_CTX* ssl_ctx_;
152 // Map of hostnames to SSL_SESSIONs; holds references to the SSL_SESSIONs,
153 // which are cleaned up when the factory is destroyed.
154 // TODO(juberti): Add LRU eviction to keep the cache from growing forever.
155 std::map<std::string, SSL_SESSION*> sessions_;
156};
157
158} // namespace rtc
159
160#endif // WEBRTC_RTC_BASE_OPENSSLADAPTER_H_