|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace whm\Smoke\Rules\Http; |
|
4
|
|
|
|
|
5
|
|
|
use whm\Smoke\Http\Response; |
|
6
|
|
|
use whm\Smoke\Rules\Rule; |
|
7
|
|
|
use whm\Smoke\Rules\ValidationFailedException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* This rule checks if a given https certificate expire in a few days. |
|
11
|
|
|
*/ |
|
12
|
|
|
class HttpsCertificateExpireRule implements Rule |
|
13
|
|
|
{ |
|
14
|
|
|
private $expireWarningTime; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param int $expireWarningTime in days |
|
18
|
|
|
*/ |
|
19
|
|
|
public function init($expireWarningTime = 14) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->expireWarningTime = $expireWarningTime; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function validate(Response $response) |
|
25
|
|
|
{ |
|
26
|
|
|
if ('https' === $response->getUri()->getScheme()) { |
|
27
|
|
|
$sslOptions = stream_context_create(array('ssl' => array('capture_peer_cert' => true))); |
|
28
|
|
|
|
|
29
|
|
|
$request = stream_socket_client( |
|
30
|
|
|
'ssl://' . $response->getUri()->getHost() . ':443', |
|
31
|
|
|
$errno, |
|
32
|
|
|
$errstr, |
|
33
|
|
|
30, |
|
34
|
|
|
STREAM_CLIENT_CONNECT, |
|
35
|
|
|
$sslOptions |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
$content = stream_context_get_params($request); |
|
39
|
|
|
$certinfo = openssl_x509_parse($content['options']['ssl']['peer_certificate']); |
|
40
|
|
|
|
|
41
|
|
|
$validFrom = date('d.m.Y H:i:s', $certinfo['validFrom_time_t']); |
|
42
|
|
|
$validTo = date('d.m.Y H:i:s', $certinfo['validTo_time_t']); |
|
43
|
|
|
|
|
44
|
|
|
if ($certinfo['validFrom_time_t'] > time() || $certinfo['validTo_time_t'] < time()) { |
|
45
|
|
|
$errorMessage = 'Certificate is expired. [' . $validFrom . ' - ' . $validTo . ']'; |
|
46
|
|
|
throw new ValidationFailedException($errorMessage); |
|
47
|
|
|
} elseif ($certinfo['validTo_time_t'] < strtotime('+' . $this->expireWarningTime . 'days')) { |
|
48
|
|
|
$errorMessage = 'Certificate warning, expires in lower than ' . $this->expireWarningTime . ' days. Certificate expires at: ' . $validTo; |
|
49
|
|
|
throw new ValidationFailedException($errorMessage); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|