ssl - 带 SSL/TLS 的 PHP 套接字 - 无法使用合适的共享密码

标签 ssl client-server php-7 tls1.2 php-socket

我正在尝试使用带 SSL/TLS 的 PHP 套接字创建服务器和客户端。但是,当向服务器发送数据时,我收到以下错误:

PHP Warning: stream_socket_accept(): SSL_R_NO_SHARED_CIPHER: no suitable shared cipher could be used. This could be because the server is missing an SSL certificate (local_cert context option) in server.php on line 32

我查看了其他示例并尝试了各种更改但无济于事。非常感谢任何帮助。

client.php

<?php

$host = '192.168.10.10';
$port = 8080;
$timeout = 30;
$cert = 'assets/cert.pem';

$context = stream_context_create(
    [ 'ssl'=> [ 'local_cert'=> $cert, "crypto_method" => STREAM_CRYPTO_METHOD_TLS_CLIENT ] ]
);

stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
stream_context_set_option($context, 'ssl', 'verify_peer', false);
stream_context_set_option($context, 'ssl', 'verify_peer_name', false);

if ($socket = stream_socket_client( 'tls://'.$host.':'.$port, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context) ) {
    $meta = stream_get_meta_data($socket);

    print_r( $meta );

    fwrite($socket, "Hello, World!\n");
    echo stream_socket_recvfrom($socket,8192);
    fclose($socket);
} else {
   echo "ERROR: $errno - $errstr\n";
}

server.php

<?php

// Set the ip and port we will listen on
$address = '192.168.10.10';
$port = 8080;

$cert = 'assets/cert.pem';

$context = stream_context_create();

stream_context_set_option($context, 'ssl', 'local_cert', $cert);
stream_context_set_option($context, 'ssl', 'crypto_method', STREAM_CRYPTO_METHOD_TLS_SERVER);

stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
stream_context_set_option($context, 'ssl', 'verify_peer', false);
stream_context_set_option($context, 'ssl', 'verify_peer_name', false);

$server = stream_socket_server('tls://'.$address.':'.$port, $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);

// Display server start time
echo "PHP Socket Server started at " . $address . " " . $port . ", at ". date( 'Y-m-d H:i:s' ) ."\n";

// loop and listen
while (true) {
    /* Accept incoming requests and handle them as child processes */
    $client = stream_socket_accept($server);

    $ip = stream_socket_get_name( $client, true );

    echo "New connection from " . $ip;

    stream_set_blocking($client, true); // block the connection until SSL is done
    stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_TLS_SERVER);

    // Read the input from the client – 1024 bytes
    $input = fread($client, 1024);

    // unblock connection
    stream_set_blocking($client, false);

    // Strip all white spaces from input
    $output = preg_replace("[ \t\n\r]", "", $input) . "\0";

    $new = $input;

    // Display Date, IP and Msg received
    echo date( 'Y-m-d H:i:s' ) . " | " . $ip . ": \033[0;32m" . $input . "\033[0m" . PHP_EOL;

    fclose($client);
}

// Close the master sockets
socket_close($sock);

最佳答案

原来这个问题与 OpenSSL 和可用的传输流有关。该问题已通过以下方式解决:

  • 将 OpenSSL 更新到最新版本(1.0.2k,以支持 TLS v1.2)
  • 重新编译 PHP 以启用 tlsv1.2 传输流
  • 更新代码以使用 tlsv1.2:// 作为服务器和客户端上的协议(protocol),例如

    $server = stream_socket_server('tlsv1.2://'.$address.':'.$port, $errno, 
      $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);
    

    $socket = stream_socket_client( 'tlsv1.2://'.$host.':'.$port, $errno,
      $errstr, 30, STREAM_CLIENT_CONNECT, $context)
    

一旦传输流更新为 TLSv1.2,就不再需要密码选项,脚本成功协商 TLS 连接。

关于ssl - 带 SSL/TLS 的 PHP 套接字 - 无法使用合适的共享密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44295193/

相关文章:

php - 在不使用反射的情况下获取类名减去命名空间

java - 随机出现javax.net.ssl.SSLException : Unrecognized SSL message,明文连接?

qt - 如何在基于 Qt 的 TCP 服务器 - 客户端应用程序中忽略 SSL 错误

c# - 连接android客户端成为C#服务器

linux - 基于UDP的服务器无法区分不同的用户

c++ - 确定服务器上已连接客户端的 IP 地址

php - mysqli 中的 mysql_field_names 使用循环

php - 查找 PHP 中何时引入内部(内置)函数和预定义常量(版本号)

javascript - node.js 中的 SSL 证书错误

iis - 如何将 IIS 证书列表导出为详细列表?