summaryrefslogtreecommitdiffstats
path: root/openssl/mksslcert.sh
blob: 9cca3fb81754fcfb5f21ab615fbdf13b5a2c1525 (plain)
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
#!/bin/sh
#
# mksslcert
#
# creates self-signed openssl certificates based on
# the local hostname or the given one
# Fallback to localhost if not set.
#
# J�rgen Daubert, jue at jue dot li


print_help() {
	echo "usage: ${0##*/} <key> <cert> [hostname]"
	echo "  key       full path to openssl private key"
	echo "  cert      full path to openssl certificate"
	echo "  hostname  host name of certificate"
}

main() {
	if [ ! "$1" -o ! "$2" ]; then
		print_help
		exit 1
	fi
	
	KEY=$1
	CRT=$2
	FQDN=$(hostname -f) || FQDN=localhost
	if [ ! -z "$3" ]; then
		FQDN="$3"
	fi
	INFO=".\n.\n.\n.\n.\n$FQDN\nroot@$FQDN"
	OPTS="req -new -nodes -x509 -days 365 -newkey rsa:1024"
	
	echo -e $INFO | openssl $OPTS -out $CRT -keyout $KEY 2> /dev/null
	
	if [ $? -ne 0 ]; then
		echo "Error: creating of certificate failed"
		exit 1
	else
		echo "SSL certificate $CRT with key $KEY for host $FQDN created"
		chmod 0600 $CRT $KEY 
	fi
}

main "$@"

# End of file