19bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels#!/bin/bash
29bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
39bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels# (c) CopyRight EdelWeb for EdelKey and OpenEvidence, 2000-2004, 2009
49bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels# Author: Peter Sylvester
59bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
69bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels# "libre" for integration with curl
79bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
89bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas EckelsOPENSSL=openssl
99bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsif [ -f /usr/local/ssl/bin/openssl ] ; then
109bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels   OPENSSL=/usr/local/ssl/bin/openssl
119bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsfi
129bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
139bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas EckelsUSAGE="echo Usage is genserv.sh <prefix> <caprefix>"
149bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
159bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas EckelsHOME=`pwd`
169bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelscd $HOME
179bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
189bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas EckelsKEYSIZE=1024
199bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas EckelsDURATION=3000
209bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
219bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas EckelsREQ=YES
229bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas EckelsP12=NO
239bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas EckelsDHP=NO
249bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
259bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas EckelsPREFIX=$1
269bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsif [ ".$PREFIX" = . ] ; then
279bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels	echo No configuration prefix
289bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels	NOTOK=1
299bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelselse
309bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels    if [ ! -f $PREFIX-sv.prm ] ; then
319bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels	echo No configuration file $PREFIX-sv.prm
329bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels	NOTOK=1
339bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels    fi
349bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsfi
359bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
369bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas EckelsCAPREFIX=$2
379bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsif [ ".$CAPREFIX" = . ] ; then
389bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels	echo No CA prefix
399bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels	NOTOK=1
409bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelselse
419bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels    if [ ! -f $CAPREFIX-ca.cacert ] ; then
429bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels	echo No CA certficate file $CAPREFIX-ca.caert
439bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels	NOTOK=1
449bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels    fi
459bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels    if [ ! -f $CAPREFIX-ca.key ] ; then
469bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels	echo No $CAPREFIX key
479bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels        NOTOK=1
489bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels    fi
499bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsfi
509bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
519bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsif [ ".$NOTOK" != . ] ; then
529bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels    echo "Sorry, I can't do that for you."
539bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels    $USAGE
549bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels    exit
559bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsfi
569bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
579bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsif [ ".$SERIAL" = . ] ; then
589bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels	GETSERIAL="\$t = time ;\$d =  \$t . substr(\$t+$$ ,-4,4)-1;print \$d"
599bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels	SERIAL=`/usr/bin/env perl -e "$GETSERIAL"`
609bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsfi
619bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
629bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsecho SERIAL=$SERIAL PREFIX=$PREFIX CAPREFIX=$CAPREFIX DURATION=$DURATION KEYSIZE=$KEYSIZE
639bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
649bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsif [ "$DHP." = YES. ] ; then
659bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels   echo "openssl dhparam -2 -out $PREFIX-sv.dhp $KEYSIZE"
669bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels   $OPENSSL dhparam -2 -out $PREFIX-sv.dhp $KEYSIZE
679bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsfi
689bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
699bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsif [ "$REQ." = YES. ] ; then
709bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels   echo "openssl req -config $PREFIX-sv.prm -newkey rsa:$KEYSIZE -keyout $PREFIX-sv.key -out $PREFIX-sv.csr -passout XXX"
719bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels   $OPENSSL req -config $PREFIX-sv.prm -newkey rsa:$KEYSIZE -keyout $PREFIX-sv.key -out $PREFIX-sv.csr -passout pass:secret
729bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsfi
739bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
749bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsecho "openssl rsa -in $PREFIX-sv.key -out $PREFIX-sv.key"
759bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels$OPENSSL rsa -in $PREFIX-sv.key -out $PREFIX-sv.key -passin pass:secret
769bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsecho pseudo secrets generated
779bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
78e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETecho "openssl rsa -in $PREFIX-sv.key -pubout -outform DER -out $PREFIX-sv.pub.der"
79e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET$OPENSSL rsa -in $PREFIX-sv.key -pubout -outform DER -out $PREFIX-sv.pub.der
809bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
81e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETecho "openssl rsa -in $PREFIX-sv.key -pubout -outform PEM -out $PREFIX-sv.pub.pem"
82e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET$OPENSSL rsa -in $PREFIX-sv.key -pubout -outform PEM -out $PREFIX-sv.pub.pem
83e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
84e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETecho "openssl x509 -set_serial $SERIAL -extfile $PREFIX-sv.prm -days $DURATION  -CA $CAPREFIX-ca.cacert -CAkey $CAPREFIX-ca.key -in $PREFIX-sv.csr -req -text -nameopt multiline -sha1 > $PREFIX-sv.crt "
85e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
86e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET$OPENSSL x509 -set_serial $SERIAL -extfile $PREFIX-sv.prm -days $DURATION  -CA $CAPREFIX-ca.cacert -CAkey $CAPREFIX-ca.key -in $PREFIX-sv.csr -req -text -nameopt multiline -sha1 > $PREFIX-sv.crt
879bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
889bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsif [ "$P12." = YES. ] ; then
899bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
909bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels   echo "$OPENSSL pkcs12 -export -des3 -out $PREFIX-sv.p12 -caname $CAPREFIX -name $PREFIX -inkey $PREFIX-sv.key -in $PREFIX-sv.crt -certfile $CAPREFIX-ca.crt "
919bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
929bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels   $OPENSSL pkcs12 -export -des3 -out $PREFIX-sv.p12 -caname $CAPREFIX -name $PREFIX -inkey $PREFIX-sv.key -in $PREFIX-sv.crt -certfile $CAPREFIX-ca.crt
939bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsfi
949bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
959bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsecho "openssl x509 -noout -text -hash -in $PREFIX-sv.selfcert -nameopt multiline"
969bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels$OPENSSL x509 -noout -text -hash -in $PREFIX-sv.crt -nameopt multiline
979bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
989bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels# revoke server cert
999bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelstouch $CAPREFIX-ca.db
1009bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsecho 01 > $CAPREFIX-ca.cnt
1019bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsecho "openssl ca -config $CAPREFIX-ca.cnf -revoke $PREFIX-sv.crt"
1029bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels$OPENSSL ca -config $CAPREFIX-ca.cnf -revoke $PREFIX-sv.crt
1039bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
1049bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels# issue CRL
1059bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsecho "openssl ca -config $CAPREFIX-ca.cnf -gencrl -out $PREFIX-sv.crl"
1069bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels$OPENSSL ca -config $CAPREFIX-ca.cnf -gencrl -out $PREFIX-sv.crl
1079bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
1089bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsecho "openssl x509 -in $PREFIX-sv.crt -outform der -out $PREFIX-sv.der "
1099bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels$OPENSSL x509 -in $PREFIX-sv.crt -outform der -out $PREFIX-sv.der
1109bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
1119bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels# all together now
1129bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelstouch $PREFIX-sv.dhp
1139bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelscat $PREFIX-sv.prm $PREFIX-sv.key  $PREFIX-sv.crt $PREFIX-sv.dhp >$PREFIX-sv.pem
1149bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelschmod o-r $PREFIX-sv.prm
1159bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
1169bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsecho "$PREFIX-sv.pem done"
1179bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
1189bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
119