]> git.street.me.uk Git - andy/dehydrated.git/blame - letsencrypt.sh
renamed PRIVATE_KEY to ACCOUNT_KEY (as suggested in #183)
[andy/dehydrated.git] / letsencrypt.sh
CommitLineData
2e8454b4 1#!/usr/bin/env bash
a1a9c8a4
LS
2
3# letsencrypt.sh by lukas2511
4# Source: https://github.com/lukas2511/letsencrypt.sh
0fa381ff
LS
5#
6# This script is licensed under The MIT License (see LICENSE for more information).
a1a9c8a4 7
69f3e78b
LS
8set -e
9set -u
10set -o pipefail
da2eeda9 11[[ -n "${ZSH_VERSION:-}" ]] && set -o SH_WORD_SPLIT && set +o FUNCTION_ARGZERO
81882a64 12umask 077 # paranoid umask, we're creating private keys
61f0b7ed 13
85a25b56
LS
14# Find directory in which this script is stored by traversing all symbolic links
15SOURCE="${0}"
16while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
17 DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
18 SOURCE="$(readlink "$SOURCE")"
19 [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
20done
21SCRIPTDIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
22
0e92aba2
MG
23BASEDIR="${SCRIPTDIR}"
24
1f6a80a0
LS
25# Create (identifiable) temporary files
26_mktemp() {
75985c6a 27 # shellcheck disable=SC2068
c3dbfe88 28 mktemp ${@:-} "${TMPDIR:-/tmp}/letsencrypt.sh-XXXXXX"
1f6a80a0
LS
29}
30
bc580335 31# Check for script dependencies
9f66bfdb 32check_dependencies() {
0af7f388 33 # just execute some dummy and/or version commands to see if required tools exist and are actually usable
115041cd 34 openssl version > /dev/null 2>&1 || _exiterr "This script requires an openssl binary."
f7c7d8c5 35 _sed "" < /dev/null > /dev/null 2>&1 || _exiterr "This script requires sed with support for extended (modern) regular expressions."
4b8883b4 36 command -v grep > /dev/null 2>&1 || _exiterr "This script requires grep."
1f6a80a0 37 _mktemp -u > /dev/null 2>&1 || _exiterr "This script requires mktemp."
0af7f388
LS
38
39 # curl returns with an error code in some ancient versions so we have to catch that
40 set +e
41 curl -V > /dev/null 2>&1
0af7f388 42 retcode="$?"
36a03146 43 set -e
0af7f388
LS
44 if [[ ! "${retcode}" = "0" ]] && [[ ! "${retcode}" = "2" ]]; then
45 _exiterr "This script requires curl."
46 fi
9f66bfdb
LS
47}
48
ff116396
LS
49# Setup default config values, search for and load configuration files
50load_config() {
00810795
LS
51 # Check for config in various locations
52 if [[ -z "${CONFIG:-}" ]]; then
53 for check_config in "/etc/letsencrypt.sh" "/usr/local/etc/letsencrypt.sh" "${PWD}" "${SCRIPTDIR}"; do
54 if [[ -e "${check_config}/config.sh" ]]; then
55 BASEDIR="${check_config}"
56 CONFIG="${check_config}/config.sh"
57 break
58 fi
59 done
60 fi
61
ff116396
LS
62 # Default values
63 CA="https://acme-v01.api.letsencrypt.org/directory"
64 LICENSE="https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf"
de173892 65 CHALLENGETYPE="http-01"
a1cb7ccc 66 CONFIG_D=
ff116396 67 HOOK=
6e048f7f 68 HOOK_CHAIN="no"
30ad9584 69 RENEW_DAYS="30"
8aa1a05b
LS
70 ACCOUNT_KEY=
71 ACCOUNT_KEY_JSON=
ff116396 72 KEYSIZE="4096"
9baf3532 73 WELLKNOWN=
ff116396 74 PRIVATE_KEY_RENEW="no"
c71ca3a8 75 KEY_ALGO=rsa
f0323faf 76 OPENSSL_CNF="$(openssl version -d | cut -d\" -f2)/openssl.cnf"
ff116396 77 CONTACT_EMAIL=
9baf3532 78 LOCKFILE=
1e33cfe5 79
81882a64 80 if [[ -z "${CONFIG:-}" ]]; then
ff116396 81 echo "#" >&2
a1cb7ccc 82 echo "# !! WARNING !! No main config file found, using default config!" >&2
ff116396 83 echo "#" >&2
81882a64 84 elif [[ -e "${CONFIG}" ]]; then
a1cb7ccc 85 echo "# INFO: Using main config file ${CONFIG}"
81882a64
LS
86 BASEDIR="$(dirname "${CONFIG}")"
87 # shellcheck disable=SC1090
88 . "${CONFIG}"
89 else
f06f764f 90 _exiterr "Specified config file doesn't exist."
81882a64 91 fi
61f0b7ed 92
a1cb7ccc
DB
93 if [[ -n "${CONFIG_D}" ]]; then
94 if [[ ! -d "${CONFIG_D}" ]]; then
95 _exiterr "The path ${CONFIG_D} specified for CONFIG_D does not point to a directory." >&2
96 fi
97
e2d8bfa4 98 for check_config_d in "${CONFIG_D}"/*.sh; do
a1cb7ccc
DB
99 if [[ ! -e "${check_config_d}" ]]; then
100 echo "# !! WARNING !! Extra configuration directory ${CONFIG_D} exists, but no configuration found in it." >&2
101 break
102 elif [[ -f "${check_config_d}" ]] && [[ -r "${check_config_d}" ]]; then
103 echo "# INFO: Using additional config file ${check_config_d}"
5c68c221 104 # shellcheck disable=SC1090
e2d8bfa4 105 . "${check_config_d}"
a1cb7ccc
DB
106 else
107 _exiterr "Specified additional config ${check_config_d} is not readable or not a file at all." >&2
108 fi
109 done
110 fi
111
81882a64
LS
112 # Remove slash from end of BASEDIR. Mostly for cleaner outputs, doesn't change functionality.
113 BASEDIR="${BASEDIR%%/}"
401f5f75 114
1e33cfe5 115 # Check BASEDIR and set default variables
f06f764f 116 [[ -d "${BASEDIR}" ]] || _exiterr "BASEDIR does not exist: ${BASEDIR}"
ed27e013 117
8aa1a05b
LS
118 [[ -z "${ACCOUNT_KEY}" ]] && ACCOUNT_KEY="${BASEDIR}/private_key.pem"
119 [[ -z "${ACCOUNT_KEY_JSON}" ]] && ACCOUNT_KEY_JSON="${BASEDIR}/private_key.json"
9baf3532
DB
120 [[ -z "${WELLKNOWN}" ]] && WELLKNOWN="${BASEDIR}/.acme-challenges"
121 [[ -z "${LOCKFILE}" ]] && LOCKFILE="${BASEDIR}/lock"
122
de173892
LS
123 [[ -n "${PARAM_HOOK:-}" ]] && HOOK="${PARAM_HOOK}"
124 [[ -n "${PARAM_CHALLENGETYPE:-}" ]] && CHALLENGETYPE="${PARAM_CHALLENGETYPE}"
c71ca3a8 125 [[ -n "${PARAM_KEY_ALGO:-}" ]] && KEY_ALGO="${PARAM_KEY_ALGO}"
e925b293 126
de173892 127 [[ "${CHALLENGETYPE}" =~ (http-01|dns-01) ]] || _exiterr "Unknown challenge type ${CHALLENGETYPE}... can not continue."
e925b293 128 if [[ "${CHALLENGETYPE}" = "dns-01" ]] && [[ -z "${HOOK}" ]]; then
de173892 129 _exiterr "Challenge type dns-01 needs a hook script for deployment... can not continue."
e925b293 130 fi
c71ca3a8 131 [[ "${KEY_ALGO}" =~ ^(rsa|prime256v1|secp384r1)$ ]] || _exiterr "Unknown public key algorithm ${KEY_ALGO}... can not continue."
ff116396
LS
132}
133
93cd114f 134# Initialize system
ff116396
LS
135init_system() {
136 load_config
81882a64 137
1e33cfe5 138 # Lockfile handling (prevents concurrent access)
291b9f24 139 LOCKDIR="$(dirname "${LOCKFILE}")"
61ba0daf 140 [[ -w "${LOCKDIR}" ]] || _exiterr "Directory ${LOCKDIR} for LOCKFILE ${LOCKFILE} is not writable, aborting."
93cd114f
LS
141 ( set -C; date > "${LOCKFILE}" ) 2>/dev/null || _exiterr "Lock file '${LOCKFILE}' present, aborting."
142 remove_lock() { rm -f "${LOCKFILE}"; }
81882a64
LS
143 trap 'remove_lock' EXIT
144
81882a64 145 # Get CA URLs
3a9e97f9 146 CA_DIRECTORY="$(http_request get "${CA}")"
81882a64
LS
147 CA_NEW_CERT="$(printf "%s" "${CA_DIRECTORY}" | get_json_string_value new-cert)" &&
148 CA_NEW_AUTHZ="$(printf "%s" "${CA_DIRECTORY}" | get_json_string_value new-authz)" &&
149 CA_NEW_REG="$(printf "%s" "${CA_DIRECTORY}" | get_json_string_value new-reg)" &&
10d9f342 150 # shellcheck disable=SC2015
81882a64 151 CA_REVOKE_CERT="$(printf "%s" "${CA_DIRECTORY}" | get_json_string_value revoke-cert)" ||
93cd114f 152 _exiterr "Problem retrieving ACME/CA-URLs, check if your configured CA points to the directory entrypoint."
81882a64 153
93cd114f
LS
154 # Export some environment variables to be used in hook script
155 export WELLKNOWN BASEDIR CONFIG
0e92aba2 156
93cd114f
LS
157 # Checking for private key ...
158 register_new_key="no"
8aa1a05b 159 if [[ -n "${PARAM_ACCOUNT_KEY:-}" ]]; then
0e92aba2 160 # a private key was specified from the command line so use it for this run
8aa1a05b
LS
161 echo "Using private key ${PARAM_ACCOUNT_KEY} instead of account key"
162 ACCOUNT_KEY="${PARAM_ACCOUNT_KEY}"
163 ACCOUNT_KEY_JSON="${PARAM_ACCOUNT_KEY}.json"
0e92aba2
MG
164 else
165 # Check if private account key exists, if it doesn't exist yet generate a new one (rsa key)
8aa1a05b 166 if [[ ! -e "${ACCOUNT_KEY}" ]]; then
81882a64 167 echo "+ Generating account key..."
8aa1a05b 168 _openssl genrsa -out "${ACCOUNT_KEY}" "${KEYSIZE}"
93cd114f 169 register_new_key="yes"
81882a64 170 fi
81882a64 171 fi
8aa1a05b 172 openssl rsa -in "${ACCOUNT_KEY}" -check 2>/dev/null > /dev/null || _exiterr "Account key is not valid, can not continue."
1ab6a436 173
81882a64 174 # Get public components from private key and calculate thumbprint
8aa1a05b
LS
175 pubExponent64="$(printf '%x' "$(openssl rsa -in "${ACCOUNT_KEY}" -noout -text | awk '/publicExponent/ {print $2}')" | hex2bin | urlbase64)"
176 pubMod64="$(openssl rsa -in "${ACCOUNT_KEY}" -noout -modulus | cut -d'=' -f2 | hex2bin | urlbase64)"
81882a64 177
21c18dd3 178 thumbprint="$(printf '{"e":"%s","kty":"RSA","n":"%s"}' "${pubExponent64}" "${pubMod64}" | openssl dgst -sha256 -binary | urlbase64)"
81882a64
LS
179
180 # If we generated a new private key in the step above we have to register it with the acme-server
93cd114f 181 if [[ "${register_new_key}" = "yes" ]]; then
81882a64 182 echo "+ Registering account key with letsencrypt..."
93cd114f
LS
183 [[ ! -z "${CA_NEW_REG}" ]] || _exiterr "Certificate authority doesn't allow registrations."
184 # If an email for the contact has been provided then adding it to the registration request
81882a64 185 if [[ -n "${CONTACT_EMAIL}" ]]; then
8aa1a05b 186 signed_request "${CA_NEW_REG}" '{"resource": "new-reg", "contact":["mailto:'"${CONTACT_EMAIL}"'"], "agreement": "'"$LICENSE"'"}' > "${ACCOUNT_KEY_JSON}"
81882a64 187 else
8aa1a05b 188 signed_request "${CA_NEW_REG}" '{"resource": "new-reg", "agreement": "'"$LICENSE"'"}' > "${ACCOUNT_KEY_JSON}"
81882a64
LS
189 fi
190 fi
181dd0ff 191
d9de894c
JTM
192 if [[ "${CHALLENGETYPE}" = "http-01" && ! -d "${WELLKNOWN}" ]]; then
193 _exiterr "WELLKNOWN directory doesn't exist, please create ${WELLKNOWN} and set appropriate permissions."
194 fi
81882a64 195}
c24843c6 196
f7c7d8c5
LS
197# Different sed version for different os types...
198_sed() {
c3c9ff4c 199 if [[ "${OSTYPE}" = "Linux" ]]; then
f7c7d8c5
LS
200 sed -r "${@}"
201 else
202 sed -E "${@}"
203 fi
204}
205
9f66bfdb
LS
206# Print error message and exit with error
207_exiterr() {
208 echo "ERROR: ${1}" >&2
209 exit 1
210}
211
994803bf 212# Encode data as url-safe formatted base64
61f0b7ed 213urlbase64() {
c6e60302 214 # urlbase64: base64 encoded string with '+' replaced with '-' and '/' replaced with '_'
f7c7d8c5 215 openssl base64 -e | tr -d '\n\r' | _sed -e 's:=*$::g' -e 'y:+/:-_:'
61f0b7ed 216}
91ce50af 217
16bef17e 218# Convert hex string to binary data
9fe313d8 219hex2bin() {
16bef17e 220 # Remove spaces, add leading zero, escape as hex string and parse with printf
f7c7d8c5 221 printf -- "$(cat | _sed -e 's/[[:space:]]//g' -e 's/^(.(.{2})*)$/0\1/' -e 's/(.{2})/\\x\1/g')"
9fe313d8 222}
61f0b7ed 223
bc580335 224# Get string value from json dictionary
09729186 225get_json_string_value() {
5c68c221
LS
226 local filter
227 filter=$(printf 's/.*"%s": *"\([^"]*\)".*/\\1/p' "$1")
228 sed -n "${filter}"
09729186
LS
229}
230
cc605a22
LS
231# OpenSSL writes to stderr/stdout even when there are no errors. So just
232# display the output if the exit code was != 0 to simplify debugging.
233_openssl() {
234 set +e
235 out="$(openssl "${@}" 2>&1)"
236 res=$?
237 set -e
39c01fd7
LS
238 if [[ ${res} -ne 0 ]]; then
239 echo " + ERROR: failed to run $* (Exitcode: ${res})" >&2
cc605a22
LS
240 echo >&2
241 echo "Details:" >&2
39c01fd7 242 echo "${out}" >&2
676d15c5 243 echo >&2
39c01fd7 244 exit ${res}
cc605a22
LS
245 fi
246}
247
59f16407 248# Send http(s) request with specified method
3a9e97f9 249http_request() {
1f6a80a0 250 tempcont="$(_mktemp)"
3cb292cb 251
1233dc95 252 set +e
dd5f36e5 253 if [[ "${1}" = "head" ]]; then
3cb292cb 254 statuscode="$(curl -s -w "%{http_code}" -o "${tempcont}" "${2}" -I)"
1233dc95 255 curlret="${?}"
dd5f36e5 256 elif [[ "${1}" = "get" ]]; then
3cb292cb 257 statuscode="$(curl -s -w "%{http_code}" -o "${tempcont}" "${2}")"
1233dc95 258 curlret="${?}"
dd5f36e5 259 elif [[ "${1}" = "post" ]]; then
3cb292cb 260 statuscode="$(curl -s -w "%{http_code}" -o "${tempcont}" "${2}" -d "${3}")"
1233dc95 261 curlret="${?}"
59f16407 262 else
1233dc95 263 set -e
59f16407 264 _exiterr "Unknown request method: ${1}"
91ce50af 265 fi
1233dc95
LS
266 set -e
267
268 if [[ ! "${curlret}" = "0" ]]; then
269 _exiterr "Problem connecting to server (curl returned with ${curlret})"
270 fi
dd5f36e5 271
3cb292cb 272 if [[ ! "${statuscode:0:1}" = "2" ]]; then
84fac541 273 echo " + ERROR: An error occurred while sending ${1}-request to ${2} (Status ${statuscode})" >&2
3cb292cb
LS
274 echo >&2
275 echo "Details:" >&2
9e79c066 276 cat "${tempcont}" >&2
3cb292cb 277 rm -f "${tempcont}"
c24843c6 278
279 # Wait for hook script to clean the challenge if used
676d15c5 280 if [[ -n "${HOOK}" ]] && [[ "${HOOK_CHAIN}" != "yes" ]] && [[ -n "${challenge_token:+set}" ]]; then
2099c77f 281 "${HOOK}" "clean_challenge" '' "${challenge_token}" "${keyauth}"
c24843c6 282 fi
283
8f6c2328 284 # remove temporary domains.txt file if used
79ff846e 285 [[ -n "${PARAM_DOMAIN:-}" && -n "${DOMAINS_TXT:-}" ]] && rm "${DOMAINS_TXT}"
dd5f36e5 286 exit 1
130ea6ab 287 fi
dd5f36e5 288
31111265 289 cat "${tempcont}"
3cb292cb 290 rm -f "${tempcont}"
91ce50af 291}
81882a64 292
1446fd88 293# Send signed request
61f0b7ed 294signed_request() {
c6e60302 295 # Encode payload as urlbase64
4aa48d33 296 payload64="$(printf '%s' "${2}" | urlbase64)"
61f0b7ed 297
c6e60302 298 # Retrieve nonce from acme-server
994803bf 299 nonce="$(http_request head "${CA}" | grep Replay-Nonce: | awk -F ': ' '{print $2}' | tr -d '\n\r')"
61f0b7ed 300
c6e60302 301 # Build header with just our public key and algorithm information
61f0b7ed
LS
302 header='{"alg": "RS256", "jwk": {"e": "'"${pubExponent64}"'", "kty": "RSA", "n": "'"${pubMod64}"'"}}'
303
c6e60302 304 # Build another header which also contains the previously received nonce and encode it as urlbase64
61f0b7ed 305 protected='{"alg": "RS256", "jwk": {"e": "'"${pubExponent64}"'", "kty": "RSA", "n": "'"${pubMod64}"'"}, "nonce": "'"${nonce}"'"}'
4aa48d33 306 protected64="$(printf '%s' "${protected}" | urlbase64)"
61f0b7ed 307
c6e60302 308 # Sign header with nonce and our payload with our private key and encode signature as urlbase64
8aa1a05b 309 signed64="$(printf '%s' "${protected64}.${payload64}" | openssl dgst -sha256 -sign "${ACCOUNT_KEY}" | urlbase64)"
61f0b7ed 310
c6e60302 311 # Send header + extended header + payload + signature to the acme-server
61f0b7ed
LS
312 data='{"header": '"${header}"', "protected": "'"${protected64}"'", "payload": "'"${payload64}"'", "signature": "'"${signed64}"'"}'
313
3a9e97f9 314 http_request post "${1}" "${data}"
61f0b7ed
LS
315}
316
a62968c9
NL
317# Extracts all subject names from a CSR
318# Outputs either the CN, or the SANs, one per line
319extract_altnames() {
320 csr="${1}" # the CSR itself (not a file)
81882a64 321
a62968c9
NL
322 if ! <<<"${csr}" openssl req -verify -noout 2>/dev/null; then
323 _exiterr "Certificate signing request isn't valid"
09729186 324 fi
3cc587c2 325
a62968c9 326 reqtext="$( <<<"${csr}" openssl req -noout -text )"
39c01fd7 327 if <<<"${reqtext}" grep -q '^[[:space:]]*X509v3 Subject Alternative Name:[[:space:]]*$'; then
a62968c9
NL
328 # SANs used, extract these
329 altnames="$( <<<"${reqtext}" grep -A1 '^[[:space:]]*X509v3 Subject Alternative Name:[[:space:]]*$' | tail -n1 )"
330 # split to one per line:
5c68c221 331 # shellcheck disable=SC1003
34f94322 332 altnames="$( <<<"${altnames}" _sed -e 's/^[[:space:]]*//; s/, /\'$'\n''/g' )"
a62968c9 333 # we can only get DNS: ones signed
5c68c221 334 if grep -qv '^DNS:' <<<"${altnames}"; then
a62968c9
NL
335 _exiterr "Certificate signing request contains non-DNS Subject Alternative Names"
336 fi
337 # strip away the DNS: prefix
338 altnames="$( <<<"${altnames}" _sed -e 's/^DNS://' )"
39c01fd7 339 echo "${altnames}"
a62968c9
NL
340
341 else
342 # No SANs, extract CN
343 altnames="$( <<<"${reqtext}" grep '^[[:space:]]*Subject:' | _sed -e 's/.* CN=([^ /,]*).*/\1/' )"
39c01fd7 344 echo "${altnames}"
3dbbb461 345 fi
a62968c9 346}
3dbbb461 347
50e7a072
NL
348# Create certificate for domain(s) and outputs it FD 3
349sign_csr() {
350 csr="${1}" # the CSR itself (not a file)
81882a64 351
50e7a072
NL
352 if { true >&3; } 2>/dev/null; then
353 : # fd 3 looks OK
354 else
355 _exiterr "sign_csr: FD 3 not open"
61f0b7ed
LS
356 fi
357
50e7a072
NL
358 shift 1 || true
359 altnames="${*:-}"
39c01fd7
LS
360 if [ -z "${altnames}" ]; then
361 altnames="$( extract_altnames "${csr}" )"
a62968c9 362 fi
3dbbb461 363
50e7a072
NL
364 if [[ -z "${CA_NEW_AUTHZ}" ]] || [[ -z "${CA_NEW_CERT}" ]]; then
365 _exiterr "Certificate authority doesn't allow certificate signing"
61f0b7ed 366 fi
c6e60302 367
6e048f7f 368 local idx=0
da2eeda9
LS
369 if [[ -n "${ZSH_VERSION:-}" ]]; then
370 local -A challenge_uris challenge_tokens keyauths deploy_args
371 else
372 local -a challenge_uris challenge_tokens keyauths deploy_args
373 fi
39c01fd7 374
6e048f7f 375 # Request challenges
1446fd88 376 for altname in ${altnames}; do
c6e60302 377 # Ask the acme-server for new challenge token and extract them from the resulting json block
579e2316 378 echo " + Requesting challenge for ${altname}..."
09729186 379 response="$(signed_request "${CA_NEW_AUTHZ}" '{"resource": "new-authz", "identifier": {"type": "dns", "value": "'"${altname}"'"}}')"
61f0b7ed 380
4b8883b4 381 challenges="$(printf '%s\n' "${response}" | sed -n 's/.*\("challenges":[^\[]*\[[^]]*]\).*/\1/p')"
526843d6 382 repl=$'\n''{' # fix syntax highlighting in Vim
e925b293 383 challenge="$(printf "%s" "${challenges//\{/${repl}}" | grep \""${CHALLENGETYPE}"\")"
f7c7d8c5 384 challenge_token="$(printf '%s' "${challenge}" | get_json_string_value token | _sed 's/[^A-Za-z0-9_\-]/_/g')"
09729186 385 challenge_uri="$(printf '%s' "${challenge}" | get_json_string_value uri)"
61f0b7ed 386
dd5f36e5 387 if [[ -z "${challenge_token}" ]] || [[ -z "${challenge_uri}" ]]; then
1446fd88 388 _exiterr "Can't retrieve challenges (${response})"
abb95693
LS
389 fi
390
c6e60302 391 # Challenge response consists of the challenge token and the thumbprint of our public certificate
61f0b7ed
LS
392 keyauth="${challenge_token}.${thumbprint}"
393
de173892
LS
394 case "${CHALLENGETYPE}" in
395 "http-01")
396 # Store challenge response in well-known location and make world-readable (so that a webserver can access it)
397 printf '%s' "${keyauth}" > "${WELLKNOWN}/${challenge_token}"
398 chmod a+r "${WELLKNOWN}/${challenge_token}"
399 keyauth_hook="${keyauth}"
400 ;;
401 "dns-01")
402 # Generate DNS entry content for dns-01 validation
21c18dd3 403 keyauth_hook="$(printf '%s' "${keyauth}" | openssl dgst -sha256 -binary | urlbase64)"
de173892
LS
404 ;;
405 esac
61f0b7ed 406
39c01fd7
LS
407 challenge_uris[${idx}]="${challenge_uri}"
408 keyauths[${idx}]="${keyauth}"
409 challenge_tokens[${idx}]="${challenge_token}"
6e048f7f 410 # Note: assumes args will never have spaces!
39c01fd7 411 deploy_args[${idx}]="${altname} ${challenge_token} ${keyauth_hook}"
6e048f7f
GD
412 idx=$((idx+1))
413 done
414
415 # Wait for hook script to deploy the challenges if used
5c68c221 416 # shellcheck disable=SC2068
2099c77f 417 [[ -n "${HOOK}" ]] && [[ "${HOOK_CHAIN}" = "yes" ]] && "${HOOK}" "deploy_challenge" ${deploy_args[@]}
6e048f7f
GD
418
419 # Respond to challenges
420 idx=0
421 for altname in ${altnames}; do
39c01fd7
LS
422 challenge_token="${challenge_tokens[${idx}]}"
423 keyauth="${keyauths[${idx}]}"
6e048f7f 424
b33f1288 425 # Wait for hook script to deploy the challenge if used
5c68c221 426 # shellcheck disable=SC2086
2099c77f 427 [[ -n "${HOOK}" ]] && [[ "${HOOK_CHAIN}" != "yes" ]] && "${HOOK}" "deploy_challenge" ${deploy_args[${idx}]}
b33f1288 428
1446fd88 429 # Ask the acme-server to verify our challenge and wait until it is no longer pending
579e2316 430 echo " + Responding to challenge for ${altname}..."
39c01fd7 431 result="$(signed_request "${challenge_uris[${idx}]}" '{"resource": "challenge", "keyAuthorization": "'"${keyauth}"'"}')"
61f0b7ed 432
da2eeda9 433 reqstatus="$(printf '%s\n' "${result}" | get_json_string_value status)"
61f0b7ed 434
da2eeda9 435 while [[ "${reqstatus}" = "pending" ]]; do
c6e60302 436 sleep 1
39c01fd7 437 result="$(http_request get "${challenge_uris[${idx}]}")"
da2eeda9 438 reqstatus="$(printf '%s\n' "${result}" | get_json_string_value status)"
61f0b7ed
LS
439 done
440
de173892 441 [[ "${CHALLENGETYPE}" = "http-01" ]] && rm -f "${WELLKNOWN}/${challenge_token}"
81882a64 442
ab301951 443 # Wait for hook script to clean the challenge if used
6e048f7f 444 if [[ -n "${HOOK}" ]] && [[ "${HOOK_CHAIN}" != "yes" ]] && [[ -n "${challenge_token}" ]]; then
5c68c221 445 # shellcheck disable=SC2086
2099c77f 446 "${HOOK}" "clean_challenge" ${deploy_args[${idx}]}
ab301951 447 fi
6e048f7f 448 idx=$((idx+1))
81882a64 449
da2eeda9 450 if [[ "${reqstatus}" = "valid" ]]; then
579e2316 451 echo " + Challenge is valid!"
76a37834 452 else
6e048f7f 453 break
76a37834 454 fi
61f0b7ed
LS
455 done
456
6e048f7f 457 # Wait for hook script to clean the challenges if used
5c68c221 458 # shellcheck disable=SC2068
75be937a 459 [[ -n "${HOOK}" ]] && [[ "${HOOK_CHAIN}" = "yes" ]] && "${HOOK}" "clean_challenge" ${deploy_args[@]}
6e048f7f 460
da2eeda9 461 if [[ "${reqstatus}" != "valid" ]]; then
6e048f7f
GD
462 # Clean up any remaining challenge_tokens if we stopped early
463 if [[ "${CHALLENGETYPE}" = "http-01" ]]; then
39c01fd7
LS
464 while [ ${idx} -lt ${#challenge_tokens[@]} ]; do
465 rm -f "${WELLKNOWN}/${challenge_tokens[${idx}]}"
6e048f7f
GD
466 idx=$((idx+1))
467 done
468 fi
469
da2eeda9 470 _exiterr "Challenge is invalid! (returned: ${reqstatus}) (result: ${result})"
6e048f7f
GD
471 fi
472
b7439a83 473 # Finally request certificate from the acme-server and store it in cert-${timestamp}.pem and link from cert.pem
579e2316 474 echo " + Requesting certificate..."
50e7a072 475 csr64="$( <<<"${csr}" openssl req -outform DER | urlbase64)"
09729186 476 crt64="$(signed_request "${CA_NEW_CERT}" '{"resource": "new-cert", "csr": "'"${csr64}"'"}' | openssl base64 -e)"
50e7a072 477 crt="$( printf -- '-----BEGIN CERTIFICATE-----\n%s\n-----END CERTIFICATE-----\n' "${crt64}" )"
1446fd88
LS
478
479 # Try to load the certificate to detect corruption
a4e7c43a 480 echo " + Checking certificate..."
50e7a072
NL
481 _openssl x509 -text <<<"${crt}"
482
483 echo "${crt}" >&3
484
485 unset challenge_token
486 echo " + Done!"
487}
488
489# Create certificate for domain(s)
490sign_domain() {
491 domain="${1}"
492 altnames="${*}"
493 timestamp="$(date +%s)"
494
495 echo " + Signing domains..."
496 if [[ -z "${CA_NEW_AUTHZ}" ]] || [[ -z "${CA_NEW_CERT}" ]]; then
497 _exiterr "Certificate authority doesn't allow certificate signing"
498 fi
499
500 # If there is no existing certificate directory => make it
501 if [[ ! -e "${BASEDIR}/certs/${domain}" ]]; then
502 echo " + Creating new directory ${BASEDIR}/certs/${domain} ..."
503 mkdir -p "${BASEDIR}/certs/${domain}"
504 fi
505
506 privkey="privkey.pem"
507 # generate a new private key if we need or want one
5c189483 508 if [[ ! -r "${BASEDIR}/certs/${domain}/privkey.pem" ]] || [[ "${PRIVATE_KEY_RENEW}" = "yes" ]]; then
50e7a072
NL
509 echo " + Generating private key..."
510 privkey="privkey-${timestamp}.pem"
511 case "${KEY_ALGO}" in
512 rsa) _openssl genrsa -out "${BASEDIR}/certs/${domain}/privkey-${timestamp}.pem" "${KEYSIZE}";;
513 prime256v1|secp384r1) _openssl ecparam -genkey -name "${KEY_ALGO}" -out "${BASEDIR}/certs/${domain}/privkey-${timestamp}.pem";;
514 esac
515 fi
516
517 # Generate signing request config and the actual signing request
518 echo " + Generating signing request..."
519 SAN=""
520 for altname in ${altnames}; do
521 SAN+="DNS:${altname}, "
522 done
523 SAN="${SAN%%, }"
524 local tmp_openssl_cnf
1f6a80a0 525 tmp_openssl_cnf="$(_mktemp)"
50e7a072
NL
526 cat "${OPENSSL_CNF}" > "${tmp_openssl_cnf}"
527 printf "[SAN]\nsubjectAltName=%s" "${SAN}" >> "${tmp_openssl_cnf}"
528 openssl req -new -sha256 -key "${BASEDIR}/certs/${domain}/${privkey}" -out "${BASEDIR}/certs/${domain}/cert-${timestamp}.csr" -subj "/CN=${domain}/" -reqexts SAN -config "${tmp_openssl_cnf}"
529 rm -f "${tmp_openssl_cnf}"
530
531 crt_path="${BASEDIR}/certs/${domain}/cert-${timestamp}.pem"
5c68c221 532 # shellcheck disable=SC2086
50e7a072 533 sign_csr "$(< "${BASEDIR}/certs/${domain}/cert-${timestamp}.csr" )" ${altnames} 3>"${crt_path}"
329acb58
LS
534
535 # Create fullchain.pem
1eb6f6d2
LS
536 echo " + Creating fullchain.pem..."
537 cat "${crt_path}" > "${BASEDIR}/certs/${domain}/fullchain-${timestamp}.pem"
3a9e97f9 538 http_request get "$(openssl x509 -in "${BASEDIR}/certs/${domain}/cert-${timestamp}.pem" -noout -text | grep 'CA Issuers - URI:' | cut -d':' -f2-)" > "${BASEDIR}/certs/${domain}/chain-${timestamp}.pem"
1446fd88 539 if ! grep -q "BEGIN CERTIFICATE" "${BASEDIR}/certs/${domain}/chain-${timestamp}.pem"; then
a733f789
LS
540 openssl x509 -in "${BASEDIR}/certs/${domain}/chain-${timestamp}.pem" -inform DER -out "${BASEDIR}/certs/${domain}/chain-${timestamp}.pem" -outform PEM
541 fi
a733f789 542 cat "${BASEDIR}/certs/${domain}/chain-${timestamp}.pem" >> "${BASEDIR}/certs/${domain}/fullchain-${timestamp}.pem"
329acb58 543
1446fd88
LS
544 # Update symlinks
545 [[ "${privkey}" = "privkey.pem" ]] || ln -sf "privkey-${timestamp}.pem" "${BASEDIR}/certs/${domain}/privkey.pem"
f343dc11 546
1446fd88
LS
547 ln -sf "chain-${timestamp}.pem" "${BASEDIR}/certs/${domain}/chain.pem"
548 ln -sf "fullchain-${timestamp}.pem" "${BASEDIR}/certs/${domain}/fullchain.pem"
3f6ff8f7
SR
549 ln -sf "cert-${timestamp}.csr" "${BASEDIR}/certs/${domain}/cert.csr"
550 ln -sf "cert-${timestamp}.pem" "${BASEDIR}/certs/${domain}/cert.pem"
f343dc11 551
c24843c6 552 # Wait for hook script to clean the challenge and to deploy cert if used
d5c9dd65
LS
553 export KEY_ALGO
554 [[ -n "${HOOK}" ]] && "${HOOK}" "deploy_cert" "${domain}" "${BASEDIR}/certs/${domain}/privkey.pem" "${BASEDIR}/certs/${domain}/cert.pem" "${BASEDIR}/certs/${domain}/fullchain.pem" "${BASEDIR}/certs/${domain}/chain.pem" "${timestamp}"
c24843c6 555
556 unset challenge_token
579e2316 557 echo " + Done!"
61f0b7ed
LS
558}
559
0a859a19 560# Usage: --cron (-c)
083c6736 561# Description: Sign/renew non-existant/changed/expiring certificates.
8f6c2328 562command_sign_domains() {
9f66bfdb
LS
563 init_system
564
8f6c2328 565 if [[ -n "${PARAM_DOMAIN:-}" ]]; then
1f6a80a0 566 DOMAINS_TXT="$(_mktemp)"
93cd114f
LS
567 printf -- "${PARAM_DOMAIN}" > "${DOMAINS_TXT}"
568 elif [[ -e "${BASEDIR}/domains.txt" ]]; then
569 DOMAINS_TXT="${BASEDIR}/domains.txt"
570 else
571 _exiterr "domains.txt not found and --domain not given"
8f6c2328 572 fi
93cd114f 573
81882a64 574 # Generate certificates for all domains found in domains.txt. Check if existing certificate are about to expire
2099c77f
LS
575 ORIGIFS="${IFS}"
576 IFS=$'\n'
33f07fcc 577 for line in $(<"${DOMAINS_TXT}" tr -d '\r' | tr '[:upper:]' '[:lower:]' | _sed -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*$//g' -e 's/[[:space:]]+/ /g' | (grep -vE '^(#|$)' || true)); do
2099c77f 578 IFS="${ORIGIFS}"
81882a64 579 domain="$(printf '%s\n' "${line}" | cut -d' ' -f1)"
8f6c2328 580 morenames="$(printf '%s\n' "${line}" | cut -s -d' ' -f2-)"
81882a64 581 cert="${BASEDIR}/certs/${domain}/cert.pem"
f9126627 582
2d097c92
MG
583 force_renew="${PARAM_FORCE:-no}"
584
8f6c2328
MG
585 if [[ -z "${morenames}" ]];then
586 echo "Processing ${domain}"
587 else
93cd114f 588 echo "Processing ${domain} with alternative names: ${morenames}"
8f6c2328
MG
589 fi
590
81882a64 591 if [[ -e "${cert}" ]]; then
93cd114f 592 printf " + Checking domain name(s) of existing cert..."
2d097c92 593
f7c7d8c5
LS
594 certnames="$(openssl x509 -in "${cert}" -text -noout | grep DNS: | _sed 's/DNS://g' | tr -d ' ' | tr ',' '\n' | sort -u | tr '\n' ' ' | _sed 's/ $//')"
595 givennames="$(echo "${domain}" "${morenames}"| tr ' ' '\n' | sort -u | tr '\n' ' ' | _sed 's/ $//' | _sed 's/^ //')"
2d097c92
MG
596
597 if [[ "${certnames}" = "${givennames}" ]]; then
598 echo " unchanged."
599 else
600 echo " changed!"
601 echo " + Domain name(s) are not matching!"
602 echo " + Names in old certificate: ${certnames}"
603 echo " + Configured names: ${givennames}"
604 echo " + Forcing renew."
605 force_renew="yes"
606 fi
607 fi
608
609 if [[ -e "${cert}" ]]; then
610 echo " + Checking expire date of existing cert..."
81882a64 611 valid="$(openssl x509 -enddate -noout -in "${cert}" | cut -d= -f2- )"
8221727a 612
93cd114f 613 printf " + Valid till %s " "${valid}"
81882a64 614 if openssl x509 -checkend $((RENEW_DAYS * 86400)) -noout -in "${cert}"; then
93cd114f 615 printf "(Longer than %d days). " "${RENEW_DAYS}"
2d097c92
MG
616 if [[ "${force_renew}" = "yes" ]]; then
617 echo "Ignoring because renew was forced!"
8f6c2328 618 else
705fb54e 619 # Certificate-Names unchanged and cert is still valid
dd33de59 620 echo "Skipping renew!"
705fb54e 621 [[ -n "${HOOK}" ]] && "${HOOK}" "unchanged_cert" "${domain}" "${BASEDIR}/certs/${domain}/privkey.pem" "${BASEDIR}/certs/${domain}/cert.pem" "${BASEDIR}/certs/${domain}/fullchain.pem" "${BASEDIR}/certs/${domain}/chain.pem"
8f6c2328
MG
622 continue
623 fi
624 else
625 echo "(Less than ${RENEW_DAYS} days). Renewing!"
81882a64 626 fi
81882a64 627 fi
8221727a 628
81882a64 629 # shellcheck disable=SC2086
93cd114f 630 sign_domain ${line}
a7934fe7 631 done
f13eaa7f 632
8f6c2328 633 # remove temporary domains.txt file if used
93cd114f
LS
634 [[ -n "${PARAM_DOMAIN:-}" ]] && rm -f "${DOMAINS_TXT}"
635
636 exit 0
81882a64 637}
3390080c 638
429ec400
NL
639# Usage: --signcsr (-s) path/to/csr.pem
640# Description: Sign a given CSR, output CRT on stdout (advanced usage)
641command_sign_csr() {
642 # redirect stdout to stderr
643 # leave stdout over at fd 3 to output the cert
644 exec 3>&1 1>&2
645
646 init_system
647
648 csrfile="${1}"
649 if [ ! -r "${csrfile}" ]; then
650 _exiterr "Could not read certificate signing request ${csrfile}"
651 fi
652
653 sign_csr "$(< "${csrfile}" )"
654
655 exit 0
656}
657
0a859a19
LS
658# Usage: --revoke (-r) path/to/cert.pem
659# Description: Revoke specified certificate
81882a64 660command_revoke() {
9f66bfdb
LS
661 init_system
662
3dcfa8b4
LS
663 [[ -n "${CA_REVOKE_CERT}" ]] || _exiterr "Certificate authority doesn't allow certificate revocation."
664
81882a64 665 cert="${1}"
c7018036
MG
666 if [[ -L "${cert}" ]]; then
667 # follow symlink and use real certificate name (so we move the real file and not the symlink at the end)
3bc1cf91
LS
668 local link_target
669 link_target="$(readlink -n "${cert}")"
670 if [[ "${link_target}" =~ ^/ ]]; then
c7018036
MG
671 cert="${link_target}"
672 else
673 cert="$(dirname "${cert}")/${link_target}"
674 fi
675 fi
3dcfa8b4
LS
676 [[ -f "${cert}" ]] || _exiterr "Could not find certificate ${cert}"
677
81882a64 678 echo "Revoking ${cert}"
3dcfa8b4 679
81882a64
LS
680 cert64="$(openssl x509 -in "${cert}" -inform PEM -outform DER | urlbase64)"
681 response="$(signed_request "${CA_REVOKE_CERT}" '{"resource": "revoke-cert", "certificate": "'"${cert64}"'"}')"
3dcfa8b4 682 # if there is a problem with our revoke request _request (via signed_request) will report this and "exit 1" out
81882a64 683 # so if we are here, it is safe to assume the request was successful
3dcfa8b4
LS
684 echo " + Done."
685 echo " + Renaming certificate to ${cert}-revoked"
81882a64
LS
686 mv -f "${cert}" "${cert}-revoked"
687}
c24843c6 688
e60682c0
LS
689# Usage: --cleanup (-gc)
690# Description: Move unused certificate files to archive directory
691command_cleanup() {
dec95fff
LS
692 load_config
693
e60682c0
LS
694 # Create global archive directory if not existant
695 if [[ ! -e "${BASEDIR}/archive" ]]; then
696 mkdir "${BASEDIR}/archive"
697 fi
698
699 # Loop over all certificate directories
700 for certdir in "${BASEDIR}/certs/"*; do
f9430025
JB
701 # Skip if entry is not a folder
702 [[ -d "${certdir}" ]] || continue
703
e60682c0
LS
704 # Get certificate name
705 certname="$(basename "${certdir}")"
706
707 # Create certitifaces archive directory if not existant
708 archivedir="${BASEDIR}/archive/${certname}"
709 if [[ ! -e "${archivedir}" ]]; then
710 mkdir "${archivedir}"
711 fi
712
713 # Loop over file-types (certificates, keys, signing-requests, ...)
714 for filetype in cert.csr cert.pem chain.pem fullchain.pem privkey.pem; do
715 # Skip if symlink is broken
716 [[ -r "${certdir}/${filetype}" ]] || continue
717
718 # Look up current file in use
5c68c221 719 current="$(basename "$(readlink "${certdir}/${filetype}")")"
e60682c0
LS
720
721 # Split filetype into name and extension
722 filebase="$(echo "${filetype}" | cut -d. -f1)"
723 fileext="$(echo "${filetype}" | cut -d. -f2)"
724
725 # Loop over all files of this type
726 for file in "${certdir}/${filebase}-"*".${fileext}"; do
ac2d8303
JB
727 # Handle case where no files match the wildcard
728 [[ -f "${file}" ]] || break
729
e60682c0
LS
730 # Check if current file is in use, if unused move to archive directory
731 filename="$(basename "${file}")"
732 if [[ ! "${filename}" = "${current}" ]]; then
5c68c221 733 echo "Moving unused file to archive directory: ${certname}/${filename}"
e60682c0
LS
734 mv "${certdir}/${filename}" "${archivedir}/${filename}"
735 fi
736 done
737 done
738 done
739
740 exit 0
741}
742
0a859a19
LS
743# Usage: --help (-h)
744# Description: Show help text
81882a64 745command_help() {
7727f5ea
LS
746 printf "Usage: %s [-h] [command [argument]] [parameter [argument]] [parameter [argument]] ...\n\n" "${0}"
747 printf "Default command: help\n\n"
0a859a19 748 echo "Commands:"
760b6894 749 grep -e '^[[:space:]]*# Usage:' -e '^[[:space:]]*# Description:' -e '^command_.*()[[:space:]]*{' "${0}" | while read -r usage; read -r description; read -r command; do
31111265 750 if [[ ! "${usage}" =~ Usage ]] || [[ ! "${description}" =~ Description ]] || [[ ! "${command}" =~ ^command_ ]]; then
7727f5ea 751 _exiterr "Error generating help text."
0a859a19 752 fi
7727f5ea 753 printf " %-32s %s\n" "${usage##"# Usage: "}" "${description##"# Description: "}"
0a859a19 754 done
7727f5ea 755 printf -- "\nParameters:\n"
760b6894 756 grep -E -e '^[[:space:]]*# PARAM_Usage:' -e '^[[:space:]]*# PARAM_Description:' "${0}" | while read -r usage; read -r description; do
31111265 757 if [[ ! "${usage}" =~ Usage ]] || [[ ! "${description}" =~ Description ]]; then
7727f5ea 758 _exiterr "Error generating help text."
0a859a19 759 fi
7727f5ea 760 printf " %-32s %s\n" "${usage##"# PARAM_Usage: "}" "${description##"# PARAM_Description: "}"
0a859a19 761 done
81882a64 762}
063d28a6 763
1ab6a436
LS
764# Usage: --env (-e)
765# Description: Output configuration variables for use in other scripts
766command_env() {
767 echo "# letsencrypt.sh configuration"
9f66bfdb 768 load_config
8aa1a05b 769 typeset -p CA LICENSE CHALLENGETYPE HOOK HOOK_CHAIN RENEW_DAYS ACCOUNT_KEY ACCOUNT_KEY_JSON KEYSIZE WELLKNOWN PRIVATE_KEY_RENEW OPENSSL_CNF CONTACT_EMAIL LOCKFILE
1ab6a436
LS
770}
771
bc580335 772# Main method (parses script arguments and calls command_* methods)
9f66bfdb
LS
773main() {
774 COMMAND=""
775 set_command() {
776 [[ -z "${COMMAND}" ]] || _exiterr "Only one command can be executed at a time. See help (-h) for more information."
777 COMMAND="${1}"
778 }
779
780 check_parameters() {
781 if [[ -z "${1:-}" ]]; then
782 echo "The specified command requires additional parameters. See help:" >&2
31111265
LS
783 echo >&2
784 command_help >&2
81882a64 785 exit 1
9f66bfdb
LS
786 elif [[ "${1:0:1}" = "-" ]]; then
787 _exiterr "Invalid argument: ${1}"
788 fi
789 }
579e2316 790
2a7b4882 791 [[ -z "${@}" ]] && eval set -- "--help"
fb0242a4 792
da2eeda9 793 while (( ${#} )); do
9f66bfdb
LS
794 case "${1}" in
795 --help|-h)
796 command_help
797 exit 0
798 ;;
579e2316 799
9f66bfdb
LS
800 --env|-e)
801 set_command env
802 ;;
579e2316 803
9f66bfdb
LS
804 --cron|-c)
805 set_command sign_domains
806 ;;
807
429ec400
NL
808 --signcsr|-s)
809 shift 1
810 set_command sign_csr
811 check_parameters "${1:-}"
812 PARAM_CSR="${1}"
813 ;;
814
9f66bfdb
LS
815 --revoke|-r)
816 shift 1
817 set_command revoke
818 check_parameters "${1:-}"
819 PARAM_REVOKECERT="${1}"
820 ;;
5060dea0 821
e60682c0
LS
822 --cleanup|-gc)
823 set_command cleanup
824 ;;
825
8f6c2328 826 # PARAM_Usage: --domain (-d) domain.tld
9f66bfdb
LS
827 # PARAM_Description: Use specified domain name(s) instead of domains.txt entry (one certificate!)
828 --domain|-d)
829 shift 1
830 check_parameters "${1:-}"
831 if [[ -z "${PARAM_DOMAIN:-}" ]]; then
832 PARAM_DOMAIN="${1}"
833 else
834 PARAM_DOMAIN="${PARAM_DOMAIN} ${1}"
835 fi
836 ;;
837
838
8f6c2328 839 # PARAM_Usage: --force (-x)
9f66bfdb
LS
840 # PARAM_Description: Force renew of certificate even if it is longer valid than value in RENEW_DAYS
841 --force|-x)
842 PARAM_FORCE="yes"
843 ;;
844
0a859a19
LS
845 # PARAM_Usage: --privkey (-p) path/to/key.pem
846 # PARAM_Description: Use specified private key instead of account key (useful for revocation)
9f66bfdb
LS
847 --privkey|-p)
848 shift 1
849 check_parameters "${1:-}"
8aa1a05b 850 PARAM_ACCOUNT_KEY="${1}"
9f66bfdb
LS
851 ;;
852
853 # PARAM_Usage: --config (-f) path/to/config.sh
854 # PARAM_Description: Use specified config file
855 --config|-f)
856 shift 1
857 check_parameters "${1:-}"
858 CONFIG="${1}"
859 ;;
860
ed27e013
MG
861 # PARAM_Usage: --hook (-k) path/to/hook.sh
862 # PARAM_Description: Use specified script for hooks
863 --hook|-k)
864 shift 1
865 check_parameters "${1:-}"
866 PARAM_HOOK="${1}"
867 ;;
868
e925b293
MG
869 # PARAM_Usage: --challenge (-t) http-01|dns-01
870 # PARAM_Description: Which challenge should be used? Currently http-01 and dns-01 are supported
871 --challenge|-t)
872 shift 1
873 check_parameters "${1:-}"
874 PARAM_CHALLENGETYPE="${1}"
875 ;;
876
c71ca3a8
MG
877 # PARAM_Usage: --algo (-a) rsa|prime256v1|secp384r1
878 # PARAM_Description: Which public key algorithm should be used? Supported: rsa, prime256v1 and secp384r1
879 --algo|-a)
880 shift 1
881 check_parameters "${1:-}"
882 PARAM_KEY_ALGO="${1}"
883 ;;
884
9f66bfdb
LS
885 *)
886 echo "Unknown parameter detected: ${1}" >&2
887 echo >&2
888 command_help >&2
889 exit 1
890 ;;
891 esac
892
893 shift 1
894 done
895
896 case "${COMMAND}" in
897 env) command_env;;
898 sign_domains) command_sign_domains;;
429ec400 899 sign_csr) command_sign_csr "${PARAM_CSR}";;
9f66bfdb 900 revoke) command_revoke "${PARAM_REVOKECERT}";;
e60682c0 901 cleanup) command_cleanup;;
7191ed25 902 *) command_help; exit 1;;
81882a64 903 esac
9f66bfdb 904}
81882a64 905
c3c9ff4c
LS
906# Determine OS type
907OSTYPE="$(uname)"
908
9f66bfdb
LS
909# Check for missing dependencies
910check_dependencies
911
912# Run script
913main "${@:-}"