|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-02-18 18:20 UTC] Jacek at veo dot pl
Description:
------------
I am creating a SMTP server based on PHP. I have a problem with TLS encryption. I tried to enable crypto after stream_socket_accept - it failed.
./configure --with-apxs2 --with-config-file-path --with-libxml-dir --with-zlib --with-zlib-dir --enable-bcmath --with-bz2 --enable-calendar --with-curl --enable-dba --with-inifile --with-flatfile --enable-dbase --enable-exif --enable-filepro --enable-ftp --with-openssl --with-openssl-dir --with-gd --with-jpeg-dir --with-png-dir --without-xpm-dir --with-freetype-dir --enable-gd-native-ttf --with-imap --with-imap-ssl --enable-mbstring --with-mysql --with-mysqli --with-pdo-mysql --enable-soap --enable-sockets --enable-sqlite-utf8 --with-xmlreader --enable-memory-limit --with-iconv --with-ncurses
Reproduce code:
---------------
<?php
$context = stream_context_create();
stream_context_set_option($context, 'tls', 'local_cert', '/server.misc');
echo 1;
$ssl = stream_socket_server('tls://0.0.0.0:4445', $errnum, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $context);
echo 2;
stream_socket_enable_crypto($ssl, TRUE, STREAM_CRYPTO_METHOD_TLS_SERVER);
echo 3;
fclose($ssl);
?>
Expected result:
----------------
123
Actual result:
--------------
12
And script is running. When I try to connect - I can, but script don't "go ahead".
PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 10:00:01 2025 UTC |
Wez, plz take a look at this reproduce code: <?php $ssl = stream_socket_server('tcp://127.0.0.1:4445', $errnum, $errstr); stream_socket_enable_crypto($ssl, TRUE, STREAM_CRYPTO_METHOD_SSLv23_SERVER); ?>#!/opt/php/513/bin/php <?php error_reporting(2047); $c=array('tls'=>array( 'verify_peer' =>false, 'allow_self_signed' =>true, 'cafile' =>'/opt/php/testscripts/newkey.pem', 'capath' =>'/opt/php/testscripts/', 'local_cert' =>'/opt/php/testscripts/newkey.pem', 'passphrase' =>'smtp', 'CN_match' =>'ai000.de' ) ); $tls=stream_context_create($c); $c=stream_socket_server('tcp://127.0.0.1:1100',$er,$es,STREAM_SERVER_BIND|STREAM_SERVER_LISTEN,$tls); while(1){ if($s=@stream_socket_accept($c)){ echo "Verbindung\n".openssl_error_string()."\n\n"; @fwrite($s,"220 ESMTP\r\n"); echo @fgets($s); @fwrite($s,"250 STARTTLS\r\n"); echo @fgets($s); @fwrite($s,"220 ESMTP\r\n"); var_dump(stream_socket_enable_crypto($s,true,STREAM_CRYPTO_METHOD_TLS_SERVER)); echo @fgets($s); } } ?> This is my test code. The negotation is endless among server script and Mozilla-Thunderbird. When I start the script below, my browser tell me: there are no conforming algorithms available. $c=stream_socket_server('ssl://127.0.0.1:1100',$er,$es,STREAM_SERVER_BIND|STREAM_SERVER_LISTEN,$tls); The Discription ("stream_socket_enable_crypto ( resource stream, bool enable [, int crypto_type [, resource session_stream]] )") is obscure. What is "resource session_stream"? This word is singly used there and no records describe it.Code: ----- <?php $context = stream_context_create(array( 'ssl' => array( 'verify_peer' => FALSE, 'allow_self_signed' => TRUE, 'local_cert' => '/host.pem' ) )); echo 1; $ssl = stream_socket_server('ssl://0.0.0.0:4445', $errnum, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $context); echo 2; stream_socket_enable_crypto($ssl, TRUE, STREAM_CRYPTO_METHOD_TLS_SERVER); echo 3; fclose($ssl); ?> Result: ------- I created combined file, as on the website, but I receive (PHP 5.1.4): Warning: stream_socket_enable_crypto(): Unable to set private key file `/host.pem' in /repr.php on line 15 Warning: stream_socket_enable_crypto(): failed to create an SSL handle in /repr.php on line 15The below example calls stream_socket_enable_crypto on client stream socket and on a server stream socket. It works on my version of PHP 5.1.2 just fine. So, if it doesn't work for you then, we can determine what version of PHP5 is broken. This is the output. e@eon:~/dev/examples/tls$ ./tls-server.php 1: STARTTLS Error (if any): 2: EHLO world As you see... no errors. It got the "EHLO world" command sent over an TLS channel from the client just fine. e@eon:~/dev/examples/tls$ ./tls-client.php 1: 220 ESMTP 2: 250 STARTTLS bool(true) This is the code that produced the output. Below is the code I use to make a sample TLS SMTP server. It works perfectly on my platform and version of PHP. The output from the tests are above. #!/usr/bin/php5 <?php // Hello World! SMTP TLS Server // Tested on PHP 5.1.2-1+b1 (cli) (built: Mar 20 2006 04:17:24) $context = stream_context_create(); // local_cert must be in PEM format stream_context_set_option($context, 'ssl', 'local_cert', './server.pem'); // Pass Phrase (password) of private key stream_context_set_option($context, 'ssl', 'passphrase', 'comet'); stream_context_set_option($context, 'ssl', 'allow_self_signed', true); stream_context_set_option($context, 'ssl', 'verify_peer', false); // Create the server socket $server = stream_socket_server('tcp://0.0.0.0:9001', $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context); while(1){ if($client=stream_socket_accept($server)){ @fwrite($client,"220 ESMTP\r\n"); @fwrite($client,"250 STARTTLS\r\n"); // Client should now send STARTTLS echo "1: " . @fgets($client); // We start the SSL Channel stream_socket_enable_crypto($client,true,STREAM_CRYPTO_METHOD_TLS_SERVER); echo "Error (if any): ".openssl_error_string()."\n"; @fwrite($client,"220 ESMTP\r\n"); echo "2: " . @fgets($client); } } ?> #!/usr/bin/php5 <? $fp = fsockopen("tcp://localhost", 9001, $errno, $errstr,30); if (!$fp) die ("Unable to connect: $errstr ($errno)"); echo "1: " . fgets($fp); echo "2: " . fgets($fp); fwrite($fp, "STARTTLS\r\n"); var_dump(stream_socket_enable_crypto($fp,true,STREAM_CRYPTO_METHOD_TLS_CLIENT) ); fwrite($fp, "EHLO world"); ?>