-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds 3-level certificate chain generation script
Signed-off-by: Akhilesh Kr. Yadav <akhileshkr.yadav@Akhileshs-MacBook-Air.local> Signed-off-by: Akhilesh Kr. Yadav <akhileshkr.yadav@Akhileshs-MacBook-Air.local>
- Loading branch information
Akhilesh Kr. Yadav
authored and
Akhilesh Kr. Yadav
committed
Feb 10, 2025
1 parent
14a8274
commit 5be2f7a
Showing
6 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-----BEGIN EC PRIVATE KEY----- | ||
MHcCAQEEICoAlM1RuYHR4AdyqUgP6o4rx9XlNa3aj8fBqrTboTSvoAoGCCqGSM49 | ||
AwEHoUQDQgAE22AhzRa88KigQuv2dI2ILdJsLIOmtqzSUrt79UP/98+OYeFclOED | ||
NjdahFsdZcZ9tyUEUF3I23Prl1RMszEh/Q== | ||
-----END EC PRIVATE KEY----- |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
#!/bin/bash | ||
# SPDX-License-Identifier: Apache-2.0 | ||
set -e | ||
|
||
ROOT_CERT_NAME=rootCA | ||
INTERMEDIATE_CERT_NAME=intermediateCA | ||
END_ENTITY_CERT_NAME=endEntity | ||
|
||
THIS_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | ||
MISC_DIR="$THIS_DIR/../misc" | ||
|
||
mkdir -p "$MISC_DIR" | ||
|
||
function create_root_cert() { | ||
if [[ -f "${MISC_DIR}/${ROOT_CERT_NAME}.der" ]]; then | ||
echo "Root certificate already exists. Skipping creation." | ||
return | ||
fi | ||
openssl ecparam -name prime256v1 -genkey -noout -out ${MISC_DIR}/${ROOT_CERT_NAME}.key | ||
openssl req -x509 -new -nodes -key ${MISC_DIR}/${ROOT_CERT_NAME}.key -sha256 -days 3650 \ | ||
-subj "/CN=Acme Inc." -out ${MISC_DIR}/${ROOT_CERT_NAME}.crt | ||
openssl x509 -in ${MISC_DIR}/${ROOT_CERT_NAME}.crt -outform der -out ${MISC_DIR}/${ROOT_CERT_NAME}.der | ||
rm -f ${MISC_DIR}/${ROOT_CERT_NAME}.crt # Remove the PEM certificate | ||
echo "Created ${MISC_DIR}/${ROOT_CERT_NAME}.der and ${MISC_DIR}/${ROOT_CERT_NAME}.key" | ||
} | ||
|
||
function create_intermediate_cert() { | ||
if [[ -f "${MISC_DIR}/${INTERMEDIATE_CERT_NAME}.der" ]]; then | ||
echo "Intermediate certificate already exists. Skipping creation." | ||
return | ||
fi | ||
openssl ecparam -name prime256v1 -genkey -noout -out ${MISC_DIR}/${INTERMEDIATE_CERT_NAME}.key | ||
openssl req -new -key ${MISC_DIR}/${INTERMEDIATE_CERT_NAME}.key -out ${MISC_DIR}/${INTERMEDIATE_CERT_NAME}.csr -subj "/CN=Acme Gizmos" | ||
openssl x509 -req -in ${MISC_DIR}/${INTERMEDIATE_CERT_NAME}.csr -CA ${MISC_DIR}/${ROOT_CERT_NAME}.der -CAkey ${MISC_DIR}/${ROOT_CERT_NAME}.key -CAcreateserial -out ${MISC_DIR}/${INTERMEDIATE_CERT_NAME}.crt -days 1825 -sha256 | ||
openssl x509 -in ${MISC_DIR}/${INTERMEDIATE_CERT_NAME}.crt -outform der -out ${MISC_DIR}/${INTERMEDIATE_CERT_NAME}.der | ||
rm -f ${MISC_DIR}/${INTERMEDIATE_CERT_NAME}.crt # Remove the PEM certificate | ||
echo "Created ${MISC_DIR}/${INTERMEDIATE_CERT_NAME}.der and ${MISC_DIR}/${INTERMEDIATE_CERT_NAME}.key" | ||
} | ||
|
||
function create_end_entity_cert() { | ||
if ([[ -f "${MISC_DIR}/${END_ENTITY_CERT_NAME}.der" ]] && [[ -f "${MISC_DIR}/${END_ENTITY_CERT_NAME}.key" ]]); then | ||
echo "End-entity certificate and key already exist. Skipping creation." | ||
return | ||
fi | ||
openssl ecparam -name prime256v1 -genkey -noout -out ${MISC_DIR}/${END_ENTITY_CERT_NAME}.key | ||
openssl req -new -key ${MISC_DIR}/${END_ENTITY_CERT_NAME}.key -out ${MISC_DIR}/${END_ENTITY_CERT_NAME}.csr -subj "/CN=Acme Gizmo CoRIM signer" | ||
openssl x509 -req -in ${MISC_DIR}/${END_ENTITY_CERT_NAME}.csr -CA ${MISC_DIR}/${INTERMEDIATE_CERT_NAME}.der -CAkey ${MISC_DIR}/${INTERMEDIATE_CERT_NAME}.key -CAcreateserial -out ${MISC_DIR}/${END_ENTITY_CERT_NAME}.crt -days 825 -sha256 -CAform der | ||
openssl x509 -in ${MISC_DIR}/${END_ENTITY_CERT_NAME}.crt -outform der -out ${MISC_DIR}/${END_ENTITY_CERT_NAME}.der | ||
rm -f ${MISC_DIR}/${END_ENTITY_CERT_NAME}.crt # Remove the PEM certificate | ||
echo "Created ${MISC_DIR}/${END_ENTITY_CERT_NAME}.der and ${MISC_DIR}/${END_ENTITY_CERT_NAME}.key" | ||
} | ||
|
||
function clean_intermediate() { | ||
pushd "$MISC_DIR" > /dev/null || exit 1 | ||
echo "rm -f -- *.csr *.srl" | ||
rm -f -- *.csr *.srl | ||
popd > /dev/null || exit 1 | ||
} | ||
|
||
function clean_cert() { | ||
pushd "$MISC_DIR" > /dev/null || exit 1 | ||
local cert="$1" | ||
echo "rm -f \"${cert}.der\" \"${cert}.key\"" | ||
rm -f "${cert}.der" "${cert}.key" | ||
popd > /dev/null || exit 1 | ||
} | ||
|
||
function clean_all() { | ||
clean_intermediate | ||
clean_cert "$ROOT_CERT_NAME" | ||
clean_cert "$INTERMEDIATE_CERT_NAME" | ||
clean_cert "$END_ENTITY_CERT_NAME" | ||
} | ||
|
||
function help() { | ||
set +e | ||
read -r -d '' usage <<-EOF | ||
Usage: gen-certs [-h] [-C] [COMMAND] | ||
This script is used to (re-)generate certificates used for a veraison | ||
deployment. The certificates are signed by a CA certificate called | ||
${ROOT_CERT_NAME}.crt. If this does not exist, a self-signed one will | ||
be generated. | ||
Commands: | ||
create | ||
Create the root, intermediate, and end-entity certificates. | ||
clean | ||
Clean output artifacts for the certificates. | ||
clean_all | ||
Clean both intermediate and output artifacts for everything (including | ||
the root CA cert). | ||
help | ||
Print this message and exit (same as -h option). | ||
Options: | ||
-h Print this message and exit. | ||
-C Do not clean up intermediate artifacts (e.g., CSRs). | ||
EOF | ||
|
||
echo "$usage" | ||
} | ||
|
||
function _check_openssl() { | ||
if [[ "$(which openssl 2>/dev/null)" == "" ]]; then | ||
echo -e "ERROR: openssl executable must be installed to use this command." | ||
exit 1 | ||
fi | ||
} | ||
|
||
function _check_root_cert() { | ||
if [[ ! -f "${MISC_DIR}/${ROOT_CERT_NAME}.der" ]]; then | ||
create_root_cert | ||
fi | ||
} | ||
|
||
_should_clean_intermediate=true | ||
|
||
OPTIND=1 | ||
|
||
while getopts "hC" opt; do | ||
case "$opt" in | ||
h) help; exit 0;; | ||
C) _should_clean_intermediate=false;; | ||
*) break;; | ||
esac | ||
done | ||
|
||
shift $((OPTIND-1)) | ||
[ "${1:-}" = "--" ] && shift | ||
|
||
command=$1 | ||
case $command in | ||
help) | ||
help | ||
exit 0 | ||
;; | ||
clean) | ||
clean_intermediate | ||
;; | ||
clean_all) | ||
clean_all | ||
;; | ||
create) | ||
create_root_cert | ||
create_intermediate_cert | ||
create_end_entity_cert | ||
if [[ $_should_clean_intermediate == true ]]; then | ||
clean_intermediate | ||
fi | ||
;; | ||
*) | ||
echo -e "ERROR: unexpected command: \"$command\" (use -h for help)" | ||
;; | ||
esac |