|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" |
| 6 | +OUTPUT_DIR="$SCRIPT_DIR/../tests" |
| 7 | + |
| 8 | +######################### CA ######################### |
| 9 | + |
| 10 | +echo "Generating CA private key and certificate..." |
| 11 | + |
| 12 | +openssl genpkey -algorithm RSA -out "$OUTPUT_DIR"/ca.key.pem |
| 13 | + |
| 14 | +openssl req -new -x509 -days 73050 -key "$OUTPUT_DIR"/ca.key.pem -out "$OUTPUT_DIR"/ca.cert.pem \ |
| 15 | + -config "$SCRIPT_DIR"/openssl.cnf -extensions v3_ca -subj "/CN=Test Self-Signed CA" |
| 16 | + |
| 17 | +echo "CA certificate and key created successfully." |
| 18 | + |
| 19 | +######################### SERVER ######################### |
| 20 | + |
| 21 | +echo "Generating server private key and CSR..." |
| 22 | + |
| 23 | +openssl genpkey -algorithm RSA -out "$OUTPUT_DIR"/server.key.pem |
| 24 | + |
| 25 | +openssl req -new -key "$OUTPUT_DIR"/server.key.pem -out "$OUTPUT_DIR"/server.csr \ |
| 26 | + -config "$SCRIPT_DIR"/openssl.cnf -subj "/CN=localhost/O=Test Server" |
| 27 | + |
| 28 | +echo "Signing the server CSR with the CA certificate..." |
| 29 | + |
| 30 | +openssl x509 -req -in "$OUTPUT_DIR"/server.csr -CA "$OUTPUT_DIR"/ca.cert.pem -CAkey "$OUTPUT_DIR"/ca.key.pem \ |
| 31 | + -CAcreateserial -out "$OUTPUT_DIR"/server.cert.pem -days 73050 -extensions v3_req \ |
| 32 | + -extfile "$SCRIPT_DIR"/openssl.cnf |
| 33 | + |
| 34 | +echo "Server certificate signed successfully." |
| 35 | + |
| 36 | +######################### CLIENT ######################### |
| 37 | + |
| 38 | +echo "Generating client private key and CSR..." |
| 39 | + |
| 40 | +openssl genpkey -algorithm RSA -out "$OUTPUT_DIR"/client.key.pem |
| 41 | + |
| 42 | +openssl req -new -key "$OUTPUT_DIR"/client.key.pem -out "$OUTPUT_DIR"/client.csr \ |
| 43 | + -config "$SCRIPT_DIR"/openssl.cnf -subj "/CN=localhost/O=Test Client" |
| 44 | + |
| 45 | +echo "Signing the client CSR with the CA certificate..." |
| 46 | + |
| 47 | +openssl x509 -req -in "$OUTPUT_DIR"/client.csr -CA "$OUTPUT_DIR"/ca.cert.pem -CAkey "$OUTPUT_DIR"/ca.key.pem \ |
| 48 | + -CAcreateserial -out "$OUTPUT_DIR"/client.cert.pem -days 73050 -extensions v3_req \ |
| 49 | + -extfile "$SCRIPT_DIR"/openssl.cnf |
| 50 | + |
| 51 | +echo "Client certificate signed successfully." |
| 52 | + |
| 53 | +######################### COMPLETE ######################### |
| 54 | + |
| 55 | +rm -f "$OUTPUT_DIR"/server.csr "$OUTPUT_DIR"/client.csr |
| 56 | + |
| 57 | +echo "Complete." |
| 58 | + |
0 commit comments