php - php 中的 TLS session 恢复

标签 php session ssl resume reconnect

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

            $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 中执行恢复握手?

如有任何帮助,我们将不胜感激!

最佳答案

您可以尝试 PHP curl 并使用 CURL_LOCK_DATA_SSL_SESSION

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

CURL_LOCK_DATA_SSL_SESSION Shares SSL session IDs, reducing the time spent on the SSL handshake when reconnecting to the same server. Note that SSL session IDs are reused within the same handle by default

正如您从上面的描述中所读到的, session 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 - php 中的 TLS session 恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37216317/

相关文章:

apache - Web 服务器不断重定向到 HTTP

php - PDOException 消息的区域设置

javascript - express-session 不保存任何值

ssl - 了解 ssl 证书

c# - unity SSLStream AuthenticateAsClient 给SslPolicyErrors.RemoteCertificateNotAvailable

session - 在 Tomcat 7 中通过 http header (cookie)禁用 jsessionid

php - 如何使用 jQuery 发出 POST json 请求?

php - 将字符串分别分解为字符和数字

php - 将关联数组值获取到变量中,这是来自 cookie 的字符串格式

java - 如何为服务器端的每个 websocket session 创建相互映射?