| 1 | #!/usr/bin/env bash |
| 2 | # ========================================================================== |
| 3 | # ____ _ _____ _ |
| 4 | # / ___| _ _ ___| |_ ___ _ __ ___ |_ _|__ ___ | |___ |
| 5 | # \___ \| | | / __| __/ _ \ '_ ` _ \ _____| |/ _ \ / _ \| / __| |
| 6 | # ___) | |_| \__ \ || __/ | | | | |_____| | (_) | (_) | \__ \ |
| 7 | # |____/ \__, |___/\__\___|_| |_| |_| |_|\___/ \___/|_|___/ |
| 8 | # |___/ |
| 9 | # --- System-Tools --- |
| 10 | # https://www.nntb.no/~dreibh/system-tools/ |
| 11 | # ========================================================================== |
| 12 | # |
| 13 | # TLS Connection Tester |
| 14 | # Copyright (C) 2015-2026 by Thomas Dreibholz |
| 15 | # |
| 16 | # This program is free software: you can redistribute it and/or modify |
| 17 | # it under the terms of the GNU General Public License as published by |
| 18 | # the Free Software Foundation, either version 3 of the License, or |
| 19 | # (at your option) any later version. |
| 20 | # |
| 21 | # This program is distributed in the hope that it will be useful, |
| 22 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | # GNU General Public License for more details. |
| 25 | # |
| 26 | # You should have received a copy of the GNU General Public License |
| 27 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | # |
| 29 | # Contact: [email protected] |
| 30 | |
| 31 | # Bash options: |
| 32 | set -eu |
| 33 | |
| 34 | # gettext options: |
| 35 | export TEXTDOMAIN="test-tls-connection" |
| 36 | # export TEXTDOMAINDIR="${PWD}/locale" # Default: "/usr/share/locale" |
| 37 | |
| 38 | # shellcheck disable=SC1091 |
| 39 | . gettext.sh |
| 40 | |
| 41 | |
| 42 | # ###### Usage ############################################################## |
| 43 | usage () { |
| 44 | echo >&2 "$(gettext "Usage:") $0 host:port cafile [-s|--save-certificate certificate_file] [-n|--no-view-certificate] [-c|--no-check-certificate] [-- openssl_args ...]" |
| 45 | exit 1 |
| 46 | } |
| 47 | |
| 48 | |
| 49 | # ###### Main program ####################################################### |
| 50 | |
| 51 | # ====== Handle arguments =================================================== |
| 52 | DIRNAME="$(dirname "$0")" |
| 53 | GETOPT="$(PATH=/usr/local/bin:${PATH} which getopt)" |
| 54 | options="$(${GETOPT} -o s:nch --long save-certificate:,no-view-certificate,no-check-certificate,help -a -- "$@")" |
| 55 | # shellcheck disable=SC2181 |
| 56 | if [[ $? -ne 0 ]]; then |
| 57 | usage |
| 58 | fi |
| 59 | |
| 60 | VIEW_CERTIFICATE=1 |
| 61 | CHECK_CERTIFICATE=1 |
| 62 | CERTIFICATE_FILE="" |
| 63 | eval set -- "${options}" |
| 64 | while [ $# -gt 0 ] ; do |
| 65 | case "$1" in |
| 66 | -s | --save-certificate) |
| 67 | CERTIFICATE_FILE="$2" |
| 68 | shift 2 |
| 69 | ;; |
| 70 | -n | --no-view-certificate) |
| 71 | VIEW_CERTIFICATE=0 |
| 72 | shift |
| 73 | ;; |
| 74 | -c | --no-check-certificate) |
| 75 | CHECK_CERTIFICATE=0 |
| 76 | shift |
| 77 | ;; |
| 78 | -h | --help) |
| 79 | usage |
| 80 | # shift |
| 81 | ;; |
| 82 | --) |
| 83 | shift |
| 84 | break |
| 85 | ;; |
| 86 | esac |
| 87 | done |
| 88 | if [ $# -lt 2 ] ; then |
| 89 | usage |
| 90 | fi |
| 91 | ENDPOINT="$1" |
| 92 | CA_FILE="$2" |
| 93 | shift 2 |
| 94 | if [ ! -e "${CA_FILE}" ] ; then |
| 95 | eval_gettext >&2 "ERROR: Unable to find CA certificate file \${CA_FILE}!" |
| 96 | echo >&2 |
| 97 | exit 1 |
| 98 | fi |
| 99 | |
| 100 | # ====== Connect to endpoint ================================================ |
| 101 | echo -e "\e[34m$(eval_gettext "Connecting to \${ENDPOINT} ...")\e[0m" |
| 102 | tempOutputFile="" |
| 103 | tempCertFile="" |
| 104 | trap 'rm -f "${tempCertFile}" "${tempOutputFile}"' EXIT INT TERM HUP |
| 105 | tempOutputFile="$(mktemp -t test-tls-connection.XXXXXXXXXX.out)" |
| 106 | if [ "${CERTIFICATE_FILE}" == "" ] ; then |
| 107 | tempCertFile="$(mktemp -t test-tls-connection.XXXXXXXXXX.pem)" |
| 108 | CERTIFICATE_FILE="${tempCertFile}" |
| 109 | fi |
| 110 | |
| 111 | # shellcheck disable=SC2068 |
| 112 | echo "" | openssl s_client -showcerts -connect "${ENDPOINT}" -no-CApath -no-CAstore -CAfile "${CA_FILE}" $@ >"${tempOutputFile}" |
| 113 | |
| 114 | text-block --extract \ |
| 115 | --begin-tag '-----BEGIN CERTIFICATE-----' \ |
| 116 | --end-tag '-----END CERTIFICATE-----' \ |
| 117 | --full-tag-lines --include-tags --min-actions 1 \ |
| 118 | --input "${tempOutputFile}" \ |
| 119 | --output "${CERTIFICATE_FILE}" |
| 120 | |
| 121 | # ====== Display and verify certificate using check-certificate ============= |
| 122 | if [ ${VIEW_CERTIFICATE} -eq 1 ] && [ ${CHECK_CERTIFICATE} -eq 1 ] ; then |
| 123 | "${DIRNAME}/check-certificate" "${CA_FILE}" "${CERTIFICATE_FILE}" |
| 124 | elif [ ${VIEW_CERTIFICATE} -eq 0 ] && [ ${CHECK_CERTIFICATE} -eq 1 ] ; then |
| 125 | "${DIRNAME}/check-certificate" "${CA_FILE}" "${CERTIFICATE_FILE}" --no-view-certificate |
| 126 | elif [ ${VIEW_CERTIFICATE} -eq 1 ] && [ ${CHECK_CERTIFICATE} -eq 0 ] ; then |
| 127 | "${DIRNAME}/view-certificate" "${CERTIFICATE_FILE}" |
| 128 | fi |