Created
June 19, 2019 15:28
-
-
Save hydrian/9aa5ded5609fcbd60882db0d559c9a05 to your computer and use it in GitHub Desktop.
Normalizes cert filename based on subject and puts full text and encoded data in contents
This file contains 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
#!/bin/bash | |
ORIG_CERT_FILE="${1}" | |
if [ ! -e "${ORIG_CERT_FILE}" ] ; then | |
echo "${ORIG_CERT_FILE} filename does not exist" 1>&2 | |
exit 2 | |
fi | |
ORIG_CERT_DIR=$(dirname $(realpath "$ORIG_CERT_FILE")) | |
CERT_SUBJECT="$(openssl x509 -in "${ORIG_CERT_FILE}" -noout -subject)" | |
if [ $? -ne 0 ] ; then | |
echo "Failed to parse X509 certificate" 1>&2 | |
exit 2 | |
fi | |
CERT_FILENAME=$(echo "${CERT_SUBJECT}" |sed -E 's/.*(\/CN\=(.*$))/\2/'|sed -e 's/[^a-zA-Z0-9,._+@%/-]/_/g;').pem | |
NORMALIZED_FULL_PATH="${ORIG_CERT_DIR}/${CERT_FILENAME}" | |
openssl x509 -in "${ORIG_CERT_FILE}" -text > "${NORMALIZED_FULL_PATH}" | |
if [ $? -eq 0 ] ; then | |
echo "X509 certificate normalized as ${NORMALIZED_FULL_PATH}" | |
exit 0 | |
else | |
echo "Failed to normalize X509 certificate" 1>&2 | |
exit 2 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment