#!/bin/bash
# Reproduces the executable claims in
# https://www.heatware.net/mysql/setup-ssl-and-tls-for-security-hardening/
#
# Part 1 generates a throwaway CA + server certificate with openssl in a scratch directory and
# verifies the chain -- proving the corrected certificate sequence actually produces every file
# the [mysqld] block references. Part 2, if a local MySQL server is reachable on 127.0.0.1 with
# `mysql -u root`, reads its real SSL/TLS variables and the negotiated cipher on an actual
# connection. It never modifies any MySQL server's configuration and issues no DROP/TRUNCATE.
#
# Requires: openssl. mysql/mysqld are optional -- their checks SKIP if no server is reachable.
# Runtime: a few seconds.
# Usage: bash verify-setup-ssl-and-tls-for-security-hardening.sh
set -u
# Resolve the article path to an absolute location BEFORE any cd, so content checks work
# regardless of cwd, and SKIP (not hang or silently pass) when the article isn't alongside the
# script -- e.g. when a reader downloads only this script from /verify/.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" >/dev/null 2>&1 && pwd)"
ARTICLE="$SCRIPT_DIR/setup-ssl-and-tls-for-security-hardening.html"
W="$(mktemp -d -t hw_mysqlssl)"
cleanup() { rm -rf "$W"; }
trap cleanup EXIT
PASSES=0; FAILS=0; SKIPS=0; CONTENT=0
pass() { PASSES=$((PASSES+1)); printf 'PASS %s\n' "$1"; }
fail() { FAILS=$((FAILS+1)); printf 'FAIL %s\n expected: %s\n actual: %s\n' "$1" "$2" "$3"; }
skip() { SKIPS=$((SKIPS+1)); printf 'SKIP %s (needs %s)\n' "$1" "$2"; }
cpass() { CONTENT=$((CONTENT+1)); printf 'CONTENT %s\n' "$1"; }
# ---- content regression guards (skip gracefully if article isn't next to the script) ----
if [ -f "$ARTICLE" ]; then
# The old wrong claim is expected to appear ONCE, quoted in prose as part of owning the error
# (per Hard Rule 5) -- not as a live
asserting it as fact. Fail only if it still appears
# inside an actual HTML table structure.
if grep -q " containing '128-bit' is still present"
else
cpass "old TLS-version-fixes-key-length table is not being restated as a live fact-table"
fi
if grep -qi "that's wrong" "$ARTICLE" && grep -qi "three separate choices\|three separate choice" "$ARTICLE"; then
cpass "article explicitly owns the prior error rather than silently rewriting it"
else
fail "article explicitly owns the prior error" "correction language present" "not found"
fi
if grep -q "require_secure_transport" "$ARTICLE"; then
cpass "article documents require_secure_transport, which the original never mentioned"
else
fail "article documents require_secure_transport" "mention present" "not found"
fi
if grep -q "CAcreateserial" "$ARTICLE" && grep -q "ca-cert.pem" "$ARTICLE"; then
cpass "article's cert sequence actually generates ca-cert.pem via CA signing"
else
fail "article's cert sequence generates ca-cert.pem" "CA-signing sequence present" "not found"
fi
else
skip "content regression checks" "setup-ssl-and-tls-for-security-hardening.html next to this script"
fi
# ---- Part 1: executed -- generate a throwaway CA + server cert and verify the chain ----
if command -v openssl >/dev/null 2>&1; then
(
cd "$W" || exit 1
openssl genrsa -out ca-key.pem 2048 >/dev/null 2>&1
openssl req -x509 -new -nodes -key ca-key.pem -sha256 -days 365 \
-subj "/CN=verify_demo_CA" -out ca-cert.pem >/dev/null 2>&1
openssl genrsa -out server-key.pem 2048 >/dev/null 2>&1
openssl req -new -key server-key.pem -subj "/CN=verify_demo_server" -out server-req.pem >/dev/null 2>&1
openssl x509 -req -in server-req.pem -days 365 -CA ca-cert.pem -CAkey ca-key.pem \
-CAcreateserial -out server-cert.pem -sha256 >/dev/null 2>&1
)
if [ -f "$W/ca-cert.pem" ] && [ -f "$W/server-cert.pem" ] && [ -f "$W/server-key.pem" ]; then
pass "openssl sequence produces ca-cert.pem, server-cert.pem, and server-key.pem"
else
fail "openssl sequence produces ca-cert.pem, server-cert.pem, and server-key.pem" "all three files present" "one or more missing"
fi
VERIFY_OUT="$(openssl verify -CAfile "$W/ca-cert.pem" "$W/server-cert.pem" 2>&1)"
case "$VERIFY_OUT" in
*"server-cert.pem: OK"*) pass "server-cert.pem verifies against the generated ca-cert.pem" ;;
*) fail "server-cert.pem verifies against the generated ca-cert.pem" "server-cert.pem: OK" "$VERIFY_OUT" ;;
esac
else
skip "certificate chain generation" "openssl on PATH"
fi
# ---- Part 2: executed, only if a local MySQL server answers `mysql -u root` on 127.0.0.1 ----
if command -v mysql >/dev/null 2>&1 && mysql -h 127.0.0.1 -u root -e "SELECT 1;" >/dev/null 2>&1; then
SSLVARS="$(mysql -h 127.0.0.1 -u root -N -e "SHOW VARIABLES LIKE 'ssl_ca';" 2>/dev/null)"
if [ -n "$SSLVARS" ]; then
pass "local MySQL server has an ssl_ca variable set (auto-generated or configured): $SSLVARS"
else
fail "local MySQL server has an ssl_ca variable set" "non-empty ssl_ca" "empty"
fi
CIPHER="$(mysql -h 127.0.0.1 -u root -N -e "SELECT VARIABLE_VALUE FROM performance_schema.session_status WHERE VARIABLE_NAME='Ssl_cipher';" 2>/dev/null)"
if [ -n "$CIPHER" ]; then
pass "a normal connection to this MySQL server negotiates TLS by default (Ssl_cipher=$CIPHER)"
else
fail "a normal connection negotiates TLS by default" "non-empty Ssl_cipher" "empty (plaintext session)"
fi
else
skip "live MySQL SSL variable checks" "a local MySQL server reachable via 'mysql -u root' on 127.0.0.1"
fi
echo
echo "PASS=$PASSES FAIL=$FAILS SKIP=$SKIPS CONTENT=$CONTENT"
[ "$FAILS" -eq 0 ]