]>
Commit | Line | Data |
---|---|---|
2e8454b4 | 1 | #!/usr/bin/env bash |
61f0b7ed | 2 | |
69f3e78b LS |
3 | set -e |
4 | set -u | |
5 | set -o pipefail | |
61f0b7ed | 6 | |
f11bb1db SR |
7 | # default config values |
8 | CA="https://acme-v01.api.letsencrypt.org" | |
00a0937c | 9 | LICENSE="https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf" |
b33f1288 | 10 | HOOK_CHALLENGE= |
e300c0a1 | 11 | RENEW_DAYS="14" |
5a213f5f | 12 | KEYSIZE="4096" |
ff3dbc58 | 13 | WELLKNOWN=".acme-challenges" |
f11bb1db | 14 | |
440dc30d LS |
15 | if [[ -e "config.sh" ]]; then |
16 | . ./config.sh | |
17 | fi | |
61f0b7ed | 18 | |
181dd0ff SR |
19 | umask 077 # paranoid umask, we're creating private keys |
20 | ||
c6e60302 LS |
21 | anti_newline() { |
22 | tr -d '\n\r' | |
23 | } | |
181dd0ff | 24 | |
61f0b7ed | 25 | urlbase64() { |
c6e60302 LS |
26 | # urlbase64: base64 encoded string with '+' replaced with '-' and '/' replaced with '_' |
27 | openssl base64 -e | anti_newline | sed -r 's/=*$//g' | tr '+/' '-_' | |
61f0b7ed | 28 | } |
91ce50af | 29 | |
9fe313d8 | 30 | hex2bin() { |
c6e60302 | 31 | # Store hex string from stdin |
20e7d9d7 LS |
32 | tmphex="$(cat)" |
33 | ||
c6e60302 | 34 | # Remove spaces |
20e7d9d7 LS |
35 | hex='' |
36 | for ((i=0; i<${#tmphex}; i+=1)); do | |
37 | test "${tmphex:$i:1}" == " " || hex="${hex}${tmphex:$i:1}" | |
38 | done | |
39 | ||
c6e60302 | 40 | # Add leading zero |
20e7d9d7 LS |
41 | test $((${#hex} & 1)) == 0 || hex="0${hex}" |
42 | ||
c6e60302 | 43 | # Convert to escaped string |
20e7d9d7 LS |
44 | escapedhex='' |
45 | for ((i=0; i<${#hex}; i+=2)); do | |
46 | escapedhex=$escapedhex\\x${hex:$i:2} | |
47 | done | |
48 | ||
c6e60302 | 49 | # Convert to binary data |
20e7d9d7 | 50 | printf "${escapedhex}" |
9fe313d8 | 51 | } |
61f0b7ed | 52 | |
91ce50af LS |
53 | _request() { |
54 | temperr="$(mktemp)" | |
dd5f36e5 | 55 | if [[ "${1}" = "head" ]]; then |
c4be4c69 | 56 | curl -sSf -I "${2}" 2> "${temperr}" |
dd5f36e5 | 57 | elif [[ "${1}" = "get" ]]; then |
c4be4c69 | 58 | curl -sSf "${2}" 2> "${temperr}" |
dd5f36e5 | 59 | elif [[ "${1}" = "post" ]]; then |
c4be4c69 | 60 | curl -sSf "${2}" -d "${3}" 2> "${temperr}" |
91ce50af | 61 | fi |
dd5f36e5 | 62 | |
96d7eabe | 63 | if [[ -s "${temperr}" ]]; then |
dd5f36e5 BDS |
64 | echo " + ERROR: An error occured while sending ${1}-request to ${2} ($(<"${temperr}"))" >&2 |
65 | rm -f "${temperr}" | |
66 | exit 1 | |
130ea6ab | 67 | fi |
dd5f36e5 | 68 | |
91ce50af LS |
69 | rm -f "${temperr}" |
70 | } | |
71 | ||
61f0b7ed | 72 | signed_request() { |
c6e60302 | 73 | # Encode payload as urlbase64 |
4aa48d33 | 74 | payload64="$(printf '%s' "${2}" | urlbase64)" |
61f0b7ed | 75 | |
c6e60302 LS |
76 | # Retrieve nonce from acme-server |
77 | nonce="$(_request head "${CA}/directory" | grep Replay-Nonce: | awk -F ': ' '{print $2}' | anti_newline)" | |
61f0b7ed | 78 | |
c6e60302 | 79 | # Build header with just our public key and algorithm information |
61f0b7ed LS |
80 | header='{"alg": "RS256", "jwk": {"e": "'"${pubExponent64}"'", "kty": "RSA", "n": "'"${pubMod64}"'"}}' |
81 | ||
c6e60302 | 82 | # Build another header which also contains the previously received nonce and encode it as urlbase64 |
61f0b7ed | 83 | protected='{"alg": "RS256", "jwk": {"e": "'"${pubExponent64}"'", "kty": "RSA", "n": "'"${pubMod64}"'"}, "nonce": "'"${nonce}"'"}' |
4aa48d33 | 84 | protected64="$(printf '%s' "${protected}" | urlbase64)" |
61f0b7ed | 85 | |
c6e60302 | 86 | # Sign header with nonce and our payload with our private key and encode signature as urlbase64 |
4aa48d33 | 87 | signed64="$(printf '%s' "${protected64}.${payload64}" | openssl dgst -sha256 -sign private_key.pem | urlbase64)" |
61f0b7ed | 88 | |
c6e60302 | 89 | # Send header + extended header + payload + signature to the acme-server |
61f0b7ed LS |
90 | data='{"header": '"${header}"', "protected": "'"${protected64}"'", "payload": "'"${payload64}"'", "signature": "'"${signed64}"'"}' |
91 | ||
91ce50af | 92 | _request post "${1}" "${data}" |
61f0b7ed LS |
93 | } |
94 | ||
61f0b7ed LS |
95 | sign_domain() { |
96 | domain="${1}" | |
1f65a335 SR |
97 | altnames="${*}" |
98 | echo "Signing domain ${1} (${*})..." | |
61f0b7ed | 99 | |
c6e60302 | 100 | # If there is no existing certificate directory we need a new private key |
dd5f36e5 | 101 | if [[ ! -e "certs/${domain}" ]]; then |
d211fece | 102 | mkdir -p "certs/${domain}" |
61f0b7ed | 103 | echo " + Generating private key..." |
5a213f5f | 104 | openssl genrsa -out "certs/${domain}/privkey.pem" "${KEYSIZE}" 2> /dev/null > /dev/null |
61f0b7ed LS |
105 | fi |
106 | ||
c6e60302 LS |
107 | # Generate signing request config and the actual signing request |
108 | SAN="" | |
61f0b7ed | 109 | for altname in $altnames; do |
c6e60302 LS |
110 | SAN+="DNS:${altname}, " |
111 | done | |
112 | SAN="$(printf '%s' "${SAN}" | sed 's/,\s*$//g')" | |
113 | echo " + Generating signing request..." | |
114 | openssl req -new -sha256 -key "certs/${domain}/privkey.pem" -out "certs/${domain}/cert.csr" -subj "/CN=${domain}/" -reqexts SAN -config <(cat /etc/ssl/openssl.cnf <(printf "[SAN]\nsubjectAltName=%s" "${SAN}")) > /dev/null | |
115 | ||
116 | # Request and respond to challenges | |
117 | for altname in $altnames; do | |
118 | # Ask the acme-server for new challenge token and extract them from the resulting json block | |
61f0b7ed LS |
119 | echo " + Requesting challenge for ${altname}..." |
120 | response="$(signed_request "${CA}/acme/new-authz" '{"resource": "new-authz", "identifier": {"type": "dns", "value": "'"${altname}"'"}}')" | |
121 | ||
4aa48d33 SR |
122 | challenge_token="$(printf '%s\n' "${response}" | grep -Eo '"challenges":[^\[]*\[[^]]*]' | sed 's/{/\n{/g' | grep 'http-01' | grep -Eo '"token":\s*"[^"]*"' | cut -d'"' -f4 | sed 's/[^A-Za-z0-9_\-]/_/g')" |
123 | challenge_uri="$(printf '%s\n' "${response}" | grep -Eo '"challenges":[^\[]*\[[^]]*]' | sed 's/{/\n{/g' | grep 'http-01' | grep -Eo '"uri":\s*"[^"]*"' | cut -d'"' -f4)" | |
61f0b7ed | 124 | |
dd5f36e5 | 125 | if [[ -z "${challenge_token}" ]] || [[ -z "${challenge_uri}" ]]; then |
a1621214 | 126 | echo " + Error: Can't retrieve challenges (${response})" |
abb95693 LS |
127 | exit 1 |
128 | fi | |
129 | ||
c6e60302 | 130 | # Challenge response consists of the challenge token and the thumbprint of our public certificate |
61f0b7ed LS |
131 | keyauth="${challenge_token}.${thumbprint}" |
132 | ||
c6e60302 | 133 | # Store challenge response in well-known location and make world-readable (so that a webserver can access it) |
4aa48d33 | 134 | printf '%s' "${keyauth}" > "${WELLKNOWN}/${challenge_token}" |
2b5df371 | 135 | chmod a+r "${WELLKNOWN}/${challenge_token}" |
61f0b7ed | 136 | |
b33f1288 SR |
137 | # Wait for hook script to deploy the challenge if used |
138 | if [ -n "${HOOK_CHALLENGE}" ]; then | |
139 | ${HOOK_CHALLENGE} "${WELLKNOWN}/${challenge_token}" "${keyauth}" | |
140 | fi | |
141 | ||
c6e60302 | 142 | # Ask the acme-server to verify our challenge and wait until it becomes valid |
61f0b7ed LS |
143 | echo " + Responding to challenge for ${altname}..." |
144 | result="$(signed_request "${challenge_uri}" '{"resource": "challenge", "keyAuthorization": "'"${keyauth}"'"}')" | |
145 | ||
4aa48d33 | 146 | status="$(printf '%s\n' "${result}" | grep -Eo '"status":\s*"[^"]*"' | cut -d'"' -f4)" |
61f0b7ed | 147 | |
76a37834 | 148 | # get status until it a result is reached => not pending anymore |
dd5f36e5 | 149 | while [[ "${status}" = "pending" ]]; do |
c6e60302 | 150 | sleep 1 |
76a37834 | 151 | status="$(_request get "${challenge_uri}" | grep -Eo '"status":\s*"[^"]*"' | cut -d'"' -f4)" |
61f0b7ed LS |
152 | done |
153 | ||
76a37834 MG |
154 | if [[ "${status}" = "valid" ]]; then |
155 | echo " + Challenge is valid!" | |
156 | else | |
157 | echo " + Challenge is invalid! (returned: ${status})" | |
158 | exit 1 | |
159 | fi | |
160 | ||
61f0b7ed LS |
161 | done |
162 | ||
b7439a83 | 163 | # Finally request certificate from the acme-server and store it in cert-${timestamp}.pem and link from cert.pem |
61f0b7ed | 164 | echo " + Requesting certificate..." |
b7439a83 | 165 | timestamp="$(date +%s)" |
61f0b7ed | 166 | csr64="$(openssl req -in "certs/${domain}/cert.csr" -outform DER | urlbase64)" |
c6e60302 | 167 | crt64="$(signed_request "${CA}/acme/new-cert" '{"resource": "new-cert", "csr": "'"${csr64}"'"}' | openssl base64 -e)" |
b7439a83 MG |
168 | printf -- '-----BEGIN CERTIFICATE-----\n%s\n-----END CERTIFICATE-----\n' "${crt64}" > "certs/${domain}/cert-${timestamp}.pem" |
169 | rm -f "certs/${domain}/cert.pem" | |
170 | ln -s "cert-${timestamp}.pem" "certs/${domain}/cert.pem" | |
61f0b7ed LS |
171 | echo " + Done!" |
172 | } | |
173 | ||
5a213f5f | 174 | # Check if private key exists, if it doesn't exist yet generate a new one (rsa key) |
8221727a | 175 | register="0" |
dd5f36e5 | 176 | if [[ ! -e "private_key.pem" ]]; then |
f13eaa7f | 177 | echo "+ Generating account key..." |
5a213f5f | 178 | openssl genrsa -out "private_key.pem" "${KEYSIZE}" 2> /dev/null > /dev/null |
8221727a LS |
179 | register="1" |
180 | fi | |
181 | ||
c6e60302 | 182 | # Get public components from private key and calculate thumbprint |
9fe313d8 | 183 | pubExponent64="$(printf "%06x" "$(openssl rsa -in private_key.pem -noout -text | grep publicExponent | head -1 | cut -d' ' -f2)" | hex2bin | urlbase64)" |
4aa48d33 | 184 | pubMod64="$(printf '%s' "$(openssl rsa -in private_key.pem -noout -modulus | cut -d'=' -f2)" | hex2bin | urlbase64)" |
8221727a | 185 | |
c6e60302 | 186 | thumbprint="$(printf '%s' "$(printf '%s' '{"e":"'"${pubExponent64}"'","kty":"RSA","n":"'"${pubMod64}"'"}' | shasum -a 256 | awk '{print $1}')" | hex2bin | urlbase64)" |
8221727a | 187 | |
c6e60302 | 188 | # If we generated a new private key in the step above we have to register it with the acme-server |
dd5f36e5 | 189 | if [[ "${register}" = "1" ]]; then |
f13eaa7f | 190 | echo "+ Registering account key with letsencrypt..." |
00a0937c | 191 | signed_request "${CA}/acme/new-reg" '{"resource": "new-reg", "agreement": "'"$LICENSE"'"}' > /dev/null |
f13eaa7f LS |
192 | fi |
193 | ||
440dc30d LS |
194 | if [[ ! -e "domains.txt" ]]; then |
195 | echo "You have to create a domains.txt file listing the domains you want certificates for. Have a look at domains.txt.example." | |
196 | exit 1 | |
197 | fi | |
198 | ||
3390080c LS |
199 | if [[ ! -e "${WELLKNOWN}" ]]; then |
200 | mkdir -p "${WELLKNOWN}" | |
201 | fi | |
202 | ||
5060dea0 | 203 | # Generate certificates for all domains found in domain.txt. Check if existing certificate are about to expire |
c4be4c69 | 204 | <domains.txt sed 's/^\s*//g;s/\s*$//g' | grep -v '^#' | grep -v '^$' | while read -r line; do |
5060dea0 MG |
205 | domain="$(echo $line | cut -d' ' -f1)" |
206 | if [[ -e "certs/${domain}/cert.pem" ]]; then | |
207 | echo -n "Found existing cert for ${domain}. Expire date ..." | |
e300c0a1 | 208 | set +e; openssl x509 -checkend $((${RENEW_DAYS} * 86400)) -noout -in "certs/${domain}/cert.pem"; expiring=$?; set -e |
5060dea0 | 209 | if [[ ${expiring} -eq 0 ]]; then |
e300c0a1 | 210 | echo " is not within ${RENEW_DAYS} days. Skipping" |
5060dea0 MG |
211 | continue |
212 | fi | |
e300c0a1 | 213 | echo " is within ${RENEW_DAYS} days. Renewing..." |
5060dea0 MG |
214 | fi |
215 | ||
6221526d | 216 | sign_domain $line |
61f0b7ed | 217 | done |