|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NFePHP\Common\Certificate; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class for management and use of digital certificates A1 (PKCS # 12) |
|
7
|
|
|
* @category NFePHP |
|
8
|
|
|
* @package NFePHP\Common\ProvateKey |
|
9
|
|
|
* @copyright Copyright (c) 2008-2016 |
|
10
|
|
|
* @license http://www.gnu.org/licenses/lesser.html LGPL v3 |
|
11
|
|
|
* @author Antonio Spinelli <tonicospinelli85 at gmail dot com> |
|
12
|
|
|
* @link http://github.com/nfephp-org/sped-common for the canonical source repository |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
use NFePHP\Common\Exception\CertificateException; |
|
16
|
|
|
|
|
17
|
|
|
class PrivateKey implements SignatureInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $rawKey; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var resource |
|
26
|
|
|
*/ |
|
27
|
|
|
private $resource; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
private $details = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* PublicKey constructor. |
|
36
|
|
|
* @param string $privateKey Content of private key file |
|
37
|
|
|
*/ |
|
38
|
10 |
|
public function __construct($privateKey) |
|
39
|
|
|
{ |
|
40
|
10 |
|
$this->rawKey = $privateKey; |
|
41
|
10 |
|
$this->read(); |
|
42
|
10 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get a private key |
|
46
|
|
|
* @link http://php.net/manual/en/function.openssl-pkey-get-private.php |
|
47
|
|
|
* @return void |
|
48
|
|
|
* @throws CertificateException An error has occurred when get private key |
|
49
|
|
|
*/ |
|
50
|
10 |
|
protected function read() |
|
51
|
|
|
{ |
|
52
|
10 |
|
if (!$resource = openssl_pkey_get_private($this->rawKey)) { |
|
53
|
|
|
throw CertificateException::getPrivateKey(); |
|
54
|
|
|
} |
|
55
|
10 |
|
$this->details = openssl_pkey_get_details($resource); |
|
56
|
10 |
|
$this->resource = $resource; |
|
57
|
10 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
5 |
|
public function sign($content, $algorithm = OPENSSL_ALGO_SHA1) |
|
63
|
|
|
{ |
|
64
|
5 |
|
$encryptedData = ''; |
|
65
|
5 |
|
if (!openssl_sign($content, $encryptedData, $this->resource, $algorithm)) { |
|
66
|
|
|
throw CertificateException::signContent(); |
|
67
|
|
|
} |
|
68
|
5 |
|
return $encryptedData; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Return the modulus of private key |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
public function modulus() |
|
76
|
|
|
{ |
|
77
|
|
|
if (empty($this->details['n'])) { |
|
78
|
|
|
return ''; |
|
79
|
|
|
} |
|
80
|
|
|
return base64_encode($this->details['n']); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Return the expoent of private key |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function expoent() |
|
88
|
|
|
{ |
|
89
|
|
|
if (empty($this->details['e'])) { |
|
90
|
|
|
return ''; |
|
91
|
|
|
} |
|
92
|
|
|
return base64_encode($this->details['e']); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Return raw private key |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function __toString() |
|
100
|
|
|
{ |
|
101
|
1 |
|
return $this->rawKey; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|