| 1 | #!/usr/bin/env bash |
| 2 | # stop script on error |
| 3 | set -e |
| 4 | |
| 5 | # The files generated by this script are used in unit tests that create TLS |
| 6 | # connections between a localhost server and client. Run this script if any |
| 7 | # certificates used for tests are expired. |
| 8 | |
| 9 | # Files generated by this script: |
| 10 | # ca_root.crt: root certificate authority |
| 11 | # server.crt: certificate for server signed by ca_root.crt |
| 12 | # server.key: private key for server.crt |
| 13 | # server_chain.crt: certificate chain containing server.crt and ca_root.crt |
| 14 | # unittests.crt: self-signed certificate |
| 15 | # unittests.p8: private key, pkcs#8 syntax |
| 16 | # unittests.p12: pkcs#12 file bundling the certificate and private key. Password is "1234" |
| 17 | # ec_unittests.crt: elliptic curve self-signed certificate |
| 18 | # ec_unittests.p8: elliptic curve private key, pkcs#8 syntax |
| 19 | # ec_unittests.p12: elliptic curve pkcs#12 file bundling the certificate and private key. Password is "1234" |
| 20 | # mtls_server_root_ca.pem.crt: self-signed root CA for mTLS server |
| 21 | # mtls_server_root_ca.key: private key for mtls_server_root_ca |
| 22 | # mtls_device_root_ca.pem.crt: self-signed root CA for mTLS device |
| 23 | # mtls_device_root_ca.key: private key for mtls_device_root_ca |
| 24 | # mtls_server.pem.crt: server certificate signed by mtls_server_root_ca |
| 25 | # mtls_server.key: private key for mtls_server |
| 26 | # mtls_device.pem.crt: device certificate signed by mtls_device_root_ca |
| 27 | # mtls_device.key: private key for mtls_device |
| 28 | # mtls_untrusted_server_root_ca.pem.crt: self-signed root CA for untrusted mTLS server |
| 29 | # mtls_untrusted_server_root_ca.key: private key for mtls_untrusted_server_root_ca |
| 30 | # mtls_untrusted_server.pem.crt: server certificate signed by mtls_untrusted_server_root_ca |
| 31 | # mtls_untrusted_server.key: private key for mtls_untrusted_server |
| 32 | # mtls_server_root_ca_trust_settings.plist: macOS trust settings plist for mtls_server_root_ca |
| 33 | |
| 34 | # Create directory for use with certificate generation |
| 35 | mkdir -p certGeneration |
| 36 | # Copy files needed to generate new certificates |
| 37 | cp unittests.key certGeneration/unittests.key |
| 38 | cp ec_unittests.key certGeneration/ec_unittests.key |
| 39 | cp unittests.conf certGeneration/unittests.conf |
| 40 | cp ca_root.cnf certGeneration/ca_root.cnf |
| 41 | cd certGeneration |
| 42 | |
| 43 | # index.txt and serial are required for use with openssl config files |
| 44 | touch index.txt |
| 45 | echo 1000 > serial |
| 46 | |
| 47 | # Generating a key for the new ca_root |
| 48 | openssl genrsa -out ca_root.key 2048 |
| 49 | |
| 50 | # Generate the ca_root certificate |
| 51 | openssl req -config ca_root.cnf \ |
| 52 | -key ca_root.key \ |
| 53 | -new -x509 -days 824 -sha256 -extensions v3_ca \ |
| 54 | -out ca_root.crt \ |
| 55 | -set_serial 00 \ |
| 56 | -subj '/C=US/ST=Washington/L=Seattle/O=Amazon/OU=SDKs/CN=localhostCA/[email protected]' |
| 57 | |
| 58 | # Generate a private key for the server |
| 59 | openssl genrsa -out server.key 2048 |
| 60 | |
| 61 | # Generate a certificate signing request for the server |
| 62 | openssl req -new -sha256 \ |
| 63 | -key server.key \ |
| 64 | -out server.csr \ |
| 65 | -set_serial 02 \ |
| 66 | -subj '/C=US/ST=Washington/L=Seattle/O=Amazon/OU=SDKs/CN=localhost/[email protected]' |
| 67 | |
| 68 | # Sign the server signing request with ca_root |
| 69 | yes | openssl ca -config ca_root.cnf \ |
| 70 | -extensions server_cert \ |
| 71 | -days 824 -notext -md sha256 \ |
| 72 | -in server.csr \ |
| 73 | -out server.crt |
| 74 | |
| 75 | # Generate a certificate chain containing the ca_root and server certificates |
| 76 | cat server.crt ca_root.crt > server_chain.crt |
| 77 | |
| 78 | # Generate other unittest certificate variations |
| 79 | for base in unittests ec_unittests; do |
| 80 | openssl req -x509 -new \ |
| 81 | -key $base.key \ |
| 82 | -config unittests.conf \ |
| 83 | -out $base.crt \ |
| 84 | -days 824 |
| 85 | |
| 86 | openssl pkcs8 -topk8 \ |
| 87 | -out $base.p8 \ |
| 88 | -in $base.key \ |
| 89 | -nocrypt |
| 90 | |
| 91 | openssl pkcs12 -export \ |
| 92 | -out $base.p12 \ |
| 93 | -inkey $base.key \ |
| 94 | -in $base.crt \ |
| 95 | -password pass:1234 |
| 96 | done |
| 97 | |
| 98 | # generate_trust_plist <cert_pem> <output_plist> |
| 99 | # Generates a macOS trust settings plist that marks the given certificate as trusted (trustRoot). |
| 100 | # The alternative is to use the 'security add-trusted-cert <cert>' command but it uses a prompt, which I couldn't get |
| 101 | # around in CI. |
| 102 | generate_trust_plist() { |
| 103 | local CERT="$1" |
| 104 | local PLIST="$2" |
| 105 | local DER_FILE |
| 106 | DER_FILE=$(mktemp) |
| 107 | openssl x509 -in "$CERT" -outform DER -out "$DER_FILE" |
| 108 | |
| 109 | local FP |
| 110 | FP=$(openssl x509 -in "$CERT" -noout -fingerprint -sha1 | sed 's/.*=//;s/://g') |
| 111 | |
| 112 | # Extract issuer DER bytes (2nd SEQUENCE at d=2 in the certificate ASN.1 structure). |
| 113 | local ISSUER_INFO ISSUER_OFFSET ISSUER_HL ISSUER_L ISSUER_B64 |
| 114 | ISSUER_INFO=$(openssl asn1parse -inform DER -in "$DER_FILE" | awk '/d=2.*cons:.*SEQUENCE/{n++; if(n==2){print; exit}}') |
| 115 | ISSUER_OFFSET=$(echo "$ISSUER_INFO" | awk -F: '{gsub(/ /,"",$1); print $1}') |
| 116 | ISSUER_HL=$(echo "$ISSUER_INFO" | sed 's/.*hl=\([0-9]*\).*/\1/') |
| 117 | ISSUER_L=$(echo "$ISSUER_INFO" | sed 's/.*l= *\([0-9]*\).*/\1/') |
| 118 | ISSUER_B64=$(dd if="$DER_FILE" bs=1 skip="$ISSUER_OFFSET" count=$((ISSUER_HL + ISSUER_L)) 2>/dev/null | base64) |
| 119 | |
| 120 | # Extract serial number bytes (1st INTEGER at d=2). |
| 121 | local SERIAL_INFO SERIAL_OFFSET SERIAL_HL SERIAL_L SERIAL_B64 |
| 122 | SERIAL_INFO=$(openssl asn1parse -inform DER -in "$DER_FILE" | awk '/d=2.*prim:.*INTEGER/{print; exit}') |
| 123 | SERIAL_OFFSET=$(echo "$SERIAL_INFO" | awk -F: '{gsub(/ /,"",$1); print $1}') |
| 124 | SERIAL_HL=$(echo "$SERIAL_INFO" | sed 's/.*hl=\([0-9]*\).*/\1/') |
| 125 | SERIAL_L=$(echo "$SERIAL_INFO" | sed 's/.*l= *\([0-9]*\).*/\1/') |
| 126 | SERIAL_B64=$(dd if="$DER_FILE" bs=1 skip=$((SERIAL_OFFSET + SERIAL_HL)) count="$SERIAL_L" 2>/dev/null | base64) |
| 127 | |
| 128 | local MOD_DATE |
| 129 | MOD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ") |
| 130 | |
| 131 | cat > "$PLIST" << EOF |
| 132 | <?xml version="1.0" encoding="UTF-8"?> |
| 133 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 134 | <plist version="1.0"> |
| 135 | <dict> |
| 136 | <key>trustList</key> |
| 137 | <dict> |
| 138 | <key>${FP}</key> |
| 139 | <dict> |
| 140 | <key>issuerName</key> |
| 141 | <data> |
| 142 | ${ISSUER_B64} |
| 143 | </data> |
| 144 | <key>modDate</key> |
| 145 | <date>${MOD_DATE}</date> |
| 146 | <key>serialNumber</key> |
| 147 | <data> |
| 148 | ${SERIAL_B64} |
| 149 | </data> |
| 150 | <key>trustSettings</key> |
| 151 | <array> |
| 152 | <dict> |
| 153 | <key>kSecTrustSettingsResult</key> |
| 154 | <integer>1</integer> |
| 155 | </dict> |
| 156 | </array> |
| 157 | </dict> |
| 158 | </dict> |
| 159 | <key>trustVersion</key> |
| 160 | <integer>1</integer> |
| 161 | </dict> |
| 162 | </plist> |
| 163 | EOF |
| 164 | |
| 165 | rm "$DER_FILE" |
| 166 | } |
| 167 | |
| 168 | # Generate mTLS keys and certs |
| 169 | SUBJ='/C=US/ST=Washington/L=Seattle/O=Amazon/OU=SDKs/CN=localhost/[email protected]' |
| 170 | CA_SUBJ='/C=US/ST=Washington/L=Seattle/O=Amazon/OU=SDKs/CN=localhostCA/[email protected]' |
| 171 | |
| 172 | # Generate mtls_server_root_ca key and self-signed cert |
| 173 | openssl genrsa -out mtls_server_root_ca.key 2048 |
| 174 | openssl req -new -x509 -days 824 -sha256 \ |
| 175 | -key mtls_server_root_ca.key -subj "$CA_SUBJ" -out mtls_server_root_ca.pem.crt |
| 176 | |
| 177 | # Generate mtls_device_root_ca key and self-signed cert |
| 178 | openssl genrsa -out mtls_device_root_ca.key 2048 |
| 179 | openssl req -new -x509 -days 824 -sha256 \ |
| 180 | -key mtls_device_root_ca.key -subj "$CA_SUBJ" -out mtls_device_root_ca.pem.crt |
| 181 | |
| 182 | # Generate mtls_server key and cert (signed by mtls_server_root_ca) |
| 183 | openssl genrsa -out mtls_server.key 2048 |
| 184 | openssl req -new -key mtls_server.key -subj "$SUBJ" | \ |
| 185 | openssl x509 -req -CA mtls_server_root_ca.pem.crt -CAkey mtls_server_root_ca.key \ |
| 186 | -CAcreateserial -days 824 -sha256 -out mtls_server.pem.crt |
| 187 | |
| 188 | # Generate mtls_device key and cert (signed by mtls_device_root_ca) |
| 189 | openssl genrsa -out mtls_device.key 2048 |
| 190 | openssl req -new -key mtls_device.key -subj "$SUBJ" | \ |
| 191 | openssl x509 -req -CA mtls_device_root_ca.pem.crt -CAkey mtls_device_root_ca.key \ |
| 192 | -CAcreateserial -days 824 -sha256 -out mtls_device.pem.crt |
| 193 | |
| 194 | # Generate mtls_untrusted_server_root_ca key and self-signed cert |
| 195 | openssl genrsa -out mtls_untrusted_server_root_ca.key 2048 |
| 196 | openssl req -new -x509 -days 824 -sha256 \ |
| 197 | -key mtls_untrusted_server_root_ca.key -subj "$CA_SUBJ" -out mtls_untrusted_server_root_ca.pem.crt |
| 198 | |
| 199 | # Generate mtls_untrusted_server key and cert (signed by mtls_untrusted_server_root_ca) |
| 200 | openssl genrsa -out mtls_untrusted_server.key 2048 |
| 201 | openssl req -new -key mtls_untrusted_server.key -subj "$SUBJ" | \ |
| 202 | openssl x509 -req -CA mtls_untrusted_server_root_ca.pem.crt -CAkey mtls_untrusted_server_root_ca.key \ |
| 203 | -CAcreateserial -days 824 -sha256 -out mtls_untrusted_server.pem.crt |
| 204 | |
| 205 | # Generate trust settings plist for mtls_server_root_ca |
| 206 | generate_trust_plist mtls_server_root_ca.pem.crt mtls_server_root_ca_trust_settings.plist |
| 207 | |
| 208 | # Copy the generated certificates and keys to the resources folder |
| 209 | cd .. |
| 210 | cp certGeneration/ca_root.crt ./ca_root.crt |
| 211 | cp certGeneration/server.crt ./server.crt |
| 212 | cp certGeneration/server.key ./server.key |
| 213 | cp certGeneration/server_chain.crt ./server_chain.crt |
| 214 | cp certGeneration/server.crt ./server.crt |
| 215 | |
| 216 | for base in unittests ec_unittests; do |
| 217 | cp certGeneration/$base.crt ./$base.crt |
| 218 | cp certGeneration/$base.p8 ./$base.p8 |
| 219 | cp certGeneration/$base.p12 ./$base.p12 |
| 220 | done |
| 221 | |
| 222 | for base in mtls_server_root_ca mtls_device_root_ca mtls_untrusted_server_root_ca; do |
| 223 | cp certGeneration/$base.pem.crt ./$base.pem.crt |
| 224 | cp certGeneration/$base.key ./$base.key |
| 225 | done |
| 226 | |
| 227 | for base in mtls_server mtls_device mtls_untrusted_server; do |
| 228 | cp certGeneration/$base.pem.crt ./$base.pem.crt |
| 229 | cp certGeneration/$base.key ./$base.key |
| 230 | done |
| 231 | |
| 232 | cp certGeneration/mtls_server_root_ca_trust_settings.plist ./mtls_server_root_ca_trust_settings.plist |
| 233 | |
| 234 | # Clean up the certGeneration folder |
| 235 | rm -r certGeneration |