|
| 1 | +#!/bin/sh |
| 2 | +# |
| 3 | +# run.sh - Start an OCSP responder using wolfclu (SCGI) behind nginx. |
| 4 | +# |
| 5 | +# This script: |
| 6 | +# 1. Starts wolfclu in SCGI mode on port 8081 |
| 7 | +# 2. Starts nginx on port 8080, forwarding to wolfclu via SCGI |
| 8 | +# 3. Sends a test OCSP query using wolfssl's built-in test certs |
| 9 | +# |
| 10 | +# Prerequisites: |
| 11 | +# - wolfssl built with: --enable-ocsp --enable-ocsp-responder |
| 12 | +# - wolfclu built and installed |
| 13 | +# - nginx installed with SCGI support (default in most packages) |
| 14 | +# |
| 15 | +# Usage: |
| 16 | +# ./run.sh [options] |
| 17 | +# |
| 18 | +# Options: |
| 19 | +# --ca-cert <file> CA certificate (default: wolfSSL test ca-cert.pem) |
| 20 | +# --ca-key <file> CA private key (default: wolfSSL test ca-key.pem) |
| 21 | +# --index <file> OpenSSL-format index.txt (optional) |
| 22 | +# --port <num> nginx listen port (default: 8080) |
| 23 | +# --scgi-port <num> wolfclu SCGI port (default: 8081) |
| 24 | + |
| 25 | +set -e |
| 26 | + |
| 27 | +# Defaults - use wolfSSL test certificates |
| 28 | +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 29 | +CA_CERT="${SCRIPT_DIR}/../../../certs/ca-cert.pem" |
| 30 | +CA_KEY="${SCRIPT_DIR}/../../../certs/ca-key.pem" |
| 31 | +INDEX_FILE="" |
| 32 | +HTTP_PORT=8080 |
| 33 | +SCGI_PORT=8081 |
| 34 | +WOLFCLU_PID="" |
| 35 | +NGINX_PID="" |
| 36 | + |
| 37 | +cleanup() { |
| 38 | + echo "" |
| 39 | + echo "Shutting down..." |
| 40 | + [ -n "$WOLFCLU_PID" ] && kill "$WOLFCLU_PID" 2>/dev/null || true |
| 41 | + [ -n "$NGINX_PID" ] && kill "$NGINX_PID" 2>/dev/null || true |
| 42 | + wait 2>/dev/null || true |
| 43 | + [ -n "$WORK_DIR" ] && rm -rf "$WORK_DIR" |
| 44 | + echo "Done." |
| 45 | +} |
| 46 | +trap cleanup EXIT INT TERM |
| 47 | + |
| 48 | +# Parse arguments |
| 49 | +while [ $# -gt 0 ]; do |
| 50 | + case "$1" in |
| 51 | + --ca-cert) CA_CERT="$2"; shift 2 ;; |
| 52 | + --ca-key) CA_KEY="$2"; shift 2 ;; |
| 53 | + --index) INDEX_FILE="$2"; shift 2 ;; |
| 54 | + --port) HTTP_PORT="$2"; shift 2 ;; |
| 55 | + --scgi-port) SCGI_PORT="$2"; shift 2 ;; |
| 56 | + *) |
| 57 | + echo "Unknown option: $1" |
| 58 | + exit 1 |
| 59 | + ;; |
| 60 | + esac |
| 61 | +done |
| 62 | + |
| 63 | +# Validate files exist |
| 64 | +if [ ! -f "$CA_CERT" ]; then |
| 65 | + echo "Error: CA cert not found: $CA_CERT" |
| 66 | + exit 1 |
| 67 | +fi |
| 68 | +if [ ! -f "$CA_KEY" ]; then |
| 69 | + echo "Error: CA key not found: $CA_KEY" |
| 70 | + exit 1 |
| 71 | +fi |
| 72 | + |
| 73 | +# Check for required tools |
| 74 | +if ! command -v wolfssl >/dev/null 2>&1; then |
| 75 | + echo "Error: 'wolfssl' (wolfCLU) not found in PATH" |
| 76 | + echo "Build wolfCLU from https://github.com/wolfSSL/wolfCLU" |
| 77 | + exit 1 |
| 78 | +fi |
| 79 | +if ! command -v nginx >/dev/null 2>&1; then |
| 80 | + echo "Error: nginx not found in PATH" |
| 81 | + exit 1 |
| 82 | +fi |
| 83 | + |
| 84 | +echo "=== OCSP Responder: nginx + wolfclu (SCGI) ===" |
| 85 | +echo "" |
| 86 | +echo "CA cert: $CA_CERT" |
| 87 | +echo "CA key: $CA_KEY" |
| 88 | +echo "HTTP port: $HTTP_PORT (nginx)" |
| 89 | +echo "SCGI port: $SCGI_PORT (wolfclu)" |
| 90 | +echo "" |
| 91 | + |
| 92 | +# --- Step 1: Start wolfclu OCSP responder in SCGI mode --- |
| 93 | +echo "Starting wolfclu OCSP responder (SCGI on port $SCGI_PORT)..." |
| 94 | + |
| 95 | +set -- -scgi -port "$SCGI_PORT" -rsigner "$CA_CERT" -rkey "$CA_KEY" -CA "$CA_CERT" |
| 96 | +if [ -n "$INDEX_FILE" ]; then |
| 97 | + set -- "$@" -index "$INDEX_FILE" |
| 98 | +fi |
| 99 | + |
| 100 | +wolfssl ocsp "$@" & |
| 101 | +WOLFCLU_PID=$! |
| 102 | +sleep 1 |
| 103 | + |
| 104 | +if ! kill -0 "$WOLFCLU_PID" 2>/dev/null; then |
| 105 | + echo "Error: wolfclu failed to start" |
| 106 | + exit 1 |
| 107 | +fi |
| 108 | +echo "wolfclu started (PID $WOLFCLU_PID)" |
| 109 | + |
| 110 | +# --- Step 2: Generate nginx config with correct ports --- |
| 111 | +WORK_DIR="$(mktemp -d "$SCRIPT_DIR/tmp.XXXXXX")" |
| 112 | +NGINX_CONF="$WORK_DIR/nginx-ocsp.conf" |
| 113 | +cat > "$NGINX_CONF" <<EOF |
| 114 | +daemon off; |
| 115 | +pid $WORK_DIR/nginx-ocsp.pid; |
| 116 | +error_log /dev/stderr info; |
| 117 | +
|
| 118 | +events { |
| 119 | + worker_connections 64; |
| 120 | +} |
| 121 | +
|
| 122 | +http { |
| 123 | + client_body_temp_path $WORK_DIR/body; |
| 124 | + proxy_temp_path $WORK_DIR/proxy; |
| 125 | + fastcgi_temp_path $WORK_DIR/fastcgi; |
| 126 | + uwsgi_temp_path $WORK_DIR/uwsgi; |
| 127 | + scgi_temp_path $WORK_DIR/scgi; |
| 128 | +
|
| 129 | + access_log /dev/stdout; |
| 130 | +
|
| 131 | + server { |
| 132 | + listen $HTTP_PORT; |
| 133 | +
|
| 134 | + location / { |
| 135 | + scgi_pass 127.0.0.1:$SCGI_PORT; |
| 136 | +
|
| 137 | + scgi_param REQUEST_METHOD \$request_method; |
| 138 | + scgi_param REQUEST_URI \$request_uri; |
| 139 | + scgi_param QUERY_STRING \$query_string; |
| 140 | + scgi_param CONTENT_TYPE \$content_type; |
| 141 | + scgi_param CONTENT_LENGTH \$content_length; |
| 142 | + scgi_param DOCUMENT_URI \$document_uri; |
| 143 | + scgi_param DOCUMENT_ROOT \$document_root; |
| 144 | + scgi_param SCGI 1; |
| 145 | + scgi_param SERVER_PROTOCOL \$server_protocol; |
| 146 | + scgi_param REQUEST_SCHEME \$scheme; |
| 147 | + scgi_param HTTPS \$https if_not_empty; |
| 148 | + scgi_param REMOTE_ADDR \$remote_addr; |
| 149 | + scgi_param REMOTE_PORT \$remote_port; |
| 150 | + scgi_param SERVER_PORT \$server_port; |
| 151 | + scgi_param SERVER_NAME \$server_name; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | +EOF |
| 156 | + |
| 157 | +echo "Starting nginx (HTTP on port $HTTP_PORT)..." |
| 158 | +nginx -c "$NGINX_CONF" & |
| 159 | +NGINX_PID=$! |
| 160 | +sleep 1 |
| 161 | + |
| 162 | +if ! kill -0 "$NGINX_PID" 2>/dev/null; then |
| 163 | + echo "Error: nginx failed to start" |
| 164 | + exit 1 |
| 165 | +fi |
| 166 | +echo "nginx started (PID $NGINX_PID)" |
| 167 | + |
| 168 | +echo "" |
| 169 | +echo "=== OCSP responder is running ===" |
| 170 | +echo "" |
| 171 | +echo "Test with wolfssl:" |
| 172 | +echo " wolfssl ocsp -issuer $CA_CERT -cert ../../certs/server-cert.pem \\" |
| 173 | +echo " -url http://127.0.0.1:$HTTP_PORT/" |
| 174 | +echo "" |
| 175 | +echo "Test with openssl:" |
| 176 | +echo " openssl ocsp -issuer $CA_CERT -cert ../../certs/server-cert.pem \\" |
| 177 | +echo " -url http://127.0.0.1:$HTTP_PORT/ -resp_text" |
| 178 | +echo "" |
| 179 | +echo "Press Ctrl-C to stop." |
| 180 | +echo "" |
| 181 | + |
| 182 | +# Wait for either process to exit |
| 183 | +wait |
0 commit comments