I've finally found a way to verify signature. Sample in the documentation doesn't work. Code bellow DOES work :)
<?php
// $data is assumed to contain the data to be signed
// fetch certificate from file and ready it
$fp = fopen("path/file.pem", "r");
$cert = fread($fp, 8192);
fclose($fp);
// state whether signature is okay or not
// use the certificate, not the public key
$ok = openssl_verify($data, $signature, $cert);
if ($ok == 1) {
echo "good";
} elseif ($ok == 0) {
echo "bad";
} else {
echo "ugly, error checking signature";
}
?>
openssl_verify
(PHP 4 >= 4.0.4, PHP 5)
openssl_verify — Überprüft eine Unterschrift
Beschreibung
Rückgabewerte: Bei korrekter Unterschrift wird 1 zurückgegeben, ist die Unterschrift nicht korrekt wird 0 zurückgegeben. Tritt ein Fehler auf wird -1 zurückgegeben.
Die Funktion openssl_verify() überprüft die Korrektheit der Unterschrift signature für die angegebenen Daten data mit Hilfe des öffentlichen Schlüssels pub_key_id . Das muss der passende öffentliche zum privaten Schlüssel sein, der für die Unterschrift benutzt wurde.
Hinweis: Der optionale vierte Parameter signature_alg wurde in Version 5.2.0 hinzugefügt und hat den Default-Wert OPENSSL_ALGO_SHA1. Weitergehende Informationen enthält die Liste der Signature-Algorithmen.
Beispiel #1 openssl_verify() Beispiel
<?php
// Annahme: $data und $signature enthalten die Daten und die Unterschrift
// holen des öffentlichen Schlüssels aus dem Zertifikat und vorbereiten
$fp = fopen("/src/openssl-0.9.6/demos/sign/cert.pem", "r");
$cert = fread($fp, 8192);
fclose($fp);
$pubkeyid = openssl_get_publickey($cert);
// feststellen, ob die Unterschrift ok ist oder nicht
$ok = openssl_verify($data, $signature, $pubkeyid);
if ($ok == 1) {
echo "gut";
} elseif ($ok == 0) {
echo "schlecht";
} else {
echo "Mist, Fehler beim Überprüfen der Signatur";
}
// Löschen des Schlüssels aus dem Speicher
openssl_free_key($pubkeyid);
?>
Siehe auch openssl_sign().
openssl_verify
02-Mar-2006 06:34
14-Jun-2005 06:25
It should be noted that in order to verify a signature successfully, SHA-1 must be used to digest the data before signing.
If, for example, you are using Java to create a signature and you want to verify it in PHP, you must not use "MD5withRSA" or "SHA512withRSA" as the signature algorithm. Use "SHA1withRSA" or the like...
30-May-2002 12:36
A note about the openssl_verify() (and some of the other functions). The public key comes from a certificate in any of the support formats (as the example shows, use openssl_get_publickey() to get the resource id). But after some trial and error I found the signature string MUST BE BINARY. While no error occurs, passing a base64-formatted signature string (PEM format?), you simply get a mismatch. When I did the base64 decode myself, the verify returned a match (return value 1). You can simply drop the begin/end lines and take the output of the 'base64_decode()' function.
09-Jun-2001 11:56
Anbybody trying to get a Win32 CryptoAPI based digital signature component to work with the openssl_verify() function should be aware that the CryptoAPI PKCS1 (RSA) method uses bytes in reverse order while the openssl_verify() method expects a correctly formatted PKCS1 digital signature (as should be). I learned this the hard way and it took me some time to dig this out. A simple solution in VBScript to reverse the byte order:
N = Len(Blob.Hex)
' reverse bytes in the signature using Hex format
For i = 1 To N - 1 Step 2
s = Mid(Blob, i, 2) & s
Next
s contains the digital signature in reverse order. Blob is an arbitrary binary container.
Send the signature off in Hex format and use a hex2bin method in PHP to convert to the correct format for openssl_verify(), i.e.
function hex2bin($data) {
$len = strlen($data);
return pack("H" . $len, $data);
}
That's it, hope it helps out. BTW I used ASPEncrypt to toy around with on Win32 platform. Works only with Internet Explorer but you could also use a Java applet and have none of the abovementioned problems :-)
