Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stream socket with SSL

Tags:

I have a problem with stream socket and SSL in PHP 5.6. It seems that SSL is buffering output. In the example below "ehlo" command is ignored (server is not responding). But when something else is sent after "ehlo" e.g. a new line in another call to fwrite, server sends response.

$errno = ''; $errstr = ''; $timeout = 5; $streamContext = stream_context_create(); $host = 'ssl://smtp.some.server.com:465'; $stream = stream_socket_client($host, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext); stream_set_blocking($stream, 1); stream_set_timeout($stream, $timeout); stream_set_write_buffer($stream, 0);  $fgets = fgets($stream); print_r($fgets);  fwrite($stream, "ehlo [127.0.0.1]\r\n");  $response = ''; do {     $line = fgets($stream);     $response .= $line; } while (null !== $line && false !== $line && ' ' != $line{3});  print_r($response);  fclose($stream); 

I've got a response:

220 epicserver.net.pl ESMTP IdeaSmtpServer v0.80.2 ready.

(it's from first print_r).

But when after line: fwrite($stream, "ehlo [127.0.0.1]\r\n"); I add additional socket write: fwrite($stream, "\n"); output is ok:

220 epicserver.net.pl ESMTP IdeaSmtpServer v0.80.2 ready. 250-epicserver.net.pl Hello ip-166-242.pl [94.183.162.232], pleased to meet you 250-PIPELINING 250-ENHANCEDSTATUSCODES 250-SIZE 250-8BITMIME 250-AUTH PLAIN LOGIN 250-AUTH=PLAIN LOGIN 250 HELP 

I've tried using stunnel to check if it's a PHP SSL wrapper issue and when I change $host to $host = 'localhost:110;' (my stunnel configuration) everything works perfectly (without adding that special fwrite method).

Did anyone face that problem?

like image 402
Piotr Olaszewski Avatar asked Jun 17 '16 13:06

Piotr Olaszewski


People also ask

What is the difference between https and SSL?

More Secure – HTTPS or SSL: HTTPS and SSL are similar things but not the same. HTTPS basically a standard Internet protocol that makes the online data to be encrypted and is a more advanced and secure version of the HTTP protocol. SSL is a part of the HTTPS protocol that performs the encryption of the data.

What is SSL stands for?

Secure Sockets Layer (SSL) is a standard security technology for establishing an encrypted link between a server and a client—typically a web server (website) and a browser, or a mail server and a mail client (e.g., Outlook).


1 Answers

Replace 1st fgets with this code, to be sure fgets understand line termination correctly:

$fgets = ''; while (is_resource($stream) && !feof($stream)) {   $fgets .= fgets($stream);  } 
like image 100
Mik Avatar answered Sep 27 '22 21:09

Mik