cd53c9ee32546792c746ea77cd8a27b89a3b1ba5
[shibboleth/sp.git] / configs / keygen.sh
1 #! /bin/sh
2
3 # Added for Debian.  The upstream version is installed in /etc/shibboleth and
4 # for Debian we wanted to move it to /usr/bin, so change directories so that
5 # it puts files in the correct location.
6 cd /etc/shibboleth
7
8 while getopts h:e:y:bf c
9      do
10          case $c in
11            b)         BATCH=1;;
12            f)         FORCE=1;;
13            h)         FQDN=$OPTARG;;
14            e)         ENTITYID=$OPTARG;;
15            y)         YEARS=$OPTARG;;
16            \?)        echo keygen [-h hostname for cert] [-y years to issue cert] [-e entityID to embed in cert]
17                       exit 1;;
18          esac
19      done
20
21 if [ -n "$FORCE" ] ; then
22     rm sp-key.pem sp-cert.pem
23 fi
24
25 if  [ -s sp-key.pem -o -s sp-cert.pem ] ; then
26     if [ -z "$BATCH" ] ; then  
27         echo The files sp-key.pem and/or sp-cert.pem already exist!
28         echo Use -f option to force recreation of keypair.
29         exit 2
30     fi
31     exit 0
32 fi
33
34 # --fqdn flag added for Debian to generate better names for certificates.
35 if [ -z "$FQDN" ] ; then
36     FQDN=`hostname --fqdn`
37 fi
38
39 if [ -z "$YEARS" ] ; then
40     YEARS=10
41 fi
42
43 DAYS=`expr $YEARS \* 365`
44
45 if [ -z "$ENTITYID" ] ; then
46     ALTNAME=DNS:$FQDN
47 else
48     ALTNAME=DNS:$FQDN,URI:$ENTITYID
49 fi
50
51 cat >sp-cert.cnf <<EOF
52 # OpenSSL configuration file for creating sp-cert.pem
53 [req]
54 prompt=no
55 default_bits=2048
56 encrypt_key=no
57 default_md=sha1
58 distinguished_name=dn
59 # PrintableStrings only
60 string_mask=MASK:0002
61 x509_extensions=ext
62 [dn]
63 CN=$FQDN
64 [ext]
65 subjectAltName=$ALTNAME
66 subjectKeyIdentifier=hash
67 EOF
68
69 if [ -z "$BATCH" ] ; then
70     openssl req -config sp-cert.cnf -new -x509 -days $DAYS -keyout sp-key.pem -out sp-cert.pem
71 else
72     openssl req -config sp-cert.cnf -new -x509 -days $DAYS -keyout sp-key.pem -out sp-cert.pem 2> /dev/null
73 fi
74
75 rm sp-cert.cnf
76
77 if  [ -s sp-key.pem ] ; then
78     chmod 600 sp-key.pem
79 fi