-
Notifications
You must be signed in to change notification settings - Fork 2
/
woodyssl
executable file
·331 lines (284 loc) · 7.65 KB
/
woodyssl
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
#!/bin/bash
# Main entry for the program
#
#
#
#
#
VERSION="1.0"
AUTHOR="Woody Huang"
AUTHOR_EMAIL="woodyhuang1@gmail.com"
COMMAND_NAME="woodyssl"
ROOT_DIR="${HOME}"/."${COMMAND_NAME}"
CA_DIR="${ROOT_DIR}"/ca
CERT_DIR="${ROOT_DIR}"/cert
INSTALL_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
if [ -L "$INSTALL_DIR"/woodyssl ]; then
COMMAND_SOURCE_LOCATION=$(readlink "${INSTALL_DIR}/woodyssl")
INSTALL_DIR=$(dirname "${COMMAND_SOURCE_LOCATION}")
fi
TEMPLATE_DIR="${INSTALL_DIR}"/templates
function show_help_info {
echo -e "${COMMAND_NAME}: SSL certificate manager
usage: ${COMMAND_NAME} [OPTIONS]
-h --help show help informations
-d --domain domain name to manage
-c --create create if the domain given by -d does not exists
--clean clean everything
--ca set the folder of root certificate. The name of certificate files should be
root-ca.crt and root-ca.key
-i --import import root ca given by --ca
-e --export export certificate. if -d is set, then export the certificate of the specific domain,
otherwise, export the root ca certificate
-f --forced. used with -c, create the certificate even if the domain already exists
-r --rm remove the domain certificate
-l --list list all domains maintained by this tool
--subj set subj info for csr file. In following format: \"/C=CN/ST=Beijing/L=Beijing/O=Tsinghua/CN=Vlion Club\"
this settings will be saved and applied in the following commands.
-v --version show version"
exit
}
function set_domain_operation {
if [ -z "${DOMAIN_OPERATION}" ]; then
DOMAIN_OPERATION="$1"
else
echo "Conflict operation flag"
echo ""
show_help_info
exit
fi
}
DOMAIN_NAME=
DOMAIN_OPERATION=
FORCE_CREATE=
EXTERNAL_CA=
DEFAULT_CA_DIR="${CA_DIR}"
EXPORT_DIR=
REQURE_DOMAIN_NAME=
DEFAULT_SUBJ="/C=CN/ST=Beijing/L=Beijing/O=Tsinghua/CN=Vlion Club"
if [ -f "${TEMPLATE_DIR/subj}" ]; then
OPENSSL_SUBJ=$(cat ${TEMPLATE_DIR}/subj)
else
OPENSSL_SUBJ="${DEFAULT_SUBJ}"
fi
SET_USER_SUBJ=
# extract param
while [ $# -gt 0 ]
do
key="$1"
case $key in
-h|--help)
show_help_info
shift
exit
;;
-d|--domain)
DOMAIN_NAME="$2"
shift
shift
;;
-c|--create)
set_domain_operation "create"
shift
;;
-r|--rm)
set_domain_operation "rm"
REQURE_DOMAIN_NAME=true
shift
;;
--clean)
set_domain_operation "clean"
shift
;;
-l|--list)
if [ -d "${CERT_DIR}" ]; then
cd "${CERT_DIR}"
ls -1
else
echo "No certificate found"
fi
exit
;;
-f|--forced)
FORCE_CREATE=true
shift
;;
--subj)
OPENSSL_SUBJ="$2"
if [ -z "${OPENSSL_SUBJ}" ]; then
echo "Invalid subj"
exit
fi
SET_USER_SUBJ=true
shift
shift
;;
-v|--version)
echo "${COMMAND_NAME} ${VERSION}"
echo "Created by ${AUTHOR} (${AUTHOR_EMAIL})"
exit
;;
--ca)
CA_DIR=$(readlink -f "$2")
if [ ! -d "${CA_DIR}" ] || [ ! -f "${CA_DIR}"/root-ca.crt ] || [ ! -f "${CA_DIR}"/root-ca.key ]; then
echo "Root certificate not found in ${CA_DIR}"
echo "Make sure the folder exists, and the certificate is named as root-ca.key and root-ca.crt"
echo ""
exit
fi
EXTERNAL_CA=true
shift
shift
;;
-i|--import)
set_domain_operation "import"
shift
;;
-e|--export)
set_domain_operation "export"
EXPORT_DIR=$(readlink -f "$2")
if [ ! -d "${EXPORT_DIR}" ]; then
echo "Export destination folder ${EXPORT_DIR} not exists"
exit
fi
shift
shift
;;
*)
echo "Unrecognized flag ${key}"
echo "Use ${COMMAND_NAME} -h to show help informations"
echo ""
show_help_info
exit
;;
esac
done
if [ "${DOMAIN_OPERATION}" == "clean" ]; then
if [ -d "${ROOT_DIR}" ]; then
rm -rf "${ROOT_DIR}"
fi
exit
fi
if [ ! -d "${ROOT_DIR}" ]; then
echo "Create ssl configs container at ${ROOT_DIR}."
mkdir "${ROOT_DIR}"
fi
cd "${ROOT_DIR}"
if [ ! -d "${CERT_DIR}" ]; then
echo "Create certificate diretory at ${CERT_DIR}."
mkdir "${CERT_DIR}"
fi
if [ "${REQURE_DOMAIN_NAME}" == true ] && [ -z "${DOMAIN_NAME}" ]; then
echo "Domain name not set"
echo ""
show_help_info
exit
fi
if [ "${DOMAIN_OPERATION}" == "import" ]; then
if [ ! -d "${DEFAULT_CA_DIR}" ]; then
mkdir "${DEFAULT_CA_DIR}"
fi
if [ "${EXTERNAL_CA}" == true ] && [ "${CA_DIR}" != "${DEFAULT_CA_DIR}" ] ; then
cp "${CA_DIR}"/root-ca.key "${DEFAULT_CA_DIR}"/
cp "${CA_DIR}"/root-ca.crt "${DEFAULT_CA_DIR}"/
echo "done importing external CA"
exit
elif [ "${DOMAIN_OPERATION}" == "import" ] && [ ! "${EXTERNAL_CA}" == true ]; then
echo "Cannot import without ca"
echo ""
show_help_info
exit
elif [ "${CA_DIR}" == "${DEFAULT_CA_DIR}" ]; then
echo "The ca folder to be imported is the default folder, ignored"
exit
fi
elif [ "${DOMAIN_OPERATION}" == "rm" ]; then
cd "${CERT_DIR}"
if [ -d "./${DOMAIN_NAME}" ]; then
rm -rf ./"${DOMAIN_NAME}"
echo "Certificate of ${DOMAIN_NAME} is removed"
else
echo "Certificate for ${DOMAIN_NAME} not found"
fi
exit
elif [ "${DOMAIN_OPERATION}" == "export" ]; then
cd "${CERT_DIR}"
if [ -z "${DOMAIN_NAME}" ]; then
# export root
cp "${CA_DIR}"/root-ca.crt "${EXPORT_DIR}"/
cp "${CA_DIR}"/root-ca.key "${EXPORT_DIR}"/
elif [ -d "./${DOMAIN_NAME}" ]; then
cp "./${DOMAIN_NAME}/${DOMAIN_NAME}.key" "${EXPORT_DIR}"/
cp "./${DOMAIN_NAME}/${DOMAIN_NAME}.crt" "${EXPORT_DIR}"/
else
echo "Certificate of ${DOMAIN_NAME} not found"
exit
fi
elif [ "${DOMAIN_OPERATION}" == "create" ]; then
# make sure CA is created
if [ ! "${EXTERNAL_CA}" == true ] && [ ! -d "${CA_DIR}" ]; then
mkdir "${CA_DIR}"
fi
cd "${CA_DIR}"
# Make sure CA is generated
if [ ! -f "./root-ca.key" ]; then
echo "Root certificate not found, create a new one."
# generate CA private key
openssl genrsa -out "root-ca.key" 4096
# use the key to generate ca request file
openssl req \
-new -key "root-ca.key" \
-out "root-ca.csr" -sha256 \
-subj "${OPENSSL_SUBJ}"
# generate the ca root certificate
openssl x509 -req -days 3650 -in "root-ca.csr" \
-signkey "root-ca.key" -sha256 -out "root-ca.crt" \
-extfile "${TEMPLATE_DIR}/root-ca.cnf" -extensions \
root_ca
if [ "${SET_USER_SUBJ}" == true ]; then
echo "${OPENSSL_SUBJ}" > "${TEMPLATE_DIR}/subj"
fi
fi
if [ -z "${DOMAIN_NAME}" ]; then
echo "Success creating root certificate"
echo "done"
exit
fi
cd "${CERT_DIR}"
if [ -z "${FORCE_CREATE}" ] && [ -d "./${DOMAIN_NAME}" ]; then
echo "Certificate for ${DOMAIN_NAME} already exists"
exit
fi
if [ ! -d "./${DOMAIN_NAME}" ]; then
mkdir "./${DOMAIN_NAME}"
echo ""
echo "Create certificate for ${DOMAIN_NAME} using root ca located at ${CA_DIR}"
echo ""
else
echo ""
echo "Overriding exisiting certificate of ${DOMAIN_NAME}"
echo ""
fi
cd "./${DOMAIN_NAME}"
# generate key
openssl genrsa -out "${DOMAIN_NAME}.key" 4096
# generate csr file
openssl req -new -key "${DOMAIN_NAME}.key" -out "site.csr" -sha256 \
-subj "${OPENSSL_SUBJ} ${DOMAIN_NAME}"
SITE_CNF_FILE_TEMPLATE=$(cat "${TEMPLATE_DIR}"/site.cnf)
SITE_CNF_FILE_CONTENT=${SITE_CNF_FILE_TEMPLATE/__DOMAIN_NAME__/${DOMAIN_NAME}}
echo "${SITE_CNF_FILE_CONTENT}" > ./site.cnf
echo "
content of cnf file used:
${SITE_CNF_FILE_CONTENT}
"
openssl x509 -req -days 750 -in "site.csr" -sha256 \
-CA "${CA_DIR}/root-ca.crt" -CAkey "${CA_DIR}/root-ca.key" -CAcreateserial \
-out "${DOMAIN_NAME}.crt" -extfile "site.cnf" -extensions server
echo "Success creating certificate for ${DOMAIN_NAME}"
echo "The certificate is stored is $(pwd)"
else
echo "Select a operation"
echo ""
show_help_info
fi