-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake-ssl-certificate
executable file
·79 lines (60 loc) · 1.53 KB
/
make-ssl-certificate
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
#! /usr/bin/env bash
# Copyright 2012 James Peach
# make-ssl-certificate: Simple-minded self-signed SSL certificato generator.
#
# Usage: make-ssl-certificate DNS [DNS...]
# Generate a self-signed SSL certificate and signing key pair. The certificate
# will be valid for all the names given as arguments.
set -e # exit on error
OPENSSL=${OPENSSL:-openssl}
TMPDIR=${TMPDIR:-/tmp}
cleanup()
{
rm -f $KEYFILE
rm -f $CSRFILE
rm -f $ALTCONF
}
format()
{
$OPENSSL x509 -noout -text -in "$1"
}
altnames()
{
local names=""
while [ $# -gt 0 ]; do
if [ -z "$names" ]; then
names="subjectAltName=DNS:$1"
else
names="$names,DNS:$1"
fi
shift
done
echo $names
}
DNSNAME=$1
KEYFILE=${KEYFILE:-$TMPDIR/$DNSNAME.key}
CSRFILE=${CSRFILE:-$TMPDIR/$DNSNAME.csr}
CERTFILE=${CERTFILE:-$DNSNAME.crt}
ALTCONF=$(mktemp -t $DNSNAME.XXXXXX)
SUBJECT=${SUBJECT:-"/CN=$DNSNAME"}
ALTNAMES=${ALTNAMES:-$(altnames "$@")}
trap "rm -f $CERTFILE" 1 2 3 5 6 9 13 14 15
trap cleanup 0 1 2 3 5 6 9 13 14 15
# Make a signing key.
$OPENSSL genrsa -out $KEYFILE 1024
# Make a signing request.
$OPENSSL req -new \
-subj $SUBJECT \
-key $KEYFILE -out $CSRFILE
echo "$ALTNAMES" > $ALTCONF
# Sign a certificate.
$OPENSSL x509 -req \
-days 1024 \
-in $CSRFILE \
-signkey $KEYFILE \
-extfile $ALTCONF \
-out $CERTFILE
# Stash the key with the certificate. It's easier to keep them together.
cat $KEYFILE >> $CERTFILE
format $CERTFILE | tee -a $CERTFILE
# vim: set ts=4 sw=4 tw=79 et :