Skip to content

Commit 118602b

Browse files
tpirc3brusepeng
authored andcommitted
 修复证书序列号读取问题
* Compare serialNo using uppercase * Make ext-bcmath optional in composer.json * (fix) Fix read serialNumber of certificate
1 parent 5fe454f commit 118602b

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
composer.phar
44
/vendor/
5+
test/
56

67
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
78
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wechatpay/wechatpay-guzzle-middleware",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "WechatPay API V3 Guzzle Middleware",
55
"type": "library",
66
"keywords": [
@@ -10,8 +10,7 @@
1010
"license": "Apache-2.0",
1111
"require": {
1212
"php": ">=5.5",
13-
"ext-openssl": "*",
14-
"ext-bcmath": "*"
13+
"ext-openssl": "*"
1514
},
1615
"require-dev": {
1716
"guzzlehttp/guzzle": "^6.3"
@@ -20,6 +19,7 @@
2019
"psr-4": { "WechatPay\\GuzzleMiddleware\\" : "src/" }
2120
},
2221
"suggest": {
22+
"ext-bcmath": "Require bcmath in php 5.* version.",
2323
"guzzlehttp/guzzle": "For using wechatpay guzzle middleware."
2424
}
2525
}

src/Auth/CertificateVerifier.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function __construct(array $certificates)
5555
*/
5656
public function verify($serialNumber, $message, $signature)
5757
{
58+
$serialNumber = \strtoupper(\ltrim($serialNumber, '0')); // trim leading 0 and uppercase
5859
if (!isset($this->publicKeys[$serialNumber])) {
5960
return false;
6061
}
@@ -77,21 +78,31 @@ public function verify($serialNumber, $message, $signature)
7778
protected function parseSerialNo($certificate)
7879
{
7980
$info = \openssl_x509_parse($certificate);
80-
if (!isset($info['serialNumber'])) {
81+
if (!isset($info['serialNumber']) && !isset($info['serialNumberHex'])) {
8182
throw new \InvalidArgumentException('证书格式错误');
8283
}
8384

84-
$serialNo = $info['serialNumber'];
85-
if (\is_int($serialNo)) {
86-
return \strtoupper(\dechex($serialNo));
85+
$serialNo = '';
86+
// PHP 7.0+ provides serialNumberHex field
87+
if (isset($info['serialNumberHex'])) {
88+
$serialNo = $info['serialNumberHex'];
89+
} else {
90+
// PHP use i2s_ASN1_INTEGER in openssl to convert serial number to string,
91+
// i2s_ASN1_INTEGER may produce decimal or hexadecimal format,
92+
// depending on the version of openssl and length of data.
93+
if (\strtolower(\substr($info['serialNumber'], 0, 2)) == '0x') { // HEX format
94+
$serialNo = \substr($info['serialNumber'], 2);
95+
} else { // DEC format
96+
$value = $info['serialNumber'];
97+
$hexvalues = ['0','1','2','3','4','5','6','7',
98+
'8','9','A','B','C','D','E','F'];
99+
while ($value != '0') {
100+
$serialNo = $hexvalues[\bcmod($value, '16')].$serialNo;
101+
$value = \bcdiv($value, '16', 0);
102+
}
103+
}
87104
}
88-
$hexvalues = ['0','1','2','3','4','5','6','7',
89-
'8','9','A','B','C','D','E','F'];
90-
$hexval = '';
91-
while ($serialNo != '0') {
92-
$hexval = $hexvalues[\bcmod($serialNo, '16')].$hexval;
93-
$serialNo = \bcdiv($serialNo, '16', 0);
94-
}
95-
return $hexval;
105+
106+
return \strtoupper($serialNo);
96107
}
97108
}

0 commit comments

Comments
 (0)