【问题标题】:TLS Session Resumption in phpphp 中的 TLS 会话恢复
【发布时间】:2016-09-10 00:33:45
【问题描述】:

我正在编写一个多线程 php 客户端,它向 apache 反向代理发出 https 请求并测量一些统计数据。我正在写一篇关于通过 TLS 会话恢复提高性能的学士论文。现在我需要做一个概念证明来证明/反驳这一点。目前我有这个代码:

            $this->synchronized(function($this){
                $this->before = microtime(true);
            }, $this);

            $url = 'https://192.168.0.171/';
            # Some dummy data
            $data = array('name' => 'Nicolas', 'bank account' => '123462343');

            // use key 'http' even if you send the request to https://...
            $options = array(
                'http' => array(
                    'header' => "Content-type: application/x-www-form-urlencoded\r\n",
                    'method' => 'POST',
                    'content' => http_build_query($data)
                ),
                "ssl" => array(
                    "verify_peer" => false,
                    "verify_peer_name" => false,
                    "ciphers" => "HIGH:!SSLv2:!SSLv3"
                )
            );

            $context = stream_context_create($options);
            $result = file_get_contents($url, false, $context);
            $this->synchronized(function($this){
                $this->after = microtime(true);
            }, $this);

            $this->counter_group->write($this->before, $this->after, $result); 

这段代码可以进行完整的握手,但我似乎无法弄清楚如何在 php 中进行恢复握手?

任何帮助将不胜感激!

【问题讨论】:

  • ... 或反驳。 :)
  • file_get_contents() 在请求后立即关闭 TCP 连接……通常。需要保持活动连接头和带有句柄的 fopen()……遗憾的是,我现在没有足够的时间进行调查:-/

标签: php session ssl resume reconnect


【解决方案1】:

你可以试试 PHP curl 并使用 CURL_LOCK_DATA_SSL_SESSION

来自 PHP 文档http://php.net/manual/en/function.curl-share-setopt.php

CURL_LOCK_DATA_SSL_SESSION 共享 SSL 会话 ID,减少时间 重新连接到同一服务器时花费在 SSL 握手上。笔记 默认情况下,SSL 会话 ID 在同一句柄中重复使用

从上面的描述中可以看出,会话 ID 被同一个句柄重用。但是,如果您想在句柄之间共享,您可以使用 curl_share_init 例如

$sh = curl_share_init();
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);

那么你可以在不同的请求之间重复使用$sh

$ch1 = curl_init('https://192.168.0.171');
curl_setopt($ch1, CURLOPT_SHARE, $sh);
curl_setopt($ch1, CURLOPT_SSLVERSION, 6); // TLSV1.2
curl_setopt($ch1, CURLOPT_SSL_CIPHER_LIST, 'TLSv1');

curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS, 
http_build_query( array('name' => 'Nicolas', 'bank account' => '123462343') ));
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);

$output = curl_exec($ch1);

然后重用(恢复握手)

$ch2 = curl_init('https://192.168.0.171');
curl_setopt($ch2, CURLOPT_SHARE, $sh);
curl_setopt($ch2, CURLOPT_SSLVERSION, 6); // TLSV1.2
curl_setopt($ch2, CURLOPT_SSL_CIPHER_LIST, 'TLSv1');
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
// ( ... )
curl_exec($ch2);

并关闭连接

curl_close($ch1);
curl_close($ch2);

但您还需要使用 CURLOPT_SSLVERSION 和 CURLOPT_SSL_CIPHER_LIST 。另外,我认为你应该切换到另一种语言,因为 PHP 有自己的怪癖,如果你证明或反驳论点,最好使用更接近裸机的东西,这样你就可以确定额外的层 (PHP) 不会破坏你的基准。我确实测量了两个请求的性能,这有点违反直觉,但第二个请求几乎慢了两倍。

【讨论】:

  • 感谢您的解决方案,这几乎就是我最终实现它的方式!而且我还意识到在我的论文中使用 php 并不是最佳选择,但没有时间切换语言。话虽如此,我的论文将由我的大学出版。
猜你喜欢
  • 2020-05-21
  • 1970-01-01
  • 2019-01-08
  • 2019-11-20
  • 2013-08-26
  • 2020-12-19
  • 2017-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多