PEN Vannak
PEN Vannak

Reputation: 41

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 is wrong or missing configure the stream_context_set_option() to the server and where to set the key.pem to the configuration.

Upvotes: 1

Views: 1853

Answers (1)

PEN Vannak
PEN Vannak

Reputation: 41

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.

Upvotes: 3

Related Questions