openssl and pure bash way
Even if Stéphane Chazelas's answer, work fine and is efficient, I would like to post this bash script who will give near same result, but don't use awk
:
#!/bin/bash
exec {sslout}<> <(:)
cnt=1
while read -u $certs line; do
[ "$line" ] && case $line in
*BEGIN*)
exec {ssl}> >(openssl x509 -noout -subject >&${sslout})
echo $line 1>&$ssl
;;
*END*)
echo $line 1>&$ssl
exec {ssl}>&-
read -u $sslout subject
printf "%03d %s\n" $((cnt++)) "${subject#subject=}"
;;
*)
echo $line 1>&$ssl
;;
esac;
done {certs}< /etc/ssl/certs/ca-certificates.crt
exec {certs}>&- {sslout}>&-
One step further
Searching for certs in all dirs mentionned by SHW's answer, sorting by hashes and count
#!/bin/bash
exec {sslout}<> <(:)
cnt=0
hashed=()
while read -u $certs line; do
[ "$line" ] && case $line in
BEGIN)
exec {ssl}> >(openssl x509 -noout -hash -subject >&${sslout})
echo $line 1>&$ssl
;;
END)
echo $line 1>&$ssl
exec {ssl}>&-
read -u $sslout hash
read -u $sslout subject
((cnt++))
hashed[16#$hash]+="${subject#subject=}"$'\t'
;;
*)
echo $line 1>&$ssl
;;
esac
done {certs}< <(find /etc/ssl/certs /usr/{local/,}share/ca-certificates
-type f -exec cat {} +)
exec {certs}>&- {sslout}>&-
echo "$cnt certs read, ${#hashed[@]} different hashes."
for i in ${!hashed[@]};do
IFS=$'\t' read -a subj <<<"${hashed[i]}"
printf "%8x %s\n" $i "$subj"
((${#subj}>1)) && printf " %s\n" "${subj[@]:1}"
done
may output something like:
256 certs read, 128 different hashes.
3179a64 C = NL, O = Staat der Nederlanden, CN = Staat der Nederlanden EV Root CA
C = NL, O = Staat der Nederlanden, CN = Staat der Nederlanden EV Root CA
62cdee6 OU = GlobalSign Root CA - R3, O = GlobalSign, CN = GlobalSign
OU = GlobalSign Root CA - R3, O = GlobalSign, CN = GlobalSign
64e0aa9 C = BM, O = QuoVadis Limited, CN = QuoVadis Root CA 2 G3
C = BM, O = QuoVadis Limited, CN = QuoVadis Root CA 2 G3
...
trust list
from p11-kit package is essentially the same? – Pablo A Mar 27 '19 at 16:55bash
or any shell of the Bourne, rc, or fish families. If put on one line, it would work in any shell that I know, even csh, even the Unix V6 shell. – Stéphane Chazelas Oct 14 '20 at 16:34openssl
exits upon seeing theEND CERTIFICATE
, it would still work even if it didn't as weclose(cmd)
which closes the pipe toopenssl
and waits for the corresponding process to exit upon the nextBEGIN
upon whichopenssl
would see EOF and exit. Try withcmd=wc
instead for instance. – Stéphane Chazelas Jun 29 '22 at 15:27openssl s_client
tip to get the exact list of certs a server is sending is super useful (ran into an issue where a misconfigured server was only sending its leaf cert instead of the full chain. Browsers compensate for that, but most other SSL/TLS client libraries don't) – ncoghlan Jul 11 '22 at 06:18awk
. – x-yuri Sep 01 '22 at 10:33