TLS with php server socket

2019-06-21 20:54发布

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?

标签: php sockets ssl
1条回答
男人必须洒脱
2楼-- · 2019-06-21 21:14

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 
查看更多
登录 后发表回答