Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TLS with php server socket

Tags:

php

ssl

sockets

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?

like image 573
Macs Avatar asked Jan 20 '11 11:01

Macs


1 Answers

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 
like image 88
foogames Avatar answered Oct 21 '22 10:10

foogames