#!/bin/sh

# Copyright (C) 2014 Nikos Mavrogiannopoulos
#
# This file is part of GnuTLS.
#
# GnuTLS 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 3 of the License, or (at
# your option) any later version.
#
# GnuTLS 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 GnuTLS; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

#set -e

srcdir="${srcdir:-.}"
CERTTOOL="${CERTTOOL:-../../src/certtool${EXEEXT}}"
DIFF="${DIFF:-diff}"
OUTFILE=provable-privkey.tmp

if ! test -z "${VALGRIND}"; then
	VALGRIND="${LIBTOOL:-libtool} --mode=execute ${VALGRIND}"
fi

#RSA keys
${VALGRIND} "${CERTTOOL}" --verify-provable-privkey --load-privkey "${srcdir}/data/provable2048.pem"
rc=$?

if test "${rc}" != "0"; then
	echo "Could not verify the 2048-bit key"
	exit 1
fi

${VALGRIND} "${CERTTOOL}" --verify-provable-privkey --load-privkey "${srcdir}/data/provable3072.pem"
rc=$?

if test "${rc}" != "0"; then
	echo "Could not verify the 3072-bit key"
	exit 1
fi

${VALGRIND} "${CERTTOOL}" --generate-privkey --provable --bits 2048 --seed "AF:BF:D6:96:BA:5D:05:E3:78:A9:4B:BF:E2:95:BA:F9:94:AC:B8:7F:BC:C8:ED:FF:7A:48:EE:4F" --outfile $OUTFILE
rc=$?

if test "${rc}" != "0"; then
	echo "Could not generate a 2048-bit key"
	exit 1
fi

${VALGRIND} "${CERTTOOL}" --verify-provable-privkey --load-privkey "$OUTFILE"
rc=$?

if test "${rc}" != "0"; then
	echo "Could not verify the generated key 1"
	exit 1
fi

${VALGRIND} "${CERTTOOL}" --verify-provable-privkey --load-privkey "$OUTFILE" --seed "AF:BF:D6:96:BA:5D:05:E3:78:A9:4B:BF:E2:95:BA:F9:94:AC:B8:7F:BC:C8:ED:FF:7A:48:EE:4F"
rc=$?

if test "${rc}" != "0"; then
	echo "Could not verify the generated key 2"
	exit 1
fi

${VALGRIND} "${CERTTOOL}" --generate-privkey --provable --seed "A3:54:5C:B3:1D:70:56:1C:A0:BD:2C:C8:78:C1:9C:56:CD:69:75:50:0C:3A:FD:BF:E8:96:83:FA:52:BC:98:C5" --outfile $OUTFILE
rc=$?

if test "${rc}" != "0"; then
	echo "Could not generate a default key"
	exit 1
fi

if test "${FIPS140}" = 1;then
SEED="30:EC:33:4F:97:DB:C0:BA:9C:86:52:A7:B5:D3:F7:B2:DB:BB:48:A4:84:2E:19:0D:21:0E:01:DA:BD:53:59:81:50:37:55:EE:96:A2:70:A5:98:E9:D9:1B:22:54:66:91:69:EB:DF:45:99:D9:F7:2A:CA"
DSAFILE=provable-dsa2048-fips.pem
else
SEED="5A:0E:A0:41:77:9B:0A:B7:65:BE:25:09:C4:DE:90:E5:A0:E7:DA:AD:AE:6E:49:D3:59:38:F9:13:33:A8:E1:FE:50:9D:D2:DF:E1:96:7C:D0:04:54:28:10:34:97:D0:03:88:C8:CE:36:29:0F:E9:37:9F:80:03:CB:F8:FD:A4:DA:27"
DSAFILE=provable-dsa2048.pem
fi

#DSA keys
${VALGRIND} "${CERTTOOL}" --verify-provable-privkey --load-privkey "${srcdir}/data/${DSAFILE}"
rc=$?

if test "${rc}" != "0"; then
	echo "Could not verify the 2048-bit DSA key"
	exit 1
fi

${VALGRIND} "${CERTTOOL}" --generate-privkey --provable --bits 2048 --dsa --seed "$SEED" --outfile "$OUTFILE"
rc=$?

if test "${rc}" != "0"; then
	echo "Could not generate a 2048-bit DSA key"
	exit 1
fi

${VALGRIND} "${CERTTOOL}" --verify-provable-privkey --load-privkey "$OUTFILE"
rc=$?

if test "${rc}" != "0"; then
	echo "Could not verify the generated key 1"
	exit 1
fi

${VALGRIND} "${CERTTOOL}" --verify-provable-privkey --load-privkey "$OUTFILE" --seed "$SEED"
rc=$?

if test "${rc}" != "0"; then
	echo "Could not verify the generated key 2"
	exit 1
fi

rm -f "$OUTFILE"

exit 0
