Skip to content

Instantly share code, notes, and snippets.

@ReeganExE
Created February 17, 2019 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ReeganExE/d718ca2857770faf39c218c4523e327c to your computer and use it in GitHub Desktop.
Save ReeganExE/d718ca2857770faf39c218c4523e327c to your computer and use it in GitHub Desktop.
Import Certificate from websites to a Java keystore
#!/usr/bin/env sh
# Change this
KEYSTORE_FILE=dest_keystore
KEYSTORE_PASS=changeit
import_cert() {
local HOST=$1
local PORT=$2
# get the SSL certificate
openssl s_client -connect ${HOST}:${PORT} </dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ${HOST}.cert
# delete the old alias and then import the new one
keytool -delete -keystore ${KEYSTORE_FILE} -storepass ${KEYSTORE_PASS} -alias ${HOST} &> /dev/null
# create a keystore and import certificate
keytool -import -noprompt -trustcacerts \
-alias ${HOST} -file ${HOST}.cert \
-keystore ${KEYSTORE_FILE} -storepass ${KEYSTORE_PASS}
rm ${HOST}.cert
}
import_cert stackoverflow.com 443
import_cert www.google.com 443
import_cert 172.217.194.104 443 # google
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment