forked from redredgroovy/easy-ca
-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-signing-ca
executable file
·179 lines (142 loc) · 4.46 KB
/
create-signing-ca
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
#!/bin/bash
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# Derek Moore <derek.moore@gmail.com>
# Christian Göttsche <cgzones@googlemail.com>
set -eu
set -o pipefail
umask 0077
BIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# shellcheck disable=SC1090
source "${BIN_DIR}/functions"
# shellcheck disable=SC1090
source "${BIN_DIR}/defaults.conf"
usage() {
echo "Usage: $0 [-l] -d CA_DIR"
echo "Initializes a new signing sub-CA in the directory CA_DIR"
echo "Must be run inside a root CA dir"
echo
echo "Options:"
echo " -d CA_DIR Target directory to be created and initialized"
echo " Must not exist yet"
echo " -l Symlink toolchain (designed for development)"
echo
}
if ! openssl verify -CAfile ca/ca.crt ca/ca.crt >/dev/null 2>&1; then
echo -e -n "$ERR " && usage && exit 2
fi
CA_DIR=
SYMLINK=0
while getopts d:hl FLAG; do
case $FLAG in
d) CA_DIR=${OPTARG} ;;
h) echo -e -n "$SUCC " && usage && exit 0;;
l) SYMLINK=1 ;;
*) echo -e -n "$ERR " && usage && exit 2;;
esac
done
if [ $OPTIND -le $# ]; then
echo -e -n "$ERR " && usage && exit 2
elif [ "${CA_DIR}" = "" ]; then
echo -e -n "$SUCC " && usage && exit 2
fi
PARENT="${BIN_DIR}/.."
CA_NAME="$( basename "${CA_DIR}" )"
CA_PATH="$( fullpath "${CA_DIR}")"
export CA_PATH
echo
echo -e "$NOTE Creating new signing sub-CA in '${CA_DIR}'"
echo
init_ca_home "${CA_DIR}"
trap 'rm -Rf "${CA_DIR}"' 0 2
# early verification of root ca password
echo -e -n "$INPUT Enter passphase for root CA key: "
read -r -s PARENT_PASS
echo
export CA_PARENT_PASS="${PARENT_PASS}"
openssl rsa -check \
-in ca/private/ca.key \
-passin env:CA_PARENT_PASS \
-noout
generate_conf "${CA_DIR}/bin/defaults.conf"
# shellcheck disable=SC1090
source "${CA_DIR}/bin/defaults.conf"
# shellcheck disable=SC2153
CA_CERT_CN="${CA_CERT_O} Certificate ${CA_NAME}"
echo -e -n "$INPUT Common Name for CA certificate [${CA_CERT_CN}]: "
read -r CERT_CN
if [ -n "${CERT_CN}" ]; then
CA_CERT_CN="${CERT_CN}"
fi
echo
echo -e -n "$INPUT Enter passphase for encrypting signing sub-CA key: "
read -r -s PASS1
echo
if [ ${#PASS1} -lt 4 ]; then
echo -e "$ERR Passphrase is too short, please use at least 4 characters!"
exit 1
fi
echo -e -n "$INPUT Verifying - Enter passphase for encrypting signing sub-CA key: "
read -r -s PASS2
echo
if [ "${PASS1}" != "${PASS2}" ]; then
echo -e "$ERR Passphrases did not match, exiting."
exit 1
fi
export CA_PASS"=${PASS1}"
export SAN=""
pushd "${CA_DIR}" > /dev/null
# Generate the signing CA openssl config
template "${BIN_DIR}/templates/signing.tpl" "ca/ca.conf"
echo -e "$NOTE Creating the signing sub-CA key"
# Create the signing CA key
openssl genrsa -out ca/private/ca.key -"${CA_KEY_ALG}" -passout env:CA_PASS "${CA_KEY_LENGTH_SIGNCA}"
chmod 0400 ca/private/ca.key
echo -e "$NOTE Creating the signing sub-CA csr"
openssl req -new -batch \
-config ca/ca.conf \
-key ca/private/ca.key \
-out ca/ca.csr \
-passin env:CA_PASS
echo -e "$NOTE Creating the signing sub-CA certificate"
pushd "${PARENT}" > /dev/null
openssl ca -batch -notext \
-config ca/ca.conf \
-in "${CA_DIR}/ca/ca.csr" \
-out "${CA_DIR}/ca/ca.crt" \
-extensions signing_ca_ext \
-passin env:CA_PARENT_PASS
popd > /dev/null
echo -e "$NOTE Creating the signing sub-CA CRL"
openssl ca -gencrl -batch \
-config ca/ca.conf \
-out ca/ca.crl \
-passin env:CA_PASS
echo -e "$NOTE Creating the chain bundle"
cat "${PARENT}/ca/chain.pem" > ca/chain.pem
cat ca/ca.crt >> ca/chain.pem
cp "${PARENT}/ca/root.crt" ca/root.crt
echo -e "$NOTE Verifying trusted chain"
openssl verify -CAfile ca/chain.pem ca/ca.crt
openssl verify -CAfile ca/root.crt ca/ca.crt
if [ $SYMLINK -eq 1 ]; then
echo -e "$NOTE Symlinking toolchain (dev mode)"
CP_CMD='ln -s'
else
echo -e "$NOTE Copying toolchain"
CP_CMD='cp'
fi
$CP_CMD "${BIN_DIR}/../README.md" README.md
for BIN in ${BINARIES_SIGN}; do
$CP_CMD "${BIN_DIR}/${BIN}" bin/
done
mkdir bin/templates/
for TPL in ${TEMPLATES_SIGN}; do
$CP_CMD "${BIN_DIR}/templates/${TPL}" bin/templates/
done
popd > /dev/null
unset CA_PASS
unset CA_PARENT_PASS
trap - 0 2
echo -e "$SUCC Signing sub-CA initialized."