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