Add licenses to files
[integration/csit.git] / plans / dcaegen2-collectors-hv-ves / testsuites / collector / ssl / gen-certs.sh
1 #!/usr/bin/env bash
2 # ============LICENSE_START=======================================================
3 # csit-dcaegen2-collectors-hv-ves
4 # ================================================================================
5 # Copyright (C) 2018 NOKIA
6 # ================================================================================
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #      http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END=========================================================
19
20
21 set -eu -o pipefail -o xtrace
22
23 STORE_PASS=onaponap
24 CN_PREFIX=dcaegen2-hvves
25 DNAME_PREFIX="C=PL,ST=DL,L=Wroclaw,O=Nokia,OU=MANO,CN=${CN_PREFIX}"
26
27 store_opts="-storetype PKCS12 -storepass ${STORE_PASS} -noprompt"
28
29 function gen_key() {
30   local key_name="$1"
31   local ca="$2"
32   local keystore="-keystore ${key_name}.p12 ${store_opts}"
33   keytool -genkey -alias ${key_name} \
34       ${keystore} \
35       -keyalg RSA \
36       -validity 730 \
37       -keysize 2048 \
38       -dname "${DNAME_PREFIX}-${key_name}"
39   keytool -import -trustcacerts -alias ${ca} -file ${ca}.crt ${keystore}
40
41   keytool -certreq -alias ${key_name} -keyalg RSA ${keystore} | \
42       keytool -alias ${ca} -gencert -ext "san=dns:${CN_PREFIX}-${ca}" ${store_opts} -keystore ${ca}.p12 | \
43       keytool -alias ${key_name} -importcert ${keystore}
44 }
45
46
47 function gen_ca() {
48   local ca="$1"
49   keytool -genkeypair ${store_opts} -alias ${ca} -dname "${DNAME_PREFIX}-${ca}" -keystore ${ca}.p12
50   keytool -export -alias ${ca} -file ${ca}.crt ${store_opts} -keystore ${ca}.p12
51 }
52
53 function gen_truststore() {
54   local name="$1"
55   local trusted_ca="$2"
56   keytool -import -trustcacerts -alias ca -file ${trusted_ca}.crt ${store_opts} -keystore ${name}.p12
57 }
58
59 function clean() {
60   rm -f *.crt *.p12
61 }
62
63 if [[ $# -eq 0 ]]; then
64   gen_ca ca
65   gen_ca untrustedca
66   gen_truststore trust ca
67   gen_truststore untrustedtrust untrustedca
68   gen_key client ca
69   gen_key server ca
70   gen_key untrustedclient untrustedca
71 elif [[ $1 == "clean" ]]; then
72   clean
73 else
74   echo "usage: $0 [clean]"
75   exit 1
76 fi
77