#!/bin/bash
#
# Helpers for TLS related config
#
# Copyright (C) 2018 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see .
#
tls_dir="${TEST_DIR}/tls"
tls_x509_cleanup()
{
    rm -f "${tls_dir}"/*.pem
    rm -f "${tls_dir}"/*/*.pem
    rmdir "${tls_dir}"/*
    rmdir "${tls_dir}"
}
tls_x509_init()
{
    (certtool --help) >/dev/null 2>&1 || \
	_notrun "certtool utility not found, skipping test"
    mkdir -p "${tls_dir}"
    # use a fixed key so we don't waste system entropy on
    # each test run
    cat > "${tls_dir}/key.pem" < "${tls_dir}/ca.info" <&1 | head -1
    rm -f "${tls_dir}/ca.info"
}
tls_x509_create_server()
{
    caname=$1
    name=$2
    mkdir -p "${tls_dir}/$name"
    cat > "${tls_dir}/cert.info" <&1 | head -1
    ln -s "${tls_dir}/$caname-cert.pem" "${tls_dir}/$name/ca-cert.pem"
    ln -s "${tls_dir}/key.pem" "${tls_dir}/$name/server-key.pem"
    rm -f "${tls_dir}/cert.info"
}
tls_x509_create_client()
{
    caname=$1
    name=$2
    mkdir -p "${tls_dir}/$name"
    cat > "${tls_dir}/cert.info" <&1 | head -1
    ln -s "${tls_dir}/$caname-cert.pem" "${tls_dir}/$name/ca-cert.pem"
    ln -s "${tls_dir}/key.pem" "${tls_dir}/$name/client-key.pem"
    rm -f "${tls_dir}/cert.info"
}