~poptart/hosaka-pki

ref: 8d6f421fbd4ea6b5ac74c4add9e8321b9c370982 hosaka-pki/hosaka-pki.sh -rw-r--r-- 12.4 KiB
8d6f421f — poptart Updated to begin adding the interactive functions 1 year, 7 months ago
                                                                                
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
#!/bin/sh -e
export CONFIGDIR=""
export SSL_CA_DIR=""
export SSH_CA_DIR=""
export DAYSVALID=""
export CADAYSVALID=""
export MDALGORITHM=""
export USEINTERMEDIATE=""
export CACHECREDENTIALS=""
export WARNROOTCERT=""
export COUNTRYDEFAULT=""
export STATEDEFAULT=""
export LOCALITYDEFAULT=""
export ORGNAMEDEFAULT=""
export ORGUNITDEFAULT=""
export USERCERTCOMMENT=""
export SRVCERTCOMMENT=""
export SSLSERIALDEFAULT=""
export SSHSERIALDEFAULT=""
export SSLFILE=""

USAGESTRING="Usage: $0 [tls|ssh] [info|help|ca|server|sign|check] [OPTIONS...]"
HELPSTRING=$(cat <<'HEREDOC'
	\e[1mtls\e[0m - TLS PKI management commands
		\e[1minfo\e[0m
			Prints information about the current system and its configuration.
		\e[1mhelp\e[0m
			Prints this help and usage dialoge
		\e[1mca\e[0m
			Configures a certificate authority and establishes an intermediary
			depending on the configuration of hosaka-pki. If a CA or intermed-
			iary exists in the configuration file locations the command will
			exit.
		\e[1mserver\e[0m NAME OUTPUTDIR
			Generates a set of local keys and .csr files to be uploaded for
			signing on the CA system. NAME will be the prefix used on the file
			names; .key.pem and .csr.pem  
		\e[1msign\e[0m [PATH|SHORTNAME]
			Signs the TLS certificate based on the direct PATH or if the certi-
			cate exists in the csr/ directory of the PKI the SHORTNAME can be
			used instead.
		\e[1mcheck\e[0m
			Check the status of the PKI and prints out soon to expire certifi-
			cates. If no certificates are about to expire nothing will be output
HEREDOC
)

readconf() {
	while IFS='=' read -r key val; do
    		[ "${key##\#*}" ] || continue
    		export "$key=$val" 2>/dev/null ||
    		     printf '%s is not a valid variable\n' "$key"
	done < "$CONFIGDIR/config"
}

checkvars() {
	[ -z "$CONFIGDIR" ] && printf "Arguments cannot be empty: %s\\n" "$CONFIGDIR" 1>&2 && exit 1
	[ -z "$SSL_CA_DIR" ] && printf "Arguments cannot be empty: %s\\n" "$SSL_CA_DIR" 1>&2 && exit 1
	[ -z "$SSH_CA_DIR" ] && printf "Arguments cannot be empty: %s\\n" "$SSH_CA_DIR" 1>&2 && exit 1
	[ -z "$DAYSVALID" ] && printf "Arguments cannot be empty: %s\\n" "$DAYSVALID" 1>&2 && exit 1
	[ -z "$CADAYSVALID" ] && printf "Arguments cannot be empty: %s\\n" "$CADAYSVALID" 1>&2 && exit 1
	[ -z "$MDALGORITHM" ] && printf "Arguments cannot be empty: %s\\n" "$MDALGORITHM" 1>&2 && exit 1
	[ -z "$USEINTERMEDIATE" ] && printf "Arguments cannot be empty: %s\\n" "$USEINTERMEDIATE" 1>&2 && exit 1
	[ -z "$CACHECREDENTIALS" ] && printf "Arguments cannot be empty: %s\\n" "$CACHECREDENTIALS" 1>&2 && exit 1
	[ -z "$WARNROOTCERT" ] && printf "Arguments cannot be empty: %s\\n" "$WARNROOTCERT" 1>&2 && exit 1
	[ -z "$COUNTRYDEFAULT" ] && printf "Arguments cannot be empty: %s\\n" "$COUNTRYDEFAULT" 1>&2 && exit 1
	[ -z "$STATEDEFAULT" ] && printf "Arguments cannot be empty: %s\\n" "$STATEDEFAULT" 1>&2 && exit 1
	[ -z "$LOCALITYDEFAULT" ] && printf "Arguments cannot be empty: %s\\n" "$LOCALITYDEFAULT" 1>&2 && exit 1
	[ -z "$ORGNAMEDEFAULT" ] && printf "Arguments cannot be empty: %s\\n" "$ORGNAMEDEFAULT" 1>&2 && exit 1
	[ -z "$ORGUNITDEFAULT" ] && printf "Arguments cannot be empty: %s\\n" "$ORGUNITDEFAULT" 1>&2 && exit 1
	[ -z "$USERCERTCOMMENT" ] && printf "Arguments cannot be empty: %s\\n" "$USERCERTCOMMENT" 1>&2 && exit 1
	[ -z "$SRVCERTCOMMENT" ] && printf "Arguments cannot be empty: %s\\n" "$SRVCERTCOMMENT" 1>&2 && exit 1
	[ -z "$SSLSERIALDEFAULT" ] && printf "Arguments cannot be empty: %s\\n" "$SSLSERIALDEFAULT" 1>&2 && exit 1
	[ -z "$SSHSERIALDEFAULT" ] && printf "Arguments cannot be empty: %s\\n" "$SSHSERIALDEFAULT" 1>&2 && exit 1
}

getconf() {
	#The "compilation" step used for hosaka-pki just uses templates and
	#this will work even if the template injection works or not.
	CONFIGDIR="{{CONFIGDIR}}"
	case $CONFIGDIR in
		""|"{{CONFIGDIR}}")
			CONFIGDIR="/etc/hosaka/pki"
			;;
		*)
			CONFIGDIR="${CONFIGDIR}"
			;;
	esac
	[ -f "${CONFIGDIR}/config" ] && readconf "${CONFIGDIR}/config"
}

tlsgenconf() {
	#Generate conf for the CA and if an intermediate is in use, generate
	#for that as well
	export SSLFILE="ca"
	sed -e "s%{{SSLDIR}}%${SSL_CA_DIR}%g" \
		-e "s%{{MDALGORITHM}}%${MDALGORITHM}%g" \
		-e "s%{{DAYSVALID}}%${DAYSVALID}%g" \
		-e "s%{{CADAYSVALID}}%${CADAYSVALID}%g" \
		-e "s%{{COUNTRYDEFAULT}}%${COUNTRYDEFAULT}%g" \
		-e "s%{{STATEDEFAULT}}%${STATEDEFAULT}%g" \
		-e "s%{{LOCALITYDEFAULT}}%${LOCALITYDEFAULT}%g" \
		-e "s%{{ORGNAMEDEFAULT}}%${ORGNAMEDEFAULT}%g" \
		-e "s%{{ORGUNITDEFAULT}}%${ORGUNITDEFAULT}%g" \
		-e "s%{{USERCERTCOMMENT}}%${USERCERTCOMMENT}%g" \
		-e "s%{{SERVERCERTCOMMENT}}%${SERVERCERTCOMMENT}%g" \
		-e "s%{{SSLSERIALDEFAULT}}%${SSLSERIALDEFAULT}%g" \
		-e "s%{{SSLFILE}}%${SSLFILE}%g" \
		< "$CONFIGDIR/util/openssl.cnf.tmpl" > "$SSL_CA_DIR/openssl.cnf"
	tlsuseintermediate && export SSLFILE="intermediate"
	tlsuseintermediate && sed -e "s%{{SSLDIR}}%${SSL_CA_DIR}/intermediate%g" \
		-e "s%{{MDALGORITHM}}%${MDALGORITHM}%g" \
		-e "s%{{DAYSVALID}}%${DAYSVALID}%g" \
		-e "s%{{CADAYSVALID}}%${CADAYSVALID}%g" \
		-e "s%{{COUNTRYDEFAULT}}%${COUNTRYDEFAULT}%g" \
		-e "s%{{STATEDEFAULT}}%${STATEDEFAULT}%g" \
		-e "s%{{LOCALITYDEFAULT}}%${LOCALITYDEFAULT}%g" \
		-e "s%{{ORGNAMEDEFAULT}}%${ORGNAMEDEFAULT}%g" \
		-e "s%{{ORGUNITDEFAULT}}%${ORGUNITDEFAULT}%g" \
		-e "s%{{USERCERTCOMMENT}}%${USERCERTCOMMENT}%g" \
		-e "s%{{SERVERCERTCOMMENT}}%${SERVERCERTCOMMENT}%g" \
		-e "s%{{SSLSERIALDEFAULT}}%${SSLSERIALDEFAULT}%g" \
		-e "s%{{SSLFILE}}%${SSLFILE}%g" \
		< "$CONFIGDIR/util/openssl.cnf.tmpl" > "$SSL_CA_DIR/openssl_intermediate.cnf"
}

tlsuseintermediate() {
	case "$USEINTERMEDIATE" in
		y*|Y*)
			return 0
		;;
		*)
			return 1
		;;
	esac
	return 1
}

tlscachecredential() {
	case "$CACHECREDENTIALS" in
		y*|Y*)
			return 0
		;;
		*)
			return 1
		;;
	esac
	return 1
}


tlscreatedirs() {
	mkdir -p "${SSL_CA_DIR}/intermediate" \
		"${SSL_CA_DIR}/certs" \
		"${SSL_CA_DIR}/crl" \
		"${SSL_CA_DIR}/csr" \
		"${SSL_CA_DIR}/private" \
		"${SSL_CA_DIR}/new" && \
	chmod 700 "${SSL_CA_DIR}/private"
	tlsuseintermediate && \
		mkdir -p "${SSL_CA_DIR}/intermediate" \
			"${SSL_CA_DIR}/intermediate/certs" \
			"${SSL_CA_DIR}/intermediate/crl" \
			"${SSL_CA_DIR}/intermediate/csr" \
			"${SSL_CA_DIR}/intermediate/private" \
			"${SSL_CA_DIR}/intermediate/new" && \
		chmod 700 "${SSL_CA_DIR}/intermediate/private"
}

tlsgenca() {
	tlscreatedirs
	_UMASK="$(umask)"
	umask 077
	_CACACHE=""
	if [ -f "${SSL_CA_DIR}/private/ca.key.pem" ]; then
		printf "ca key already exists! bailing...\\n" 1>&2
		exit 2
	fi
	printf "generating ca\\n"
	if [ "$(tlscachecredential)" -lt 1 ]; then
	    printf "Enter CA key password: " && read -r _CACACHE 		
		printf "%s" "$_CACACHE" >"${SSL_CA_DIR}/private/cachefile"
		openssl ecparam -genkey -name secp521r1 | openssl ec \
			-aes-256-cbc -passout file:"${SSL_CA_DIR}/private/cachefile" \
			-out "${SSL_CA_DIR}/private/ca.key.pem"
		openssl req -config "${SSL_CA_DIR}/openssl.cnf" -x509 -new \
			-"$MDALGORITHM" -nodes -key "${SSL_CA_DIR}/private/ca.key.pem" -days 3650 \
			-passin file:"${SSL_CA_DIR}/private/cachefile" -out "${SSL_CA_DIR}/certs/ca.cert.pem"
	else 
		openssl ecparam -genkey -name secp521r1 | openssl ec -aes-256-cbc \
			-out "${SSL_CA_DIR}/private/ca.key.pem"
		openssl req -config "${SSL_CA_DIR}/openssl.cnf" -x509 -new \
			-"$MDALGORITHM" -nodes -key "${SSL_CA_DIR}/private/ca.key.pem" -days 3650 \
			-out "${SSL_CA_DIR}/certs/ca.cert.pem"
	fi
	touch "${SSL_CA_DIR}/index.txt"
	umask "${_UMASK}"
}

tlsgenserver() {
	#TODO actually check / generate based on cli
	if [ ! -d "$2" ]; then
		printf "%s is not a directory\\n" "$2"
	fi
	_UMASK="$(umask)"
	umask 077
	printf "generating server keys\\n"
	openssl ecparam -genkey -name secp521r1 | openssl ec -aes-256-cbc \
		-out "$2/$1.key.pem"
	openssl req -x509 -config "${SSL_CA_DIR}/openssl.cnf" -new \
		-nodes -key "$2/$1.key.pem" -days "$DAYSVALID" \
		-out "$2/$1.csr.pem"
	cat "$2/$1.csr.pem"
	umask "${_UMASK}"
}

tlsgenintermediate() {
	_UMASK="$(umask)"
	umask 077
	if [ -f "${SSL_CA_DIR}/intermediate/private/intermediate.key.pem" ]; then
		printf "intermediate key already exists! bailing...\\n" 1>&2
		exit 2
	fi
	printf "generating intermediate ca\\n"
	if [ "$(tlscachecredential)" -lt 1 ]; then
	    printf "Enter Intermediate CA key password: " && read -r _ICACACHE 		
		printf "%s" "$_ICACACHE" > "${SSL_CA_DIR}/intermediate/private/cachefile"
		openssl ecparam -genkey -name secp521r1 | openssl ec \
			-aes-256-cbc -passout file:"${SSL_CA_DIR}/intermediate/private/cachefile" \
			-out "${SSL_CA_DIR}/intermediate/private/intermediate.key.pem"
		printf "signing intermediate ca\\n"
		touch "${SSL_CA_DIR}/intermediate/index.txt"
		printf "generating intermediate csr\\n"
		openssl req -config "${SSL_CA_DIR}/openssl_intermediate.cnf" -new \
			-key "${SSL_CA_DIR}/intermediate/private/intermediate.key.pem" \
			-passin file:"${SSL_CA_DIR}/intermediate/private/cachefile" \
			-out "${SSL_CA_DIR}/intermediate/csr/intermediate.csr.pem"
		printf "signing intermediate ca\\nenter ca cert password\\n"
		openssl ca -config "${SSL_CA_DIR}/openssl.cnf" \
			-extensions v3_intermediate_ca -days "$CADAYSVALID" -notext \
			-md "$MDALGORITHM" \
			-passin file:"${SSL_CA_DIR}/private/cachefile" \
			-in "${SSL_CA_DIR}/intermediate/csr/intermediate.csr.pem" \
			-out "${SSL_CA_DIR}/intermediate/certs/intermediate.cert.pem"
	else 
		openssl ecparam -genkey -name secp521r1 | openssl ec -aes-256-cbc \
			-out "${SSL_CA_DIR}/intermediate/private/intermediate.key.pem"
		touch "${SSL_CA_DIR}/intermediate/index.txt"
		printf "generating intermediate csr\\n"
		openssl req -config "${SSL_CA_DIR}/openssl_intermediate.cnf" -new -md "$MDALGORITHM" \
			-key "${SSL_CA_DIR}/intermediate/private/intermediate.key.pem" \
			-out "${SSL_CA_DIR}/intermediate/csr/intermediate.csr.pem"
		printf "signing intermediate ca\\nenter ca cert password\\n"
		openssl ca -config "${SSL_CA_DIR}/openssl.cnf" \
			-extensions v3_intermediate_ca -days "$CADAYSVALID" -notext \
			-md "$MDALGORITHM" \
			-in "${SSL_CA_DIR}/intermediate/csr/intermediate.csr.pem" \
			-out "${SSL_CA_DIR}/intermediate/certs/intermediate.cert.pem"
	fi
	umask "${_UMASK}"
}

tlscheckcaondisk() {
	[ -f "$SSL_CA_DIR/private/ca.key.pem" ] && printf "CA file is on disk. This is not a secure configuration and the root CA should not be stored with the intermediate and should only be used to generate new intermediates.\\n" 1>&2
}

tlsserialgen() {
	if [ ! -f "${SSL_CA_DIR}/serial" ]; then
		printf "%s\\n" "$SSLSERIALDEFAULT" > "${SSL_CA_DIR}/serial"
		tlsuseintermediate && \
			printf "%s\\n" "$SSLSERIALDEFAULT" > "${SSL_CA_DIR}/intermediate/serial"
	fi
}

tlscacheclean() {
	rm -f "${SSL_CA_DIR}/intermediate/private/cachefile"
	rm -f "${SSL_CA_DIR}/private/cachefile"
}

tlssignserver(){
	if [ $# != 1 ]; then
		printf "signing only works on a single target, and one is required\\n" 1>&2
		exit 3
	fi
	export _CSROUT=""
	if [ -f "${SSL_CA_DIR}/intermediate/csr/$(basename $1).csr.pem" ]; then
		_CSROUT="${SSL_CA_DIR}/intermediate/certs/$(basename "$1").pem"
	elif [ -f "$1" ]; then
		_CSROUT="$1"
	else
		printf "shortname or path for signature is incorrect %s\\n" "$1"
		exit 3
	fi
	#TODO check if file exists and is readable
	printf "signing csr\\n"
	if [ "$(tlsuseintermediate)" -lt 1 ]; then
		openssl ca -config "${SSL_CA_DIR}/openssl_intermediate.cnf" \
			-extensions v3_intermediate_ca -days "$CADAYSVALID" -notext \
			-md "$MDALGORITHM" \
			-in "$1" \
			-out "$_CSROUT"
	else
		openssl ca -config "${SSL_CA_DIR}/openssl.cnf" \
			-extensions v3_intermediate_ca -days "$CADAYSVALID" -notext \
			-md "$MDALGORITHM" \
			-in "$1" \
			-out "$_CSROUT"
	fi
}

if [ $# -lt 1 ]; then
	printf "%s\\n%b\\n" "$USAGESTRING" "$HELPSTRING" 1>&2
	exit 4
fi

case "$1" in
	"tls")
		shift 1
		case "$1" in
			"info")
				shift 1
			;;
			"help")
				printf "%s\\n%b\\n" "$USAGESTRING" "$HELPSTRING"
				exit 0
			;;
			"ca")
				getconf
				checkvars
				tlscheckcaondisk
				tlscreatedirs
				tlsgenconf
				tlsgenca
				tlsserialgen
				tlsgenintermediate
				tlscacheclean
				exit 0
			;;
			"server")
				shift 1
				getconf
				checkvars
				tlscheckcaondisk
				tlscreatedirs
				tlsgenserver "$1" "$2"
			;;
			"sign")
				shift 1
				getconf
				checkvars
				tlscheckcaondisk
				tlscreatedirs
				tlssignserver "$1"
			;;
			"check")
				shift 1
				getconf
				checkvars
				tlscheckcaondisk
				#TODO soon to expire
			;;
			*)
				printf "%s\\n%b\\n" "$USAGESTRING" "$HELPSTRING" 1>&2
                exit 1
			;;
		esac
	;;
	"ssh")
		shift 1
	;;
	*)
		printf "%s\\n%b\\n" "$USAGESTRING" "$HELPSTRING" 1>&2
		exit 1
	;;
esac