2021-03-08 09:39:36 +00:00
|
|
|
#! /bin/bash
|
|
|
|
|
2021-03-18 01:09:32 +00:00
|
|
|
# Usage:
|
|
|
|
# ./gen_certs.sh [cert-kind]
|
|
|
|
#
|
|
|
|
# [cert-kind]:
|
|
|
|
# ed25519
|
|
|
|
# rsa_sha256
|
|
|
|
# ecdsa_nistp256_sha256
|
|
|
|
# ecdsa_nistp384_sha384
|
2024-03-26 22:38:36 +00:00
|
|
|
# ecdsa_nistp521_sha512
|
2024-04-16 09:39:52 +00:00
|
|
|
# client
|
2021-03-08 09:39:36 +00:00
|
|
|
#
|
2021-03-18 01:09:32 +00:00
|
|
|
# Generate a certificate of the [cert-kind] key type, or if no cert-kind is
|
|
|
|
# specified, all of the certificates.
|
2021-03-08 09:39:36 +00:00
|
|
|
#
|
2021-03-18 01:09:32 +00:00
|
|
|
# Examples:
|
|
|
|
# ./gen_certs.sh ed25519
|
|
|
|
# ./gen_certs.sh rsa_sha256
|
2021-03-08 09:39:36 +00:00
|
|
|
|
|
|
|
# TODO: `rustls` (really, `webpki`) doesn't currently use the CN in the subject
|
|
|
|
# to check if a certificate is valid for a server name sent via SNI. It's not
|
|
|
|
# clear if this is intended, since certificates _should_ have a `subjectAltName`
|
|
|
|
# with a DNS name, or if it simply hasn't been implemented yet. See
|
|
|
|
# https://bugzilla.mozilla.org/show_bug.cgi?id=552346 for a bit more info.
|
|
|
|
|
2021-03-18 01:09:32 +00:00
|
|
|
CA_SUBJECT="/C=US/ST=CA/O=Rocket CA/CN=Rocket Root CA"
|
2021-03-08 09:39:36 +00:00
|
|
|
SUBJECT="/C=US/ST=CA/O=Rocket/CN=localhost"
|
|
|
|
ALT="DNS:localhost"
|
|
|
|
|
2021-03-18 01:09:32 +00:00
|
|
|
function gen_ca() {
|
|
|
|
openssl genrsa -out ca_key.pem 4096
|
|
|
|
openssl req -new -x509 -days 3650 -key ca_key.pem \
|
|
|
|
-subj "${CA_SUBJECT}" -out ca_cert.pem
|
|
|
|
}
|
|
|
|
|
|
|
|
function gen_ca_if_non_existent() {
|
|
|
|
if ! [ -f ./ca_cert.pem ]; then gen_ca; fi
|
|
|
|
}
|
|
|
|
|
2021-03-08 09:39:36 +00:00
|
|
|
function gen_rsa_sha256() {
|
2021-03-18 01:09:32 +00:00
|
|
|
gen_ca_if_non_existent
|
|
|
|
|
|
|
|
openssl req -newkey rsa:4096 -nodes -sha256 -keyout rsa_sha256_key.pem \
|
|
|
|
-subj "${SUBJECT}" -out server.csr
|
2021-03-08 09:39:36 +00:00
|
|
|
|
2021-03-18 01:09:32 +00:00
|
|
|
openssl x509 -req -sha256 -extfile <(printf "subjectAltName=${ALT}") -days 3650 \
|
|
|
|
-CA ca_cert.pem -CAkey ca_key.pem -CAcreateserial \
|
|
|
|
-in server.csr -out rsa_sha256_cert.pem
|
|
|
|
|
2021-07-07 19:16:12 +00:00
|
|
|
openssl pkcs12 -export -password pass:rocket \
|
|
|
|
-in rsa_sha256_cert.pem -inkey rsa_sha256_key.pem -out rsa_sha256.p12
|
|
|
|
|
2021-03-18 01:09:32 +00:00
|
|
|
rm ca_cert.srl server.csr
|
2021-03-08 09:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function gen_ed25519() {
|
2021-03-18 01:09:32 +00:00
|
|
|
gen_ca_if_non_existent
|
|
|
|
|
|
|
|
openssl genpkey -algorithm ED25519 > ed25519_key.pem
|
2021-03-08 09:39:36 +00:00
|
|
|
|
2021-03-18 01:09:32 +00:00
|
|
|
openssl req -new -key ed25519_key.pem -subj "${SUBJECT}" -out server.csr
|
2021-07-07 19:16:12 +00:00
|
|
|
|
2021-03-18 01:09:32 +00:00
|
|
|
openssl x509 -req -extfile <(printf "subjectAltName=${ALT}") -days 3650 \
|
|
|
|
-CA ca_cert.pem -CAkey ca_key.pem -CAcreateserial \
|
|
|
|
-in server.csr -out ed25519_cert.pem
|
2021-03-08 09:39:36 +00:00
|
|
|
|
2021-07-07 19:16:12 +00:00
|
|
|
openssl pkcs12 -export -password pass:rocket \
|
|
|
|
-in ed25519_cert.pem -inkey ed25519_key.pem -out ed25519.p12
|
|
|
|
|
2021-03-18 01:09:32 +00:00
|
|
|
rm ca_cert.srl server.csr
|
2021-03-08 09:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function gen_ecdsa_nistp256_sha256() {
|
2021-03-18 01:09:32 +00:00
|
|
|
gen_ca_if_non_existent
|
2021-03-08 09:39:36 +00:00
|
|
|
|
2021-03-18 01:09:32 +00:00
|
|
|
openssl ecparam -out ecdsa_nistp256_sha256_key.pem -name prime256v1 -genkey
|
2021-03-08 09:39:36 +00:00
|
|
|
|
2021-03-18 01:09:32 +00:00
|
|
|
# Convert to pkcs8 format supported by rustls
|
|
|
|
openssl pkcs8 -topk8 -nocrypt -in ecdsa_nistp256_sha256_key.pem \
|
|
|
|
-out ecdsa_nistp256_sha256_key_pkcs8.pem
|
2021-03-08 09:39:36 +00:00
|
|
|
|
2021-03-18 01:09:32 +00:00
|
|
|
openssl req -new -nodes -sha256 -key ecdsa_nistp256_sha256_key_pkcs8.pem \
|
|
|
|
-subj "${SUBJECT}" -out server.csr
|
2021-03-08 09:39:36 +00:00
|
|
|
|
2021-03-18 01:09:32 +00:00
|
|
|
openssl x509 -req -sha256 -extfile <(printf "subjectAltName=${ALT}") -days 3650 \
|
|
|
|
-CA ca_cert.pem -CAkey ca_key.pem -CAcreateserial \
|
|
|
|
-in server.csr -out ecdsa_nistp256_sha256_cert.pem
|
|
|
|
|
2021-07-07 19:16:12 +00:00
|
|
|
openssl pkcs12 -export -password pass:rocket -in ecdsa_nistp256_sha256_cert.pem \
|
|
|
|
-inkey ecdsa_nistp256_sha256_key_pkcs8.pem -out ecdsa_nistp256_sha256.p12
|
|
|
|
|
2021-03-18 01:09:32 +00:00
|
|
|
rm ca_cert.srl server.csr ecdsa_nistp256_sha256_key.pem
|
|
|
|
}
|
2021-03-08 09:39:36 +00:00
|
|
|
|
|
|
|
function gen_ecdsa_nistp384_sha384() {
|
2021-03-18 01:09:32 +00:00
|
|
|
gen_ca_if_non_existent
|
|
|
|
|
|
|
|
openssl ecparam -out ecdsa_nistp384_sha384_key.pem -name secp384r1 -genkey
|
|
|
|
|
|
|
|
# Convert to pkcs8 format supported by rustls
|
|
|
|
openssl pkcs8 -topk8 -nocrypt -in ecdsa_nistp384_sha384_key.pem \
|
|
|
|
-out ecdsa_nistp384_sha384_key_pkcs8.pem
|
2021-03-08 09:39:36 +00:00
|
|
|
|
2021-03-18 01:09:32 +00:00
|
|
|
openssl req -new -nodes -sha384 -key ecdsa_nistp384_sha384_key_pkcs8.pem \
|
|
|
|
-subj "${SUBJECT}" -out server.csr
|
2021-03-08 09:39:36 +00:00
|
|
|
|
2021-03-18 01:09:32 +00:00
|
|
|
openssl x509 -req -sha384 -extfile <(printf "subjectAltName=${ALT}") -days 3650 \
|
|
|
|
-CA ca_cert.pem -CAkey ca_key.pem -CAcreateserial \
|
|
|
|
-in server.csr -out ecdsa_nistp384_sha384_cert.pem
|
2021-03-08 09:39:36 +00:00
|
|
|
|
2021-07-07 19:16:12 +00:00
|
|
|
openssl pkcs12 -export -password pass:rocket -in ecdsa_nistp384_sha384_cert.pem \
|
|
|
|
-inkey ecdsa_nistp384_sha384_key_pkcs8.pem -out ecdsa_nistp384_sha384.p12
|
|
|
|
|
2021-03-18 01:09:32 +00:00
|
|
|
rm ca_cert.srl server.csr ecdsa_nistp384_sha384_key.pem
|
2021-03-08 09:39:36 +00:00
|
|
|
}
|
|
|
|
|
2024-03-25 12:31:00 +00:00
|
|
|
function gen_ecdsa_nistp521_sha512() {
|
|
|
|
gen_ca_if_non_existent
|
|
|
|
|
|
|
|
openssl ecparam -out ecdsa_nistp521_sha512_key.pem -name secp521r1 -genkey
|
|
|
|
|
|
|
|
# Convert to pkcs8 format supported by rustls
|
|
|
|
openssl pkcs8 -topk8 -nocrypt -in ecdsa_nistp521_sha512_key.pem \
|
|
|
|
-out ecdsa_nistp521_sha512_key_pkcs8.pem
|
|
|
|
|
|
|
|
openssl req -new -nodes -sha512 -key ecdsa_nistp521_sha512_key_pkcs8.pem \
|
|
|
|
-subj "${SUBJECT}" -out server.csr
|
|
|
|
|
|
|
|
openssl x509 -req -sha512 -extfile <(printf "subjectAltName=${ALT}") -days 3650 \
|
|
|
|
-CA ca_cert.pem -CAkey ca_key.pem -CAcreateserial \
|
|
|
|
-in server.csr -out ecdsa_nistp521_sha512_cert.pem
|
|
|
|
|
|
|
|
openssl pkcs12 -export -password pass:rocket -in ecdsa_nistp521_sha512_cert.pem \
|
|
|
|
-inkey ecdsa_nistp521_sha512_key_pkcs8.pem -out ecdsa_nistp521_sha512.p12
|
|
|
|
|
|
|
|
rm ca_cert.srl server.csr ecdsa_nistp521_sha512_key.pem
|
|
|
|
}
|
|
|
|
|
2024-04-16 09:39:52 +00:00
|
|
|
function gen_client_cert() {
|
|
|
|
openssl req -newkey rsa:2048 -nodes -keyout client.key -out client.csr
|
|
|
|
openssl x509 -req -extfile <(printf "subjectAltName=DNS:${ALT}") -days 365 \
|
|
|
|
-in client.csr -CA ca_cert.pem -CAkey ca_key.pem -CAcreateserial \
|
|
|
|
-out client.crt
|
|
|
|
|
|
|
|
cat client.key client.crt ca_cert.pem > client.pem
|
|
|
|
rm client.key client.crt client.csr ca_cert.srl
|
|
|
|
}
|
|
|
|
|
2021-03-08 09:39:36 +00:00
|
|
|
case $1 in
|
|
|
|
ed25519) gen_ed25519 ;;
|
|
|
|
rsa_sha256) gen_rsa_sha256 ;;
|
|
|
|
ecdsa_nistp256_sha256) gen_ecdsa_nistp256_sha256 ;;
|
|
|
|
ecdsa_nistp384_sha384) gen_ecdsa_nistp384_sha384 ;;
|
2024-03-25 12:31:00 +00:00
|
|
|
ecdsa_nistp521_sha512) gen_ecdsa_nistp521_sha512 ;;
|
2024-04-16 09:39:52 +00:00
|
|
|
client) gen_client_cert ;;
|
2021-03-08 09:39:36 +00:00
|
|
|
*)
|
|
|
|
gen_ed25519
|
|
|
|
gen_rsa_sha256
|
|
|
|
gen_ecdsa_nistp256_sha256
|
|
|
|
gen_ecdsa_nistp384_sha384
|
2024-03-25 12:31:00 +00:00
|
|
|
gen_ecdsa_nistp521_sha512
|
2021-03-08 09:39:36 +00:00
|
|
|
;;
|
|
|
|
esac
|