hhvm icon indicating copy to clipboard operation
hhvm copied to clipboard

Incorrect behavior when using TLS sockets with pfsockopen

Open apaloma opened this issue 11 years ago • 0 comments

Data does not appear to be read/written correctly out of a TLS socket when opened with pfsockopen. The second time pfsockopen is called, it seems that the socket returned is unusable.

For my repro, I have a server that waits for a connection. When a connection is received, it reads four bytes and then sends back those same four bytes. It does this three times. The client code just sends four bytes and checks that the four bytes it gets back are the same as what it sent. It does this three times.

Using PHP, this works fine. The client receives the same four bytes it sent all three times. With HHVM, the client only receives the first response. It never receives the second response and appears that the server never receives the client's second request. HHVM will work as expected if you do all three read/write pairs with only one pfsockopen call. It's the read/writes using the socket returned by the second call to pfsockopen that seem to not function properly.

My server code does not work at all in HHVM, but that sounds like a separate issue. Use PHP for the server and HHVM for the client to repro.

Server code:

<?php

$pemfile = './server.pem';

// Certificate data:
$data = array(
    "countryName" => "US",
    "stateOrProvinceName" => "Washington",
    "localityName" => "Bellevue",
    "organizationName" => "Test Corp",
    "organizationalUnitName" => "Test Team",
    "commonName" => "Foo Bar",
    "emailAddress" => "[email protected]"
);

// Generate certificate
$pkey = openssl_pkey_new();
$cert = openssl_csr_new( $data, $pkey );
$cert = openssl_csr_sign( $cert, null, $pkey, 365 );

// Generate PEM file
$pem_passphrase = 'testing';
$pem = array();
openssl_x509_export( $cert, $pem[0] );
openssl_pkey_export( $pkey, $pem[1], $pem_passphrase );

// Save PEM file
file_put_contents( $pemfile, implode( $pem ) );

$context = stream_context_create();

stream_context_set_option( $context, 'ssl', 'local_cert', $pemfile );
stream_context_set_option( $context, 'ssl', 'passphrase', $pem_passphrase );
stream_context_set_option( $context, 'ssl', 'allow_self_signed', true );
stream_context_set_option( $context, 'ssl', 'verify_peer', false );

$socket = stream_socket_server( 'tls://127.0.0.1:9100', $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context );
if ( !$socket )
{
    die( "$errstr ($errno)" );
}

if ( $conn = stream_socket_accept( $socket ) )
{
    $numRequestsExpected = 3;
    while ( $numRequestsExpected > 0 )
    {
        $numRequestsExpected--;
        $alldata = '';
        $data = '';
        $bytes = 4;
        while ( $bytes > 0 && $data = fread( $conn, $bytes ) )
        {
            echo "Read " . strlen( $data ) . " bytes: $data\n";
            $bytes -= strlen( $data );
            $alldata .= $data;
        }

        fwrite( $conn, $alldata, 4 );
    }
    fclose( $conn );
}

fclose( $socket );

?>

Client code:

<?php
function DoRequest()
{
    $conn = pfsockopen( 'tls://127.0.0.1', 9100, $errno, $errstr, 1.0 );

    if ( $conn === FALSE )
    {
        echo "Could not open SSL socket\n";
        die;
    }

    $results = fwrite( $conn, "ping", 4 );
    if ( !$results )
    {
            echo "Failed writing to socket\n";
            die;
    }

    $alldata = '';
    $data = '';
    $bytes = 4;
    stream_set_timeout( $conn, 1 );
    while ( $bytes > 0 && $data = fread( $conn, $bytes ) )
    {
        echo "Read " . strlen( $data ) . " bytes\n";
        $bytes -= strlen( $data );
        $alldata .= $data;
    }

    echo "Read: " . $alldata . "\n";

    if ( $alldata != "ping" )
    {
        echo "      ^ Unexpected response.\n";
        die;
    }
}

echo "Req1\n";
DoRequest();
echo "Req2\n";
DoRequest();
echo "Req3\n";
DoRequest();
?>

Expected server output:

Read 4 bytes: ping
Read 4 bytes: ping
Read 4 bytes: ping

Actual server output (using PHP with the HHVM client):

Read 4 bytes: ping
PHP Warning:  fread(): SSL operation failed with code 1. OpenSSL Error messages:
error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number in /home/valveweb/server.php on line 52
PHP Warning:  fread(): SSL: Connection reset by peer in /home/valveweb/server.php on line 52

Expected client output:

Req1
Read 4 bytes
Read: ping
Req2
Read 4 bytes
Read: ping
Req3
Read 4 bytes
Read: ping

Actual HHVM client output:

Req1
Read 4 bytes
Read: ping
Req2
Read:
      ^ Unexpected response.

apaloma avatar May 14 '15 01:05 apaloma