我有一个证书捆绑包.crt文件。
做openssl x509 -in bundle.crt -text -noout
仅显示根证书。
我如何查看所有其他证书?
http://comments.gmane.org/gmane.comp.encryption.openssl.user/43587 暗示了这一点:
openssl crl2pkcs7 -nocrl -certfile CHAINED.pem | openssl pkcs7 -print_certs -text -noout
这确实对我有用,但是我不了解细节,因此无法说是否有任何警告。
Java的keytool
可以达到目的:
_keytool -printcert -v -file <certs.crt>
_
Annotation: Windows双击不起作用。 Windows仅读取密钥库中的第一个证书,并自动从其内置的证书库扩展信任链。
结果:
.crt
_文件中除第一个证书之外的所有内容都不会显示.crt
_文件中显示的信任链不同。这可能会导致错误的结论。以下 此FAQ 导致我进入 此Perl脚本 ,这很强烈地向我暗示openssl
不具有本机支持。 ñ日 证书捆绑在一起,我们必须使用某种工具将输入切成小块,然后再将每个证书提供给openssl
。这个Perl脚本是根据上面链接的Nick Burch的脚本自由改编的,似乎可以做到:
#!/usr/bin/Perl
# script for splitting multi-cert input into individual certs
# Artistic Licence
#
# v0.0.1 Nick Burch <[email protected]>
# v0.0.2 Tom Yates <[email protected]>
#
$filename = shift;
unless($filename) {
die("You must specify a cert file.\n");
}
open INP, "<$filename" or die("Unable to load \"$filename\"\n");
$thisfile = "";
while(<INP>) {
$thisfile .= $_;
if($_ =~ /^\-+END(\s\w+)?\sCERTIFICATE\-+$/) {
print "Found a complete certificate:\n";
print `echo \'$thisfile\' | openssl x509 -noout -text`;
$thisfile = "";
}
}
close INP;
Oneliner显示文件中每个证书的摘要。
openssl crl2pkcs7 -nocrl -certfile CHAINED.pem | openssl pkcs7 -print_certs -noout
(在其他答案中提到了类似的突击队,但是没有--text选项,这会产生较短的输出)。
例:
$ openssl crl2pkcs7 -nocrl -certfile bundled.crt | openssl pkcs7 -print_certs -noout
subject=/C=NL/postalCode=5705 CN/L=City/street=Example 20/O=Foobar B.V./OU=ICT/OU=Wildcard SSL/CN=*.example.com
issuer=/C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=COMODO RSA Organization Validation Secure Server CA
subject=/C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=COMODO RSA Organization Validation Secure Server CA
issuer=/C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=COMODO RSA Certification Authority
subject=/C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=COMODO RSA Certification Authority
issuer=/C=SE/O=AddTrust AB/OU=AddTrust External TTP Network/CN=AddTrust External CA Roo
这可能并不漂亮,也不是优雅,但是它很快并且对我有用,在Linux上使用bash以及ca-cert捆绑文件中的PEM格式化块。
while read line
do
if [ "${line//END}" != "$line" ]; then
txt="$txt$line\n"
printf -- "$txt" | openssl x509 -subject -issuer -noout
txt=""
else
txt="$txt$line\n"
fi
done < /path/to/bundle/file
您可以将所有内容放到一行,然后调整openssl选项以适合。我真的希望有一个更优雅的解决方案,但是在这种情况下,我认为找到一种更优雅的解决方案要比破解不雅致的解决方案花费更多的时间。
由于没有基于awk的解决方案:
$ cat ca-bundle | awk '/BEGIN/ { i++; } /BEGIN/, /END/ { print > i ".extracted.crt" }'
$ ls *.extracted.crt | while read cert; do openssl x509 -in $cert -text -noout; done
第一个命令通过查找BEGIN和END行将包分成证书。第二个命令循环遍历提取的证书并显示它们。
在bash中,通常只需要一行(长)代码:-)
tfile=$( mktemp -u ) && \
csplit -z -q -f "$tfile" bundle.crt '/----BEGIN CERTIFICATE-----/' '{*}' && \
find "${tfile%/*}" -name "${tfile##*/}*" -exec openssl x509 -noout -subject -in "{}" \; -delete
我想在这里输入惯用的Perl命令行:
Perl -ne "\$n++ if /BEGIN/; print if \$n == 1;" mysite.pem
如果有文字,则稍作调整:
Perl -ne "\$n++ if /^-----BEGIN CERTIFICATE-----\$/; print if \$n == 3 && /^-----BEGIN CERTIFICATE-----\$/.../^-----END CERTIFICATE-----\$/;" mysite.pem
只需更改第二条语句中n的值即可获得第n个证书。
对MadHatter帖子的小改动,使您可以直接复制/粘贴到CLI。我还包括了MD5哈希,这在确保证书正确时非常有用。返回的标准输入行是证书的md5哈希值。
Perl -e 'my $thisfile = "";
foreach (<>) {
$thisfile .= $_;
if($_ =~ /^\-+END(\s\w+)?\sCERTIFICATE\-+$/) {
print "Found a complete certificate:\n";
print `echo "$thisfile" | openssl x509 -noout -text`;
print `echo "$thisfile" | openssl x509 -noout -modulus | openssl md5`;
$thisfile = "";
}
}' < my_id_cert_and_ca_bundle.crt
如果您想看到尼斯简短的输出,请使用此版本。如果仅检查您是否已包含所有证书,而未真正检查证书的使用情况等,则将很有帮助。
Perl -e 'my $thisfile = "";
foreach (<>) {
$thisfile .= $_;
if($_ =~ /^\-+END(\s\w+)?\sCERTIFICATE\-+$/) {
print "Found a complete certificate:\n";
print `echo "$thisfile" | openssl x509 -noout -serial -subject -dates -alias -issuer`;
print `echo "$thisfile" | openssl x509 -noout -modulus | openssl md5` . "\n";
$thisfile = "";
}
}' < my_id_cert_and_ca_bundle.crt
万一您的openssl版本不支持所有这些标志,这里可以使用一些egrep。与第一个相同,只是通过管道传递给egrep。
Perl -e '.....
' < my_id_cert_and_ca_bundle.crt | egrep "Serial|Subject:|Not |Public-Key|^Cert|stdin|ssuer"
要检查私钥的MD5哈希,您可以执行以下操作。
openssl rsa -noout -modulus -in privateKey.key | openssl md5
参考: SSL购物者-证书密钥匹配者
这是一个基于awk的解决方案,它不依赖中间文件。
cat bundle.crt | awk '{
if ($0 == "-----BEGIN CERTIFICATE-----") cert=""
else if ($0 == "-----END CERTIFICATE-----") print cert
else cert=cert$0
}' | while read CERT; do
echo "$CERT" | base64 -d | openssl x509 -inform DER -text -noout
done
它通过从stdin读取PEM块并将每个块连接到单个base64编码行来工作。然后读取,解码行并将其作为DER编码证书传递给openssl。