2009-10-12 17:46:50 -04:00
|
|
|
#! /bin/bash
|
|
|
|
|
|
|
|
. "/home/alex/code/ca-scripts/lib/ca-functions"
|
|
|
|
|
2010-03-13 10:00:04 -05:00
|
|
|
# XXX: Add an interactive mode to this script to obviate the need for a
|
|
|
|
# pre-existing config file? e.g.
|
|
|
|
# $ cd /empty/directory or /dir/containing/partial/conf
|
|
|
|
# $ ca-init -i
|
|
|
|
# or maybe:
|
|
|
|
# $ ca-init -i /path/to/CA_HOME
|
|
|
|
# + modify config file loading to check current directory too?
|
|
|
|
|
|
|
|
# XXX: corollary to above: provide --long-options for all config file
|
|
|
|
# variables also, reducing or eliminating *requirement* for config file?
|
|
|
|
|
2009-10-12 17:46:50 -04:00
|
|
|
usage() {
|
|
|
|
cat <<__EOT__
|
|
|
|
Usage: $PROGNAME [options]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-h, --help Print this helpful message!
|
|
|
|
-c, --encrypt Encrypt CA private key with Triple-DES
|
|
|
|
-f, --config FILE Use config file instead of $CONFFILE
|
|
|
|
-i, --template FILE Use alternative index.html template
|
|
|
|
-o, --output FILE Generate CA index.html in FILE
|
2009-10-16 13:37:11 -04:00
|
|
|
-s, --crt-only Only generate CA cert/key, use pre-created config
|
|
|
|
-x, --cnf-only Only generate CA config file, don't create CA cert/key
|
2009-10-12 17:46:50 -04:00
|
|
|
|
|
|
|
__EOT__
|
|
|
|
}
|
|
|
|
|
2009-10-16 13:37:11 -04:00
|
|
|
short='hcf:i:o:sx'
|
2009-10-17 09:18:35 -04:00
|
|
|
long='help,encrypt,config:,template:,output:,crt-only,cnf-only'
|
2009-10-12 17:46:50 -04:00
|
|
|
opts=$( getopt -o "$short" -l "$long" -n "$PROGNAME" -- "$@" )
|
|
|
|
if [ 0 -ne $? ]; then echo; usage; exit 1; fi
|
|
|
|
eval set -- "$opts";
|
|
|
|
|
|
|
|
while :; do
|
|
|
|
case "$1" in
|
|
|
|
-h|--help) usage; exit 0;;
|
|
|
|
-c|--encrypt) CRYPTKEY=""; shift;;
|
|
|
|
-f|--config) shift; CONFFILE="$1"; shift;;
|
|
|
|
-i|--template) shift; INDEXTPL="$1"; shift;;
|
|
|
|
-o|--output) shift; INDEXOUT="$1"; shift;;
|
2009-10-16 13:37:11 -04:00
|
|
|
-s|--crt-only) CRT_ONLY=1; shift;;
|
|
|
|
-x|--cnf-only) CNF_ONLY=1; shift;;
|
2009-10-12 17:46:50 -04:00
|
|
|
--) shift; break;;
|
|
|
|
*) echo "Unknown value '$1'"; exit 1;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
# load up the configuration file
|
|
|
|
ca_load_conf
|
|
|
|
|
2009-10-16 13:37:11 -04:00
|
|
|
if [ 1 -eq "$CRT_ONLY" -a 1 -eq "$CNF_ONLY" ]; then
|
|
|
|
error "The --crt-only and --cnf-only options are mutually exclusive."
|
|
|
|
fi
|
2009-10-18 12:20:09 -04:00
|
|
|
if [ 1 -eq "$CNF_ONLY" -a -n "$INDEXOUT" ]; then
|
|
|
|
error "Cannot generate index.html when not creating certificates."
|
|
|
|
fi
|
2009-10-12 17:46:50 -04:00
|
|
|
|
2009-10-16 13:37:11 -04:00
|
|
|
if [ 1 -ne "$CRT_ONLY" ]; then
|
|
|
|
# create the directory structure that'll be populated by the scripts
|
|
|
|
mkdir -p $CA_HOME/{cnf,crl,crt,csr,db,idx,key,p12}
|
|
|
|
echo "01" > $CA_HOME/db/crlnumber
|
|
|
|
touch $CA_HOME/db/index.txt
|
|
|
|
touch $CA_HOME/db/.rand
|
2009-10-12 17:46:50 -04:00
|
|
|
|
2009-10-16 13:37:11 -04:00
|
|
|
# generate an openssl configuration for this CA
|
|
|
|
ca_template ca-config "$CA_HOME/cnf/$CA_NAME.ca.cnf"
|
|
|
|
fi
|
|
|
|
if [ 1 -ne "$CNF_ONLY" ]; then
|
2009-10-18 12:20:09 -04:00
|
|
|
if [ ! -f "$CA_HOME/cnf/$CA_NAME.ca.cnf" ]; then
|
|
|
|
# looks like someone's running ca-init with -s without using -x first
|
|
|
|
error "Could not find CA config. Please run ca-init -x before using ca-init -s."
|
|
|
|
fi
|
2009-10-16 13:37:11 -04:00
|
|
|
# generate a self-signed cert that is valid for 10 years, with
|
|
|
|
# ... the private key in $CA_HOME/key/$CA_NAME.ca.key
|
|
|
|
# ... the certificate in $CA_HOME/crt/$CA_NAME.ca.crt
|
|
|
|
# ... using the config in $CA_HOME/cnf/$CA_NAME.ca.cnf
|
|
|
|
openssl req -new $CRYPTKEY -config "$CA_HOME/cnf/$CA_NAME.ca.cnf" \
|
2009-10-17 09:18:35 -04:00
|
|
|
-keyout "$CA_HOME/key/$CA_NAME.ca.key" \
|
|
|
|
-out "$CA_HOME/csr/$CA_NAME.ca.csr"
|
2009-10-18 12:20:09 -04:00
|
|
|
chmod 400 "$CA_HOME/key/$CA_NAME.ca.key"
|
2009-10-12 17:46:50 -04:00
|
|
|
|
2009-10-16 13:37:11 -04:00
|
|
|
openssl ca -create_serial -selfsign -days 3652 -batch \
|
2009-10-17 09:18:35 -04:00
|
|
|
-name ca_scripts -extensions ca_x509_extensions \
|
|
|
|
-config "$CA_HOME/cnf/$CA_NAME.ca.cnf" \
|
|
|
|
-in "$CA_HOME/csr/$CA_NAME.ca.csr" \
|
|
|
|
-keyfile "$CA_HOME/key/$CA_NAME.ca.key" \
|
|
|
|
-out "$CA_HOME/crt/$CA_NAME.ca.crt"
|
2009-10-12 17:46:50 -04:00
|
|
|
|
2009-10-16 13:37:11 -04:00
|
|
|
# generate an initial CRL too (yes it will be empty, but we should serve it)
|
|
|
|
ca_gen_crl
|
|
|
|
if [ -n "$INDEXOUT" ]; then
|
|
|
|
ca_checksum
|
|
|
|
ca_template $INDEXTPL $INDEXOUT
|
|
|
|
fi
|
2009-10-12 17:46:50 -04:00
|
|
|
fi
|