2022-02-09 16:58:35 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2022-07-07 15:19:48 +00:00
|
|
|
source bin/helper.sh
|
|
|
|
|
2022-02-09 16:58:35 +00:00
|
|
|
WORKDIR=$(dirname "$0")
|
|
|
|
LOCAL_DOMAIN=$1
|
|
|
|
|
|
|
|
CA_KEY=$WORKDIR/ca-key.pem
|
|
|
|
CA_CRT=$WORKDIR/ca-cert.pem
|
|
|
|
|
|
|
|
SRV_KEY=$WORKDIR/server-key.pem
|
|
|
|
SRV_REQ=$WORKDIR/server-req.csr
|
|
|
|
SRV_CRT=$WORKDIR/server-cert.pem
|
|
|
|
|
|
|
|
CLI_KEY=$WORKDIR/client-key.pem
|
|
|
|
CLI_REQ=$WORKDIR/client-req.csr
|
|
|
|
CLI_CRT=$WORKDIR/client-cert.pem
|
|
|
|
|
2023-06-07 09:30:48 +00:00
|
|
|
SUBJ="/O=TrueCloudLab"
|
2022-02-09 16:58:35 +00:00
|
|
|
|
|
|
|
if [[ ! -f $CA_KEY || ! -f $CA_CRT ]]; then
|
2022-07-07 15:19:48 +00:00
|
|
|
openssl req -newkey rsa:4096 -x509 -days 365 -nodes -keyout $CA_KEY -out $CA_CRT -subj $SUBJ 2>&1 ||
|
|
|
|
die "CA certificate was not created"
|
2022-02-09 16:58:35 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! -f $SRV_KEY || ! -f $SRV_CRT ]]; then
|
2022-07-07 15:19:48 +00:00
|
|
|
openssl req -newkey rsa:4096 -nodes -keyout $SRV_KEY -out $SRV_REQ -subj $SUBJ 2>&1 ||
|
|
|
|
die "Server certificate was not created"
|
2022-02-09 16:58:35 +00:00
|
|
|
|
2022-07-07 15:19:48 +00:00
|
|
|
openssl x509 -req -days 365 -set_serial 01 -in $SRV_REQ -out $SRV_CRT -CA $CA_CRT -CAkey $CA_KEY \
|
|
|
|
-extensions san -extfile <(printf "[san]\nsubjectAltName=DNS:nats.$LOCAL_DOMAIN") 2>&1 || {
|
2022-02-09 16:58:35 +00:00
|
|
|
rm $SRV_REQ
|
2022-07-07 15:19:48 +00:00
|
|
|
die "Server certificate was not signed by CA"
|
2022-02-09 16:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rm $SRV_REQ
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! -f $CLI_KEY || ! -f $CLI_CRT ]]; then
|
2022-07-07 15:19:48 +00:00
|
|
|
openssl req -newkey rsa:4096 -nodes -keyout $CLI_KEY -out $CLI_REQ -subj $SUBJ 2>&1 ||
|
|
|
|
die "Client certificate was not created"
|
2022-02-09 16:58:35 +00:00
|
|
|
|
2022-07-07 15:19:48 +00:00
|
|
|
openssl x509 -req -days 365 -set_serial 01 -in $CLI_REQ -out $CLI_CRT -CA $CA_CRT -CAkey $CA_KEY 2>&1 || {
|
2022-02-09 16:58:35 +00:00
|
|
|
rm $CLI_REQ
|
2022-07-07 15:19:48 +00:00
|
|
|
die "Client certificate was not signed by CA"
|
2022-02-09 16:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rm $CLI_REQ
|
|
|
|
fi
|