Macs
Macs

Reputation: 71

TLS with php server socket

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?

Upvotes: 7

Views: 3174

Answers (1)

foogames
foogames

Reputation: 39

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 

Upvotes: 3

Related Questions