-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.ssl.sh
More file actions
executable file
·347 lines (287 loc) · 9.79 KB
/
script.ssl.sh
File metadata and controls
executable file
·347 lines (287 loc) · 9.79 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env bash
######
## @see https://developer.hashicorp.com/nomad/tutorials/transport-security/security-enable-tls#node-certificates
## @see https://github.com/cloudflare/cfssl/wiki/Creating-a-new-CSR
######
set -euo pipefail
######################## SETUP
DOCS_URI='https://github.com/nirv-ai/docs/blob/main/cfssl/README.md'
SCRIPTS_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]%/}")" &>/dev/null && pwd)"
SCRIPTS_DIR_PARENT="$(dirname $SCRIPTS_DIR)"
# UTILS
for util in $SCRIPTS_DIR/utils/*.sh; do
source $util
done
######################## INTERFACE
# grouped by increasing order of dependency
CA_CN="${CA_CN:-mesh.nirv.ai}"
CA_PEM_NAME="${CA_PEM_NAME:-ca}"
CFSSL_CONFIG_NAME="${CFSSL_CONFIG_NAME:-cfssl.json}"
CLI_NAME="${CLI_NAME:-cli}"
CLIENT_NAME="${CLIENT_NAME:-client}"
SERVER_NAME="${SERVER_NAME:-server}"
JAIL_DIR_TLS="${JAIL_DIR_TLS:-$JAIL/$CA_CN/tls}"
CA_CERT="${CA_CERT:-$JAIL_DIR_TLS/$CA_PEM_NAME}.pem"
CA_PRIVKEY="${CA_PRIVKEY:-$JAIL_DIR_TLS/$CA_PEM_NAME}-key.pem"
declare -A EFFECTIVE_INTERFACE=(
[CA_CERT]=$CA_CERT
[CA_CN]=$CA_CN
[CA_PEM_NAME]=$CA_PEM_NAME
[CA_PRIVKEY]=$CA_PRIVKEY
[CFSSL_CONFIG_NAME]=$CFSSL_CONFIG_NAME
[CFSSL_DIR]=$CFSSL_DIR
[CLI_NAME]=$CLI_NAME
[CLIENT_NAME]=$CLIENT_NAME
[JAIL_DIR_TLS]=$JAIL_DIR_TLS
[SCRIPTS_DIR_PARENT]=$SCRIPTS_DIR_PARENT
[SCRIPTS_DIR]=$SCRIPTS_DIR
[SERVER_NAME]=$SERVER_NAME
)
######################## CREDIT CHECK
echo_debug_interface
throw_missing_program cfssl 404 'sudo apt install golang-cfssl'
throw_missing_program cfssljson 404 'sudo apt install golang-cfssl'
throw_missing_program jq 404 'sudo apt install jq'
throw_missing_dir $CFSSL_DIR 404 'cant find cfssl configuration files'
throw_missing_dir $JAIL 404 "mkdir -p $JAIL"
######################## FNS
## reusable
create_tls_dir() {
mkdir -p ${1:-JAIL_DIR_TLS}
}
chmod_cert_files() {
declare pub_perm=0644
declare prv_perm=0640
request_sudo "setting permissions on new TLS certs\n[FILES]$JAIL_DIR_TLS/*.pem \n\n[$pub_perm] *.pem\n[$prv_perm] *-key.pem"
sudo chmod $pub_perm $JAIL_DIR_TLS/*.pem
sudo chmod $prv_perm $JAIL_DIR_TLS/*key.pem
}
get_cfssl_config() {
local use_cfssl_config=${1:-$CFSSL_CONFIG_NAME}
local use_cn_config_dir=${2:-CFSSL_DIR}
if test -f "${use_cn_config_dir}/${use_cfssl_config}"; then
echo "${use_cn_config_dir}/${use_cfssl_config}"
return 0
else
# go up 1 directory
# just incase they didnt follow instructions
# and put the cfssl.json in the cfssl dir instead of the CA_CN dir
# lol i did this like 3 times like wtf
next_cn_config_dir=$(dirname $use_cn_config_dir)
if test -f "${next_cn_config_dir}/${use_cfssl_config}"; then
echo "${next_cn_config_dir}/${use_cfssl_config}"
return 0
else
# use the hardcoded configs/cfssl.json and throw if missing
default_cfssl_config="${CFSSL_DIR}/cfssl.json"
if test ! -f $default_cfssl_config; then
# throw the original request
throw_missing_file "${use_cn_config_dir}/${use_cfssl_config}" 404 "cfssl configuration required"
fi
echo "$default_cfssl_config"
return 0
fi
fi
}
## actions
create_root_ca() {
local use_ca_cn="${1:-$CA_CN}"
local CA_CONFIG_DIR="${CFSSL_DIR}/${use_ca_cn}"
local CA_CONFIG_FILE="${CA_CONFIG_DIR}/csr.root.ca.json"
throw_missing_file $CA_CONFIG_FILE 404 "root ca configuration file"
echo_debug "creating rootca with config: $(cat $CA_CONFIG_FILE | jq)"
local this_jail_tls_dir="${JAIL}/${use_ca_cn}/tls"
create_tls_dir $this_jail_tls_dir
local ca_file="$this_jail_tls_dir/${2:-$CA_PEM_NAME}"
if test -f "$ca_file.pem"; then
echo_debug "file already_exists:\n$ca_file"
else
cfssl genkey -initca $CA_CONFIG_FILE | cfssljson -bare "$ca_file"
fi
chmod_cert_files
}
create_server_cert() {
local total=${1:-1}
local use_ca_cn=${2:-$CA_CN}
local CA_PEM_NAME=${3:-$CA_PEM_NAME}
local SERVER_NAME=${4:-$SERVER_NAME}
local use_cfssl_config_name=${5:-$CFSSL_CONFIG_NAME}
local this_jail_tls_dir="${JAIL}/${use_ca_cn}/tls"
local CA_CERT="${this_jail_tls_dir}/${CA_PEM_NAME}.pem"
local CA_PRIVKEY="${this_jail_tls_dir}/${CA_PEM_NAME}-key.pem"
throw_missing_file $CA_CERT 400 "create rootca before creating certs"
throw_missing_file $CA_PRIVKEY 400 "create rootca before creating certs"
local CN_CONFIG_DIR="${CFSSL_DIR}/${use_ca_cn}"
local SERVER_CONFIG="${CN_CONFIG_DIR}/csr.server.${SERVER_NAME}.json"
throw_missing_file $SERVER_CONFIG 404 'couldnt find server csr config'
local use_cfssl_config="$(get_cfssl_config $use_cfssl_config_name $CN_CONFIG_DIR)"
echo_debug "creating $total server certificates"
declare -i i=0
while [ $i -lt $total ]; do
if test -f ${this_jail_tls_dir}/$SERVER_NAME-${i}.pem; then
echo_debug "[INFO] $SERVER_NAME-${i}.pem exists; skipping"
i=$((i + 1))
continue
fi
cfssl gencert \
-ca=$CA_CERT \
-ca-key=$CA_PRIVKEY \
-config=$use_cfssl_config \
$SERVER_CONFIG |
cfssljson -bare "${this_jail_tls_dir}/$SERVER_NAME-${i}"
i=$((i + 1))
done
chmod_cert_files
}
create_client_cert() {
local total=${1:-1}
local use_ca_cn=${2:-$CA_CN}
local CA_PEM_NAME=${3:-$CA_PEM_NAME}
local CLIENT_NAME=${4:-$CLIENT_NAME}
local use_cfssl_config_name=${5:-$CFSSL_CONFIG_NAME}
local this_jail_tls_dir="${JAIL}/${use_ca_cn}/tls"
local CA_CERT="${this_jail_tls_dir}/${CA_PEM_NAME}.pem"
local CA_PRIVKEY="${this_jail_tls_dir}/${CA_PEM_NAME}-key.pem"
throw_missing_file $CA_CERT 400 "create rootca before creating certs"
throw_missing_file $CA_PRIVKEY 400 "create rootca before creating certs"
local CN_CONFIG_DIR="${CFSSL_DIR}/${use_ca_cn}"
local CLIENT_CONFIG="${CN_CONFIG_DIR}/csr.client.${CLIENT_NAME}.json"
throw_missing_file $CLIENT_CONFIG 404 'couldnt find client csr config'
local use_cfssl_config="$(get_cfssl_config $use_cfssl_config_name $CN_CONFIG_DIR)"
echo_debug "creating $total client certificates"
declare -i i=0
while [ $i -lt $total ]; do
if test -f ${this_jail_tls_dir}/$CLIENT_NAME-${i}.pem; then
echo_debug "[INFO] $CLIENT_NAME-${i}.pem exists; skipping"
i=$((i + 1))
continue
fi
cfssl gencert \
-ca=$CA_CERT \
-ca-key=$CA_PRIVKEY \
-config=$use_cfssl_config \
$CLIENT_CONFIG |
cfssljson -bare "${this_jail_tls_dir}/$CLIENT_NAME-${i}"
i=$((i + 1))
done
chmod_cert_files
}
create_cli_cert() {
local total=${1:-1}
local use_ca_cn=${2:-$CA_CN}
local CA_PEM_NAME=${3:-$CA_PEM_NAME}
local CLI_NAME=${4:-$CLI_NAME}
local use_cfssl_config_name=${5:-$CFSSL_CONFIG_NAME}
local this_jail_tls_dir="${JAIL}/${use_ca_cn}/tls"
local CA_CERT="${JAIL_DIR_TLS}/${CA_PEM_NAME}.pem"
local CA_PRIVKEY="${JAIL_DIR_TLS}/${CA_PEM_NAME}-key.pem"
throw_missing_file $CA_CERT 400 "create rootca before creating certs"
throw_missing_file $CA_PRIVKEY 400 "create rootca before creating certs"
local CN_CONFIG_DIR="${CFSSL_DIR}/${use_ca_cn}"
local CLI_CONFIG="${CN_CONFIG_DIR}/csr.cli.${CLI_NAME}.json"
throw_missing_file $CLI_CONFIG 404 'couldnt find cli csr config'
local use_cfssl_config="$(get_cfssl_config $use_cfssl_config_name $CN_CONFIG_DIR)"
echo_debug "creating $total cli certificates"
i=0
while [ $i -lt $total ]; do
if test -f "${this_jail_tls_dir}/$CLI_NAME-${i}.pem"; then
echo_debug "[INFO] $CLI_NAME-${i}.pem exists; skipping"
i=$((i + 1))
continue
fi
cfssl gencert \
-ca=$CA_CERT \
-ca-key=$CA_PRIVKEY \
-config=$use_cfssl_config \
-profile=client \
$CLI_CONFIG |
cfssljson -bare "${this_jail_tls_dir}/$CLI_NAME-${i}"
i=$((i + 1))
done
chmod_cert_files
}
create_p12_cert() {
throw_missing_program openssl 404 'openssl required for browser p12 certs'
local cert_name=${1:-client-0}
local use_ca_cn=${2:-$CA_CN}
local this_jail_tls_dir="${JAIL}/${use_ca_cn}/tls"
local client_pub="${this_jail_tls_dir}/${cert_name}.pem"
local client_prv="${this_jail_tls_dir}/${cert_name}-key.pem"
throw_missing_file $client_pub 400 "no pubkey exists for $cert_name"
throw_missing_file $client_prv 400 "no privkey exists for $cert_name"
openssl pkcs12 -export -in $client_pub -inkey $client_prv -out $this_jail_tls_dir/$cert_name.p12
}
######################## PROGRAM
cmd=${1:-''}
case $cmd in
info)
what=${2:-''}
case $what in
cert)
pem_file="${JAIL_DIR_TLS}/${3:?'pem name is required'}.pem"
throw_missing_file $pem_file 404 'couldnt find local cert file'
echo_debug 'retrieving data from local certificate file'
cfssl certinfo -cert $pem_file
;;
csr)
csr_file="${JAIL_DIR_TLS}/${3:?'csr name is required'}.csr"
throw_missing_file $csr_file 404 'couldnt find local csr file'
echo_debug 'retrieving data from local CSR file'
cfssl certinfo -csr $csr_file
;;
*) invalid_request ;;
esac
;;
create)
what=${2:-''}
case $what in
rootca)
use_ca_cn=${3:-''}
use_ca_pem_name=${4:-''}
create_root_ca $use_ca_cn $use_ca_pem_name
;;
server)
use_total=${3:-''}
use_ca_cn=${4:-''}
use_ca_pem_name=${5:-''}
use_server_name=${6:-''}
use_cfssl_config=${7-''}
create_server_cert \
$use_total \
$use_ca_cn \
$use_ca_pem_name \
$use_server_name \
$use_cfssl_config
;;
client)
use_total=${3:-''}
use_ca_cn=${4:-''}
use_ca_pem_name=${5:-''}
use_client_name=${6:-''}
use_cfssl_config=${7-''}
create_client_cert \
$use_total \
$use_ca_cn \
$use_ca_pem_name \
$use_client_name \
$use_cfssl_config
;;
cli)
use_total=${3:-''}
use_ca_cn=${4:-''}
use_ca_pem_name=${5:-''}
use_cli_name=${6:-''}
use_cfssl_config=${7-''}
create_cli_cert \
$use_total \
$use_ca_cn \
$use_ca_pem_name \
$use_cli_name \
$use_cfssl_config
;;
p12) create_p12_cert ${3:-''} ${4:-''} ;;
*) invalid_request ;;
esac
;;
*) invalid_request ;;
esac