blob: 16b62eb3feb2d23b71ce5f103a379c225c76b036 [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
11#ifndef WEBRTC_BASE_OPENSSLSTREAMADAPTER_H__
12#define WEBRTC_BASE_OPENSSLSTREAMADAPTER_H__
13
14#include <string>
jbauch555604a2016-04-26 03:13:22 -070015#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016#include <vector>
17
18#include "webrtc/base/buffer.h"
19#include "webrtc/base/sslstreamadapter.h"
20#include "webrtc/base/opensslidentity.h"
21
22typedef struct ssl_st SSL;
23typedef struct ssl_ctx_st SSL_CTX;
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +000024typedef struct ssl_cipher_st SSL_CIPHER;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025typedef struct x509_store_ctx_st X509_STORE_CTX;
26
27namespace rtc {
28
29// This class was written with OpenSSLAdapter (a socket adapter) as a
Taylor Brandstetterc8762a82016-08-11 12:01:49 -070030// starting point. It has similar structure and functionality, but uses a
31// "peer-to-peer" mode, verifying the peer's certificate using a digest
32// sent over a secure signaling channel.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033//
34// Static methods to initialize and deinit the SSL library are in
Taylor Brandstetterc8762a82016-08-11 12:01:49 -070035// OpenSSLAdapter. These should probably be moved out to a neutral class.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036//
Taylor Brandstetterc8762a82016-08-11 12:01:49 -070037// In a few cases I have factored out some OpenSSLAdapter code into static
38// methods so it can be reused from this class. Eventually that code should
39// probably be moved to a common support class. Unfortunately there remain a
40// few duplicated sections of code. I have not done more restructuring because
41// I did not want to affect existing code that uses OpenSSLAdapter.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042//
Taylor Brandstetterc8762a82016-08-11 12:01:49 -070043// This class does not support the SSL connection restart feature present in
44// OpenSSLAdapter. I am not entirely sure how the feature is useful and I am
45// not convinced that it works properly.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046//
Taylor Brandstetterc8762a82016-08-11 12:01:49 -070047// This implementation is careful to disallow data exchange after an SSL error,
48// and it has an explicit SSL_CLOSED state. It should not be possible to send
49// any data in clear after one of the StartSSL methods has been called.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050
51// Look in sslstreamadapter.h for documentation of the methods.
52
53class OpenSSLIdentity;
54
55///////////////////////////////////////////////////////////////////////////////
56
57class OpenSSLStreamAdapter : public SSLStreamAdapter {
58 public:
59 explicit OpenSSLStreamAdapter(StreamInterface* stream);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000060 ~OpenSSLStreamAdapter() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000061
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000062 void SetIdentity(SSLIdentity* identity) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063
64 // Default argument is for compatibility
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000065 void SetServerRole(SSLRole role = SSL_SERVER) override;
deadbeef89824f62016-09-30 11:55:43 -070066 bool SetPeerCertificateDigest(
67 const std::string& digest_alg,
68 const unsigned char* digest_val,
69 size_t digest_len,
70 SSLPeerCertificateDigestError* error = nullptr) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000071
jbauch555604a2016-04-26 03:13:22 -070072 std::unique_ptr<SSLCertificate> GetPeerCertificate() const override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073
Taylor Brandstetterc8762a82016-08-11 12:01:49 -070074 // Goes from state SSL_NONE to either SSL_CONNECTING or SSL_WAIT, depending
75 // on whether the underlying stream is already open or not.
76 int StartSSL() override;
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000077 void SetMode(SSLMode mode) override;
Joachim Bauch831c5582015-05-20 12:48:41 +020078 void SetMaxProtocolVersion(SSLProtocolVersion version) override;
skvladd0309122017-02-02 17:18:37 -080079 void SetInitialRetransmissionTimeout(int timeout_ms) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000081 StreamResult Read(void* data,
82 size_t data_len,
83 size_t* read,
84 int* error) override;
85 StreamResult Write(const void* data,
86 size_t data_len,
87 size_t* written,
88 int* error) override;
89 void Close() override;
90 StreamState GetState() const override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000091
Guo-wei Shieh456696a2015-09-30 21:48:54 -070092 // TODO(guoweis): Move this away from a static class method.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080093 static std::string SslCipherSuiteToName(int crypto_suite);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +000094
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -070095 bool GetSslCipherSuite(int* cipher) override;
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +000096
torbjorng43166b82016-03-11 00:06:47 -080097 int GetSslVersion() const override;
98
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000099 // Key Extractor interface
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000100 bool ExportKeyingMaterial(const std::string& label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200101 const uint8_t* context,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000102 size_t context_len,
103 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200104 uint8_t* result,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000105 size_t result_len) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000106
107 // DTLS-SRTP interface
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800108 bool SetDtlsSrtpCryptoSuites(const std::vector<int>& crypto_suites) override;
109 bool GetDtlsSrtpCryptoSuite(int* crypto_suite) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000110
deadbeef89824f62016-09-30 11:55:43 -0700111 bool IsTlsConnected() override;
112
deadbeef1b54a5f2017-01-23 19:39:57 -0800113 // Capabilities interfaces.
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700114 static bool IsBoringSsl();
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700115
torbjorng43166b82016-03-11 00:06:47 -0800116 static bool IsAcceptableCipher(int cipher, KeyType key_type);
117 static bool IsAcceptableCipher(const std::string& cipher, KeyType key_type);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000118
deadbeef6cf94a02016-11-28 17:38:34 -0800119 // Use our timeutils.h source of timing in BoringSSL, allowing us to test
120 // using a fake clock.
121 static void enable_time_callback_for_testing();
122
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123 protected:
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000124 void OnEvent(StreamInterface* stream, int events, int err) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000125
126 private:
127 enum SSLState {
128 // Before calling one of the StartSSL methods, data flows
129 // in clear text.
130 SSL_NONE,
131 SSL_WAIT, // waiting for the stream to open to start SSL negotiation
132 SSL_CONNECTING, // SSL negotiation in progress
133 SSL_CONNECTED, // SSL stream successfully established
134 SSL_ERROR, // some SSL error occurred, stream is closed
135 SSL_CLOSED // Clean close
136 };
137
138 enum { MSG_TIMEOUT = MSG_MAX+1};
139
140 // The following three methods return 0 on success and a negative
141 // error code on failure. The error code may be from OpenSSL or -1
142 // on some other error cases, so it can't really be interpreted
143 // unfortunately.
144
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000145 // Prepare SSL library, state is SSL_CONNECTING.
146 int BeginSSL();
147 // Perform SSL negotiation steps.
148 int ContinueSSL();
149
150 // Error handler helper. signal is given as true for errors in
151 // asynchronous contexts (when an error method was not returned
152 // through some other method), and in that case an SE_CLOSE event is
153 // raised on the stream with the specified error.
154 // A 0 error means a graceful close, otherwise there is not really enough
155 // context to interpret the error code.
deadbeef89824f62016-09-30 11:55:43 -0700156 // |alert| indicates an alert description (one of the SSL_AD constants) to
157 // send to the remote endpoint when closing the association. If 0, a normal
158 // shutdown will be performed.
159 void Error(const char* context, int err, uint8_t alert, bool signal);
160 void Cleanup(uint8_t alert);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000161
162 // Override MessageHandler
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000163 void OnMessage(Message* msg) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000164
165 // Flush the input buffers by reading left bytes (for DTLS)
166 void FlushInput(unsigned int left);
167
168 // SSL library configuration
169 SSL_CTX* SetupSSLContext();
deadbeef89824f62016-09-30 11:55:43 -0700170 // Verify the peer certificate matches the signaled digest.
171 bool VerifyPeerCertificate();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000172 // SSL certification verification error handler, called back from
173 // the openssl library. Returns an int interpreted as a boolean in
174 // the C style: zero means verification failure, non-zero means
175 // passed.
176 static int SSLVerifyCallback(int ok, X509_STORE_CTX* store);
177
deadbeef89824f62016-09-30 11:55:43 -0700178 bool waiting_to_verify_peer_certificate() const {
179 return client_auth_enabled() && !peer_certificate_verified_;
180 }
181
182 bool has_peer_certificate_digest() const {
183 return !peer_certificate_digest_algorithm_.empty() &&
184 !peer_certificate_digest_value_.empty();
185 }
186
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000187 SSLState state_;
188 SSLRole role_;
189 int ssl_error_code_; // valid when state_ == SSL_ERROR or SSL_CLOSED
190 // Whether the SSL negotiation is blocked on needing to read or
191 // write to the wrapped stream.
192 bool ssl_read_needs_write_;
193 bool ssl_write_needs_read_;
194
195 SSL* ssl_;
196 SSL_CTX* ssl_ctx_;
197
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700198 // Our key and certificate.
jbauch555604a2016-04-26 03:13:22 -0700199 std::unique_ptr<OpenSSLIdentity> identity_;
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700200 // The certificate that the peer presented. Initially null, until the
201 // connection is established.
jbauch555604a2016-04-26 03:13:22 -0700202 std::unique_ptr<OpenSSLCertificate> peer_certificate_;
deadbeef89824f62016-09-30 11:55:43 -0700203 bool peer_certificate_verified_ = false;
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700204 // The digest of the certificate that the peer must present.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000205 Buffer peer_certificate_digest_value_;
206 std::string peer_certificate_digest_algorithm_;
207
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000208 // The DtlsSrtp ciphers
209 std::string srtp_ciphers_;
210
211 // Do DTLS or not
212 SSLMode ssl_mode_;
Joachim Bauch831c5582015-05-20 12:48:41 +0200213
214 // Max. allowed protocol version
215 SSLProtocolVersion ssl_max_version_;
skvladd0309122017-02-02 17:18:37 -0800216
217 // A 50-ms initial timeout ensures rapid setup on fast connections, but may
218 // be too aggressive for low bandwidth links.
219 int dtls_handshake_timeout_ms_ = 50;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000220};
221
222/////////////////////////////////////////////////////////////////////////////
223
224} // namespace rtc
225
226#endif // WEBRTC_BASE_OPENSSLSTREAMADAPTER_H__