forge icon indicating copy to clipboard operation
forge copied to clipboard

Unable to verify forge signature on server side (RSA/sha256/PSS)

Open harryzun opened this issue 11 years ago • 3 comments

Despite my best efforts, I can't seem to get client-side Forge to work with server-side phpseclib.

Now I know there are existing issues with using forge with third party libraries, but even after looking over those issues and stackoverflow, I'm still having trouble with verifying signatures.

The client signs some data, encodes it in base64, and sends it to the server using AJAX where it is decoded and verified.

// Client (forge)
var md = forge.md.sha256.create();
md.update('plaintext', 'utf8');
var pss = forge.pss.create({
    md: forge.md.sha256.create(),
    mgf: forge.mgf.mgf1.create(forge.md.sha256.create()),
    saltLength: 16
});
var signature = clientKeyPair.privateKey.sign(md, pss);
return forge.util.encode64(signature); // sent to server
// Server (phpseclib)
$signature = base64_decode($fromClient); // received from client
$rsa = new Crypt_RSA();
$rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PSS);
$rsa->setHash('sha256');
$rsa->setSaltLength(16);
$rsa->loadKey($publicKey);
return $rsa->verify('plaintext', $signature); // true when signature is valid

Neither forge nor phpseclib report any errors, yet verifying the signature on phpseclib always returns false when it shouldn't. What am I doing wrong? Perhaps I'm using the wrong encoding?

Here are two signatures made with each library using the same parameters:

forge:

TCcbd2EGCcDVedj8v3u5NDbAQXi/SLqBHcLkFAxGELt/F23+mPwhlv+EM/3f1Y125rUydaCDizNQ2w20bYy7+K8KHiko5CRt8rMNj0i+Pld5kHjQxas8z/r00SrmVf4UTIfxVO8r8TvyeFHiC30CiVJk7aA7gkz6PMLj/Zw9mCQNu04BENce9fAINkw5epiUHYp7jHJ9VwtdGsfBH2Ks0M6/gDPUs29XXfFiMkC2Zwzcgtg2pzNgY6MJRtZ3VsHih71PkULTPXHXs3hXlZe7I5cOk06rR7y/z3wUdXurE0c9SHgkTMOwxI/wcaHGA0ma0WnaF+3h4a7ADmqZcC7tvg==

phpseclib:

Hj4VlvtQwhZspMcc1ZYWk09O40UdEcOTzx/mP+rwayA1e7LkpUdzSxVe8q34H7oB4TYh+NtjfiX4aZhh2MVlwA4NtrXH91vmPxjU+phEQs0evgSyHH1UOBluK+WAW8yhwBvtDA0DdlicHpNrNe4/2syuqH+SDfVLpDhCRL1P3IcdBGwZsGIWGCXF2Cd9qB9ENB7MwLck2+e9pbz66VKdndITk5wto9okyKE4cW+8KBurpzWwJ/1KFbTanlrf+h2Z0Y6sP22vjKkD1HqBaWfI5qP89y97QQRlIl71d1fSY04zMROsbPoUxFUCmCaeZbwi3wT2vC9lpSr26ZqftkULFw==

Thanks!

harryzun avatar Mar 25 '15 00:03 harryzun

@harryzun, have you tried setting the MGF hash algorithm before verifying on the php side? I think the default for phpseclib is SHA-1 and you're using SHA-256 in your client-side forge code:

$rsa->setMGFHash('sha256');

dlongley avatar Mar 25 '15 01:03 dlongley

Turns out that was the issue, thanks so much! Sorry I bugged you with what apparently wasn't even an issue with Forge!

harryzun avatar Mar 25 '15 01:03 harryzun

No problem, glad it's working! :)

dlongley avatar Mar 25 '15 02:03 dlongley