blob: b57ea8fd33c93681696fe5650b55199c6633cd91 [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"
Steve Anton786de702017-08-17 15:15:46 -070019#include "webrtc/rtc_base/opensslidentity.h"
kjellandere96c45b2017-06-30 10:45:21 -070020#include "webrtc/rtc_base/ssladapter.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022typedef struct ssl_st SSL;
23typedef struct ssl_ctx_st SSL_CTX;
24typedef struct x509_store_ctx_st X509_STORE_CTX;
Justin Uberti1d445502017-08-14 17:04:34 -070025typedef struct ssl_session_st SSL_SESSION;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020027namespace rtc {
28
Justin Uberti1d445502017-08-14 17:04:34 -070029class OpenSSLAdapterFactory;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020030
31class OpenSSLAdapter : public SSLAdapter, public MessageHandler {
Justin Uberti1d445502017-08-14 17:04:34 -070032 public:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020033 static bool InitializeSSL(VerificationCallback callback);
34 static bool InitializeSSLThread();
35 static bool CleanupSSL();
36
Justin Uberti1d445502017-08-14 17:04:34 -070037 explicit OpenSSLAdapter(AsyncSocket* socket,
38 OpenSSLAdapterFactory* factory = nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020039 ~OpenSSLAdapter() override;
40
41 void SetMode(SSLMode mode) override;
Steve Anton786de702017-08-17 15:15:46 -070042 void SetIdentity(SSLIdentity* identity) override;
43 void SetRole(SSLRole role) override;
44 AsyncSocket* Accept(SocketAddress* paddr) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020045 int StartSSL(const char* hostname, bool restartable) override;
46 int Send(const void* pv, size_t cb) override;
47 int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override;
48 int Recv(void* pv, size_t cb, int64_t* timestamp) override;
49 int RecvFrom(void* pv,
50 size_t cb,
51 SocketAddress* paddr,
52 int64_t* timestamp) override;
53 int Close() override;
54
55 // Note that the socket returns ST_CONNECTING while SSL is being negotiated.
56 ConnState GetState() const override;
Justin Uberti1d445502017-08-14 17:04:34 -070057 bool IsResumedSession() override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020058
Justin Uberti1d445502017-08-14 17:04:34 -070059 // Creates a new SSL_CTX object, configured for client-to-server usage
60 // with SSLMode |mode|, and if |enable_cache| is true, with support for
61 // storing successful sessions so that they can be later resumed.
62 // OpenSSLAdapterFactory will call this method to create its own internal
63 // SSL_CTX, and OpenSSLAdapter will also call this when used without a
64 // factory.
65 static SSL_CTX* CreateContext(SSLMode mode, bool enable_cache);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020066
Justin Uberti1d445502017-08-14 17:04:34 -070067 protected:
68 void OnConnectEvent(AsyncSocket* socket) override;
69 void OnReadEvent(AsyncSocket* socket) override;
70 void OnWriteEvent(AsyncSocket* socket) override;
71 void OnCloseEvent(AsyncSocket* socket, int err) override;
72
73 private:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020074 enum SSLState {
75 SSL_NONE, SSL_WAIT, SSL_CONNECTING, SSL_CONNECTED, SSL_ERROR
76 };
77
78 enum { MSG_TIMEOUT };
79
80 int BeginSSL();
81 int ContinueSSL();
82 void Error(const char* context, int err, bool signal = true);
83 void Cleanup();
84
85 // Return value and arguments have the same meanings as for Send; |error| is
86 // an output parameter filled with the result of SSL_get_error.
87 int DoSslWrite(const void* pv, size_t cb, int* error);
88
89 void OnMessage(Message* msg) override;
90
91 static bool VerifyServerName(SSL* ssl, const char* host,
92 bool ignore_bad_cert);
93 bool SSLPostConnectionCheck(SSL* ssl, const char* host);
94#if !defined(NDEBUG)
Justin Uberti1d445502017-08-14 17:04:34 -070095 // In debug builds, logs info about the state of the SSL connection.
96 static void SSLInfoCallback(const SSL* ssl, int where, int ret);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020097#endif
98 static int SSLVerifyCallback(int ok, X509_STORE_CTX* store);
99 static VerificationCallback custom_verify_callback_;
100 friend class OpenSSLStreamAdapter; // for custom_verify_callback_;
101
Justin Uberti1d445502017-08-14 17:04:34 -0700102 // If the SSL_CTX was created with |enable_cache| set to true, this callback
103 // will be called when a SSL session has been successfully established,
104 // to allow its SSL_SESSION* to be cached for later resumption.
105 static int NewSSLSessionCallback(SSL* ssl, SSL_SESSION* session);
106
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200107 static bool ConfigureTrustedRootCertificates(SSL_CTX* ctx);
Justin Uberti1d445502017-08-14 17:04:34 -0700108
109 // Parent object that maintains shared state.
110 // Can be null if state sharing is not needed.
111 OpenSSLAdapterFactory* factory_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200112
113 SSLState state_;
Steve Anton786de702017-08-17 15:15:46 -0700114 std::unique_ptr<OpenSSLIdentity> identity_;
115 SSLRole role_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200116 bool ssl_read_needs_write_;
117 bool ssl_write_needs_read_;
118 // If true, socket will retain SSL configuration after Close.
Justin Uberti1d445502017-08-14 17:04:34 -0700119 // TODO(juberti): Remove this unused flag.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200120 bool restartable_;
121
122 // This buffer is used if SSL_write fails with SSL_ERROR_WANT_WRITE, which
123 // means we need to keep retrying with *the same exact data* until it
124 // succeeds. Afterwards it will be cleared.
125 Buffer pending_data_;
126
127 SSL* ssl_;
128 SSL_CTX* ssl_ctx_;
129 std::string ssl_host_name_;
130 // Do DTLS or not
131 SSLMode ssl_mode_;
132
133 bool custom_verification_succeeded_;
134};
135
Justin Uberti1d445502017-08-14 17:04:34 -0700136class OpenSSLAdapterFactory : public SSLAdapterFactory {
137 public:
138 OpenSSLAdapterFactory();
139 ~OpenSSLAdapterFactory() override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200140
Justin Uberti1d445502017-08-14 17:04:34 -0700141 void SetMode(SSLMode mode) override;
142 OpenSSLAdapter* CreateAdapter(AsyncSocket* socket) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200143
Justin Uberti1d445502017-08-14 17:04:34 -0700144 static OpenSSLAdapterFactory* Create();
Henrik Kjellanderc0362762017-06-29 08:03:04 +0200145
Justin Uberti1d445502017-08-14 17:04:34 -0700146 private:
147 SSL_CTX* ssl_ctx() { return ssl_ctx_; }
148 // Looks up a session by hostname. The returned SSL_SESSION is not up_refed.
149 SSL_SESSION* LookupSession(const std::string& hostname);
150 // Adds a session to the cache, and up_refs it. Any existing session with the
151 // same hostname is replaced.
152 void AddSession(const std::string& hostname, SSL_SESSION* session);
153 friend class OpenSSLAdapter;
154
155 SSLMode ssl_mode_;
156 // Holds the shared SSL_CTX for all created adapters.
157 SSL_CTX* ssl_ctx_;
158 // Map of hostnames to SSL_SESSIONs; holds references to the SSL_SESSIONs,
159 // which are cleaned up when the factory is destroyed.
160 // TODO(juberti): Add LRU eviction to keep the cache from growing forever.
161 std::map<std::string, SSL_SESSION*> sessions_;
162};
163
164} // namespace rtc
165
166#endif // WEBRTC_RTC_BASE_OPENSSLADAPTER_H_