| 1 | #!/bin/bash |
| 2 | # Generate self-signed certificates for EMQX MQTT TLS testing |
| 3 | # These certificates are for testing only - NOT for production use! |
| 4 | |
| 5 | set -e |
| 6 | |
| 7 | CERTS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 8 | |
| 9 | echo "Generating self-signed certificates for MQTT TLS testing..." |
| 10 | |
| 11 | # Generate CA key and certificate |
| 12 | openssl genrsa -out "$CERTS_DIR/ca.key" 2048 |
| 13 | openssl req -new -x509 -days 3650 -key "$CERTS_DIR/ca.key" \ |
| 14 | -out "$CERTS_DIR/ca.pem" \ |
| 15 | -subj "/CN=shoutrrr-test-ca/O=Shoutrrr/C=US" |
| 16 | |
| 17 | # Generate server key |
| 18 | openssl genrsa -out "$CERTS_DIR/key.pem" 2048 |
| 19 | |
| 20 | # Generate server certificate request with localhost SAN |
| 21 | cat > "$CERTS_DIR/server.cnf" <<EOF |
| 22 | [req] |
| 23 | default_bits = 2048 |
| 24 | prompt = no |
| 25 | default_md = sha256 |
| 26 | distinguished_name = dn |
| 27 | req_extensions = v3_req |
| 28 | |
| 29 | [dn] |
| 30 | C = US |
| 31 | O = Shoutrrr |
| 32 | CN = localhost |
| 33 | |
| 34 | [v3_req] |
| 35 | subjectAltName = @alt_names |
| 36 | |
| 37 | [alt_names] |
| 38 | DNS.1 = localhost |
| 39 | DNS.2 = emqx |
| 40 | IP.1 = 127.0.0.1 |
| 41 | IP.2 = 0.0.0.0 |
| 42 | EOF |
| 43 | |
| 44 | openssl req -new -key "$CERTS_DIR/key.pem" \ |
| 45 | -out "$CERTS_DIR/server.csr" \ |
| 46 | -config "$CERTS_DIR/server.cnf" |
| 47 | |
| 48 | # Sign the server certificate with CA |
| 49 | openssl x509 -req -days 365 \ |
| 50 | -in "$CERTS_DIR/server.csr" \ |
| 51 | -CA "$CERTS_DIR/ca.pem" \ |
| 52 | -CAkey "$CERTS_DIR/ca.key" \ |
| 53 | -CAcreateserial \ |
| 54 | -out "$CERTS_DIR/cert.pem" \ |
| 55 | -extensions v3_req \ |
| 56 | -extfile "$CERTS_DIR/server.cnf" |
| 57 | |
| 58 | # Set permissions |
| 59 | chmod 600 "$CERTS_DIR"/*.key |
| 60 | chmod 644 "$CERTS_DIR"/*.pem |
| 61 | |
| 62 | # Cleanup |
| 63 | rm -f "$CERTS_DIR/ca.key" "$CERTS_DIR/server.csr" "$CERTS_DIR/server.cnf" "$CERTS_DIR/ca.srl" |
| 64 | |
| 65 | echo "Certificates generated successfully!" |
| 66 | echo " - CA: $CERTS_DIR/ca.pem" |
| 67 | echo " - Server cert: $CERTS_DIR/cert.pem" |
| 68 | echo " - Server key: $CERTS_DIR/key.pem" |
| 69 | echo "" |
| 70 | echo "To use with EMQX, mount these files and configure:" |
| 71 | echo " EMQX_LISTENERS__SSL__DEFAULT__SSL_OPTIONS__CACERTFILE=/opt/emqx/etc/certs/ca.pem" |
| 72 | echo " EMQX_LISTENERS__SSL__DEFAULT__SSL_OPTIONS__CERTFILE=/opt/emqx/etc/certs/cert.pem" |
| 73 | echo " EMQX_LISTENERS__SSL__DEFAULT__SSL_OPTIONS__KEYFILE=/opt/emqx/etc/certs/key.pem" |