-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_node_cert.sh
360 lines (325 loc) · 11.7 KB
/
gen_node_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
357
358
359
360
#!/bin/bash
set -e
# SHELL_FOLDER=$(cd $(dirname $0);pwd)
current_dir=$(pwd)
key_path=""
gmkey_path=""
output_dir="newNode"
logfile="build.log"
conf_path="conf"
gm_conf_path="gmconf/"
TASSL_CMD="${HOME}"/.fisco/tassl
guomi_mode=
sdk_cert=
LOG_WARN()
{
local content=${1}
echo -e "\033[31m[WARN] ${content}\033[0m"
}
LOG_INFO()
{
local content=${1}
echo -e "\033[32m[INFO] ${content}\033[0m"
}
help() {
cat << EOF
Usage:
-c <cert path> [Required] cert key path
-g <gm cert path> gmcert key path, if generate gm node cert
-s If set -s, generate certificate for sdk
-o <Output Dir> Default ${output_dir}
-h Help
e.g
$0 -c nodes/cert/agency -o newNode #generate node certificate
$0 -c nodes/cert/agency -o newSDK -s #generate sdk certificate
$0 -c nodes/cert/agency -g nodes/gmcert/agency -o newNode_GM #generate gm node certificate
$0 -c nodes/cert/agency -g nodes/gmcert/agency -o newSDK_GM -s #generate gm sdk certificate
EOF
exit 0
}
check_and_install_tassl(){
if [ ! -f "${TASSL_CMD}" ];then
LOG_INFO "Downloading tassl binary ..."
if [[ "$(uname)" == "Darwin" ]];then
curl -#LO https://github.com/FISCO-BCOS/LargeFiles/raw/master/tools/tassl_mac.tar.gz
mv tassl_mac.tar.gz tassl.tar.gz
else
curl -#LO https://github.com/FISCO-BCOS/LargeFiles/raw/master/tools/tassl.tar.gz
fi
tar zxvf tassl.tar.gz && rm tassl.tar.gz
chmod u+x tassl
mkdir -p "${HOME}"/.fisco
mv tassl "${HOME}"/.fisco/tassl
fi
}
parse_params()
{
while getopts "c:o:g:hs" option;do
case $option in
c) [ ! -z $OPTARG ] && key_path=$OPTARG
;;
o) [ ! -z $OPTARG ] && output_dir=$OPTARG
;;
g) guomi_mode="yes" && gmkey_path=$OPTARG
check_and_install_tassl
;;
s) sdk_cert="true";;
h) help;;
esac
done
}
print_result()
{
echo "=============================================================="
LOG_INFO "Cert Path : $key_path"
[ ! -z "${guomi_mode}" ] && LOG_INFO "GM Cert Path: $gmkey_path"
LOG_INFO "Output Dir : $output_dir"
echo "=============================================================="
LOG_INFO "All completed. Files in $output_dir"
}
getname() {
local name="$1"
if [ -z "$name" ]; then
return 0
fi
[[ "$name" =~ ^.*/$ ]] && {
name="${name%/*}"
}
name="${name##*/}"
echo "$name"
}
check_name() {
local name="$1"
local value="$2"
[[ "$value" =~ ^[a-zA-Z0-9._-]+$ ]] || {
echo "$name name [$value] invalid, it should match regex: ^[a-zA-Z0-9._-]+\$"
exit $EXIT_CODE
}
}
exit_with_clean()
{
local content=${1}
echo -e "\033[31m[ERROR] ${content}\033[0m"
if [ -d "${output_dir}" ];then
rm -rf "${output_dir}"
fi
exit 1
}
file_must_exists() {
if [ ! -f "$1" ]; then
exit_with_clean "$1 file does not exist, please check!"
fi
}
dir_must_exists() {
if [ ! -d "$1" ]; then
exit_with_clean "$1 DIR does not exist, please check!"
fi
}
dir_must_not_exists() {
if [ -e "$1" ]; then
LOG_WARN "$1 DIR exists, please clean old DIR!"
exit 1
fi
}
gen_cert_secp256k1() {
capath="$1"
certpath="$2"
name="$3"
type="$4"
openssl ecparam -out $certpath/${type}.param -name secp256k1
openssl genpkey -paramfile "$certpath/${type}.param" -out "$certpath/${type}.key" 2> /dev/null
openssl pkey -in "$certpath/${type}.key" -pubout -out "$certpath/${type}.pubkey" 2> /dev/null
openssl req -new -sha256 -subj "/CN=${name}/O=fisco-bcos/OU=${type}" -key "$certpath/${type}.key" -out "$certpath/${type}.csr"
openssl x509 -req -days 3650 -sha256 -in "$certpath/${type}.csr" -CAkey "$capath/agency.key" -CA "$capath/agency.crt" \
-force_pubkey "$certpath/${type}.pubkey" -out "$certpath/${type}.crt" -CAcreateserial -extensions v3_req -extfile "$capath/cert.cnf" 2> /dev/null
openssl ec -in "$certpath/${type}.key" -outform DER 2> /dev/null | tail -c +8 | head -c 32 | xxd -p -c 32 | cat >"$certpath/${type}.private"
rm -f $certpath/${type}.csr
}
gen_node_cert() {
if [ "" == "$(openssl ecparam -list_curves 2>&1 | grep secp256k1)" ]; then
echo "openssl don't support secp256k1, please upgrade openssl!"
exit -1
fi
agpath="${1}"
agency=$(basename "$agpath")
ndpath="${2}"
node="node"
dir_must_exists "$agpath"
file_must_exists "$agpath/agency.key"
check_name agency "$agency"
dir_must_not_exists "$ndpath"
check_name node "$node"
mkdir -p $ndpath
gen_cert_secp256k1 "$agpath" "$ndpath" "$node" "node"
#nodeid is pubkey
openssl ec -text -noout -in "$ndpath/node.key" 2> /dev/null| sed -n '7,11p' | tr -d ": \n" | awk '{print substr($0,3);}' | cat >"$ndpath/node.nodeid"
cp $agpath/ca.crt $agpath/agency.crt $ndpath
}
gen_node_cert_with_extensions_gm() {
capath="$1"
certpath="$2"
name="$3"
type="$4"
extensions="$5"
$TASSL_CMD genpkey -paramfile $capath/gmsm2.param -out $certpath/gm${type}.key
$TASSL_CMD req -new -subj "/CN=$name/O=fiscobcos/OU=agency" -config "$capath/gmcert.cnf" -key "$certpath/gm${type}.key" -out "$certpath/gm${type}.csr"
$TASSL_CMD x509 -req -CA $capath/gmagency.crt -CAkey $capath/gmagency.key -days 3650 -CAcreateserial -in $certpath/gm${type}.csr -out $certpath/gm${type}.crt -extfile $capath/gmcert.cnf -extensions $extensions
rm -f $certpath/gm${type}.csr
}
gen_node_cert_gm() {
agpath="${1}"
agency=$(basename "$agpath")
ndpath="${2}"
node=$(basename "$ndpath")
dir_must_exists "$agpath"
file_must_exists "$agpath/gmagency.key"
check_name agency "$agency"
mkdir -p $ndpath
dir_must_exists "$ndpath"
check_name node "$node"
mkdir -p $ndpath
gen_node_cert_with_extensions_gm "$agpath" "$ndpath" "$node" node v3_req
gen_node_cert_with_extensions_gm "$agpath" "$ndpath" "$node" ennode v3enc_req
#nodeid is pubkey
$TASSL_CMD ec -in $ndpath/gmnode.key -text -noout | sed -n '7,11p' | sed 's/://g' | tr "\n" " " | sed 's/ //g' | awk '{print substr($0,3);}' | cat > $ndpath/gmnode.nodeid
#serial
if [ "" != "$($TASSL_CMD version | grep 1.0.2)" ];then
$TASSL_CMD x509 -text -in $ndpath/gmnode.crt | sed -n '5p' | sed 's/://g' | tr "\n" " " | sed 's/ //g' | sed 's/[a-z]/\u&/g' | cat > $ndpath/gmnode.serial
else
$TASSL_CMD x509 -text -in $ndpath/gmnode.crt | sed -n '4p' | sed 's/ //g' | sed 's/.*(0x//g' | sed 's/)//g' |sed 's/[a-z]/\u&/g' | cat > $ndpath/gmnode.serial
fi
cp $agpath/gmca.crt $agpath/gmagency.crt $ndpath
cd $ndpath
}
generate_script_template()
{
local filepath=$1
cat << EOF > "${filepath}"
#!/bin/bash
SHELL_FOLDER=\$(cd \$(dirname \$0);pwd)
EOF
chmod +x ${filepath}
}
generate_node_scripts()
{
local output=$1
generate_script_template "$output/start.sh"
cat << EOF >> "$output/start.sh"
fisco_bcos=\${SHELL_FOLDER}/../fisco-bcos
cd \${SHELL_FOLDER}
node=\$(basename \${SHELL_FOLDER})
node_pid=\`ps aux|grep "\${fisco_bcos}"|grep -v grep|awk '{print \$2}'\`
if [ ! -z \${node_pid} ];then
echo " \${node} is running, pid is \$node_pid."
exit 0
else
nohup \${fisco_bcos} -c config.ini 2>>nohup.out &
sleep 0.5
fi
node_pid=\`ps aux|grep "\${fisco_bcos}"|grep -v grep|awk '{print \$2}'\`
if [ ! -z \${node_pid} ];then
echo " \${node} start successfully"
else
echo " \${node} start failed"
cat nohup.out
fi
EOF
generate_script_template "$output/stop.sh"
cat << EOF >> "$output/stop.sh"
fisco_bcos=\${SHELL_FOLDER}/../fisco-bcos
node=\$(basename \${SHELL_FOLDER})
node_pid=\`ps aux|grep "\${fisco_bcos}"|grep -v grep|awk '{print \$2}'\`
try_times=5
i=0
while [ \$i -lt \${try_times} ]
do
if [ -z \${node_pid} ];then
echo " \${node} isn't running."
exit 0
fi
[ ! -z \${node_pid} ] && kill \${node_pid}
sleep 0.4
node_pid=\`ps aux|grep "\${fisco_bcos}"|grep -v grep|awk '{print \$2}'\`
if [ -z \${node_pid} ];then
echo " stop \${node} success."
exit 0
fi
((i=i+1))
done
EOF
}
main(){
if [ ! -z "$(openssl version | grep reSSL)" ];then
export PATH="/usr/local/opt/openssl/bin:$PATH"
fi
while :
do
gen_node_cert "${key_path}" "${output_dir}"
cd ${output_dir}
mkdir -p ${conf_path}/
mv *.* ${conf_path}/
cd ${current_dir}
#private key should not start with 00
privateKey=$(openssl ec -in "${output_dir}/${conf_path}/node.key" -text 2> /dev/null| sed -n '3,5p' | sed 's/://g'| tr "\n" " "|sed 's/ //g')
len=${#privateKey}
head2=${privateKey:0:2}
if [ "64" != "${len}" ] || [ "00" == "$head2" ];then
rm -rf ${output_dir}
continue;
fi
if [ -n "$guomi_mode" ]; then
gen_node_cert_gm ${gmkey_path} ${output_dir} > ${logfile} 2>&1
mkdir -p ${gm_conf_path}/
mv ./*.* ${gm_conf_path}/
cd ${current_dir}
#private key should not start with 00
privateKey=$($TASSL_CMD ec -in "${output_dir}/${gm_conf_path}/gmnode.key" -text 2> /dev/null| sed -n '3,5p' | sed 's/://g'| tr "\n" " "|sed 's/ //g')
len=${#privateKey}
head2=${privateKey:0:2}
if [ "64" != "${len}" ] || [ "00" == "$head2" ];then
rm -rf ${output_dir}
continue;
fi
fi
break;
done
# generate_node_scripts "${output_dir}"
cat ${key_path}/agency.crt >> ${output_dir}/${conf_path}/node.crt
cat ${key_path}/ca.crt >> ${output_dir}/${conf_path}/node.crt
if [ -n "$guomi_mode" ]; then
cat ${gmkey_path}/gmagency.crt >> ${output_dir}/${gm_conf_path}/gmnode.crt
#move origin conf to gm conf
# rm ${output_dir}/${conf_path}/node.nodeid
cp -r ${output_dir}/${conf_path} ${output_dir}/${gm_conf_path}/origin_cert
#remove original cert files
rm -rf ${output_dir:?}/${conf_path}
mv ${output_dir}/${gm_conf_path} ${output_dir}/${conf_path}
fi
if [[ -n "${sdk_cert}" ]]; then
if [ -n "$guomi_mode" ]; then
mv "${output_dir}/${conf_path}/gmnode.key" "${output_dir}/${conf_path}/gmsdk.key"
mv "${output_dir}/${conf_path}/gmnode.crt" "${output_dir}/${conf_path}/gmsdk.crt"
mv "${output_dir}/${conf_path}/gmennode.key" "${output_dir}/${conf_path}/gmensdk.key"
mv "${output_dir}/${conf_path}/gmennode.crt" "${output_dir}/${conf_path}/gmensdk.crt"
mv "${output_dir}/${conf_path}/gmca.crt" "${output_dir}/${conf_path}/gmca.crt"
mv "${output_dir}/${conf_path}/gmnode.nodeid" "${output_dir}/${conf_path}/gmsdk.publickey"
mv "${output_dir}/${conf_path}/origin_cert/node.key" "${output_dir}/sdk.key"
mv "${output_dir}/${conf_path}/origin_cert/node.crt" "${output_dir}/sdk.crt"
mv "${output_dir}/${conf_path}/origin_cert/ca.crt" "${output_dir}/ca.crt"
mv "${output_dir}/${conf_path}/origin_cert/node.nodeid" "${output_dir}/sdk.publickey"
rm -rf "${output_dir:?}/${conf_path}/origin_cert"
rm "${output_dir}/${conf_path}/gmnode*"
mv "${output_dir}/${conf_path}" "${output_dir}/gm"
else
mv "${output_dir}/${conf_path}/node.key" "${output_dir}/sdk.key"
mv "${output_dir}/${conf_path}/node.nodeid" "${output_dir}/sdk.publickey"
mv "${output_dir}/${conf_path}/node.crt" "${output_dir}/sdk.crt"
mv "${output_dir}/${conf_path}/ca.crt" "${output_dir}/ca.crt"
rm -rf "${output_dir:?}/${conf_path}"
fi
fi
if [ -f "${logfile}" ];then rm "${logfile}";fi
}
parse_params $@
main
print_result