blob: 34572f7a7955efd66e2f4366d86cbba2c50054d0 [file] [log] [blame]
Gary Wu9abb61c2018-09-27 10:38:50 -07001#!/usr/bin/env bash
2
3set -eu -o pipefail -o xtrace
4
5STORE_PASS=onaponap
6CN_PREFIX=dcaegen2-hvves
7DNAME_PREFIX="C=PL,ST=DL,L=Wroclaw,O=Nokia,OU=MANO,CN=${CN_PREFIX}"
8
9store_opts="-storetype PKCS12 -storepass ${STORE_PASS} -noprompt"
10
11function 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
29function 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
35function 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
41function clean() {
42 rm -f *.crt *.p12
43}
44
45if [[ $# -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
53elif [[ $1 == "clean" ]]; then
54 clean
55else
56 echo "usage: $0 [clean]"
57 exit 1
58fi
59