How to check Paypal web host signature in PHP?

I am not very good at SSL and certificates. I used the message " How to use hash_hmac () with" SHA256withRSA "in PHP? " To find out if I can work with webcams using PayPal.

The problem I am facing is getting the following error after the call openssl_verify()and return result (0):

OpenSSL error opensl_verify: 04091068: rsa procedure: INT_RSA_VERIFY: bad signature

I tried to solve this problem, but the documentation of errors and features on the Internet is minimal.

My current code is as follows:

 // get the header post to my php file by PayPal
 $headers = apache_request_headers();
 // get the body post to me php file by PayPal
 $body = @file_get_contents('php://input');
 $json = json_decode($body);

 // TransmissionId|TransmissionTimeStamp|WebhookId|CRC32 as per PayPal documentation
 $sigString = $headers['Paypal-Transmission-Id'].'|'.$headers['Paypal-Transmission-Time'].'|'.$json->id.'|'.crc32($body);

 // $headers['Paypal-Cert-Url'] contains the "-----BEGIN CERTIFICATE---MIIHmjCCBoKgAwIBAgIQDB8 ... -----END CERTIFICATE-----"
 $pubKey = openssl_pkey_get_public(file_get_contents($headers['Paypal-Cert-Url']));

 // and this is the call to verify that returns result (0)
 $verifyResult = openssl_verify($sigString, base64_decode($headers['Paypal-Transmission-Sig']), $pubKey, 'sha256WithRSAEncryption');

, , , openssl_pkey_get_details($pubKey), :

OpenSSL opensl_verify: 0906D06C: PEM: PEM_read_bio: OpenSSL opensl_verify: 04091068: rsa-: INT_RSA_VERIFY:

, base64_decode() , (0) :

OpenSSL opensl_verify: 04091077: rsa-: INT_RSA_VERIFY:

?

+4
2

, , Open SSL API- PayPal PHP Restful.

API PayPal Restful , webhook: /v1//--

PayPal-PHP-SDK VerifyWebhookSignature, .

Script, VerifyWebhookSignature.

+1

:

$pubKey = openssl_pkey_get_public(file_get_contents($headers['PAYPAL-CERT-URL']));
$details = openssl_pkey_get_details($pubKey);

$verifyResult = openssl_verify($sigString, base64_decode($headers['PAYPAL-TRANSMISSION-SIG']), $details['key'], 'sha256WithRSAEncryption');

if ($verifyResult === 0) {
    throw new Exception('signature incorrect');
} elseif ($verifyResult === -1) {
    throw new Exception('error checking signature');
}
-1

Source: https://habr.com/ru/post/1627679/


All Articles