ca-sign: Verify cert request before acting on it.
[hcoop/scripts.git] / scripts / ca-sign
CommitLineData
d00fad4c 1#!/bin/bash
b4e6da64 2#
3# Sign a certificate request as a CA. Run this on deleuze as an
d00fad4c 4# admin. If a domain is provided, then the certificate request must
5# apply only to that domain.
b4e6da64 6#
d00fad4c 7# Usage: ca-sign days request.csr outfile.pem [domain]
b4e6da64 8
d00fad4c 9if test -n "$5" || test -z "$3"; then
d10cbde6 10 echo "Incorrect arguments."
d00fad4c 11 echo "Usage: ca-sign days request.csr outfile.pem [domain]"
12 exit 1
13fi
14
15# Make sure we run this from deleuze
16if test "$(hostname -s)" != "deleuze"; then
17 echo "Error: This script must be run from deleuze."
d10cbde6 18 exit 1
19fi
b4e6da64 20
21DIR=/var/local/lib/ca
22CONF=$DIR/openssl.cnf
23POLICY=policy_anything
24
25# Certificate revocation list
26CRL1=$DIR/crl-v1
27CRL2=$DIR/crl-v2
28CA_LOC=/afs/hcoop.net/user/h/hc/hcoop/public_html/ca
29
d00fad4c 30# Parameters
b4e6da64 31DAYS=$1
32REQUEST=$2
33PEM=$3
d00fad4c 34DOMAIN=$4
35
36# Verify request
37STATUS=$(openssl req -noout -in "$REQUEST" -verify 2>&1)
38if test "$STATUS" != "verify OK"; then
39 echo "Error: This is not a valid certificate request."
40 exit 1
41fi
42if test -n "$DOMAIN"; then
43 CN=$(openssl req -text -in "$REQUEST" | grep "Subject:" | grep "CN=." | \
44 sed -r -e 's/^.*CN=([^/=,]+).*$/\1/1')
45 if test "${CN%%${DOMAIN}}" = "${CN}"; then
46 echo "Error: Domain in cert does not match $DOMAIN."
47 exit 1
48 fi
49fi
50
51# Get new serial number
b4e6da64 52ID=$(cat -- $DIR/serial)
53
d00fad4c 54# Exit on error
55set -e
56
b4e6da64 57# Sign.
58echo "Signing certificate request $REQUEST ..."
59openssl ca -config $CONF -policy $POLICY -out $PEM -in $REQUEST -days $DAYS
60echo
61
62# Make a copy of the request
63cp $REQUEST $DIR/requests/$ID.csr
64
65# Update revocation list.
66echo "Updating certificate revocation list ..."
cd47379b 67openssl ca -config $CONF -batch -gencrl -crldays 30 -out $CRL1.pem
b4e6da64 68openssl crl -outform DER -out $CRL1.crl -in $CRL1.pem
cd47379b 69openssl ca -config $CONF -batch -gencrl -crldays 30 -crlexts crl_ext \
b4e6da64 70 -out $CRL2.pem
71openssl crl -outform DER -out $CRL2.crl -in $CRL2.pem
72cp $CRL1.crl $CRL2.crl $CA_LOC
73echo
74
75echo "Don't forget to run ca-install to install the signed certificate!"