Skip to content

Instantly share code, notes, and snippets.

@NiceRath
Created March 4, 2024 08:09
Show Gist options
  • Save NiceRath/321cae8d087ed8ae89b6a0a5fb271916 to your computer and use it in GitHub Desktop.
Save NiceRath/321cae8d087ed8ae89b6a0a5fb271916 to your computer and use it in GitHub Desktop.
OpenSSL scan directory for expired OCSP
#!/usr/bin/env bash
set -eo pipefail
if [ -z "$1" ]
then
echo "You need to supply the path to a certificate-directory to scan"
exit 1
fi
set -u
CHECK_DIR="$1"
OCSP_FILE_EXT='.pem.ocsp'
MAX_AGE_DAYS=5
MAX_AGE_SEC="$(( MAX_AGE_DAYS * 24 * 3600 ))"
NOW="$(date +"%s")"
if ! [ -d "$CHECK_DIR" ]
then
echo "Directory '${CHECK_DIR}' does not exist!"
exit 1
fi
for file in "$CHECK_DIR"/*
do
if [[ "$file" == *"$OCSP_FILE_EXT" ]]
then
ocsp_update_time_str="$(openssl ocsp -respin "$file" -text -noverify | grep 'This Update' | cut -d ':' -f2-)"
ocsp_update_time="$(date -d "$ocsp_update_time_str" +"%s")"
ocsp_expire_time="$(( ocsp_update_time + MAX_AGE_SEC ))"
if (( ocsp_expire_time < NOW ))
then
ocsp_expired_since="$(( (NOW - ocsp_expire_time) / 3600 ))"
echo "EXPIRED: ${file} (since ${ocsp_expired_since}h)"
fi
fi
done
@NiceRath
Copy link
Author

NiceRath commented Mar 4, 2024

Basically - extract the This Update time and convert it to a unix timestamp:
date -d "$(openssl ocsp -respin "${ocsp_file}" -text -noverify | grep "This Update" | cut -d ':' -f2-)" +"%s"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment