Ruudt
Ruudt

Reputation: 262

PHP openssl_sign stops executing any and all scripting

I'm having trouble with openssl. To better pinpoint the problem I've done two things; I reduced the code to a single test page (which I've gotten off of php.net). And after the problem persisted I uploaded tat code to a server. The server worked as expected, so the problem must be in my setup!? I found someone who experienced the exact same problem, but his code was different and the solution doesn't work for me (Service Applications and Google Analytics API V3: Error 101 (net::ERR_CONNECTION_RESET)).

This is the code:

<?php

$data = "Beeeeer is really good.. hic...";

// You can get a simple private/public key pair using:
// openssl genrsa 512 >private_key.txt
// openssl rsa -pubout <private_key.txt >public_key.txt

// IMPORTANT: The key pair below is provided for testing only.
// For security reasons you must get a new key pair
// for production use, obviously.

$private_key = <<<EOD
-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBANDiE2+Xi/WnO+s120NiiJhNyIButVu6zxqlVzz0wy2j4kQVUC4Z
RZD80IY+4wIiX2YxKBZKGnd2TtPkcJ/ljkUCAwEAAQJAL151ZeMKHEU2c1qdRKS9
sTxCcc2pVwoAGVzRccNX16tfmCf8FjxuM3WmLdsPxYoHrwb1LFNxiNk1MXrxjH3R
6QIhAPB7edmcjH4bhMaJBztcbNE1VRCEi/bisAwiPPMq9/2nAiEA3lyc5+f6DEIJ
h1y6BWkdVULDSM+jpi1XiV/DevxuijMCIQCAEPGqHsF+4v7Jj+3HAgh9PU6otj2n
Y79nJtCYmvhoHwIgNDePaS4inApN7omp7WdXyhPZhBmulnGDYvEoGJN66d0CIHra
I2SvDkQ5CmrzkW5qPaE2oO7BSqAhRZxiYpZFb5CI
-----END RSA PRIVATE KEY-----
EOD;
$public_key = <<<EOD
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANDiE2+Xi/WnO+s120NiiJhNyIButVu6
zxqlVzz0wy2j4kQVUC4ZRZD80IY+4wIiX2YxKBZKGnd2TtPkcJ/ljkUCAwEAAQ==
-----END PUBLIC KEY-----
EOD;

$binary_signature = "";

// At least with PHP 5.2.2 / OpenSSL 0.9.8b (Fedora 7)
// there seems to be no need to call openssl_get_privatekey or similar.
// Just pass the key as defined above

openssl_sign($data, $binary_signature, $private_key, "sha1");

// Check signature
$ok = openssl_verify($data, $binary_signature, $public_key, "sha1");
echo "check #1: ";
if ($ok == 1) {
    echo "signature ok (as it should be)\n";
} elseif ($ok == 0) {
    echo "bad (there's something wrong)\n";
} else {
    echo "ugly, error checking signature\n";
}

$ok = openssl_verify('tampered'.$data, $binary_signature, $public_key, "sha1");
echo "check #2: ";
if ($ok == 1) {
    echo "ERROR: Data has been tampered, but signature is still valid! Argh!\n";
} elseif ($ok == 0) {
    echo "bad signature (as it should be, since data has beent tampered)\n";
} else {
    echo "ugly, error checking signature\n";
}

When I run the code above the page takes a while to load, but then displays net::ERR_CONNECTION_RESET:

net::ERR_CONNECTION_RESET

I have openssl enabled openssl

And I am using PHP 5.4.7 PHP 5.4.7

These are just as the other case I found, except I'm not initiating anything twice, and this code works on the server, which is PHP 5.3.18.

What more can I do?

Upvotes: 4

Views: 1883

Answers (2)

Ravi Joshi
Ravi Joshi

Reputation: 661

This issue is in due to different versions of OpenSSL Library and OpenSSL Header.

Try PHP version >= 5.5 . You will not face this issue.

Note: If you are using XAMP all the version 1.8.* are having this issues avoid using them in case you want to use OpenSSL.

Upvotes: 0

Ruudt
Ruudt

Reputation: 262

velcrow seems to be right. I've reinstalled and now all is fine.

Upvotes: 1

Related Questions