1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Punkstar\Ssl; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Punkstar\Ssl\Parser\SanParser; |
7
|
|
|
|
8
|
|
|
class Certificate |
9
|
|
|
{ |
10
|
|
|
protected $rawCert; |
11
|
|
|
protected $certData; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var SanParser |
15
|
|
|
*/ |
16
|
|
|
protected $sanParser; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Certificate constructor. |
20
|
|
|
* |
21
|
|
|
* @param string $certificate |
22
|
|
|
* @param SanParser $sanParser |
23
|
|
|
*/ |
24
|
13 |
|
public function __construct($certificate, SanParser $sanParser = null) |
25
|
|
|
{ |
26
|
13 |
|
if ($sanParser === null) { |
27
|
13 |
|
$sanParser = new SanParser(); |
28
|
|
|
} |
29
|
|
|
|
30
|
13 |
|
$this->sanParser = $sanParser; |
31
|
|
|
|
32
|
13 |
|
$this->rawCert = $certificate; |
33
|
13 |
|
$this->certData = $this->extractCertData($certificate); |
34
|
12 |
|
$this->sanParser = $sanParser; |
35
|
12 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return DateTime |
39
|
|
|
*/ |
40
|
4 |
|
public function validFrom(): \DateTime |
41
|
|
|
{ |
42
|
4 |
|
$date = new DateTime(); |
43
|
4 |
|
$date->setTimestamp($this->certData['validFrom_time_t']); |
44
|
4 |
|
return $date; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return DateTime |
49
|
|
|
*/ |
50
|
4 |
|
public function validTo(): \DateTime |
51
|
|
|
{ |
52
|
4 |
|
$date = new DateTime(); |
53
|
4 |
|
$date->setTimestamp($this->certData['validTo_time_t']); |
54
|
4 |
|
return $date; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
4 |
|
public function certName(): string |
61
|
|
|
{ |
62
|
4 |
|
return $this->certData['name']; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
7 |
|
public function subject(): array |
69
|
|
|
{ |
70
|
7 |
|
return $this->certData['subject']; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
4 |
|
public function issuer(): array |
77
|
|
|
{ |
78
|
4 |
|
return $this->certData['issuer']; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
7 |
|
public function sans(): array |
85
|
|
|
{ |
86
|
7 |
|
return $this->sanParser->parse($this->certData['extensions']['subjectAltName']); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
4 |
|
public function signatureAlgorithm(): string |
93
|
|
|
{ |
94
|
4 |
|
return $this->certData['signatureTypeSN']; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
8 |
|
public function toString(): string |
101
|
|
|
{ |
102
|
8 |
|
return $this->rawCert; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
8 |
|
public function __toString() |
109
|
|
|
{ |
110
|
8 |
|
return $this->toString(); |
111
|
|
|
} |
112
|
|
|
|
113
|
13 |
|
protected function extractCertData($certificate): array |
114
|
|
|
{ |
115
|
13 |
|
$parsedData = openssl_x509_parse($certificate); |
116
|
|
|
|
117
|
13 |
|
if ($parsedData === false) { |
118
|
1 |
|
throw new Exception("Unable to extract data from certificate.", Exception::MALFORMED_CERTIFICATE); |
119
|
|
|
} |
120
|
|
|
|
121
|
12 |
|
return $parsedData; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|