Skip to content

Commit

Permalink
fix: throw exception on stream_socket_enable_crypto() warning (php-am…
Browse files Browse the repository at this point in the history
  • Loading branch information
ramunasd authored Feb 7, 2024
1 parent 0ed3ecc commit 76eee28
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
27 changes: 21 additions & 6 deletions PhpAmqpLib/Wire/IO/StreamIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function connect()

$options = stream_context_get_options($context);
if (isset($options['ssl']['crypto_method'])) {
$this->enable_crypto();
$this->enableCrypto();
}

$this->heartbeat = $this->initial_heartbeat;
Expand Down Expand Up @@ -432,16 +432,31 @@ protected function extract_error_code($message)
return 0;
}

private function enable_crypto(): void
/**
* @throws AMQPIOException
*/
private function enableCrypto(): void
{
$timeout_at = time() + ($this->read_timeout + $this->write_timeout) * 2; // 2 round-trips during handshake

do {
$enabled = stream_socket_enable_crypto($this->sock, true);
} while ($enabled !== true && time() < $timeout_at);
try {
$this->setErrorHandler();
do {
$enabled = stream_socket_enable_crypto($this->sock, true);
if ($enabled === true) {
return;
}
$this->throwOnError();
usleep(1e3);
} while ($enabled === 0 && time() < $timeout_at);
} catch (\ErrorException $exception) {
throw new AMQPIOException($exception->getMessage(), $exception->getCode(), $exception);
} finally {
$this->restoreErrorHandler();
}

if ($enabled !== true) {
throw new AMQPIOException('Can not enable crypto');
throw new AMQPIOException('Could not enable socket crypto');
}
}
}
1 change: 0 additions & 1 deletion tests/Unit/Connection/AMQPConnectionConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public function external_auth_with_user_credentials()
public function secure_with_incorrect_crypto_method()
{
$this->expectException(AMQPIOException::class);
$this->expectExceptionMessage('Can not enable crypto');

$cert_dir = realpath(__DIR__ . "/../../certs");
$config = new AMQPConnectionConfig();
Expand Down

0 comments on commit 76eee28

Please sign in to comment.