Skip to content

Instantly share code, notes, and snippets.

@daurrutia
Created February 15, 2023 03:24
Show Gist options
  • Save daurrutia/8bc25ade7152f6a08d295008dd90bb74 to your computer and use it in GitHub Desktop.
Save daurrutia/8bc25ade7152f6a08d295008dd90bb74 to your computer and use it in GitHub Desktop.
Import certificates previously converted from a bundle in PKCS7 format to PEM format into a Java keystore
#!/bin/bash
# author:
# cmcginty https://stackoverflow.com/a/29997111/7742737
# example:
# ./import-pem.sh TrustedCAs.PEM changeit truststore.jks
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment