Minimal X.509 certificate authority.
[ca] / bin / make-ca-key
CommitLineData
b294f6b5
MW
1#! /bin/sh
2
3set -e
4certroot=$(cd ${0%/*}/..; pwd)
5cd "$certroot"
6umask 022
7
8## Archive any existing CA.
9if [ -f ca.cert ]; then
10 mkdir -p archive
11 if [ -f archive/state/serial ]; then
12 next=$(cat archive/state/serial)
13 else
14 mkdir -p archive/state
15 next=1
16 fi
17 mkdir archive/"$next"
18 mv ca.cert certs crls index private state archive/"$next"/
19 expr "$next" + 1 >archive/state/serial.new
20 mv archive/state/serial.new archive/state/serial
21fi
22
23## Clear out the old CA completely.
24rm -rf certs index private tmp state
25rm -f ca.cert distorted.crl
26
27## Build a new one.
28mkdir -m750 private
29mkdir -m775 certs crls index index/byhash index/byserial state tmp
30chown root:ca certs crls index index/byhash index/byserial private state tmp
31touch state/db
32echo 01 >state/serial
33echo 01 >state/crlnumber
34
35## Set the CA subject name. It won't fit on one line, and there's no
36## good way of continuing it. Have fun parsing the sed.
37subject=$(sed -n 's:^:/:;1h;2,$H;${x;s/\n//g;p;}' <<EOF
38C=GB
39ST=Cambridgeshire
40O=distorted.org.uk
41OU=Certificate Authority
42CN=distorted.org.uk top-level CA
43emailAddress=ca@distorted.org.uk
44EOF
45)
46
47## Build the new CA key and certificate.
48umask 027
49openssl req -new -config openssl.conf -x509 -days 3650 \
50 -out ca.cert -keyout private/ca.key \
51 -subj "$subject"
52chown root:ca private/ca.key
53chmod 644 ca.cert