-
Notifications
You must be signed in to change notification settings - Fork 32
/
ca-gen
executable file
·335 lines (293 loc) · 6.65 KB
/
ca-gen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env bash
set -e
set -u
set -o pipefail
NAME="ca-gen"
VERSION="v0.10"
DATE="2022-12-18"
# Generate default options
DEF_KEYSIZE=2048
DEF_DAYS=3650
DEF_SIGN_SIGNATURE="sha256"
# Subject default options
DEF_COUNTRY=
DEF_STATE=
DEF_CITY=
DEF_ORG=
DEF_UNIT=
DEF_CN=
DEF_EMAIL=
# Verbosity
DEF_VERBOSE=
log() {
local type="${1}" # err, warn, info
local message="${2}" # message to log
if [ "${type}" = "err" ]; then
printf "%s: [ERR] %s\n" "${NAME}" "${message}" 1>&2 # stdout -> stderr
fi
if [ "${type}" = "warn" ]; then
printf "%s: [WARN] %s\n" "${NAME}" "${message}" 1>&2 # stdout -> stderr
fi
if [ "${DEF_VERBOSE:-}" = "1" ]; then
if [ "${type}" = "info" ]; then
printf "%s: [INFO] %s\n" "${NAME}" "${message}"
fi
fi
}
print_version() {
echo "${NAME}: Version ${VERSION} (${DATE}) by cytopia"
echo "https://github.com/devilbox/cert-gen/"
}
print_help() {
echo "USAGE: ${NAME} -n CN [-kdcslouev] <keyfile> <crtfile>"
echo " ${NAME} --help"
echo " ${NAME} --version"
echo
echo "Required arguments"
echo " -n CN Common Name"
echo
echo "Optional arguments"
echo " -k int Key size in bits"
echo " -d int Validity in days"
echo " -c C Subject two letter country name (C)"
echo " -s ST Subject state name (ST)"
echo " -l L Subject location (L)"
echo " -o O Subject organization (O)"
echo " -u OU Subject organizational unit (OU)"
echo " -e Email Subject email (emailAddress)"
echo " -v Verbose output"
echo
echo "Required parameter"
echo " <keyfile> Path to output key file"
echo " <crtfile> Path to output cert file"
}
################################################################################
# Entrypoint: Parse cmd args
################################################################################
# Get options
while [ ${#} -gt 0 ]; do
case "${1}" in
# ---- Help / version
--version)
print_version
exit
;;
--help)
print_help
exit
;;
-v)
DEF_VERBOSE=1
shift
;;
# ---- Options
-k)
shift
if [ -z "${1:-}" ]; then
log "err" "Usage: -k requires an argument."
exit 1
fi
DEF_KEYSIZE="${1}"
shift
;;
-d)
shift
if [ -z "${1:-}" ]; then
log "err" "Usage: -d requires an argument."
exit 1
fi
DEF_DAYS="${1}"
shift
;;
-c)
shift
if [ -z "${1:-}" ]; then
log "err" "Usage: -c requires an argument."
exit 1
fi
DEF_COUNTRY="${1}"
shift
;;
-s)
shift
if [ -z "${1:-}" ]; then
log "err" "Usage: -s requires an argument."
exit 1
fi
DEF_STATE="${1}"
shift
;;
-l)
shift
if [ -z "${1:-}" ]; then
log "err" "Usage: -l requires an argument."
exit 1
fi
DEF_CITY="${1}"
shift
;;
-o)
shift
if [ -z "${1:-}" ]; then
log "err" "Usage: -o requires an argument."
exit 1
fi
DEF_ORG="${1}"
shift
;;
-u)
shift
if [ -z "${1:-}" ]; then
log "err" "Usage: -u requires an argument."
exit 1
fi
DEF_UNIT="${1}"
shift
;;
-n)
shift
if [ -z "${1:-}" ]; then
log "err" "Usage: -n requires an argument."
exit 1
fi
DEF_CN="${1}"
shift
;;
-e)
shift
if [ -z "${1:-}" ]; then
log "err" "Usage: -e requires an argument."
exit 1
fi
DEF_EMAIL="${1}"
shift
;;
# ---- Stop here
--) # End of all options
shift
break
;;
-*) # Unknown option
log "err" "Usage: Unknown option: ${1}"
exit 1
;;
*) # No more options
break
;;
esac
done
################################################################################
# Entrypoint: Validate cmd args
################################################################################
if [ -z "${DEF_CN}" ]; then
log "err" "Usage: -n is required. See --help for help."
exit 1
fi
if [ "${#}" -lt "2" ]; then
log "err" "Usage: <keyfile> and <crtfile> are required. See --help for help."
exit 1
fi
CA_KEY_FILE="${1}"
CA_CRT_FILE="${2}"
################################################################################
# Entrypoint: Execute
################################################################################
###
### Build subject
###
SUBJECT=
if [ -n "${DEF_COUNTRY}" ]; then
SUBJECT="${SUBJECT}/C=${DEF_COUNTRY}"
fi
if [ -n "${DEF_STATE}" ]; then
SUBJECT="${SUBJECT}/ST=${DEF_STATE}"
fi
if [ -n "${DEF_CITY}" ]; then
SUBJECT="${SUBJECT}/L=${DEF_CITY}"
fi
if [ -n "${DEF_ORG}" ]; then
SUBJECT="${SUBJECT}/O=${DEF_ORG}"
fi
if [ -n "${DEF_UNIT}" ]; then
SUBJECT="${SUBJECT}/OU=${DEF_UNIT}"
fi
if [ -n "${DEF_CN}" ]; then
SUBJECT="${SUBJECT}/CN=${DEF_CN}"
fi
if [ -n "${DEF_EMAIL}" ]; then
SUBJECT="${SUBJECT}/emailAddress=${DEF_EMAIL}"
fi
###
### Build commands
###
###
### 1. Create Key
###
# Command
cmd="openssl genrsa \
-out ${CA_KEY_FILE} \
${DEF_KEYSIZE}"
# Trim newlines/whitespaces
cmd="$( echo "${cmd}" | tr -s " " )"
# Execute
log "info" "Create CA KEY file: ${CA_KEY_FILE}"
if ! out="$( eval "${cmd}" 2>&1 )"; then
log "err" "Command: ${cmd}"
log "err" "Output: ${out}"
exit 1
fi
###
### 2. Create dnQualifier
###
# Subject dnQualifier (Public key thumbprint, see SMPTE 430-2-2006 sections 5.3.1, 5.4 and DCI CTP section 2.1.11)
ca_dnq="$( openssl rsa -outform PEM -pubout -in "${CA_KEY_FILE}" 2>/dev/null | openssl base64 -d | dd bs=1 skip=24 2>/dev/null | openssl sha1 -binary | openssl base64 )"
ca_dnq="${ca_dnq//\//\\/}" # echo "${ca_dnq}" | sed 's|/|\\/|g' )" # can have values like '0Za8/aABE05Aroz7le1FOpEdFhk=', note the '/'. protect for name parser
SUBJECT="${SUBJECT}/dnQualifier=${ca_dnq}"
###
### 3. Create CA
###
OPENSSL_CONFIG="$( cat <<'HEREDOC'
[req]
distinguished_name = req_distinguished_name
[req_distinguished_name]
[ v3_ca ]
basicConstraints = critical, CA:TRUE
subjectKeyIdentifier = hash
keyUsage = critical, digitalSignature, cRLSign, keyCertSign
authorityKeyIdentifier = keyid:always,issuer:always
HEREDOC
)"
# Command
cmd="openssl req \
-new \
-x509 \
-nodes \
-${DEF_SIGN_SIGNATURE} \
-days ${DEF_DAYS} \
-key ${CA_KEY_FILE} \
-subj '${SUBJECT}' \
-extensions v3_ca \
-config <(echo \"${OPENSSL_CONFIG}\") \
-out ${CA_CRT_FILE}"
# Trim newlines/whitespaces
cmd="$( echo "${cmd}" | tr -s " " )"
# Execute
log "info" "Create CA CRT file: ${CA_CRT_FILE}"
if ! out="$( eval "${cmd}" 2>&1 )"; then
log "err" "Command: ${cmd}"
log "err" "Output: ${out}"
exit 1
fi
###
### 4. Validate
###
log "info" "Verify CA CRT file: ${CA_CRT_FILE}"
if ! out="$( openssl x509 -in "${CA_CRT_FILE}" -text )"; then
log "err" "CA CRT verification failed: ${out}"
exit 1
fi
log "info" "Verify CA CRT issuer"
if ! out="$( openssl x509 -noout -subject -issuer -in "${CA_CRT_FILE}" )"; then
log "err" "CA CRT issuer failed: ${out}"
exit 1
fi