genserv.sh revision 9bd90e6e25f1e55f50201c87a1b5837de7e5b64a
1#!/bin/bash
2
3# (c) CopyRight EdelWeb for EdelKey and OpenEvidence, 2000-2004, 2009
4# Author: Peter Sylvester
5
6# "libre" for integration with curl
7
8OPENSSL=openssl
9if [ -f /usr/local/ssl/bin/openssl ] ; then
10   OPENSSL=/usr/local/ssl/bin/openssl
11fi
12
13USAGE="echo Usage is genserv.sh <prefix> <caprefix>"
14
15HOME=`pwd`
16cd $HOME
17
18KEYSIZE=1024
19DURATION=3000
20
21REQ=YES
22P12=NO
23DHP=NO
24
25PREFIX=$1
26if [ ".$PREFIX" = . ] ; then
27	echo No configuration prefix
28	NOTOK=1
29else
30    if [ ! -f $PREFIX-sv.prm ] ; then
31	echo No configuration file $PREFIX-sv.prm
32	NOTOK=1
33    fi
34fi
35
36CAPREFIX=$2
37if [ ".$CAPREFIX" = . ] ; then
38	echo No CA prefix
39	NOTOK=1
40else
41    if [ ! -f $CAPREFIX-ca.cacert ] ; then
42	echo No CA certficate file $CAPREFIX-ca.caert
43	NOTOK=1
44    fi
45    if [ ! -f $CAPREFIX-ca.key ] ; then
46	echo No $CAPREFIX key
47        NOTOK=1
48    fi
49fi
50
51if [ ".$NOTOK" != . ] ; then
52    echo "Sorry, I can't do that for you."
53    $USAGE
54    exit
55fi
56
57if [ ".$SERIAL" = . ] ; then
58	GETSERIAL="\$t = time ;\$d =  \$t . substr(\$t+$$ ,-4,4)-1;print \$d"
59	SERIAL=`/usr/bin/env perl -e "$GETSERIAL"`
60fi
61
62echo SERIAL=$SERIAL PREFIX=$PREFIX CAPREFIX=$CAPREFIX DURATION=$DURATION KEYSIZE=$KEYSIZE
63
64if [ "$DHP." = YES. ] ; then
65   echo "openssl dhparam -2 -out $PREFIX-sv.dhp $KEYSIZE"
66   $OPENSSL dhparam -2 -out $PREFIX-sv.dhp $KEYSIZE
67fi
68
69if [ "$REQ." = YES. ] ; then
70   echo "openssl req -config $PREFIX-sv.prm -newkey rsa:$KEYSIZE -keyout $PREFIX-sv.key -out $PREFIX-sv.csr -passout XXX"
71   $OPENSSL req -config $PREFIX-sv.prm -newkey rsa:$KEYSIZE -keyout $PREFIX-sv.key -out $PREFIX-sv.csr -passout pass:secret
72fi
73
74echo "openssl rsa -in $PREFIX-sv.key -out $PREFIX-sv.key"
75$OPENSSL rsa -in $PREFIX-sv.key -out $PREFIX-sv.key -passin pass:secret
76echo pseudo secrets generated
77
78echo "openssl x509 -set_serial $SERIAL -extfile $PREFIX-sv.prm -days $DURATION  -CA $CAPREFIX-ca.cacert -CAkey $CAPREFIX-ca.key -in $PREFIX-sv.csr -req -out $PREFIX-sv.crt -text -nameopt multiline -sha1"
79
80$OPENSSL x509 -set_serial $SERIAL -extfile $PREFIX-sv.prm -days $DURATION  -CA $CAPREFIX-ca.cacert -CAkey $CAPREFIX-ca.key -in $PREFIX-sv.csr -req -out $PREFIX-sv.crt -text -nameopt multiline -sha1
81
82if [ "$P12." = YES. ] ; then
83
84   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 "
85
86   $OPENSSL pkcs12 -export -des3 -out $PREFIX-sv.p12 -caname $CAPREFIX -name $PREFIX -inkey $PREFIX-sv.key -in $PREFIX-sv.crt -certfile $CAPREFIX-ca.crt
87fi
88
89echo "openssl x509 -noout -text -hash -in $PREFIX-sv.selfcert -nameopt multiline"
90$OPENSSL x509 -noout -text -hash -in $PREFIX-sv.crt -nameopt multiline
91
92# revoke server cert
93touch $CAPREFIX-ca.db
94echo 01 > $CAPREFIX-ca.cnt
95echo "openssl ca -config $CAPREFIX-ca.cnf -revoke $PREFIX-sv.crt"
96$OPENSSL ca -config $CAPREFIX-ca.cnf -revoke $PREFIX-sv.crt
97
98# issue CRL
99echo "openssl ca -config $CAPREFIX-ca.cnf -gencrl -out $PREFIX-sv.crl"
100$OPENSSL ca -config $CAPREFIX-ca.cnf -gencrl -out $PREFIX-sv.crl
101
102echo "openssl x509 -in $PREFIX-sv.crt -outform der -out $PREFIX-sv.der "
103$OPENSSL x509 -in $PREFIX-sv.crt -outform der -out $PREFIX-sv.der
104
105# all together now
106touch $PREFIX-sv.dhp
107cat $PREFIX-sv.prm $PREFIX-sv.key  $PREFIX-sv.crt $PREFIX-sv.dhp >$PREFIX-sv.pem
108chmod o-r $PREFIX-sv.prm
109
110echo "$PREFIX-sv.pem done"
111
112
113