~poptart/hosaka-pki

ref: 37325529913857f683edcca7d634e44054604cab hosaka-pki/hosaka-pki.sh -rw-r--r-- 8.1 KiB
37325529 — poptart README cleanup 1 year, 9 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
#!/bin/sh -e
export CONFIGDIR=""
export SSL_CA_DIR=""
export SSH_CA_DIR=""
export DAYSVALID=""
export CADAYSVALID=""
export MDALGORITHM=""
export USEINTERMEDIATE=""
export WARNROOTCERT=""
export COUNTRYDEFAULT=""
export STATEDEFAULT=""
export LOCALITYDEFAULT=""
export ORGNAMEDEFAULT=""
export ORGUNITDEFAULT=""
export USERCERTCOMMENT=""
export SRVCERTCOMMENT=""
export SSLSERIALDEFAULT=""
export SSHSERIALDEFAULT=""

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 "$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"
}

sslgenconf() {
	#Generate conf for the CA and if an intermediate is in use, generate
	#for that as well
	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" \
		< "$CONFIGDIR/util/openssl.cnf.tmpl" > "$SSL_CA_DIR/openssl.cnf"
	ssluseintermediate && 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" \
		< "$CONFIGDIR/util/openssl.cnf.tmpl" > "$SSL_CA_DIR/openssl_intermediate.cnf"
}

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

sslcreatedirs() {
	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"
	ssluseintermediate && \
		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"
}

sslgenca() {
	sslcreatedirs
	_UMASK="$(umask)"
	umask 077
	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"
	#TODO support automation ie, symm keys stored on disk
	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 \
		-md "$MDALGORITHM" -nodes -key "${SSL_CA_DIR}/private/ca.key.pem" -days 3650 \
		-out "${SSL_CA_DIR}/certs/ca.cert.pem"
	touch "${SSL_CA_DIR}/index.txt"
	umask "${_UMASK}"
}

sslgenserver() {
	#TODO actually check / generate based on cli
	_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 \
		-md "$MDALGORITHM" -nodes -key "$2/$1.key.pem" -days "$DAYSVALID" \
		-out "$2/$1.csr.pem"
	cat "$2/$1.csr.pem"
	umask "${_UMASK}"
}

sslgenintermediate() {
	_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"
	#TODO support automation ie, symm keys stored on disk
	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"
	umask "${_UMASK}"
}

sslcheckcaondisk() {
	[ -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
}

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

sslsignserver(){
	if [ $# != 1 ]; then
		printf "signing only works on a single target, and one is required\\n" 1>&2
		exit 3
	fi
	#TODO check if file exists and is readable
	printf "signing csr\\n"
	if [ "$(ssluseintermediate)" ]; then
		openssl ca -config "${SSL_CA_DIR}/openssl_intermediate.cnf" \
			-extensions v3_intermediate_ca -days "$CADAYSVALID" -notext \
			-md "$MDALGORITHM" \
			-in "$1" \
			-out "${SSL_CA_DIR}/intermediate/certs/$(basename "$1").pem"
	else
		openssl ca -config "${SSL_CA_DIR}/openssl.cnf" \
			-extensions v3_intermediate_ca -days "$CADAYSVALID" -notext \
			-md "$MDALGORITHM" \
			-in "$1" \
			-out "${SSL_CA_DIR}/intermediate/certs/$(basename "$1").pem"
	fi
}



#getconf
#sslcheckcaondisk
#sslcreatedirs
#sslgenconf
#sslgenca
#sslserialgen
#sslgenintermediate


#sslsignclient() {
#
#}
#sslcheckexpir() {
#
#}