简体   繁体   中英

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.

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

Warning: stream_socket_client(): Failed to enable crypto in D:\\xampp\\htdocs\\ssl_test\\index2.php on line 9

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

Note that I have two common files coming with for certificate cert.pem, key.pem and the 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.

If someone know, please help me on this.

Many thanks in advanced, Vannak.

I found the solutions why I got this error because my cert.pem required attache private key. So I need to add one more line attaching my 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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