#!/usr/bin/env bash
#
# Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

CA_NAME=${1:-ca}
SERVER_NAME=${2:-server}
CLIENT_NAME=${3:-client}
CERTS_DIR=${4-.}

cat > "$CERTS_DIR/$SERVER_NAME.conf" << EOF
[req]
req_extensions = v3_req
distinguished_name = req_distinguished_name
[req_distinguished_name]
[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS.1 = localhost
DNS.2 = $SERVER_NAME
IP.1 = 127.0.0.1
EOF

cat > "$CERTS_DIR/$CLIENT_NAME.conf" << EOF
[req]
req_extensions = v3_req
distinguished_name = req_distinguished_name
[req_distinguished_name]
[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = clientAuth
EOF

# Create a certificate authority
openssl genrsa -out "$CERTS_DIR/$CA_NAME.key" 2048 > /dev/null 2>&1
openssl req -x509 -new -nodes -key "$CERTS_DIR/$CA_NAME.key" -days 100000 -out "$CERTS_DIR/$CA_NAME.crt" -subj "/CN=$CA_NAME" > /dev/null 2>&1

# Create a server certiticate
openssl genrsa -out "$CERTS_DIR/$SERVER_NAME.key" 2048 > /dev/null 2>&1
openssl req -new -key "$CERTS_DIR/$SERVER_NAME.key" -out "$CERTS_DIR/$SERVER_NAME.csr" -subj "/CN=$SERVER_NAME" -config "$CERTS_DIR/$SERVER_NAME.conf" > /dev/null 2>&1
openssl x509 -req -in "$CERTS_DIR/$SERVER_NAME.csr" -CA "$CERTS_DIR/$CA_NAME.crt" -CAkey "$CERTS_DIR/$CA_NAME.key" -CAcreateserial -out "$CERTS_DIR/$SERVER_NAME.crt" -days 100000 -extensions v3_req -extfile "$CERTS_DIR/$SERVER_NAME.conf" > /dev/null 2>&1

# Create a client certiticate
openssl genrsa -out "$CERTS_DIR/$CLIENT_NAME.key" 2048 > /dev/null 2>&1
openssl req -new -key "$CERTS_DIR/$CLIENT_NAME.key" -out "$CERTS_DIR/$CLIENT_NAME.csr" -subj "/CN=$CLIENT_NAME" -config "$CERTS_DIR/$CLIENT_NAME.conf" > /dev/null 2>&1
openssl x509 -req -in "$CERTS_DIR/$CLIENT_NAME.csr" -CA "$CERTS_DIR/$CA_NAME.crt" -CAkey "$CERTS_DIR/$CA_NAME.key" -CAcreateserial -out "$CERTS_DIR/$CLIENT_NAME.crt" -days 100000 -extensions v3_req -extfile "$CERTS_DIR/$CLIENT_NAME.conf" > /dev/null 2>&1

# Clean up after we're done.
rm "$CERTS_DIR/$SERVER_NAME.csr" "$CERTS_DIR/$CLIENT_NAME.csr"
rm "$CERTS_DIR/$SERVER_NAME.conf" "$CERTS_DIR/$CLIENT_NAME.conf"

if [ -f "$CERTS_DIR/$CA_NAME.srl" ]; then
    rm "$CERTS_DIR/$CA_NAME.srl"
fi