-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_crypto.sh
148 lines (130 loc) · 3.44 KB
/
generate_crypto.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
###################################
# This phase creates all the certs
# and keys we'll use in a later
# phase, for testing the ca_app
# library.
###################################
##############
# Set env vars
##############
# Domains
LOCAL_DOMAIN="example.net"
# Base paths
CRYPTO_EXPORT_PATH="${HOME}/export"
CRYPTO_DIR="${HOME}/crypto"
LOCAL_CA_DIR="${CRYPTO_DIR}/local_ca"
LOCAL_DEV_DIR="${CRYPTO_DIR}/local_dev"
# General device info
DEVICE_SERIAL="abc123"
DEVICE_MODEL="air-quality-sensor"
# Building DIDN-IDs for devices
L_DIDN_ID="${DEVICE_SERIAL}.${DEVICE_MODEL}._device.${LOCAL_DOMAIN}"
# Specific file paths
## Local CA
LOCAL_CA_KEY="${LOCAL_CA_DIR}/ca.example.net.key.pem"
LOCAL_CA_CERT="${LOCAL_CA_DIR}/ca.example.net.cert.pem"
## Local Dev
LOCAL_DEV_KEY="${LOCAL_DEV_DIR}/${L_DIDN_ID}.key.pem"
LOCAL_DEV_CSR="${LOCAL_DEV_DIR}/${L_DIDN_ID}.csr.pem"
LOCAL_DEV_CERT="${LOCAL_DEV_DIR}/${L_DIDN_ID}.cert.pem"
##############
# Install OpenSSL
##############
apt-get update && \
apt-get install -y \
openssl \
tree
##############
# Fix OpenSSL
# config for
# CA signing
##############
# RUN cat /etc/ssl/openssl.cnf | \
# sed \
# -e 's/^\[ usr_cert \]/[ usr_cert ]\n\nsubjectAltName = DNS:copy/g' \
# -e 's/^\[ v3_req \]/[ v3_req ]\n\nsubjectAltName = DNS:copy/g' \
# | tee /usr/lib/ssl/openssl.cnf
cat /usr/lib/ssl/openssl.cnf
##############
# Create dirs
##############
mkdir -p \
${CRYPTO_EXPORT_PATH} \
${CRYPTO_DIR} \
${LOCAL_CA_DIR} \
${LOCAL_DEV_DIR} \
${LOCAL_CA_DIR}/demoCA/
##############
# Drop dev
# conf files
##############
cp /usr/lib/ssl/openssl.cnf ${LOCAL_CA_DIR}/san.cnf
echo "[ SAN ]\nsubjectAltName = DNS:${L_DIDN_ID}" >> ${LOCAL_CA_DIR}/san.cnf
##############
# Create local
# CA
##############
cd ${LOCAL_CA_DIR}
openssl genrsa \
-out ${LOCAL_CA_KEY} \
4096
openssl req \
-key ${LOCAL_CA_KEY} \
-new \
-x509 \
-days 7300 \
-sha256 \
-subj "/C=US/ST=CA/O=Example Networks/CN=Example Networks CA" \
-out ${LOCAL_CA_CERT}
openssl x509 -noout -text -in ${LOCAL_CA_CERT}
touch ${LOCAL_CA_DIR}/demoCA/index.txt
touch ${LOCAL_CA_DIR}/demoCA/index.txt.attr
##############
# Create local
# device
##############
cd ${LOCAL_CA_DIR}
openssl genrsa \
-out ${LOCAL_DEV_KEY} \
2048
openssl req \
-key ${LOCAL_DEV_KEY} \
-new \
-sha256 \
-subj "/C=US/ST=CA/O=Example Networks/CN=${L_DIDN_ID}" \
-addext "subjectAltName = DNS:${L_DIDN_ID}" \
-out ${LOCAL_DEV_CSR}
echo "#################### LOCAL DEV CSR ####################"
openssl req -noout -text -in ${LOCAL_DEV_CSR}
openssl ca \
-extensions usr_cert \
-extensions v3_req \
-extensions SAN \
-days 375 \
-notext \
-md sha256 \
-keyfile ${LOCAL_CA_KEY} \
-cert ${LOCAL_CA_CERT} \
-outdir ${LOCAL_DEV_DIR} \
-create_serial \
-extfile ${LOCAL_CA_DIR}/san.cnf \
-batch \
-in ${LOCAL_DEV_CSR} \
-out ${LOCAL_DEV_CERT}
echo "#################### LOCAL DEV CERTIFICATE ####################"
openssl x509 -noout -text -in ${LOCAL_DEV_CERT}
##############
# Copy files
# for export
##############
cp ${LOCAL_CA_KEY} ${CRYPTO_EXPORT_PATH}
cp ${LOCAL_CA_CERT} ${CRYPTO_EXPORT_PATH}
cp ${LOCAL_DEV_KEY} ${CRYPTO_EXPORT_PATH}
cp ${LOCAL_DEV_CSR} ${CRYPTO_EXPORT_PATH}
cp ${LOCAL_DEV_CERT} ${CRYPTO_EXPORT_PATH}
##############
# Print results
##############
echo "######## RESULTING FILES ##########"
ls -lah ${CRYPTO_EXPORT_PATH}
echo "Crypto builder phase complete!"