blob: 05e81021696162b626029bf2a9d723475305665c [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
30// starting point. It has similar structure and functionality, with
31// the peer-to-peer mode added.
32//
33// Static methods to initialize and deinit the SSL library are in
34// OpenSSLAdapter. This class also uses
35// OpenSSLAdapter::custom_verify_callback_ (a static field). These
36// should probably be moved out to a neutral class.
37//
38// In a few cases I have factored out some OpenSSLAdapter code into
39// static methods so it can be reused from this class. Eventually that
40// code should probably be moved to a common support
41// class. Unfortunately there remain a few duplicated sections of
42// code. I have not done more restructuring because I did not want to
43// affect existing code that uses OpenSSLAdapter.
44//
45// This class does not support the SSL connection restart feature
46// present in OpenSSLAdapter. I am not entirely sure how the feature
47// is useful and I am not convinced that it works properly.
48//
49// This implementation is careful to disallow data exchange after an
50// SSL error, and it has an explicit SSL_CLOSED state. It should not
51// be possible to send any data in clear after one of the StartSSL
52// methods has been called.
53
54// Look in sslstreamadapter.h for documentation of the methods.
55
56class OpenSSLIdentity;
57
58///////////////////////////////////////////////////////////////////////////////
59
60class OpenSSLStreamAdapter : public SSLStreamAdapter {
61 public:
62 explicit OpenSSLStreamAdapter(StreamInterface* stream);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000063 ~OpenSSLStreamAdapter() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000064
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000065 void SetIdentity(SSLIdentity* identity) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066
67 // Default argument is for compatibility
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000068 void SetServerRole(SSLRole role = SSL_SERVER) override;
69 bool SetPeerCertificateDigest(const std::string& digest_alg,
70 const unsigned char* digest_val,
71 size_t digest_len) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000072
jbauch555604a2016-04-26 03:13:22 -070073 std::unique_ptr<SSLCertificate> GetPeerCertificate() const override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000075 int StartSSLWithServer(const char* server_name) override;
76 int StartSSLWithPeer() override;
77 void SetMode(SSLMode mode) override;
Joachim Bauch831c5582015-05-20 12:48:41 +020078 void SetMaxProtocolVersion(SSLProtocolVersion version) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000080 StreamResult Read(void* data,
81 size_t data_len,
82 size_t* read,
83 int* error) override;
84 StreamResult Write(const void* data,
85 size_t data_len,
86 size_t* written,
87 int* error) override;
88 void Close() override;
89 StreamState GetState() const override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000090
Guo-wei Shieh456696a2015-09-30 21:48:54 -070091 // TODO(guoweis): Move this away from a static class method.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080092 static std::string SslCipherSuiteToName(int crypto_suite);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +000093
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -070094 bool GetSslCipherSuite(int* cipher) override;
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +000095
torbjorng43166b82016-03-11 00:06:47 -080096 int GetSslVersion() const override;
97
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000098 // Key Extractor interface
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000099 bool ExportKeyingMaterial(const std::string& label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200100 const uint8_t* context,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000101 size_t context_len,
102 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200103 uint8_t* result,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000104 size_t result_len) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105
106 // DTLS-SRTP interface
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800107 bool SetDtlsSrtpCryptoSuites(const std::vector<int>& crypto_suites) override;
108 bool GetDtlsSrtpCryptoSuite(int* crypto_suite) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000109
110 // Capabilities interfaces
111 static bool HaveDtls();
112 static bool HaveDtlsSrtp();
113 static bool HaveExporter();
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
119 protected:
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000120 void OnEvent(StreamInterface* stream, int events, int err) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121
122 private:
123 enum SSLState {
124 // Before calling one of the StartSSL methods, data flows
125 // in clear text.
126 SSL_NONE,
127 SSL_WAIT, // waiting for the stream to open to start SSL negotiation
128 SSL_CONNECTING, // SSL negotiation in progress
129 SSL_CONNECTED, // SSL stream successfully established
130 SSL_ERROR, // some SSL error occurred, stream is closed
131 SSL_CLOSED // Clean close
132 };
133
134 enum { MSG_TIMEOUT = MSG_MAX+1};
135
136 // The following three methods return 0 on success and a negative
137 // error code on failure. The error code may be from OpenSSL or -1
138 // on some other error cases, so it can't really be interpreted
139 // unfortunately.
140
141 // Go from state SSL_NONE to either SSL_CONNECTING or SSL_WAIT,
142 // depending on whether the underlying stream is already open or
143 // not.
144 int StartSSL();
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 void Error(const char* context, int err, bool signal);
157 void Cleanup();
158
159 // Override MessageHandler
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000160 void OnMessage(Message* msg) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000161
162 // Flush the input buffers by reading left bytes (for DTLS)
163 void FlushInput(unsigned int left);
164
165 // SSL library configuration
166 SSL_CTX* SetupSSLContext();
167 // SSL verification check
168 bool SSLPostConnectionCheck(SSL* ssl, const char* server_name,
169 const X509* peer_cert,
170 const std::string& peer_digest);
171 // SSL certification verification error handler, called back from
172 // the openssl library. Returns an int interpreted as a boolean in
173 // the C style: zero means verification failure, non-zero means
174 // passed.
175 static int SSLVerifyCallback(int ok, X509_STORE_CTX* store);
176
177 SSLState state_;
178 SSLRole role_;
179 int ssl_error_code_; // valid when state_ == SSL_ERROR or SSL_CLOSED
180 // Whether the SSL negotiation is blocked on needing to read or
181 // write to the wrapped stream.
182 bool ssl_read_needs_write_;
183 bool ssl_write_needs_read_;
184
185 SSL* ssl_;
186 SSL_CTX* ssl_ctx_;
187
188 // Our key and certificate, mostly useful in peer-to-peer mode.
jbauch555604a2016-04-26 03:13:22 -0700189 std::unique_ptr<OpenSSLIdentity> identity_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000190 // in traditional mode, the server name that the server's certificate
191 // must specify. Empty in peer-to-peer mode.
192 std::string ssl_server_name_;
193 // The certificate that the peer must present or did present. Initially
194 // null in traditional mode, until the connection is established.
jbauch555604a2016-04-26 03:13:22 -0700195 std::unique_ptr<OpenSSLCertificate> peer_certificate_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000196 // In peer-to-peer mode, the digest of the certificate that
197 // the peer must present.
198 Buffer peer_certificate_digest_value_;
199 std::string peer_certificate_digest_algorithm_;
200
201 // OpenSSLAdapter::custom_verify_callback_ result
202 bool custom_verification_succeeded_;
203
204 // The DtlsSrtp ciphers
205 std::string srtp_ciphers_;
206
207 // Do DTLS or not
208 SSLMode ssl_mode_;
Joachim Bauch831c5582015-05-20 12:48:41 +0200209
210 // Max. allowed protocol version
211 SSLProtocolVersion ssl_max_version_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000212};
213
214/////////////////////////////////////////////////////////////////////////////
215
216} // namespace rtc
217
218#endif // WEBRTC_BASE_OPENSSLSTREAMADAPTER_H__