|
| 1 | +#!/bin/bash |
| 2 | +# ------------------------------------------------------------------------------ |
| 3 | +# Create TLSA records from the current & backup Let's Encrypt Intermediate CAs |
| 4 | +# |
| 5 | +# Author : Esa Jokinen (oh2fih) |
| 6 | +# Home : https://github.com/oh2fih/Misc-Scripts |
| 7 | +# ------------------------------------------------------------------------------ |
| 8 | + |
| 9 | +SOURCE="/certificates/" |
| 10 | +BASE_URL="https://letsencrypt.org" |
| 11 | + |
| 12 | +# Check for requirements. Print all unmet requirements at once. |
| 13 | + |
| 14 | +required_command() { |
| 15 | + if ! command -v "$1" &> /dev/null; then |
| 16 | + if [ -z ${2+x} ]; then |
| 17 | + echo -e "\033[0;31mThis script requires ${1}!\033[0m" >&2 |
| 18 | + else |
| 19 | + echo -e "\033[0;31mThis script requires ${1} ${2}!\033[0m" >&2 |
| 20 | + fi |
| 21 | + ((UNMET=UNMET+1)) |
| 22 | + fi |
| 23 | +} |
| 24 | + |
| 25 | +UNMET=0 |
| 26 | + |
| 27 | +required_command "openssl" "for creating TLSA records" |
| 28 | +required_command "curl" "for fetching data" |
| 29 | +required_command "grep" |
| 30 | +required_command "sed" |
| 31 | +required_command "awk" |
| 32 | + |
| 33 | +if [ "$UNMET" -gt 0 ]; then |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | + |
| 37 | +# Get URLs for the Subordinate (Intermediate) CAs, including backups |
| 38 | + |
| 39 | +INTERMEDIATE_PATHS=$( |
| 40 | + curl --silent "${BASE_URL}${SOURCE}" \ |
| 41 | + | sed '/subordinate-intermediate-cas/d' \ |
| 42 | + | sed '/.summary.Retired..summary./q' \ |
| 43 | + | grep -oE "/certs/[0-9]+/[0-9a-zA-Z]+(-cross)?.pem" |
| 44 | +) |
| 45 | + |
| 46 | +if [ "$INTERMEDIATE_PATHS" = "" ]; then |
| 47 | + echo "Failed to fetch certificate list from ${BASE_URL}${SOURCE}" >&2 |
| 48 | + exit 1 |
| 49 | +fi |
| 50 | + |
| 51 | +# Create TLSA records |
| 52 | + |
| 53 | +while IFS= read -r path ; do |
| 54 | + echo "[${BASE_URL}${path}]" >&2 |
| 55 | + PEM=$(curl --silent "${BASE_URL}${path}") |
| 56 | + if [[ "$PEM" =~ ^[-]+BEGIN[[:space:]]CERTIFICATE[-]+ ]]; then |
| 57 | + echo "$PEM" \ |
| 58 | + | openssl x509 -outform DER \ |
| 59 | + | openssl dgst -sha256 -hex \ |
| 60 | + | awk '{print "le-ca TLSA 2 1 1", $NF}' |
| 61 | + fi |
| 62 | + |
| 63 | +done <<< "$INTERMEDIATE_PATHS" |
0 commit comments