Jump to content

Recommended Posts

Need to conduct an SSL Socket session via PHP.

 

The following openssl command line works:

 

openssl s_client -cert '/path/to/cert/cert.pem'  -key '/path/to/key/key.pem'  -CAfile /path/to/ca/ca.crt -connect my.host.tld:12345

 

But I can't seem to "make it happen" via PHP.

 

After reading that fsockopen() does not support an SSL "context" parameter in PHP 5, I tried a simpler test via stream_context_create() and stream_socket_client(), etc., but it seems that I can't supply the same arguments/opitons to stream_context_create as I can to openssl. For instance, stream_context_create seems not to allow setting a "key" option, but seems to require cert passphrase which the working ssl command does not require (not to mention that I do not know the password for the cert. 

 

It would be really cool if I could so soemthing like this, but I'm stuck as to how to resolve the "key vs. cert password" issue:

 

<?php

 

$host = 'my.host.tld';

$port = 12345;

$timeout = 10;

 

$cert = '/path/to/cert/cert.pem';

$key = '/path/to/cafile/cafile.pem';

$cafile = '/path/to/cafile/cafile.pem';

 

$context = stream_context_create(array('ssl'=>array('local_cert'=>$cert,

                                                    'key'=>'$key,

                                                    'cafile'=>'$cafile,

)));

 

if ($fp = stream_socket_client('ssl://'.$host.':'.$port, $errno, $errstr, 30,

        STREAM_CLIENT_CONNECT, $context)) {

    fwrite($fp, "\n");

    echo fread($fp, 26);

    fclose($fp);

} else {

  echo "ERROR: $errno - $errstr<br />\n";

}

 

?>

 

Any help appreciated.

Link to comment
https://forums.phpfreaks.com/topic/67461-ssl-socket-connection-problems/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.