1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LiquidWeb\SslCertificate; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use phpseclib\Math\BigInteger; |
7
|
|
|
|
8
|
|
|
class SslChain |
9
|
|
|
{ |
10
|
|
|
/** @var string */ |
11
|
|
|
protected $name; |
12
|
|
|
|
13
|
|
|
/** @var array */ |
14
|
|
|
protected $subject; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $hash; |
18
|
|
|
|
19
|
|
|
/** @var array */ |
20
|
|
|
protected $issuer; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $version; |
24
|
|
|
|
25
|
|
|
/** @var BigInteger */ |
26
|
|
|
protected $serial; |
27
|
|
|
|
28
|
|
|
/** @var Carbon */ |
29
|
|
|
protected $validFrom; |
30
|
|
|
|
31
|
|
|
/** @var Carbon */ |
32
|
|
|
protected $validTo; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
protected $signatureType; |
36
|
|
|
|
37
|
|
|
private static function setValidFromDate($utcInput): Carbon |
38
|
|
|
{ |
39
|
|
|
return Carbon::createFromTimestampUTC($utcInput); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private static function setValidToDate($utcInput): Carbon |
43
|
|
|
{ |
44
|
|
|
return Carbon::createFromTimestampUTC($utcInput); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function __construct(array $chainInput) |
48
|
|
|
{ |
49
|
|
|
$this->name = $chainInput['name']; |
50
|
|
|
$this->subject = $chainInput['subject']; |
51
|
|
|
$this->hash = $chainInput['hash']; |
52
|
|
|
$this->issuer = $chainInput['issuer']; |
53
|
|
|
$this->version = $chainInput['version']; |
54
|
|
|
$this->serial = new BigInteger($chainInput['serialNumber']); |
55
|
|
|
$this->validFrom = self::setValidFromDate($chainInput['validFrom_time_t']); |
56
|
|
|
$this->validTo = self::setValidToDate($chainInput['validTo_time_t']); |
57
|
|
|
$this->signatureType = $chainInput['signatureTypeSN']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getLocationName(): string |
61
|
|
|
{ |
62
|
|
|
return $this->subject['C'] ?? ''; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getOrganizationName(): string |
66
|
|
|
{ |
67
|
|
|
return $this->subject['O'] ?? ''; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getOrganizationUnitName(): string |
71
|
|
|
{ |
72
|
|
|
return $this->subject['OU'] ?? ''; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getCommonName(): string |
76
|
|
|
{ |
77
|
|
|
return $this->subject['CN'] ?? ''; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getHash(): string |
81
|
|
|
{ |
82
|
|
|
return $this->hash; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getIssuerLocationName(): string |
86
|
|
|
{ |
87
|
|
|
return $this->issuer['C'] ?? ''; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getIssuerOrganizationName(): string |
91
|
|
|
{ |
92
|
|
|
return $this->issuer['O'] ?? ''; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getIssuerOrganizationUnitName(): string |
96
|
|
|
{ |
97
|
|
|
if (isset($this->issuer['OU'])) { |
98
|
|
|
if (is_array($this->issuer['OU'])) { |
99
|
|
|
return $this->issuer['OU'][0] ?? ''; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this->issuer['OU'] ?? ''; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getIssuerCommonName(): string |
107
|
|
|
{ |
108
|
|
|
return $this->issuer['CN'] ?? ''; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getSerialNumber(): string |
112
|
|
|
{ |
113
|
|
|
return strtoupper($this->serial->toHex()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function validFromDate(): Carbon |
117
|
|
|
{ |
118
|
|
|
return $this->validFrom; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function expirationDate(): Carbon |
122
|
|
|
{ |
123
|
|
|
return $this->validTo; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getSignatureAlgorithm(): string |
127
|
|
|
{ |
128
|
|
|
return $this->signatureType; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function isExpired(): bool |
132
|
|
|
{ |
133
|
|
|
return $this->expirationDate()->isPast(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function isValid() |
137
|
|
|
{ |
138
|
|
|
if (! Carbon::now()->between($this->validFromDate(), $this->expirationDate())) { |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return true; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function isValidUntil(Carbon $carbon): bool |
146
|
|
|
{ |
147
|
|
|
if ($this->expirationDate()->gt($carbon)) { |
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->isValid(); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|