blob: dcb802972c715c7c6e3d804e82eb8247cdb2bc24 [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_SSLSTREAMADAPTER_H_
12#define WEBRTC_BASE_SSLSTREAMADAPTER_H_
13
14#include <string>
15#include <vector>
16
17#include "webrtc/base/stream.h"
18#include "webrtc/base/sslidentity.h"
19
20namespace rtc {
21
22// SSLStreamAdapter : A StreamInterfaceAdapter that does SSL/TLS.
23// After SSL has been started, the stream will only open on successful
24// SSL verification of certificates, and the communication is
25// encrypted of course.
26//
27// This class was written with SSLAdapter as a starting point. It
28// offers a similar interface, with two differences: there is no
29// support for a restartable SSL connection, and this class has a
30// peer-to-peer mode.
31//
32// The SSL library requires initialization and cleanup. Static method
33// for doing this are in SSLAdapter. They should possibly be moved out
34// to a neutral class.
35
36
37enum SSLRole { SSL_CLIENT, SSL_SERVER };
38enum SSLMode { SSL_MODE_TLS, SSL_MODE_DTLS };
Joachim Bauch831c5582015-05-20 12:48:41 +020039enum SSLProtocolVersion {
40 SSL_PROTOCOL_TLS_10,
41 SSL_PROTOCOL_TLS_11,
42 SSL_PROTOCOL_TLS_12,
43 SSL_PROTOCOL_DTLS_10 = SSL_PROTOCOL_TLS_11,
44 SSL_PROTOCOL_DTLS_12 = SSL_PROTOCOL_TLS_12,
45};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046
47// Errors for Read -- in the high range so no conflict with OpenSSL.
48enum { SSE_MSG_TRUNC = 0xff0001 };
49
50class SSLStreamAdapter : public StreamAdapterInterface {
51 public:
52 // Instantiate an SSLStreamAdapter wrapping the given stream,
53 // (using the selected implementation for the platform).
54 // Caller is responsible for freeing the returned object.
55 static SSLStreamAdapter* Create(StreamInterface* stream);
56
57 explicit SSLStreamAdapter(StreamInterface* stream)
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +000058 : StreamAdapterInterface(stream), ignore_bad_cert_(false),
59 client_auth_enabled_(true) { }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060
61 void set_ignore_bad_cert(bool ignore) { ignore_bad_cert_ = ignore; }
62 bool ignore_bad_cert() const { return ignore_bad_cert_; }
63
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +000064 void set_client_auth_enabled(bool enabled) { client_auth_enabled_ = enabled; }
65 bool client_auth_enabled() const { return client_auth_enabled_; }
66
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067 // Specify our SSL identity: key and certificate. Mostly this is
68 // only used in the peer-to-peer mode (unless we actually want to
69 // provide a client certificate to a server).
70 // SSLStream takes ownership of the SSLIdentity object and will
71 // free it when appropriate. Should be called no more than once on a
72 // given SSLStream instance.
73 virtual void SetIdentity(SSLIdentity* identity) = 0;
74
75 // Call this to indicate that we are to play the server's role in
76 // the peer-to-peer mode.
77 // The default argument is for backward compatibility
78 // TODO(ekr@rtfm.com): rename this SetRole to reflect its new function
79 virtual void SetServerRole(SSLRole role = SSL_SERVER) = 0;
80
81 // Do DTLS or TLS
82 virtual void SetMode(SSLMode mode) = 0;
83
Joachim Bauch831c5582015-05-20 12:48:41 +020084 // Set maximum supported protocol version. The highest version supported by
85 // both ends will be used for the connection, i.e. if one party supports
86 // DTLS 1.0 and the other DTLS 1.2, DTLS 1.0 will be used.
87 // If requested version is not supported by underlying crypto library, the
88 // next lower will be used.
89 virtual void SetMaxProtocolVersion(SSLProtocolVersion version) = 0;
90
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000091 // The mode of operation is selected by calling either
92 // StartSSLWithServer or StartSSLWithPeer.
93 // Use of the stream prior to calling either of these functions will
94 // pass data in clear text.
95 // Calling one of these functions causes SSL negotiation to begin as
96 // soon as possible: right away if the underlying wrapped stream is
97 // already opened, or else as soon as it opens.
98 //
99 // These functions return a negative error code on failure.
100 // Returning 0 means success so far, but negotiation is probably not
101 // complete and will continue asynchronously. In that case, the
102 // exposed stream will open after successful negotiation and
103 // verification, or an SE_CLOSE event will be raised if negotiation
104 // fails.
105
106 // StartSSLWithServer starts SSL negotiation with a server in
107 // traditional mode. server_name specifies the expected server name
108 // which the server's certificate needs to specify.
109 virtual int StartSSLWithServer(const char* server_name) = 0;
110
111 // StartSSLWithPeer starts negotiation in the special peer-to-peer
112 // mode.
113 // Generally, SetIdentity() and possibly SetServerRole() should have
114 // been called before this.
115 // SetPeerCertificate() or SetPeerCertificateDigest() must also be called.
116 // It may be called after StartSSLWithPeer() but must be called before the
117 // underlying stream opens.
118 virtual int StartSSLWithPeer() = 0;
119
120 // Specify the digest of the certificate that our peer is expected to use in
121 // peer-to-peer mode. Only this certificate will be accepted during
122 // SSL verification. The certificate is assumed to have been
123 // obtained through some other secure channel (such as the XMPP
124 // channel). Unlike SetPeerCertificate(), this must specify the
125 // terminal certificate, not just a CA.
126 // SSLStream makes a copy of the digest value.
127 virtual bool SetPeerCertificateDigest(const std::string& digest_alg,
128 const unsigned char* digest_val,
129 size_t digest_len) = 0;
130
131 // Retrieves the peer's X.509 certificate, if a connection has been
132 // established. It returns the transmitted over SSL, including the entire
133 // chain. The returned certificate is owned by the caller.
134 virtual bool GetPeerCertificate(SSLCertificate** cert) const = 0;
135
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000136 // Retrieves the name of the cipher suite used for the connection
137 // (e.g. "TLS_RSA_WITH_AES_128_CBC_SHA").
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000138 virtual bool GetSslCipher(std::string* cipher);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000139
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000140 // Key Exporter interface from RFC 5705
141 // Arguments are:
142 // label -- the exporter label.
143 // part of the RFC defining each exporter
144 // usage (IN)
145 // context/context_len -- a context to bind to for this connection;
146 // optional, can be NULL, 0 (IN)
147 // use_context -- whether to use the context value
148 // (needed to distinguish no context from
149 // zero-length ones).
150 // result -- where to put the computed value
151 // result_len -- the length of the computed value
152 virtual bool ExportKeyingMaterial(const std::string& label,
153 const uint8* context,
154 size_t context_len,
155 bool use_context,
156 uint8* result,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000157 size_t result_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000158
159 // DTLS-SRTP interface
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000160 virtual bool SetDtlsSrtpCiphers(const std::vector<std::string>& ciphers);
161 virtual bool GetDtlsSrtpCipher(std::string* cipher);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000162
163 // Capabilities testing
164 static bool HaveDtls();
165 static bool HaveDtlsSrtp();
166 static bool HaveExporter();
167
Joachim Bauch831c5582015-05-20 12:48:41 +0200168 // Returns the default Ssl cipher used between streams of this class
169 // for the given protocol version. This is used by the unit tests.
170 static std::string GetDefaultSslCipher(SSLProtocolVersion version);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000171
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000172 private:
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000173 // If true, the server certificate need not match the configured
174 // server_name, and in fact missing certificate authority and other
175 // verification errors are ignored.
176 bool ignore_bad_cert_;
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000177
178 // If true (default), the client is required to provide a certificate during
179 // handshake. If no certificate is given, handshake fails. This applies to
180 // server mode only.
181 bool client_auth_enabled_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000182};
183
184} // namespace rtc
185
186#endif // WEBRTC_BASE_SSLSTREAMADAPTER_H_