| 1 | #!/usr/bin/env 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 | |
| 16 | set -eu |
| 17 | |
| 18 | usage() { |
| 19 | echo "gen_self_signed_cert.sh [OPTION] |
| 20 | |
| 21 | Options: |
| 22 | --user-type Server or client depending on intended usage of the cert |
| 23 | --hash-alg The type of hash algorithm to use to sign the cert in openssl format, ex; sha256 |
| 24 | --dns A DNS type subject alternative name to add to the cert. Can be repeated. |
| 25 | --ip An IP type subject alternative name to add to the cert. Can be repeated. |
| 26 | --uri A URI type subject alternative name to add to the cert. Can be repeated. |
| 27 | --cn The CN to add to the cert |
| 28 | --key-type The type of key for the generated certificate to have. Either rsa or ecdsa. |
| 29 | --rsa-key-size Size of rsa key for generated certificate. |
| 30 | --curve-name name of the ECC curve to use for the generated ECDSA certificate. |
| 31 | --prefix Prefix for the output certificate and private key file name. Defaults to the CN value. |
| 32 | " |
| 33 | exit 0; |
| 34 | } |
| 35 | |
| 36 | GETOPT="getopt" |
| 37 | |
| 38 | # use gnu-getopt on macos |
| 39 | if [[ "$OSTYPE" == "darwin"* ]]; then |
| 40 | GETOPT="/usr/local/opt/gnu-getopt/bin/getopt" |
| 41 | |
| 42 | if ! [ -x "$(command -v $GETOPT)" ]; then |
| 43 | echo 'Error: getopt is not installed. Install with `brew install gnu-getopt`' >&2 |
| 44 | exit 1 |
| 45 | fi |
| 46 | fi |
| 47 | |
| 48 | # This only works with gnu getopt. |
| 49 | PARSED_OPTS=`$GETOPT -o vdn: --long help,user-type:,rsa-key-size:,curve-name:,hash-alg:,ip:,uri:,dns:,cn:,key-type:,prefix: -n 'parse-options' -- "$@"` |
| 50 | eval set -- "$PARSED_OPTS" |
| 51 | |
| 52 | USER_TYPE="server" |
| 53 | KEY_TYPE="rsa" |
| 54 | RSA_KEY_SIZE="2048" |
| 55 | CURVE_NAME="prime256v1" |
| 56 | HASH_ALG="sha256" |
| 57 | SANS= |
| 58 | CN="s2nTestCert" |
| 59 | PREFIX= |
| 60 | |
| 61 | while true; do |
| 62 | case "$1" in |
| 63 | --help ) usage ;; |
| 64 | --cn ) CN="$2" ; shift 2 ;; |
| 65 | --dns ) SANS="$SANS""DNS:$2," ; shift 2 ;; |
| 66 | --ip ) SANS="$SANS""IP:$2," ; shift 2 ;; |
| 67 | --uri ) SANS="$SANS""URI:$2," ; shift 2 ;; |
| 68 | --hash-alg ) HASH_ALG="$2" ; shift 2 ;; |
| 69 | --key-type ) KEY_TYPE="$2" ; shift 2 ;; |
| 70 | --rsa-key-size ) RSA_KEY_SIZE="$2" ; shift 2 ;; |
| 71 | --curve-name ) CURVE_NAME="$2" ; shift 2 ;; |
| 72 | --user-type ) USER_TYPE="$2" ; shift 2 ;; |
| 73 | --prefix ) PREFIX="$2" ; shift 2 ;; |
| 74 | -- ) shift; break ;; |
| 75 | * ) break ;; |
| 76 | esac |
| 77 | done |
| 78 | |
| 79 | # Trim the railing comma, openssl x509 config expects the last SAN value to have no comma. |
| 80 | SANS=`echo $SANS | sed 's/,*$//g'` |
| 81 | if [ -z "$PREFIX" ]; then |
| 82 | PREFIX=$CN |
| 83 | fi |
| 84 | |
| 85 | # Picked up by cert_config.cfg. It might have been simpler to just generate all certs with both |
| 86 | # serverAuth and clientAuth KeyUsage. |
| 87 | KEY_USAGE= |
| 88 | if [ "$USER_TYPE" == "server" ] |
| 89 | then |
| 90 | KEY_USAGE="serverAuth"; |
| 91 | elif [ "$USER_TYPE" == "client" ] |
| 92 | then |
| 93 | KEY_USAGE="clientAuth"; |
| 94 | elif [ "$USER_TYPE" == "both" ] |
| 95 | then |
| 96 | KEY_USAGE="serverAuth, clientAuth"; |
| 97 | else |
| 98 | echo "Incorrect user-type: $USER_TYPE" |
| 99 | usage ; |
| 100 | fi |
| 101 | |
| 102 | config=""" |
| 103 | [req] |
| 104 | distinguished_name = req_distinguished_name |
| 105 | x509_extensions = v3_req |
| 106 | prompt = no |
| 107 | [req_distinguished_name] |
| 108 | C = US |
| 109 | ST = WA |
| 110 | L = Seattle |
| 111 | O = Amazon |
| 112 | OU = s2n |
| 113 | """ |
| 114 | |
| 115 | if [[ ! -z "$CN" ]]; then |
| 116 | config+="CN = $CN" |
| 117 | fi |
| 118 | |
| 119 | config+=""" |
| 120 | |
| 121 | [v3_req] |
| 122 | keyUsage = keyEncipherment, dataEncipherment, digitalSignature |
| 123 | extendedKeyUsage = $KEY_USAGE |
| 124 | """ |
| 125 | |
| 126 | if [[ ! -z "$SANS" ]]; then |
| 127 | config+="subjectAltName = $SANS" |
| 128 | fi |
| 129 | |
| 130 | cert_conf_path=$(mktemp) |
| 131 | echo "$config" > $cert_conf_path |
| 132 | |
| 133 | # append an underscore if there's a prefix |
| 134 | if [[ ! -z "$PREFIX" ]]; then |
| 135 | PREFIX="${PREFIX}_" |
| 136 | fi |
| 137 | |
| 138 | if [ "$KEY_TYPE" == "rsa" ]; then |
| 139 | openssl req -x509 -config "$cert_conf_path" -newkey rsa:${RSA_KEY_SIZE} -${HASH_ALG} -nodes -keyout ${PREFIX}rsa_key.pem -out ${PREFIX}rsa_cert.pem -days 36500 |
| 140 | elif [ "$KEY_TYPE" == "ecdsa" ]; then |
| 141 | openssl ecparam -out "${PREFIX}ecdsa_key.pem" -name "$CURVE_NAME" -genkey |
| 142 | openssl req -new -config "$cert_conf_path" -${HASH_ALG} -days 36500 -nodes -x509 -key "${PREFIX}ecdsa_key.pem" -out "${PREFIX}ecdsa_cert.pem" |
| 143 | else |
| 144 | echo "Incorrect key-type: $KEY_TYPE" |
| 145 | usage ; |
| 146 | fi |
| 147 | |
| 148 | rm $cert_conf_path |
| 149 |