|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace XmlSigner; |
|
4
|
|
|
|
|
5
|
|
|
use XmlSigner\Exception\CertificateException; |
|
6
|
|
|
|
|
7
|
|
|
class Certificate implements SignerInterface, VerifierInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Read a PFX certificate and return this class |
|
11
|
|
|
* @param string $content |
|
12
|
|
|
* @param string $password |
|
13
|
|
|
* @return Certificate |
|
14
|
|
|
* @throws CertificateException |
|
15
|
|
|
*/ |
|
16
|
5 |
|
public static function readPfx($content, $password) |
|
17
|
|
|
{ |
|
18
|
5 |
|
$certs = []; |
|
19
|
5 |
|
if (!openssl_pkcs12_read($content, $certs, $password)) { |
|
20
|
1 |
|
throw CertificateException::unableToRead(); |
|
21
|
|
|
} |
|
22
|
4 |
|
$chain = ''; |
|
23
|
4 |
|
if (!empty($certs['extracerts'])) { |
|
24
|
4 |
|
foreach ($certs['extracerts'] as $ec) { |
|
25
|
4 |
|
$chain .= $ec; |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
4 |
|
return new Certificate( |
|
29
|
4 |
|
new PrivateKey($certs['pkey']), |
|
30
|
4 |
|
new PublicKey($certs['cert']), |
|
31
|
4 |
|
$chain |
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
private $privateKey; |
|
36
|
|
|
private $publicKey; |
|
37
|
|
|
private $chainKeysString; |
|
38
|
|
|
|
|
39
|
4 |
|
public function __construct( |
|
40
|
|
|
PrivateKey $privateKey, |
|
41
|
|
|
PublicKey $publicKey, |
|
42
|
|
|
$chainKeysString = null |
|
43
|
|
|
) { |
|
44
|
4 |
|
$this->privateKey = $privateKey; |
|
45
|
4 |
|
$this->publicKey = $publicKey; |
|
46
|
4 |
|
$this->chainKeysString = $chainKeysString; |
|
47
|
4 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
2 |
|
public function sign($content, $algorithm = OPENSSL_ALGO_SHA1) |
|
53
|
|
|
{ |
|
54
|
2 |
|
return $this->privateKey->sign($content, $algorithm); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function verify($data, $signature, $algorithm = OPENSSL_ALGO_SHA1) |
|
61
|
|
|
{ |
|
62
|
1 |
|
return $this->publicKey->verify($data, $signature, $algorithm); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
public function publicKey() : string |
|
66
|
|
|
{ |
|
67
|
1 |
|
return $this->publicKey->unformated(); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|