changed to unmaintained
[aaf/authz.git] / conf / CA / bootstrap.sh
1 #!/bin/bash
2 #########
3 #  ============LICENSE_START====================================================
4 #  org.onap.aaf
5 #  ===========================================================================
6 #  Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
7 #  ===========================================================================
8 #  Licensed under the Apache License, Version 2.0 (the "License");
9 #  you may not use this file except in compliance with the License.
10 #  You may obtain a copy of the License at
11 #
12 #       http://www.apache.org/licenses/LICENSE-2.0
13 #
14 #  Unless required by applicable law or agreed to in writing, software
15 #  distributed under the License is distributed on an "AS IS" BASIS,
16 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 #  See the License for the specific language governing permissions and
18 #  limitations under the License.
19 #  ============LICENSE_END====================================================
20 #
21 # Streamlined AAF Bootstrap initial Cert
22 # Removed Variables so it can be run for AutoDeployments
23 #
24 echo "Bootstrap AAF Certificate"
25 mkdir -p private certs newcerts
26 chmod 700 private
27 chmod 755 certs newcerts
28 touch index.txt
29 echo "unique_subject = no" > index.txt.attr
30 if [ ! -e ./serial ]; then
31   echo $(date +%s)_$(shuf -i 0-1000000 -n 1)  > ./serial
32 fi
33
34 NAME=aaf.bootstrap
35 HOSTNAME="${HOSTNAME:=$(hostname -)}"
36 PUBLIC_FQDN="${aaf_locator_public_fqdn:=$HOSTNAME}"
37 FQDN="${aaf_locator_fqdn:=$PUBLIC_FQDN}"
38 FQI=aaf@aaf.osaaf.org
39 SUBJECT="/CN=$FQDN/OU=$FQI`cat subject.aaf`"
40 SIGNER_P12=$1
41 SIGNER_KEY=/tmp/aaf_signer.key
42 SIGNER_CRT=/tmp/aaf_signer.crt
43 PASSPHRASE=$2
44 if [ "PASSPHRASE" = "" ]; then
45   PASSPHRASE="something easy"
46 fi
47 BOOTSTRAP_SAN=/tmp/$NAME.san
48 BOOTSTRAP_KEY=/tmp/$NAME.key
49 BOOTSTRAP_CSR=/tmp/$NAME.csr
50 BOOTSTRAP_CRT=/tmp/$NAME.crt
51 BOOTSTRAP_CHAIN=/tmp/$NAME.chain
52 BOOTSTRAP_P12=$NAME.p12
53 BOOTSTRAP_ISSUER=$NAME.issuer
54
55
56 # If Signer doesn't exist, create Self-Signed CA
57 if [ ! -e "$SIGNER_P12"  ]; then
58   # Creating Signer CA
59   openssl req -config openssl.conf -x509 -sha256 -extensions v3_ca \
60     -newkey rsa:4096 -subj /CN="Signer$(cat subject.aaf)" \
61     -keyout $SIGNER_KEY -out $SIGNER_CRT -days 365 -passout stdin << EOF
62 $PASSPHRASE
63 EOF
64
65   # Move to P12 (Signer)
66   openssl pkcs12 -name RootCA -export -in $SIGNER_CRT -inkey $SIGNER_KEY -out $SIGNER_P12 -passin stdin -passout stdin << EOF
67 $PASSPHRASE
68 $PASSPHRASE
69 $PASSPHRASE
70 EOF
71
72 else
73   # Get Private key from P12
74   openssl pkcs12 -in $SIGNER_P12 -nocerts -nodes -passin stdin -passout stdin -out $SIGNER_KEY << EOF
75 $PASSPHRASE
76 $PASSPHRASE
77 EOF
78
79   # Get Cert from P12
80   openssl pkcs12 -in $SIGNER_P12 -clcerts -nokeys -passin stdin -out $SIGNER_CRT << EOF
81 $PASSPHRASE
82 EOF
83
84 fi
85
86 # SANS
87 cp san.conf $BOOTSTRAP_SAN
88 SANS=$FQDN
89 if [ "$FQDN" -ne "$HOSTNAME" ]; then
90   SANS="$SANS $HOSTNAME"
91 fi
92
93 for ROOT in $(cat san_root.aaf); do
94    SANS="$SANS $ROOT"
95    for C in service locate oauth token introspect gui cm hello; do
96      SANS="$SANS $C.$ROOT"
97    done
98 done
99
100 for C in service locate oauth token introspect gui cm hello; do
101    SANS="$SANS aaf-$C"
102    SANS="$SANS aaf-$C.onap"
103 done
104
105 NUM=1
106 for D in $SANS; do
107     echo "DNS.$NUM = $D" >> $BOOTSTRAP_SAN
108     NUM=$((NUM+1))
109 done
110
111 # Create CSR
112 openssl req -new -newkey rsa:2048 -nodes -keyout $BOOTSTRAP_KEY \
113         -out $BOOTSTRAP_CSR -outform PEM -subj "$SUBJECT" \
114         -passout stdin  << EOF
115 $PASSPHRASE
116 EOF
117
118 echo Sign it
119 openssl ca -batch -config openssl.conf -extensions server_cert \
120         -cert $SIGNER_CRT -keyfile $SIGNER_KEY \
121         -policy policy_loose \
122         -days 365 \
123         -passin stdin \
124         -out $BOOTSTRAP_CRT \
125         -extfile $BOOTSTRAP_SAN \
126         -infiles $BOOTSTRAP_CSR << EOF
127 $PASSPHRASE
128 EOF
129
130 # Make a P12
131 # Add THIS Intermediate CA into chain
132 cat $BOOTSTRAP_CRT
133 cp $BOOTSTRAP_CRT $BOOTSTRAP_CHAIN
134 cat $SIGNER_CRT >> $BOOTSTRAP_CHAIN
135 cat $BOOTSTRAP_CHAIN
136
137 # Note: Openssl will pickup and load all Certs in the Chain file
138 #openssl pkcs12 -name $FQI -export -in $BOOTSTRAP_CRT -inkey $BOOTSTRAP_KEY -CAfile $SIGNER_CRT -out $BOOTSTRAP_P12 -passin stdin -passout stdin << EOF
139 openssl pkcs12 -name $FQI -export -in $BOOTSTRAP_CHAIN -inkey $BOOTSTRAP_KEY -out $BOOTSTRAP_P12 -passin stdin -passout stdin << EOF
140 $PASSPHRASE
141 $PASSPHRASE
142 $PASSPHRASE
143 EOF
144
145 # Make Issuer name
146 ISSUER=$(openssl x509 -subject -noout -in $SIGNER_CRT | cut -c 9- | sed -e 's/ = /=/g' -e 's/\//, /g')
147 for I in $ISSUER; do
148   if [ -z "$REVERSE" ]; then
149     REVERSE="${I%,}"
150   else
151     REVERSE="${I%,}, ${REVERSE}"
152   fi
153 done
154 echo "$REVERSE" > $BOOTSTRAP_ISSUER
155
156 # Cleanup
157 rm -f $BOOTSTRAP_SAN $BOOTSTRAP_KEY $BOOTSTRAP_CSR $BOOTSTRAP_CRT $SIGNER_KEY $SIGNER_CRT $BOOTSTRAP_CHAIN