Added docker-compose ranpm
[nonrtric/plt/ranpm.git] / docker-proj / config / https / 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 SRV="pm-https-server"
53 openssl req -new -x509 -nodes -days 365000  -key ca.key -subj "/C=SE/ST=./L=./O=EST/OU=EST/CN=$SRV/emailAddress=a@example.com" -out httpsca.crt 2> /dev/null
54
55 check_error $?
56
57
58 #for (( i=0; i<${1}; i++ )); do
59 for (( i=1; i<=${1}; i++ )); do
60     SRV="pm-https-server-$i"
61
62     echo " Generating cert and key  for server $SRV"
63     openssl req -newkey rsa:2048 -nodes -days 365000 -subj "/C=SE/ST=./L=./O=ERIC/OU=ERIC/CN=$SRV/emailAddress=a@example.com" -keyout https-$i.key -out https-req$i.crt 2> /dev/null
64
65     check_error $?
66
67     openssl x509 -req -days 365000 -set_serial 01 -in https-req$i.crt -out https-$i.crt -CA httpsca.crt -CAkey ca.key
68     check_error $?
69     echo " Verifying cert towards ca cert"
70     openssl verify -CAfile httpsca.crt httpsca.crt https-$i.crt
71     check_error $?
72
73 done
74
75 echo "DONE"
76 exit 0
77