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 createForHostName(string $url, int $timeout = 30): SslCertificate |
13
|
|
|
{ |
14
|
|
|
$rawCertificateFields = Downloader::downloadCertificateFromUrl($url, $timeout); |
15
|
|
|
|
16
|
|
|
return new static($rawCertificateFields); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function __construct(array $rawCertificateFields) |
20
|
|
|
{ |
21
|
|
|
$this->rawCertificateFields = $rawCertificateFields; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function getRawCertificateFields(): array |
25
|
|
|
{ |
26
|
|
|
return $this->rawCertificateFields; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getIssuer(): string |
30
|
|
|
{ |
31
|
|
|
return $this->rawCertificateFields['issuer']['CN']; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getDomain(): string |
35
|
|
|
{ |
36
|
|
|
return $this->rawCertificateFields['subject']['CN'] ?? ''; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getAdditionalDomains(): array |
40
|
|
|
{ |
41
|
|
|
$additionalDomains = explode(', ', $this->rawCertificateFields['extensions']['subjectAltName'] ?? ''); |
42
|
|
|
|
43
|
|
|
return array_map(function (string $domain) { |
44
|
|
|
return str_replace('DNS:', '', $domain); |
45
|
|
|
}, $additionalDomains); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function validFromDate(): Carbon |
49
|
|
|
{ |
50
|
|
|
return Carbon::createFromTimestampUTC($this->rawCertificateFields['validFrom_time_t']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function expirationDate(): Carbon |
54
|
|
|
{ |
55
|
|
|
return Carbon::createFromTimestampUTC($this->rawCertificateFields['validTo_time_t']); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function isExpired(): bool |
59
|
|
|
{ |
60
|
|
|
return $this->expirationDate()->isPast(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function isValid(string $url = null) |
64
|
|
|
{ |
65
|
|
|
if (!Carbon::now()->between($this->validFromDate(), $this->expirationDate())) { |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (!empty($url)) { |
70
|
|
|
return $this->appliesToUrl($url ?? $this->getDomain()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function appliesToUrl(string $url): bool |
77
|
|
|
{ |
78
|
|
|
$host = (new Url($url))->getHostName(); |
79
|
|
|
|
80
|
|
|
$certificateHosts = array_merge([$this->getDomain()], $this->getAdditionalDomains()); |
81
|
|
|
|
82
|
|
|
foreach ($certificateHosts as $certificateHost) { |
83
|
|
|
if ($host === $certificateHost) { |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($this->wildcardHostCoversHost($certificateHost, $host)) { |
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function wildcardHostCoversHost(string $wildcardHost, string $host): bool |
96
|
|
|
{ |
97
|
|
|
if ($host == $wildcardHost) { |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!starts_with($wildcardHost, '*')) { |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$wildcardHostWithoutWildcard = substr($wildcardHost, 2); |
106
|
|
|
|
107
|
|
|
return ends_with($host, $wildcardHostWithoutWildcard); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|