|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\SslCertificate; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
|
|
7
|
|
|
class SslCertificate |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var array */ |
|
10
|
|
|
protected $rawCertificateFields = []; |
|
11
|
|
|
|
|
12
|
|
|
public static function download(): Downloader |
|
13
|
|
|
{ |
|
14
|
|
|
return new Downloader(); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public static function createForHostName(string $url, int $timeout = 30): SslCertificate |
|
18
|
|
|
{ |
|
19
|
|
|
$sslCertificate = Downloader::downloadCertificateFromUrl($url, $timeout); |
|
20
|
|
|
|
|
21
|
|
|
return $sslCertificate; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(array $rawCertificateFields) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->rawCertificateFields = $rawCertificateFields; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function getRawCertificateFields(): array |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->rawCertificateFields; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getIssuer(): string |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->rawCertificateFields['issuer']['CN']; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getDomain(): string |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->rawCertificateFields['subject']['CN'] ?? ''; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getSignatureAlgorithm(): string |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->rawCertificateFields['signatureTypeSN'] ?? ''; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getAdditionalDomains(): array |
|
50
|
|
|
{ |
|
51
|
|
|
$additionalDomains = explode(', ', $this->rawCertificateFields['extensions']['subjectAltName'] ?? ''); |
|
52
|
|
|
|
|
53
|
|
|
return array_map(function (string $domain) { |
|
54
|
|
|
return str_replace('DNS:', '', $domain); |
|
55
|
|
|
}, $additionalDomains); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function validFromDate(): Carbon |
|
59
|
|
|
{ |
|
60
|
|
|
return Carbon::createFromTimestampUTC($this->rawCertificateFields['validFrom_time_t']); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function expirationDate(): Carbon |
|
64
|
|
|
{ |
|
65
|
|
|
return Carbon::createFromTimestampUTC($this->rawCertificateFields['validTo_time_t']); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function isExpired(): bool |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->expirationDate()->isPast(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function isValid(string $url = null) |
|
74
|
|
|
{ |
|
75
|
|
|
if (! Carbon::now()->between($this->validFromDate(), $this->expirationDate())) { |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if (! empty($url)) { |
|
80
|
|
|
return $this->appliesToUrl($url ?? $this->getDomain()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function isValidUntil(Carbon $carbon, string $url = null): bool |
|
87
|
|
|
{ |
|
88
|
|
|
if ($this->expirationDate()->lte($carbon)) { |
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $this->isValid($url); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function appliesToUrl(string $url): bool |
|
96
|
|
|
{ |
|
97
|
|
|
$host = (new Url($url))->getHostName(); |
|
98
|
|
|
|
|
99
|
|
|
$certificateHosts = array_merge([$this->getDomain()], $this->getAdditionalDomains()); |
|
100
|
|
|
|
|
101
|
|
|
foreach ($certificateHosts as $certificateHost) { |
|
102
|
|
|
if ($host === $certificateHost) { |
|
103
|
|
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if ($this->wildcardHostCoversHost($certificateHost, $host)) { |
|
107
|
|
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return false; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
protected function wildcardHostCoversHost(string $wildcardHost, string $host): bool |
|
115
|
|
|
{ |
|
116
|
|
|
if ($host === $wildcardHost) { |
|
117
|
|
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if (! starts_with($wildcardHost, '*')) { |
|
121
|
|
|
return false; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$wildcardHostWithoutWildcard = substr($wildcardHost, 2); |
|
125
|
|
|
|
|
126
|
|
|
return substr_count($wildcardHost, '.') >= substr_count($host, '.') && ends_with($host, $wildcardHostWithoutWildcard); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function getRawCertificateFieldsJson(): string |
|
130
|
|
|
{ |
|
131
|
|
|
return json_encode($this->getRawCertificateFields()); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function getHash(): string |
|
135
|
|
|
{ |
|
136
|
|
|
return md5($this->getRawCertificateFieldsJson()); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function __toString(): string |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->getRawCertificateFieldsJson(); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|