blob: a9d98fd6929f4382114ab601d0a8dc8184093a03 [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>
15#include <vector>
16
17#include "webrtc/base/buffer.h"
18#include "webrtc/base/sslstreamadapter.h"
19#include "webrtc/base/opensslidentity.h"
20
21typedef struct ssl_st SSL;
22typedef struct ssl_ctx_st SSL_CTX;
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +000023typedef struct ssl_cipher_st SSL_CIPHER;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024typedef struct x509_store_ctx_st X509_STORE_CTX;
25
26namespace rtc {
27
28// This class was written with OpenSSLAdapter (a socket adapter) as a
29// starting point. It has similar structure and functionality, with
30// the peer-to-peer mode added.
31//
32// Static methods to initialize and deinit the SSL library are in
33// OpenSSLAdapter. This class also uses
34// OpenSSLAdapter::custom_verify_callback_ (a static field). These
35// should probably be moved out to a neutral class.
36//
37// In a few cases I have factored out some OpenSSLAdapter code into
38// static methods so it can be reused from this class. Eventually that
39// code should probably be moved to a common support
40// class. Unfortunately there remain a few duplicated sections of
41// code. I have not done more restructuring because I did not want to
42// affect existing code that uses OpenSSLAdapter.
43//
44// This class does not support the SSL connection restart feature
45// present in OpenSSLAdapter. I am not entirely sure how the feature
46// is useful and I am not convinced that it works properly.
47//
48// This implementation is careful to disallow data exchange after an
49// SSL error, and it has an explicit SSL_CLOSED state. It should not
50// be possible to send any data in clear after one of the StartSSL
51// methods has been called.
52
53// Look in sslstreamadapter.h for documentation of the methods.
54
55class OpenSSLIdentity;
56
57///////////////////////////////////////////////////////////////////////////////
58
59class OpenSSLStreamAdapter : public SSLStreamAdapter {
60 public:
61 explicit OpenSSLStreamAdapter(StreamInterface* stream);
62 virtual ~OpenSSLStreamAdapter();
63
64 virtual void SetIdentity(SSLIdentity* identity);
65
66 // Default argument is for compatibility
67 virtual void SetServerRole(SSLRole role = SSL_SERVER);
68 virtual bool SetPeerCertificateDigest(const std::string& digest_alg,
69 const unsigned char* digest_val,
70 size_t digest_len);
71
72 virtual bool GetPeerCertificate(SSLCertificate** cert) const;
73
74 virtual int StartSSLWithServer(const char* server_name);
75 virtual int StartSSLWithPeer();
76 virtual void SetMode(SSLMode mode);
77
78 virtual StreamResult Read(void* data, size_t data_len,
79 size_t* read, int* error);
80 virtual StreamResult Write(const void* data, size_t data_len,
81 size_t* written, int* error);
82 virtual void Close();
83 virtual StreamState GetState() const;
84
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +000085 // Return the RFC (5246, 3268, etc.) cipher name for an OpenSSL cipher.
86 static const char* GetRfcSslCipherName(const SSL_CIPHER* cipher);
87
88 virtual bool GetSslCipher(std::string* cipher);
89
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000090 // Key Extractor interface
91 virtual bool ExportKeyingMaterial(const std::string& label,
92 const uint8* context,
93 size_t context_len,
94 bool use_context,
95 uint8* result,
96 size_t result_len);
97
98
99 // DTLS-SRTP interface
100 virtual bool SetDtlsSrtpCiphers(const std::vector<std::string>& ciphers);
101 virtual bool GetDtlsSrtpCipher(std::string* cipher);
102
103 // Capabilities interfaces
104 static bool HaveDtls();
105 static bool HaveDtlsSrtp();
106 static bool HaveExporter();
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000107 static std::string GetDefaultSslCipher();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000108
109 protected:
110 virtual void OnEvent(StreamInterface* stream, int events, int err);
111
112 private:
113 enum SSLState {
114 // Before calling one of the StartSSL methods, data flows
115 // in clear text.
116 SSL_NONE,
117 SSL_WAIT, // waiting for the stream to open to start SSL negotiation
118 SSL_CONNECTING, // SSL negotiation in progress
119 SSL_CONNECTED, // SSL stream successfully established
120 SSL_ERROR, // some SSL error occurred, stream is closed
121 SSL_CLOSED // Clean close
122 };
123
124 enum { MSG_TIMEOUT = MSG_MAX+1};
125
126 // The following three methods return 0 on success and a negative
127 // error code on failure. The error code may be from OpenSSL or -1
128 // on some other error cases, so it can't really be interpreted
129 // unfortunately.
130
131 // Go from state SSL_NONE to either SSL_CONNECTING or SSL_WAIT,
132 // depending on whether the underlying stream is already open or
133 // not.
134 int StartSSL();
135 // Prepare SSL library, state is SSL_CONNECTING.
136 int BeginSSL();
137 // Perform SSL negotiation steps.
138 int ContinueSSL();
139
140 // Error handler helper. signal is given as true for errors in
141 // asynchronous contexts (when an error method was not returned
142 // through some other method), and in that case an SE_CLOSE event is
143 // raised on the stream with the specified error.
144 // A 0 error means a graceful close, otherwise there is not really enough
145 // context to interpret the error code.
146 void Error(const char* context, int err, bool signal);
147 void Cleanup();
148
149 // Override MessageHandler
150 virtual void OnMessage(Message* msg);
151
152 // Flush the input buffers by reading left bytes (for DTLS)
153 void FlushInput(unsigned int left);
154
155 // SSL library configuration
156 SSL_CTX* SetupSSLContext();
157 // SSL verification check
158 bool SSLPostConnectionCheck(SSL* ssl, const char* server_name,
159 const X509* peer_cert,
160 const std::string& peer_digest);
161 // SSL certification verification error handler, called back from
162 // the openssl library. Returns an int interpreted as a boolean in
163 // the C style: zero means verification failure, non-zero means
164 // passed.
165 static int SSLVerifyCallback(int ok, X509_STORE_CTX* store);
166
167 SSLState state_;
168 SSLRole role_;
169 int ssl_error_code_; // valid when state_ == SSL_ERROR or SSL_CLOSED
170 // Whether the SSL negotiation is blocked on needing to read or
171 // write to the wrapped stream.
172 bool ssl_read_needs_write_;
173 bool ssl_write_needs_read_;
174
175 SSL* ssl_;
176 SSL_CTX* ssl_ctx_;
177
178 // Our key and certificate, mostly useful in peer-to-peer mode.
179 scoped_ptr<OpenSSLIdentity> identity_;
180 // in traditional mode, the server name that the server's certificate
181 // must specify. Empty in peer-to-peer mode.
182 std::string ssl_server_name_;
183 // The certificate that the peer must present or did present. Initially
184 // null in traditional mode, until the connection is established.
185 scoped_ptr<OpenSSLCertificate> peer_certificate_;
186 // In peer-to-peer mode, the digest of the certificate that
187 // the peer must present.
188 Buffer peer_certificate_digest_value_;
189 std::string peer_certificate_digest_algorithm_;
190
191 // OpenSSLAdapter::custom_verify_callback_ result
192 bool custom_verification_succeeded_;
193
194 // The DtlsSrtp ciphers
195 std::string srtp_ciphers_;
196
197 // Do DTLS or not
198 SSLMode ssl_mode_;
199};
200
201/////////////////////////////////////////////////////////////////////////////
202
203} // namespace rtc
204
205#endif // WEBRTC_BASE_OPENSSLSTREAMADAPTER_H__