ca-sign: Verify cert request before acting on it.
[hcoop/scripts.git] / ca-sign
CommitLineData
8bc08255 1#!/bin/bash
4c237a24 2#
3# Sign a certificate request as a CA. Run this on deleuze as an
8bc08255 4# admin. If a domain is provided, then the certificate request must
5# apply only to that domain.
4c237a24 6#
8bc08255 7# Usage: ca-sign days request.csr outfile.pem [domain]
4c237a24 8
8bc08255 9if test -n "$5" || test -z "$3"; then
e07d61c2 10 echo "Incorrect arguments."
8bc08255 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."
e07d61c2 18 exit 1
19fi
4c237a24 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
8bc08255 30# Parameters
4c237a24 31DAYS=$1
32REQUEST=$2
33PEM=$3
8bc08255 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
4c237a24 52ID=$(cat -- $DIR/serial)
53
8bc08255 54# Exit on error
55set -e
56
4c237a24 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 ..."
87d0fa09 67openssl ca -config $CONF -batch -gencrl -crldays 30 -out $CRL1.pem
4c237a24 68openssl crl -outform DER -out $CRL1.crl -in $CRL1.pem
87d0fa09 69openssl ca -config $CONF -batch -gencrl -crldays 30 -crlexts crl_ext \
4c237a24 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!"