Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -eu -o pipefail -o xtrace |
| 4 | |
| 5 | STORE_PASS=onaponap |
| 6 | CN_PREFIX=dcaegen2-hvves |
| 7 | DNAME_PREFIX="C=PL,ST=DL,L=Wroclaw,O=Nokia,OU=MANO,CN=${CN_PREFIX}" |
| 8 | |
| 9 | store_opts="-storetype PKCS12 -storepass ${STORE_PASS} -noprompt" |
| 10 | |
| 11 | function gen_key() { |
| 12 | local key_name="$1" |
| 13 | local ca="$2" |
| 14 | local keystore="-keystore ${key_name}.p12 ${store_opts}" |
| 15 | keytool -genkey -alias ${key_name} \ |
| 16 | ${keystore} \ |
| 17 | -keyalg RSA \ |
| 18 | -validity 730 \ |
| 19 | -keysize 2048 \ |
| 20 | -dname "${DNAME_PREFIX}-${key_name}" |
| 21 | keytool -import -trustcacerts -alias ${ca} -file ${ca}.crt ${keystore} |
| 22 | |
| 23 | keytool -certreq -alias ${key_name} -keyalg RSA ${keystore} | \ |
| 24 | keytool -alias ${ca} -gencert -ext "san=dns:${CN_PREFIX}-${ca}" ${store_opts} -keystore ${ca}.p12 | \ |
| 25 | keytool -alias ${key_name} -importcert ${keystore} |
| 26 | } |
| 27 | |
| 28 | |
| 29 | function gen_ca() { |
| 30 | local ca="$1" |
| 31 | keytool -genkeypair ${store_opts} -alias ${ca} -dname "${DNAME_PREFIX}-${ca}" -keystore ${ca}.p12 |
| 32 | keytool -export -alias ${ca} -file ${ca}.crt ${store_opts} -keystore ${ca}.p12 |
| 33 | } |
| 34 | |
| 35 | function gen_truststore() { |
| 36 | local name="$1" |
| 37 | local trusted_ca="$2" |
| 38 | keytool -import -trustcacerts -alias ca -file ${trusted_ca}.crt ${store_opts} -keystore ${name}.p12 |
| 39 | } |
| 40 | |
| 41 | function clean() { |
| 42 | rm -f *.crt *.p12 |
| 43 | } |
| 44 | |
| 45 | if [[ $# -eq 0 ]]; then |
| 46 | gen_ca ca |
| 47 | gen_ca untrustedca |
| 48 | gen_truststore trust ca |
| 49 | gen_truststore untrustedtrust untrustedca |
| 50 | gen_key client ca |
| 51 | gen_key server ca |
| 52 | gen_key untrustedclient untrustedca |
| 53 | elif [[ $1 == "clean" ]]; then |
| 54 | clean |
| 55 | else |
| 56 | echo "usage: $0 [clean]" |
| 57 | exit 1 |
| 58 | fi |
| 59 | |