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