1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NFePHP\Common; |
4
|
|
|
|
5
|
|
|
use NFePHP\Common\Exception\CertificateException; |
6
|
|
|
|
7
|
|
|
class Certificate |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
public $companyName; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
public $privateKey; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
public $publicKey; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \DateTime |
26
|
|
|
*/ |
27
|
|
|
public $validFrom; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \DateTime |
31
|
|
|
*/ |
32
|
|
|
public $validTo; |
33
|
|
|
|
34
|
1 |
|
public function __construct($content, $password = '') |
35
|
|
|
{ |
36
|
1 |
|
$this->read($content, $password); |
37
|
1 |
|
$this->load(); |
38
|
1 |
|
} |
39
|
|
|
|
40
|
1 |
|
private function read($content, $password) |
41
|
|
|
{ |
42
|
1 |
|
$certs = []; |
43
|
1 |
|
if (!openssl_pkcs12_read($content, $certs, $password)) { |
44
|
|
|
throw CertificateException::unableToRead(); |
45
|
|
|
} |
46
|
1 |
|
$this->privateKey = $certs['pkey']; |
47
|
1 |
|
$this->publicKey = $certs['cert']; |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Load info from certificate |
52
|
|
|
* @throws CertificateException |
53
|
|
|
*/ |
54
|
1 |
|
private function load() |
55
|
|
|
{ |
56
|
1 |
|
if (!$resource = openssl_x509_read($this->publicKey)) { |
57
|
|
|
throw CertificateException::unableToOpen(); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$detail = openssl_x509_parse($resource, false); |
61
|
1 |
|
$this->companyName = $detail['subject']['commonName']; |
62
|
1 |
|
$this->validFrom = \DateTime::createFromFormat('ymdHis\Z', $detail['validFrom']); |
|
|
|
|
63
|
1 |
|
$this->validTo = \DateTime::createFromFormat('ymdHis\Z', $detail['validTo']); |
|
|
|
|
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Check if certificate has been expired. |
68
|
|
|
* @return bool Returns true when it is truth, otherwise false. |
69
|
|
|
*/ |
70
|
1 |
|
public function isExpired() |
71
|
|
|
{ |
72
|
1 |
|
$now = new \DateTime('now'); |
73
|
1 |
|
return $this->validFrom <= $now && $this->validTo >= $now; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public function sign($content, $algorithm = OPENSSL_ALGO_SHA1) |
77
|
|
|
{ |
78
|
1 |
|
if (!$privateResource = openssl_pkey_get_private($this->privateKey)) { |
79
|
|
|
throw CertificateException::getPrivateKey(); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
$encryptedData = ''; |
83
|
1 |
|
if (!openssl_sign($content, $encryptedData, $privateResource, $algorithm)) { |
84
|
|
|
throw CertificateException::signContent(); |
85
|
|
|
} |
86
|
1 |
|
return $encryptedData; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.