5

I am trying to verify a signature in php, and have exhausted myself trying every example I have found on the net. I've gone round in circles so much I have probably missed the solution now.

So I have test data which is

$msg = "test data";

which produced this signature using the private key from my key pair

$signature = "avALtk00btVyV74e5UdXJ/VClVV/fsuoLZpXQjiCrkVijsmMZsYWZujN56+Aa2CEQYkomDsm9CJ/Tue7lNP0tYVZz9Y0RngpcV9VT9V3i+3rbvbBEnuJuS/5e+PR7kQGMh8rVuCtHpAJhSePMyipC3kM90EQJ0jyY3rFaHDNpSzVBpOnRYLzqbsdy45v0bN78A2J/HaIhJy87Sh4X1a+WMg9PLkqSSYZnRYOB8XVDCYfyeeekcvI4rvP51wBQcaLwu7S0xPQA8yHfJqMXCqdmBVUQZrk/X+CujdXUyJItDWA8j2N8AHmcAD5oRaJ6bX3zCQFM1QnKMi1ETLudzIqfA==";

and this is the public key from the signing key pair

$key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxv4nCiH4vXvSLsvlceCOk3yfH1EQgNqNaVGdnFxdw9IIjSVZvTVH45NCodCJ0GlHoDwQM7DMV1+QrtF91cn44xg4Ys9zr1xkaT4jWBTe3YKoTqJoLHR4UU03F6Y1jTELhjY2a2Kt0ijyvAOKM4bm3gCItfMx59ETGInz7Oubb1T4IJ8TuWmZsh+X57c6fgv0B2+eTr/5FMK2VxXV5tHkB9UNLBgnbw0IZuC6izF4OFk9hxgh96i5wCf2HhHaNoEryx7ZV2ZG9a0OQnYZ+x1zaOIw6dJkV7rip3H57ksQfoQWM0GKMBB7cWIgWsf/GlbYTVgw26MvzEzGPb9uCfx8rwIDAQAB";

I have tried wrapping the key with this

$pubkey = "-----BEGIN RSA PUBLIC KEY-----" . $key . "-----END RSA PUBLIC KEY-----";

and with this

$pubkey = "-----BEGIN PUBLIC KEY-----" . $key . "-----END PUBLIC KEY-----";

I have tried creating a public key id with both wrappings and without any wrapping, like this

$pubkeyid = openssl_pkey_get_public($pubkey);
$pubkeyid = openssl_pkey_get_public($key);

and I have tried verifying the signature with $key and $pubkeyid, with various algorithms and with none, like this

openssl_verify($msg, base64_decode($signature), $pubkeyid);
openssl_verify($msg, base64_decode($signature), $key);
openssl_verify($msg, base64_decode($signature), $pubkeyid, "sha256withRSAEncryption");
openssl_verify($msg, base64_decode($signature), $key, "sha256withRSAEncryption");
openssl_verify($msg, base64_decode($signature), $pubkeyid, OPENSSL_ALGO_SHA256);
openssl_verify($msg, base64_decode($signature), $key, OPENSSL_ALGO_SHA256);

I probably tried some other permutations, but can't remember now. My head hurts.

No matter what I tried, I have not managed to verify the signature. I can verify the signature using the public key in java easily.

I loathe asking for a working php example because I have tried so many I have already found on the net and just can't get them to work. Unfortunately phpseclib is not an option for me, so I have to use openssl.

Where am I going wrong?

11
  • How did you generate the signature? Have you confirmed that openssl_pkey_get_public isn't returning false before trying to use it? What is the source of the key used for signing? What format is it in?
    – miken32
    Nov 7, 2018 at 3:17
  • Have you thought of using a libary, there a a few good ones out there that may help you, LibSodium, HaLite or PHPSecLib just to name a few. In general they use OpenSSL if it's available (and fall back to a pure PHP implementation if its not), But the real benefit is a OOP interface and more logical workflow for your code. Unfortunately phpseclib is not an option for me Why not, if I may ask? Nov 7, 2018 at 3:34
  • @miken32 good point, I haven't checked the return value from openssl_pkey_get_public for a while now. Just checked and it returns false from both wrappings and if I just use key without a wrapping. The signature was generated in java and verified in java, before sending it to the php script. The signature and public key received in the php script have been checked against their values in the java program to make sure they are the same. Nov 7, 2018 at 3:35
  • @ArtisticPhoenix I can't use phpseclib because the anal web host refuses to load it. I decided to try using openssl instead of trying to load phpseclib locally, but have been swallowed up in this nightmare, so not got round to it. I think I'm at the stage I need to do that so I can use RSA, but I'll look in to those libraries you mentioned, if I can load them locally. Nov 7, 2018 at 3:38
  • 2
    Given the value you have for $key above, you want $key = "-----BEGIN PUBLIC KEY-----\n$key\n-----END PUBLIC KEY-----"; Algorithm for verification depends on the algorithm used to generate the signature.
    – miken32
    Nov 7, 2018 at 4:16

1 Answer 1

3

Thanks to @miken32 I have now finally fixed the code. Turns out all I was missing was a couple of line breaks when formatting the PEM key. So the final, and very simple code is:

// Get base64 encoded public key.
// NOTE: this is just for testing the code, final production code stores the public key in a db.
$pubkey = $_POST['pubkey'];

// Convert pubkey in to PEM format (don't forget the line breaks).
$pubkey_pem = "-----BEGIN PUBLIC KEY-----\n$pubkey\n-----END PUBLIC KEY-----";

// Get public key.
$key = openssl_pkey_get_public($pubkey_pem);

if ($key == 0)
{
    $result = "Bad key zero.";
}
elseif ($key == false)
{
    $result = "Bad key false.";
}
else
{
    // Verify signature (use the same algorithm used to sign the msg).
    $result = openssl_verify($msg, base64_decode($signature), $key, OPENSSL_ALGO_SHA256);

    if ($result == 1)
    {
        $result = "Verified";
    }
    elseif ($result == 0)
    {
        $result = "Unverified";
    }
    else
    {
        $result = "Unknown verification response";
    }

}

// do something with the result.
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.