HOSTNAME env for Install
[aaf/authz.git] / conf / CA / manual.sh
1 #
2 # Initialize a manual Cert.  This is NOT entered in Certman Records
3 # $1 - CN (Common Name)
4 # $2 - FQI (Fully Qualified Identity)
5 # $3-$n - SANs (Service Alias Names)
6 #
7
8 if [ "$2" = "" ]; then
9   echo "FQI (Fully Qualified Identity): "
10   read FQI
11 fi
12
13 if [ "$1" = "" -o "$1" = "-local" ]; then
14   echo "Personal Certificate"
15   SUBJECT="/CN=$FQI/OU=V1`cat subject.aaf`"
16   NAME=$FQI
17 else
18   echo "Application Certificate"
19   SUBJECT="/CN=$1/OU=$FQI`cat subject.aaf`"
20   NAME=$1
21
22   if [ "$3" = "" ]; then
23     echo "Enter any SANS, delimited by spaces: "
24     read SANS
25   else
26     SANS=""
27     while [ ! "$3" = "" ]; do
28     SANS=${SANS}" "$3
29     shift
30     done
31   fi
32 fi
33
34 # Do SANs
35 if [ "$SANS" = "" ]; then
36    echo no SANS
37     if [ -e $NAME.san ]; then
38       rm $NAME.san
39     fi
40   else
41    echo some SANS: $SANS
42     cp ../san.conf $NAME.san
43     NUM=1
44     for D in $SANS; do
45         echo "DNS.$NUM = $D" >> $NAME.san
46               NUM=$((NUM+1))
47     done
48 fi
49
50 echo $SUBJECT
51
52 if [ ! -e $NAME.csr ]; then
53   if [ "$1" = "-local" ]; then
54         echo "IMPORTANT: If for any reason, you kill this process, type 'stty sane'"
55         echo "Enter the PassPhrase for the Key for $FQI: "
56         `stty -echo`
57         read PASSPHRASE
58         `stty echo`
59
60         # remove any previous Private key
61         rm private/$NAME.key
62         # Create regular rsa encrypted key
63         openssl req -new -newkey rsa:2048 -sha256 -keyout private/$NAME.key \
64           -out $NAME.csr -outform PEM -subj "$SUBJECT" \
65           -passout stdin  << EOF
66 $PASSPHRASE
67 EOF
68         chmod 400 private/$NAME.key
69   else
70         openssl req -newkey rsa:2048 -sha256 -keyout private/$NAME.key -out $NAME.csr -outform PEM -subj "$SUBJECT"
71         chmod 400 $NAME.key
72         echo "# All done, print result"
73         openssl req -verify -text -noout -in $NAME.csr
74   fi
75 fi
76
77   # Sign it
78   if [ -e $NAME.san ]; then
79     openssl ca -config ../openssl.conf -extensions server_cert -out certs/$NAME.crt \
80         -cert certs/ca.crt -keyfile private/ca.key \
81         -policy policy_loose \
82         -days 360 \
83         -extfile $NAME.san \
84         -infiles $NAME.csr
85   else
86     openssl ca -config ../openssl.conf -extensions server_cert -out certs/$NAME.crt \
87         -cert certs/ca.crt -keyfile private/ca.key \
88         -policy policy_loose \
89         -days 360 \
90         -infiles $NAME.csr
91   fi