File: ssl-certificates.sh

package info (click to toggle)
kopanocore 8.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 15,400 kB
  • sloc: cpp: 175,422; python: 24,623; perl: 7,319; php: 6,056; sh: 2,172; makefile: 1,294; xml: 45; ansic: 1
file content (133 lines) | stat: -rwxr-xr-x 3,604 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-only

CADIR=./demoCA
DAYS=$((3*365))
NAME=$1

if [ -d /usr/share/ssl/misc ]; then
	# SuSE / RHEL4
	if [ -x /usr/share/ssl/misc/CA.pl ]; then
		CASCRIPT=/usr/share/ssl/misc/CA.pl
	elif [ -x /usr/share/ssl/misc/CA.sh ]; then
		CASCRIPT=/usr/share/ssl/misc/CA.sh
	elif [ -x /usr/share/ssl/misc/CA ]; then
		CASCRIPT=/usr/share/ssl/misc/CA
	fi
	if [ -f /usr/share/ssl/misc/openssl.cnf ]; then
		# RHEL4
		CADIR=`grep -w ^dir -m 1 /usr/share/ssl/misc/openssl.cnf | awk {'print $3'}`
	elif [ -f /etc/ssl/openssl.cnf ]; then
		# SuSE
		CADIR=`grep -w ^dir -m 1 /etc/ssl/openssl.cnf | awk {'print $3'}`
	fi
elif [ -d /usr/lib/ssl/misc ]; then
	# Debian / Ubuntu
	if [ -x /usr/lib/ssl/misc/CA.pl ]; then
		CASCRIPT=/usr/lib/ssl/misc/CA.pl
	elif [ -x /usr/lib/ssl/misc/CA.sh ]; then
		CASCRIPT=/usr/lib/ssl/misc/CA.sh
	elif [ -x /usr/lib/ssl/misc/CA ]; then
		CASCRIPT=/usr/lib/ssl/misc/CA
	fi
	if [ -f /usr/lib/ssl/misc/openssl.cnf ]; then
		# --
		CADIR=`grep -w ^dir -m 1 /usr/lib/ssl/misc/openssl.cnf | awk {'print $3'}`
	elif [ -f /etc/ssl/openssl.cnf ]; then
		# Debian / Ubuntu
		CADIR=`grep -w ^dir -m 1 /etc/ssl/openssl.cnf | awk {'print $3'}`
	fi
elif [ -d /etc/pki/tls/misc ]; then
	# Fedora Core, RHEL5, RHEL6
	if [ -x /etc/pki/tls/misc/CA.pl ]; then
		CASCRIPT=/etc/pki/tls/misc/CA.pl
	elif [ -x /etc/pki/tls/misc/CA.sh ]; then
		CASCRIPT=/etc/pki/tls/misc/CA.sh
	elif [ -x /etc/pki/tls/misc/CA ]; then
		CASCRIPT=/etc/pki/tls/misc/CA
	fi
	if [ -f /etc/pki/tls/openssl.cnf ]; then
		CADIR=`grep -w ^dir -m 1 /etc/pki/tls/openssl.cnf | awk {'print $3'}`
	elif [ -f /etc/ssl/openssl.cnf ]; then
		# --
		CADIR=`grep -w ^dir -m 1 /etc/ssl/openssl.cnf | awk {'print $3'}`
	fi
elif [ -d /var/lib/ssl/misc ]; then
	# ALTLinux
	if [ -x /var/lib/ssl/misc/CA ]; then
		CASCRIPT=/var/lib/ssl/misc/CA
	fi
	if [ -f /etc/openssl/openssl.cnf ]; then
		# ALTLinux
		CADIR=`grep -w ^dir -m 1 /etc/openssl/openssl.cnf | awk {'print $3'}`
	fi
fi

if [ -z "$CASCRIPT" ]; then
	echo "OpenSSL CA script not found. Type script location below, or press enter to exit."
	read CASCRIPT
	if [ -z "$CASCRIPT" ]; then
		exit 0
	fi
	if [ ! -x "$CASCRIPT" ]; then
		echo "Script '$CASCRIPT' does not exist, or is not executable."
		exit 1
	fi
fi

if [ -z "$NAME" ]; then
	while [ -z "$NAME" -o -f "$NAME.pem" ]; do
		echo -n "Enter the name of the service:  "
		read NAME
		if [ -z "$NAME" ]; then
			echo "No name given."
		elif [ -f "$NAME.pem" ]; then
			echo "$NAME.pem already exists."
		fi
	done
fi

set -e

if [ ! -d "$CADIR" -o ! -f "$CADIR/serial" ]; then
	echo "No Certificate Authority Root found in current directory."
	echo "Press enter to create, or ctrl-c to exit."
	read dummy
	if [ -d "$CADIR" ]; then
		mv $CADIR ${CADIR}-backup.kopano
	fi
	$CASCRIPT -newca
fi

echo
echo "Now creating service certificate"
echo

# create new local service certificate
openssl req -new -keyout newkey.pem -out newreq.pem -days $DAYS

echo
echo "Signing certificate"
echo
openssl ca -days $DAYS -policy policy_anything -out newcert.pem -infiles newreq.pem

cat newkey.pem newcert.pem > $NAME.pem
chmod 600 $NAME.pem
rm newkey.pem newcert.pem newreq.pem

echo
echo -n "Create public key from this certificate? [y]  "
read public
PUBCMD="openssl rsa -in $NAME.pem -out $NAME-public.pem -outform PEM -pubout"
if [ -z "$public" -o "$public" = "y" ]; then
	set -- -e
	$PUBCMD
	if [ $? -ne 0 ]; then
		echo
		echo "No public key created. Use the following command to create it:"
		echo $PUBCMD
	fi
else
	echo "No public key created. Use the following command to create it:"
	echo $PUBCMD
fi