@@ -13,23 +13,27 @@ set -e
1313
1414# Generate root CA
1515function generate_ca_root {
16- local CA_ROOT_PASS=" ${1} "
17- local AUTHORITY=" ${2} "
16+ local AUTHORITY_NAME=" ${1} "
17+
18+ pki_logger " Creating CA-root for authority ${AUTHORITY_NAME} ..."
19+
20+ # set passphrase for ca-root and store it in the vault-ca
21+ local CA_ROOT_PASS=$( setPassphrase ca " ca_root_${AUTHORITY_NAME} " )
1822
1923 # Correctly set certificate CN (env var is read inside the openssl configuration file)
20- export OPENSSL_CN=" ca-root_${AUTHORITY } "
24+ export OPENSSL_CN=" ca-root_${AUTHORITY_NAME } "
2125 pki_logger " OPENSSL_CN : ${OPENSSL_CN} "
2226 # Correctly set certificate DIRECTORY (env var is read inside the openssl configuration file)
23- export OPENSSL_CA_DIR=" ${AUTHORITY } "
27+ export OPENSSL_CA_DIR=" ${AUTHORITY_NAME } "
2428 pki_logger " OPENSSL_CA_DIR : ${OPENSSL_CA_DIR} "
2529
2630 local CA_DIR=${CA_DIR} /${OPENSSL_CA_DIR}
2731 if [ ! -d ${CA_DIR} ]; then
28- pki_logger " Create directory ${CA_DIR} "
32+ pki_logger " Creating directory ${CA_DIR} "
2933 mkdir -p ${CA_DIR} ;
3034 fi
3135
32- pki_logger " Create CA-root request..."
36+ pki_logger " Creating CA-root request for authority ${AUTHORITY_NAME} ..."
3337 openssl req \
3438 -config ${CONFIG_DIR} /ca-config \
3539 -new \
@@ -38,7 +42,7 @@ function generate_ca_root {
3842 -passout pass:${CA_ROOT_PASS} \
3943 -batch
4044
41- pki_logger " Sign CA-root certificate..."
45+ pki_logger " Signing CA-root certificate for authority ${AUTHORITY_NAME} ..."
4246 openssl ca \
4347 -config ${CONFIG_DIR} /ca-config \
4448 -selfsign \
@@ -51,24 +55,29 @@ function generate_ca_root {
5155
5256# Generate intermediate CA
5357function generate_ca_intermediate {
54- local CA_INTERMEDIATE_PASS=" ${1} "
55- local CA_ROOT_PASS=" ${2} "
56- local AUTHORITY=" ${3} "
58+ local AUTHORITY_NAME=" ${1} "
59+
60+ pki_logger " Creating CA-intermediate for authority ${AUTHORITY_NAME} ..."
61+
62+ # get passphrase for ca-root from the vault-ca
63+ local CA_ROOT_PASS=$( getPassphrase ca " ca_root_${AUTHORITY_NAME} " )
64+ # set passphrase for ca-intermediate and store it in the vault-ca
65+ local CA_INTERMEDIATE_PASS=$( setPassphrase ca " ca_intermediate_${AUTHORITY_NAME} " )
5766
5867 # Correctly set certificate CN (env var is read inside the openssl configuration file)
59- export OPENSSL_CN=" ca-intermediate_${AUTHORITY } "
68+ export OPENSSL_CN=" ca-intermediate_${AUTHORITY_NAME } "
6069 pki_logger " OPENSSL_CN : ${OPENSSL_CN} "
6170 # Correctly set certificate DIRECTORY (env var is read inside the openssl configuration file)
62- export OPENSSL_CA_DIR=${AUTHORITY }
71+ export OPENSSL_CA_DIR=${AUTHORITY_NAME }
6372 pki_logger " OPENSSL_CA_DIR : ${OPENSSL_CA_DIR} "
6473
6574 local CA_DIR=${CA_DIR} /${OPENSSL_CA_DIR}
6675 if [ ! -d ${CA_DIR} ]; then
67- pki_logger " Create directory ${OPENSSL_CA_DIR} "
76+ pki_logger " Creating directory ${OPENSSL_CA_DIR} "
6877 mkdir -p ${CA_DIR} ;
6978 fi
7079
71- pki_logger " Create CA-intermediate request..."
80+ pki_logger " Creating CA-intermediate request for authority ${AUTHORITY_NAME} ..."
7281 openssl req \
7382 -config ${CONFIG_DIR} /ca-config \
7483 -new \
@@ -78,7 +87,7 @@ function generate_ca_intermediate {
7887 -passout pass:${CA_INTERMEDIATE_PASS} \
7988 -batch
8089
81- pki_logger " Sign CA-intermediate certificate..."
90+ pki_logger " Signing CA-intermediate certificate for authority ${AUTHORITY_NAME} ..."
8291 openssl ca \
8392 -config ${CONFIG_DIR} /ca-config \
8493 -extensions extension_ca_intermediate \
@@ -100,11 +109,6 @@ function init_config_ca {
100109 touch " ${CONFIG_DIR} /${CA_DIR} /crlnumber"
101110}
102111
103- function get_autorities() {
104- # To override
105- echo " "
106- }
107-
108112# ###############################################################################
109113# ################################# Main ##################################
110114# ###############################################################################
@@ -152,30 +156,23 @@ function main() {
152156 fi
153157
154158 # Create CA per authorities
155- AUTHORITIES=" $( get_autorities) "
156- for AUTHORITY in ${AUTHORITIES[@]}
157- do
158- mkdir -p ${CA_DIR} /${AUTHORITY}
159- init_config_ca ${AUTHORITY}
160-
161- if [ ! -f ${CA_DIR} /${AUTHORITY} /ca-root.crt ]; then
162- pki_logger " Creation of CA-root for ${AUTHORITY} ..."
163- # Generate CA_ROOT_PASS & store it in the vault-ca
164- CA_ROOT_PASS=$( setPassphrase ca " ca_root_${AUTHORITY} " )
165- generate_ca_root ${CA_ROOT_PASS} ${AUTHORITY}
159+ for AUTHORITY_NAME in $( get_autorities) ; do
160+ mkdir -p ${CA_DIR} /${AUTHORITY_NAME}
161+ init_config_ca ${AUTHORITY_NAME}
162+
163+ if [ ! -f ${CA_DIR} /${AUTHORITY_NAME} /ca-root.crt ]; then
164+ # Generate ca-root for authority & store passphrase in the vault-ca
165+ generate_ca_root ${AUTHORITY_NAME}
166166 else
167- pki_logger " CA-root for ${AUTHORITY } already exists, it will not be recreated..."
167+ pki_logger " CA-root for authority ${AUTHORITY_NAME } already exists, it will not be recreated..."
168168 fi
169- if [ ! -f ${CA_DIR} /${AUTHORITY} /ca-intermediate.crt ]; then
170- pki_logger " Creation of CA-intermediate for ${AUTHORITY} ..."
171- # Generate CA_INTERMEDIATE_PASS & store it in the vault-ca
172- CA_INTERMEDIATE_PASS=$( setPassphrase ca " ca_intermediate_${AUTHORITY} " )
173- generate_ca_intermediate ${CA_INTERMEDIATE_PASS} ${CA_ROOT_PASS} ${AUTHORITY}
174-
175- purge_directory " ${CONFIG_DIR} /${AUTHORITY} "
176- purge_directory " ${CA_DIR} /${AUTHORITY} "
169+ if [ ! -f ${CA_DIR} /${AUTHORITY_NAME} /ca-intermediate.crt ]; then
170+ # Generate ca-intermediate for authority & store passphrase in the vault-ca
171+ generate_ca_intermediate ${AUTHORITY_NAME}
172+ purge_directory " ${CONFIG_DIR} /${AUTHORITY_NAME} "
173+ purge_directory " ${CA_DIR} /${AUTHORITY_NAME} "
177174 else
178- pki_logger " CA-intermediate for ${AUTHORITY } already exists, it will not be recreated..."
175+ pki_logger " CA-intermediate for authority ${AUTHORITY_NAME } already exists, it will not be recreated..."
179176 fi
180177 pki_logger " ----------------------------------------------"
181178 done
@@ -186,4 +183,5 @@ function main() {
186183 fi
187184 pki_logger " =============================================="
188185 pki_logger " End of CA creation procedure"
186+
189187}
0 commit comments