forked from GLVis/glvis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glvis-keygen.sh
executable file
·389 lines (364 loc) · 11 KB
/
glvis-keygen.sh
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/bin/bash
# Copyright (c) 2010-2024, Lawrence Livermore National Security, LLC. Produced
# at the Lawrence Livermore National Laboratory. All Rights reserved. See files
# LICENSE and NOTICE for details. LLNL-CODE-443271.
#
# This file is part of the GLVis visualization tool and library. For more
# information and source code availability see https://glvis.org.
#
# GLVis is free software; you can redistribute it and/or modify it under the
# terms of the BSD-3 license. We welcome feedback and contributions, see file
# CONTRIBUTING.md for details.
GLVIS_KEYS_DIR="${HOME}"/.config/glvis
NAME_COMMENT_SERVER="GLVis server key"
NAME_COMMENT_CLIENT="GLVis client key"
function have_command()
{
command -v $1 > /dev/null 2>&1
}
function check_gpg2()
{
if ! have_command ${GPG2}; then
printf "\nThe required command \"${GPG2}\" was not found. Stop.\n\n"
exit 1
fi
}
function check_certtool()
{
if ! have_command ${CERTTOOL}; then
printf "\nThe required command \"${CERTTOOL}\" was not found. Stop.\n\n"
exit 1
fi
if { ${CERTTOOL} --help | head -1 | grep -q GnuTLS; }; then
# newer versions of GnuTLS certtool
return 0
elif { ${CERTTOOL} --version | head -1 | grep -q GnuTLS; }; then
# older versions of GnuTLS certtool
return 0
else
printf "\nThe required command \"${CERTTOOL}\" is not GnuTLS certtool."
printf " Stop.\n\n"
exit 1
fi
}
function gen_keys_gpg2()
{
local IN=""
IN="${IN}Key-Type: RSA\n"
IN="${IN}Key-Length: 2048\n"
IN="${IN}Subkey-Type: RSA\n"
IN="${IN}Subkey-Length: 2048\n"
IN="${IN}Name-Real: "'${NAME_REAL}'"\n"
IN="${IN}Name-Comment: "'${NAME_COMMENT}'"\n"
IN="${IN}Name-Email: "'${NAME_EMAIL}'"\n"
IN="${IN}Expire-Date: 0\n"
IN="${IN}%%no-protection\n" # for gpg2 version >= 2.1
IN="${IN}%%commit"
IN=$(printf "${IN}")
eval "IN=\"${IN}\""
# printf "%s\n" "${IN}"
local GPG2_ARGS=("--homedir" "${KEY_DIR}" "--batch" "--gen-key" "-")
printf "%s\n" "${IN}" | ${GPG2} "${GPG2_ARGS[@]}"
}
function gen_keys_certtool()
{
local CT="${CERTTOOL}" ROLE="$1" IN=""
cd "${KEY_DIR}" || return 1
# Generate user CA key
$CT --generate-privkey --outfile ca-key.pem || return 1
# Generate self-signed user CA certificate
IN="cn = \"${NAME_REAL}'s GLVis ${ROLE} CA Certificate\"\n"
IN="${IN}email = \"${NAME_EMAIL}\"\n"
IN="${IN}expiration_days = 3651\n"
IN="${IN}ca\n"
IN="${IN}cert_signing_key\n"
printf "$IN" > ca-cert.cfg
$CT --generate-self-signed --load-privkey ca-key.pem \
--outfile ca-cert.pem --template ca-cert.cfg \
> ca-cert.txt 2>&1 || return 1
rm -f ca-cert.cfg ca-cert.txt
# Generate user key
$CT --generate-privkey --outfile key.pem || return 1
# Generate user certificate signed with the CA key & certificate
IN="cn = \"${NAME_REAL}'s GLVis ${ROLE} Certificate\"\n"
IN="${IN}email = \"${NAME_EMAIL}\"\n"
IN="${IN}expiration_days = 3650\n"
IN="${IN}signing_key\n"
IN="${IN}encryption_key\n"
if [ "${ROLE}" == "Client" ]; then
IN="${IN}tls_www_client\n"
else
IN="${IN}tls_www_server\n"
fi
printf "$IN" > cert.cfg
$CT --generate-certificate --load-privkey key.pem \
--outfile cert.pem --load-ca-certificate ca-cert.pem \
--load-ca-privkey ca-key.pem --template cert.cfg \
> cert.txt 2>&1 || return 1
rm -f cert.cfg cert.txt
}
function check_keys_gpg2()
{
if [ -s "${GLVIS_KEYS_DIR}"/server/pubring.gpg ] && \
[ -s "${GLVIS_KEYS_DIR}"/server/secring.gpg ]; then
GEN_SERVER_KEY="NO"
fi
if [ -s "${GLVIS_KEYS_DIR}"/client/pubring.gpg ] && \
[ -s "${GLVIS_KEYS_DIR}"/client/secring.gpg ]; then
GEN_CLIENT_KEY="NO"
fi
if [ -s "${GLVIS_KEYS_DIR}"/client/trusted-servers.gpg ]; then
ADD_SERVER_KEY="NO"
fi
if [ -s "${GLVIS_KEYS_DIR}"/server/trusted-clients.gpg ]; then
ADD_CLIENT_KEY="NO"
fi
}
function check_keys_certtool()
{
if [ -s "${GLVIS_KEYS_DIR}"/server/cert.pem ] && \
[ -s "${GLVIS_KEYS_DIR}"/server/key.pem ]; then
GEN_SERVER_KEY="NO"
fi
if [ -s "${GLVIS_KEYS_DIR}"/client/cert.pem ] && \
[ -s "${GLVIS_KEYS_DIR}"/client/key.pem ]; then
GEN_CLIENT_KEY="NO"
fi
if [ -s "${GLVIS_KEYS_DIR}"/client/trusted-servers.pem ]; then
ADD_SERVER_KEY="NO"
fi
if [ -s "${GLVIS_KEYS_DIR}"/server/trusted-clients.pem ]; then
ADD_CLIENT_KEY="NO"
fi
}
# $1 - scr dir, $2 - dest dir
function add_keys_gpg2()
{
cd "${GLVIS_KEYS_DIR}" && \
${GPG2} --homedir "${GLVIS_KEYS_DIR}"/$1 --export --armor \
--output "$1.asc" && \
${GPG2} --homedir "${GLVIS_KEYS_DIR}"/$2 --no-default-keyring \
--keyring "trusted-${1}s.gpg" --import "$1.asc"
}
# $1 - scr dir, $2 - dest dir
add_keys_certtool()
{
cd "${GLVIS_KEYS_DIR}" && \
cp -fp $1/ca-cert.pem $2/trusted-${1}s.pem
}
function list_keys_gpg2()
{
# ${GPG2} --homedir "${GLVIS_KEYS_DIR}"/server --list-secret-keys
if [ -s "${GLVIS_KEYS_DIR}"/server/trusted-clients.gpg ]; then
${GPG2} --homedir "${GLVIS_KEYS_DIR}"/server \
--keyring "trusted-clients.gpg" --list-public-keys
else
printf "Server/trusted-client keys not found.\n"
fi
# ${GPG2} --homedir "${GLVIS_KEYS_DIR}"/client --list-secret-keys
if [ -s "${GLVIS_KEYS_DIR}"/client/trusted-servers.gpg ]; then
${GPG2} --homedir "${GLVIS_KEYS_DIR}"/client \
--keyring "trusted-servers.gpg" --list-public-keys
else
printf "Client/trusted-server keys not found.\n"
fi
}
function list_keys_certtool()
{
local CT="${CERTTOOL}"
local sc="${GLVIS_KEYS_DIR}"/server/cert.pem sn="Server certificate"
local cc="${GLVIS_KEYS_DIR}"/client/cert.pem cn="Client certificate"
if [ -s "$sc" ]; then
echo "$sn:"
echo "-------------------"
$CT --certificate-info --infile "$sc"
else
printf "$sn not found.\n"
fi
echo
if [ -s "$cc" ]; then
echo "$cn:"
echo "-------------------"
$CT --certificate-info --infile "$cc"
else
printf "$cn not found.\n"
fi
}
function read_name_email()
{
NAME_REAL="$1"
NAME_EMAIL="$2"
if [ "${NAME_REAL}" == "" ]; then
if [ -t 0 ]; then
if [ -s "${HOME}"/.gitconfig ] && have_command git; then
FULL_NAME=$(git config user.name)
elif [ $(uname -s) == "Darwin" ]; then
FULL_NAME=$(id -P ${USER} | awk -F: '{print $8}')
elif have_command getent; then
FULL_NAME=$(getent passwd ${USER} | awk -F: '{print $5}')
else
FULL_NAME=${USER}
fi
read -p "Enter your name [${FULL_NAME}]: " NAME_REAL
if [ "${NAME_REAL}" == "" ]; then
NAME_REAL="${FULL_NAME}"
fi
else
return 1
fi
fi
if [ "${NAME_EMAIL}" == "" ]; then
if [ -t 0 ]; then
if [ -s "${HOME}"/.gitconfig ] && have_command git; then
FULL_EMAIL=$(git config user.email)
else
FULL_EMAIL="${USER}@${HOST}"
fi
read -p "Enter your email [${FULL_EMAIL}]: " NAME_EMAIL
if [ "${NAME_EMAIL}" == "" ]; then
NAME_EMAIL="${FULL_EMAIL}"
fi
else
return 1
fi
fi
}
function print_usage()
{
printf "Usage:\n"
printf " $0 {-h|--help}\n"
printf " $0 [<var>=<value>]... {-l|--list}\n"
printf " $0 [<var>=<value>]... [\"Your Name\"] [\"Your Email\"]\n"
printf "Valid variables:\n"
printf " keytype={x509|gpg} (current value: ${KEYTYPE})\n"
printf " certtool=<certtool-prog> (current value: ${CERTTOOL}"
printf ", keytype: x509)\n"
printf " gpg2=<gpg2-prog> (current value: ${GPG2}"
printf ", keytype: gpg)\n"
}
function select_params()
{
# Key generation programs: gpg2, certtool
GPG2=${gpg2:-gpg2}
case "$OSTYPE" in
darwin*)
CERTTOOL=${certtool:-gnutls-certtool}
;;
*)
CERTTOOL=${certtool:-certtool}
;;
esac
# Key type to generate: gpg or x509
KEYTYPE=${keytype:-x509}
case "$KEYTYPE" in
gpg)
keytype_prog=gpg2
;;
x509)
keytype_prog=certtool
;;
*)
printf "\nInvalid keytype: '${KEYTYPE}'. Stop.\n\n"
exit 1
;;
esac
}
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
select_params
print_usage
exit 0
;;
-l|--list)
select_params
check_$keytype_prog
echo
list_keys_$keytype_prog
exit 0
;;
-*)
printf "Unknown option: '$1'. Stop.\n\n"
exit 1
;;
*=*)
eval $1
shift
;;
*)
break
;;
esac
done
select_params
GEN_SERVER_KEY="YES"
GEN_CLIENT_KEY="YES"
ADD_SERVER_KEY="YES"
ADD_CLIENT_KEY="YES"
check_keys_$keytype_prog
if [ "${GEN_SERVER_KEY}" == "YES" ] || [ "${GEN_CLIENT_KEY}" == "YES" ]; then
check_$keytype_prog
if ! read_name_email "$@"; then
print_usage
exit 1
fi
elif [ "${ADD_SERVER_KEY}" == "YES" ] || [ "${ADD_CLIENT_KEY}" == "YES" ]; then
check_$keytype_prog
fi
echo
if [ "${GEN_SERVER_KEY}" == "NO" ]; then
printf "Server key exists.\n\n"
else
printf "\r---------------------------\n"
printf " Generating server key\n"
printf "\r---------------------------\n"
NAME_COMMENT="${NAME_COMMENT_SERVER}"
KEY_DIR="${GLVIS_KEYS_DIR}"/server
echo mkdir -p "${KEY_DIR}"
mkdir -p "${KEY_DIR}"
if ! gen_keys_$keytype_prog "Server"; then
printf "\nGeneration failed. Stop.\n\n"
exit 1
fi
printf "\r---------------------------\n\n"
fi
if [ "${GEN_CLIENT_KEY}" == "NO" ]; then
printf "Client key exists.\n\n"
else
printf "\r---------------------------\n"
printf " Generating client key\n"
printf "\r---------------------------\n"
NAME_COMMENT="${NAME_COMMENT_CLIENT}"
KEY_DIR="${GLVIS_KEYS_DIR}"/client
echo mkdir -p "${KEY_DIR}"
mkdir -p "${KEY_DIR}"
if ! gen_keys_$keytype_prog "Client"; then
printf "\nGeneration failed. Stop.\n\n"
exit 1
fi
printf "\r---------------------------\n\n"
fi
if [ "${ADD_SERVER_KEY}" == "NO" ]; then
printf "Client's trusted-servers keyring exists.\n\n"
else
printf "\r-------------------------------------------------------\n"
printf " Adding the server key to the client's trusted-servers\n"
printf "\r-------------------------------------------------------\n"
if ! add_keys_$keytype_prog "server" "client"; then
printf "\nOperation failed. Stop.\n\n"
exit 1
fi
printf "\r-------------------------------------------------------\n\n"
fi
if [ "${ADD_CLIENT_KEY}" == "NO" ]; then
printf "Server's trusted-clients keyring exists.\n\n"
else
printf "\r-------------------------------------------------------\n"
printf " Adding the client key to the server's trusted-clients\n"
printf "\r-------------------------------------------------------\n"
if ! add_keys_$keytype_prog "client" "server"; then
printf "\nOperation failed. Stop.\n\n"
exit 1
fi
printf "\r-------------------------------------------------------\n\n"
fi