blob: c3cab2fd785eb10d1261560b47fb499cbfe5a595 [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_OPENSSL_ADAPTER_H_
12#define RTC_BASE_OPENSSL_ADAPTER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
Benjamin Wright19aab2e2018-04-05 15:39:06 -070017#include <memory>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018#include <string>
Benjamin Wright19aab2e2018-04-05 15:39:06 -070019#include <vector>
20
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/async_socket.h"
Yves Gerey988cc082018-10-23 12:03:01 +020022#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/message_handler.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/openssl_identity.h"
25#include "rtc_base/openssl_session_cache.h"
Yves Gerey988cc082018-10-23 12:03:01 +020026#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "rtc_base/socket_address.h"
28#include "rtc_base/ssl_adapter.h"
29#include "rtc_base/ssl_certificate.h"
30#include "rtc_base/ssl_identity.h"
31#include "rtc_base/ssl_stream_adapter.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020033namespace rtc {
34
Benjamin Wright61c5cc82018-10-26 17:50:00 -070035class OpenSSLAdapter final : public SSLAdapter, public MessageHandler {
Justin Uberti1d445502017-08-14 17:04:34 -070036 public:
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070037 static bool InitializeSSL();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020038 static bool CleanupSSL();
39
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070040 // Creating an OpenSSLAdapter requires a socket to bind to, an optional
41 // session cache if you wish to improve performance by caching sessions for
42 // hostnames you have previously connected to and an optional
43 // SSLCertificateVerifier which can override any existing trusted roots to
44 // validate a peer certificate. The cache and verifier are effectively
45 // immutable after the the SSL connection starts.
Justin Uberti1d445502017-08-14 17:04:34 -070046 explicit OpenSSLAdapter(AsyncSocket* socket,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070047 OpenSSLSessionCache* ssl_session_cache = nullptr,
48 SSLCertificateVerifier* ssl_cert_verifier = nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049 ~OpenSSLAdapter() override;
50
Sergey Silkin9c147dd2018-09-12 10:45:38 +000051 void SetIgnoreBadCert(bool ignore) override;
52 void SetAlpnProtocols(const std::vector<std::string>& protos) override;
53 void SetEllipticCurves(const std::vector<std::string>& curves) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054 void SetMode(SSLMode mode) override;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070055 void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) override;
Steve Anton786de702017-08-17 15:15:46 -070056 void SetIdentity(SSLIdentity* identity) override;
57 void SetRole(SSLRole role) override;
58 AsyncSocket* Accept(SocketAddress* paddr) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020059 int StartSSL(const char* hostname, bool restartable) override;
60 int Send(const void* pv, size_t cb) override;
61 int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override;
62 int Recv(void* pv, size_t cb, int64_t* timestamp) override;
63 int RecvFrom(void* pv,
64 size_t cb,
65 SocketAddress* paddr,
66 int64_t* timestamp) override;
67 int Close() override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068 // Note that the socket returns ST_CONNECTING while SSL is being negotiated.
69 ConnState GetState() const override;
Justin Uberti1d445502017-08-14 17:04:34 -070070 bool IsResumedSession() override;
Justin Uberti1d445502017-08-14 17:04:34 -070071 // Creates a new SSL_CTX object, configured for client-to-server usage
72 // with SSLMode |mode|, and if |enable_cache| is true, with support for
73 // storing successful sessions so that they can be later resumed.
74 // OpenSSLAdapterFactory will call this method to create its own internal
75 // SSL_CTX, and OpenSSLAdapter will also call this when used without a
76 // factory.
77 static SSL_CTX* CreateContext(SSLMode mode, bool enable_cache);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020078
Justin Uberti1d445502017-08-14 17:04:34 -070079 protected:
80 void OnConnectEvent(AsyncSocket* socket) override;
81 void OnReadEvent(AsyncSocket* socket) override;
82 void OnWriteEvent(AsyncSocket* socket) override;
83 void OnCloseEvent(AsyncSocket* socket, int err) override;
84
85 private:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020086 enum SSLState {
Yves Gerey665174f2018-06-19 15:03:05 +020087 SSL_NONE,
88 SSL_WAIT,
89 SSL_CONNECTING,
90 SSL_CONNECTED,
91 SSL_ERROR
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020092 };
93
94 enum { MSG_TIMEOUT };
95
96 int BeginSSL();
97 int ContinueSSL();
98 void Error(const char* context, int err, bool signal = true);
99 void Cleanup();
100
101 // Return value and arguments have the same meanings as for Send; |error| is
102 // an output parameter filled with the result of SSL_get_error.
103 int DoSslWrite(const void* pv, size_t cb, int* error);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200104 void OnMessage(Message* msg) override;
Benjamin Wright9201d1a2018-04-05 12:12:26 -0700105 bool SSLPostConnectionCheck(SSL* ssl, const std::string& host);
106
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200107#if !defined(NDEBUG)
Justin Uberti1d445502017-08-14 17:04:34 -0700108 // In debug builds, logs info about the state of the SSL connection.
109 static void SSLInfoCallback(const SSL* ssl, int where, int ret);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200110#endif
111 static int SSLVerifyCallback(int ok, X509_STORE_CTX* store);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200112 friend class OpenSSLStreamAdapter; // for custom_verify_callback_;
113
Justin Uberti1d445502017-08-14 17:04:34 -0700114 // If the SSL_CTX was created with |enable_cache| set to true, this callback
115 // will be called when a SSL session has been successfully established,
116 // to allow its SSL_SESSION* to be cached for later resumption.
117 static int NewSSLSessionCallback(SSL* ssl, SSL_SESSION* session);
118
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700119 // Optional SSL Shared session cache to improve performance.
Benjamin Wright19aab2e2018-04-05 15:39:06 -0700120 OpenSSLSessionCache* ssl_session_cache_ = nullptr;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700121 // Optional SSL Certificate verifier which can be set by a third party.
122 SSLCertificateVerifier* ssl_cert_verifier_ = nullptr;
123 // The current connection state of the (d)TLS connection.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200124 SSLState state_;
Steve Anton786de702017-08-17 15:15:46 -0700125 std::unique_ptr<OpenSSLIdentity> identity_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700126 // Indicates whethere this is a client or a server.
Steve Anton786de702017-08-17 15:15:46 -0700127 SSLRole role_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200128 bool ssl_read_needs_write_;
129 bool ssl_write_needs_read_;
130 // If true, socket will retain SSL configuration after Close.
Justin Uberti1d445502017-08-14 17:04:34 -0700131 // TODO(juberti): Remove this unused flag.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200132 bool restartable_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200133 // This buffer is used if SSL_write fails with SSL_ERROR_WANT_WRITE, which
134 // means we need to keep retrying with *the same exact data* until it
135 // succeeds. Afterwards it will be cleared.
136 Buffer pending_data_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200137 SSL* ssl_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700138 // Holds the SSL context, which may be shared if an session cache is provided.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200139 SSL_CTX* ssl_ctx_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700140 // Hostname of server that is being connected, used for SNI.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200141 std::string ssl_host_name_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700142 // Set the adapter to DTLS or TLS mode before creating the context.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200143 SSLMode ssl_mode_;
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000144 // If true, the server certificate need not match the configured hostname.
145 bool ignore_bad_cert_;
146 // List of protocols to be used in the TLS ALPN extension.
147 std::vector<std::string> alpn_protocols_;
148 // List of elliptic curves to be used in the TLS elliptic curves extension.
149 std::vector<std::string> elliptic_curves_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700150 // Holds the result of the call to run of the ssl_cert_verify_->Verify()
151 bool custom_cert_verifier_status_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200152};
153
Benjamin Wright19aab2e2018-04-05 15:39:06 -0700154// The OpenSSLAdapterFactory is responsbile for creating multiple new
155// OpenSSLAdapters with a shared SSL_CTX and a shared SSL_SESSION cache. The
156// SSL_SESSION cache allows existing SSL_SESSIONS to be reused instead of
157// recreating them leading to a significant performance improvement.
Justin Uberti1d445502017-08-14 17:04:34 -0700158class OpenSSLAdapterFactory : public SSLAdapterFactory {
159 public:
160 OpenSSLAdapterFactory();
161 ~OpenSSLAdapterFactory() override;
Benjamin Wright19aab2e2018-04-05 15:39:06 -0700162 // Set the SSL Mode to use with this factory. This should only be set before
163 // the first adapter is created with the factory. If it is called after it
164 // will DCHECK.
Justin Uberti1d445502017-08-14 17:04:34 -0700165 void SetMode(SSLMode mode) override;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700166 // Set a custom certificate verifier to be passed down to each instance
167 // created with this factory. This should only ever be set before the first
168 // call to the factory and cannot be changed after the fact.
169 void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) override;
Benjamin Wright19aab2e2018-04-05 15:39:06 -0700170 // Constructs a new socket using the shared OpenSSLSessionCache. This means
171 // existing SSLSessions already in the cache will be reused instead of
172 // re-created for improved performance.
Justin Uberti1d445502017-08-14 17:04:34 -0700173 OpenSSLAdapter* CreateAdapter(AsyncSocket* socket) override;
Henrik Kjellanderc0362762017-06-29 08:03:04 +0200174
Justin Uberti1d445502017-08-14 17:04:34 -0700175 private:
Benjamin Wright19aab2e2018-04-05 15:39:06 -0700176 // Holds the SSLMode (DTLS,TLS) that will be used to set the session cache.
177 SSLMode ssl_mode_ = SSL_MODE_TLS;
178 // Holds a cache of existing SSL Sessions.
179 std::unique_ptr<OpenSSLSessionCache> ssl_session_cache_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700180 // Provides an optional custom callback for verifying SSL certificates, this
181 // in currently only used for TLS-TURN connections.
182 SSLCertificateVerifier* ssl_cert_verifier_ = nullptr;
Benjamin Wright19aab2e2018-04-05 15:39:06 -0700183 // TODO(benwright): Remove this when context is moved to OpenSSLCommon.
184 // Hold a friend class to the OpenSSLAdapter to retrieve the context.
Justin Uberti1d445502017-08-14 17:04:34 -0700185 friend class OpenSSLAdapter;
Justin Uberti1d445502017-08-14 17:04:34 -0700186};
187
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700188std::string TransformAlpnProtocols(const std::vector<std::string>& protos);
189
Justin Uberti1d445502017-08-14 17:04:34 -0700190} // namespace rtc
191
Steve Anton10542f22019-01-11 09:11:00 -0800192#endif // RTC_BASE_OPENSSL_ADAPTER_H_