Skip to content

Instantly share code, notes, and snippets.

@hydrian
Created June 19, 2019 15:28
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 hydrian/9aa5ded5609fcbd60882db0d559c9a05 to your computer and use it in GitHub Desktop.
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
#!/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