TLS with php server socket

2019-06-21 21:19发布

问题:

Hi
My proplem is that I have a open socket in my server side code (written php) and I need to trasform this socket to use TLS, with negotiation and decription of the stream before passing it to the already existing code that work with a clear stream without encryption. What is the simplest solution to archive this?

回答1:

PHP supports TLS/SSL listening using a stream context. Something like this should work:

$listenstr = "tls://0.0.0.0:1234";

$ctx=stream_context_create(array('ssl'=>array(
    "local_cert"=>"mycertandkey.pem",
    "passphrase"=>"badpassphrase"
)));

$s = stream_socket_server($listenstr,$errno,$errstr,STREAM_SERVER_BIND|STREAM_SERVER_LISTEN,$ctx);
$conn = stream_socket_accept($s);
// do stuff with your new connection here 


标签: php sockets ssl