PEM 인코딩 인증서에서 SSL 인증서 만료 날짜를 확인하는 방법은 무엇입니까?
Mac 또는 Linux에 실제 파일과 Bash 셸이 있는 경우 인증서 파일이 언제 만료되는지 어떻게 쿼리할 수 있습니까?웹 사이트가 아니라 인증서 파일 자체입니다. CSR, 키, 펨 및 체인 파일이 있다고 가정하면 말입니다.
와 함께openssl:
openssl x509 -enddate -noout -in file.pem
출력은 다음과 같은 형식입니다.
notAfter=Nov 3 22:23:50 2014 GMT
또한 위의 날짜를 구문 분석하지 않고 인증서가 만료되었는지 여부 또는 특정 기간 내에 만료되는지 여부를 쉽게 확인하는 방법은 MikeW의 답변을 참조하십시오.
인증서가 만료되었는지(또는 다음 N초 이내에 만료될 예정인지) 여부만 알고 싶다면,-checkend <seconds>에 대한 선택권.openssl x509다음과 같은 정보를 제공합니다.
if openssl x509 -checkend 86400 -noout -in file.pem
then
echo "Certificate is good for another day!"
else
echo "Certificate has expired or will do so within 24 hours!"
echo "(or is invalid/not found)"
fi
이렇게 하면 날짜/시간을 직접 비교해야 하는 번거로움이 줄어듭니다.
openssl의 종료 코드를 반환합니다.0(0)은 인증서가 만료되지 않았고 앞으로 86400초 동안 만료되지 않는 경우입니다. 위의 예와 같습니다.인증서가 만료되었거나 이미 만료된 경우(또는 잘못된/존재하지 않는 파일과 같은 다른 오류) 반환 코드는 다음과 같습니다.1.
(물론 시간/날짜가 올바르게 설정되어 있다고 가정합니다)
이전 버전의 openssl에는 에 지정된 시간을 의미하는 버그가 있습니다.checkend너무 커서 0이 항상 반환됩니다(https://github.com/openssl/openssl/issues/6180) ).
여러 인증서의 만료 순서에 따라 가장 최근에 먼저 만료되는 인증서를 나열하는 bash 명령줄입니다.
for pem in /etc/ssl/certs/*.pem; do
printf '%s: %s\n' \
"$(date --date="$(openssl x509 -enddate -noout -in "$pem"|cut -d= -f 2)" --iso-8601)" \
"$pem"
done | sort
샘플 출력:
2015-12-16: /etc/ssl/certs/Staat_der_Nederlanden_Root_CA.pem
2016-03-22: /etc/ssl/certs/CA_Disig.pem
2016-08-14: /etc/ssl/certs/EBG_Elektronik_Sertifika_Hizmet_S.pem
명령:
# cat {key_name} | openssl x509 -noout -enddate
Example: # cat tower.cert | openssl x509 -noout -enddate
결과:
notAfter=Dec 7 04:03:32 2023 GMT
DNS 라운드 로빈을 사용한다고 가정하고 모든 서버를 확인하는 bash 기능이 있습니다.GNU 날짜가 필요하며 Mac OS에서는 작동하지 않습니다.
function check_certs () {
if [ -z "$1" ]
then
echo "domain name missing"
exit 1
fi
name="$1"
shift
now_epoch=$( date +%s )
dig +noall +answer $name | while read _ _ _ _ ip;
do
echo -n "$ip:"
expiry_date=$( echo | openssl s_client -showcerts -servername $name -connect $ip:443 2>/dev/null | openssl x509 -inform pem -noout -enddate | cut -d "=" -f 2 )
echo -n " $expiry_date";
expiry_epoch=$( date -d "$expiry_date" +%s )
expiry_days="$(( ($expiry_epoch - $now_epoch) / (3600 * 24) ))"
echo " $expiry_days days"
done
}
출력 예:
$ check_certs stackoverflow.com
151.101.1.69: Aug 14 12:00:00 2019 GMT 603 days
151.101.65.69: Aug 14 12:00:00 2019 GMT 603 days
151.101.129.69: Aug 14 12:00:00 2019 GMT 603 days
151.101.193.69: Aug 14 12:00:00 2019 GMT 603 days
승인된 답변과 동일하지만 다음과 같은 경우에도 작동합니다..crt뿐만 아니라 파일로.pem파일, 만약 당신이 찾을 수 없다면..pem파일 위치.
openssl x509 -enddate -noout -in e71c8ea7fa97ad6c.crt
결과:
notAfter=Mar 29 06:15:00 2020 GMT
도메인의 특정 기간이 나중에 만료되는 경우 true/false에 대한 한 줄 검사(예: 15일):
openssl x509 -checkend $(( 24*3600*15 )) -noout -in <(openssl s_client -showcerts -connect my.domain.com:443 </dev/null 2>/dev/null | openssl x509 -outform PEM)
if [ $? -eq 0 ]; then
echo 'good'
else
echo 'bad'
fi
MAC OSX(El Capitan)의 경우니콜라스의 사례를 수정한 것이 저에게 효과가 있었습니다.
for pem in /path/to/certs/*.pem; do
printf '%s: %s\n' \
"$(date -jf "%b %e %H:%M:%S %Y %Z" "$(openssl x509 -enddate -noout -in "$pem"|cut -d= -f 2)" +"%Y-%m-%d")" \
"$pem";
done | sort
샘플 출력:
2014-12-19: /path/to/certs/MDM_Certificate.pem
2015-11-13: /path/to/certs/MDM_AirWatch_Certificate.pem
macOS는 그것을 좋아하지 않았습니다.--date=또는--iso-8601내 시스템의 플래그.
bash 변수로 전환
이 질문은 bash 태그가 붙어 있기 때문에 날짜를 저장하는 데 자주 사용합니다. 이것은 남은 계산 시간에 유용합니다.$EPOCHSECONDS은 " " " " 를 통해 합니다.printf '%(dateFmt)T 바시즘:
{ read -r certStart;read -r certEnd;}< <(date -f <(cut -d = -f 2 <(
openssl x509 -dates -noout -in "$file")) +%s)
그리고나서
printf '%-6s %(%a %d %b %Y, %H %Z)T\n' start $certStart end $certEnd
start Mon 01 Nov 2004, 17 UTC
end Mon 01 Jan 2035, 05 UTC
샘플, 내열의 을 나열/etc/ssl/certs남은 계산 기간(일:
for file in /etc/ssl/certs/*pem;do
{ read -r certStart;read -r certEnd;}< <(
date -f <(cut -d = -f 2 <(
openssl x509 -dates -noout -in "$file")) +%s)
printf "%(%d %b %Y %T)T - %(%d %b %Y %T)T: %6d %s\n" \
$certStart $certEnd $(( (certEnd - EPOCHSECONDS)/86400 )) ${file##*/}
done
05 May 2011 09:37:37 - 31 Dec 2030 09:37:37: 3034 ACCVRAIZ1.pem
26 Oct 2010 08:38:03 - 26 Oct 2040 08:38:03: 6620 Buypass_Class_2_Root_CA.pem
19 Jan 2010 00:00:00 - 18 Jan 2038 23:59:59: 5609 COMODO_RSA_Certification_Authority.pem
13 Nov 2012 00:00:00 - 19 Jan 2038 03:14:07: 5609 GlobalSign_ECC_Root_CA_-_R4.pem
06 Apr 2001 07:29:40 - 06 Apr 2021 07:29:40: -522 Sonera_Class_2_Root_CA.pem
29 Jun 2004 17:39:16 - 29 Jun 2034 17:39:16: 4310 Starfield_Class_2_CA.pem
04 Feb 2016 12:32:16 - 31 Dec 2029 17:23:16: 2669 TrustCor_RootCert_CA-1.pem
01 Nov 2004 17:14:04 - 01 Jan 2035 05:37:19: 4495 XRamp_Global_CA_Root.pem
...
더 완전한 bash x509 판독값:
for file in /etc/ssl/certs/*pem;do
mapfile -t x509 < <(openssl x509 -noout -dates -subject -in "$file")
x509=("${x509[@]#*=}")
mapfile -t dates < <(IFS=$'\n';date -f - <<<"${x509[*]::2}" +%s)
str="${x509[-1]}"
declare -A Subj='([CN]="${file##*/}")'
while [[ "$str" ]] ;do
lhs=${str%%=*} rhs=${str#$lhs= } rhs=${rhs%% = *} rhs=${rhs%, *}
Subj[${lhs// }]="$rhs"
str=${str#"$lhs= $rhs"} str=${str#, }
done
printf "%(%d %b %Y %T)T - %(%d %b %Y %T)T: %s\n" \
${dates[@]} "${Subj[CN]}"
done
05 May 2011 09:37:37 - 31 Dec 2030 09:37:37: 3034 ACCVRAIZ1
26 Oct 2010 08:38:03 - 26 Oct 2040 08:38:03: 6620 Buypass Class 2 Root CA
19 Jan 2010 00:00:00 - 18 Jan 2038 23:59:59: 5609 COMODO RSA Certification Authority
13 Nov 2012 00:00:00 - 19 Jan 2038 03:14:07: 5609 GlobalSign
06 Apr 2001 07:29:40 - 06 Apr 2021 07:29:40: -522 Sonera Class2 CA
29 Jun 2004 17:39:16 - 29 Jun 2034 17:39:16: 4310 Starfield_Class_2_CA.pem
04 Feb 2016 12:32:16 - 31 Dec 2029 17:23:16: 2669 TrustCor RootCert CA-1
01 Nov 2004 17:14:04 - 01 Jan 2035 05:37:19: 4495 XRamp Global Certification Authority
...
참고: 일부 인증서는 제목에 필드가 없습니다.이를 위해 초기화했습니다.$Subj을 CN파일 이름으로: 필에서파이름일:declare -A Subj='([CN]="${file##*/}")'
전체 배시 스크립트
여기에서 전체 bash 스크립트를 공유하고 명령줄 인수의 모든 인증서를 표시합니다. 또는 에 의해 표시될 수 있습니다. 지난 일, 남은 일, 대체 도메인 수 및 모든 Alt를 한 줄(긴)로 표시합니다.
#!/bin/bash
showCert() {
local x509 dates lhs rhs str alts
mapfile -t x509 < <(
openssl x509 -noout -dates -subject -ext subjectAltName -in "$1")
x509=("${x509[@]#*=}")
mapfile -t dates < <(IFS=$'\n';date -f - <<<"${x509[*]::2}" +%s)
str="${x509[2]}"
local -A Subj;Subj[CN]="${file##*/}"
while [[ -n "$str" ]]; do
lhs=${str%%=*} rhs=${str#$lhs= } rhs=${rhs%% = *} rhs=${rhs%, *}
Subj[${lhs// }]="$rhs"
str=${str#"$lhs= $rhs"} str=${str#, }
done
read -ra alts <<<"${x509[4]//,}"
alts=("${alts[@]#*:}")
printf " %(%d %b %Y %H:%M)T %(%d %b %Y %H:%M)T %6d %6d %-30s %3d %s\n" \
"${dates[@]}" $(((dates[1]-EPOCHSECONDS)/86400)) $(((EPOCHSECONDS-
dates[0])/86400)) "${Subj[CN]}" "${#alts[@]}" "${alts[*]}"
}
checkIsIpv4() { # throw an error if not valid IPv4
local _iPointer _i _a _vareq=()
for _i ;do
case $_i in *[^0-9.]* ) return 1 ;; esac
read -ra _a <<<"${_i//./ }"
[ ${#_a[@]} -eq 4 ] || return 1
for _iPointer in "${_a[@]}" ;do
(( _iPointer == ( _iPointer & 255 ) )) || return 2
done
done
}
checkIsLabel() {
((${#1}<4 || ${#1}>253)) && return 1
[[ -z ${1//[a-zA-Z0-9.-]} ]] || return 2
[[ -z ${1//.} ]] && return 3
set -- ${1//./ }
(($#<2 )) && return 4
:
}
printf ' %-17s %-17s %6s %6s %-30s %2s\n' Not\ before Not\ after left \
past Common\ Name Alt
for arg ;do
if [ -f "$arg" ] ;then
showCert "$arg"
elif checkIsLabel "$arg" || checkIsIpv4 "$arg" ;then
showCert <(openssl s_client -ign_eof -connect "$arg:443" \
<<<$'HEAD / HTTP/1.0\r\n\r' 2> /dev/null)
else
echo "Unknown argument: '$arg'."
fi
done
사용 샘플:
./certShow.sh /etc/ssl/certs/ssl-cert-snakeoil.pem www.example.com
Not before Not after left past Common Name Alt
08 Sep 2021 16:49 06 Sep 2031 16:49 3277 372 hostname.local 1 hostname.local
14 Mar 2022 00:00 14 Mar 2023 23:59 179 186 www.example.org 8 www.example.org example.net example.edu example.com example.org www.example.com www.example.edu www.example.net
이유로) 프로그램을 하려면 Linux의 GUI를 합니다.gcr-viewer됩니다.)gcr됨)gcr-viewer))
gcr-viewer file.pem
# or
gcr-viewer file.crt
인증서 만료 여부를 확인하기 위해 동일한 관련 bash 스크립트를 만들었습니다.필요한 경우 동일하게 사용할 수 있습니다.
대본
https://github.com/zeeshanjamal16/usefulScripts/blob/master/sslCertificateExpireCheck.sh
리드미
https://github.com/zeeshanjamal16/usefulScripts/blob/master/README.md
언급URL : https://stackoverflow.com/questions/21297853/how-to-determine-ssl-cert-expiration-date-from-a-pem-encoded-certificate
'programing' 카테고리의 다른 글
| 문자열의 마지막 단어 바꾸기 - C# (0) | 2023.05.09 |
|---|---|
| C#에서 여러 공간을 단일 공간으로 대체하려면 어떻게 해야 합니까? (0) | 2023.05.09 |
| MongoDB: 무조건적인 업데이트? (0) | 2023.05.09 |
| Azure 로그인 자격 증명을 사용하여 FTP를 통해 Azure 웹 사이트에 연결 (0) | 2023.05.09 |
| \r\n, \r 및 \n의 차이는 무엇입니까? (0) | 2023.05.09 |