Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,21 @@ jobs:
php-version: '8.5'
job-description: 'on macOS'

- operating-system: 'ubuntu-latest'
php-version: '8.6'
job-description: '(experimental)'
experimental: true
psalm: 'skip'
style-fix: 'none'
composer-require-checker-version: 'none'
composer-flags: '--ignore-platform-req=php+'

name: PHP ${{ matrix.php-version }} ${{ matrix.job-description }}

runs-on: ${{ matrix.operating-system }}

continue-on-error: ${{ matrix.experimental == true }}

steps:
- name: Set git to use LF
run: |
Expand Down
98 changes: 80 additions & 18 deletions test/ClientBadSslIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,108 @@

namespace Amp\Http\Client;

use Amp\Http\Client\Connection\DefaultConnectionFactory;
use Amp\Http\Client\Connection\UnlimitedConnectionPool;
use Amp\Http\Client\Support\LocalHttpBin;
use Amp\PHPUnit\AsyncTestCase;
use Amp\Socket\ClientTlsContext;
use Amp\Socket\ConnectContext;
use Amp\Socket\ResourceSocket;
use Amp\Socket\ServerSocket;
use Revolt\EventLoop;
use function Amp\delay;
use function Amp\Socket\listen;

final class ClientBadSslIntegrationTest extends AsyncTestCase
{
private HttpClient $client;

protected function setUp(): void
{
parent::setUp();

$this->client = (new HttpClientBuilder)->retry(0)->build();
}

public function testSelfSignedCertificate(): void
{
$request = new Request('https://self-signed.badssl.com/');
$httpBin = LocalHttpBin::startSecureServer();
$client = (new HttpClientBuilder)->retry(0)->build();

$this->expectException(TlsException::class);
$this->expectExceptionMessageMatches("/^Connection to 'self-signed.badssl.com:443' @ '.+' closed during TLS handshake: certificate verify failed$/");
$this->expectExceptionMessageMatches("/^Connection to '127.0.0.1:\d+' @ '.+' closed during TLS handshake: certificate verify failed$/");

$this->client->request($request);
try {
$client->request(new Request($httpBin->getServerUri()));
} finally {
$httpBin->stopServer();
}
}

public function testWrongHostCertificate(): void
{
$request = new Request('https://wrong.host.badssl.com/');
$httpBin = LocalHttpBin::startSecureServer();
$tlsContext = (new ClientTlsContext(''))->withoutPeerVerification()->withPeerNameVerification();
$client = $this->buildClient($tlsContext);

$this->expectException(TlsException::class);
$this->expectExceptionMessageMatches("/^Connection to 'wrong.host.badssl.com:443' @ '.+' closed during TLS handshake: Peer certificate .+ did not match expected .*`wrong.host.badssl.com'$/");
$this->expectExceptionMessageMatches("/^Connection to '127.0.0.1:\d+' @ '.+' closed during TLS handshake: Peer certificate .+ did not match expected .*`127.0.0.1'$/");

$this->client->request($request);
try {
$client->request(new Request($httpBin->getServerUri()));
} finally {
$httpBin->stopServer();
}
}

/**
* @requires OSFAMILY Linux
*/
public function testDhKeyTooSmall(): void
{
$request = new Request('https://dh512.badssl.com/');
$server = $this->listenWithDhCipher();
$tlsContext = (new ClientTlsContext(''))
->withoutPeerVerification()
->withoutPeerNameVerification()
->withSecurityLevel(3);
$client = $this->buildClient($tlsContext);

$this->expectException(TlsException::class);
$this->expectExceptionMessageMatches("/^Connection to 'dh512.badssl.com:443' @ '.+' closed during TLS handshake: dh key too small$/");
$this->expectExceptionMessageMatches("/^Connection to '127.0.0.1:\d+' @ '.+' closed during TLS handshake: dh key too small$/");

try {
$client->request(new Request('https://' . $server->getAddress() . '/'));
} finally {
$server->close();
}
}

private function buildClient(ClientTlsContext $tlsContext): HttpClient
{
$connectContext = (new ConnectContext)->withTlsContext($tlsContext);
$connectionFactory = new DefaultConnectionFactory(connectContext: $connectContext);

return (new HttpClientBuilder)->retry(0)->usingPool(new UnlimitedConnectionPool($connectionFactory))->build();
}

private function listenWithDhCipher(): ServerSocket
{
$server = listen('127.0.0.1:0');

$resource = $server->getResource();
\stream_context_set_option($resource, 'ssl', 'local_cert', __DIR__ . '/tls/amphp.org.pem');
\stream_context_set_option($resource, 'ssl', 'dh_param', __DIR__ . '/tls/dh2048.pem');
\stream_context_set_option($resource, 'ssl', 'ciphers', 'DHE-RSA-AES128-GCM-SHA256');

EventLoop::queue(function () use ($server): void {
while ($socket = $server->accept()) {
$this->handshakeOverTls12($socket);
}
});

return $server;
}

private function handshakeOverTls12(ResourceSocket $socket): void
{
$resource = $socket->getResource();

$this->client->request($request);
do {
$result = @\stream_socket_enable_crypto($resource, true, \STREAM_CRYPTO_METHOD_TLSv1_2_SERVER);
if ($result === 0) {
delay(0.01);
}
} while ($result === 0);
}
}
Loading
Loading