| 1 |
#!/bin/bash |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
# Generate CA and certificates for testing; these are static due to the expired cert (see below) |
| 4 |
set -eux |
| 5 |
|
| 6 |
# Their lifetimes need to be long enough for long-term support distributions, i.e. > 15 years |
| 7 |
DAYS=10000 |
| 8 |
|
| 9 |
openssl req -config ca.conf -x509 -newkey rsa:2048 -out ca.pem -subj '/O=Cockpit/OU=test/CN=CA/' -nodes -days "$DAYS" |
| 10 |
mkdir certs |
| 11 |
touch index.txt |
| 12 |
echo 01 > serial |
| 13 |
|
| 14 |
for user in alice bob; do |
| 15 |
openssl genrsa -out ${user}.key 2048 |
| 16 |
|
| 17 |
openssl req -new -key ${user}.key -out ${user}.csr -config ca.conf -subj "/CN=${user}/DC=COCKPIT/DC=LAN/" |
| 18 |
openssl req -in ${user}.csr -text |
| 19 |
openssl ca -batch -config ca.conf -in ${user}.csr -days "$DAYS" -notext -extensions usr_cert -out ${user}.pem -subj "/CN=${user}/DC=COCKPIT/DC=LAN/" |
| 20 |
# for browser or smart card import |
| 21 |
openssl pkcs12 -export -password pass:foo -in ${user}.pem -inkey ${user}.key -out ${user}.p12 |
| 22 |
done |
| 23 |
|
| 24 |
# reset so that we can re-build cert with different lifetime |
| 25 |
rm index.txt* |
| 26 |
touch index.txt |
| 27 |
# there is no way to generate an immediately expired cert with "openssl ca", so this has to age for a day to really be expired; or |
| 28 |
# temporarily set back your clock one day |
| 29 |
openssl ca -batch -config ca.conf -in alice.csr -days 1 -notext -extensions usr_cert -out alice-expired.pem -subj "/CN=alice/DC=COCKPIT/DC=LAN/" |
| 30 |
|
| 31 |
rm -r certs |
| 32 |
rm -- index.txt* serial* *.csr |
| 33 |
|
| 34 |
# Update fingerprints in testing.h |
| 35 |
update_fingerprint() { # args: MACRO_NAME CERT_FILE |
| 36 |
local macro="$1" |
| 37 |
local fp |
| 38 |
fp="$(openssl x509 -in "$2" -noout -fingerprint -sha256)" |
| 39 |
local fpc |
| 40 |
fpc="$(cut -d= -f2 <<< "$fp" | tr -d ':' | tr '[:upper:]' '[:lower:]')" |
| 41 |
sed -i "s/^#define $macro.*/#define $macro \"$fpc\"/" ../testing.h |
| 42 |
echo " $macro: $fp" |
| 43 |
} |
| 44 |
|
| 45 |
update_fingerprint CLIENT_CERT_FINGERPRINT alice.pem |
| 46 |
update_fingerprint CLIENT_EXPIRED_FINGERPRINT alice-expired.pem |
| 47 |
update_fingerprint ALTERNATE_FINGERPRINT bob.pem |