File: generate.sh

package info (click to toggle)
aws-crt-python 0.16.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 78,328 kB
  • sloc: ansic: 330,743; python: 18,949; makefile: 6,271; sh: 3,712; asm: 754; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (286 lines) | stat: -rwxr-xr-x 8,052 bytes parent folder | download | duplicates (3)
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
#!/bin/bash
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
#  http://aws.amazon.com/apache2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
#

install_dir="$(pwd)"
openssl_conf_path="$(pwd)/openssl.conf"

get_subj() {
  common_name="$1"
  echo "/C=US/ST=Massachusetts/L=Boston/O=Amazon/OU=s2n/CN=${common_name}"
}

init_ca_dir() {
  ca_dir="$1"
  mkdir -p "${ca_dir}"
  touch "${ca_dir}/index.txt"
  echo 01 > "${ca_dir}/serial"
  echo 01 > "${ca_dir}/crlnumber"
}

generate_key_cert() {
  path="$1"
  type="$2"

  common_name=$(basename "${path}")
  out="csr.pem"
  opts=()

  case "${type}" in
    ca)
      opts=("-x509" "-days" "36500" "-extensions" "v3_ca")
      out="cert.pem"
      ;;
    intermediate)
      ;;
    leaf)
      # Common name must be localhost for s2n-tls default verify host callback
      common_name="localhost"
      ;;
    *)
      echo "invalid type"
      exit
  esac

  pushd "${path}" || exit

  openssl genrsa -out key.pem 4096 || exit
  chmod 400 key.pem

  ca_dir="." \
  openssl req -config "${openssl_conf_path}" \
      -key key.pem \
      -new -sha256 \
      "${opts[@]}" \
      -out "${out}" \
      -subj "$(get_subj ${common_name})" \
      || exit

  # Create a chain with just the root certificate for validating child certificates
  if [ "${type}" = "ca" ]; then
    cp cert.pem chain.pem
  fi

  popd || exit
}

verify_cert() {
  ca_file_path="$1"
  cert_file_path="$2"
  crl_file_path="$3"

  opts=()
  if [ -n "${crl_file_path}" ]; then
      is_revoked=$4
      if [ -z "${is_revoked}" ]; then
        exit
      fi

      opts=("-CRLfile" "${crl_file_path}" "-crl_check")
  fi

  verify_stderr=$(
    openssl verify \
        -CAfile "${ca_file_path}" \
        "${opts[@]}" \
        "${cert_file_path}" \
        2>&1
  )
  verify_ret=$?

  if [ -n "${crl_file_path}" ] && [ ${is_revoked} -eq 1 ]; then
    # Ensure openssl verify failed due to certificate revocation
    if ! [[ "${verify_stderr}" =~ .*"certificate revoked".* ]]; then
      exit
    fi
  else
    # Ensure openssl verify succeeded
    if [ ${verify_ret} -ne 0 ]; then
      exit
    fi
  fi
}

sign_cert() {
  signer_path="$1"
  to_sign_path="$2"
  signer_type="$3"

  opts=()
  case "${signer_type}" in
    ca)
      opts=("-extensions" "v3_intermediate_ca")
      ;;
    intermediate)
      opts=("-extensions" "usr_cert")
      ;;
    *)
      echo "invalid type"
      exit
  esac

  ca_dir="${signer_path}" \
  openssl ca -config "${openssl_conf_path}" -batch \
      -days 36500 -notext -md sha256 \
      "${opts[@]}" \
      -in "${to_sign_path}/csr.pem" \
      -out "${to_sign_path}/cert.pem" \
      || exit

  # Ensure openssl can validate the signed certificate
  verify_cert "${signer_path}/chain.pem" "${to_sign_path}/cert.pem"

  # Create a chain that includes the newly signed certificate and all parent certificates
  # for validating future child certificates
  if [ "${signer_type}" = "ca" ]; then
    cat "${to_sign_path}/cert.pem" "${signer_path}/chain.pem" > "${to_sign_path}/chain.pem"
  fi
}

generate_crl() {
  path="$1"

  pushd "${path}" || exit
  ca_dir="." \
  openssl ca -config "${openssl_conf_path}" -gencrl \
    -out crl.pem \
      || exit
  popd || exit
}

revoke_cert() {
  ca_path="$1"
  cert_path="$2"

  ca_dir="${ca_path}" \
  openssl ca -config "${openssl_conf_path}" \
      -revoke "${cert_path}/cert.pem" \
      || exit
  generate_crl "${ca_path}"
}

base_dir=$(mktemp -d)
pushd "${base_dir}" || exit

#
# Generate certificate chains of valid and revoked certificates with 1 intermediate CA.
#
# root -> valid intermediate   -> valid leaf
#                              -> revoked leaf
#      -> revoked intermediate -> valid leaf
#                              -> revoked leaf
#

# Generate root certificate
init_ca_dir root
generate_key_cert root ca

# Generate intermediate certificates
init_ca_dir root/intermediate
generate_key_cert root/intermediate intermediate
sign_cert root root/intermediate ca

init_ca_dir root/intermediate_revoked
generate_key_cert root/intermediate_revoked intermediate
sign_cert root root/intermediate_revoked ca

# Generate leaf certificates
mkdir root/intermediate/leaf
generate_key_cert root/intermediate/leaf leaf
sign_cert root/intermediate root/intermediate/leaf intermediate

mkdir root/intermediate/leaf_revoked
generate_key_cert root/intermediate/leaf_revoked leaf
sign_cert root/intermediate root/intermediate/leaf_revoked intermediate

mkdir root/intermediate_revoked/leaf
generate_key_cert root/intermediate_revoked/leaf leaf
sign_cert root/intermediate_revoked root/intermediate_revoked/leaf intermediate

mkdir root/intermediate_revoked/leaf_revoked
generate_key_cert root/intermediate_revoked/leaf_revoked leaf
sign_cert root/intermediate_revoked root/intermediate_revoked/leaf_revoked intermediate

# Generate CRLs and revoke the revoked certificates
generate_crl root
revoke_cert root root/intermediate_revoked

generate_crl root/intermediate
revoke_cert root/intermediate root/intermediate/leaf_revoked

generate_crl root/intermediate_revoked
revoke_cert root/intermediate_revoked root/intermediate/leaf_revoked

# Ensure openssl verify reads generated CRLs and properly rejects certificates
verify_cert root/chain.pem root/intermediate/cert.pem root/crl.pem 0
verify_cert root/chain.pem root/intermediate_revoked/cert.pem root/crl.pem 1

verify_cert root/intermediate/chain.pem root/intermediate/leaf/cert.pem root/intermediate/crl.pem 0
verify_cert root/intermediate/chain.pem root/intermediate/leaf_revoked/cert.pem root/intermediate/crl.pem 1

verify_cert root/intermediate_revoked/chain.pem root/intermediate_revoked/leaf/cert.pem root/intermediate_revoked/crl.pem 0
verify_cert root/intermediate_revoked/chain.pem root/intermediate_revoked/leaf_revoked/cert.pem root/intermediate_revoked/crl.pem 1

# Generate CRLs with invalid timestamps. Requires Openssl 3.0.
ca_dir="root/intermediate" \
openssl ca -config "${openssl_conf_path}" -gencrl \
    -crl_lastupdate 21220101000000Z \
    -out "${install_dir}/intermediate_invalid_this_update_crl.pem" \
    || exit

ca_dir="root/intermediate" \
openssl ca -config "${openssl_conf_path}" -gencrl \
    -crl_nextupdate 500101000000Z \
    -out "${install_dir}/intermediate_invalid_next_update_crl.pem" \
    || exit

#
# Create certificate chain pems and copy CRLs and keys
#

cp root/cert.pem "${install_dir}/root_cert.pem"

cat root/intermediate/leaf/cert.pem \
    root/intermediate/cert.pem \
    > "${install_dir}/none_revoked_cert_chain.pem"
cp root/intermediate/leaf/key.pem "${install_dir}/none_revoked_key.pem"

cat root/intermediate/leaf_revoked/cert.pem \
    root/intermediate/cert.pem \
    > "${install_dir}/leaf_revoked_cert_chain.pem"
cp root/intermediate/leaf_revoked/key.pem "${install_dir}/leaf_revoked_key.pem"

cat root/intermediate_revoked/leaf/cert.pem \
    root/intermediate_revoked/cert.pem \
    > "${install_dir}/intermediate_revoked_cert_chain.pem"
cp root/intermediate_revoked/leaf/key.pem "${install_dir}/intermediate_revoked_key.pem"

cat root/intermediate_revoked/leaf_revoked/cert.pem \
    root/intermediate_revoked/cert.pem \
    > "${install_dir}/all_revoked_cert_chain.pem"
cp root/intermediate_revoked/leaf_revoked/key.pem "${install_dir}/all_revoked_key.pem"

cp root/crl.pem "${install_dir}/root_crl.pem"
cp root/intermediate/crl.pem "${install_dir}/intermediate_crl.pem"
cp root/intermediate_revoked/crl.pem "${install_dir}/intermediate_revoked_crl.pem"

popd || exit

# Cleanup
rm -rf "${base_dir}"

# Ensure that s2nc accepts all generated certificates
./test_s2nc.sh || exit

echo "Generation successful."