-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcert.sh
executable file
·356 lines (298 loc) · 7.73 KB
/
cert.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
#!/bin/sh
# /*
# Copyright (C) HWPORT.COM
# All rights reserved.
# Author: JAEHYUK CHO <mailto:minzkn@minzkn.com>
# */
# *.key : 개인키 파일 (유출 주의)
# *.enc.key : 개인키 암호화된 파일 (유출 주의)
# *.csr : 인증서 요청 파일
# *.der : DER형식의 인증서 파일
# *.pem : PEM형식의 인증서 파일
# *.srl : Serial 파일
# OpenSSL 명령어
DEF_openssl_command=openssl
DEF_rootca_config_pathname=rootca.conf
DEF_host_config_pathname=host.conf
DEF_rootca_pathname=rootca
DEF_rootca_expire_days=36500
DEF_host_expire_days=1825
generate_key_file()
{
local s_pathname=${1}
[ -z "${s_pathname}" ] && return 1
# 2048bit 개인키 생성하여 개인키 분실에 대비해 AES 256bit 로 암호화 합니다.
# AES 이므로 암호(pass phrase)를 분실하면 개인키를 얻을수 없으니 꼭 기억해야 합니다.
echo "Generating KEY(Encrypt) file : ${s_pathname}.enc.key"
${DEF_openssl_command} genrsa -aes256 -out "${s_pathname}.enc.key" 2048
[ "${?}" -eq 0 -a -f "${s_pathname}.enc.key" ] || return 1
#개인키의 유출 방지를 위해 group 과 other의 permission 을 모두 제거합니다.
chmod 600 "${s_pathname}.enc.key"
# 개인키 암호 해제 (이 파일은 최대한 보안 유지 필요, 이 파일은 암호를 매번 물어보지 않게 되지만 보안상 유출되지 않도록 특별히 관리 필요)
echo "Removing pass phrase KEY file : ${s_pathname}.key from ${s_pathname}.enc.key"
${DEF_openssl_command} \
rsa \
-in "${s_pathname}.enc.key" \
-out "${s_pathname}.key"
[ "${?}" -eq 0 -a -f "${s_pathname}.key" ] || return 1
#개인키의 유출 방지를 위해 group 과 other의 permission 을 모두 제거합니다.
chmod 600 "${s_pathname}.key"
echo "Generated KEY file."
return 0
}
generate_csr_file()
{
local s_pathname=${1}
local s_config_pathname=${2}
[ -z "${s_pathname}" ] && return 1
[ -z "${s_config_pathname}" ] && s_config_pathname="${s_pathname}.conf"
# 인증서 요청 생성
echo "Generating CSR file : ${s_pathname}.csr"
if [ -f "${s_config_pathname}" ]
then
${DEF_openssl_command} req -new -key "${s_pathname}.enc.key" -out "${s_pathname}.csr" -config "${s_config_pathname}"
else
${DEF_openssl_command} req -new -key "${s_pathname}.enc.key" -out "${s_pathname}.csr"
fi
[ "${?}" -eq 0 -a -f "${s_pathname}.csr" ] || return 1
echo "Generated CSR file."
return 0
}
generate_root_crt_file()
{
local s_pathname=${1}
local s_expire_days=${2}
local s_config_pathname=${3}
[ -z "${s_pathname}" ] && return 1
[ -z "${s_expire_days}" ] && return 1
[ -z "${s_config_pathname}" ] && s_config_pathname="${s_pathname}.conf"
echo "Generating CRT file : ${s_pathname}.crt"
# self-signed 인증서 생성
if [ -f "${s_config_pathname}" ]
then
${DEF_openssl_command} \
x509 \
-req \
-days "${s_expire_days}" \
-extensions v3_ca \
-set_serial 1 \
-in "${s_pathname}.csr" \
-signkey "${s_pathname}.enc.key" \
-outform PEM \
-out "${s_pathname}.crt" \
-extfile "${s_config_pathname}"
else
${DEF_openssl_command} \
x509 \
-req \
-days "${s_expire_days}" \
-extensions v3_ca \
-set_serial 1 \
-in "${s_pathname}.csr" \
-signkey "${s_pathname}.enc.key" \
-outform PEM \
-out "${s_pathname}.crt"
fi
[ "${?}" -eq 0 -a -f "${s_pathname}.crt" ] || return 1
echo "Generated CRT file."
echo "Copying CRT to PEM file : ${s_pathname}.pem"
cp -f "${s_pathname}.crt" "${s_pathname}.pem"
[ "${?}" -eq 0 -a -f "${s_pathname}.pem" ] || return 1
echo "Copyed PEM file."
echo "Converting PEM to DER file : ${s_pathname}.der"
${DEF_openssl_command} \
x509 \
-inform PEM \
-in "${s_pathname}.pem" \
-outform DER \
-out "${s_pathname}.der"
[ "${?}" -eq 0 -a -f "${s_pathname}.der" ] || return 1
echo "Converted DER file."
return 0
}
generate_crt_file()
{
local s_pathname=${1}
local s_expire_days=${2}
local s_config_pathname=${3}
local s_rootca_pathname=${4}
[ -z "${s_pathname}" ] && return 1
[ -z "${s_expire_days}" ] && return 1
[ -z "${s_config_pathname}" ] && s_config_pathname="${s_pathname}.conf"
[ -z "${s_rootca_pathname}" ] && return 1
echo "Generating CRT file : ${s_pathname}.crt"
if [ -f "${s_config_pathname}" ]
then
${DEF_openssl_command} \
x509 \
-req \
-days "${s_expire_days}" \
-extensions v3_user \
-in "${s_pathname}.csr" \
-CA "${s_rootca_pathname}.crt" \
-CAcreateserial \
-CAkey "${s_rootca_pathname}.enc.key" \
-outform PEM \
-out "${s_pathname}.crt" \
-extfile "${s_config_pathname}"
else
${DEF_openssl_command} \
x509 \
-req \
-days "${s_expire_days}" \
-extensions v3_user \
-in "${s_pathname}.csr" \
-CA "${s_rootca_pathname}.crt" \
-CAcreateserial \
-CAkey "${s_rootca_pathname}.enc.key" \
-outform PEM \
-out "${s_pathname}.crt"
fi
[ "${?}" -eq 0 -a -f "${s_pathname}.crt" ] || return 1
echo "Generated CRT file."
echo "Copying CRT to PEM file : ${s_pathname}.pem"
cp -f "${s_pathname}.crt" "${s_pathname}.pem"
[ "${?}" -eq 0 -a -f "${s_pathname}.pem" ] || return 1
echo "Copyed PEM file."
echo "Converting PEM to DER file : ${s_pathname}.der"
${DEF_openssl_command} \
x509 \
-inform PEM \
-in "${s_pathname}.pem" \
-outform DER \
-out "${s_pathname}.der"
[ "${?}" -eq 0 -a -f "${s_pathname}.der" ] || return 1
echo "Converted DER file."
return 0
}
# 제대로 생성되었는지 확인을 위해 인증서의 정보를 출력
dump_info()
{
local s_pathname=${1}
[ -z "${s_pathname}" ] && return 1
echo "Dumping CRT file."
${DEF_openssl_command} \
x509 \
-inform PEM \
-in "${s_pathname}.crt" \
-text
if [ "${?}" -ne 0 ]
then
echo "dump info failed !"
return 1
fi
return 0
}
generate_rootca()
{
local s_pathname=${1}
local s_expire_days=${2}
[ -z "${s_pathname}" ] && return 1
[ -z "${s_expire_days}" ] && return 1
# CA 가 사용할 RSA key pair(public, private key) 생성
generate_key_file \
"${s_pathname}"
if [ "${?}" -ne 0 ]
then
echo "key generate failed !"
return 1
fi
generate_csr_file \
"${s_pathname}" \
"${DEF_rootca_config_pathname}"
if [ "${?}" -ne 0 ]
then
echo "csr generate failed !"
return 1
fi
generate_root_crt_file \
"${s_pathname}" \
"${s_expire_days}" \
"${DEF_rootca_config_pathname}"
if [ "${?}" -ne 0 ]
then
echo "crt generate failed !"
return 1
fi
dump_info \
"${s_pathname}"
return 0
}
generate_cert()
{
local s_pathname=${1}
local s_expire_days=${2}
[ -z "${s_pathname}" ] && s_pathname="host0"
[ -z "${s_expire_days}" ] && return 1
generate_key_file \
"${s_pathname}"
if [ "${?}" -ne 0 ]
then
echo "key generate failed !"
return 1
fi
generate_csr_file \
"${s_pathname}" \
"${DEF_host_config_pathname}"
if [ "${?}" -ne 0 ]
then
echo "csr generate failed !"
return 1
fi
generate_crt_file \
"${s_pathname}" \
"${s_expire_days}" \
"${DEF_host_config_pathname}" \
"${DEF_rootca_pathname}"
if [ "${?}" -ne 0 ]
then
echo "crt generate failed !"
return 1
fi
dump_info \
"${s_pathname}"
return 0
}
case "${1}" in
ca|rootca)
generate_rootca \
"${DEF_rootca_pathname}" \
"${DEF_rootca_expire_days}"
if [ "${?}" -eq 0 ]
then
echo "generated root CA."
else
echo "root CA generate failed !"
return 1
fi
;;
gen|generate|cert|sign)
if [ ! -f "${DEF_rootca_pathname}.crt" ]
then
echo "${DEF_rootca_pathname}.crt not found !"
return 1
fi
generate_cert \
"${2}" \
"${DEF_host_expire_days}"
if [ "${?}" -eq 0 ]
then
echo "generated cert."
else
echo "cert generate failed !"
return 1
fi
;;
clean)
echo "cleaning..."
rm -f *.key *.csr *.crt *.der *.pem *.srl
;;
*)
echo "$(basename ${0}) v1.0 Copyrights (C) HWPORT.COM - All rights reserved."
echo ""
echo "usage: ${0} <rootca | gen [<name>] | clean>"
echo ""
;;
esac
exit 0
# End of cert.sh