keytool在单个文件中导入多个证书

Fak*_*een 14 java ssl-certificate

如何使用keytool [到cert store]在单个文件中导入多个证书?

keytool -importcert只导入第一个.

cmc*_*nty 11

一个bash脚本,它将从PEM文件导入所有证书:

#!/bin/bash
PEM_FILE=$1
PASSWORD=$2
KEYSTORE=$3
# number of certs in the PEM file
CERTS=$(grep 'END CERTIFICATE' $PEM_FILE| wc -l)

# For every cert in the PEM file, extract it and import into the JKS keystore
# awk command: step 1, if line is in the desired cert, print the line
#              step 2, increment counter when last line of cert is found
for N in $(seq 0 $(($CERTS - 1))); do
  ALIAS="${PEM_FILE%.*}-$N"
  cat $PEM_FILE |
    awk "n==$N { print }; /END CERTIFICATE/ { n++ }" |
    keytool -noprompt -import -trustcacerts \
            -alias $ALIAS -keystore $KEYSTORE -storepass $PASSWORD
done
Run Code Online (Sandbox Code Playgroud)

例如:

./jks_import_pem TrustedCAs.PEM changeit truststore.jks
Run Code Online (Sandbox Code Playgroud)


bow*_*ore 8

如果要包含CA证书,则应添加该-trustcacerts选项.

如果一个PEM文件中有多个证书链,则必须拆分该文件.