henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [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 | |
jbauch | 1286d0e | 2016-04-26 10:13:22 | [diff] [blame^] | 14 | #include <memory> |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "webrtc/base/stream.h" |
| 19 | #include "webrtc/base/sslidentity.h" |
| 20 | |
| 21 | namespace rtc { |
| 22 | |
Guo-wei Shieh | 838c3b5 | 2015-11-19 03:41:53 | [diff] [blame] | 23 | // Constants for SSL profile. |
| 24 | const int TLS_NULL_WITH_NULL_NULL = 0; |
| 25 | |
Guo-wei Shieh | 5acc8ec | 2015-10-01 04:48:54 | [diff] [blame] | 26 | // Constants for SRTP profiles. |
Guo-wei Shieh | 838c3b5 | 2015-11-19 03:41:53 | [diff] [blame] | 27 | const int SRTP_INVALID_CRYPTO_SUITE = 0; |
torbjorng | d7d54cb | 2016-04-09 18:35:29 | [diff] [blame] | 28 | #ifndef SRTP_AES128_CM_SHA1_80 |
Guo-wei Shieh | 82d2585 | 2015-10-05 19:43:27 | [diff] [blame] | 29 | const int SRTP_AES128_CM_SHA1_80 = 0x0001; |
torbjorng | d7d54cb | 2016-04-09 18:35:29 | [diff] [blame] | 30 | #endif |
| 31 | #ifndef SRTP_AES128_CM_SHA1_32 |
Guo-wei Shieh | 82d2585 | 2015-10-05 19:43:27 | [diff] [blame] | 32 | const int SRTP_AES128_CM_SHA1_32 = 0x0002; |
torbjorng | d7d54cb | 2016-04-09 18:35:29 | [diff] [blame] | 33 | #endif |
Guo-wei Shieh | 5acc8ec | 2015-10-01 04:48:54 | [diff] [blame] | 34 | |
| 35 | // Cipher suite to use for SRTP. Typically a 80-bit HMAC will be used, except |
| 36 | // in applications (voice) where the additional bandwidth may be significant. |
| 37 | // A 80-bit HMAC is always used for SRTCP. |
| 38 | // 128-bit AES with 80-bit SHA-1 HMAC. |
| 39 | extern const char CS_AES_CM_128_HMAC_SHA1_80[]; |
| 40 | // 128-bit AES with 32-bit SHA-1 HMAC. |
| 41 | extern const char CS_AES_CM_128_HMAC_SHA1_32[]; |
| 42 | |
Guo-wei Shieh | 838c3b5 | 2015-11-19 03:41:53 | [diff] [blame] | 43 | // Given the DTLS-SRTP protection profile ID, as defined in |
| 44 | // https://tools.ietf.org/html/rfc4568#section-6.2 , return the SRTP profile |
| 45 | // name, as defined in https://tools.ietf.org/html/rfc5764#section-4.1.2. |
| 46 | std::string SrtpCryptoSuiteToName(int crypto_suite); |
| 47 | |
| 48 | // The reverse of above conversion. |
| 49 | int SrtpCryptoSuiteFromName(const std::string& crypto_suite); |
Guo-wei Shieh | 5acc8ec | 2015-10-01 04:48:54 | [diff] [blame] | 50 | |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 51 | // SSLStreamAdapter : A StreamInterfaceAdapter that does SSL/TLS. |
| 52 | // After SSL has been started, the stream will only open on successful |
| 53 | // SSL verification of certificates, and the communication is |
| 54 | // encrypted of course. |
| 55 | // |
| 56 | // This class was written with SSLAdapter as a starting point. It |
| 57 | // offers a similar interface, with two differences: there is no |
| 58 | // support for a restartable SSL connection, and this class has a |
| 59 | // peer-to-peer mode. |
| 60 | // |
| 61 | // The SSL library requires initialization and cleanup. Static method |
| 62 | // for doing this are in SSLAdapter. They should possibly be moved out |
| 63 | // to a neutral class. |
| 64 | |
| 65 | |
| 66 | enum SSLRole { SSL_CLIENT, SSL_SERVER }; |
| 67 | enum SSLMode { SSL_MODE_TLS, SSL_MODE_DTLS }; |
Joachim Bauch | 1bb60c8 | 2015-05-20 10:48:41 | [diff] [blame] | 68 | enum SSLProtocolVersion { |
| 69 | SSL_PROTOCOL_TLS_10, |
| 70 | SSL_PROTOCOL_TLS_11, |
| 71 | SSL_PROTOCOL_TLS_12, |
| 72 | SSL_PROTOCOL_DTLS_10 = SSL_PROTOCOL_TLS_11, |
| 73 | SSL_PROTOCOL_DTLS_12 = SSL_PROTOCOL_TLS_12, |
| 74 | }; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 75 | |
| 76 | // Errors for Read -- in the high range so no conflict with OpenSSL. |
| 77 | enum { SSE_MSG_TRUNC = 0xff0001 }; |
| 78 | |
| 79 | class SSLStreamAdapter : public StreamAdapterInterface { |
| 80 | public: |
| 81 | // Instantiate an SSLStreamAdapter wrapping the given stream, |
| 82 | // (using the selected implementation for the platform). |
| 83 | // Caller is responsible for freeing the returned object. |
| 84 | static SSLStreamAdapter* Create(StreamInterface* stream); |
| 85 | |
| 86 | explicit SSLStreamAdapter(StreamInterface* stream) |
tkchin@webrtc.org | 4f81cfb | 2014-09-23 05:56:44 | [diff] [blame] | 87 | : StreamAdapterInterface(stream), ignore_bad_cert_(false), |
| 88 | client_auth_enabled_(true) { } |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 89 | |
| 90 | void set_ignore_bad_cert(bool ignore) { ignore_bad_cert_ = ignore; } |
| 91 | bool ignore_bad_cert() const { return ignore_bad_cert_; } |
| 92 | |
tkchin@webrtc.org | 4f81cfb | 2014-09-23 05:56:44 | [diff] [blame] | 93 | void set_client_auth_enabled(bool enabled) { client_auth_enabled_ = enabled; } |
| 94 | bool client_auth_enabled() const { return client_auth_enabled_; } |
| 95 | |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 96 | // Specify our SSL identity: key and certificate. Mostly this is |
| 97 | // only used in the peer-to-peer mode (unless we actually want to |
| 98 | // provide a client certificate to a server). |
| 99 | // SSLStream takes ownership of the SSLIdentity object and will |
| 100 | // free it when appropriate. Should be called no more than once on a |
| 101 | // given SSLStream instance. |
| 102 | virtual void SetIdentity(SSLIdentity* identity) = 0; |
| 103 | |
| 104 | // Call this to indicate that we are to play the server's role in |
| 105 | // the peer-to-peer mode. |
| 106 | // The default argument is for backward compatibility |
| 107 | // TODO(ekr@rtfm.com): rename this SetRole to reflect its new function |
| 108 | virtual void SetServerRole(SSLRole role = SSL_SERVER) = 0; |
| 109 | |
| 110 | // Do DTLS or TLS |
| 111 | virtual void SetMode(SSLMode mode) = 0; |
| 112 | |
Joachim Bauch | 1bb60c8 | 2015-05-20 10:48:41 | [diff] [blame] | 113 | // Set maximum supported protocol version. The highest version supported by |
| 114 | // both ends will be used for the connection, i.e. if one party supports |
| 115 | // DTLS 1.0 and the other DTLS 1.2, DTLS 1.0 will be used. |
| 116 | // If requested version is not supported by underlying crypto library, the |
| 117 | // next lower will be used. |
| 118 | virtual void SetMaxProtocolVersion(SSLProtocolVersion version) = 0; |
| 119 | |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 120 | // The mode of operation is selected by calling either |
| 121 | // StartSSLWithServer or StartSSLWithPeer. |
| 122 | // Use of the stream prior to calling either of these functions will |
| 123 | // pass data in clear text. |
| 124 | // Calling one of these functions causes SSL negotiation to begin as |
| 125 | // soon as possible: right away if the underlying wrapped stream is |
| 126 | // already opened, or else as soon as it opens. |
| 127 | // |
| 128 | // These functions return a negative error code on failure. |
| 129 | // Returning 0 means success so far, but negotiation is probably not |
| 130 | // complete and will continue asynchronously. In that case, the |
| 131 | // exposed stream will open after successful negotiation and |
| 132 | // verification, or an SE_CLOSE event will be raised if negotiation |
| 133 | // fails. |
| 134 | |
| 135 | // StartSSLWithServer starts SSL negotiation with a server in |
| 136 | // traditional mode. server_name specifies the expected server name |
| 137 | // which the server's certificate needs to specify. |
| 138 | virtual int StartSSLWithServer(const char* server_name) = 0; |
| 139 | |
| 140 | // StartSSLWithPeer starts negotiation in the special peer-to-peer |
| 141 | // mode. |
| 142 | // Generally, SetIdentity() and possibly SetServerRole() should have |
| 143 | // been called before this. |
| 144 | // SetPeerCertificate() or SetPeerCertificateDigest() must also be called. |
| 145 | // It may be called after StartSSLWithPeer() but must be called before the |
| 146 | // underlying stream opens. |
| 147 | virtual int StartSSLWithPeer() = 0; |
| 148 | |
| 149 | // Specify the digest of the certificate that our peer is expected to use in |
| 150 | // peer-to-peer mode. Only this certificate will be accepted during |
| 151 | // SSL verification. The certificate is assumed to have been |
| 152 | // obtained through some other secure channel (such as the XMPP |
| 153 | // channel). Unlike SetPeerCertificate(), this must specify the |
| 154 | // terminal certificate, not just a CA. |
| 155 | // SSLStream makes a copy of the digest value. |
| 156 | virtual bool SetPeerCertificateDigest(const std::string& digest_alg, |
| 157 | const unsigned char* digest_val, |
| 158 | size_t digest_len) = 0; |
| 159 | |
| 160 | // Retrieves the peer's X.509 certificate, if a connection has been |
| 161 | // established. It returns the transmitted over SSL, including the entire |
kwiberg | b137c21 | 2016-04-06 12:15:06 | [diff] [blame] | 162 | // chain. |
jbauch | 1286d0e | 2016-04-26 10:13:22 | [diff] [blame^] | 163 | virtual std::unique_ptr<SSLCertificate> GetPeerCertificate() const = 0; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 164 | |
Guo-wei Shieh | 5acc8ec | 2015-10-01 04:48:54 | [diff] [blame] | 165 | // Retrieves the IANA registration id of the cipher suite used for the |
| 166 | // connection (e.g. 0x2F for "TLS_RSA_WITH_AES_128_CBC_SHA"). |
Guo-wei Shieh | 838c3b5 | 2015-11-19 03:41:53 | [diff] [blame] | 167 | virtual bool GetSslCipherSuite(int* cipher_suite); |
pthatcher@webrtc.org | 2fb6991 | 2015-02-11 22:34:36 | [diff] [blame] | 168 | |
torbjorng | dea3f23 | 2016-03-11 08:06:47 | [diff] [blame] | 169 | virtual int GetSslVersion() const = 0; |
| 170 | |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 171 | // Key Exporter interface from RFC 5705 |
| 172 | // Arguments are: |
| 173 | // label -- the exporter label. |
| 174 | // part of the RFC defining each exporter |
| 175 | // usage (IN) |
| 176 | // context/context_len -- a context to bind to for this connection; |
| 177 | // optional, can be NULL, 0 (IN) |
| 178 | // use_context -- whether to use the context value |
| 179 | // (needed to distinguish no context from |
| 180 | // zero-length ones). |
| 181 | // result -- where to put the computed value |
| 182 | // result_len -- the length of the computed value |
| 183 | virtual bool ExportKeyingMaterial(const std::string& label, |
Peter Boström | 07e22e6 | 2015-10-07 10:23:21 | [diff] [blame] | 184 | const uint8_t* context, |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 185 | size_t context_len, |
| 186 | bool use_context, |
Peter Boström | 07e22e6 | 2015-10-07 10:23:21 | [diff] [blame] | 187 | uint8_t* result, |
kwiberg@webrtc.org | 786b634 | 2015-03-09 22:21:53 | [diff] [blame] | 188 | size_t result_len); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 189 | |
| 190 | // DTLS-SRTP interface |
Guo-wei Shieh | 838c3b5 | 2015-11-19 03:41:53 | [diff] [blame] | 191 | virtual bool SetDtlsSrtpCryptoSuites(const std::vector<int>& crypto_suites); |
| 192 | virtual bool GetDtlsSrtpCryptoSuite(int* crypto_suite); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 193 | |
| 194 | // Capabilities testing |
| 195 | static bool HaveDtls(); |
| 196 | static bool HaveDtlsSrtp(); |
| 197 | static bool HaveExporter(); |
| 198 | |
torbjorng | dea3f23 | 2016-03-11 08:06:47 | [diff] [blame] | 199 | // Returns true iff the supplied cipher is deemed to be strong. |
| 200 | // TODO(torbjorng): Consider removing the KeyType argument. |
| 201 | static bool IsAcceptableCipher(int cipher, KeyType key_type); |
| 202 | static bool IsAcceptableCipher(const std::string& cipher, KeyType key_type); |
Guo-wei Shieh | 5acc8ec | 2015-10-01 04:48:54 | [diff] [blame] | 203 | |
| 204 | // TODO(guoweis): Move this away from a static class method. Currently this is |
| 205 | // introduced such that any caller could depend on sslstreamadapter.h without |
| 206 | // depending on specific SSL implementation. |
Guo-wei Shieh | 838c3b5 | 2015-11-19 03:41:53 | [diff] [blame] | 207 | static std::string SslCipherSuiteToName(int cipher_suite); |
pthatcher@webrtc.org | 2fb6991 | 2015-02-11 22:34:36 | [diff] [blame] | 208 | |
tkchin@webrtc.org | 4f81cfb | 2014-09-23 05:56:44 | [diff] [blame] | 209 | private: |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 210 | // If true, the server certificate need not match the configured |
| 211 | // server_name, and in fact missing certificate authority and other |
| 212 | // verification errors are ignored. |
| 213 | bool ignore_bad_cert_; |
tkchin@webrtc.org | 4f81cfb | 2014-09-23 05:56:44 | [diff] [blame] | 214 | |
| 215 | // If true (default), the client is required to provide a certificate during |
| 216 | // handshake. If no certificate is given, handshake fails. This applies to |
| 217 | // server mode only. |
| 218 | bool client_auth_enabled_; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | } // namespace rtc |
| 222 | |
| 223 | #endif // WEBRTC_BASE_SSLSTREAMADAPTER_H_ |