2
0
mirror of https://github.com/mozilla/cipherscan.git synced 2024-11-22 14:23:41 +01:00

Merge pull request #77 from floatingatoll/bashisms

Various bash cleanups and one bugfix
This commit is contained in:
Julien Vehent 2015-09-03 10:13:30 -04:00
commit 3f3e22b09a

View File

@ -10,16 +10,23 @@
DOBENCHMARK=0 DOBENCHMARK=0
BENCHMARKITER=30 BENCHMARKITER=30
REALPATH=$(dirname $0) DIRNAMEPATH=$(dirname "$0")
REALPATH="$DIRNAMEPATH"
# make sure this doesn't error out when readlink -f isn't available (OSX) # make sure this doesn't error out when readlink -f isn't available (OSX)
readlink -f $0 &>/dev/null && REALPATH=$(dirname $(readlink -f $0)) readlink -f "$0" &>/dev/null && REALPATH=$(dirname "$(readlink -f "$0")")
OPENSSLBIN="${REALPATH}/openssl" if [[ "$(uname -s)" == "Darwin" ]]; then
if [ "$(uname -s)" == "Darwin" ]; then
OPENSSLBIN="${REALPATH}/openssl-darwin64" OPENSSLBIN="${REALPATH}/openssl-darwin64"
else
OPENSSLBIN="${REALPATH}/openssl"
fi
OPENSSLBINHELP="$($OPENSSLBIN s_client -help 2>&1)"
if ! [[ $OPENSSLBINHELP =~ -connect ]]; then
echo "$OPENSSLBIN s_client doesn't accept the -connect parameter, which is extremely strange; refusing to proceed." 1>&2
exit 1
fi fi
# cipherscan requires bash4, which doesn't come by default in OSX # cipherscan requires bash4, which doesn't come by default in OSX
if [ ${BASH_VERSINFO[0]} -lt 4 ]; then if [[ ${BASH_VERSINFO[0]} -lt 4 ]]; then
echo "Bash version 4 is required to run cipherscan." echo "Bash version 4 is required to run cipherscan."
echo "Please upgrade your version of bash (ex: brew install bash)." echo "Please upgrade your version of bash (ex: brew install bash)."
exit 1 exit 1
@ -28,36 +35,36 @@ fi
# test that timeout or gtimeout (darwin) are present # test that timeout or gtimeout (darwin) are present
TIMEOUTBIN="$(which timeout)" TIMEOUTBIN="$(which timeout)"
if [ "$TIMEOUTBIN" == "" ]; then if [[ "$TIMEOUTBIN" == "" ]]; then
TIMEOUTBIN="$(which gtimeout)" TIMEOUTBIN="$(which gtimeout)"
if [ "$TIMEOUTBIN" == "" ]; then if [[ "$TIMEOUTBIN" == "" ]]; then
echo "neither timeout nor gtimeout are present. install coreutils with {apt-get,yum,brew} install coreutils" echo "neither timeout nor gtimeout are present. install coreutils with {apt-get,yum,brew} install coreutils"
exit 1 exit 1
fi fi
fi fi
# Check for busybox, which has different arguments # Check for busybox, which has different arguments
TIMEOUTOUTPUT=$(($TIMEOUTBIN --help) 2>&1) TIMEOUTOUTPUT="$($TIMEOUTBIN --help 2>&1)"
if [[ "$TIMEOUTOUTPUT" =~ BusyBox ]]; then if [[ "$TIMEOUTOUTPUT" =~ BusyBox ]]; then
TIMEOUTBIN="$TIMEOUTBIN -t" TIMEOUTBIN="$TIMEOUTBIN -t"
fi fi
# use custom config file to enable GOST ciphers # use custom config file to enable GOST ciphers
if [[ -e $(dirname $0)/openssl.cnf ]]; then if [[ -e $DIRNAMEPATH/openssl.cnf ]]; then
export OPENSSL_CONF="$(dirname $0)/openssl.cnf" export OPENSSL_CONF="$DIRNAMEPATH/openssl.cnf"
fi fi
# find a list of trusted CAs on the local system, or use the provided list # find a list of trusted CAs on the local system, or use the provided list
if [ -z "$CACERTS" ]; then if [[ -z "$CACERTS" ]]; then
for f in /etc/pki/tls/certs/ca-bundle.crt /etc/ssl/certs/ca-certificates.crt; do for f in /etc/pki/tls/certs/ca-bundle.crt /etc/ssl/certs/ca-certificates.crt; do
if [ -e "$f" ]; then if [[ -e "$f" ]]; then
CACERTS="$f" CACERTS="$f"
break break
fi fi
done done
fi fi
if [ ! -e "$CACERTS" ]; then if [[ ! -e "$CACERTS" ]]; then
CACERTS="$(dirname $0)/ca-bundle.crt" CACERTS="$DIRNAMEPATH/ca-bundle.crt"
fi fi
# RSA ciphers are put at the end to force Google servers to accept ECDSA ciphers # RSA ciphers are put at the end to force Google servers to accept ECDSA ciphers
@ -65,7 +72,8 @@ fi
CIPHERSUITE="ALL:COMPLEMENTOFALL:+aRSA" CIPHERSUITE="ALL:COMPLEMENTOFALL:+aRSA"
# some servers are intolerant to large client hello, try a shorter list of # some servers are intolerant to large client hello, try a shorter list of
# ciphers with them # ciphers with them
SHORTCIPHERSUITE=('ECDHE-ECDSA-AES128-GCM-SHA256' SHORTCIPHERSUITE=(
'ECDHE-ECDSA-AES128-GCM-SHA256'
'ECDHE-RSA-AES128-GCM-SHA256' 'ECDHE-RSA-AES128-GCM-SHA256'
'ECDHE-RSA-AES256-GCM-SHA384' 'ECDHE-RSA-AES256-GCM-SHA384'
'ECDHE-ECDSA-AES256-SHA' 'ECDHE-ECDSA-AES256-SHA'
@ -88,7 +96,8 @@ SHORTCIPHERSUITE=('ECDHE-ECDSA-AES128-GCM-SHA256'
'CAMELLIA256-SHA' 'CAMELLIA256-SHA'
'DES-CBC3-SHA' 'DES-CBC3-SHA'
'RC4-SHA' 'RC4-SHA'
'RC4-MD5') 'RC4-MD5'
)
# as some servers are intolerant to large client hello's (or ones that have # as some servers are intolerant to large client hello's (or ones that have
# RC4 ciphers below position 64), use the following for cipher testing in case # RC4 ciphers below position 64), use the following for cipher testing in case
# of problems # of problems
@ -126,7 +135,7 @@ FALLBACKCIPHERSUITE=(
'EXP-DES-CBC-SHA' 'EXP-DES-CBC-SHA'
'EXP-RC2-CBC-MD5' 'EXP-RC2-CBC-MD5'
'EXP-RC4-MD5' 'EXP-RC4-MD5'
) )
DEBUG=0 DEBUG=0
VERBOSE=0 VERBOSE=0
DELAY=0 DELAY=0
@ -192,65 +201,69 @@ EXAMPLES: $0 -starttls xmpp jabber.ccc.de:5222
} }
verbose() { verbose() {
if [ $VERBOSE != 0 ]; then if [[ $VERBOSE != 0 ]]; then
echo "$@" >&2 echo "$@" >&2
fi fi
} }
debug(){ debug(){
if [ $DEBUG == 1 ]; then if [[ $DEBUG == 1 ]]; then
echo Debug: "$@" >&2 echo Debug: "$@" >&2
set -evx set -evx
fi fi
} }
# obtain an array of curves supported by openssl # obtain an array of curves supported by openssl
CURVES=(sect163k1 # K-163 CURVES=(
sect163r1 'sect163k1' # K-163
sect163r2 # B-163 'sect163r1'
sect193r1 'sect163r2' # B-163
sect193r2 'sect193r1'
sect233k1 # K-233 'sect193r2'
sect233r1 # B-233 'sect233k1' # K-233
sect239k1 'sect233r1' # B-233
sect283k1 # K-283 'sect239k1'
sect283r1 # B-283 'sect283k1' # K-283
sect409k1 # K-409 'sect283r1' # B-283
sect409r1 # B-409 'sect409k1' # K-409
sect571k1 # K-571 'sect409r1' # B-409
sect571r1 # B-571 'sect571k1' # K-571
secp160k1 'sect571r1' # B-571
secp160r1 'secp160k1'
secp160r2 'secp160r1'
secp192k1 'secp160r2'
prime192v1 # P-192 secp192r1 'secp192k1'
secp224k1 'prime192v1' # P-192 secp192r1
secp224r1 # P-224 'secp224k1'
secp256k1 'secp224r1' # P-224
prime256v1 # P-256 secp256r1 'secp256k1'
secp384r1 # P-384 'prime256v1' # P-256 secp256r1
secp521r1 # P-521 'secp384r1' # P-384
brainpoolP256r1 'secp521r1' # P-521
brainpoolP384r1 'brainpoolP256r1'
brainpoolP512r1) 'brainpoolP384r1'
'brainpoolP512r1'
)
# many curves have alternative names, this array provides a mapping to find the IANA # many curves have alternative names, this array provides a mapping to find the IANA
# name of a curve using its alias # name of a curve using its alias
CURVES_MAP=("sect163k1 K-163" CURVES_MAP=(
"sect163r2 B-163" 'sect163k1 K-163'
"sect233k1 K-233" 'sect163r2 B-163'
"sect233r1 B-233" 'sect233k1 K-233'
"sect283k1 K-283" 'sect233r1 B-233'
"sect283r1 B-283" 'sect283k1 K-283'
"sect409k1 K-409" 'sect283r1 B-283'
"sect409r1 B-409" 'sect409k1 K-409'
"sect571k1 K-571" 'sect409r1 B-409'
"sect571r1 B-571" 'sect571k1 K-571'
"prime192v1 P-192 secp192r1" 'sect571r1 B-571'
"secp224r1 P-224" 'prime192v1 P-192 secp192r1'
"prime256v1 P-256 secp256r1" 'secp224r1 P-224'
"secp384r1 P-384" 'prime256v1 P-256 secp256r1'
"secp521r1 P-521") 'secp384r1 P-384'
'secp521r1 P-521'
)
get_curve_name() { get_curve_name() {
local identifier=$1 local identifier=$1
@ -275,26 +288,20 @@ c_hash() {
fi fi
if [[ ! -e $1/${h}.${num} ]]; then if [[ ! -e $1/${h}.${num} ]]; then
# file doesn't exist, create a link # file doesn't exist, create a link
pushd "$1" > /dev/null if pushd "$1" > /dev/null; then
ln -s "$2" "${h}.${num}" ln -s "$2" "${h}.${num}"
else
echo "'pushd $1' failed unexpectedly, refusing to proceed" 1>&2
exit 1
fi
popd > /dev/null popd > /dev/null
break break
fi fi
done done
} }
crude_grep() {
while read line; do
if [[ $line =~ $1 ]]; then
return 0
fi
done
return 1
}
check_option_support() { check_option_support() {
$OPENSSLBIN s_client -help 2>&1 | crude_grep "$1" [[ $OPENSSLBINHELP =~ "$1" ]]
return $?
} }
parse_openssl_output() { parse_openssl_output() {
@ -416,7 +423,7 @@ test_cipher_on_target() {
# sslv2 client hello doesn't support SNI extension # sslv2 client hello doesn't support SNI extension
# in SSLv3 mode OpenSSL just ignores the setting so it's ok # in SSLv3 mode OpenSSL just ignores the setting so it's ok
# -status exception is ignored in SSLv2, go figure # -status exception is ignored in SSLv2, go figure
if [ "$tls_version" == "-ssl2" ]; then if [[ "$tls_version" == "-ssl2" ]]; then
if [[ "$sslcommand" =~ (.*)(-servername\ [^ ]*)(.*) ]]; then if [[ "$sslcommand" =~ (.*)(-servername\ [^ ]*)(.*) ]]; then
cmnd="${BASH_REMATCH[1]} ${BASH_REMATCH[3]}" cmnd="${BASH_REMATCH[1]} ${BASH_REMATCH[3]}"
else else
@ -438,7 +445,7 @@ test_cipher_on_target() {
local certificate_count=$certs_found local certificate_count=$certs_found
debug "server presented $certificate_count certificates" debug "server presented $certificate_count certificates"
local i local i
for ((i=0; i<$certificate_count; i=i+1 )); do for ((i=0; i<certificate_count; i=i+1 )); do
# extract i'th certificate # extract i'th certificate
local cert="${current_raw_certificates[$i]}" local cert="${current_raw_certificates[$i]}"
@ -447,7 +454,7 @@ test_cipher_on_target() {
# compare the values not just checksums so that eventual collision # compare the values not just checksums so that eventual collision
# doesn't mess up results # doesn't mess up results
if [[ ${known_certs[$cksum]} == $cert ]]; then if [[ ${known_certs[$cksum]} == $cert ]]; then
if [ -n "${current_certificates}" ]; then if [[ -n "${current_certificates}" ]]; then
current_certificates+="," current_certificates+=","
fi fi
current_certificates+="\"${cert_checksums[$cksum]}\"" current_certificates+="\"${cert_checksums[$cksum]}\""
@ -498,7 +505,7 @@ test_cipher_on_target() {
fi fi
fi fi
# save the sha sum for reporting # save the sha sum for reporting
if [ -n "${current_certificates}" ]; then if [[ -n "${current_certificates}" ]]; then
current_certificates+="," current_certificates+=","
fi fi
current_certificates+="\"${sha256sum}\"" current_certificates+="\"${sha256sum}\""
@ -515,20 +522,20 @@ test_cipher_on_target() {
verbose "connection successful; protocol: $current_protocol, cipher: $current_cipher, previous cipher: $previous_cipher" verbose "connection successful; protocol: $current_protocol, cipher: $current_cipher, previous cipher: $previous_cipher"
fi fi
# handling of TLSv1.2 only cipher suites # handling of TLSv1.2 only cipher suites
if [ ! -z "$previous_cipher" ] && [ "$previous_cipher" != "$current_cipher" ] && [ "$current_cipher" != "0000" ]; then if [[ ! -z "$previous_cipher" ]] && [[ "$previous_cipher" != "$current_cipher" ]] && [[ "$current_cipher" != "0000" ]]; then
unset protocols unset protocols
fi fi
previous_cipher=$current_cipher previous_cipher=$current_cipher
# connection succeeded, add TLS version to positive results # connection succeeded, add TLS version to positive results
if [ -z "$protocols" ]; then if [[ -z "$protocols" ]]; then
protocols=$current_protocol protocols=$current_protocol
else else
protocols="$protocols,$current_protocol" protocols="$protocols,$current_protocol"
fi fi
cipher=$current_cipher cipher=$current_cipher
pfs=$current_pfs pfs=$current_pfs
[ -z $pfs ] && pfs="None" [[ -z $pfs ]] && pfs="None"
pubkey=$current_pubkey pubkey=$current_pubkey
sigalg=$current_sigalg sigalg=$current_sigalg
trusted=$current_trusted trusted=$current_trusted
@ -539,13 +546,13 @@ test_cipher_on_target() {
done done
# if cipher is empty, that means none of the TLS version worked with # if cipher is empty, that means none of the TLS version worked with
# the current cipher # the current cipher
if [ -z "$cipher" ]; then if [[ -z "$cipher" ]]; then
verbose "handshake failed, no ciphersuite was returned" verbose "handshake failed, no ciphersuite was returned"
result='ConnectionFailure' result='ConnectionFailure'
return 2 return 2
# if cipher contains NONE, the cipher wasn't accepted # if cipher contains NONE, the cipher wasn't accepted
elif [ "$cipher" == '(NONE) ' ]; then elif [[ "$cipher" == '(NONE) ' ]]; then
result="$cipher $protocols $pubkey $sigalg $trusted $tickethint $ocspstaple $pfs $current_curves $curves_ordering" result="$cipher $protocols $pubkey $sigalg $trusted $tickethint $ocspstaple $pfs $current_curves $curves_ordering"
verbose "handshake failed, server returned ciphersuite '$result'" verbose "handshake failed, server returned ciphersuite '$result'"
return 1 return 1
@ -556,9 +563,9 @@ test_cipher_on_target() {
# if pfs uses ECDH, test supported curves # if pfs uses ECDH, test supported curves
if [[ $pfs =~ ECDH ]]; then if [[ $pfs =~ ECDH ]]; then
has_curves="True" has_curves="True"
if [ $TEST_CURVES == "True" ]; then if [[ $TEST_CURVES == "True" ]]; then
test_curves test_curves
if [ "$ecc_ciphers" != "" ]; then if [[ "$ecc_ciphers" != "" ]]; then
ecc_ciphers+=":" ecc_ciphers+=":"
fi fi
ecc_ciphers+="$cipher" ecc_ciphers+="$cipher"
@ -582,7 +589,7 @@ bench_cipher() {
for i in $(seq 1 $BENCHMARKITER); do for i in $(seq 1 $BENCHMARKITER); do
debug Connection $i debug Connection $i
(echo "Q" | $sslcommand 2>/dev/null 1>/dev/null) (echo "Q" | $sslcommand 2>/dev/null 1>/dev/null)
if [ $? -gt 0 ]; then if [[ $? -gt 0 ]]; then
break break
fi fi
done done
@ -590,19 +597,19 @@ bench_cipher() {
local t="$(($(date +%s%N) - t))" local t="$(($(date +%s%N) - t))"
verbose "Benchmarking done in $t nanoseconds" verbose "Benchmarking done in $t nanoseconds"
# Microseconds # Microseconds
cipherbenchms="$((t/1000/$BENCHMARKITER))" cipherbenchms="$((t/1000/BENCHMARKITER))"
} }
# Connect to the target and retrieve the chosen cipher # Connect to the target and retrieve the chosen cipher
# recursively until the connection fails # recursively until the connection fails
get_cipher_pref() { get_cipher_pref() {
[ "$OUTPUTFORMAT" == "terminal" ] && [ $DEBUG -lt 1 ] && echo -n '.' [[ "$OUTPUTFORMAT" == "terminal" ]] && [[ $DEBUG -lt 1 ]] && echo -n '.'
local ciphersuite="$1" local ciphersuite="$1"
local sslcommand="$TIMEOUTBIN $TIMEOUT $OPENSSLBIN s_client" local sslcommand="$TIMEOUTBIN $TIMEOUT $OPENSSLBIN s_client"
if [ -n "$CAPATH" ]; then if [[ -n "$CAPATH" ]]; then
sslcommand+=" -CApath $CAPATH -showcerts" sslcommand+=" -CApath $CAPATH -showcerts"
elif [ -e $CACERTS ]; then elif [[ -e $CACERTS ]]; then
sslcommand+=" -CAfile $CACERTS" sslcommand+=" -CAfile $CACERTS"
fi fi
sslcommand+=" -status $SCLIENTARGS -connect $TARGET -cipher $ciphersuite" sslcommand+=" -status $SCLIENTARGS -connect $TARGET -cipher $ciphersuite"
@ -611,7 +618,7 @@ get_cipher_pref() {
test_cipher_on_target "$sslcommand" test_cipher_on_target "$sslcommand"
local success=$? local success=$?
# If the connection succeeded with the current cipher, benchmark and store # If the connection succeeded with the current cipher, benchmark and store
if [ $success -eq 0 ]; then if [[ $success -eq 0 ]]; then
cipherspref=("${cipherspref[@]}" "$result") cipherspref=("${cipherspref[@]}" "$result")
ciphercertificates=("${ciphercertificates[@]}" "$certificates") ciphercertificates=("${ciphercertificates[@]}" "$certificates")
pciph=($result) pciph=($result)
@ -634,14 +641,14 @@ display_results_in_terminal() {
for cipher in "${cipherspref[@]}"; do for cipher in "${cipherspref[@]}"; do
# get first in array # get first in array
pciph=($cipher) pciph=($cipher)
if [ $DOBENCHMARK -eq 1 ]; then if [[ $DOBENCHMARK -eq 1 ]]; then
bench_cipher "$pciph" bench_cipher "$pciph"
r="$ctr $cipher $cipherbenchms" r="$ctr $cipher $cipherbenchms"
else else
r="$ctr $cipher" r="$ctr $cipher"
fi fi
local cipher_data=($cipher) local cipher_data=($cipher)
if [ $ctr -eq 1 ]; then if [[ $ctr -eq 1 ]]; then
pubkey="${cipher_data[2]}" pubkey="${cipher_data[2]}"
sigalg="${cipher_data[3]}" sigalg="${cipher_data[3]}"
trusted="${cipher_data[4]}" trusted="${cipher_data[4]}"
@ -651,19 +658,19 @@ display_results_in_terminal() {
curvesordering="${cipher_data[9]}" curvesordering="${cipher_data[9]}"
fi fi
else else
if [ "$pubkey" != "${cipher_data[2]}" ]; then if [[ "$pubkey" != "${cipher_data[2]}" ]]; then
different=True different=True
fi fi
if [ "$sigalg" != "${cipher_data[3]}" ]; then if [[ "$sigalg" != "${cipher_data[3]}" ]]; then
different=True different=True
fi fi
if [ "$trusted" != "${cipher_data[4]}" ]; then if [[ "$trusted" != "${cipher_data[4]}" ]]; then
different=True different=True
fi fi
if [ "$tickethint" != "${cipher_data[5]}" ]; then if [[ "$tickethint" != "${cipher_data[5]}" ]]; then
different=True different=True
fi fi
if [ "$ocspstaple" != "${cipher_data[6]}" ]; then if [[ "$ocspstaple" != "${cipher_data[6]}" ]]; then
different=True different=True
fi fi
if [[ "$curvesordering" == "" && "${cipher_data[9]}" != "" ]]; then if [[ "$curvesordering" == "" && "${cipher_data[9]}" != "" ]]; then
@ -678,26 +685,26 @@ display_results_in_terminal() {
done done
header="prio ciphersuite protocols" header="prio ciphersuite protocols"
if [ $different == "True" ]; then if [[ $different == "True" ]]; then
header+=" pubkey_size signature_algoritm trusted ticket_hint ocsp_staple" header+=" pubkey_size signature_algoritm trusted ticket_hint ocsp_staple"
fi fi
header+=" pfs" header+=" pfs"
if [ $has_curves == "True" ]; then if [[ $has_curves == "True" ]]; then
header+=" curves" header+=" curves"
if [[ $TEST_CURVES == "True" && $different == "True" ]]; then if [[ $TEST_CURVES == "True" && $different == "True" ]]; then
header+=" curves_ordering" header+=" curves_ordering"
fi fi
fi fi
if [ $DOBENCHMARK -eq 1 ]; then if [[ $DOBENCHMARK -eq 1 ]]; then
header+=" avg_handshake_microsec" header+=" avg_handshake_microsec"
fi fi
ctr=0 ctr=0
for result in "${results[@]}"; do for result in "${results[@]}"; do
if [ $ctr -eq 0 ]; then if [[ $ctr -eq 0 ]]; then
echo $header echo $header
ctr=$((ctr+1)) ctr=$((ctr+1))
fi fi
if [ $different == "True" ]; then if [[ $different == "True" ]]; then
echo $result|grep -v '(NONE)' echo $result|grep -v '(NONE)'
else else
# prints priority, ciphersuite, protocols and pfs # prints priority, ciphersuite, protocols and pfs
@ -705,8 +712,8 @@ display_results_in_terminal() {
fi fi
done|column -t done|column -t
echo echo
if [ $different != "True" ]; then if [[ $different != "True" ]]; then
if [ "$trusted" == "True" ]; then if [[ "$trusted" == "True" ]]; then
echo "Certificate: trusted, $pubkey bit, $sigalg signature" echo "Certificate: trusted, $pubkey bit, $sigalg signature"
else else
echo "Certificate: UNTRUSTED, $pubkey bit, $sigalg signature" echo "Certificate: UNTRUSTED, $pubkey bit, $sigalg signature"
@ -723,7 +730,7 @@ display_results_in_terminal() {
else else
echo "Cipher ordering: client" echo "Cipher ordering: client"
fi fi
if [ $TEST_CURVES == "True" ]; then if [[ $TEST_CURVES == "True" ]]; then
echo "Curves ordering: $curvesordering" echo "Curves ordering: $curvesordering"
echo "Curves fallback: $fallback_supported" echo "Curves fallback: $fallback_supported"
fi fi
@ -746,7 +753,7 @@ display_results_in_json() {
echo -n "{\"target\":\"$TARGET\",\"utctimestamp\":\"$(date -u '+%FT%T.0Z')\",\"serverside\":\"${serverside}\",\"ciphersuite\": [" echo -n "{\"target\":\"$TARGET\",\"utctimestamp\":\"$(date -u '+%FT%T.0Z')\",\"serverside\":\"${serverside}\",\"ciphersuite\": ["
for cipher in "${cipherspref[@]}"; do for cipher in "${cipherspref[@]}"; do
local cipher_arr=($cipher) local cipher_arr=($cipher)
[ $ctr -gt 0 ] && echo -n ',' [[ $ctr -gt 0 ]] && echo -n ','
echo -n "{\"cipher\":\"${cipher_arr[0]}\"," echo -n "{\"cipher\":\"${cipher_arr[0]}\","
echo -n "\"protocols\":[\"${cipher_arr[1]//,/\",\"}\"]," echo -n "\"protocols\":[\"${cipher_arr[1]//,/\",\"}\"],"
echo -n "\"pubkey\":[\"${cipher_arr[2]//,/\",\"}\"]," echo -n "\"pubkey\":[\"${cipher_arr[2]//,/\",\"}\"],"
@ -758,12 +765,12 @@ display_results_in_json() {
echo -n "\"ticket_hint\":\"${cipher_arr[5]}\"," echo -n "\"ticket_hint\":\"${cipher_arr[5]}\","
echo -n "\"ocsp_stapling\":\"${cipher_arr[6]}\"," echo -n "\"ocsp_stapling\":\"${cipher_arr[6]}\","
pfs="${cipher_arr[7]}" pfs="${cipher_arr[7]}"
[ "$pfs" == "" ] && pfs="None" [[ "$pfs" == "" ]] && pfs="None"
echo -n "\"pfs\":\"$pfs\"" echo -n "\"pfs\":\"$pfs\""
if [[ "${cipher_arr[0]}" =~ ECDH ]]; then if [[ "${cipher_arr[0]}" =~ ECDH ]]; then
echo -n "," echo -n ","
echo -n "\"curves\":[\"${cipher_arr[8]//,/\",\"}\"]" echo -n "\"curves\":[\"${cipher_arr[8]//,/\",\"}\"]"
if [ $TEST_CURVES == "True" ]; then if [[ $TEST_CURVES == "True" ]]; then
echo -n "," echo -n ","
echo -n "\"curves_ordering\":\"${cipher_arr[9]}\"" echo -n "\"curves_ordering\":\"${cipher_arr[9]}\""
fi fi
@ -772,14 +779,14 @@ display_results_in_json() {
ctr=$((ctr+1)) ctr=$((ctr+1))
done done
echo -n ']' echo -n ']'
if [ $TEST_CURVES == "True" ]; then if [[ $TEST_CURVES == "True" ]]; then
echo -n ",\"curves_fallback\":\"$fallback_supported\"" echo -n ",\"curves_fallback\":\"$fallback_supported\""
fi fi
echo -n ',"configs":{' echo -n ',"configs":{'
ctr=0 ctr=0
for test_name in "${!tls_tolerance[@]}"; do for test_name in "${!tls_tolerance[@]}"; do
local result=(${tls_tolerance[$test_name]}) local result=(${tls_tolerance[$test_name]})
[ $ctr -gt 0 ] && echo -n "," [[ $ctr -gt 0 ]] && echo -n ","
echo -n "\"$test_name\":{" echo -n "\"$test_name\":{"
if [[ ${result[0]} == "False" ]]; then if [[ ${result[0]} == "False" ]]; then
echo -n "\"tolerant\":\"False\"" echo -n "\"tolerant\":\"False\""
@ -826,15 +833,15 @@ test_serverside_ordering() {
fi fi
local sslcommand="$TIMEOUTBIN $TIMEOUT $OPENSSLBIN s_client" local sslcommand="$TIMEOUTBIN $TIMEOUT $OPENSSLBIN s_client"
if [ -n "$CAPATH" ]; then if [[ -n "$CAPATH" ]]; then
sslcommand+=" -CApath $CAPATH -showcerts" sslcommand+=" -CApath $CAPATH -showcerts"
elif [ -e "$CACERTS" ]; then elif [[ -e "$CACERTS" ]]; then
sslcommand+=" -CAfile $CACERTS" sslcommand+=" -CAfile $CACERTS"
fi fi
sslcommand+=" -status $SCLIENTARGS -connect $TARGET -cipher $ciphersuite" sslcommand+=" -status $SCLIENTARGS -connect $TARGET -cipher $ciphersuite"
test_cipher_on_target "$sslcommand" test_cipher_on_target "$sslcommand"
if [ $? -ne 0 ]; then if [[ $? -ne 0 ]]; then
serverside="True" serverside="True"
else else
local selected=($result) local selected=($result)
@ -847,9 +854,6 @@ test_serverside_ordering() {
} }
test_curves() { test_curves() {
# "True" if server supports ciphers that don't use ECC at a lower priority
local fallback_available="False"
# return variable: list of curves supported by server, in order # return variable: list of curves supported by server, in order
current_curves="" current_curves=""
# return variable: check if server uses server side or client side ordering # return variable: check if server uses server side or client side ordering
@ -866,9 +870,9 @@ test_curves() {
# prepare the ssl command we'll be using # prepare the ssl command we'll be using
local sslcommand="" local sslcommand=""
sslcommand="$TIMEOUTBIN $TIMEOUT $OPENSSLBIN s_client" sslcommand="$TIMEOUTBIN $TIMEOUT $OPENSSLBIN s_client"
if [ -n "$CAPATH" ]; then if [[ -n "$CAPATH" ]]; then
sslcommand+=" -CApath $CAPATH -showcerts" sslcommand+=" -CApath $CAPATH -showcerts"
elif [ -e "$CACERTS" ]; then elif [[ -e "$CACERTS" ]]; then
sslcommand+=" -CAfile $CACERTS" sslcommand+=" -CAfile $CACERTS"
fi fi
sslcommand+=" -status $SCLIENTARGS -connect $TARGET -cipher $current_cipher" sslcommand+=" -status $SCLIENTARGS -connect $TARGET -cipher $current_cipher"
@ -900,7 +904,7 @@ test_curves() {
local ephem_data=(${current_pfs//,/ }) local ephem_data=(${current_pfs//,/ })
local cname="" local cname=""
if [[ ${ephem_data[0]} =~ ECDH ]]; then if [[ ${ephem_data[0]} =~ ECDH ]]; then
if [ "$current_curves" != "" ]; then if [[ "$current_curves" != "" ]]; then
current_curves+="," current_curves+=","
fi fi
cname="$(get_curve_name ${ephem_data[1]})" cname="$(get_curve_name ${ephem_data[1]})"
@ -908,14 +912,14 @@ test_curves() {
current_curves+="$cname" current_curves+="$cname"
fi fi
for id in "${!curves[@]}"; do for id in "${!curves[@]}"; do
if [ "$cname" == ${curves[$id]} ]; then if [[ "$cname" == ${curves[$id]} ]]; then
# we know it's supported, remove it from set of offered ones # we know it's supported, remove it from set of offered ones
unset curves[$id] unset curves[$id]
break break
fi fi
done done
fi fi
[ "$OUTPUTFORMAT" == "terminal" ] && [ $DEBUG -lt 1 ] && echo -n '.' [[ "$OUTPUTFORMAT" == "terminal" ]] && [[ $DEBUG -lt 1 ]] && echo -n '.'
done done
# don't penalize servers that will negotiate all curves we know of... # don't penalize servers that will negotiate all curves we know of...
@ -928,11 +932,11 @@ test_curves() {
# #
local tmp_curves=(${current_curves//,/ }) local tmp_curves=(${current_curves//,/ })
verbose "Server supported curves: ${tmp_curves[@]}" verbose "Server supported curves: ${tmp_curves[*]}"
# server supports just one or none, so it effectively uses server side # server supports just one or none, so it effectively uses server side
# ordering (as it dictates what curves client must support) # ordering (as it dictates what curves client must support)
if [ ${#tmp_curves[@]} -lt 2 ]; then if [[ ${#tmp_curves[@]} -lt 2 ]]; then
curves_ordering="server" curves_ordering="server"
else else
# server supports at least 2 curves, rotate their order, see if # server supports at least 2 curves, rotate their order, see if
@ -955,13 +959,13 @@ test_curves() {
else else
local ephem_data=(${current_pfs//,/ }) local ephem_data=(${current_pfs//,/ })
verbose "Server selected $current_cipher with $current_pfs" verbose "Server selected $current_cipher with $current_pfs"
verbose "ephem_data: ${ephem_data[@]}" verbose "ephem_data: ${ephem_data[*]}"
if [[ ${ephem_data[0]} =~ ECDH ]]; then if [[ ${ephem_data[0]} =~ ECDH ]]; then
verbose "Server did select ${ephem_data[1]} curve" verbose "Server did select ${ephem_data[1]} curve"
curves_ordering="inconclusive-${ephem_data[1]}" curves_ordering="inconclusive-${ephem_data[1]}"
local cname="$(get_curve_name ${ephem_data[1]})" local cname="$(get_curve_name ${ephem_data[1]})"
if [ "$cname" == "$most_wanted" ]; then if [[ "$cname" == "$most_wanted" ]]; then
curves_ordering="client" curves_ordering="client"
else else
curves_ordering="server" curves_ordering="server"
@ -975,13 +979,11 @@ test_curves() {
} }
test_curves_fallback() { test_curves_fallback() {
# "True" if server supports ciphers that don't use ECC at a lower priority
local fallback_available="False"
# return variable: whatever a server will fall back to non ECC suite when # return variable: whatever a server will fall back to non ECC suite when
# client doesn't advertise support for curves the server needs # client doesn't advertise support for curves the server needs
fallback_supported="unknown" fallback_supported="unknown"
if [ "$ecc_ciphers" == "" ]; then if [[ "$ecc_ciphers" == "" ]]; then
verbose "No ECC cipher found, can't test curve fallback" verbose "No ECC cipher found, can't test curve fallback"
return return
fi fi
@ -989,9 +991,9 @@ test_curves_fallback() {
# prepare the ssl command we'll be using # prepare the ssl command we'll be using
local sslcommand="" local sslcommand=""
sslcommand="$TIMEOUTBIN $TIMEOUT $OPENSSLBIN s_client" sslcommand="$TIMEOUTBIN $TIMEOUT $OPENSSLBIN s_client"
if [ -n "$CAPATH" ]; then if [[ -n "$CAPATH" ]]; then
sslcommand+=" -CApath $CAPATH -showcerts" sslcommand+=" -CApath $CAPATH -showcerts"
elif [ -e "$CACERTS" ]; then elif [[ -e "$CACERTS" ]]; then
sslcommand+=" -CAfile $CACERTS" sslcommand+=" -CAfile $CACERTS"
fi fi
sslcommand+=" -status $SCLIENTARGS -connect $TARGET -cipher $ecc_ciphers" sslcommand+=" -status $SCLIENTARGS -connect $TARGET -cipher $ecc_ciphers"
@ -1030,7 +1032,7 @@ test_curves_fallback() {
local cname="$(get_curve_name ${ephem_data[1]})" local cname="$(get_curve_name ${ephem_data[1]})"
verbose "Server selected curve $cname" verbose "Server selected curve $cname"
for id in "${!curves[@]}"; do for id in "${!curves[@]}"; do
if [ "${curves[id]}" == "$cname" ]; then if [[ "${curves[id]}" == "$cname" ]]; then
unset curves[$id] unset curves[$id]
break break
fi fi
@ -1083,9 +1085,9 @@ test_tls_tolerance() {
# cipher string and no options are specified) # cipher string and no options are specified)
# #
local sslcommand="$TIMEOUTBIN $TIMEOUT $OPENSSLBIN s_client" local sslcommand="$TIMEOUTBIN $TIMEOUT $OPENSSLBIN s_client"
if [ -n "$CAPATH" ]; then if [[ -n "$CAPATH" ]]; then
sslcommand+=" -CApath $CAPATH -showcerts" sslcommand+=" -CApath $CAPATH -showcerts"
elif [ -e "$CACERTS" ]; then elif [[ -e "$CACERTS" ]]; then
sslcommand+=" -CAfile $CACERTS" sslcommand+=" -CAfile $CACERTS"
fi fi
sslcommand+=" -connect $TARGET -cipher $CIPHERSUITE" sslcommand+=" -connect $TARGET -cipher $CIPHERSUITE"
@ -1111,9 +1113,9 @@ test_tls_tolerance() {
IFS="$OLDIFS" IFS="$OLDIFS"
local sslcommand="$TIMEOUTBIN $TIMEOUT $OPENSSLBIN s_client" local sslcommand="$TIMEOUTBIN $TIMEOUT $OPENSSLBIN s_client"
if [ -n "$CAPATH" ]; then if [[ -n "$CAPATH" ]]; then
sslcommand+=" -CApath $CAPATH -showcerts" sslcommand+=" -CApath $CAPATH -showcerts"
elif [ -e "$CACERTS" ]; then elif [[ -e "$CACERTS" ]]; then
sslcommand+=" -CAfile $CACERTS" sslcommand+=" -CAfile $CACERTS"
fi fi
sslcommand+=" -connect $TARGET -cipher $ciphers" sslcommand+=" -connect $TARGET -cipher $ciphers"
@ -1185,9 +1187,9 @@ test_tls_tolerance() {
IFS="$OLDIFS" IFS="$OLDIFS"
local sslcommand="$TIMEOUTBIN $TIMEOUT $OPENSSLBIN s_client" local sslcommand="$TIMEOUTBIN $TIMEOUT $OPENSSLBIN s_client"
if [ -n "$CAPATH" ]; then if [[ -n "$CAPATH" ]]; then
sslcommand+=" -CApath $CAPATH -showcerts" sslcommand+=" -CApath $CAPATH -showcerts"
elif [ -e "$CACERTS" ]; then elif [[ -e "$CACERTS" ]]; then
sslcommand+=" -CAfile $CACERTS" sslcommand+=" -CAfile $CACERTS"
fi fi
sslcommand+=" $SCLIENTARGS -connect $TARGET -cipher $ciphers:!SSLv2" sslcommand+=" $SCLIENTARGS -connect $TARGET -cipher $ciphers:!SSLv2"
@ -1269,8 +1271,8 @@ test_tls_tolerance() {
} }
# If no options are given, give usage information and exit (with error code) # If no options are given, give usage information and exit (with error code)
if [ $# -eq 0 ]; then if [[ $# -eq 0 ]]; then
usage; usage
exit 1 exit 1
fi fi
@ -1353,7 +1355,7 @@ HOST=$(sed -e 's/:.*//'<<<"${TEMPTARGET}")
PORT=$(sed -e 's/.*://'<<<"${TEMPTARGET}") PORT=$(sed -e 's/.*://'<<<"${TEMPTARGET}")
# Default to https if no port given # Default to https if no port given
if [ "$HOST" = "$PORT" ]; then if [[ "$HOST" = "$PORT" ]]; then
PORT=443 PORT=443
fi fi
@ -1364,22 +1366,22 @@ TARGET=$HOST:$PORT
debug "target: $TARGET" debug "target: $TARGET"
# test our openssl is usable # test our openssl is usable
if [ ! -x $OPENSSLBIN ]; then if [[ ! -x $OPENSSLBIN ]]; then
OPENSSLBIN=$(which openssl) OPENSSLBIN=$(which openssl)
if [ "$OUTPUTFORMAT" == "terminal" ]; then if [[ "$OUTPUTFORMAT" == "terminal" ]]; then
echo "custom openssl not executable, falling back to system one from $OPENSSLBIN" echo "custom openssl not executable, falling back to system one from $OPENSSLBIN"
fi fi
fi fi
if [ $TEST_CURVES == "True" ]; then if [[ $TEST_CURVES == "True" ]]; then
if [ ! -z "$($OPENSSLBIN s_client -curves 2>&1|head -1|grep 'unknown option')" ]; then if [[ ! -z "$($OPENSSLBIN s_client -curves 2>&1|head -1|grep 'unknown option')" ]]; then
echo "curves testing not available with your version of openssl, disabling it" echo "curves testing not available with your version of openssl, disabling it"
TEST_CURVES="False" TEST_CURVES="False"
fi fi
fi fi
if [ $VERBOSE != 0 ] ; then if [[ $VERBOSE != 0 ]] ; then
[ -n "$CACERTS" ] && echo "Using trust anchors from $CACERTS" [[ -n "$CACERTS" ]] && echo "Using trust anchors from $CACERTS"
echo "Loading $($OPENSSLBIN ciphers -v $CIPHERSUITE 2>/dev/null|grep Kx|wc -l) ciphersuites from $(echo -n $($OPENSSLBIN version 2>/dev/null))" echo "Loading $($OPENSSLBIN ciphers -v $CIPHERSUITE 2>/dev/null|grep Kx|wc -l) ciphersuites from $(echo -n $($OPENSSLBIN version 2>/dev/null))"
$OPENSSLBIN ciphers ALL 2>/dev/null $OPENSSLBIN ciphers ALL 2>/dev/null
fi fi
@ -1388,7 +1390,7 @@ SCLIENTARGS=$(sed -e s,${TEMPTARGET},,<<<"${@}")
debug "sclientargs: $SCLIENTARGS" debug "sclientargs: $SCLIENTARGS"
cipherspref=(); cipherspref=()
ciphercertificates=() ciphercertificates=()
results=() results=()
@ -1419,7 +1421,7 @@ if [[ $TEST_CURVES == "True" ]]; then
test_curves_fallback test_curves_fallback
fi fi
if [ "$OUTPUTFORMAT" == "json" ]; then if [[ "$OUTPUTFORMAT" == "json" ]]; then
display_results_in_json display_results_in_json
else else
echo echo
@ -1427,13 +1429,13 @@ else
fi fi
# If asked, test every single cipher individually # If asked, test every single cipher individually
if [ $ALLCIPHERS -gt 0 ]; then if [[ $ALLCIPHERS -gt 0 ]]; then
echo; echo "All accepted ciphersuites" echo; echo "All accepted ciphersuites"
for c in $($OPENSSLBIN ciphers -v ALL:COMPLEMENTOFALL 2>/dev/null |awk '{print $1}'|sort|uniq); do for c in $($OPENSSLBIN ciphers -v ALL:COMPLEMENTOFALL 2>/dev/null |awk '{print $1}'|sort|uniq); do
r="fail" r="fail"
osslcommand="$TIMEOUTBIN $TIMEOUT $OPENSSLBIN s_client $SCLIENTARGS -connect $TARGET -cipher $c" osslcommand="$TIMEOUTBIN $TIMEOUT $OPENSSLBIN s_client $SCLIENTARGS -connect $TARGET -cipher $c"
test_cipher_on_target "$osslcommand" test_cipher_on_target "$osslcommand"
if [ $? -eq 0 ]; then if [[ $? -eq 0 ]]; then
r="pass" r="pass"
fi fi
echo "$c $r"|awk '{printf "%-35s %s\n",$1,$2}' echo "$c $r"|awk '{printf "%-35s %s\n",$1,$2}'