php
PHP - SSL 만료일 검사기 구현
PHP 로 SSL 만료일 및 지원 여부를 구현할 수 있습니다. 사용된 함수는 아래와 같습니다.
- stream_context_create()
- stream_socket_client()
- stream_context_get_params()
- openssl_x509_parse()
<?php
/*
SSL 만료일 검사기
*/
function getCertDetails($domain)
{
$domain = strtolower($domain);
// 도메인이 유효하지 않으면 code '1'를 반환
if(!preg_match("/(^[a-z0-9\-\.]{2,25}\.[a-z]{2,12})$/", $domain))
{
return array(
'code' => 1,
'error' => 'This is not a valid domain.'
);
}
$url = "https://" . $domain;
$orignalParse = parse_url($url, PHP_URL_HOST);
$get = stream_context_create(
array(
'ssl' => array(
'capture_peer_cert' => true,
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
)
);
$read = stream_socket_client(
"ssl://" . $orignalParse . ":443", $errno, $errstr, 10, STREAM_CLIENT_CONNECT, $get
);
$cert = @stream_context_get_params($read);
$certInfo = @openssl_x509_parse($cert['options']['ssl']['peer_certificate']);
// SSL을 지원하지 않으면 code '2'를 반환
if(empty($certInfo))
{
return array(
'code' => 2,
'error' => 'ssl is not supported.'
);
}
$certExpiry=strtotime(date('Y-m-d', $certInfo['validTo_time_t'])." 00:00:00");
$now=strtotime(date('Y-m-d', time())." 00:00:00");
$certInfo['validLeft']=floor(($certExpiry-$now)/86400);
$dns_info = array();
$mdns = explode(',', $certInfo['extensions']['subjectAltName']);
foreach($mdns as $w)
{
$dns_info[] = substr(trim($w), 4);
}
$certInfo['domain'] = $domain;
$certInfo['alias'] = $dns_info;
return $certInfo;
}
$get = getCertDetails('www.habonyphp.com');
if($get['code'] === 1)
echo '도메인 이름이 유효하지 않습니다.';
else if($get['code'] === 2)
echo 'SSL을 지원하지 않습니다.';
else
{
$str = [
// 도메인
'domain' => $get['domain'],
// SSL 만료일
'expiry' => date("Y-m-d", $get['validTo_time_t']),
// SSL 남은 일수
'daysLeft' => $get['validLeft'],
// SSL 상표명
'issuer' => $get['issuer']['O'],
// DNS 정보
'dns' => $get['alias']
];
}
print_r($str);
/*
결과:
Array
(
[domain] => www.habonyphp.com
[expiry] => 2022-01-23
[daysLeft] => 58
[issuer] => Google Trust Services LLC
[dns] => Array
(
[0] => www.habonyphp.com
)
)
*/
?>
0 댓글