Skip to content

Commit 896d55d

Browse files
committed
Use openssl asn1parse in sign_string
1 parent 304ef0a commit 896d55d

1 file changed

Lines changed: 22 additions & 58 deletions

File tree

getssl

Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -339,15 +339,16 @@ check_challenge_completion() { # checks with the ACME server if our challenge is
339339

340340
# if ACME response is that their check gave an invalid response, error exit
341341
if [[ "$status" == "invalid" ]] ; then
342-
err_detail=$(json_get "$response" detail)
342+
err_detail=$(echo "$response" | grep "detail")
343343
error_exit "$domain:Verify error:$err_detail"
344344
fi
345345

346346
# if ACME response is pending ( they haven't completed checks yet) then wait and try again.
347347
if [[ "$status" == "pending" ]] ; then
348348
info "Pending"
349349
else
350-
error_exit "$domain:Verify error:$response"
350+
err_detail=$(echo "$response" | grep "detail")
351+
error_exit "$domain:Verify error:$status:$err_detail"
351352
fi
352353
debug "sleep 5 secs before testing verify again"
353354
sleep 5
@@ -431,7 +432,7 @@ check_config() { # check the config files for all obvious errors
431432
info "${DOMAIN}: ACL location not specified for domain $d in $DOMAIN_DIR/getssl.cfg"
432433
config_errors=true
433434
fi
434-
# check domain exist
435+
# check domain exists
435436
if [[ "$DNS_CHECK_FUNC" == "drill" ]] || [[ "$DNS_CHECK_FUNC" == "dig" ]]; then
436437
if [[ "$($DNS_CHECK_FUNC "${d}" |grep -c "${d}")" -ge 1 ]]; then
437438
debug "found IP for ${d}"
@@ -1807,65 +1808,28 @@ sign_string() { # sign a string with a given key and algorithm and return urlbas
18071808
if openssl rsa -in "${skey}" -noout 2>/dev/null ; then # RSA key
18081809
signed64="$(printf '%s' "${str}" | openssl dgst -"$signalg" -sign "$key" | urlbase64)"
18091810
elif openssl ec -in "${skey}" -noout 2>/dev/null ; then # Elliptic curve key.
1810-
signed=$(printf '%s' "${str}" | openssl dgst -"$signalg" -sign "$key" -hex | awk '{print $2}')
1811-
debug "EC signature $signed"
1812-
if [[ "${signed:4:4}" == "021f" ]]; then #sha256 which needs padding
1813-
R=$(echo -n 00;echo "$signed" | cut -c 9-70)
1814-
part2=$(echo "$signed" | cut -c 71-)
1815-
elif [[ "${signed:4:4}" == "0220" ]]; then #sha256
1816-
R=$(echo "$signed" | cut -c 9-72)
1817-
part2=$(echo "$signed" | cut -c 73-)
1818-
elif [[ "${signed:4:4}" == "0221" ]]; then #sha256 which needs trimming
1819-
R=$(echo "$signed" | cut -c 11-74)
1820-
part2=$(echo "$signed" | cut -c 75-)
1821-
elif [[ "${signed:4:4}" == "022f" ]]; then #sha384 which needs padding
1822-
info "Padding sha384"
1823-
R=$(echo -n 00;echo "$signed" | cut -c 9-102)
1824-
part2=$(echo "$signed" | cut -c 103-)
1825-
elif [[ "${signed:4:4}" == "0230" ]]; then #sha384
1826-
R=$(echo "$signed" | cut -c 9-104)
1827-
part2=$(echo "$signed" | cut -c 105-)
1828-
elif [[ "${signed:4:4}" == "0231" ]]; then #sha384 which needs trimming
1829-
R=$(echo "$signed" | cut -c 11-106)
1830-
part2=$(echo "$signed" | cut -c 107-)
1831-
elif [[ "${signed:6:4}" == "0240" ]]; then #sha512 which needs padding
1832-
R=$(echo -n 00;echo "$signed" | cut -c 9-138)
1833-
part2=$(echo "$signed" | cut -c 141-)
1834-
elif [[ "${signed:6:4}" == "0241" ]]; then #sha512 which needs padding
1835-
R=$(echo -n 00;echo "$signed" | cut -c 11-140)
1836-
part2=$(echo "$signed" | cut -c 141-)
1837-
elif [[ "${signed:6:4}" == "0242" ]]; then #sha512
1838-
R=$(echo "$signed" | cut -c 11-142)
1839-
part2=$(echo "$signed" | cut -c 143-)
1811+
# ECDSA signature width
1812+
# e.g. 521 bits requires 66 bytes to express, a signature consists of 2 integers so 132 bytes
1813+
# https://crypto.stackexchange.com/questions/12299/ecc-key-size-and-signature-size/
1814+
if [ "$signalg" = "sha256" ]; then
1815+
w=64
1816+
elif [ "$signalg" = "sha384" ]; then
1817+
w=96
1818+
elif [ "$signalg" = "sha512" ]; then
1819+
w=132
18401820
else
1841-
error_exit "error in EC signing couldn't get R from $signed"
1821+
error_exit "Unknown signing algorithm $signalg"
18421822
fi
1823+
asn1parse=$(printf '%s' "${str}" | openssl dgst -"$signalg" -sign "$key" | openssl asn1parse -inform DER)
1824+
#shellcheck disable=SC2086
1825+
R=$(echo $asn1parse | awk '{ print $13 }' | cut -c2-)
18431826
debug "R $R"
1844-
1845-
if [[ "${part2:0:4}" == "021f" ]]; then #sha256 with padding
1846-
S=$(echo -n 00;echo "$part2" | cut -c 5-)
1847-
elif [[ "${part2:0:4}" == "0220" ]]; then #sha256
1848-
S=$(echo "$part2" | cut -c 5-68)
1849-
elif [[ "${part2:0:4}" == "0221" ]]; then #sha256
1850-
S=$(echo "$part2" | cut -c 7-70)
1851-
elif [[ "${part2:0:4}" == "022f" ]]; then #sha384 with padding
1852-
S=$(echo -n 00;echo "$part2" | cut -c 5-)
1853-
elif [[ "${part2:0:4}" == "0230" ]]; then #sha384
1854-
S=$(echo "$part2" | cut -c 5-100)
1855-
elif [[ "${part2:0:4}" == "0231" ]]; then #sha384
1856-
S=$(echo "$part2" | cut -c 7-102)
1857-
elif [[ "${part2:0:4}" == "0240" ]]; then #sha512 with padding
1858-
S=$(echo -n 00;echo "$part2" | cut -c 5-)
1859-
elif [[ "${part2:0:4}" == "0241" ]]; then #sha512 with padding
1860-
S=$(echo -n 00;echo "$part2" | cut -c 5-)
1861-
elif [[ "${part2:0:4}" == "0242" ]]; then #sha512
1862-
S=$(echo "$part2" | cut -c 5-)
1863-
else
1864-
error_exit "error in EC signing couldn't get S from $signed"
1865-
fi
1866-
1827+
#shellcheck disable=SC2086
1828+
S=$(echo $asn1parse | awk '{ print $20 }' | cut -c2-)
18671829
debug "S $S"
1868-
signed64=$(printf '%s' "${R}${S}" | hex2bin | urlbase64 )
1830+
1831+
# pad R and S to the correct length for the signing algorithm
1832+
signed64=$(printf "%${w}s%${w}s" "${R}" "${S}" | tr ' ' '0' | hex2bin | urlbase64 )
18691833
debug "encoded RS $signed64"
18701834
fi
18711835
}

0 commit comments

Comments
 (0)