Skip to content
PSUT / IoTEngineering learning studio
WORKSPACE
Offline-first portalDr. Mohammad Alja’afreh
Cyber Defense StudioIoT Security and PrivacyExperimentsWorkspaces
ENGINEERING WORKSPACE
Home/Engineering Workspaces/Isolated MQTT TLS Practice
PSUT IoT Engineering StudioPrincess Sumaya University for Technology · Dr. Mohammad Alja’afreh
Optional local practice
Supporting engineering workspace

MQTT over TLS — isolated WSL broker

Local-only positive and negative verification with two separate identities. Does not modify the existing toolkit. Requires Mosquitto 2.x, OpenSSL and a free port 8883.

Return to Security Studio

01 · Ubuntu WSL — check prerequisites

Run in Ubuntu, not PowerShell. Use existing Mosquitto tools. If missing, install mosquitto, mosquitto-clients and openssl using apt. Port 8883 must be unused. Do not stop the existing 1884 broker.

command -v mosquitto mosquitto_pub mosquitto_sub openssl
ss -ltn | grep ":8883" || true

02 · Create a new private practice directory

Run once. mktemp returns a unique directory; keep this terminal open. Use the printed directory in every other terminal (replace the path placeholder). Keys are only for this lab. Do not copy private keys into the website.

umask 077
LAB=$(mktemp -d "$HOME/psut-security.XXXXXX")
cd "$LAB"
printf "%s\n" "$LAB"

03 · Generate local CA and broker certificate

Use a private test CA and a certificate with localhost SAN. The CA private key remains local; distribute only ca.crt to the test client. Short certificate lifetime is intentional.

openssl req -x509 -newkey rsa:2048 -nodes -keyout ca.key -out ca.crt -days 7 -sha256 -subj '/CN=PSUT Local Training CA' -addext 'basicConstraints=critical,CA:TRUE' -addext 'keyUsage=critical,keyCertSign,cRLSign'
openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr -subj '/CN=localhost'
printf 'subjectAltName=DNS:localhost,IP:127.0.0.1
keyUsage=digitalSignature,keyEncipherment
extendedKeyUsage=serverAuth
basicConstraints=CA:FALSE
' > server.ext
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 7 -sha256 -extfile server.ext
openssl verify -CAfile ca.crt -verify_hostname localhost server.crt

04 · Create separate accounts

Use different temporary passwords; the prompts do not echo them. The -c flag creates a new password file; never use it against an existing production password file. No password is needed by the website.

mosquitto_passwd -c passwords sensor
mosquitto_passwd passwords viewer

05 · Configure the isolated broker

This is a standalone Mosquitto 2.x classroom configuration, using traditional password_file and acl_file directives. For versions that replace these options, see the official migration documentation. Do not copy the config into /etc/mosquitto. The listener binds only to loopback. Run the foreground process as your ordinary Ubuntu user.

cat > acl <<'ACL'
user sensor
topic write psut/security/demo/telemetry
user viewer
topic read psut/security/demo/telemetry
ACL
cat > broker.conf <<EOF
listener 8883 127.0.0.1
allow_anonymous false
password_file $LAB/passwords
acl_file $LAB/acl
certfile $LAB/server.crt
keyfile $LAB/server.key
tls_version tlsv1.2
persistence false
connection_messages true
log_type all
EOF
mosquitto -c "$LAB/broker.conf" -v

06 · Verify actual TLS trust

Open a second Ubuntu terminal. Replace the placeholder path with the directory printed in step 02. Expected: Verify return code: 0 (ok), not merely a TCP connection. The broker requires MQTT authentication later; this opens only TLS and does not send CONNECT.

cd /path/to/your/psut-security.XXXXXX
openssl s_client -connect 127.0.0.1:8883 -servername localhost -CAfile ca.crt -verify_hostname localhost -verify_return_error </dev/null

07 · Start authorized subscriber

In the second terminal, read a temporary password into a shell variable. It is not echoed or placed literally in shell history; command arguments may still be visible to local processes. Use disposable credentials on your own machine only. Subscribe first; -C 1 exits after one message, -W 25 bounds the wait. MQTT 5 reason codes are useful for negative tests.

read -rs -p 'Viewer demo password: ' PW; echo
mosquitto_sub -h localhost -p 8883 --cafile ca.crt -V mqttv5 -u viewer -P "$PW" -t psut/security/demo/telemetry -q 1 -C 1 -W 25 -v
unset PW

08 · Publish a valid fictional sample

Open a third Ubuntu terminal in the same directory. Execute while the authorized subscriber is waiting. Expected: viewer receives the exact JSON. The independent website and 1884 services are not involved.

read -rs -p 'Sensor demo password: ' PW; echo
mosquitto_pub -h localhost -p 8883 --cafile ca.crt -V mqttv5 -u sensor -P "$PW" -t psut/security/demo/telemetry -q 1 -m '{"temperature":25,"humidity":60}'
unset PW

09 · Verify denied publication safely

Repeat the authorized subscription from step 07, then attempt the same fictional publication using viewer credentials. This is a controlled negative authorization test against your own isolated broker. Expected: no delivery; inspect the MQTT 5 reason code and broker log for denial. A subscriber timeout alone is insufficient evidence; compare with step 08.

read -rs -p 'Viewer demo password: ' PW; echo
mosquitto_pub -h localhost -p 8883 --cafile ca.crt -V mqttv5 -u viewer -P "$PW" -t psut/security/demo/telemetry -q 1 -m '{"temperature":25,"humidity":60}'
unset PW

10 · Document and stop

Record TLS validation, authorized delivery and denied publication as separate observations. In the broker terminal press Ctrl+C. Keep the private directory only while needed; remove it manually when finished. Confirm original port 1884 remains available. Do not share certificates’ private keys, passwords or unredacted logs.

ss -ltn | grep -E ":(1884|8883)" || true

References: Mosquitto configuration · Migration notes. TLS 1.2 minimum permits 1.3 when negotiated; inspect the actual protocol from OpenSSL. WSL broker access is intentionally local only. No public broker or real attack traffic is involved.