我对 SSL 和证书不是很了解。我用了帖子 “How to use hash_hmac() with "SHA256withRSA" on PHP? ”,看看我是否可以通过 PayPal 获得 webhook。
我遇到的问题是在调用 openssl_verify()
并返回结果 (0) 后出现以下错误:
OpenSSL error openssl_verify error:04091068:rsa routines:INT_RSA_VERIFY:bad signature
我已尝试解决此问题,但有关错误和网络功能的文档很少甚至没有。
我当前的代码如下所示:
// 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 error openssl_verify error:0906D06C:PEM routines:PEM_read_bio:no start line OpenSSL error openssl_verify error:04091068:rsa routines:INT_RSA_VERIFY:bad signature
我还尝试了一种变体,不在 header 上使用 base64_decode()
,但会得到相同的返回结果 (0),但会出现错误:
OpenSSL error openssl_verify error:04091077:rsa routines:INT_RSA_VERIFY:wrong signature length
签名有什么问题?
最佳答案
您可能想使用这段代码:
$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');
}
关于php - 如何在 PHP 中验证 Paypal webhook 签名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35257436/