forked from Azure/azure-iot-sdk-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
certGen.sh
executable file
·370 lines (316 loc) · 13.6 KB
/
certGen.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
361
362
363
364
365
366
367
368
369
370
#!/bin/bash
## Copyright (c) Microsoft. All rights reserved.
## Licensed under the MIT license. See LICENSE file in the project root for full license information.
###############################################################################
# This script demonstrates creating X.509 certificates for an Azure IoT Hub
# CA Cert deployment.
#
# These certs MUST NOT be used in production. It is expected that production
# certificates will be created using a company's proper secure signing process.
# These certs are intended only to help demonstrate and prototype CA certs.
###############################################################################
cert_dir="."
algorithm="genrsa"
key_bits_length="4096"
days_till_expire=100
openssl_config_file="./openssl.cnf"
ca_password="P@ssw0rd"
function makeCNsubject()
{
local result="/CN=${1}"
case $OSTYPE in
msys|win32) result="/${result}"
esac
echo "$result"
}
function warn_certs_not_for_production()
{
tput smso
tput setaf 3
echo "Certs generated by this script are not for production (e.g. they have hard-coded passwords of 'P@ssw0rd'."
echo "This script is only to help you understand Azure IoT Hub CA Certificates."
echo "Use your official, secure mechanisms for this cert generation."
echo "Also note that these certs will expire in ${days_till_expire} days."
tput sgr0
}
function generate_root_ca()
{
local common_name=$1
local password_cmd=" -aes256 -passout pass:${ca_password} "
if [ ! -f ${cert_dir}/private/${common_name}.ca.key.pem ]; then
echo "Creating the Root CA Private Key"
openssl ${algorithm} \
${password_cmd} \
-out ${cert_dir}/private/${common_name}.ca.key.pem \
${key_bits_length}
[ $? -eq 0 ] || exit $?
chmod 400 ${cert_dir}/private/${common_name}.ca.key.pem
[ $? -eq 0 ] || exit $?
fi
echo "Creating the Root CA Certificate"
password_cmd=" -passin pass:${ca_password} "
openssl req \
-new \
-x509 \
-config ${openssl_config_file} \
${password_cmd} \
-key ${cert_dir}/private/${common_name}.ca.key.pem \
-subj "$(makeCNsubject "${common_name}")" \
-days ${days_till_expire} \
-sha256 \
-extensions v3_ca \
-out ${cert_dir}/certs/${common_name}.ca.cert.pem
[ $? -eq 0 ] || exit $?
cp ${cert_dir}/certs/${common_name}.ca.cert.pem ${cert_dir}/certs/${common_name}.chain.cert.pem
[ $? -eq 0 ] || exit $?
chmod 444 ${cert_dir}/certs/${common_name}.ca.cert.pem
[ $? -eq 0 ] || exit $?
echo "CA Root Certificate Generated At:"
echo "---------------------------------"
echo " ${cert_dir}/certs/${common_name}.ca.cert.pem"
echo ""
openssl x509 -noout -text \
-in ${cert_dir}/certs/${common_name}.ca.cert.pem
warn_certs_not_for_production
[ $? -eq 0 ] || exit $?
}
###############################################################################
# Generate Intermediate CA Cert
###############################################################################
function generate_intermediate_ca()
{
local common_name=$1
local ca_common_name=$2
local password_cmd=" -aes256 -passout pass:${ca_password} "
if [ ! -f ${cert_dir}/private/${common_name}.ca.key.pem ]; then
echo "Creating the Intermediate Device CA"
echo "-----------------------------------"
openssl ${algorithm} \
${password_cmd} \
-out ${cert_dir}/private/${common_name}.ca.key.pem \
${key_bits_length}
[ $? -eq 0 ] || exit $?
chmod 400 ${cert_dir}/private/${common_name}.ca.key.pem
[ $? -eq 0 ] || exit $?
fi
echo "Creating the Intermediate Device CA CSR"
echo "-----------------------------------"
password_cmd=" -passin pass:${ca_password} "
openssl req -new -sha256 \
${password_cmd} \
-config ${openssl_config_file} \
-subj "$(makeCNsubject "${common_name}")" \
-key ${cert_dir}/private/${common_name}.ca.key.pem \
-out ${cert_dir}/csr/${common_name}.ca.csr.pem
[ $? -eq 0 ] || exit $?
echo "Signing the Intermediate Certificate with Root CA Cert"
echo "-----------------------------------"
password_cmd=" -passin pass:${ca_password} "
openssl ca -batch \
-config ${openssl_config_file} \
${password_cmd} \
-extensions v3_intermediate_ca \
-days ${days_till_expire} -notext -md sha256 \
-keyfile ${cert_dir}/private/${ca_common_name}.ca.key.pem \
-cert ${cert_dir}/certs/${ca_common_name}.ca.cert.pem \
-in ${cert_dir}/csr/${common_name}.ca.csr.pem \
-out ${cert_dir}/certs/${common_name}.ca.cert.pem
[ $? -eq 0 ] || exit $?
chmod 444 ${cert_dir}/certs/${common_name}.ca.cert.pem
[ $? -eq 0 ] || exit $?
echo "Verify signature of the Intermediate Device Certificate with Root CA"
echo "-----------------------------------"
openssl verify \
-CAfile ${cert_dir}/certs/${ca_common_name}.chain.cert.pem \
${cert_dir}/certs/${common_name}.ca.cert.pem
[ $? -eq 0 ] || exit $?
echo "Intermediate CA Certificate Generated At:"
echo "-----------------------------------------"
echo " ${cert_dir}/certs/${common_name}.ca.cert.pem"
echo ""
openssl x509 -noout -text \
-in ${cert_dir}/certs/${common_name}.ca.cert.pem
[ $? -eq 0 ] || exit $?
echo "Create Root + Intermediate CA Chain Certificate"
echo "-----------------------------------"
cat ${cert_dir}/certs/${common_name}.ca.cert.pem \
${cert_dir}/certs/${ca_common_name}.chain.cert.pem > \
${cert_dir}/certs/${common_name}.chain.cert.pem
[ $? -eq 0 ] || exit $?
chmod 444 ${cert_dir}/certs/${common_name}.chain.cert.pem
[ $? -eq 0 ] || exit $?
echo "Root + Intermediate CA Chain Certificate Generated At:"
echo "------------------------------------------------------"
echo " ${cert_dir}/certs/${common_name}.chain.cert.pem"
warn_certs_not_for_production
}
###############################################################################
# Generate a Certificate for a device using specific openssl extension and
# signed with either the root or intermediate cert.
###############################################################################
function generate_device_certificate_common()
{
local common_name="${1}"
local device_prefix="${2}"
local ca_prefix="${3}"
local openssl_config_extension="${4}"
local cert_type_diagnostic="${5}"
local password_cmd=" -passin pass:${ca_password} "
if [ ! -f ${cert_dir}/private/${device_prefix}.key.pem ]; then
echo "Creating ${cert_type_diagnostic} Certificate"
echo "----------------------------------------"
openssl ${algorithm} \
-out ${cert_dir}/private/${device_prefix}.key.pem \
${key_bits_length}
[ $? -eq 0 ] || exit $?
chmod 444 ${cert_dir}/private/${device_prefix}.key.pem
[ $? -eq 0 ] || exit $?
fi
echo "Create the ${cert_type_diagnostic} Certificate Request"
echo "----------------------------------------"
openssl req -config ${openssl_config_file} \
-key ${cert_dir}/private/${device_prefix}.key.pem \
-subj "$(makeCNsubject "${common_name}")" \
-new -sha256 -out ${cert_dir}/csr/${device_prefix}.csr.pem
[ $? -eq 0 ] || exit $?
openssl ca -batch -config ${openssl_config_file} \
${password_cmd} \
-extensions "${openssl_config_extension}" \
-days ${days_till_expire} -notext -md sha256 \
-keyfile ${cert_dir}/private/${ca_prefix}.ca.key.pem \
-cert ${cert_dir}/certs/${ca_prefix}.ca.cert.pem \
-in ${cert_dir}/csr/${device_prefix}.csr.pem \
-out ${cert_dir}/certs/${device_prefix}.cert.pem
[ $? -eq 0 ] || exit $?
chmod 444 ${cert_dir}/certs/${device_prefix}.cert.pem
[ $? -eq 0 ] || exit $?
echo "Verify signature of the ${cert_type_diagnostic}" \
" certificate with the signer"
echo "-----------------------------------"
openssl verify \
-CAfile ${cert_dir}/certs/${ca_prefix}.chain.cert.pem \
${cert_dir}/certs/${device_prefix}.cert.pem
[ $? -eq 0 ] || exit $?
echo "${cert_type_diagnostic} Certificate Generated At:"
echo "----------------------------------------"
echo " ${cert_dir}/certs/${device_prefix}.cert.pem"
echo ""
openssl x509 -noout -text \
-in ${cert_dir}/certs/${device_prefix}.cert.pem
[ $? -eq 0 ] || exit $?
}
###############################################################################
# Generate a certificate for a leaf device
# signed with either the root or intermediate cert.
###############################################################################
function generate_leaf_certificate()
{
local common_name="${1}"
local device_prefix="${2}"
local ca_prefix="${3}"
generate_device_certificate_common "${common_name}" "${device_prefix}" \
"${ca_prefix}" \
"usr_cert" \
"Leaf Device"
}
###############################################################################
# Creates required directories and removes left over cert files.
# Run prior to creating Root CA; after that these files need to persist.
###############################################################################
function prepare_filesystem()
{
if [ ! -f ${openssl_config_file} ]; then
echo "Missing file ${openssl_config_file}"
exit 1
fi
cd ${cert_dir}
rm -rf csr
rm -rf private
rm -rf certs
rm -rf intermediateCerts
rm -rf newcerts
mkdir -p csr
mkdir -p private
mkdir -p certs
mkdir -p csr/device
mkdir -p private/device
mkdir -p certs/device
mkdir -p intermediateCerts
mkdir -p newcerts
rm -f index.txt
touch index.txt
rm -f serial
echo 01 > serial
}
###############################################################################
# Generates a certificate for verification, chained directly to the root.
###############################################################################
function generate_verification_certificate()
{
if [ $# -ne 2 ]; then
echo "Usage: <subjectName> <caSubjectName>"
exit 1
fi
rm -f ${cert_dir}/private/verification-code.key.pem
rm -f ${cert_dir}/certs/verification-code.cert.pem
generate_leaf_certificate "${1}" "verification-code" \
"${2}"
}
###############################################################################
# Generates a certificate for a device, chained to the intermediate.
###############################################################################
function generate_device_certificate()
{
if [ $# -ne 2 ]; then
echo "Usage: <subjectName> <caSubjectName>"
exit 1
fi
rm -f ${cert_dir}/private/device/$1.key.pem
rm -f ${cert_dir}/certs/device/$1.key.pem
rm -f ${cert_dir}/certs/device/$1-full-chain.cert.pem
generate_leaf_certificate "${1}" "device/${1}" \
"${2}"
}
###############################################################################
# Generates a certificate for Edge device, chained to the intermediate.
###############################################################################
function generate_edge_device_certificate()
{
if [ $# -ne 2 ]; then
echo "Usage: <subjectName> <caSubjectName>"
exit 1
fi
rm -f ${cert_dir}/private/device/$1.key.pem
rm -f ${cert_dir}/certs/device/$1.cert.pem
rm -f ${cert_dir}/certs/device/$1-full-chain.cert.pem
# Note: Appending a '.ca' to the common name is useful in situations
# where a user names their hostname as the edge device name.
# By doing so we avoid TLS validation errors where we have a server or
# client certificate where the hostname is used as the common name
# which essentially results in "loop" for validation purposes.
generate_device_certificate_common "${1}.ca" "device/${1}" \
${2} \
"v3_intermediate_ca" "Edge Device"
}
if [ "${1}" == "init" ]; then
prepare_filesystem
elif [ "${1}" == "create_root_certificate" ]; then
generate_root_ca "${2}"
elif [ "${1}" == "create_intermediate_certificate" ]; then
generate_intermediate_ca "${2}" "${3}"
elif [ "${1}" == "create_verification_certificate" ]; then
generate_verification_certificate "${2}" "${3}"
elif [ "${1}" == "create_device_certificate" ]; then
generate_device_certificate "${2}" "${3}"
elif [ "${1}" == "create_edge_device_certificate" ]; then
generate_edge_device_certificate "${2}" "${3}"
else
echo "Usage: init # Initializes directory structure"
echo " create_root_certificate <subjectName> # Creates new root certificate"
echo " create_intermediate_certificate <subjectName> <caSubjectName> # Creates new intermediate certificate issued by some CA"
echo " create_verification_certificate <subjectName> <caSubjectName> # Creates a verification certificate, signed with <subjectName> and issued by some CA"
echo " create_device_certificate <subjectName> <caSubjectName> # Creates a device certificate, signed with <subjectName> and issued by some CA"
echo " create_edge_device_certificate <subjectName> <caSubjectName> # Creates an edge device certificate, signed with <subjectName> and issued by some CA"
exit 1
fi
warn_certs_not_for_production