8000 fix: throw exception on stream_socket_enable_crypto() warning by ramunasd · Pull Request #1158 · php-amqplib/php-amqplib · GitHub
[go: up one dir, main page]

Skip to content

fix: throw exception on stream_socket_enable_crypto() warning #1158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2024
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
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