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