Skip to content

Commit 2413eac

Browse files
committed
Add public key certificate encryption method.
1 parent d51b35c commit 2413eac

File tree

4 files changed

+347
-32
lines changed

4 files changed

+347
-32
lines changed

src/AbstractAopGateway.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,86 @@ public function setPrivateKey($value)
158158
}
159159

160160

161+
/**
162+
* @return mixed
163+
*/
164+
public function getAlipayRootCert()
165+
{
166+
return $this->getParameter('alipay_root_cert');
167+
}
168+
169+
170+
/**
171+
* @param $value
172+
*
173+
* @return $this
174+
*/
175+
public function setAlipayRootCert($value)
176+
{
177+
return $this->setParameter('alipay_root_cert', $value);
178+
}
179+
180+
181+
/**
182+
* @return mixed
183+
*/
184+
public function getAlipayPublicCert()
185+
{
186+
return $this->getParameter('alipay_public_cert');
187+
}
188+
189+
190+
/**
191+
* @param $value
192+
*
193+
* @return $this
194+
*/
195+
public function setAlipayPublicCert($value)
196+
{
197+
return $this->setParameter('alipay_public_cert', $value);
198+
}
199+
200+
201+
/**
202+
* @return mixed
203+
*/
204+
public function getAppCert()
205+
{
206+
return $this->getParameter('app_cert');
207+
}
208+
209+
210+
/**
211+
* @param $value
212+
*
213+
* @return $this
214+
*/
215+
public function setAppCert($value)
216+
{
217+
return $this->setParameter('app_cert', $value);
218+
}
219+
220+
221+
/**
222+
* @return mixed
223+
*/
224+
public function getCheckAlipayPublicCert()
225+
{
226+
return $this->getParameter('check_alipay_public_cert');
227+
}
228+
229+
230+
/**
231+
* @param bool $value
232+
*
233+
* @return $this
234+
*/
235+
public function setCheckAlipayPublicCert($value)
236+
{
237+
return $this->setParameter('check_alipay_public_cert', $value);
238+
}
239+
240+
161241
/**
162242
* @return mixed
163243
*/

src/Common/Signer.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/**
88
* Sign Tool for Alipay
99
* Class Signer
10+
*
1011
* @package Omnipay\Alipay\Common
1112
*/
1213
class Signer
@@ -17,7 +18,7 @@ class Signer
1718
const KEY_TYPE_PUBLIC = 1;
1819
const KEY_TYPE_PRIVATE = 2;
1920

20-
protected $ignores = ['sign', 'sign_type'];
21+
protected $ignores = ['sign'];
2122

2223
protected $sort = true;
2324

@@ -124,16 +125,29 @@ protected function sort(&$params)
124125
}
125126

126127

128+
/**
129+
* @param string $privateKey
130+
* @param int $alg
131+
*
132+
* @return string
133+
* @throws Exception
134+
*/
127135
public function signWithRSA($privateKey, $alg = OPENSSL_ALGO_SHA1)
128136
{
129137
$content = $this->getContentToSign();
130138

131-
$sign = $this->signContentWithRSA($content, $privateKey, $alg);
132-
133-
return $sign;
139+
return $this->signContentWithRSA($content, $privateKey, $alg);
134140
}
135141

136142

143+
/**
144+
* @param string $content
145+
* @param string $privateKey
146+
* @param int $alg
147+
*
148+
* @return string
149+
* @throws Exception
150+
*/
137151
public function signContentWithRSA($content, $privateKey, $alg = OPENSSL_ALGO_SHA1)
138152
{
139153
$privateKey = $this->prefix($privateKey);
@@ -153,9 +167,8 @@ public function signContentWithRSA($content, $privateKey, $alg = OPENSSL_ALGO_SH
153167
}
154168

155169
openssl_free_key($res);
156-
$sign = base64_encode($sign);
157170

158-
return $sign;
171+
return base64_encode($sign);
159172
}
160173

161174

@@ -201,8 +214,8 @@ public function format($key, $type)
201214
/**
202215
* Convert one line key to standard format
203216
*
204-
* @param $key
205-
* @param $type
217+
* @param string $key
218+
* @param int $type
206219
*
207220
* @return string
208221
*/
@@ -236,6 +249,15 @@ public function verifyWithMD5($content, $sign, $key)
236249
}
237250

238251

252+
/**
253+
* @param string $content
254+
* @param string $sign
255+
* @param string $publicKey
256+
* @param int $alg
257+
*
258+
* @return bool
259+
* @throws Exception
260+
*/
239261
public function verifyWithRSA($content, $sign, $publicKey, $alg = OPENSSL_ALGO_SHA1)
240262
{
241263
$publicKey = $this->prefix($publicKey);

src/Common/helpers.php

Lines changed: 121 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php
22

3-
/**
4-
* Get an item from an array using "dot" notation.
5-
*
6-
* @param array $array
7-
* @param string $key
8-
* @param mixed $default
9-
*
10-
* @return mixed
11-
*/
12-
if (! function_exists('array_get')) {
3+
if (!function_exists('array_get')) {
4+
/**
5+
* Get an item from an array using "dot" notation.
6+
*
7+
* @param array $array
8+
* @param string $key
9+
* @param mixed $default
10+
*
11+
* @return mixed
12+
*/
1313
function array_get($array, $key, $default = null)
1414
{
1515
if (is_null($key)) {
@@ -21,7 +21,7 @@ function array_get($array, $key, $default = null)
2121
}
2222

2323
foreach (explode('.', $key) as $segment) {
24-
if (! is_array($array) || ! array_key_exists($segment, $array)) {
24+
if (!is_array($array) || !array_key_exists($segment, $array)) {
2525
return value($default);
2626
}
2727

@@ -32,7 +32,7 @@ function array_get($array, $key, $default = null)
3232
}
3333
}
3434

35-
if (! function_exists('array_has')) {
35+
if (!function_exists('array_has')) {
3636
function array_has($array, $key)
3737
{
3838
if (empty($array) || is_null($key)) {
@@ -44,7 +44,7 @@ function array_has($array, $key)
4444
}
4545

4646
foreach (explode('.', $key) as $segment) {
47-
if (! is_array($array) || ! array_key_exists($segment, $array)) {
47+
if (!is_array($array) || !array_key_exists($segment, $array)) {
4848
return false;
4949
}
5050

@@ -55,16 +55,116 @@ function array_has($array, $key)
5555
}
5656
}
5757

58-
/**
59-
* Return the default value of the given value.
60-
*
61-
* @param mixed $value
62-
*
63-
* @return mixed
64-
*/
65-
if (! function_exists('value')) {
58+
if (!function_exists('value')) {
59+
/**
60+
* Return the default value of the given value.
61+
*
62+
* @param mixed $value
63+
*
64+
* @return mixed
65+
*/
6666
function value($value)
6767
{
6868
return $value instanceof Closure ? $value() : $value;
6969
}
7070
}
71+
72+
if (!function_exists('hex2dec')) {
73+
/**
74+
* @param string $hex
75+
*
76+
* @return string
77+
*/
78+
function hex2dec($hex)
79+
{
80+
$dec = '0';
81+
82+
$len = strlen($hex);
83+
84+
for ($i = 1; $i <= $len; $i++) {
85+
$n = $hex[$i - 1];
86+
if (ctype_xdigit($n)) {
87+
$dec = bcadd($dec, bcmul((string)hexdec($n), bcpow('16', (string)($len - $i))));
88+
}
89+
}
90+
91+
return $dec;
92+
}
93+
}
94+
95+
if (!function_exists('getRootCertSN')) {
96+
/**
97+
* @param string $certPath
98+
*
99+
* @return string|null
100+
*/
101+
function getRootCertSN($certPath)
102+
{
103+
$array = explode('-----END CERTIFICATE-----', file_get_contents($certPath));
104+
105+
$rootSN = null;
106+
107+
foreach ($array as $i) {
108+
$ssl = openssl_x509_parse($i . '-----END CERTIFICATE-----');
109+
110+
if (in_array($ssl['signatureTypeLN'], ['sha1WithRSAEncryption', 'sha256WithRSAEncryption'])) {
111+
$sn = getCertSN($ssl, true);
112+
if (is_null($rootSN)) {
113+
$rootSN = $sn;
114+
} else {
115+
$rootSN .= "_{$sn}";
116+
}
117+
}
118+
}
119+
120+
return $rootSN;
121+
}
122+
}
123+
124+
if (!function_exists('getCertSN')) {
125+
/**
126+
* @param string $cert
127+
* @param bool $parsed
128+
*
129+
* @return string|null
130+
*/
131+
function getCertSN($cert, $parsed = false)
132+
{
133+
if ($parsed) {
134+
$ssl = $cert;
135+
} else {
136+
if (is_file($cert)) {
137+
$cert = file_get_contents($cert);
138+
}
139+
$ssl = openssl_x509_parse($cert);
140+
}
141+
142+
if (strpos($ssl['serialNumber'], '0x') === 0) {
143+
$ssl['serialNumber'] = hex2dec($ssl['serialNumber']);
144+
}
145+
146+
$array = array_reverse($ssl['issuer']);
147+
148+
$names = [];
149+
foreach ($array as $key => $value) {
150+
$names[] = "{$key}={$value}";
151+
}
152+
153+
return md5(implode(',', $names) . $ssl['serialNumber']);
154+
}
155+
}
156+
157+
if (!function_exists('getPublicKey')) {
158+
/**
159+
* @param string $certPath
160+
*
161+
* @return string
162+
*/
163+
function getPublicKey($certPath)
164+
{
165+
$pkey = openssl_pkey_get_public(file_get_contents($certPath));
166+
$keyData = openssl_pkey_get_details($pkey);
167+
$public_key = str_replace('-----BEGIN PUBLIC KEY-----', '', $keyData['key']);
168+
return trim(str_replace('-----END PUBLIC KEY-----', '', $public_key));
169+
}
170+
}

0 commit comments

Comments
 (0)