webrtc_m130/rtc_base/openssl_stream_adapter.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

259 lines
9.3 KiB
C
Raw Normal View History

/*
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef RTC_BASE_OPENSSL_STREAM_ADAPTER_H_
#define RTC_BASE_OPENSSL_STREAM_ADAPTER_H_
#include <openssl/ossl_typ.h>
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <string>
#include <vector>
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "rtc_base/buffer.h"
#ifdef OPENSSL_IS_BORINGSSL
#include "rtc_base/boringssl_identity.h"
#else
#include "rtc_base/openssl_identity.h"
#endif
#include "rtc_base/ssl_identity.h"
#include "rtc_base/ssl_stream_adapter.h"
#include "rtc_base/stream.h"
#include "rtc_base/system/rtc_export.h"
Revert "[Sheriff] Revert "Remove MessageHandler[AutoCleanup] dependency from StreamInterface."" This reverts commit af05c833dac86d1e8496b0cf17eac4514b9b2b2e. Reason for revert: The failure in remoting_unittests has been addressed. Original change's description: > [Sheriff] Revert "Remove MessageHandler[AutoCleanup] dependency from StreamInterface." > > This reverts commit eb79dd9ffdc41e4ca86803bfc1317e0961a8a8a6. > > Reason for revert: breaks WebRTC roll into Chrome: > https://crrev.com/c/2445696 > > Sample failure: > https://ci.chromium.org/p/chromium/builders/try/linux-rel/506049 > [ RUN ] PseudoTcpAdapterTest.DeleteOnConnected > > Original change's description: > > Remove MessageHandler[AutoCleanup] dependency from StreamInterface. > > > > This includes relying on related types such as MessageData and > > PostEvent functionality inside the StreamInterface itself. > > > > This affects mostly tests but OpenSSLStreamAdapter > > requires special attention. > > > > Bug: webrtc:11988 > > Change-Id: Ib5c895f1bdf77bb49e3162bd49718f8a98812d91 > > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/185505 > > Commit-Queue: Tommi <tommi@webrtc.org> > > Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> > > Cr-Commit-Position: refs/heads/master@{#32290} > > TBR=kwiberg@webrtc.org,tommi@webrtc.org > > Change-Id: I23d7a311a73c739eba872a21e6123235465c28cc > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: webrtc:11988 > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/186564 > Commit-Queue: Marina Ciocea <marinaciocea@webrtc.org> > Reviewed-by: Marina Ciocea <marinaciocea@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#32299} TBR=kwiberg@webrtc.org,tommi@webrtc.org,marinaciocea@webrtc.org # Not skipping CQ checks because original CL landed > 1 day ago. Bug: webrtc:11988 Change-Id: Iff07e0943fc5dded9eeed5c2626798691594300d Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/186700 Reviewed-by: Tommi <tommi@webrtc.org> Commit-Queue: Tommi <tommi@webrtc.org> Cr-Commit-Position: refs/heads/master@{#32314}
2020-10-05 12:43:53 +00:00
#include "rtc_base/task_utils/pending_task_safety_flag.h"
#include "rtc_base/task_utils/repeating_task.h"
namespace rtc {
// This class was written with OpenSSLAdapter (a socket adapter) as a
// starting point. It has similar structure and functionality, but uses a
// "peer-to-peer" mode, verifying the peer's certificate using a digest
// sent over a secure signaling channel.
//
// Static methods to initialize and deinit the SSL library are in
// OpenSSLAdapter. These should probably be moved out to a neutral class.
//
// In a few cases I have factored out some OpenSSLAdapter code into static
// methods so it can be reused from this class. Eventually that code should
// probably be moved to a common support class. Unfortunately there remain a
// few duplicated sections of code. I have not done more restructuring because
// I did not want to affect existing code that uses OpenSSLAdapter.
//
// This class does not support the SSL connection restart feature present in
// OpenSSLAdapter. I am not entirely sure how the feature is useful and I am
// not convinced that it works properly.
//
// This implementation is careful to disallow data exchange after an SSL error,
// and it has an explicit SSL_CLOSED state. It should not be possible to send
// any data in clear after one of the StartSSL methods has been called.
// Look in sslstreamadapter.h for documentation of the methods.
class SSLCertChain;
///////////////////////////////////////////////////////////////////////////////
// If `allow` has a value, its value determines if legacy TLS protocols are
// allowed, overriding the default configuration.
// If `allow` has no value, any previous override is removed and the default
// configuration is restored.
RTC_EXPORT void SetAllowLegacyTLSProtocols(const absl::optional<bool>& allow);
class OpenSSLStreamAdapter final : public SSLStreamAdapter {
public:
explicit OpenSSLStreamAdapter(std::unique_ptr<StreamInterface> stream);
~OpenSSLStreamAdapter() override;
void SetIdentity(std::unique_ptr<SSLIdentity> identity) override;
SSLIdentity* GetIdentityForTesting() const override;
// Default argument is for compatibility
void SetServerRole(SSLRole role = SSL_SERVER) override;
bool SetPeerCertificateDigest(
absl::string_view digest_alg,
const unsigned char* digest_val,
size_t digest_len,
SSLPeerCertificateDigestError* error = nullptr) override;
std::unique_ptr<SSLCertChain> GetPeerSSLCertChain() const override;
// Goes from state SSL_NONE to either SSL_CONNECTING or SSL_WAIT, depending
// on whether the underlying stream is already open or not.
int StartSSL() override;
void SetMode(SSLMode mode) override;
void SetMaxProtocolVersion(SSLProtocolVersion version) override;
void SetInitialRetransmissionTimeout(int timeout_ms) override;
StreamResult Read(void* data,
size_t data_len,
size_t* read,
int* error) override;
StreamResult Write(const void* data,
size_t data_len,
size_t* written,
int* error) override;
void Close() override;
StreamState GetState() const override;
// TODO(guoweis): Move this away from a static class method.
static std::string SslCipherSuiteToName(int crypto_suite);
bool GetSslCipherSuite(int* cipher) override;
SSLProtocolVersion GetSslVersion() const override;
bool GetSslVersionBytes(int* version) const override;
// Key Extractor interface
bool ExportKeyingMaterial(absl::string_view label,
const uint8_t* context,
size_t context_len,
bool use_context,
uint8_t* result,
size_t result_len) override;
// DTLS-SRTP interface
bool SetDtlsSrtpCryptoSuites(const std::vector<int>& crypto_suites) override;
bool GetDtlsSrtpCryptoSuite(int* crypto_suite) override;
bool IsTlsConnected() override;
// Capabilities interfaces.
static bool IsBoringSsl();
static bool IsAcceptableCipher(int cipher, KeyType key_type);
static bool IsAcceptableCipher(absl::string_view cipher, KeyType key_type);
// Use our timeutils.h source of timing in BoringSSL, allowing us to test
// using a fake clock.
static void EnableTimeCallbackForTesting();
private:
enum SSLState {
// Before calling one of the StartSSL methods, data flows
// in clear text.
SSL_NONE,
SSL_WAIT, // waiting for the stream to open to start SSL negotiation
SSL_CONNECTING, // SSL negotiation in progress
SSL_CONNECTED, // SSL stream successfully established
SSL_ERROR, // some SSL error occurred, stream is closed
SSL_CLOSED // Clean close
};
void OnEvent(StreamInterface* stream, int events, int err);
Revert "[Sheriff] Revert "Remove MessageHandler[AutoCleanup] dependency from StreamInterface."" This reverts commit af05c833dac86d1e8496b0cf17eac4514b9b2b2e. Reason for revert: The failure in remoting_unittests has been addressed. Original change's description: > [Sheriff] Revert "Remove MessageHandler[AutoCleanup] dependency from StreamInterface." > > This reverts commit eb79dd9ffdc41e4ca86803bfc1317e0961a8a8a6. > > Reason for revert: breaks WebRTC roll into Chrome: > https://crrev.com/c/2445696 > > Sample failure: > https://ci.chromium.org/p/chromium/builders/try/linux-rel/506049 > [ RUN ] PseudoTcpAdapterTest.DeleteOnConnected > > Original change's description: > > Remove MessageHandler[AutoCleanup] dependency from StreamInterface. > > > > This includes relying on related types such as MessageData and > > PostEvent functionality inside the StreamInterface itself. > > > > This affects mostly tests but OpenSSLStreamAdapter > > requires special attention. > > > > Bug: webrtc:11988 > > Change-Id: Ib5c895f1bdf77bb49e3162bd49718f8a98812d91 > > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/185505 > > Commit-Queue: Tommi <tommi@webrtc.org> > > Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> > > Cr-Commit-Position: refs/heads/master@{#32290} > > TBR=kwiberg@webrtc.org,tommi@webrtc.org > > Change-Id: I23d7a311a73c739eba872a21e6123235465c28cc > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: webrtc:11988 > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/186564 > Commit-Queue: Marina Ciocea <marinaciocea@webrtc.org> > Reviewed-by: Marina Ciocea <marinaciocea@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#32299} TBR=kwiberg@webrtc.org,tommi@webrtc.org,marinaciocea@webrtc.org # Not skipping CQ checks because original CL landed > 1 day ago. Bug: webrtc:11988 Change-Id: Iff07e0943fc5dded9eeed5c2626798691594300d Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/186700 Reviewed-by: Tommi <tommi@webrtc.org> Commit-Queue: Tommi <tommi@webrtc.org> Cr-Commit-Position: refs/heads/master@{#32314}
2020-10-05 12:43:53 +00:00
void PostEvent(int events, int err);
void SetTimeout(int delay_ms);
// The following three methods return 0 on success and a negative
// error code on failure. The error code may be from OpenSSL or -1
// on some other error cases, so it can't really be interpreted
// unfortunately.
// Prepare SSL library, state is SSL_CONNECTING.
int BeginSSL();
// Perform SSL negotiation steps.
int ContinueSSL();
// Error handler helper. signal is given as true for errors in
// asynchronous contexts (when an error method was not returned
// through some other method), and in that case an SE_CLOSE event is
// raised on the stream with the specified error.
// A 0 error means a graceful close, otherwise there is not really enough
// context to interpret the error code.
// `alert` indicates an alert description (one of the SSL_AD constants) to
// send to the remote endpoint when closing the association. If 0, a normal
// shutdown will be performed.
void Error(const char* context, int err, uint8_t alert, bool signal);
void Cleanup(uint8_t alert);
// Flush the input buffers by reading left bytes (for DTLS)
void FlushInput(unsigned int left);
// SSL library configuration
SSL_CTX* SetupSSLContext();
// Verify the peer certificate matches the signaled digest.
bool VerifyPeerCertificate();
#ifdef OPENSSL_IS_BORINGSSL
// SSL certificate verification callback. See SSL_CTX_set_custom_verify.
static enum ssl_verify_result_t SSLVerifyCallback(SSL* ssl,
uint8_t* out_alert);
#else
Only verify the certificate once. WebRTC is currently using the SSL_CTX_set_verify callback. This configures a callback for use with X509_STORE_CTX_set_verify_cb. See https://www.openssl.org/docs/man1.0.2/crypto/X509_STORE_CTX_set_verify_cb.html This callback does not override certificate verification. Rather, it allows EACH failure in OpenSSL's built-in certificate verification, as well as the final success, to be overridden (that's why there's an ok parameter). It still runs the usual OpenSSL certificate verification (which will never succeed). The upshot is that the callback is called multiple times and OpenSSLStreamAdapter does a ton of redundant work and checks the hash at least twice, or more for certificates with other errors. Instead, use SSL_CTX_set_cert_verify_callback. This short-circuits the OpenSSL behavior entirely and uses a caller-supplied one. https://commondatastorage.googleapis.com/chromium-boringssl-docs/ssl.h.html#SSL_CTX_set_cert_verify_callback https://wiki.openssl.org/index.php/Manual:SSL_CTX_set_cert_verify_callback(3) (This also removes the SSL_CTX_set_verify_depth call which is ignored with SSL_CTX_set_cert_verify_callback. It didn't do anything before either---it tells OpenSSL to reject chains that are too short, but the rejection was overwritten by the callback anyway.) (Later on, we'll need to switch this to the BoringSSL-only SSL_CTX_set_custom_verify and CRYPTO_BUFFER APIs to fix WebRTC's contribution to Chrome's binary size, but I've left that alone for the time being.) Bug: none Change-Id: I9320a367d0961935836df63dc6f0868b069f0af0 Reviewed-on: https://webrtc-review.googlesource.com/4581 Commit-Queue: David Benjamin <davidben@webrtc.org> Reviewed-by: Taylor Brandstetter <deadbeef@webrtc.org> Cr-Commit-Position: refs/heads/master@{#20053}
2017-09-29 12:14:08 -04:00
// SSL certificate verification callback. See
// SSL_CTX_set_cert_verify_callback.
static int SSLVerifyCallback(X509_STORE_CTX* store, void* arg);
#endif
bool WaitingToVerifyPeerCertificate() const {
return GetClientAuthEnabled() && !peer_certificate_verified_;
}
bool HasPeerCertificateDigest() const {
return !peer_certificate_digest_algorithm_.empty() &&
!peer_certificate_digest_value_.empty();
}
const std::unique_ptr<StreamInterface> stream_;
Revert "[Sheriff] Revert "Remove MessageHandler[AutoCleanup] dependency from StreamInterface."" This reverts commit af05c833dac86d1e8496b0cf17eac4514b9b2b2e. Reason for revert: The failure in remoting_unittests has been addressed. Original change's description: > [Sheriff] Revert "Remove MessageHandler[AutoCleanup] dependency from StreamInterface." > > This reverts commit eb79dd9ffdc41e4ca86803bfc1317e0961a8a8a6. > > Reason for revert: breaks WebRTC roll into Chrome: > https://crrev.com/c/2445696 > > Sample failure: > https://ci.chromium.org/p/chromium/builders/try/linux-rel/506049 > [ RUN ] PseudoTcpAdapterTest.DeleteOnConnected > > Original change's description: > > Remove MessageHandler[AutoCleanup] dependency from StreamInterface. > > > > This includes relying on related types such as MessageData and > > PostEvent functionality inside the StreamInterface itself. > > > > This affects mostly tests but OpenSSLStreamAdapter > > requires special attention. > > > > Bug: webrtc:11988 > > Change-Id: Ib5c895f1bdf77bb49e3162bd49718f8a98812d91 > > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/185505 > > Commit-Queue: Tommi <tommi@webrtc.org> > > Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> > > Cr-Commit-Position: refs/heads/master@{#32290} > > TBR=kwiberg@webrtc.org,tommi@webrtc.org > > Change-Id: I23d7a311a73c739eba872a21e6123235465c28cc > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: webrtc:11988 > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/186564 > Commit-Queue: Marina Ciocea <marinaciocea@webrtc.org> > Reviewed-by: Marina Ciocea <marinaciocea@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#32299} TBR=kwiberg@webrtc.org,tommi@webrtc.org,marinaciocea@webrtc.org # Not skipping CQ checks because original CL landed > 1 day ago. Bug: webrtc:11988 Change-Id: Iff07e0943fc5dded9eeed5c2626798691594300d Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/186700 Reviewed-by: Tommi <tommi@webrtc.org> Commit-Queue: Tommi <tommi@webrtc.org> Cr-Commit-Position: refs/heads/master@{#32314}
2020-10-05 12:43:53 +00:00
rtc::Thread* const owner_;
webrtc::ScopedTaskSafety task_safety_;
webrtc::RepeatingTaskHandle timeout_task_;
SSLState state_;
SSLRole role_;
int ssl_error_code_; // valid when state_ == SSL_ERROR or SSL_CLOSED
// Whether the SSL negotiation is blocked on needing to read or
// write to the wrapped stream.
bool ssl_read_needs_write_;
bool ssl_write_needs_read_;
SSL* ssl_;
SSL_CTX* ssl_ctx_;
// Our key and certificate.
#ifdef OPENSSL_IS_BORINGSSL
std::unique_ptr<BoringSSLIdentity> identity_;
#else
std::unique_ptr<OpenSSLIdentity> identity_;
#endif
// The certificate chain that the peer presented. Initially null, until the
// connection is established.
std::unique_ptr<SSLCertChain> peer_cert_chain_;
bool peer_certificate_verified_ = false;
// The digest of the certificate that the peer must present.
Buffer peer_certificate_digest_value_;
std::string peer_certificate_digest_algorithm_;
// The DtlsSrtp ciphers
std::string srtp_ciphers_;
// Do DTLS or not
SSLMode ssl_mode_;
// Max. allowed protocol version
SSLProtocolVersion ssl_max_version_;
// A 50-ms initial timeout ensures rapid setup on fast connections, but may
// be too aggressive for low bandwidth links.
int dtls_handshake_timeout_ms_ = 50;
Make a switch to disable DTLS 1.0, TLS 1.0 and TLS 1.1 downgrade in WebRTC. This reverts commit af1f8655b2cb69af382396ea642eb0a2bf04bb4d Landing the change with default set to "enabled" (DTLS 1.0 will continue to work by default), so that flipping the default can be a separate CL. Original change's description: > Revert "Disable DTLS 1.0, TLS 1.0 and TLS 1.1 downgrade in WebRTC." > > This reverts commit 7276b974b78ea4f409d8738b1b6f1515f7a8968e. > > Reason for revert: Changing to a later Chrome release. > > Original change's description: > > Disable DTLS 1.0, TLS 1.0 and TLS 1.1 downgrade in WebRTC. > > > > This change disables DTLS 1.0, TLS 1.0 and TLS 1.1 in WebRTC by default. This > > is part of a larger effort at Google to remove old TLS protocols: > > https://security.googleblog.com/2018/10/modernizing-transport-security.html > > > > For the M74 timeline I have added a disabled by default field trial > > WebRTC-LegacyTlsProtocols which can be enabled to support these cipher suites > > as consumers move away from these legacy cipher protocols but it will be off > > in Chrome. > > > > This is compliant with the webrtc-security-arch specification which states: > > > > All Implementations MUST implement DTLS 1.2 with the > > TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 cipher suite and the P-256 > > curve [FIPS186]. Earlier drafts of this specification required DTLS > > 1.0 with the cipher suite TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, and > > at the time of this writing some implementations do not support DTLS > > 1.2; endpoints which support only DTLS 1.2 might encounter > > interoperability issues. The DTLS-SRTP protection profile > > SRTP_AES128_CM_HMAC_SHA1_80 MUST be supported for SRTP. > > Implementations MUST favor cipher suites which support (Perfect > > Forward Secrecy) PFS over non-PFS cipher suites and SHOULD favor AEAD > > over non-AEAD cipher suites. > > > > Bug: webrtc:10261 > > Change-Id: I847c567592911cc437f095376ad67585b4355fc0 > > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/125141 > > Commit-Queue: Benjamin Wright <benwright@webrtc.org> > > Reviewed-by: David Benjamin <davidben@webrtc.org> > > Reviewed-by: Qingsi Wang <qingsi@webrtc.org> > > Cr-Commit-Position: refs/heads/master@{#27006} > > TBR=steveanton@webrtc.org,davidben@webrtc.org,qingsi@webrtc.org,benwright@webrtc.org > > # Not skipping CQ checks because original CL landed > 1 day ago. > > Bug: webrtc:10261 > Change-Id: I34727e65c069e1fb2ad71838828ad0a22b5fe811 > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/130367 > Commit-Queue: Benjamin Wright <benwright@webrtc.org> > Reviewed-by: Benjamin Wright <benwright@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#27403} Bug: webrtc:10261 Change-Id: I28c6819d37665976e396df280b4abf48fb91d533 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/169851 Commit-Queue: Harald Alvestrand <hta@webrtc.org> Reviewed-by: Benjamin Wright <benwright@webrtc.org> Reviewed-by: Qingsi Wang <qingsi@webrtc.org> Reviewed-by: Harald Alvestrand <hta@webrtc.org> Cr-Commit-Position: refs/heads/master@{#30733}
2020-03-09 19:39:36 +01:00
// TODO(https://bugs.webrtc.org/10261): Completely remove this option in M84.
const bool support_legacy_tls_protocols_flag_;
};
/////////////////////////////////////////////////////////////////////////////
} // namespace rtc
#endif // RTC_BASE_OPENSSL_STREAM_ADAPTER_H_