henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | namespace 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 | |
| 37 | enum SSLRole { SSL_CLIENT, SSL_SERVER }; |
| 38 | enum SSLMode { SSL_MODE_TLS, SSL_MODE_DTLS }; |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 39 | enum 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 46 | |
| 47 | // Errors for Read -- in the high range so no conflict with OpenSSL. |
| 48 | enum { SSE_MSG_TRUNC = 0xff0001 }; |
| 49 | |
| 50 | class 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.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 58 | : StreamAdapterInterface(stream), ignore_bad_cert_(false), |
| 59 | client_auth_enabled_(true) { } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 60 | |
| 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.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 64 | void set_client_auth_enabled(bool enabled) { client_auth_enabled_ = enabled; } |
| 65 | bool client_auth_enabled() const { return client_auth_enabled_; } |
| 66 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 67 | // 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 Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 84 | // 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 91 | // 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.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 136 | // Retrieves the name of the cipher suite used for the connection |
| 137 | // (e.g. "TLS_RSA_WITH_AES_128_CBC_SHA"). |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 138 | virtual bool GetSslCipher(std::string* cipher); |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 139 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 140 | // 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.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 157 | size_t result_len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 158 | |
| 159 | // DTLS-SRTP interface |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 160 | virtual bool SetDtlsSrtpCiphers(const std::vector<std::string>& ciphers); |
| 161 | virtual bool GetDtlsSrtpCipher(std::string* cipher); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 162 | |
| 163 | // Capabilities testing |
| 164 | static bool HaveDtls(); |
| 165 | static bool HaveDtlsSrtp(); |
| 166 | static bool HaveExporter(); |
| 167 | |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 168 | // Returns the default Ssl cipher used between streams of this class |
| 169 | // for the given protocol version. This is used by the unit tests. |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 170 | // TODO(torbjorng@webrtc.org): Fix callers to avoid default parameter. |
| 171 | static std::string GetDefaultSslCipher(SSLProtocolVersion version, |
| 172 | KeyType key_type = KT_DEFAULT); |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 173 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 174 | private: |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 175 | // If true, the server certificate need not match the configured |
| 176 | // server_name, and in fact missing certificate authority and other |
| 177 | // verification errors are ignored. |
| 178 | bool ignore_bad_cert_; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 179 | |
| 180 | // If true (default), the client is required to provide a certificate during |
| 181 | // handshake. If no certificate is given, handshake fails. This applies to |
| 182 | // server mode only. |
| 183 | bool client_auth_enabled_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | } // namespace rtc |
| 187 | |
| 188 | #endif // WEBRTC_BASE_SSLSTREAMADAPTER_H_ |