blob: ccd6460d02bc109a57e4e4cc7ace4b3f80d08ab5 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_OPENSSLSTREAMADAPTER_H_
12#define RTC_BASE_OPENSSLSTREAMADAPTER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <string>
15#include <memory>
16#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/buffer.h"
19#include "rtc_base/opensslidentity.h"
20#include "rtc_base/sslstreamadapter.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 ssl_cipher_st SSL_CIPHER;
25typedef struct x509_store_ctx_st X509_STORE_CTX;
26
27namespace rtc {
28
29// This class was written with OpenSSLAdapter (a socket adapter) as a
30// 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.
33//
34// Static methods to initialize and deinit the SSL library are in
35// OpenSSLAdapter. These should probably be moved out to a neutral class.
36//
37// 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.
42//
43// 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.
46//
47// 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.
50
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);
60 ~OpenSSLStreamAdapter() override;
61
62 void SetIdentity(SSLIdentity* identity) override;
63
64 // Default argument is for compatibility
65 void SetServerRole(SSLRole role = SSL_SERVER) override;
66 bool SetPeerCertificateDigest(
67 const std::string& digest_alg,
68 const unsigned char* digest_val,
69 size_t digest_len,
70 SSLPeerCertificateDigestError* error = nullptr) override;
71
72 std::unique_ptr<SSLCertificate> GetPeerCertificate() const override;
73
74 // 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;
77 void SetMode(SSLMode mode) override;
78 void SetMaxProtocolVersion(SSLProtocolVersion version) override;
79 void SetInitialRetransmissionTimeout(int timeout_ms) override;
80
81 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;
91
92 // TODO(guoweis): Move this away from a static class method.
93 static std::string SslCipherSuiteToName(int crypto_suite);
94
95 bool GetSslCipherSuite(int* cipher) override;
96
97 int GetSslVersion() const override;
98
99 // Key Extractor interface
100 bool ExportKeyingMaterial(const std::string& label,
101 const uint8_t* context,
102 size_t context_len,
103 bool use_context,
104 uint8_t* result,
105 size_t result_len) override;
106
107 // DTLS-SRTP interface
108 bool SetDtlsSrtpCryptoSuites(const std::vector<int>& crypto_suites) override;
109 bool GetDtlsSrtpCryptoSuite(int* crypto_suite) override;
110
111 bool IsTlsConnected() override;
112
113 // Capabilities interfaces.
114 static bool IsBoringSsl();
115
116 static bool IsAcceptableCipher(int cipher, KeyType key_type);
117 static bool IsAcceptableCipher(const std::string& cipher, KeyType key_type);
118
119 // 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
123 protected:
124 void OnEvent(StreamInterface* stream, int events, int err) override;
125
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
145 // 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.
156 // |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);
161
162 // Override MessageHandler
163 void OnMessage(Message* msg) override;
164
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();
170 // Verify the peer certificate matches the signaled digest.
171 bool VerifyPeerCertificate();
David Benjamindc246562017-09-29 12:14:08 -0400172 // SSL certificate verification callback. See
173 // SSL_CTX_set_cert_verify_callback.
174 static int SSLVerifyCallback(X509_STORE_CTX* store, void* arg);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200175
176 bool waiting_to_verify_peer_certificate() const {
177 return client_auth_enabled() && !peer_certificate_verified_;
178 }
179
180 bool has_peer_certificate_digest() const {
181 return !peer_certificate_digest_algorithm_.empty() &&
182 !peer_certificate_digest_value_.empty();
183 }
184
185 SSLState state_;
186 SSLRole role_;
187 int ssl_error_code_; // valid when state_ == SSL_ERROR or SSL_CLOSED
188 // Whether the SSL negotiation is blocked on needing to read or
189 // write to the wrapped stream.
190 bool ssl_read_needs_write_;
191 bool ssl_write_needs_read_;
192
193 SSL* ssl_;
194 SSL_CTX* ssl_ctx_;
195
196 // Our key and certificate.
197 std::unique_ptr<OpenSSLIdentity> identity_;
198 // The certificate that the peer presented. Initially null, until the
199 // connection is established.
200 std::unique_ptr<OpenSSLCertificate> peer_certificate_;
201 bool peer_certificate_verified_ = false;
202 // The digest of the certificate that the peer must present.
203 Buffer peer_certificate_digest_value_;
204 std::string peer_certificate_digest_algorithm_;
205
206 // The DtlsSrtp ciphers
207 std::string srtp_ciphers_;
208
209 // Do DTLS or not
210 SSLMode ssl_mode_;
211
212 // Max. allowed protocol version
213 SSLProtocolVersion ssl_max_version_;
214
215 // A 50-ms initial timeout ensures rapid setup on fast connections, but may
216 // be too aggressive for low bandwidth links.
217 int dtls_handshake_timeout_ms_ = 50;
218};
219
220/////////////////////////////////////////////////////////////////////////////
221
222} // namespace rtc
223
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200224#endif // RTC_BASE_OPENSSLSTREAMADAPTER_H_