| 1 | #!/bin/bash |
| 2 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | # You may not use this file except in compliance with the License. |
| 6 | # A copy of the License is located at |
| 7 | # |
| 8 | # http://aws.amazon.com/apache2.0 |
| 9 | # |
| 10 | # or in the "license" file accompanying this file. This file is distributed |
| 11 | # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | # express or implied. See the License for the specific language governing |
| 13 | # permissions and limitations under the License. |
| 14 | |
| 15 | # immediately bail if any command fails |
| 16 | set -e |
| 17 | |
| 18 | # This script assumes the existence of |
| 19 | # ca_key.pem |
| 20 | # ca_cert.pem |
| 21 | # server_key.pem - the private key for early expiry cert being generated |
| 22 | # config/ca.cnf |
| 23 | # config/server_early_expiry.cnf |
| 24 | |
| 25 | echo "generating serial file" |
| 26 | touch config/serial |
| 27 | # the openssl ca command will read this file to figure out the serial number, |
| 28 | # increment it for each certificate that is signed. This is the serial number in |
| 29 | # hex notation. |
| 30 | echo "1024" > config/serial |
| 31 | |
| 32 | echo "generating server early expiry CSR" |
| 33 | openssl req -new -nodes -key server_key.pem -out server_early_expire.csr -config config/server_early_expire.cnf |
| 34 | |
| 35 | echo "creating empty CA database" |
| 36 | # it's mandatory for this to exist, although it is initially empty |
| 37 | # this data base will be updated during the ca command |
| 38 | touch certs_early_expire_index.txt |
| 39 | |
| 40 | echo "generating server certificate and signing it" |
| 41 | # this directory holds the duplicate certs that the CA command generates |
| 42 | # https://security.stackexchange.com/questions/111448/how-to-avoid-writing-pem-while-signing-a-csr |
| 43 | mkdir -p to_nuke |
| 44 | |
| 45 | # use the "batch" option to disable prompting |
| 46 | # the enddate argument is in YYYYMMDDHHMMSS format |
| 47 | # use the "notext" option because s2n can't parse .pem certificates with the |
| 48 | # text information inside of them |
| 49 | openssl ca -batch \ |
| 50 | -in server_early_expire.csr \ |
| 51 | -out server_cert_early_expire.pem \ |
| 52 | -config config/ca.cnf \ |
| 53 | -outdir to_nuke \ |
| 54 | -enddate 20370101010101Z \ |
| 55 | -extfile config/server_early_expire.cnf \ |
| 56 | -extensions req_ext \ |
| 57 | -notext |
| 58 | |
| 59 | echo "verifying generated certificates" |
| 60 | openssl verify -CAfile ca_cert.pem server_cert_early_expire.pem |
| 61 | |
| 62 | echo "generating ocsp response" |
| 63 | # This is the target next_update date for the OCSP response. This date needs to |
| 64 | # be before 2038 so that next_update field can be tested on 32 bit platforms. |
| 65 | target_date="2036-01-01" |
| 66 | current_date=$(date +%Y-%m-%d) |
| 67 | expiration_days=$(( ($(date -d "$target_date" +%s) - $(date -d "$current_date" +%s)) / 86400 )) |
| 68 | |
| 69 | # The & launches the server in the background |
| 70 | openssl ocsp -port 8889 -text -CA ca_cert.pem \ |
| 71 | -index certs_early_expire_index.txt \ |
| 72 | -rkey ocsp_key.pem \ |
| 73 | -rsigner ocsp_cert.pem \ |
| 74 | -nrequest 1 \ |
| 75 | -ndays $expiration_days \ |
| 76 | & |
| 77 | |
| 78 | openssl ocsp -CAfile ca_cert.pem \ |
| 79 | -url http://127.0.0.1:8889 \ |
| 80 | -issuer ca_cert.pem \ |
| 81 | -verify_other ocsp_cert.pem \ |
| 82 | -cert server_cert_early_expire.pem -respout ocsp_response_early_expire.der |
| 83 | |
| 84 | echo "removing temporary files" |
| 85 | rm server_early_expire.csr |
| 86 | # openssl generates <file>, <file>.attr, <file>.old, and <file>.attr.old |
| 87 | # delete all of them |
| 88 | rm certs_early_expire_index* -f |
| 89 | rm to_nuke -rf |
| 90 | rm config/serial |
| 91 | rm config/serial.old |
| 92 |