Last active
May 9, 2017 12:58
-
-
Save yyuu/fc51da1dc40295c02128 to your computer and use it in GitHub Desktop.
pem2jks.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# pem2jks.sh - convert bundled PEM certificate to JKS format | |
# | |
# Special thanks to Justin Ludwig about his article on converting PEM to JKS. | |
# http://blog.swwomm.com/2015/02/importing-new-rds-ca-certificate-into.html | |
# | |
set -e | |
tmp="$(mktemp -d "/tmp/$(basename "$0" ".sh").XXXXX")" | |
on_exit() { | |
rm -fr "${tmp}" | |
} | |
trap on_exit HUP TERM EXIT | |
get_alias() { | |
openssl x509 -noout -text -in "$1" | perl -ne 'next unless /Subject:/; s/.*CN=//; print' | |
} | |
abspath() { | |
( cd "$(dirname "$1")" && echo "${PWD}/$(basename "$1")" ) | |
} | |
_pem2jks() { | |
local pem="$(abspath "$1")" | |
local jks="$(abspath "$2")" | |
local dir="$(mktemp -d "${tmp}/pem.XXXXX")" | |
pushd "${dir}" 1>/dev/null | |
rm -f "${jks}" | |
csplit -f cert. -s -z "${pem}" '/-BEGIN CERTIFICATE-/' '{*}' | |
for cert in "cert."*; do | |
name="$(get_alias "${cert}")" | |
echo "${name}" 1>&2 | |
keytool -import -keystore "${jks}" -storepass "${storepass}" -noprompt -alias "$(get_alias "${cert}")" -file "${cert}" 1>/dev/null 2>&1 | |
done | |
popd 1>/dev/null | |
} | |
read -p "Password: " -s | |
storepass="${REPLY}" | |
echo | |
for pem; do | |
jks="$(basename "${pem}" ".pem").jks" | |
_pem2jks "${pem}" "${jks}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You have literally changed my life with this script. Thanks!