|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +echo "[*] Setting up environment..." |
| 5 | +SCRIPT_PATH="$(cd "$(dirname "$0")" && pwd)/$(basename "$0")" |
| 6 | +REPO_ROOT=$(git -C "$(dirname "$SCRIPT_PATH")" rev-parse --show-toplevel) |
| 7 | +source $REPO_ROOT/scripts/env-setup || true |
| 8 | + |
| 9 | +if [[ -z "${OPENSSL_MODULES:-}" ]]; then |
| 10 | + echo "Environment not set up: OPENSSL_MODULES is not defined or empty" |
| 11 | + exit 1 |
| 12 | +elif [[ ! -d "$OPENSSL_MODULES" ]]; then |
| 13 | + echo "Could not find wolfProvider at $OPENSSL_MODULES" |
| 14 | + echo "Please build it first..." |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +echo "[*] Installing build dependencies..." |
| 19 | +apt-get update |
| 20 | +DEBIAN_FRONTEND=noninteractive apt-get install -y \ |
| 21 | + git \ |
| 22 | + build-essential \ |
| 23 | + autotools-dev \ |
| 24 | + autoconf \ |
| 25 | + libtool \ |
| 26 | + pkg-config \ |
| 27 | + libpam0g-dev \ |
| 28 | + libnss3-dev \ |
| 29 | + libpcsclite-dev \ |
| 30 | + opensc \ |
| 31 | + softhsm2 \ |
| 32 | + pcscd \ |
| 33 | + pcsc-tools \ |
| 34 | + sudo \ |
| 35 | + systemd \ |
| 36 | + ssh \ |
| 37 | + vim \ |
| 38 | + gnupg \ |
| 39 | + wget \ |
| 40 | + curl |
| 41 | + |
| 42 | +echo "[*] Cloning pam_pkcs11..." |
| 43 | +cd /opt |
| 44 | +if [[ ! -d "pam_pkcs11" ]]; then |
| 45 | + git clone https://github.com/OpenSC/pam_pkcs11.git |
| 46 | +fi |
| 47 | +cd pam_pkcs11 |
| 48 | + |
| 49 | +echo "[*] Building pam_pkcs11 from source..." |
| 50 | +./bootstrap |
| 51 | +./configure --prefix=/usr --sysconfdir=/etc --with-pam-dir=/lib/security --disable-nls |
| 52 | +make -j"$(nproc)" |
| 53 | +make install |
| 54 | + |
| 55 | +echo "[*] Creating test user..." |
| 56 | +if ! id -u testuser &>/dev/null; then |
| 57 | + useradd -m testuser |
| 58 | + echo 'testuser:testpass' | chpasswd |
| 59 | + echo "[*] Created user 'testuser'" |
| 60 | +else |
| 61 | + echo "[*] User 'testuser' already exists, skipping creation" |
| 62 | +fi |
| 63 | + |
| 64 | +echo "[*] Configuring pam_pkcs11..." |
| 65 | + |
| 66 | +# Generate dummy CA cert if missing |
| 67 | +if [ ! -f /test/certs/test-ca.crt ]; then |
| 68 | + echo "[*] Generating dummy test-ca.crt..." |
| 69 | + mkdir -p /test/certs |
| 70 | + openssl req -x509 -newkey rsa:2048 -nodes \ |
| 71 | + -keyout /test/certs/test-ca.key \ |
| 72 | + -out /test/certs/test-ca.crt \ |
| 73 | + -days 365 -subj "/CN=Test CA/O=Example" |
| 74 | +fi |
| 75 | + |
| 76 | +mkdir -p /etc/pam_pkcs11/cacerts |
| 77 | +cp /test/certs/test-ca.crt /etc/pam_pkcs11/cacerts/ |
| 78 | +pkcs11_make_hash_link /etc/pam_pkcs11/cacerts/ |
| 79 | + |
| 80 | +# Generate test certificate and key if missing |
| 81 | +if [ ! -f /test/certs/test-cert.pem ]; then |
| 82 | + echo "[*] Generating test-cert.pem and key..." |
| 83 | + mkdir -p /test/certs |
| 84 | + openssl req -newkey rsa:2048 -nodes \ |
| 85 | + -keyout /test/certs/test-key.pem \ |
| 86 | + -x509 -days 365 -out /test/certs/test-cert.pem \ |
| 87 | + -subj "/CN=Test User/OU=Testing/O=Example Corp/C=US" |
| 88 | +fi |
| 89 | + |
| 90 | +# Extract cert subject in one-line format suitable for pam_pkcs11 |
| 91 | +CERT_SUBJECT=$(openssl x509 -in /test/certs/test-cert.pem -noout -subject -nameopt oneline | sed 's/subject=//') |
| 92 | + |
| 93 | +echo "[*] Writing pkcs11_mapper.map with subject: $CERT_SUBJECT" |
| 94 | + |
| 95 | +echo "subject=$CERT_SUBJECT; uid=testuser" | tee /etc/pam_pkcs11/pkcs11_mapper.map > /dev/null |
| 96 | + |
| 97 | +# Backup and modify PAM config |
| 98 | +cp /etc/pam.d/common-auth /etc/pam.d/common-auth.bak |
| 99 | +echo "auth sufficient pam_pkcs11.so debug" | tee /etc/pam.d/common-auth > /dev/null |
| 100 | +cat /etc/pam.d/common-auth.bak | tee -a /etc/pam.d/common-auth > /dev/null |
| 101 | + |
| 102 | +echo "[*] Initializing SoftHSM (simulated smartcard)..." |
| 103 | +mkdir -p /var/lib/softhsm/tokens |
| 104 | +softhsm2-util --init-token --free --label "testtoken" --pin 1234 --so-pin 123456 |
| 105 | + |
| 106 | +echo "[*] Importing test certificate into SoftHSM..." |
| 107 | +pkcs11-tool --module /usr/lib/softhsm/libsofthsm2.so \ |
| 108 | + --login --pin 1234 --write-object /test/certs/test-cert.pem --type cert --label "testcert" |
| 109 | + |
| 110 | +echo "[*] Starting pcscd..." |
| 111 | +if ps aux | grep '[p]cscd' > /dev/null; then |
| 112 | + echo "pcscd is already running" |
| 113 | +else |
| 114 | + echo "pcscd is not running, starting it now..." |
| 115 | + pcscd & |
| 116 | +fi |
| 117 | + |
| 118 | +echo "[*] Creating pam_pkcs11.conf..." |
| 119 | +if [ -f "./etc/pam_pkcs11.conf.example" ]; then |
| 120 | + cp ./etc/pam_pkcs11.conf.example /etc/pam_pkcs11/pam_pkcs11.conf |
| 121 | +else |
| 122 | + echo "ERROR: pam_pkcs11.conf.example not found in current directory" |
| 123 | + exit 1 |
| 124 | +fi |
| 125 | + |
| 126 | +echo "[*] Configuring pam_pkcs11.conf for SoftHSM module..." |
| 127 | + |
| 128 | +# Set correct module usage line |
| 129 | +sed -i 's|^use_pkcs11_module.*|use_pkcs11_module = softhsm;|' /etc/pam_pkcs11/pam_pkcs11.conf |
| 130 | + |
| 131 | +# Set the SoftHSM module path |
| 132 | +sed -i '/^pkcs11_module softhsm {/,/^}/ s|^\s*module\s*=.*| module = /usr/lib/softhsm/libsofthsm2.so;|' /etc/pam_pkcs11/pam_pkcs11.conf |
| 133 | + |
| 134 | +echo "[*] Checking SoftHSM PKCS#11 module dependencies..." |
| 135 | +ldd /usr/lib/softhsm/libsofthsm2.so | tee /tmp/libsofthsm2.ldd |
| 136 | +if grep -q "not found" /tmp/libsofthsm2.ldd; then |
| 137 | + echo "ERROR: Missing dependencies for SoftHSM PKCS#11 module!" |
| 138 | + exit 1 |
| 139 | +fi |
| 140 | + |
| 141 | +echo "[*] Testing SoftHSM PKCS#11 module loadability with pkcs11-tool..." |
| 142 | +if ! pkcs11-tool --module /usr/lib/softhsm/libsofthsm2.so -L; then |
| 143 | + echo "ERROR: Failed to load SoftHSM PKCS#11 module" |
| 144 | + exit 1 |
| 145 | +fi |
| 146 | + |
| 147 | +echo "[*] Testing login via su..." |
| 148 | +su testuser -c 'echo "✅ Logged in as testuser"' |
| 149 | + |
| 150 | +echo "[*] All done." |
0 commit comments