You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1184 lines
34 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "rtc_base/openssl_stream_adapter.h"
  11. #include <openssl/bio.h>
  12. #include <openssl/crypto.h>
  13. #include <openssl/err.h>
  14. #include <openssl/rand.h>
  15. #include <openssl/tls1.h>
  16. #include <openssl/x509v3.h>
  17. #ifndef OPENSSL_IS_BORINGSSL
  18. #include <openssl/dtls1.h>
  19. #include <openssl/ssl.h>
  20. #endif
  21. #include <memory>
  22. #include <utility>
  23. #include <vector>
  24. #include "rtc_base/checks.h"
  25. #include "rtc_base/logging.h"
  26. #include "rtc_base/numerics/safe_conversions.h"
  27. #include "rtc_base/openssl.h"
  28. #include "rtc_base/openssl_adapter.h"
  29. #include "rtc_base/openssl_digest.h"
  30. #include "rtc_base/openssl_identity.h"
  31. #include "rtc_base/ssl_certificate.h"
  32. #include "rtc_base/stream.h"
  33. #include "rtc_base/thread.h"
  34. #include "rtc_base/time_utils.h"
  35. #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
  36. #error "webrtc requires at least OpenSSL version 1.1.0, to support DTLS-SRTP"
  37. #endif
  38. // Defines for the TLS Cipher Suite Map.
  39. #define DEFINE_CIPHER_ENTRY_SSL3(name) \
  40. { SSL3_CK_##name, "TLS_" #name }
  41. #define DEFINE_CIPHER_ENTRY_TLS1(name) \
  42. { TLS1_CK_##name, "TLS_" #name }
  43. namespace rtc {
  44. namespace {
  45. // SRTP cipher suite table. |internal_name| is used to construct a
  46. // colon-separated profile strings which is needed by
  47. // SSL_CTX_set_tlsext_use_srtp().
  48. struct SrtpCipherMapEntry {
  49. const char* internal_name;
  50. const int id;
  51. };
  52. // Cipher name table. Maps internal OpenSSL cipher ids to the RFC name.
  53. struct SslCipherMapEntry {
  54. uint32_t openssl_id;
  55. const char* rfc_name;
  56. };
  57. // This isn't elegant, but it's better than an external reference
  58. constexpr SrtpCipherMapEntry kSrtpCipherMap[] = {
  59. {"SRTP_AES128_CM_SHA1_80", SRTP_AES128_CM_SHA1_80},
  60. {"SRTP_AES128_CM_SHA1_32", SRTP_AES128_CM_SHA1_32},
  61. {"SRTP_AEAD_AES_128_GCM", SRTP_AEAD_AES_128_GCM},
  62. {"SRTP_AEAD_AES_256_GCM", SRTP_AEAD_AES_256_GCM}};
  63. #ifndef OPENSSL_IS_BORINGSSL
  64. // The "SSL_CIPHER_standard_name" function is only available in OpenSSL when
  65. // compiled with tracing, so we need to define the mapping manually here.
  66. constexpr SslCipherMapEntry kSslCipherMap[] = {
  67. // TLS v1.0 ciphersuites from RFC2246.
  68. DEFINE_CIPHER_ENTRY_SSL3(RSA_RC4_128_SHA),
  69. {SSL3_CK_RSA_DES_192_CBC3_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA"},
  70. // AES ciphersuites from RFC3268.
  71. {TLS1_CK_RSA_WITH_AES_128_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA"},
  72. {TLS1_CK_DHE_RSA_WITH_AES_128_SHA, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA"},
  73. {TLS1_CK_RSA_WITH_AES_256_SHA, "TLS_RSA_WITH_AES_256_CBC_SHA"},
  74. {TLS1_CK_DHE_RSA_WITH_AES_256_SHA, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA"},
  75. // ECC ciphersuites from RFC4492.
  76. DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_RC4_128_SHA),
  77. {TLS1_CK_ECDHE_ECDSA_WITH_DES_192_CBC3_SHA,
  78. "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA"},
  79. DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_128_CBC_SHA),
  80. DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_256_CBC_SHA),
  81. DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_RC4_128_SHA),
  82. {TLS1_CK_ECDHE_RSA_WITH_DES_192_CBC3_SHA,
  83. "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA"},
  84. DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_128_CBC_SHA),
  85. DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_256_CBC_SHA),
  86. // TLS v1.2 ciphersuites.
  87. {TLS1_CK_RSA_WITH_AES_128_SHA256, "TLS_RSA_WITH_AES_128_CBC_SHA256"},
  88. {TLS1_CK_RSA_WITH_AES_256_SHA256, "TLS_RSA_WITH_AES_256_CBC_SHA256"},
  89. {TLS1_CK_DHE_RSA_WITH_AES_128_SHA256,
  90. "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256"},
  91. {TLS1_CK_DHE_RSA_WITH_AES_256_SHA256,
  92. "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256"},
  93. // TLS v1.2 GCM ciphersuites from RFC5288.
  94. DEFINE_CIPHER_ENTRY_TLS1(RSA_WITH_AES_128_GCM_SHA256),
  95. DEFINE_CIPHER_ENTRY_TLS1(RSA_WITH_AES_256_GCM_SHA384),
  96. DEFINE_CIPHER_ENTRY_TLS1(DHE_RSA_WITH_AES_128_GCM_SHA256),
  97. DEFINE_CIPHER_ENTRY_TLS1(DHE_RSA_WITH_AES_256_GCM_SHA384),
  98. DEFINE_CIPHER_ENTRY_TLS1(DH_RSA_WITH_AES_128_GCM_SHA256),
  99. DEFINE_CIPHER_ENTRY_TLS1(DH_RSA_WITH_AES_256_GCM_SHA384),
  100. // ECDH HMAC based ciphersuites from RFC5289.
  101. {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256,
  102. "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256"},
  103. {TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384,
  104. "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384"},
  105. {TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
  106. "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"},
  107. {TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384,
  108. "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384"},
  109. // ECDH GCM based ciphersuites from RFC5289.
  110. DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_128_GCM_SHA256),
  111. DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_256_GCM_SHA384),
  112. DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_128_GCM_SHA256),
  113. DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_256_GCM_SHA384),
  114. {0, nullptr}};
  115. #endif // #ifndef OPENSSL_IS_BORINGSSL
  116. #ifdef OPENSSL_IS_BORINGSSL
  117. // Enabled by EnableTimeCallbackForTesting. Should never be set in production
  118. // code.
  119. bool g_use_time_callback_for_testing = false;
  120. // Not used in production code. Actual time should be relative to Jan 1, 1970.
  121. void TimeCallbackForTesting(const SSL* ssl, struct timeval* out_clock) {
  122. int64_t time = TimeNanos();
  123. out_clock->tv_sec = time / kNumNanosecsPerSec;
  124. out_clock->tv_usec = (time % kNumNanosecsPerSec) / kNumNanosecsPerMicrosec;
  125. }
  126. #endif
  127. } // namespace
  128. //////////////////////////////////////////////////////////////////////
  129. // StreamBIO
  130. //////////////////////////////////////////////////////////////////////
  131. static int stream_write(BIO* h, const char* buf, int num);
  132. static int stream_read(BIO* h, char* buf, int size);
  133. static int stream_puts(BIO* h, const char* str);
  134. static long stream_ctrl(BIO* h, int cmd, long arg1, void* arg2);
  135. static int stream_new(BIO* h);
  136. static int stream_free(BIO* data);
  137. static BIO_METHOD* BIO_stream_method() {
  138. static BIO_METHOD* method = [] {
  139. BIO_METHOD* method = BIO_meth_new(BIO_TYPE_BIO, "stream");
  140. BIO_meth_set_write(method, stream_write);
  141. BIO_meth_set_read(method, stream_read);
  142. BIO_meth_set_puts(method, stream_puts);
  143. BIO_meth_set_ctrl(method, stream_ctrl);
  144. BIO_meth_set_create(method, stream_new);
  145. BIO_meth_set_destroy(method, stream_free);
  146. return method;
  147. }();
  148. return method;
  149. }
  150. static BIO* BIO_new_stream(StreamInterface* stream) {
  151. BIO* ret = BIO_new(BIO_stream_method());
  152. if (ret == nullptr) {
  153. return nullptr;
  154. }
  155. BIO_set_data(ret, stream);
  156. return ret;
  157. }
  158. // bio methods return 1 (or at least non-zero) on success and 0 on failure.
  159. static int stream_new(BIO* b) {
  160. BIO_set_shutdown(b, 0);
  161. BIO_set_init(b, 1);
  162. BIO_set_data(b, 0);
  163. return 1;
  164. }
  165. static int stream_free(BIO* b) {
  166. if (b == nullptr) {
  167. return 0;
  168. }
  169. return 1;
  170. }
  171. static int stream_read(BIO* b, char* out, int outl) {
  172. if (!out) {
  173. return -1;
  174. }
  175. StreamInterface* stream = static_cast<StreamInterface*>(BIO_get_data(b));
  176. BIO_clear_retry_flags(b);
  177. size_t read;
  178. int error;
  179. StreamResult result = stream->Read(out, outl, &read, &error);
  180. if (result == SR_SUCCESS) {
  181. return checked_cast<int>(read);
  182. } else if (result == SR_BLOCK) {
  183. BIO_set_retry_read(b);
  184. }
  185. return -1;
  186. }
  187. static int stream_write(BIO* b, const char* in, int inl) {
  188. if (!in) {
  189. return -1;
  190. }
  191. StreamInterface* stream = static_cast<StreamInterface*>(BIO_get_data(b));
  192. BIO_clear_retry_flags(b);
  193. size_t written;
  194. int error;
  195. StreamResult result = stream->Write(in, inl, &written, &error);
  196. if (result == SR_SUCCESS) {
  197. return checked_cast<int>(written);
  198. } else if (result == SR_BLOCK) {
  199. BIO_set_retry_write(b);
  200. }
  201. return -1;
  202. }
  203. static int stream_puts(BIO* b, const char* str) {
  204. return stream_write(b, str, checked_cast<int>(strlen(str)));
  205. }
  206. static long stream_ctrl(BIO* b, int cmd, long num, void* ptr) {
  207. switch (cmd) {
  208. case BIO_CTRL_RESET:
  209. return 0;
  210. case BIO_CTRL_EOF: {
  211. StreamInterface* stream = static_cast<StreamInterface*>(ptr);
  212. // 1 means end-of-stream.
  213. return (stream->GetState() == SS_CLOSED) ? 1 : 0;
  214. }
  215. case BIO_CTRL_WPENDING:
  216. case BIO_CTRL_PENDING:
  217. return 0;
  218. case BIO_CTRL_FLUSH:
  219. return 1;
  220. case BIO_CTRL_DGRAM_QUERY_MTU:
  221. // openssl defaults to mtu=256 unless we return something here.
  222. // The handshake doesn't actually need to send packets above 1k,
  223. // so this seems like a sensible value that should work in most cases.
  224. // Webrtc uses the same value for video packets.
  225. return 1200;
  226. default:
  227. return 0;
  228. }
  229. }
  230. /////////////////////////////////////////////////////////////////////////////
  231. // OpenSSLStreamAdapter
  232. /////////////////////////////////////////////////////////////////////////////
  233. OpenSSLStreamAdapter::OpenSSLStreamAdapter(StreamInterface* stream)
  234. : SSLStreamAdapter(stream),
  235. state_(SSL_NONE),
  236. role_(SSL_CLIENT),
  237. ssl_read_needs_write_(false),
  238. ssl_write_needs_read_(false),
  239. ssl_(nullptr),
  240. ssl_ctx_(nullptr),
  241. ssl_mode_(SSL_MODE_TLS),
  242. ssl_max_version_(SSL_PROTOCOL_TLS_12) {}
  243. OpenSSLStreamAdapter::~OpenSSLStreamAdapter() {
  244. Cleanup(0);
  245. }
  246. void OpenSSLStreamAdapter::SetIdentity(SSLIdentity* identity) {
  247. RTC_DCHECK(!identity_);
  248. identity_.reset(static_cast<OpenSSLIdentity*>(identity));
  249. }
  250. void OpenSSLStreamAdapter::SetServerRole(SSLRole role) {
  251. role_ = role;
  252. }
  253. bool OpenSSLStreamAdapter::SetPeerCertificateDigest(
  254. const std::string& digest_alg,
  255. const unsigned char* digest_val,
  256. size_t digest_len,
  257. SSLPeerCertificateDigestError* error) {
  258. RTC_DCHECK(!peer_certificate_verified_);
  259. RTC_DCHECK(!HasPeerCertificateDigest());
  260. size_t expected_len;
  261. if (error) {
  262. *error = SSLPeerCertificateDigestError::NONE;
  263. }
  264. if (!OpenSSLDigest::GetDigestSize(digest_alg, &expected_len)) {
  265. RTC_LOG(LS_WARNING) << "Unknown digest algorithm: " << digest_alg;
  266. if (error) {
  267. *error = SSLPeerCertificateDigestError::UNKNOWN_ALGORITHM;
  268. }
  269. return false;
  270. }
  271. if (expected_len != digest_len) {
  272. if (error) {
  273. *error = SSLPeerCertificateDigestError::INVALID_LENGTH;
  274. }
  275. return false;
  276. }
  277. peer_certificate_digest_value_.SetData(digest_val, digest_len);
  278. peer_certificate_digest_algorithm_ = digest_alg;
  279. if (!peer_cert_chain_) {
  280. // Normal case, where the digest is set before we obtain the certificate
  281. // from the handshake.
  282. return true;
  283. }
  284. if (!VerifyPeerCertificate()) {
  285. Error("SetPeerCertificateDigest", -1, SSL_AD_BAD_CERTIFICATE, false);
  286. if (error) {
  287. *error = SSLPeerCertificateDigestError::VERIFICATION_FAILED;
  288. }
  289. return false;
  290. }
  291. if (state_ == SSL_CONNECTED) {
  292. // Post the event asynchronously to unwind the stack. The caller
  293. // of ContinueSSL may be the same object listening for these
  294. // events and may not be prepared for reentrancy.
  295. PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0);
  296. }
  297. return true;
  298. }
  299. std::string OpenSSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) {
  300. #ifdef OPENSSL_IS_BORINGSSL
  301. const SSL_CIPHER* ssl_cipher = SSL_get_cipher_by_value(cipher_suite);
  302. if (!ssl_cipher) {
  303. return std::string();
  304. }
  305. return SSL_CIPHER_standard_name(ssl_cipher);
  306. #else
  307. for (const SslCipherMapEntry* entry = kSslCipherMap; entry->rfc_name;
  308. ++entry) {
  309. if (cipher_suite == static_cast<int>(entry->openssl_id)) {
  310. return entry->rfc_name;
  311. }
  312. }
  313. return std::string();
  314. #endif
  315. }
  316. bool OpenSSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) {
  317. if (state_ != SSL_CONNECTED) {
  318. return false;
  319. }
  320. const SSL_CIPHER* current_cipher = SSL_get_current_cipher(ssl_);
  321. if (current_cipher == nullptr) {
  322. return false;
  323. }
  324. *cipher_suite = static_cast<uint16_t>(SSL_CIPHER_get_id(current_cipher));
  325. return true;
  326. }
  327. int OpenSSLStreamAdapter::GetSslVersion() const {
  328. if (state_ != SSL_CONNECTED) {
  329. return -1;
  330. }
  331. int ssl_version = SSL_version(ssl_);
  332. if (ssl_mode_ == SSL_MODE_DTLS) {
  333. if (ssl_version == DTLS1_VERSION) {
  334. return SSL_PROTOCOL_DTLS_10;
  335. } else if (ssl_version == DTLS1_2_VERSION) {
  336. return SSL_PROTOCOL_DTLS_12;
  337. }
  338. } else {
  339. if (ssl_version == TLS1_VERSION) {
  340. return SSL_PROTOCOL_TLS_10;
  341. } else if (ssl_version == TLS1_1_VERSION) {
  342. return SSL_PROTOCOL_TLS_11;
  343. } else if (ssl_version == TLS1_2_VERSION) {
  344. return SSL_PROTOCOL_TLS_12;
  345. }
  346. }
  347. return -1;
  348. }
  349. // Key Extractor interface
  350. bool OpenSSLStreamAdapter::ExportKeyingMaterial(const std::string& label,
  351. const uint8_t* context,
  352. size_t context_len,
  353. bool use_context,
  354. uint8_t* result,
  355. size_t result_len) {
  356. if (SSL_export_keying_material(ssl_, result, result_len, label.c_str(),
  357. label.length(), const_cast<uint8_t*>(context),
  358. context_len, use_context) != 1) {
  359. return false;
  360. }
  361. return true;
  362. }
  363. bool OpenSSLStreamAdapter::SetDtlsSrtpCryptoSuites(
  364. const std::vector<int>& ciphers) {
  365. if (state_ != SSL_NONE) {
  366. return false;
  367. }
  368. std::string internal_ciphers;
  369. for (const int cipher : ciphers) {
  370. bool found = false;
  371. for (const auto& entry : kSrtpCipherMap) {
  372. if (cipher == entry.id) {
  373. found = true;
  374. if (!internal_ciphers.empty()) {
  375. internal_ciphers += ":";
  376. }
  377. internal_ciphers += entry.internal_name;
  378. break;
  379. }
  380. }
  381. if (!found) {
  382. RTC_LOG(LS_ERROR) << "Could not find cipher: " << cipher;
  383. return false;
  384. }
  385. }
  386. if (internal_ciphers.empty()) {
  387. return false;
  388. }
  389. srtp_ciphers_ = internal_ciphers;
  390. return true;
  391. }
  392. bool OpenSSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) {
  393. RTC_DCHECK(state_ == SSL_CONNECTED);
  394. if (state_ != SSL_CONNECTED) {
  395. return false;
  396. }
  397. const SRTP_PROTECTION_PROFILE* srtp_profile =
  398. SSL_get_selected_srtp_profile(ssl_);
  399. if (!srtp_profile) {
  400. return false;
  401. }
  402. *crypto_suite = srtp_profile->id;
  403. RTC_DCHECK(!SrtpCryptoSuiteToName(*crypto_suite).empty());
  404. return true;
  405. }
  406. bool OpenSSLStreamAdapter::IsTlsConnected() {
  407. return state_ == SSL_CONNECTED;
  408. }
  409. int OpenSSLStreamAdapter::StartSSL() {
  410. // Don't allow StartSSL to be called twice.
  411. if (state_ != SSL_NONE) {
  412. return -1;
  413. }
  414. if (StreamAdapterInterface::GetState() != SS_OPEN) {
  415. state_ = SSL_WAIT;
  416. return 0;
  417. }
  418. state_ = SSL_CONNECTING;
  419. if (int err = BeginSSL()) {
  420. Error("BeginSSL", err, 0, false);
  421. return err;
  422. }
  423. return 0;
  424. }
  425. void OpenSSLStreamAdapter::SetMode(SSLMode mode) {
  426. RTC_DCHECK(state_ == SSL_NONE);
  427. ssl_mode_ = mode;
  428. }
  429. void OpenSSLStreamAdapter::SetMaxProtocolVersion(SSLProtocolVersion version) {
  430. RTC_DCHECK(ssl_ctx_ == nullptr);
  431. ssl_max_version_ = version;
  432. }
  433. void OpenSSLStreamAdapter::SetInitialRetransmissionTimeout(int timeout_ms) {
  434. RTC_DCHECK(ssl_ctx_ == nullptr);
  435. dtls_handshake_timeout_ms_ = timeout_ms;
  436. }
  437. //
  438. // StreamInterface Implementation
  439. //
  440. StreamResult OpenSSLStreamAdapter::Write(const void* data,
  441. size_t data_len,
  442. size_t* written,
  443. int* error) {
  444. RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Write(" << data_len << ")";
  445. switch (state_) {
  446. case SSL_NONE:
  447. // pass-through in clear text
  448. return StreamAdapterInterface::Write(data, data_len, written, error);
  449. case SSL_WAIT:
  450. case SSL_CONNECTING:
  451. return SR_BLOCK;
  452. case SSL_CONNECTED:
  453. if (WaitingToVerifyPeerCertificate()) {
  454. return SR_BLOCK;
  455. }
  456. break;
  457. case SSL_ERROR:
  458. case SSL_CLOSED:
  459. default:
  460. if (error) {
  461. *error = ssl_error_code_;
  462. }
  463. return SR_ERROR;
  464. }
  465. // OpenSSL will return an error if we try to write zero bytes
  466. if (data_len == 0) {
  467. if (written) {
  468. *written = 0;
  469. }
  470. return SR_SUCCESS;
  471. }
  472. ssl_write_needs_read_ = false;
  473. int code = SSL_write(ssl_, data, checked_cast<int>(data_len));
  474. int ssl_error = SSL_get_error(ssl_, code);
  475. switch (ssl_error) {
  476. case SSL_ERROR_NONE:
  477. RTC_LOG(LS_VERBOSE) << " -- success";
  478. RTC_DCHECK_GT(code, 0);
  479. RTC_DCHECK_LE(code, data_len);
  480. if (written)
  481. *written = code;
  482. return SR_SUCCESS;
  483. case SSL_ERROR_WANT_READ:
  484. RTC_LOG(LS_VERBOSE) << " -- error want read";
  485. ssl_write_needs_read_ = true;
  486. return SR_BLOCK;
  487. case SSL_ERROR_WANT_WRITE:
  488. RTC_LOG(LS_VERBOSE) << " -- error want write";
  489. return SR_BLOCK;
  490. case SSL_ERROR_ZERO_RETURN:
  491. default:
  492. Error("SSL_write", (ssl_error ? ssl_error : -1), 0, false);
  493. if (error) {
  494. *error = ssl_error_code_;
  495. }
  496. return SR_ERROR;
  497. }
  498. // not reached
  499. }
  500. StreamResult OpenSSLStreamAdapter::Read(void* data,
  501. size_t data_len,
  502. size_t* read,
  503. int* error) {
  504. RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Read(" << data_len << ")";
  505. switch (state_) {
  506. case SSL_NONE:
  507. // pass-through in clear text
  508. return StreamAdapterInterface::Read(data, data_len, read, error);
  509. case SSL_WAIT:
  510. case SSL_CONNECTING:
  511. return SR_BLOCK;
  512. case SSL_CONNECTED:
  513. if (WaitingToVerifyPeerCertificate()) {
  514. return SR_BLOCK;
  515. }
  516. break;
  517. case SSL_CLOSED:
  518. return SR_EOS;
  519. case SSL_ERROR:
  520. default:
  521. if (error) {
  522. *error = ssl_error_code_;
  523. }
  524. return SR_ERROR;
  525. }
  526. // Don't trust OpenSSL with zero byte reads
  527. if (data_len == 0) {
  528. if (read) {
  529. *read = 0;
  530. }
  531. return SR_SUCCESS;
  532. }
  533. ssl_read_needs_write_ = false;
  534. const int code = SSL_read(ssl_, data, checked_cast<int>(data_len));
  535. const int ssl_error = SSL_get_error(ssl_, code);
  536. switch (ssl_error) {
  537. case SSL_ERROR_NONE:
  538. RTC_LOG(LS_VERBOSE) << " -- success";
  539. RTC_DCHECK_GT(code, 0);
  540. RTC_DCHECK_LE(code, data_len);
  541. if (read) {
  542. *read = code;
  543. }
  544. if (ssl_mode_ == SSL_MODE_DTLS) {
  545. // Enforce atomic reads -- this is a short read
  546. unsigned int pending = SSL_pending(ssl_);
  547. if (pending) {
  548. RTC_LOG(LS_INFO) << " -- short DTLS read. flushing";
  549. FlushInput(pending);
  550. if (error) {
  551. *error = SSE_MSG_TRUNC;
  552. }
  553. return SR_ERROR;
  554. }
  555. }
  556. return SR_SUCCESS;
  557. case SSL_ERROR_WANT_READ:
  558. RTC_LOG(LS_VERBOSE) << " -- error want read";
  559. return SR_BLOCK;
  560. case SSL_ERROR_WANT_WRITE:
  561. RTC_LOG(LS_VERBOSE) << " -- error want write";
  562. ssl_read_needs_write_ = true;
  563. return SR_BLOCK;
  564. case SSL_ERROR_ZERO_RETURN:
  565. RTC_LOG(LS_VERBOSE) << " -- remote side closed";
  566. Close();
  567. return SR_EOS;
  568. break;
  569. default:
  570. Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false);
  571. if (error) {
  572. *error = ssl_error_code_;
  573. }
  574. return SR_ERROR;
  575. }
  576. // not reached
  577. }
  578. void OpenSSLStreamAdapter::FlushInput(unsigned int left) {
  579. unsigned char buf[2048];
  580. while (left) {
  581. // This should always succeed
  582. const int toread = (sizeof(buf) < left) ? sizeof(buf) : left;
  583. const int code = SSL_read(ssl_, buf, toread);
  584. const int ssl_error = SSL_get_error(ssl_, code);
  585. RTC_DCHECK(ssl_error == SSL_ERROR_NONE);
  586. if (ssl_error != SSL_ERROR_NONE) {
  587. RTC_DLOG(LS_VERBOSE) << " -- error " << code;
  588. Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false);
  589. return;
  590. }
  591. RTC_LOG(LS_VERBOSE) << " -- flushed " << code << " bytes";
  592. left -= code;
  593. }
  594. }
  595. void OpenSSLStreamAdapter::Close() {
  596. Cleanup(0);
  597. RTC_DCHECK(state_ == SSL_CLOSED || state_ == SSL_ERROR);
  598. // When we're closed at SSL layer, also close the stream level which
  599. // performs necessary clean up. Otherwise, a new incoming packet after
  600. // this could overflow the stream buffer.
  601. StreamAdapterInterface::Close();
  602. }
  603. StreamState OpenSSLStreamAdapter::GetState() const {
  604. switch (state_) {
  605. case SSL_WAIT:
  606. case SSL_CONNECTING:
  607. return SS_OPENING;
  608. case SSL_CONNECTED:
  609. if (WaitingToVerifyPeerCertificate()) {
  610. return SS_OPENING;
  611. }
  612. return SS_OPEN;
  613. default:
  614. return SS_CLOSED;
  615. }
  616. // not reached
  617. }
  618. void OpenSSLStreamAdapter::OnEvent(StreamInterface* stream,
  619. int events,
  620. int err) {
  621. int events_to_signal = 0;
  622. int signal_error = 0;
  623. RTC_DCHECK(stream == this->stream());
  624. if ((events & SE_OPEN)) {
  625. RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent SE_OPEN";
  626. if (state_ != SSL_WAIT) {
  627. RTC_DCHECK(state_ == SSL_NONE);
  628. events_to_signal |= SE_OPEN;
  629. } else {
  630. state_ = SSL_CONNECTING;
  631. if (int err = BeginSSL()) {
  632. Error("BeginSSL", err, 0, true);
  633. return;
  634. }
  635. }
  636. }
  637. if ((events & (SE_READ | SE_WRITE))) {
  638. RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent"
  639. << ((events & SE_READ) ? " SE_READ" : "")
  640. << ((events & SE_WRITE) ? " SE_WRITE" : "");
  641. if (state_ == SSL_NONE) {
  642. events_to_signal |= events & (SE_READ | SE_WRITE);
  643. } else if (state_ == SSL_CONNECTING) {
  644. if (int err = ContinueSSL()) {
  645. Error("ContinueSSL", err, 0, true);
  646. return;
  647. }
  648. } else if (state_ == SSL_CONNECTED) {
  649. if (((events & SE_READ) && ssl_write_needs_read_) ||
  650. (events & SE_WRITE)) {
  651. RTC_LOG(LS_VERBOSE) << " -- onStreamWriteable";
  652. events_to_signal |= SE_WRITE;
  653. }
  654. if (((events & SE_WRITE) && ssl_read_needs_write_) ||
  655. (events & SE_READ)) {
  656. RTC_LOG(LS_VERBOSE) << " -- onStreamReadable";
  657. events_to_signal |= SE_READ;
  658. }
  659. }
  660. }
  661. if ((events & SE_CLOSE)) {
  662. RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent(SE_CLOSE, " << err
  663. << ")";
  664. Cleanup(0);
  665. events_to_signal |= SE_CLOSE;
  666. // SE_CLOSE is the only event that uses the final parameter to OnEvent().
  667. RTC_DCHECK(signal_error == 0);
  668. signal_error = err;
  669. }
  670. if (events_to_signal) {
  671. StreamAdapterInterface::OnEvent(stream, events_to_signal, signal_error);
  672. }
  673. }
  674. int OpenSSLStreamAdapter::BeginSSL() {
  675. RTC_DCHECK(state_ == SSL_CONNECTING);
  676. // The underlying stream has opened.
  677. RTC_LOG(LS_INFO) << "BeginSSL with peer.";
  678. BIO* bio = nullptr;
  679. // First set up the context.
  680. RTC_DCHECK(ssl_ctx_ == nullptr);
  681. ssl_ctx_ = SetupSSLContext();
  682. if (!ssl_ctx_) {
  683. return -1;
  684. }
  685. bio = BIO_new_stream(static_cast<StreamInterface*>(stream()));
  686. if (!bio) {
  687. return -1;
  688. }
  689. ssl_ = SSL_new(ssl_ctx_);
  690. if (!ssl_) {
  691. BIO_free(bio);
  692. return -1;
  693. }
  694. SSL_set_app_data(ssl_, this);
  695. SSL_set_bio(ssl_, bio, bio); // the SSL object owns the bio now.
  696. if (ssl_mode_ == SSL_MODE_DTLS) {
  697. #ifdef OPENSSL_IS_BORINGSSL
  698. DTLSv1_set_initial_timeout_duration(ssl_, dtls_handshake_timeout_ms_);
  699. #else
  700. // Enable read-ahead for DTLS so whole packets are read from internal BIO
  701. // before parsing. This is done internally by BoringSSL for DTLS.
  702. SSL_set_read_ahead(ssl_, 1);
  703. #endif
  704. }
  705. SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE |
  706. SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
  707. // Do the connect
  708. return ContinueSSL();
  709. }
  710. int OpenSSLStreamAdapter::ContinueSSL() {
  711. RTC_LOG(LS_VERBOSE) << "ContinueSSL";
  712. RTC_DCHECK(state_ == SSL_CONNECTING);
  713. // Clear the DTLS timer
  714. Thread::Current()->Clear(this, MSG_TIMEOUT);
  715. const int code = (role_ == SSL_CLIENT) ? SSL_connect(ssl_) : SSL_accept(ssl_);
  716. const int ssl_error = SSL_get_error(ssl_, code);
  717. switch (ssl_error) {
  718. case SSL_ERROR_NONE:
  719. RTC_LOG(LS_VERBOSE) << " -- success";
  720. // By this point, OpenSSL should have given us a certificate, or errored
  721. // out if one was missing.
  722. RTC_DCHECK(peer_cert_chain_ || !GetClientAuthEnabled());
  723. state_ = SSL_CONNECTED;
  724. if (!WaitingToVerifyPeerCertificate()) {
  725. // We have everything we need to start the connection, so signal
  726. // SE_OPEN. If we need a client certificate fingerprint and don't have
  727. // it yet, we'll instead signal SE_OPEN in SetPeerCertificateDigest.
  728. //
  729. // TODO(deadbeef): Post this event asynchronously to unwind the stack.
  730. // The caller of ContinueSSL may be the same object listening for these
  731. // events and may not be prepared for reentrancy.
  732. // PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0);
  733. StreamAdapterInterface::OnEvent(stream(), SE_OPEN | SE_READ | SE_WRITE,
  734. 0);
  735. }
  736. break;
  737. case SSL_ERROR_WANT_READ: {
  738. RTC_LOG(LS_VERBOSE) << " -- error want read";
  739. struct timeval timeout;
  740. if (DTLSv1_get_timeout(ssl_, &timeout)) {
  741. int delay = timeout.tv_sec * 1000 + timeout.tv_usec / 1000;
  742. Thread::Current()->PostDelayed(RTC_FROM_HERE, delay, this, MSG_TIMEOUT,
  743. 0);
  744. }
  745. } break;
  746. case SSL_ERROR_WANT_WRITE:
  747. RTC_LOG(LS_VERBOSE) << " -- error want write";
  748. break;
  749. case SSL_ERROR_ZERO_RETURN:
  750. default:
  751. RTC_LOG(LS_VERBOSE) << " -- error " << code;
  752. SSLHandshakeError ssl_handshake_err = SSLHandshakeError::UNKNOWN;
  753. int err_code = ERR_peek_last_error();
  754. if (err_code != 0 && ERR_GET_REASON(err_code) == SSL_R_NO_SHARED_CIPHER) {
  755. ssl_handshake_err = SSLHandshakeError::INCOMPATIBLE_CIPHERSUITE;
  756. }
  757. SignalSSLHandshakeError(ssl_handshake_err);
  758. return (ssl_error != 0) ? ssl_error : -1;
  759. }
  760. return 0;
  761. }
  762. void OpenSSLStreamAdapter::Error(const char* context,
  763. int err,
  764. uint8_t alert,
  765. bool signal) {
  766. RTC_LOG(LS_WARNING) << "OpenSSLStreamAdapter::Error(" << context << ", "
  767. << err << ", " << static_cast<int>(alert) << ")";
  768. state_ = SSL_ERROR;
  769. ssl_error_code_ = err;
  770. Cleanup(alert);
  771. if (signal) {
  772. StreamAdapterInterface::OnEvent(stream(), SE_CLOSE, err);
  773. }
  774. }
  775. void OpenSSLStreamAdapter::Cleanup(uint8_t alert) {
  776. RTC_LOG(LS_INFO) << "Cleanup";
  777. if (state_ != SSL_ERROR) {
  778. state_ = SSL_CLOSED;
  779. ssl_error_code_ = 0;
  780. }
  781. if (ssl_) {
  782. int ret;
  783. // SSL_send_fatal_alert is only available in BoringSSL.
  784. #ifdef OPENSSL_IS_BORINGSSL
  785. if (alert) {
  786. ret = SSL_send_fatal_alert(ssl_, alert);
  787. if (ret < 0) {
  788. RTC_LOG(LS_WARNING) << "SSL_send_fatal_alert failed, error = "
  789. << SSL_get_error(ssl_, ret);
  790. }
  791. } else {
  792. #endif
  793. ret = SSL_shutdown(ssl_);
  794. if (ret < 0) {
  795. RTC_LOG(LS_WARNING)
  796. << "SSL_shutdown failed, error = " << SSL_get_error(ssl_, ret);
  797. }
  798. #ifdef OPENSSL_IS_BORINGSSL
  799. }
  800. #endif
  801. SSL_free(ssl_);
  802. ssl_ = nullptr;
  803. }
  804. if (ssl_ctx_) {
  805. SSL_CTX_free(ssl_ctx_);
  806. ssl_ctx_ = nullptr;
  807. }
  808. identity_.reset();
  809. peer_cert_chain_.reset();
  810. // Clear the DTLS timer
  811. Thread::Current()->Clear(this, MSG_TIMEOUT);
  812. }
  813. void OpenSSLStreamAdapter::OnMessage(Message* msg) {
  814. // Process our own messages and then pass others to the superclass
  815. if (MSG_TIMEOUT == msg->message_id) {
  816. RTC_LOG(LS_INFO) << "DTLS timeout expired";
  817. DTLSv1_handle_timeout(ssl_);
  818. ContinueSSL();
  819. } else {
  820. StreamInterface::OnMessage(msg);
  821. }
  822. }
  823. SSL_CTX* OpenSSLStreamAdapter::SetupSSLContext() {
  824. SSL_CTX* ctx =
  825. SSL_CTX_new(ssl_mode_ == SSL_MODE_DTLS ? DTLS_method() : TLS_method());
  826. if (ctx == nullptr) {
  827. return nullptr;
  828. }
  829. // TODO(https://bugs.webrtc.org/10261): Evaluate and drop (D)TLS 1.0 and 1.1
  830. // support by default.
  831. SSL_CTX_set_min_proto_version(
  832. ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_VERSION : TLS1_VERSION);
  833. switch (ssl_max_version_) {
  834. case SSL_PROTOCOL_TLS_10:
  835. SSL_CTX_set_max_proto_version(
  836. ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_VERSION : TLS1_VERSION);
  837. break;
  838. case SSL_PROTOCOL_TLS_11:
  839. SSL_CTX_set_max_proto_version(
  840. ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_VERSION : TLS1_1_VERSION);
  841. break;
  842. case SSL_PROTOCOL_TLS_12:
  843. default:
  844. SSL_CTX_set_max_proto_version(
  845. ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_2_VERSION : TLS1_2_VERSION);
  846. break;
  847. }
  848. #ifdef OPENSSL_IS_BORINGSSL
  849. // SSL_CTX_set_current_time_cb is only supported in BoringSSL.
  850. if (g_use_time_callback_for_testing) {
  851. SSL_CTX_set_current_time_cb(ctx, &TimeCallbackForTesting);
  852. }
  853. #endif
  854. if (identity_ && !identity_->ConfigureIdentity(ctx)) {
  855. SSL_CTX_free(ctx);
  856. return nullptr;
  857. }
  858. #if !defined(NDEBUG)
  859. SSL_CTX_set_info_callback(ctx, OpenSSLAdapter::SSLInfoCallback);
  860. #endif
  861. int mode = SSL_VERIFY_PEER;
  862. if (GetClientAuthEnabled()) {
  863. // Require a certificate from the client.
  864. // Note: Normally this is always true in production, but it may be disabled
  865. // for testing purposes (e.g. SSLAdapter unit tests).
  866. mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
  867. }
  868. // Configure a custom certificate verification callback to check the peer
  869. // certificate digest. Note the second argument to SSL_CTX_set_verify is to
  870. // override individual errors in the default verification logic, which is not
  871. // what we want here.
  872. SSL_CTX_set_verify(ctx, mode, nullptr);
  873. SSL_CTX_set_cert_verify_callback(ctx, SSLVerifyCallback, nullptr);
  874. // Select list of available ciphers. Note that !SHA256 and !SHA384 only
  875. // remove HMAC-SHA256 and HMAC-SHA384 cipher suites, not GCM cipher suites
  876. // with SHA256 or SHA384 as the handshake hash.
  877. // This matches the list of SSLClientSocketOpenSSL in Chromium.
  878. SSL_CTX_set_cipher_list(
  879. ctx, "DEFAULT:!NULL:!aNULL:!SHA256:!SHA384:!aECDH:!AESGCM+AES256:!aPSK");
  880. if (!srtp_ciphers_.empty()) {
  881. if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_ciphers_.c_str())) {
  882. SSL_CTX_free(ctx);
  883. return nullptr;
  884. }
  885. }
  886. return ctx;
  887. }
  888. bool OpenSSLStreamAdapter::VerifyPeerCertificate() {
  889. if (!HasPeerCertificateDigest() || !peer_cert_chain_ ||
  890. !peer_cert_chain_->GetSize()) {
  891. RTC_LOG(LS_WARNING) << "Missing digest or peer certificate.";
  892. return false;
  893. }
  894. const OpenSSLCertificate* leaf_cert =
  895. static_cast<const OpenSSLCertificate*>(&peer_cert_chain_->Get(0));
  896. unsigned char digest[EVP_MAX_MD_SIZE];
  897. size_t digest_length;
  898. if (!OpenSSLCertificate::ComputeDigest(
  899. leaf_cert->x509(), peer_certificate_digest_algorithm_, digest,
  900. sizeof(digest), &digest_length)) {
  901. RTC_LOG(LS_WARNING) << "Failed to compute peer cert digest.";
  902. return false;
  903. }
  904. Buffer computed_digest(digest, digest_length);
  905. if (computed_digest != peer_certificate_digest_value_) {
  906. RTC_LOG(LS_WARNING)
  907. << "Rejected peer certificate due to mismatched digest.";
  908. return false;
  909. }
  910. // Ignore any verification error if the digest matches, since there is no
  911. // value in checking the validity of a self-signed cert issued by untrusted
  912. // sources.
  913. RTC_LOG(LS_INFO) << "Accepted peer certificate.";
  914. peer_certificate_verified_ = true;
  915. return true;
  916. }
  917. std::unique_ptr<SSLCertChain> OpenSSLStreamAdapter::GetPeerSSLCertChain()
  918. const {
  919. return peer_cert_chain_ ? peer_cert_chain_->Clone() : nullptr;
  920. }
  921. int OpenSSLStreamAdapter::SSLVerifyCallback(X509_STORE_CTX* store, void* arg) {
  922. // Get our SSL structure and OpenSSLStreamAdapter from the store.
  923. SSL* ssl = reinterpret_cast<SSL*>(
  924. X509_STORE_CTX_get_ex_data(store, SSL_get_ex_data_X509_STORE_CTX_idx()));
  925. OpenSSLStreamAdapter* stream =
  926. reinterpret_cast<OpenSSLStreamAdapter*>(SSL_get_app_data(ssl));
  927. #if defined(OPENSSL_IS_BORINGSSL)
  928. STACK_OF(X509)* chain = SSL_get_peer_full_cert_chain(ssl);
  929. // Creates certificate chain.
  930. std::vector<std::unique_ptr<SSLCertificate>> cert_chain;
  931. for (X509* cert : chain) {
  932. cert_chain.emplace_back(new OpenSSLCertificate(cert));
  933. }
  934. stream->peer_cert_chain_.reset(new SSLCertChain(std::move(cert_chain)));
  935. #else
  936. // Record the peer's certificate.
  937. X509* cert = X509_STORE_CTX_get0_cert(store);
  938. stream->peer_cert_chain_.reset(
  939. new SSLCertChain(std::make_unique<OpenSSLCertificate>(cert)));
  940. #endif
  941. // If the peer certificate digest isn't known yet, we'll wait to verify
  942. // until it's known, and for now just return a success status.
  943. if (stream->peer_certificate_digest_algorithm_.empty()) {
  944. RTC_LOG(LS_INFO) << "Waiting to verify certificate until digest is known.";
  945. return 1;
  946. }
  947. if (!stream->VerifyPeerCertificate()) {
  948. X509_STORE_CTX_set_error(store, X509_V_ERR_CERT_REJECTED);
  949. return 0;
  950. }
  951. return 1;
  952. }
  953. bool OpenSSLStreamAdapter::IsBoringSsl() {
  954. #ifdef OPENSSL_IS_BORINGSSL
  955. return true;
  956. #else
  957. return false;
  958. #endif
  959. }
  960. #define CDEF(X) \
  961. { static_cast<uint16_t>(TLS1_CK_##X & 0xffff), "TLS_" #X }
  962. struct cipher_list {
  963. uint16_t cipher;
  964. const char* cipher_str;
  965. };
  966. // TODO(torbjorng): Perhaps add more cipher suites to these lists.
  967. static const cipher_list OK_RSA_ciphers[] = {
  968. CDEF(ECDHE_RSA_WITH_AES_128_CBC_SHA),
  969. CDEF(ECDHE_RSA_WITH_AES_256_CBC_SHA),
  970. CDEF(ECDHE_RSA_WITH_AES_128_GCM_SHA256),
  971. #ifdef TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA256
  972. CDEF(ECDHE_RSA_WITH_AES_256_GCM_SHA256),
  973. #endif
  974. #ifdef TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
  975. CDEF(ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256),
  976. #endif
  977. };
  978. static const cipher_list OK_ECDSA_ciphers[] = {
  979. CDEF(ECDHE_ECDSA_WITH_AES_128_CBC_SHA),
  980. CDEF(ECDHE_ECDSA_WITH_AES_256_CBC_SHA),
  981. CDEF(ECDHE_ECDSA_WITH_AES_128_GCM_SHA256),
  982. #ifdef TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA256
  983. CDEF(ECDHE_ECDSA_WITH_AES_256_GCM_SHA256),
  984. #endif
  985. #ifdef TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
  986. CDEF(ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256),
  987. #endif
  988. };
  989. #undef CDEF
  990. bool OpenSSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) {
  991. if (key_type == KT_RSA) {
  992. for (const cipher_list& c : OK_RSA_ciphers) {
  993. if (cipher == c.cipher) {
  994. return true;
  995. }
  996. }
  997. }
  998. if (key_type == KT_ECDSA) {
  999. for (const cipher_list& c : OK_ECDSA_ciphers) {
  1000. if (cipher == c.cipher) {
  1001. return true;
  1002. }
  1003. }
  1004. }
  1005. return false;
  1006. }
  1007. bool OpenSSLStreamAdapter::IsAcceptableCipher(const std::string& cipher,
  1008. KeyType key_type) {
  1009. if (key_type == KT_RSA) {
  1010. for (const cipher_list& c : OK_RSA_ciphers) {
  1011. if (cipher == c.cipher_str) {
  1012. return true;
  1013. }
  1014. }
  1015. }
  1016. if (key_type == KT_ECDSA) {
  1017. for (const cipher_list& c : OK_ECDSA_ciphers) {
  1018. if (cipher == c.cipher_str) {
  1019. return true;
  1020. }
  1021. }
  1022. }
  1023. return false;
  1024. }
  1025. void OpenSSLStreamAdapter::EnableTimeCallbackForTesting() {
  1026. #ifdef OPENSSL_IS_BORINGSSL
  1027. g_use_time_callback_for_testing = true;
  1028. #endif
  1029. }
  1030. } // namespace rtc