|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -xeuo pipefail |
| 4 | + |
| 5 | +function prepare() { |
| 6 | + local cwd=$1 |
| 7 | + local fwd=$(readlink -f "$cwd") |
| 8 | + mkdir -p "$cwd"/{certs,crl,newcerts,private} |
| 9 | + echo 1000 > "$cwd/serial" |
| 10 | + touch "$cwd"/{index.txt,index.txt.attr} |
| 11 | + |
| 12 | + echo ' |
| 13 | + [ ca ] |
| 14 | + default_ca = CA_default |
| 15 | + [ CA_default ] |
| 16 | + dir = '"$fwd"' |
| 17 | + certs = $dir/certs # Where the issued certs are kept |
| 18 | + crl_dir = $dir/crl # Where the issued crl are kept |
| 19 | + database = $dir/index.txt # database index file. |
| 20 | + new_certs_dir = $dir/newcerts # default place for new certs. |
| 21 | + certificate = $dir/cacert.pem # The CA certificate |
| 22 | + serial = $dir/serial # The current serial number |
| 23 | + crl = $dir/crl.pem # The current CRL |
| 24 | + private_key = $dir/private/ca.key.pem # The private key |
| 25 | + RANDFILE = $dir/.rnd # private random number file |
| 26 | + nameopt = default_ca |
| 27 | + certopt = default_ca |
| 28 | + policy = policy_match |
| 29 | + default_days = 365 |
| 30 | + default_md = sha256 |
| 31 | +
|
| 32 | + [ policy_match ] |
| 33 | + countryName = optional |
| 34 | + stateOrProvinceName = optional |
| 35 | + organizationName = optional |
| 36 | + organizationalUnitName = optional |
| 37 | + commonName = supplied |
| 38 | + emailAddress = optional |
| 39 | +
|
| 40 | + [req] |
| 41 | + req_extensions = v3_req |
| 42 | + distinguished_name = req_distinguished_name |
| 43 | +
|
| 44 | + [req_distinguished_name] |
| 45 | +
|
| 46 | + [v3_req]' > "$cwd/openssl.cnf" |
| 47 | + |
| 48 | + if [[ $cwd == out ]] ; then |
| 49 | + echo "keyUsage = digitalSignature, keyEncipherment" >> "$cwd/openssl.cnf" |
| 50 | + echo "extendedKeyUsage = serverAuth, clientAuth" >> "$cwd/openssl.cnf" |
| 51 | + echo "subjectAltName = DNS:localhost" >> "$cwd/openssl.cnf" |
| 52 | + else |
| 53 | + echo "basicConstraints = CA:TRUE" >> "$cwd/openssl.cnf" |
| 54 | + fi |
| 55 | +} |
| 56 | + |
| 57 | +# chain generates three certificates in a chain. |
| 58 | +function chain() { |
| 59 | + rm {root,intermediate,out} -rf |
| 60 | + prepare root |
| 61 | + prepare intermediate |
| 62 | + prepare out |
| 63 | + |
| 64 | + # Create root certificate and key. |
| 65 | + openssl genrsa -out root/private/ca.key 2048 |
| 66 | + openssl req -new -x509 -sha256 -days 3650 \ |
| 67 | + -config root/openssl.cnf -extensions v3_req \ |
| 68 | + -key root/private/ca.key --out root/certs/ca.crt \ |
| 69 | + -subj '/CN=TEST-root' |
| 70 | + |
| 71 | + # Create intermediate key and request. |
| 72 | + openssl genrsa -out intermediate/private/intermediate.key 2048 |
| 73 | + openssl req -new -sha256 \ |
| 74 | + -config intermediate/openssl.cnf -extensions v3_req \ |
| 75 | + -key intermediate/private/intermediate.key -out intermediate/certs/intermediate.csr \ |
| 76 | + -subj '/CN=TEST-intermediate' |
| 77 | + |
| 78 | + # Sign intermediate request with root to create a cert. |
| 79 | + openssl ca -batch -notext -md sha256 \ |
| 80 | + -config intermediate/openssl.cnf -extensions v3_req \ |
| 81 | + -keyfile root/private/ca.key -cert root/certs/ca.crt \ |
| 82 | + -in intermediate/certs/intermediate.csr \ |
| 83 | + -out intermediate/certs/intermediate.crt |
| 84 | + |
| 85 | + # Create a key and request for an end certificate. |
| 86 | + openssl req -new -days 365 -nodes -newkey rsa:2048 \ |
| 87 | + -config out/openssl.cnf -extensions v3_req \ |
| 88 | + -keyout out/private/localhost.key -out out/certs/localhost.csr \ |
| 89 | + -subj "/CN=localhost" |
| 90 | + |
| 91 | + # Sign that with the intermediate. |
| 92 | + openssl ca -batch \ |
| 93 | + -config out/openssl.cnf -extensions v3_req \ |
| 94 | + -keyfile intermediate/private/intermediate.key -cert intermediate/certs/intermediate.crt \ |
| 95 | + -out out/certs/localhost.crt \ |
| 96 | + -infiles out/certs/localhost.csr |
| 97 | + |
| 98 | + mv out/certs/localhost.crt chain-leaf.crt |
| 99 | + mv out/private/localhost.key chain-leaf.key |
| 100 | + mv intermediate/certs/intermediate.crt chain-intermediate.crt |
| 101 | + mv intermediate/private/intermediate.key chain-intermediate.key |
| 102 | + mv root/certs/ca.crt chain-root.crt |
| 103 | + mv root/private/ca.key chain-root.key |
| 104 | + |
| 105 | + rm {out,intermediate,root} -r |
| 106 | + |
| 107 | + cat chain-leaf.crt chain-intermediate.crt chain-root.crt > chain.crt |
| 108 | + cp chain-leaf.key chain.key |
| 109 | +} |
| 110 | + |
| 111 | +# non-signing generates a self-signed certificate that has cert signing |
| 112 | +# explicitly omitted. |
| 113 | +function non-signing() { |
| 114 | + openssl req -x509 -nodes -newkey rsa:2048 \ |
| 115 | + -keyout no-signing.key -out no-signing.crt \ |
| 116 | + -addext "keyUsage = digitalSignature, keyEncipherment" \ |
| 117 | + -addext "subjectAltName=DNS:localhost" \ |
| 118 | + -subj "/CN=localhost" |
| 119 | +} |
| 120 | + |
| 121 | +# self-signed generates a certificate without specifying key usage. |
| 122 | +function self-signed() { |
| 123 | + openssl req -x509 -nodes -newkey rsa:2048 \ |
| 124 | + -keyout self-signed.key -out self-signed.crt \ |
| 125 | + -addext "subjectAltName=DNS:localhost" \ |
| 126 | + -subj "/CN=localhost" |
| 127 | +} |
| 128 | + |
| 129 | +function main() { |
| 130 | + local name=$1 ; shift |
| 131 | + "$name" "$@" |
| 132 | +} |
| 133 | + |
| 134 | +main "$@" |
0 commit comments