【问题标题】:PHP Sockets with SSL / TLS - no suitable shared cipher could be used带有 SSL / TLS 的 PHP 套接字 - 无法使用合适的共享密码
【发布时间】:2017-05-31 21:47:38
【问题描述】:

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

PHP 警告:stream_socket_accept(): SSL_R_NO_SHARED_CIPHER: no 可以使用合适的共享密码。这可能是因为 服务器缺少 SSL 证书(local_cert 上下文选项) server.php 在第 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);

【问题讨论】:

  • 我对 PHP 中的 SSL 服务器一无所知,但您肯定需要在某处设置私钥选项吗?
  • cert.pem 包含私钥和证书(如 local_cert),根据 PHP 文档。
  • 不是根据错误信息,而且您实际上并没有告诉钥匙在里面。
  • 如果未设置私钥,则错误为“无法设置私钥文件”。由于pem都包含了,所以不显示这个错误,
  • 如我所说,我仔细地对 PHP 中的 SSL 一无所知,但这是服务器没有私钥的症状之一。

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


【解决方案1】:

事实证明,该问题与 OpenSSL 和可用的传输流有关。该问题已通过以下方式解决:

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

    $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 连接。

【讨论】:

    【解决方案2】:

    我遇到了类似的问题。但是我的证书和私钥在单独的文件中。根据 PHP 文档,您应该能够单独加载私钥:

    stream_context_set_option($context, 'ssl', 'local_cert', '/etc/apache2/ssl/public_crt.cert');
    stream_context_set_option($context, 'ssl', 'local_pk', '/etc/apache2/ssl/private_key.pem');
    

    原来这对我不起作用,导致原始帖子中出现相同的错误消息。将证书和密钥组合在一个文件中就可以了:

    cat public_crt.cert private_key.pem > combined_cert.pem
    

    并在 PHP 中设置新的组合证书,使事情变得有效:

    stream_context_set_option($context, 'ssl', 'local_cert', '/etc/apache2/ssl/combined_cert.pem');
    

    【讨论】:

      【解决方案3】:

      PHP 在 SSL 方面存在可悲的问题。更糟糕的是,它们依赖于操作系统,大多数修复都是针对 REHL 的。我正在为您提供示例完整代码和其他指南。他们可能会帮助你。为避免这些错误,this library is commonly used。这并不总是实用的。

      第 32 行应该是这样的:

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

      这里是stream blocking doc on PHP manual,也是read thisHere is a good guide on using SSL with PHP,这里是example of client-server。可以通过这种方式添加密码:

      $context = stream_context_create(
          [
              'ssl' => [
                  'ciphers' => 'DHE-RSA-AES256-SHA:LONG-CIPHER',
              ],
          ]
      );
      

      【讨论】:

        猜你喜欢
        • 2018-10-11
        • 2017-01-11
        • 2013-09-02
        • 1970-01-01
        • 1970-01-01
        • 2017-09-28
        • 2011-06-12
        • 1970-01-01
        • 2017-04-12
        相关资源
        最近更新 更多