1
1
#! /bin/bash
2
+ set -euo pipefail
3
+ IFS=$' \n\t '
2
4
#
3
5
# Sets up the Primary Root CA
4
6
# #
@@ -19,33 +21,70 @@ export ROOTCANAME=$rootCA
19
21
export CABASEURL=$caBaseURL
20
22
export COUNTRYNAME=$countryName
21
23
export ORGNAME=$organizationName
22
- export ORGUNITNAME=$organizationalUnitName
23
24
export ROOTCOMMONNAME=$rootCommonName
25
+ export ORGUNITNAME=$organizationalUnitName
26
+
27
+ # # Help message
28
+ function showHelp {
29
+ echo -e " ${yellow} Usage:${NC} "
30
+ echo " $0 [options] [command]"
31
+ echo " "
32
+ echo -e " ${yellow} Options:${NC} "
33
+ echo -e " ${green} --help -h${NC} Display this help message."
34
+ echo " "
35
+ echo -e " ${yellow} Available Commands:${NC} "
36
+ echo -e " ${green} create ${NC} Creates a new Root CA."
37
+ echo " "
38
+ echo -e " Default command is ${green} create${NC} if none is specified."
39
+ }
40
+
41
+ # # Info message
42
+ function showCreateMessage {
43
+ echo -e " ${yellow} You will be creating a new Root Certificate Authority.${NC} "
44
+ echo -e " ${yellow} This will create new directories, certificates, keys and ${NC} "
45
+ echo -e " ${yellow} other files, and may overwrite files with the same names ${NC} "
46
+ echo -e " ${yellow} that already exist.${NC} "
47
+ echo -n " Do you want to proceed? [y/N] "
48
+ read answer
49
+ echo " "
50
+ if [[ " $answer " != " y" && " $answer " != " Y" ]] ; then
51
+ exit 0
52
+ fi
53
+ }
24
54
25
55
# # Create directories
26
- mkdir -p $basepath /ca/root-ca/private $basepath /ca/root-ca/db $basepath /crl $basepath /certs
27
- chmod 700 $basepath /ca/root-ca/private
28
- echo -n " *.key" > $basepath /ca/root-ca/private/.gitignore
56
+ function createDirectories {
57
+ mkdir -p $basepath /ca/root-ca/private $basepath /ca/root-ca/db $basepath /crl $basepath /certs
58
+ chmod 700 $basepath /ca/root-ca/private
59
+ echo -n " *.key" > $basepath /ca/root-ca/private/.gitignore
60
+ }
29
61
30
62
# # Create database
31
- if ! [[ -f " $basepath /ca/root-ca/db/$rootCA .db" ]] ; then
32
- cp /dev/null $basepath /ca/root-ca/db/$rootCA .db
33
- fi
34
- if ! [[ -f " $basepath /ca/root-ca/db/$rootCA .db.attr" ]] ; then
35
- cp /dev/null $basepath /ca/root-ca/db/$rootCA .db.attr
36
- fi
37
- if ! [[ -f " $basepath /ca/root-ca/db/$rootCA .crt.srl" ]] ; then
38
- echo 01 > $basepath /ca/root-ca/db/$rootCA .crt.srl
39
- fi
40
- if ! [[ -f " $basepath /ca/root-ca/db/$rootCA .crl.srl" ]] ; then
41
- echo 01 > $basepath /ca/root-ca/db/$rootCA .crl.srl
42
- fi
63
+ function createDatabase {
64
+ if ! [[ -f " $basepath /ca/root-ca/db/$rootCA .db" ]] ; then
65
+ cp /dev/null $basepath /ca/root-ca/db/$rootCA .db
66
+ fi
67
+ if ! [[ -f " $basepath /ca/root-ca/db/$rootCA .db.attr" ]] ; then
68
+ cp /dev/null $basepath /ca/root-ca/db/$rootCA .db.attr
69
+ fi
70
+ if ! [[ -f " $basepath /ca/root-ca/db/$rootCA .crt.srl" ]] ; then
71
+ echo 01 > $basepath /ca/root-ca/db/$rootCA .crt.srl
72
+ fi
73
+ if ! [[ -f " $basepath /ca/root-ca/db/$rootCA .crl.srl" ]] ; then
74
+ echo 01 > $basepath /ca/root-ca/db/$rootCA .crl.srl
75
+ fi
76
+ }
43
77
44
78
# # Create CA request
45
79
# # Use -key ca/root-ca/private/root-ca.key to generate
46
80
# # a new CSR.
47
81
function genCsr {
48
- if ! [[ -f " $basepath /ca/root-ca/private/$rootCA .key" ]] ; then
82
+ keyPath=" $basepath /ca/root-ca/private/$rootCA .key"
83
+
84
+ if ! [[ -f $keyPath && -s $keyPath ]] ; then
85
+ echo -e " ${yellow} A new key will now be created.${NC} "
86
+ echo -e " ${yellowBold} You must enter a password between 4 and 1024 characters.${NC} "
87
+
49
88
openssl req -new \
50
89
-sha256 \
51
90
-config $basepath /etc/root-ca.conf \
@@ -62,17 +101,19 @@ function genCsr {
62
101
63
102
# # Check if the CSR exists. If so, ask the user if they
64
103
# # want to replace it. Otherwise, just create the CSR.
65
- if [[ -f " $basepath /ca/$rootCA .csr" ]] ; then
66
- echo -e " ${red} Root CA CSR exists!${NC} "
67
- echo -n " Do you want to create a new one? (y/N): "
68
- read answer
69
- echo " "
70
- if [[ " $answer " == " y" || " $answer " == " Y" ]] ; then
104
+ function checkCsr {
105
+ if [[ -f " $basepath /ca/$rootCA .csr" ]] ; then
106
+ echo -e " ${red} Root CA CSR exists!${NC} "
107
+ echo -n " Do you want to create a new one? (y/N): "
108
+ read answer
109
+ echo " "
110
+ if [[ " $answer " == " y" || " $answer " == " Y" ]] ; then
111
+ genCsr
112
+ fi
113
+ else
71
114
genCsr
72
115
fi
73
- else
74
- genCsr
75
- fi
116
+ }
76
117
77
118
# # Create CA certificate
78
119
function genCrt {
@@ -84,27 +125,69 @@ function genCrt {
84
125
-enddate 20501231235959Z
85
126
}
86
127
87
- if [[ -f " $basepath /ca/$rootCA .crt" ]] ; then
88
- echo -e " ${red} Root CA certificate exists!${NC} "
89
- echo -n " Do you want to create a new one? (y/N): "
90
- read answer
91
- echo " "
92
- if [[ " $answer " == " y" || " $answer " == " Y" ]] ; then
128
+ # # Check if certificate exists and prompt to overwrite
129
+ function checkCrt {
130
+ if [[ -f " $basepath /ca/$rootCA .crt" ]] ; then
131
+ echo -e " ${red} Root CA certificate exists!${NC} "
132
+ echo -n " Do you want to create a new one? (y/N): "
133
+ read answer
134
+ echo " "
135
+ if [[ " $answer " == " y" || " $answer " == " Y" ]] ; then
136
+ genCrt
137
+ fi
138
+ else
93
139
genCrt
94
140
fi
95
- else
96
- genCrt
97
- fi
141
+ }
98
142
99
143
# # Create CRL
100
- echo -n " Do you want to generate a CRL? (y/N): "
101
- read answer
102
- echo " "
103
-
104
- if [[ " $answer " == " y" || " $answer " == " Y" ]] ; then
144
+ function genCrl {
105
145
openssl ca -gencrl \
106
146
-config $basepath /etc/root-ca.conf \
107
147
-out $basepath /crl/$rootCA .crl
148
+ }
149
+
150
+ # # Ask to create a CRL file
151
+ function checkCrl {
152
+ echo -n " Do you want to generate a CRL? (y/N): "
153
+ read answer
154
+ echo " "
155
+
156
+ if [[ " $answer " == " y" || " $answer " == " Y" ]] ; then
157
+ genCrl
158
+ fi
159
+ }
160
+
161
+ function finish {
162
+ echo -e " ${greenBold} Done!${NC} "
163
+ }
164
+
165
+ # # If no arguments came in, default to create
166
+ if [[ -z " $@ " ]] ; then
167
+ set " create"
108
168
fi
109
169
110
- echo -e " ${greenBold} Done!${NC} "
170
+ # # Loop through command parameters
171
+ for i
172
+ do
173
+ case $i in
174
+ -\? | -h | help )
175
+ showHelp
176
+ exit 0
177
+ ;;
178
+ create )
179
+ # # Create a new CA
180
+ showCreateMessage
181
+ createDirectories
182
+ createDatabase
183
+ checkCsr
184
+ checkCrt
185
+ checkCrl
186
+ finish
187
+ ;;
188
+ * )
189
+ echo -e " ${redBold} Unknown arg '$i '${NC} " ;
190
+ exit 1
191
+ ;;
192
+ esac
193
+ done
0 commit comments