Skip to content

Commit 3812554

Browse files
committed
Backport readability and more improvements from 3.3dev
The opossum patch improved http_get() , http_get_header/http_head() in terms of readability. This was backported to improve maintainability. Also in pwned keys if not pwned appear now in green/OK and not just info level. HAS_UDS2 was renamed to HAS2_UDS.
1 parent 9b795df commit 3812554

File tree

1 file changed

+48
-38
lines changed

1 file changed

+48
-38
lines changed

testssl.sh

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ HAS_AES128_GCM=false
367367
HAS_AES256_GCM=false
368368
HAS_ZLIB=false
369369
HAS_UDS=false
370-
HAS_UDS2=false
370+
HAS2_UDS=false
371371
HAS_ENABLE_PHA=false
372372
HAS_DIG=false
373373
HAS_DIG_R=true
@@ -1783,12 +1783,13 @@ filter_input() {
17831783
sed -e 's/#.*$//' -e '/^$/d' <<< "$1" | tr -d '\n' | tr -d '\t' | tr -d '\r'
17841784
}
17851785

1786-
# Dl's any URL (arg1) via HTTP 1.1 GET from port 80, arg2: file to store http body.
1786+
# Dl any URL (arg1) via HTTP 1.1 GET from port 80 or 443 (curl/wget). arg2: file to store http body.
17871787
# Proxy is not honored yet (see cmd line switches) -- except when using curl or wget.
1788-
# There the environment variable is used automatically
1789-
# Currently it is being used by check_revocation_crl() only.
1788+
# The PROXY environment variable is used when specifiied
1789+
# Currently this is being used by check_revocation_crl() only.
1790+
#
17901791
http_get() {
1791-
local proto z
1792+
local proto="" foo=""
17921793
local node="" query=""
17931794
local dl="$2"
17941795
local useragent="$UA_STD"
@@ -1822,7 +1823,7 @@ http_get() {
18221823
# Worst option: slower and hiccups with chunked transfers. Workaround for the
18231824
# latter is using HTTP/1.0. We do not support https here, yet.
18241825
# First the URL will be split
1825-
IFS=/ read -r proto z node query <<< "$1"
1826+
IFS=/ read -r proto foo node query <<< "$1"
18261827
proto=${proto%:}
18271828
if [[ "$proto" != http ]]; then
18281829
pr_warning "protocol $proto not supported yet"
@@ -1841,7 +1842,7 @@ http_get() {
18411842
printf -- "%b" "GET $proto://$node/$query HTTP/1.0\r\nUser-Agent: $useragent\r\nHost: $node\r\nAccept: */*\r\n\r\n" >&33
18421843
fi
18431844
else
1844-
IFS=/ read -r proto z node query <<< "$1"
1845+
IFS=/ read -r proto foo node query <<< "$1"
18451846
exec 33<>/dev/tcp/$node/80
18461847
printf -- "%b" "GET /$query HTTP/1.0\r\nUser-Agent: $useragent\r\nHost: $node\r\nAccept: */*\r\n\r\n" >&33
18471848
fi
@@ -1858,49 +1859,55 @@ http_get() {
18581859
fi
18591860
}
18601861

1861-
# Outputs the headers when downloading any URL (arg1) via HTTP 1.1 GET from port 80.
1862+
# Outputs the HTTP headers via HTTP 1.1 HEAD command via HTTPS and a valid certificate
1863+
# arg1 is the URL
1864+
# arg2 is optional and could be a request header. curl/wget don't send empty headers otherwise
1865+
#
18621866
# Only works if curl or wget is available.
1863-
# There the environment variable is used automatically
1864-
# Currently it is being used by check_pwnedkeys() only.
1865-
http_get_header() {
1867+
# The proxy environment variable is used automatically.
1868+
# Currently it is being used by check_pwnedkeys() only
1869+
#
1870+
http_head() {
18661871
local proto
18671872
local node="" query=""
1868-
local dl="$2"
1873+
local request_header="$2"
18691874
local useragent="$UA_STD"
1870-
local jsonID="http_get_header"
1871-
local headers
1875+
local response_headers=""
1876+
local xtra_params=""
18721877
local -i ret
18731878

18741879
"$SNEAKY" && useragent="$UA_SNEAKY"
18751880

18761881
if type -p curl &>/dev/null; then
1882+
xtra_params="--connect-timeout $HEADER_MAXSLEEP --head -s"
18771883
if [[ -z "$PROXY" ]]; then
1878-
headers="$(curl --head -s --noproxy '*' -A $''"$useragent"'' "$1")"
1884+
response_headers="$(curl $xtra_params --noproxy '*' -H $''"$request_header"'' -A $''"$useragent"'' "$1")"
18791885
else
18801886
# for the sake of simplicity assume the proxy is using http
1881-
headers="$(curl --head -s -x $PROXYIP:$PROXYPORT -A $''"$useragent"'' "$1")"
1887+
response_headers="$(curl $xtra_params -x $PROXYIP:$PROXYPORT -H $''"$request_header"'' -A $''"$useragent"'' "$1")"
18821888
fi
18831889
ret=$?
1884-
[[ $ret -eq 0 ]] && tm_out "$headers"
1890+
[[ $ret -eq 0 ]] && tm_out "$response_headers"
18851891
return $ret
18861892
elif type -p wget &>/dev/null; then
1893+
xtra_params="--timeout=$HEADER_MAXSLEEP --tries=1 --cache=off"
18871894
# wget has no proxy command line. We need to use http_proxy instead. And for the sake of simplicity
18881895
# assume the GET protocol we query is using http -- http_proxy is the $ENV not for the connection TO
18891896
# the proxy, but for the protocol we query THROUGH the proxy
18901897
if [[ -z "$PROXY" ]]; then
1891-
headers="$(wget --no-proxy -q -S -U $''"$useragent"'' -O /dev/null "$1" 2>&1)"
1898+
response_headers="$(wget --no-proxy -q -S $xtra_params --header $''"$request_header"'' -U $''"$useragent"'' -O /dev/null "$1" 2>&1)"
18921899
else
18931900
if [[ -z "$http_proxy" ]]; then
1894-
headers="$(http_proxy=http://$PROXYIP:$PROXYPORT wget -q -S -U $''"$useragent"'' -O /dev/null "$1" 2>&1)"
1901+
response_headers="$(http_proxy=http://$PROXYIP:$PROXYPORT wget -q -S $xtra_params --header $''"$request_header"'' -U $''"$useragent"'' -O /dev/null "$1" 2>&1)"
18951902
else
1896-
headers="$(wget -q -S -U $''"$useragent"'' -O /dev/null "$1" 2>&1)"
1903+
response_headers="$(wget -q -S $xtra_params --header $''"$request_header"'' -U $''"$useragent"'' -O /dev/null "$1" 2>&1)"
18971904
fi
18981905
fi
18991906
ret=$?
1900-
[[ $ret -eq 0 ]] && tm_out "$headers"
1907+
[[ $ret -eq 0 ]] && tm_out "$response_headers"
19011908
# wget(1): "8: Server issued an error response.". Happens e.g. when 404 is returned. However also if the call wasn't correct (400)
19021909
# So we assume for now that everything is submitted correctly. We parse the error code too later
1903-
[[ $ret -eq 8 ]] && ret=0 && tm_out "$headers"
1910+
[[ $ret -eq 8 ]] && ret=0 && tm_out "$response_headers"
19041911
return $ret
19051912
else
19061913
return 1
@@ -1937,6 +1944,7 @@ ldap_get() {
19371944
# 1 - key not found in database
19381945
# 2 - key found in database
19391946
# 7 - network/proxy failure
1947+
#
19401948
check_pwnedkeys() {
19411949
local cert="$1"
19421950
local cert_key_algo="$2"
@@ -1966,7 +1974,7 @@ check_pwnedkeys() {
19661974
fi
19671975
fingerprint="$($OPENSSL pkey -pubin -outform DER <<< "$pubkey" 2>/dev/null | $OPENSSL dgst -sha256 -hex 2>/dev/null)"
19681976
fingerprint="${fingerprint#*= }"
1969-
response="$(http_get_header "https://v1.pwnedkeys.com/$fingerprint")"
1977+
response="$(http_head "https://v1.pwnedkeys.com/$fingerprint")"
19701978
# Handle curl's/wget's connectivity exit codes
19711979
case $? in
19721980
4|5|7) return 7 ;;
@@ -5472,6 +5480,7 @@ add_proto_offered() {
54725480
# arg1: protocol string or hex code for TLS protocol
54735481
# echos: 0 if proto known being offered, 1: known not being offered, 2: we don't know yet whether proto is being offered
54745482
# return value is always zero
5483+
#
54755484
has_server_protocol() {
54765485
local proto
54775486
local proto_val_pair
@@ -5506,6 +5515,7 @@ has_server_protocol() {
55065515

55075516

55085517
# the protocol check needs to be revamped. It sucks, see above
5518+
#
55095519
run_protocols() {
55105520
local using_sockets=true
55115521
local supported_no_ciph1="supported but couldn't detect a cipher (may need debugging)"
@@ -9873,7 +9883,7 @@ certificate_info() {
98739883
check_pwnedkeys "$HOSTCERT" "$cert_key_algo" "$cert_keysize"
98749884
case "$?" in
98759885
0) outln "not checked"; fileout "pwnedkeys${json_postfix}" "INFO" "not checked" ;;
9876-
1) outln "not in database"; fileout "pwnedkeys${json_postfix}" "INFO" "not in database" ;;
9886+
1) pr_svrty_good "not in database"; fileout "pwnedkeys${json_postfix}" "OK" "not in database" ;;
98779887
2) pr_svrty_critical "NOT ok --"; outln " key appears in database"; fileout "pwnedkeys${json_postfix}" "CRITICAL" "private key is known" ;;
98789888
7) prln_warning "error querying https://v1.pwnedkeys.com"; fileout "pwnedkeys${json_postfix}" "WARN" "connection error" ;;
98799889
esac
@@ -17290,6 +17300,7 @@ run_ccs_injection(){
1729017300

1729117301

1729217302
# see https://blog.filippo.io/finding-ticketbleed/ | https://filippo.io/ticketbleed/
17303+
#
1729317304
run_ticketbleed() {
1729417305
local tls_hexcode tls_proto=""
1729517306
local sessticket_tls="" session_tckt_tls=""
@@ -17314,7 +17325,7 @@ run_ticketbleed() {
1731417325
pr_bold " Ticketbleed"; out " ($cve), experiment. "
1731517326

1731617327
if [[ "$SERVICE" != HTTP ]] && [[ "$CLIENT_AUTH" != required ]]; then
17317-
outln "(applicable only for HTTPS)"
17328+
outln "(applicable only for HTTP service)"
1731817329
fileout "$jsonID" "INFO" "not applicable, not HTTP" "$cve" "$cwe"
1731917330
return 0
1732017331
fi
@@ -19912,7 +19923,7 @@ run_starttls_injection() {
1991219923
outln "Need socat for this check"
1991319924
return 1
1991419925
fi
19915-
if ! "$HAS_UDS2" && ! "$HAS_UDS"; then
19926+
if ! "$HAS2_UDS" && ! "$HAS_UDS"; then
1991619927
fileout "$jsonID" "WARN" "Need OpenSSL with Unix-domain socket s_client support for this check" "$cve" "$cwe" "$hint"
1991719928
outln "Need an OpenSSL with Unix-domain socket s_client support for this check"
1991819929
return 1
@@ -19936,9 +19947,9 @@ run_starttls_injection() {
1993619947
$SOCAT FD:5 UNIX-LISTEN:$uds 2>/dev/null &
1993719948
socat_pid=$!
1993819949

19939-
if "$HAS_UDS"; then
19950+
if "$HAS_DS"; then
1994019951
openssl_bin="$OPENSSL"
19941-
elif "$HAS_UDS2"; then
19952+
elif "$HAS2_UDS"; then
1994219953
openssl_bin="$OPENSSL2"
1994319954
fi
1994419955
# normally the interesting fallback we grep later for is in fd2 but we'll catch also stdout here
@@ -20696,7 +20707,7 @@ find_openssl_binary() {
2069620707
local s_client_has=$TEMPDIR/s_client_has.txt
2069720708
local s_client_has2=$TEMPDIR/s_client_has2.txt
2069820709
local s_client_starttls_has=$TEMPDIR/s_client_starttls_has.txt
20699-
local s_client_starttls_has2=$TEMPDIR/s_client_starttls_has2
20710+
local s_client2_starttls_has=$TEMPDIR/s_client2_starttls_has
2070020711
local openssl_location="" cwd=""
2070120712
local curve="" ossl_tls13_supported_curves
2070220713
local ossl_line1="" yr=""
@@ -20843,7 +20854,7 @@ find_openssl_binary() {
2084320854
HAS_AES256_GCM=false
2084420855
HAS_ZLIB=false
2084520856
HAS_UDS=false
20846-
HAS_UDS2=false
20857+
HAS2_UDS=false
2084720858
TRUSTED1ST=""
2084820859
HAS_ENABLE_PHA=false
2084920860

@@ -20882,12 +20893,11 @@ find_openssl_binary() {
2088220893

2088320894
$OPENSSL s_client -noservername </dev/null 2>&1 | grep -aiq "unknown option" || HAS_NOSERVERNAME=true
2088420895
$OPENSSL s_client -ciphersuites </dev/null 2>&1 | grep -aiq "unknown option" || HAS_CIPHERSUITES=true
20885-
20886-
$OPENSSL ciphers @SECLEVEL=0:ALL > /dev/null 2> /dev/null && HAS_SECLEVEL=true
20887-
2088820896
$OPENSSL s_client -comp </dev/null 2>&1 | grep -aiq "unknown option" || HAS_COMP=true
2088920897
$OPENSSL s_client -no_comp </dev/null 2>&1 | grep -aiq "unknown option" || HAS_NO_COMP=true
2089020898

20899+
$OPENSSL ciphers @SECLEVEL=0:ALL > /dev/null 2> /dev/null && HAS_SECLEVEL=true
20900+
2089120901
OPENSSL_NR_CIPHERS=$(count_ciphers "$(actually_supported_osslciphers 'ALL:COMPLEMENTOFALL' 'ALL')")
2089220902

2089320903
if [[ $OPENSSL_NR_CIPHERS -le 140 ]]; then
@@ -20993,9 +21003,9 @@ find_openssl_binary() {
2099321003
# We also check, whether there's $OPENSSL2 which has TLS 1.3
2099421004
if [[ ! "$OSSL_NAME" =~ LibreSSL ]] && [[ ! $OSSL_VER =~ 1.1.1 ]] && [[ $OSSL_VER_MAJOR -lt 3 ]]; then
2099521005
OPENSSL_CONF='' $OPENSSL2 s_client -help 2>$s_client_has2
20996-
OPENSSL_CONF='' $OPENSSL2 s_client -starttls foo 2>$s_client_starttls_has2
20997-
grep -q 'Unix-domain socket' $s_client_has2 && HAS_UDS2=true
20998-
grep -q 'xmpp-server' $s_client_starttls_has2 && HAS_XMPP_SERVER2=true
21006+
OPENSSL_CONF='' $OPENSSL2 s_client -starttls foo 2>$s_client2_starttls_has
21007+
grep -q 'Unix-domain socket' $s_client_has2 && HAS2_UDS=true
21008+
grep -q 'xmpp-server' $s_client2_starttls_has && HAS_XMPP_SERVER2=true
2099921009
# Likely we don't need the following second check here, see 6 lines above
2100021010
if grep -wq 'tls1_3' $s_client_has2; then
2100121011
OPENSSL_CONF='' OPENSSL2_HAS_TLS_1_3=true
@@ -21191,7 +21201,7 @@ single check as <options> ("$PROG_NAME URI" does everything except -E and -g):
2119121201
-E, --cipher-per-proto checks those per protocol
2119221202
-s, --std, --categories tests standard cipher categories by strength
2119321203
-f, --fs, --forward-secrecy checks forward secrecy settings
21194-
-p, --protocols checks TLS/SSL protocols (including SPDY/HTTP2)
21204+
-p, --protocols checks TLS/SSL protocols (including ALPN/HTTP2 and SPDY)
2119521205
-g, --grease tests several server implementation bugs like GREASE and size limitations
2119621206
-S, --server-defaults displays the server's default picks and certificate info
2119721207
-P, --server-preference displays the server's picks: protocol+cipher
@@ -21375,7 +21385,7 @@ HAS_SIEVE: $HAS_SIEVE
2137521385
HAS_NNTP: $HAS_NNTP
2137621386
HAS_IRC: $HAS_IRC
2137721387
HAS_UDS: $HAS_UDS
21378-
HAS_UDS2: $HAS_UDS2
21388+
HAS2_UDS: $HAS2_UDS
2137921389
HAS_ENABLE_PHA: $HAS_ENABLE_PHA
2138021390

2138121391
HAS_DIG: $HAS_DIG

0 commit comments

Comments
 (0)