blob: 8d2f4b2bcf10437e1b76d1c3d55072f1d0dafadf (
plain)
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
|
#!/bin/bash
# generate new ca certificate, roll over the old one(s)
set -e
key_dir='#ETCDIR#/simple-pki/keys'
if [ -r '#ETCDIR#/simple-pki/ca.conf' ]; then
. '#ETCDIR#/simple-pki/ca.conf'
fi
if [ -n "${ca_user}" ] \
&& [ "$(whoami)" != "${ca_user}" ]; then
exec su "${ca_user}" -c "$0"
fi
if [ -f "${key_dir}/${ca_name}.key.new" ] \
&& [ -f "${key_dir}/${ca_name}.crt.new" ]; then
if [ "$(stat -c%Y "${key_dir}/${ca_name}.key.new")" -lt "$(($(date +%s)-60*60*24*ca_min_duration))" ] \
|| [ ! -f "${key_dir}/${ca_name}.key" ] \
|| [ "$(stat -c%Y "${key_dir}/${ca_name}.crt.new")" -lt "$(($(date +%s)-60*60*24*ca_min_duration))" ] \
|| [ ! -f "${key_dir}/${ca_name}.crt" ]; then
mv "${key_dir}/${ca_name}.key"{.new,}
mv "${key_dir}/${ca_name}.crt"{.new,}
fi
fi
if [ ! -f "${key_dir}/${ca_name}.key.new" ] \
|| [ ! -f "${key_dir}/${ca_name}.crt.new" ]; then
openssl req -new \
-newkey rsa:4096 -sha256 \
-keyout "${key_dir}/${ca_name}.key.new" \
-out "${key_dir}/${ca_name}.csr.new" \
-nodes \
-subj "${ca_subject_prefix}"'/CN=Certification Authority' \
-addext 'subjectKeyIdentifier = hash' \
-addext 'basicConstraints = critical, CA:true' \
-addext 'keyUsage = keyCertSign, cRLSign'
if [ -f "${key_dir}/${ca_name}.key" ]; then
previous_key="${key_dir}/${ca_name}.key"
else
previous_key="${key_dir}/${ca_name}.key.new"
fi
openssl req -x509 \
-sha256 \
-in "${key_dir}/${ca_name}.csr.new" \
-key "${previous_key}" \
-out "${key_dir}/${ca_name}.crt.new" \
-days 365 -nodes \
-addext 'subjectKeyIdentifier = hash' \
-addext 'authorityKeyIdentifier = keyid:always, issuer' \
-addext 'basicConstraints = critical, CA:true' \
-addext 'keyUsage = keyCertSign, cRLSign'
rm "${key_dir}/${ca_name}.csr.new"
fi
rsync --ignore-missing-args \
"${key_dir}/${ca_name}.crt"{.new,} \
"${remote_host}:${remote_dir}/"
(
cd "${key_dir}"
find . -maxdepth 1 \
-type f \( -name "${ca_name}"'.crt' -o -name "${ca_name}"'.crt.new' \) \
-printf '%TY-%Tm-%TdT%TT ' \
-exec sha512sum {} \; \
| sed '
s/\.[0-9]\+ / /
s@\s\s\+\(\S\+/\)\?@ @
'
) \
| ssh "${remote_host}" '
cd "'"${remote_dir}"'"
while read -r time sum file; do
rm -f ????-??-??T??\:??\:??".${file}"
mv "${file}" "${time}.${file}"
sed -i '"'"'
/ [^.]\+\.'"'"'"${file//./\.}"'"'"'$/d
'"'"' sha512sums
printf '"'"'%s %s\n'"'"' "${sum}" "${time}.${file}" \
>> sha512sums
done
'
|