2019-09-14 10:21:41 +02:00
#!/bin/sh
2022-07-20 09:21:40 +02:00
set -e
2022-07-07 05:03:08 +02:00
# you can set the hostname if you want, but it'll default to localhost
if [ -z " $CERT_HOSTNAME " ] ; then
CERT_HOSTNAME = "localhost"
fi
2022-03-28 00:36:25 +02:00
2022-07-07 05:03:08 +02:00
# also where the files are stored
if [ -z " $KANI_TMP " ] ; then
KANI_TMP = /tmp/kanidm/
fi
2022-03-28 00:36:25 +02:00
ALTNAME_FILE = " ${ KANI_TMP } altnames.cnf "
2022-09-02 06:21:20 +02:00
CANAME_FILE = " ${ KANI_TMP } ca.cnf "
2022-03-28 00:36:25 +02:00
CACERT = " ${ KANI_TMP } ca.pem "
CAKEY = " ${ KANI_TMP } cakey.pem "
2022-09-02 06:21:20 +02:00
CADB = " ${ KANI_TMP } ca.txt "
CASRL = " ${ KANI_TMP } ca.srl "
2022-03-28 00:36:25 +02:00
KEYFILE = " ${ KANI_TMP } key.pem "
CERTFILE = " ${ KANI_TMP } cert.pem "
CSRFILE = " ${ KANI_TMP } cert.csr "
CHAINFILE = " ${ KANI_TMP } chain.pem "
2022-06-20 12:16:55 +02:00
DHFILE = " ${ KANI_TMP } dh.pem "
2022-03-28 00:36:25 +02:00
if [ ! -d " ${ KANI_TMP } " ] ; then
echo " Creating temp kanidm dir: ${ KANI_TMP } "
mkdir -p " ${ KANI_TMP } "
fi
2022-09-02 06:21:20 +02:00
cat > " ${ CANAME_FILE } " << DEVEOF
[ req]
nsComment = "Certificate Authority"
distinguished_name = req_distinguished_name
req_extensions = v3_ca
[ req_distinguished_name ]
countryName = Country Name ( 2 letter code)
countryName_default = AU
countryName_min = 2
countryName_max = 2
stateOrProvinceName = State or Province Name ( full name)
stateOrProvinceName_default = Queensland
localityName = Locality Name ( eg, city)
localityName_default = Brisbane
0.organizationName = Organization Name ( eg, company)
0.organizationName_default = INSECURE EXAMPLE
organizationalUnitName = Organizational Unit Name ( eg, section)
organizationalUnitName_default = kanidm
commonName = Common Name ( eg, your name or your server\' s hostname)
commonName_max = 64
commonName_default = insecure.ca.localhost
[ v3_ca ]
subjectKeyIdentifier = hash
basicConstraints = critical, CA:true
keyUsage = critical, digitalSignature, cRLSign, keyCertSign
DEVEOF
2022-03-28 00:36:25 +02:00
cat > " ${ ALTNAME_FILE } " << DEVEOF
2022-09-02 06:21:20 +02:00
[ ca]
default_ca = CA_default
[ CA_default ]
# Directory and file locations.
dir = ${ KANI_TMP }
certs = ${ KANI_TMP }
crl_dir = ${ KANI_TMP }
new_certs_dir = ${ KANI_TMP }
database = ${ CADB }
serial = ${ CASRL }
# The root key and root certificate.
private_key = ${ CAKEY }
certificate = ${ CACERT }
# SHA-1 is deprecated, so use SHA-2 instead.
default_md = sha256
name_opt = ca_default
cert_opt = ca_default
default_days = 3650
preserve = no
policy = policy_loose
[ policy_loose ]
countryName = optional
stateOrProvinceName = optional
localityName = optional
organizationName = optional
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
2019-09-14 10:21:41 +02:00
[ req]
nsComment = "Certificate"
distinguished_name = req_distinguished_name
2019-11-16 05:40:45 +01:00
req_extensions = v3_req
2019-09-14 10:21:41 +02:00
[ req_distinguished_name ]
countryName = Country Name ( 2 letter code)
countryName_default = AU
countryName_min = 2
countryName_max = 2
stateOrProvinceName = State or Province Name ( full name)
stateOrProvinceName_default = Queensland
localityName = Locality Name ( eg, city)
localityName_default = Brisbane
0.organizationName = Organization Name ( eg, company)
0.organizationName_default = INSECURE EXAMPLE
organizationalUnitName = Organizational Unit Name ( eg, section)
2022-06-20 12:16:55 +02:00
organizationalUnitName_default = kanidm
2019-09-14 10:21:41 +02:00
commonName = Common Name ( eg, your name or your server\' s hostname)
commonName_max = 64
2022-09-02 06:21:20 +02:00
commonName_default = ${ CERT_HOSTNAME }
2019-09-14 10:21:41 +02:00
[ v3_req ]
basicConstraints = CA:FALSE
2022-09-02 06:21:20 +02:00
nsCertType = server
nsComment = "Server Certificate"
subjectKeyIdentifier = hash
keyUsage = critical, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
2019-09-14 10:21:41 +02:00
subjectAltName = @alt_names
[ alt_names]
2019-11-16 05:40:45 +01:00
DNS.1 = localhost
IP.1 = 127.0.0.1
2019-09-14 10:21:41 +02:00
DEVEOF
2022-09-02 06:21:20 +02:00
touch ${ CADB }
echo 1000 > ${ CASRL }
2022-09-20 06:23:54 +02:00
echo "Make the ca key..."
2022-09-02 06:21:20 +02:00
openssl ecparam -genkey -name prime256v1 -noout -out " ${ CAKEY } "
2022-09-20 06:23:54 +02:00
echo "Self sign the CA..."
openssl req -batch -config " ${ CANAME_FILE } " \
2022-09-02 06:21:20 +02:00
-key " ${ CAKEY } " \
-new -x509 -days +31 \
-sha256 -extensions v3_ca \
2022-03-28 00:36:25 +02:00
-out " ${ CACERT } " \
2022-09-02 06:21:20 +02:00
-nodes
2022-03-28 00:36:25 +02:00
2022-09-20 06:23:54 +02:00
echo "Generating the server private key..."
2022-09-02 06:21:20 +02:00
openssl ecparam -genkey -name prime256v1 -noout -out " ${ KEYFILE } "
2022-03-28 00:36:25 +02:00
2022-09-20 06:23:54 +02:00
echo "Generating the certficate signing request..."
2022-09-02 06:21:20 +02:00
openssl req -sha256 -new \
2022-09-20 06:23:54 +02:00
-batch \
2022-09-02 06:21:20 +02:00
-config " ${ ALTNAME_FILE } " -extensions v3_req \
2022-03-28 00:36:25 +02:00
-key " ${ KEYFILE } " \
2022-07-07 05:03:08 +02:00
-nodes \
2022-03-28 00:36:25 +02:00
-out " ${ CSRFILE } "
2022-07-07 05:03:08 +02:00
2022-09-20 06:23:54 +02:00
echo "Signing the certificate..."
2022-09-02 06:21:20 +02:00
openssl ca -config " ${ ALTNAME_FILE } " \
2022-09-20 06:23:54 +02:00
-batch \
2022-09-02 06:21:20 +02:00
-extensions v3_req \
-days 31 -notext -md sha256 \
2022-03-28 00:36:25 +02:00
-in " ${ CSRFILE } " \
2022-09-02 06:21:20 +02:00
-out " ${ CERTFILE } "
2021-02-16 02:40:25 +01:00
# Create the chain
2022-03-28 00:36:25 +02:00
cat " ${ CERTFILE } " " ${ CACERT } " > " ${ CHAINFILE } "
2019-09-14 10:21:41 +02:00
2022-06-20 12:16:55 +02:00
# create the dh file for RADIUS
openssl dhparam -in " ${ CAFILE } " -out " ${ DHFILE } " 2048
2022-03-28 00:36:25 +02:00
echo " Certificate chain is at: ${ CHAINFILE } "
echo " Private key is at: ${ KEYFILE } "
2022-07-07 05:03:08 +02:00
echo ""
2022-09-02 06:21:20 +02:00
echo "**Remember** the default action is to store the files in /tmp/ so they'll be deleted on reboot! Set the KANI_TMP environment variable before running this script if you want to change that. You'll need to update server config elsewhere if you do, however."