简体   繁体   English

PHP:无法使用带有功能stream_socket_client()的TLS协议设置私钥文件

[英]PHP: Unable to set private key file using TLS protocol with function stream_socket_client()

I am having a problem with using stream_socket_client() in PHP to connect to the server using TLS protocol. 我在使用TLS协议在PHP中使用stream_socket_client()连接服务器时遇到问题。

Let me show my code below: 让我在下面显示我的代码:

$host = '192.168.0.112';
$context = stream_context_create();
$result = stream_context_set_option($context, 'ssl', 'local_cert', 'D:/xampp/htdocs/ssl_test/cert.pem'); 
$result = stream_context_set_option($context, 'ssl', 'verify_peer', 'true');
$result = stream_context_set_option($context, 'ssl', 'passphrase', '123456'); 
$socket = stream_socket_client('tls://'.$host.':5443/Exec', $errno, $errstr, 60, STREAM_CLIENT_CONNECT, $context);

After I run, I got the message error like this: 运行后,我得到如下消息错误:

Warning: stream_socket_client(): Unable to set private key file `D:\\xampp\\htdocs\\ssl_test\\cert.pem' in D:\\xampp\\htdocs\\ssl_test\\index2.php on line 9 警告:stream_socket_client():无法在第9行的D:\\ xampp \\ htdocs \\ ssl_test \\ index2.php中设置私钥文件`D:\\ xampp \\ htdocs \\ ssl_test \\ cert.pem'

Warning: stream_socket_client(): Failed to enable crypto in D:\\xampp\\htdocs\\ssl_test\\index2.php on line 9 警告:stream_socket_client():无法在第9行的D:\\ xampp \\ htdocs \\ ssl_test \\ index2.php中启用加密

Warning: stream_socket_client(): unable to connect to tls://192.168.0.112:5443/Exec (Unknown error) in D:\\xampp\\htdocs\\ssl_test\\index2.php on line 9 警告:stream_socket_client():无法连接到第9行D:\\ xampp \\ htdocs \\ ssl_test \\ index2.php中的tls://192.168.0.112:5443 / Exec(未知错误)

Note that I have two common files coming with for certificate cert.pem, key.pem and the password="123456" 请注意,我有两个用于证书cert.pem的公用文件,key.pem和password =“ 123456”

I don't know what wrong or missing configure the stream_context_set_option() to the server and where to set the key.pem to the configuration. 我不知道是什么错误或遗漏将stream_context_set_option()配置到服务器以及在哪里将key.pem设置为配置。

If someone know, please help me on this. 如果有人知道,请对此提供帮助。

Many thanks in advanced, Vannak. 非常感谢高级Vannak。

I found the solutions why I got this error because my cert.pem required attache private key. 我找到了为什么出现此错误的解决方案,因为我的cert.pem需要附加的私钥。 So I need to add one more line attaching my key.pem. 因此,我需要再添加一行附加key.pem。

stream_context_set_option($context, 'ssl', 'local_pk', 'D:/xampp/htdocs/ssl_test/key.pem');

Hopefully this problem would be helpful for someone else. 希望这个问题对其他人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM