3
3
4
4
CERT_BASE=" certs"
5
5
6
- if [ -z $1 ]; then
7
- echo " Usage: gen_certs.sh <hostname > <ip> [<password>]"
6
+ if [ -z $1 ] || [ -z $2 ] ; then
7
+ echo " Usage: gen_certs.sh <server_hostname > <ip> [<password>]"
8
8
exit 1
9
9
fi
10
10
11
+ server_hostname=$1
12
+ ip=$2
13
+ password=$3
14
+
11
15
mkdir -p $CERT_BASE
12
16
17
+ function print_red () {
18
+ printf " \033[0;31m$1 ...\033[0m\n"
19
+ }
20
+
13
21
# Setting up a CA
22
+ print_red " Generating rootCA"
14
23
openssl genrsa -out $CERT_BASE /rootCA.key 2048
15
24
openssl req -subj /C=/ST=/L=/O=/CN=rootCA -x509 -new -nodes -key $CERT_BASE /rootCA.key -sha256 -days 1095 -out $CERT_BASE /rootCA.pem
16
25
17
26
# Setting up device cert and key
27
+ print_red " Generating device certificates with CN $server_hostname and IP $ip "
18
28
openssl genrsa -out $CERT_BASE /device.key 2048
19
- openssl req -subj /C=/ST=/L=/O=/CN=$1 -new -key $CERT_BASE /device.key -out $CERT_BASE /device.csr
20
- openssl x509 -req -in $CERT_BASE /device.csr -CA $CERT_BASE /rootCA.pem -CAkey $CERT_BASE /rootCA.key -CAcreateserial -out $CERT_BASE /device.crt -days 1095 -sha256 -extfile <( printf " %s" " subjectAltName=DNS:$1 ,IP:$2 " )
29
+ openssl req -subj /C=/ST=/L=/O=/CN=$server_hostname -new -key $CERT_BASE /device.key -out $CERT_BASE /device.csr
30
+ openssl x509 -req -in $CERT_BASE /device.csr -CA $CERT_BASE /rootCA.pem -CAkey $CERT_BASE /rootCA.key -CAcreateserial -out $CERT_BASE /device.crt -days 1095 -sha256 -extfile <( printf " %s" " subjectAltName=DNS:$server_hostname ,IP:$ip " )
21
31
22
- # Encrypt device key - needed for input to IOS
23
- if [ ! -z $3 ]; then
24
- openssl rsa -des3 -in $CERT_BASE /device.key -out $CERT_BASE /device.des3.key -passout pass:$2
32
+ # Encrypt device key
33
+ if [ ! -z $password ]; then
34
+ print_red " Encrypting device certificates and bundling with password"
35
+ # DES 3 for device, needed for input to IOS XE
36
+ openssl rsa -des3 -in $CERT_BASE /device.key -out $CERT_BASE /device.des3.key -passout pass:$password
37
+ # PKCS #12 for device, needed for NX-OS
38
+ # Uncertain if this is correct
39
+ openssl pkcs12 -export -out $CERT_BASE /device.pfx -inkey $CERT_BASE /device.key -in $CERT_BASE /device.crt -certfile $CERT_BASE /device.crt -password pass:$password
25
40
else
26
- echo " Skipping device key encryption. "
41
+ print_red " Skipping device key encryption"
27
42
fi
28
43
29
44
# Setting up client cert and key
45
+ hostname=$( hostname)
46
+ print_red " Generating client certificates with CN $hostname "
30
47
openssl genrsa -out $CERT_BASE /client.key 2048
31
- openssl req -subj /C=/ST=/L=/O=/CN=gnmi_client -new -key $CERT_BASE /client.key -out $CERT_BASE /client.csr
48
+ openssl req -subj /C=/ST=/L=/O=/CN=$hostname -new -key $CERT_BASE /client.key -out $CERT_BASE /client.csr
32
49
openssl x509 -req -in $CERT_BASE /client.csr -CA $CERT_BASE /rootCA.pem -CAkey $CERT_BASE /rootCA.key -CAcreateserial -out $CERT_BASE /client.crt -days 1095 -sha256
0 commit comments