ca-scripts/bin/ca-init

90 lines
3.1 KiB
Text
Raw Normal View History

#! /bin/bash
. "/home/alex/code/ca-scripts/lib/ca-functions"
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
__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'
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;;
--) shift; break;;
*) echo "Unknown value '$1'"; exit 1;;
esac
done
# load up the configuration file
CA_CRT_TYPE="ca"
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-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-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-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-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
fi