question

Matt Farley avatar image
Matt Farley asked

Problem with PHP and coming July 14 requirement for SHA2

My app works perfectly with the current SHA1 certificate verification. But I'm now testing SHA2, per the new requirement here: https://forums.developer.amazon.com/forums/thread.jspa?threadID=5764&tstart=0 Unfortunately, I'm getting a strange error "algorithm mismatch": $jsonRequest = file_get_contents('php://input'); $pubkeyid = file_get_contents(' https://s3.amazonaws.com/echo.api/echo-api-cert-2.pem'); $sslCheck = openssl_verify($jsonRequest, base64_decode($_SERVER['HTTP_SIGNATURE']), $pubkeyid, 'sha256'); if ($sslCheck != 1) die(openssl_error_string()); ---- and the output --- $ curl -k https://myEndPointUrl/ -H "SignatureCertChainUrl: https://s3.amazonaws.com/echo.api/echo-api-cert-2.pem" -H "Signature:YuKKodC8SuhCWi4xFkmbMLbd3sBn8rSLE5txlto13OOZ5az/ qdCkJagn5nlKtk+0V5J/IpdiSwmbAGH3W/XofH3sUQaLI1PmzEoowgCaLCwo 37tThTaxxCP4TIrk8p0EiciB3cb8KiDtuxAxFvD9aW+/ijI+L2AUH3hncW5n moMXV2QOcNjZ3pYMblVz5XuJINv9Z13BsoTVBbRdFAUazgt1yUg2crHJhnZS rzobInEGY1DL2YGlINt707rACnuOKBZWYsdHCuBglNSyKiQBAlgGo0QtUVHX 30cPk8mMPq9lx7OBI5aK9+dy04Dk2TddNozm/d1otuH7mUXWaonAkg==" -H "Accept-Charset:utf-8" -H "Accept:application/json" -H "Content-Type:application/json; charset=utf-8" --data-binary '{"version": "1.0", "session":{"new": true, "sessionId": "session1234", "application": {"applicationId": "this check has been disabled"}, "attributes": {}, "user": {"userId": null}}, "request": {"type": "LaunchRequest", "requestId": "request5678", "timestamp": "2015-06-22T12:34:56Z"}}' [b]error:04077064:rsa routines:RSA_verify:algorithm mismatch[/b] I've tried most of the algorithms listed at: http://php.net/manual/en/function.openssl-get-md-methods.php $ssl_check = openssl_verify($jsonRequest, base64_decode($_SERVER['HTTP_SIGNATURE']), $pubkeyid, 'sha256'); $ssl_check = openssl_verify($jsonRequest, base64_decode($_SERVER['HTTP_SIGNATURE']), $pubkeyid, 'sha256WithRSAEncryption'); $ssl_check = openssl_verify($jsonRequest, base64_decode($_SERVER['HTTP_SIGNATURE']), $pubkeyid, 'sha512'); etc.... They all yield: error:04077064:rsa routines:RSA_verify:algorithm mismatch Except when I test the old SHA1 algorithms, the error I get is: error:04077068:rsa routines:RSA_verify:bad signature Any ideas? (server is PHP 5.3.2 and supports SHA2)
alexa skills kitsubmission testing certification
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Amazon Customer avatar image
Amazon Customer answered
I got "bad signature" with PHP 5.5.12 using 'sha256' and 'sha256WithRSAEncryption' I iterated over the digests: $digests_and_aliases = openssl_get_md_methods(true); foreach($digests_and_aliases as $d) { $sslCheck = openssl_verify($jsonRequest, base64_decode($_SERVER['HTTP_SIGNATURE']), $pubkeyid, $d); echo $d . "\t" . $sslCheck . "\t" . ($sslCheck != 1 ? openssl_error_string() : '') . PHP_EOL; } ( I know there is redundancy in the above with aliases but I wanted to be comprehensive ). I wonder if the signature in the test code is incorrect?
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Farley avatar image
Matt Farley answered
I had the same thought.... I noticed that if you download the new certificate and view its properties, it lists the algorithm as 1.2.840.113549.1.1.11 whereas the first SHA1 certificate lists the algorithm as SHA1-RSA. See: http://www.alvestrand.no/objectid/1.2.840.113549.1.1.11.html Maybe we need the new certificate to self-identify as the alphanumeric "SHA2-RSA" instead of the 1.2.x
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Farley avatar image
Matt Farley answered
Bump! Can anyone from Amazon comment? Have they reproduced this issue internally? I've tried on several PHP servers and they all have the same problem. July 14 is here!
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Farley avatar image
Matt Farley answered
Good news! I'm happy to report that Amazon has switched to SHA256 and my PHP code works just fine. There must have been an error/typo with the example CURL statement they asked us to test with.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Nick Gardner avatar image
Nick Gardner answered
Hey Matt, Glad you were able to get it worked out, let us know if you have any more questions about it. -Nick
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

BL avatar image
BL answered
Hello, I ran into similar issues this week and had a hard time making my SSL signature verification method work in PHP. Like you, I first tried to pass "sha256" and "sha256WithRSAEncryption" to openssl_verify() but it did not work (algorithm mismatch). I then created a loop to identify which was the right algorithm and [b]it seems that it is still sha1[/b]. foreach (openssl_get_md_methods() as $method) { $ssl_check = openssl_verify($json_request, base64_decode($_SERVER['HTTP_SIGNATURE']), file_get_contents(' https://s3.amazonaws.com/echo.api/echo-api-cert-2.pem'), $method); if ($ssl_check) echo 'Method '.$method.' OK !'; } In my case I got: Method DSA OK ! Method DSA-SHA OK ! [b]Method SHA1 OK ![/b] Method dsaEncryption OK ! Method dsaWithSHA OK ! Method ecdsa-with-SHA1 OK ! [b]Method sha1 OK ![/b] Method whirlpool OK ! So for others who are trying to implement this method, here is the right code: $ssl_check = openssl_verify($json_request, base64_decode($_SERVER['HTTP_SIGNATURE']), file_get_contents(' https://s3.amazonaws.com/echo.api/echo-api-cert-2.pem'), 'sha1'); if (!$ssl_check) die('Invalid SSL Signature'); I hope it helps, and I am assuming at some point you will have to replace 'sha1' by 'sha256'.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Farley avatar image
Matt Farley answered
Interesting! Thanks for the info. In my code I'm not specifying an algorithm (leaving that parameter blank), so PHP must be defaulting to SHA1 for me and I didn't realize it.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.