Instrumental | 7a1817b | 2018-11-05 11:11:15 -0600 | [diff] [blame] | 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==================================================== |
Instrumental | cc596dd | 2018-08-23 09:52:14 -0500 | [diff] [blame] | 20 | # |
| 21 | # Streamlined AAF Bootstrap initial Cert |
| 22 | # Removed Variables so it can be run for AutoDeployments |
| 23 | # |
| 24 | echo "Bootstrap AAF Certificate" |
Instrumental | b8a8129 | 2018-08-23 16:32:45 -0500 | [diff] [blame] | 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 |
Instrumental | 0d4ec12 | 2018-08-30 14:33:08 -0500 | [diff] [blame] | 30 | if [ ! -e ./serial ]; then |
Instrumental | 93871ff | 2018-10-15 07:37:28 -0500 | [diff] [blame] | 31 | echo $(date +%s)_$(shuf -i 0-1000000 -n 1) > ./serial |
Instrumental | 0d4ec12 | 2018-08-30 14:33:08 -0500 | [diff] [blame] | 32 | fi |
Instrumental | b8a8129 | 2018-08-23 16:32:45 -0500 | [diff] [blame] | 33 | |
Instrumental | cc596dd | 2018-08-23 09:52:14 -0500 | [diff] [blame] | 34 | NAME=aaf.bootstrap |
Instrumental | 27afb02 | 2019-02-07 16:36:56 -0600 | [diff] [blame] | 35 | HOSTNAME="${HOSTNAME:=$(hostname -)}" |
| 36 | FQDN="${aaf_locator_fqdn:=$HOSTNAME}" |
Instrumental | cc596dd | 2018-08-23 09:52:14 -0500 | [diff] [blame] | 37 | FQI=aaf@aaf.osaaf.org |
| 38 | SUBJECT="/CN=$FQDN/OU=$FQI`cat subject.aaf`" |
| 39 | SIGNER_P12=$1 |
| 40 | SIGNER_KEY=/tmp/aaf_signer.key |
| 41 | SIGNER_CRT=/tmp/aaf_signer.crt |
| 42 | PASSPHRASE=$2 |
| 43 | if [ "PASSPHRASE" = "" ]; then |
| 44 | PASSPHRASE="something easy" |
| 45 | fi |
| 46 | BOOTSTRAP_SAN=/tmp/$NAME.san |
| 47 | BOOTSTRAP_KEY=/tmp/$NAME.key |
| 48 | BOOTSTRAP_CSR=/tmp/$NAME.csr |
| 49 | BOOTSTRAP_CRT=/tmp/$NAME.crt |
Instrumental | b8a8129 | 2018-08-23 16:32:45 -0500 | [diff] [blame] | 50 | BOOTSTRAP_CHAIN=/tmp/$NAME.chain |
Instrumental | cc596dd | 2018-08-23 09:52:14 -0500 | [diff] [blame] | 51 | BOOTSTRAP_P12=$NAME.p12 |
Instrumental | bc299c0 | 2018-09-25 06:42:31 -0500 | [diff] [blame] | 52 | BOOTSTRAP_ISSUER=$NAME.issuer |
Instrumental | cc596dd | 2018-08-23 09:52:14 -0500 | [diff] [blame] | 53 | |
| 54 | |
| 55 | # If Signer doesn't exist, create Self-Signed CA |
| 56 | if [ ! -e "$SIGNER_P12" ]; then |
| 57 | # Creating Signer CA |
| 58 | openssl req -config openssl.conf -x509 -sha256 -extensions v3_ca \ |
Instrumental | b8a8129 | 2018-08-23 16:32:45 -0500 | [diff] [blame] | 59 | -newkey rsa:4096 -subj /CN="Signer$(cat subject.aaf)" \ |
| 60 | -keyout $SIGNER_KEY -out $SIGNER_CRT -days 365 -passout stdin << EOF |
| 61 | $PASSPHRASE |
| 62 | EOF |
Instrumental | cc596dd | 2018-08-23 09:52:14 -0500 | [diff] [blame] | 63 | |
| 64 | # Move to P12 (Signer) |
| 65 | openssl pkcs12 -name RootCA -export -in $SIGNER_CRT -inkey $SIGNER_KEY -out $SIGNER_P12 -passin stdin -passout stdin << EOF |
| 66 | $PASSPHRASE |
| 67 | $PASSPHRASE |
| 68 | $PASSPHRASE |
| 69 | EOF |
| 70 | |
| 71 | else |
| 72 | # Get Private key from P12 |
| 73 | openssl pkcs12 -in $SIGNER_P12 -nocerts -nodes -passin stdin -passout stdin -out $SIGNER_KEY << EOF |
| 74 | $PASSPHRASE |
| 75 | $PASSPHRASE |
| 76 | EOF |
| 77 | |
| 78 | # Get Cert from P12 |
| 79 | openssl pkcs12 -in $SIGNER_P12 -clcerts -nokeys -passin stdin -out $SIGNER_CRT << EOF |
| 80 | $PASSPHRASE |
| 81 | EOF |
| 82 | |
| 83 | fi |
| 84 | |
| 85 | # SANS |
| 86 | cp san.conf $BOOTSTRAP_SAN |
Instrumental | 1e3be60 | 2018-10-03 19:40:44 -0500 | [diff] [blame] | 87 | SANS=$FQDN |
| 88 | if [ "$FQDN" -ne "$HOSTNAME" ]; then |
| 89 | SANS="$SANS $HOSTNAME" |
| 90 | fi |
| 91 | |
| 92 | for ROOT in $(cat san_root.aaf); do |
| 93 | SANS="$SANS $ROOT" |
Instrumental | 12414fe | 2019-01-22 10:27:32 -0600 | [diff] [blame] | 94 | for C in service locate oauth token introspect gui cm hello; do |
Instrumental | 1e3be60 | 2018-10-03 19:40:44 -0500 | [diff] [blame] | 95 | SANS="$SANS $C.$ROOT" |
| 96 | done |
| 97 | done |
Instrumental | 65cdc09 | 2018-10-15 12:34:59 -0500 | [diff] [blame] | 98 | |
Instrumental | 12414fe | 2019-01-22 10:27:32 -0600 | [diff] [blame] | 99 | for C in service locate oauth token introspect gui cm hello; do |
Instrumental | 65cdc09 | 2018-10-15 12:34:59 -0500 | [diff] [blame] | 100 | SANS="$SANS aaf-$C" |
| 101 | SANS="$SANS aaf-$C.onap" |
| 102 | done |
| 103 | |
Instrumental | cc596dd | 2018-08-23 09:52:14 -0500 | [diff] [blame] | 104 | NUM=1 |
Instrumental | 1e3be60 | 2018-10-03 19:40:44 -0500 | [diff] [blame] | 105 | for D in $SANS; do |
Instrumental | cc596dd | 2018-08-23 09:52:14 -0500 | [diff] [blame] | 106 | echo "DNS.$NUM = $D" >> $BOOTSTRAP_SAN |
| 107 | NUM=$((NUM+1)) |
| 108 | done |
| 109 | |
| 110 | # Create CSR |
Instrumental | b8a8129 | 2018-08-23 16:32:45 -0500 | [diff] [blame] | 111 | openssl req -new -newkey rsa:2048 -nodes -keyout $BOOTSTRAP_KEY \ |
| 112 | -out $BOOTSTRAP_CSR -outform PEM -subj "$SUBJECT" \ |
| 113 | -passout stdin << EOF |
| 114 | $PASSPHRASE |
| 115 | EOF |
Instrumental | cc596dd | 2018-08-23 09:52:14 -0500 | [diff] [blame] | 116 | |
Instrumental | b8a8129 | 2018-08-23 16:32:45 -0500 | [diff] [blame] | 117 | echo Sign it |
| 118 | openssl ca -batch -config openssl.conf -extensions server_cert \ |
Instrumental | cc596dd | 2018-08-23 09:52:14 -0500 | [diff] [blame] | 119 | -cert $SIGNER_CRT -keyfile $SIGNER_KEY \ |
| 120 | -policy policy_loose \ |
Instrumental | 08e9340 | 2018-10-03 08:38:52 -0500 | [diff] [blame] | 121 | -days 365 \ |
Instrumental | b8a8129 | 2018-08-23 16:32:45 -0500 | [diff] [blame] | 122 | -passin stdin \ |
| 123 | -out $BOOTSTRAP_CRT \ |
Instrumental | cc596dd | 2018-08-23 09:52:14 -0500 | [diff] [blame] | 124 | -extfile $BOOTSTRAP_SAN \ |
Instrumental | b8a8129 | 2018-08-23 16:32:45 -0500 | [diff] [blame] | 125 | -infiles $BOOTSTRAP_CSR << EOF |
| 126 | $PASSPHRASE |
| 127 | EOF |
Instrumental | cc596dd | 2018-08-23 09:52:14 -0500 | [diff] [blame] | 128 | |
| 129 | # Make a P12 |
| 130 | # Add THIS Intermediate CA into chain |
Instrumental | b8a8129 | 2018-08-23 16:32:45 -0500 | [diff] [blame] | 131 | cat $BOOTSTRAP_CRT |
| 132 | cp $BOOTSTRAP_CRT $BOOTSTRAP_CHAIN |
| 133 | cat $SIGNER_CRT >> $BOOTSTRAP_CHAIN |
Instrumental | 08e9340 | 2018-10-03 08:38:52 -0500 | [diff] [blame] | 134 | cat $BOOTSTRAP_CHAIN |
Instrumental | cc596dd | 2018-08-23 09:52:14 -0500 | [diff] [blame] | 135 | |
| 136 | # Note: Openssl will pickup and load all Certs in the Chain file |
Instrumental | 08e9340 | 2018-10-03 08:38:52 -0500 | [diff] [blame] | 137 | #openssl pkcs12 -name $FQI -export -in $BOOTSTRAP_CRT -inkey $BOOTSTRAP_KEY -CAfile $SIGNER_CRT -out $BOOTSTRAP_P12 -passin stdin -passout stdin << EOF |
Instrumental | b8a8129 | 2018-08-23 16:32:45 -0500 | [diff] [blame] | 138 | openssl pkcs12 -name $FQI -export -in $BOOTSTRAP_CHAIN -inkey $BOOTSTRAP_KEY -out $BOOTSTRAP_P12 -passin stdin -passout stdin << EOF |
Instrumental | cc596dd | 2018-08-23 09:52:14 -0500 | [diff] [blame] | 139 | $PASSPHRASE |
| 140 | $PASSPHRASE |
| 141 | $PASSPHRASE |
| 142 | EOF |
| 143 | |
Instrumental | bc299c0 | 2018-09-25 06:42:31 -0500 | [diff] [blame] | 144 | # Make Issuer name |
| 145 | ISSUER=$(openssl x509 -subject -noout -in $SIGNER_CRT | cut -c 10-) |
| 146 | for I in ${ISSUER//\// }; do |
| 147 | if [ -n "$CADI_X509_ISSUER" ]; then |
| 148 | CADI_X509_ISSUER=", $CADI_X509_ISSUER" |
| 149 | fi |
| 150 | CADI_X509_ISSUER="$I$CADI_X509_ISSUER" |
| 151 | done |
| 152 | echo $CADI_X509_ISSUER > $BOOTSTRAP_ISSUER |
| 153 | |
Instrumental | cc596dd | 2018-08-23 09:52:14 -0500 | [diff] [blame] | 154 | # Cleanup |
Instrumental | 08e9340 | 2018-10-03 08:38:52 -0500 | [diff] [blame] | 155 | rm -f $BOOTSTRAP_SAN $BOOTSTRAP_KEY $BOOTSTRAP_CSR $BOOTSTRAP_CRT $SIGNER_KEY $SIGNER_CRT $BOOTSTRAP_CHAIN |