Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keytool importing multiple certificates in single file

How to import multiple certificates in a single file with keytool [to cert store]?

keytool -importcert only imports the first one.

like image 875
Fakrudeen Avatar asked Feb 02 '13 10:02

Fakrudeen


People also ask

Can I import multiple certificates into keystore?

Introduction. Keytool is a certificate management utility included with Java. It allows users to create a single store, called a keystore, that can hold multiple certificates within it. This file can then be assigned or installed to a server and used for SSL/TLS connections.

How do I import a certificate from one keystore to another?

The command "importkeystore" is used to import an entire keystore into another keystore, which means all entries from the source keystore, including keys and certificates, are all imported to the destination keystore within a single command. You can use this command to import entries from a different type of keystore.


1 Answers

A bash script that will import all certificates from a PEM file:

#!/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

For example:

./jks_import_pem TrustedCAs.PEM changeit truststore.jks
like image 162
cmcginty Avatar answered Sep 20 '22 20:09

cmcginty