ec58eee214a05de7537cec1861619f863589bbe9
[nonrtric/plt/ranpm.git] / install / helm / ran / certs / gen-certs.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2023 Nordix Foundation. All rights reserved.
5 #  ========================================================================
6 #  Licensed under the Apache License, Version 2.0 (the "License");
7 #  you may not use this file except in compliance with the License.
8 #  You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #  Unless required by applicable law or agreed to in writing, software
13 #  distributed under the License is distributed on an "AS IS" BASIS,
14 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #  See the License for the specific language governing permissions and
16 #  limitations under the License.
17 #  ============LICENSE_END=================================================
18 #
19
20 echo "Generating https certs"
21 SD=$(dirname -- "$0")
22 echo "script-home: "$SD
23
24 cd $SD
25
26 print_usage() {
27     echo "Usage: gen-certs.sh <num-certs>"
28     exit 1
29 }
30
31 check_error() {
32     if [ $1 -ne 0 ]; then
33         echo "Failed $2"
34         echo "Exiting..."
35         exit 1
36     fi
37 }
38
39 if [ $# -ne 1 ]; then
40     print_usage
41 fi
42
43 rm *.crt
44 rm *.key
45
46 echo "Generating ca cert and key"
47 echo " Generating ca key"
48 openssl genrsa 2048 > ca.key  2> /dev/null
49 check_error $?
50
51 echo " Generating ca cert"
52 cat <<__EOF__ | openssl req -new -x509 -nodes -days 365000  -key ca.key  -out httpsca.crt 2> /dev/null
53 SE
54 .
55 .
56 EST
57 EST
58 $SRV
59 .
60 __EOF__
61 check_error $?
62
63
64 for (( i=0; i<${1}; i++ )); do
65     SRV="pm-https-server-$i.pm-https-server.ran"
66
67     echo " Generating cert and key  for server $SRV"
68 cat <<__EOF__ | openssl req -newkey rsa:2048 -nodes -days 365000  -keyout https-$i.key -out https-req$i.crt 2> /dev/null
69 SE
70 .
71 .
72 ERIC
73 ERIC
74 $SRV
75 .
76
77 __EOF__
78     check_error $?
79
80     openssl x509 -req -days 365000 -set_serial 01 -in https-req$i.crt -out https-$i.crt -CA httpsca.crt -CAkey ca.key
81     check_error $?
82     echo " Verifying cert towards ca cert"
83     openssl verify -CAfile httpsca.crt httpsca.crt https-$i.crt
84     check_error $?
85
86 done
87
88 echo "DONE"
89 exit 0