-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselfsign-ca.sh
executable file
·132 lines (104 loc) · 3.89 KB
/
selfsign-ca.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
#!/usr/bin/sh
# Generates your own Certificate Authority for development.
# This script should be executed just once.
set -e
SELFSIGN_INSTALL_DIR=.
CA_DAYS=1825
CERT_OPTS="-days $CA_DAYS"
if [ ! command -v openssl &> /dev/null ]; then
echo "openssl could not be found. Please install OpenSSL and run this script after"
exit
fi
gen_ca_cert () {
local SETUP_OPENSSL_CNF="$SELFSIGN_INSTALL_DIR/ca/openssl.cnf"
local SETUP_CERT_PEM_PATH="$SELFSIGN_INSTALL_DIR/ca/certs/ca.cert.pem"
local SETUP_CERT_PKEY_PATH="$SELFSIGN_INSTALL_DIR/ca/private/ca.key.pem"
# Generate private key
if [ ! -f "$SETUP_CERT_PKEY_PATH" ]; then
echo "Generating CA private key"
openssl genrsa -out $SETUP_CERT_PKEY_PATH 4096
chmod 400 $SETUP_CERT_PKEY_PATH
echo "Generated CA private key"
else
echo "CA private key already exists"
fi
if [ ! -f "$SETUP_CERT_PEM_PATH" ]; then
echo "Generating CA certificate"
openssl req -config $SETUP_OPENSSL_CNF \
-key $SETUP_CERT_PKEY_PATH \
-new -x509 -sha256 \
$CERT_OPTS \
-out $SETUP_CERT_PEM_PATH
chmod 444 $SETUP_CERT_PEM_PATH
echo "Created CA certificate"
else
echo "CA certificate already exists"
fi
}
gen_intermediate_cert() {
local SETUP_CA_OPENSSL_CNF="$SELFSIGN_INSTALL_DIR/ca/openssl.cnf"
local SETUP_OPENSSL_CNF="$SELFSIGN_INSTALL_DIR/ca/intermediate/openssl.cnf"
local SETUP_PRIV_DIR="$SELFSIGN_INSTALL_DIR/ca/intermediate/private"
local SETUP_CERTS_DIR="$SELFSIGN_INSTALL_DIR/ca/intermediate/certs"
local SETUP_CSR_DIR="$SELFSIGN_INSTALL_DIR/ca/intermediate/csr"
local SETUP_CERT_PEM_PATH="$SETUP_CERTS_DIR/intermediate.cert.pem"
local SETUP_CERT_PKEY_PATH="$SETUP_PRIV_DIR/intermediate.key.pem"
local SETUP_CERT_CSR_PATH="$SETUP_CSR_DIR/intermediate.csr.pem"
# Generate private key
if [ ! -f "$SETUP_CERT_PKEY_PATH" ]; then
echo "Generating Intermediate CA private key"
openssl genrsa -out $SETUP_CERT_PKEY_PATH 4096
chmod 666 $SETUP_CERT_PKEY_PATH
echo "Generated Intermediate CA private key"
else
echo "Intermediate CA private key already exists"
fi
if [ ! -f "$SETUP_CERT_CSR_PATH" ]; then
echo "Generating Intermediate CA certificate signing request (CSR)"
openssl req -config $SETUP_OPENSSL_CNF -new -sha256 \
-extensions v3_intermediate_ca \
-key $SETUP_CERT_PKEY_PATH \
-out $SETUP_CERT_CSR_PATH
echo "Generated Intermediate CA certificate signing request (CSR)"
else
echo "Intermediate CA certificate signing request (CSR) already exists"
fi
if [ ! -f "$SETUP_CERT_PEM_PATH" ]; then
echo "Generating Intermediate CA certificate"
openssl ca -config $SETUP_CA_OPENSSL_CNF \
-extensions v3_intermediate_ca \
-notext -md sha256 \
$CERT_OPTS \
-in $SETUP_CERT_CSR_PATH \
-out $SETUP_CERT_PEM_PATH
chmod 666 $SETUP_CERT_PEM_PATH
echo "Generated Intermediate CA certificate"
else
echo "Intermediate CA certificate already exists"
fi
}
gen_chain_cert () {
local SETUP_CA_PATH="$SELFSIGN_INSTALL_DIR/ca/certs/ca.cert.pem"
local SETUP_INTERMEDIATE_PATH="$SELFSIGN_INSTALL_DIR/ca/intermediate/certs/intermediate.cert.pem"
local SETUP_CHAIN_CERT_PEM_PATH="$SELFSIGN_INSTALL_DIR/ca/intermediate/certs/chain.cert.pem"
# Create the intermediate chain
if [ -f "$SETUP_CA_PATH" ]; then
if [ -f "$SETUP_INTERMEDIATE_PATH" ]; then
echo "Generating chain certificate"
if [ -f "$SETUP_CHAIN_CERT_PEM_PATH" ]; then
chmod 666 $SETUP_CHAIN_CERT_PEM_PATH
fi
cat $SETUP_INTERMEDIATE_PATH $SETUP_CA_PATH > $SETUP_CHAIN_CERT_PEM_PATH
chmod 666 $SETUP_CHAIN_CERT_PEM_PATH
echo "Generated chain certificate $SETUP_CHAIN_CERT_PEM_PATH"
else
echo "Missing Intermediate certificate"
fi
else
echo "Missing CA certificate"
fi
}
# Verify or setup CA directory
gen_ca_cert
gen_intermediate_cert
gen_chain_cert