繁体   English   中英

如何从 PEM 编码证书中确定 SSL 证书到期日期?

[英]How to determine SSL cert expiration date from a PEM encoded certificate?

如果我有实际文件和 Bash shell 在 Mac 或 Linux 中,我如何查询证书文件何时到期? 不是 web 站点,而是证书文件本身,假设我有 csr、密钥、pem 和链文件。

使用openssl

openssl x509 -enddate -noout -in file.pem

输出格式如下:

notAfter=Nov  3 22:23:50 2014 GMT

另请参阅MikeW 的答案,了解如何轻松检查证书是否已过期,或者是否会在特定时间段内过期,而无需解析上面的日期。

如果您只想知道证书是否已过期(或将在接下来的 N 秒内过期), openssl x509-checkend <seconds>选项会告诉您:

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 (零)退出代码,并且在接下来的 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

这是一个 bash 函数,它检查您的所有服务器,假设您使用的是 DNS 循环。 请注意,这需要 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

如果域证书将在一段时间后过期(例如 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

与接受的答案相同,但请注意,它甚至适用于.crt文件,而不仅仅是.pem文件,以防万一您无法找到.pem文件位置。

openssl x509 -enddate -noout -in e71c8ea7fa97ad6c.crt

结果:

notAfter=Mar 29 06:15:00 2020 GMT

对于 MAC OSX (El Capitan) 这种对 Nicholas 示例的修改对我有用。

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标志。

如果(出于某种原因)您想在 Linux 中使用 GUI 应用程序,请使用gcr-viewer (在大多数发行版中,它由gcrgcr (否则在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

进入变量

As this question is tagged , I often use UNIX EPOCH to store dates, this is useful for compute and format output via printf '%(dateFmt)T bashism :

{ read -r certStart;read -r certEnd;}< <(date -f <(cut -d = -f 2 <(
    openssl x509 -{start,end}date -noout -in "$file")) +%s)

然后

printf '%-6s %(%a %d %b %Y, %H %Z)T\n' start $certStart end $certEnd

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM