#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "net/socket/ssl_client_socket.h"
#include <errno.h>
#include <string.h>
#include <algorithm>
#include <memory>
#include <optional>
#include <string_view>
#include <tuple>
#include <utility>
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/lock.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "base/values.h"
#include "build/build_config.h"
#include "crypto/rsa_private_key.h"
#include "net/base/address_list.h"
#include "net/base/completion_once_callback.h"
#include "net/base/features.h"
#include "net/base/host_port_pair.h"
#include "net/base/io_buffer.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/base/network_anonymization_key.h"
#include "net/base/schemeful_site.h"
#include "net/base/test_completion_callback.h"
#include "net/cert/asn1_util.h"
#include "net/cert/cert_database.h"
#include "net/cert/ct_policy_status.h"
#include "net/cert/mock_cert_verifier.h"
#include "net/cert/mock_client_cert_verifier.h"
#include "net/cert/sct_auditing_delegate.h"
#include "net/cert/signed_certificate_timestamp_and_status.h"
#include "net/cert/test_root_certs.h"
#include "net/cert/x509_util.h"
#include "net/dns/host_resolver.h"
#include "net/http/transport_security_state.h"
#include "net/http/transport_security_state_test_util.h"
#include "net/log/net_log_event_type.h"
#include "net/log/net_log_source.h"
#include "net/log/test_net_log.h"
#include "net/log/test_net_log_util.h"
#include "net/socket/client_socket_factory.h"
#include "net/socket/client_socket_handle.h"
#include "net/socket/read_buffering_stream_socket.h"
#include "net/socket/socket_test_util.h"
#include "net/socket/ssl_server_socket.h"
#include "net/socket/stream_socket.h"
#include "net/socket/tcp_client_socket.h"
#include "net/socket/tcp_server_socket.h"
#include "net/ssl/ssl_cert_request_info.h"
#include "net/ssl/ssl_client_session_cache.h"
#include "net/ssl/ssl_config.h"
#include "net/ssl/ssl_config_service.h"
#include "net/ssl/ssl_connection_status_flags.h"
#include "net/ssl/ssl_handshake_details.h"
#include "net/ssl/ssl_info.h"
#include "net/ssl/ssl_server_config.h"
#include "net/ssl/test_ssl_config_service.h"
#include "net/ssl/test_ssl_private_key.h"
#include "net/test/cert_test_util.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "net/test/gtest_util.h"
#include "net/test/key_util.h"
#include "net/test/ssl_test_util.h"
#include "net/test/test_data_directory.h"
#include "net/test/test_with_task_environment.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#include "third_party/boringssl/src/include/openssl/bio.h"
#include "third_party/boringssl/src/include/openssl/evp.h"
#include "third_party/boringssl/src/include/openssl/hpke.h"
#include "third_party/boringssl/src/include/openssl/pem.h"
#include "third_party/boringssl/src/include/openssl/ssl.h"
#include "url/gurl.h"
IsError;
IsOk;
_;
Bool;
Combine;
Return;
Values;
ValuesIn;
namespace net {
class NetLogWithSource;
namespace {
const uint8_t kGoodHashValueVectorInput = …;
const uint8_t kBadHashValueVectorInput = …;
constexpr uint16_t kModernTLS12Cipher = …;
constexpr uint16_t kRSACipher = …;
constexpr uint16_t kCBCCipher = …;
constexpr uint16_t k3DESCipher = …;
class SynchronousErrorStreamSocket : public WrappedStreamSocket { … };
int SynchronousErrorStreamSocket::Read(IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback) { … }
int SynchronousErrorStreamSocket::ReadIfReady(IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback) { … }
int SynchronousErrorStreamSocket::Write(
IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback,
const NetworkTrafficAnnotationTag& traffic_annotation) { … }
class FakeBlockingStreamSocket : public WrappedStreamSocket { … };
int FakeBlockingStreamSocket::Read(IOBuffer* buf,
int len,
CompletionOnceCallback callback) { … }
int FakeBlockingStreamSocket::ReadIfReady(IOBuffer* buf,
int len,
CompletionOnceCallback callback) { … }
int FakeBlockingStreamSocket::CancelReadIfReady() { … }
int FakeBlockingStreamSocket::Write(
IOBuffer* buf,
int len,
CompletionOnceCallback callback,
const NetworkTrafficAnnotationTag& traffic_annotation) { … }
void FakeBlockingStreamSocket::BlockReadResult() { … }
void FakeBlockingStreamSocket::UnblockReadResult() { … }
bool FakeBlockingStreamSocket::ReplaceReadResult(const std::string& data) { … }
void FakeBlockingStreamSocket::WaitForReadResult() { … }
void FakeBlockingStreamSocket::BlockWrite() { … }
void FakeBlockingStreamSocket::CallPendingWriteCallback(int rv) { … }
void FakeBlockingStreamSocket::UnblockWrite() { … }
void FakeBlockingStreamSocket::WaitForWrite() { … }
void FakeBlockingStreamSocket::OnReadCompleted(int result) { … }
void FakeBlockingStreamSocket::CompleteReadIfReady(scoped_refptr<IOBuffer> buf,
int rv) { … }
void FakeBlockingStreamSocket::ReturnReadResult() { … }
class CountingStreamSocket : public WrappedStreamSocket { … };
class DeleteSocketCallback : public TestCompletionCallbackBase { … };
class MockRequireCTDelegate : public TransportSecurityState::RequireCTDelegate { … };
class MockSCTAuditingDelegate : public SCTAuditingDelegate { … };
class ManySmallRecordsHttpResponse : public test_server::HttpResponse { … };
class SSLClientSocketTest : public PlatformTest, public WithTaskEnvironment { … };
enum ReadIfReadyTransport { … };
enum ReadIfReadySSL { … };
class StreamSocketWithoutReadIfReady : public WrappedStreamSocket { … };
class ClientSocketFactoryWithoutReadIfReady : public ClientSocketFactory { … };
std::vector<uint16_t> GetTLSVersions() { … }
class SSLClientSocketVersionTest
: public SSLClientSocketTest,
public ::testing::WithParamInterface<uint16_t> { … };
class SSLClientSocketReadTest
: public SSLClientSocketTest,
public ::testing::WithParamInterface<
std::tuple<ReadIfReadyTransport, ReadIfReadySSL, uint16_t>> { … };
INSTANTIATE_TEST_SUITE_P(…);
class SSLClientSocketCertRequestInfoTest : public SSLClientSocketVersionTest { … };
class SSLClientSocketFalseStartTest : public SSLClientSocketTest { … };
int MakeHTTPRequest(StreamSocket* socket, const char* path = "/") { … }
class ZeroRTTResponse : public test_server::HttpResponse { … };
std::unique_ptr<test_server::HttpResponse> HandleZeroRTTRequest(
const test_server::HttpRequest& request) { … }
class SSLClientSocketZeroRTTTest : public SSLClientSocketTest { … };
std::string FormatTLS12Alert(uint8_t alert) { … }
class HangingCertVerifier : public CertVerifier { … };
class MockSSLClientContextObserver : public SSLClientContext::Observer { … };
}
INSTANTIATE_TEST_SUITE_P(…);
TEST_P(SSLClientSocketVersionTest, Connect) { … }
TEST_P(SSLClientSocketVersionTest, ConnectSyncVerify) { … }
TEST_P(SSLClientSocketVersionTest, ConnectExpired) { … }
TEST_P(SSLClientSocketVersionTest, ConnectExpiredSyncVerify) { … }
TEST_P(SSLClientSocketVersionTest, SocketDestroyedDuringVerify) { … }
TEST_P(SSLClientSocketVersionTest, ConnectMismatched) { … }
TEST_P(SSLClientSocketVersionTest, ConnectBadValidity) { … }
TEST_P(SSLClientSocketVersionTest, ConnectBadValidityIgnoreCertErrors) { … }
#if BUILDFLAG(ENABLE_CLIENT_CERTIFICATES)
TEST_P(SSLClientSocketVersionTest, ConnectClientAuthCertRequested) { … }
TEST_P(SSLClientSocketVersionTest, ConnectClientAuthSendNullCert) { … }
#endif
TEST_P(SSLClientSocketReadTest, Read) { … }
TEST_F(SSLClientSocketTest, Connect_WithSynchronousError) { … }
TEST_P(SSLClientSocketReadTest, Read_WithSynchronousError) { … }
TEST_P(SSLClientSocketVersionTest, Write_WithSynchronousError) { … }
TEST_P(SSLClientSocketVersionTest, Write_WithSynchronousErrorNoRead) { … }
TEST_P(SSLClientSocketReadTest, Read_FullDuplex) { … }
TEST_P(SSLClientSocketReadTest, Read_DeleteWhilePendingFullDuplex) { … }
TEST_P(SSLClientSocketReadTest, Read_WithWriteError) { … }
TEST_F(SSLClientSocketTest, Connect_WithZeroReturn) { … }
TEST_P(SSLClientSocketReadTest, Read_WithZeroReturn) { … }
TEST_P(SSLClientSocketReadTest, Read_WithAsyncZeroReturn) { … }
TEST_P(SSLClientSocketReadTest, Read_WithFatalAlert) { … }
TEST_P(SSLClientSocketReadTest, Read_SmallChunks) { … }
TEST_P(SSLClientSocketReadTest, Read_ManySmallRecords) { … }
TEST_P(SSLClientSocketReadTest, Read_Interrupted) { … }
TEST_P(SSLClientSocketReadTest, Read_FullLogging) { … }
TEST_F(SSLClientSocketTest, PrematureApplicationData) { … }
TEST_F(SSLClientSocketTest, CipherSuiteDisables) { … }
TEST_F(SSLClientSocketTest, LegacyTLSVersions) { … }
TEST_F(SSLClientSocketTest, ClientSocketHandleNotFromPool) { … }
TEST_P(SSLClientSocketVersionTest, ExportKeyingMaterial) { … }
TEST(SSLClientSocket, SerializeNextProtos) { … }
TEST_P(SSLClientSocketVersionTest, VerifyServerChainProperlyOrdered) { … }
TEST_P(SSLClientSocketVersionTest, VerifyReturnChainProperlyOrdered) { … }
#if BUILDFLAG(ENABLE_CLIENT_CERTIFICATES)
INSTANTIATE_TEST_SUITE_P(…);
TEST_P(SSLClientSocketCertRequestInfoTest,
DontRequestClientCertsIfServerCertInvalid) { … }
TEST_P(SSLClientSocketCertRequestInfoTest, NoAuthorities) { … }
TEST_P(SSLClientSocketCertRequestInfoTest, TwoAuthorities) { … }
TEST_P(SSLClientSocketCertRequestInfoTest, CertKeyTypes) { … }
#endif
TEST_P(SSLClientSocketVersionTest, ConnectSignedCertTimestampsTLSExtension) { … }
TEST_P(SSLClientSocketVersionTest, ConnectSignedCertTimestampsEnablesOCSP) { … }
TEST_P(SSLClientSocketVersionTest, ReuseStates) { … }
TEST_P(SSLClientSocketVersionTest, IsFatalErrorNotSetOnNonFatalError) { … }
TEST_P(SSLClientSocketVersionTest, IsFatalErrorSetOnFatalError) { … }
TEST_P(SSLClientSocketVersionTest, ReusableAfterWrite) { … }
TEST_P(SSLClientSocketVersionTest, SessionResumption) { … }
namespace {
class FakePeerAddressSocket : public WrappedStreamSocket { … };
}
TEST_F(SSLClientSocketTest, SessionResumption_RSA) { … }
TEST_F(SSLClientSocketTest, SessionResumptionAlpn) { … }
TEST_P(SSLClientSocketVersionTest,
SessionResumptionNetworkIsolationKeyDisabled) { … }
TEST_P(SSLClientSocketVersionTest,
SessionResumptionNetworkIsolationKeyEnabled) { … }
TEST_P(SSLClientSocketVersionTest, CertificateErrorNoResume) { … }
TEST_F(SSLClientSocketTest, RequireECDHE) { … }
TEST_F(SSLClientSocketTest, 3DES) { … }
TEST_F(SSLClientSocketTest, SHA1) { … }
TEST_F(SSLClientSocketFalseStartTest, FalseStartEnabled) { … }
TEST_F(SSLClientSocketFalseStartTest, NoAlpn) { … }
TEST_F(SSLClientSocketFalseStartTest, RSA) { … }
TEST_F(SSLClientSocketFalseStartTest, NoAEAD) { … }
TEST_F(SSLClientSocketFalseStartTest, SessionResumption) { … }
TEST_F(SSLClientSocketFalseStartTest, CompleteHandshakeWithoutRequest) { … }
TEST_F(SSLClientSocketFalseStartTest, NoSessionResumptionBeforeFinished) { … }
TEST_F(SSLClientSocketFalseStartTest, NoSessionResumptionBadFinished) { … }
TEST_F(SSLClientSocketTest, Alpn) { … }
TEST_F(SSLClientSocketTest, AlpnClientDisabled) { … }
#if BUILDFLAG(ENABLE_CLIENT_CERTIFICATES)
TEST_P(SSLClientSocketVersionTest, NoCert) { … }
TEST_P(SSLClientSocketVersionTest, SendEmptyCert) { … }
TEST_P(SSLClientSocketVersionTest, SendGoodCert) { … }
TEST_F(SSLClientSocketTest, ClearSessionCacheOnClientCertChange) { … }
TEST_F(SSLClientSocketTest, ClearSessionCacheOnClientCertDatabaseChange) { … }
TEST_F(SSLClientSocketTest, DontClearEmptyClientCertCache) { … }
TEST_F(SSLClientSocketTest, DontClearMatchingClientCertificates) { … }
TEST_F(SSLClientSocketTest, ClearMismatchingClientCertificates) { … }
TEST_F(SSLClientSocketTest,
ClearMismatchingClientCertificatesWithNullParameter) { … }
TEST_F(SSLClientSocketTest,
ClearMismatchingClientCertificatesWithNullCachedCert) { … }
TEST_F(SSLClientSocketTest, DontClearClientCertificatesWithNullCerts) { … }
TEST_F(SSLClientSocketTest, ClearMatchingCertDontClearEmptyClientCertCache) { … }
TEST_F(SSLClientSocketTest, ClearMatchingCertSingleNotMatching) { … }
TEST_F(SSLClientSocketTest, ClearMatchingCertSingleMatching) { … }
TEST_F(SSLClientSocketTest, DontClearSessionCacheOnServerCertDatabaseChange) { … }
TEST_F(SSLClientSocketTest, ClientCertSignatureAlgorithm) { … }
#endif
HashValueVector MakeHashValueVector(uint8_t value) { … }
TEST_P(SSLClientSocketVersionTest, PKPBypassedSet) { … }
TEST_P(SSLClientSocketVersionTest, PKPEnforced) { … }
namespace {
const uint16_t kEncryptingCipher = …;
const uint16_t kSigningCipher = …;
}
struct KeyUsageTest { … };
class SSLClientSocketKeyUsageTest
: public SSLClientSocketTest,
public ::testing::WithParamInterface<
std::tuple<KeyUsageTest, bool >> { … };
const KeyUsageTest kKeyUsageTests[] = …;
TEST_P(SSLClientSocketKeyUsageTest, RSAKeyUsage) { … }
INSTANTIATE_TEST_SUITE_P(…);
TEST_P(SSLClientSocketVersionTest, CTIsRequired) { … }
TEST_P(SSLClientSocketVersionTest, IgnoreCertificateErrorsBypassesRequiredCT) { … }
TEST_P(SSLClientSocketVersionTest, PKPMoreImportantThanCT) { … }
TEST_P(SSLClientSocketVersionTest, SCTAuditingReportCollected) { … }
TEST_F(SSLClientSocketTest, HandshakeFailureServerHello) { … }
TEST_F(SSLClientSocketTest, HandshakeFailureNoClientCerts) { … }
TEST_F(SSLClientSocketTest, LateHandshakeFailureMissingClientCerts) { … }
TEST_F(SSLClientSocketTest, LateHandshakeFailureSendClientCerts) { … }
TEST_F(SSLClientSocketTest, AccessDeniedNoClientCerts) { … }
TEST_F(SSLClientSocketTest, AccessDeniedClientCerts) { … }
TEST_F(SSLClientSocketZeroRTTTest, ZeroRTTEarlyDataBeforeServerHello) { … }
TEST_F(SSLClientSocketZeroRTTTest, ZeroRTTEarlyDataAfterServerHello) { … }
TEST_F(SSLClientSocketZeroRTTTest, ZeroRTTConfirmedAfterRead) { … }
TEST_F(SSLClientSocketZeroRTTTest, ZeroRTTEarlyDataLimit) { … }
TEST_F(SSLClientSocketZeroRTTTest, ZeroRTTEarlyDataLimitCancelReadIfReady) { … }
TEST_F(SSLClientSocketZeroRTTTest, ZeroRTTReject) { … }
TEST_F(SSLClientSocketZeroRTTTest, ZeroRTTWrongVersion) { … }
TEST_F(SSLClientSocketZeroRTTTest, ZeroRTTConfirmHandshake) { … }
TEST_F(SSLClientSocketZeroRTTTest, ZeroRTTReadBeforeWrite) { … }
TEST_F(SSLClientSocketZeroRTTTest, ZeroRTTDoubleConfirmHandshake) { … }
TEST_F(SSLClientSocketZeroRTTTest, ZeroRTTParallelReadConfirm) { … }
TEST_P(SSLClientSocketReadTest, IdleAfterRead) { … }
TEST_F(SSLClientSocketTest, SSLOverSSLBadCertificate) { … }
TEST_F(SSLClientSocketTest, Tag) { … }
TEST_F(SSLClientSocketTest, ECH) { … }
TEST_F(SSLClientSocketTest, ECHWrongKeys) { … }
TEST_F(SSLClientSocketTest, ECHSecurelyDisabled) { … }
TEST_F(SSLClientSocketTest, ECHSecurelyDisabledTLS12) { … }
TEST_F(SSLClientSocketTest, ECHFallbackBadCert) { … }
TEST_F(SSLClientSocketTest, InvalidECHConfigList) { … }
TEST_F(SSLClientSocketTest, ECHGreaseEnabled) { … }
TEST_F(SSLClientSocketTest, ECHGreaseDisabled) { … }
struct SSLHandshakeDetailsParams { … };
const SSLHandshakeDetailsParams kSSLHandshakeDetailsParams[] = …;
class SSLHandshakeDetailsTest
: public SSLClientSocketTest,
public ::testing::WithParamInterface<SSLHandshakeDetailsParams> { … };
INSTANTIATE_TEST_SUITE_P(…);
TEST_P(SSLHandshakeDetailsTest, Metrics) { … }
TEST_F(SSLClientSocketZeroRTTTest, EarlyDataReasonNewSession) { … }
TEST_F(SSLClientSocketZeroRTTTest, EarlyDataReasonNoResume) { … }
TEST_F(SSLClientSocketZeroRTTTest, EarlyDataReasonZeroRTT) { … }
TEST_F(SSLClientSocketZeroRTTTest, EarlyDataReasonReadServerHello) { … }
TEST_F(SSLClientSocketTest, VersionMaxOverride) { … }
TEST_F(SSLClientSocketTest, VersionMinOverride) { … }
TEST_F(SSLClientSocketTest, CancelReadIfReady) { … }
TEST_F(SSLClientSocketTest, ServerName) { … }
class SSLClientSocketAlpsTest
: public SSLClientSocketTest,
public ::testing::WithParamInterface<std::tuple<bool, bool, bool>> { … };
INSTANTIATE_TEST_SUITE_P(…);
TEST_P(SSLClientSocketAlpsTest, Alps) { … }
TEST_P(SSLClientSocketAlpsTest, UnusedProtocols) { … }
}