-
Notifications
You must be signed in to change notification settings - Fork 12
/
sign-user-cert.sh
executable file
·83 lines (75 loc) · 2.06 KB
/
sign-user-cert.sh
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
#!/bin/bash
##
## sign-user-cert.sh - sign using our root CA the user cert
## Copyright (c) 2000 Yeak Nai Siew, All Rights Reserved.
##
HASHALGO="sha256"
VALID_DAYS=730
RANDOM_SRC=/dev/urandom
CERT=$1
if [ $# -ne 1 ]; then
echo "Usage: $0 [email protected]"
exit 1
fi
if [ ! -f $CERT.csr ]; then
echo "No $CERT.csr found. You must create that first."
exit 1
fi
# Check for root CA key
if [ ! -f ca.key -o ! -f ca.crt ]; then
echo "You must have root CA key generated first."
exit 1
fi
# Sign it with our CA key #
# make sure environment exists
if [ ! -d ca.db.certs ]; then
mkdir ca.db.certs
fi
if [ ! -f ca.db.serial ]; then
echo '01' >ca.db.serial
fi
if [ ! -f ca.db.index ]; then
cp /dev/null ca.db.index
fi
# create the CA requirement to sign the cert
cat >ca.config <<EOT
[ ca ]
default_ca = default_CA
[ default_CA ]
dir = .
certs = \$dir
new_certs_dir = \$dir/ca.db.certs
database = \$dir/ca.db.index
serial = \$dir/ca.db.serial
RANDFILE = ${RANDOM_SRC}
certificate = \$dir/ca.crt
private_key = \$dir/ca.key
default_days = ${VALID_DAYS}
default_crl_days = 30
default_md = $HASHALGO
preserve = yes
x509_extensions = user_cert
policy = policy_anything
[ policy_anything ]
commonName = supplied
emailAddress = supplied
[ user_cert ]
#SXNetID = 3:yeak
subjectAltName = email:copy
basicConstraints = critical,CA:false
authorityKeyIdentifier = keyid:always
extendedKeyUsage = clientAuth,emailProtection
EOT
# revoke an existing old certificate
if [ -f $CERT.crt ]; then
openssl ca -revoke $CERT.crt -config ca.config
fi
# sign the certificate
echo "CA signing: $CERT.csr -> $CERT.crt:"
openssl ca -config ca.config -out $CERT.crt -infiles $CERT.csr
echo "CA verifying: $CERT.crt <-> CA cert"
openssl verify -CAfile ca.crt $CERT.crt
# cleanup after SSLeay
rm -f ca.config
rm -f ca.db.serial.old
rm -f ca.db.index.old