/*
 * Copyright (C) 2022 Red Hat, Inc.
 *
 * Author: Zoltan Fridrich
 *
 * 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, see <https://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gnutls/x509.h>

#include "utils.h"

#define CHECK(X)                                            \
	{                                                   \
		r = X;                                      \
		if (r < 0)                                  \
			fail("error in %d: %s\n", __LINE__, \
			     gnutls_strerror(r));           \
	}

static char cert_pem[] =
	"-----BEGIN CERTIFICATE-----\n"
	"MIIFLzCCBBegAwIBAgISAycvItcPAZ5yClzMOYYcod4cMA0GCSqGSIb3DQEBCwUA\n"
	"MDIxCzAJBgNVBAYTAlVTMRYwFAYDVQQKEw1MZXQncyBFbmNyeXB0MQswCQYDVQQD\n"
	"EwJSMzAeFw0yMjA4MjMwNjMzMjlaFw0yMjExMjEwNjMzMjhaMBcxFTATBgNVBAMT\n"
	"DHZvaWRwb2ludC5pbzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANSt\n"
	"AazUWttuU/swyEdt70bpod6knYDJavnFUwicpT4ZfPh84Y2ci9Ay9oTVR8LzVq+o\n"
	"3FIGxXlBFhCtoGA5k5Soao/JB40+gsY+O8LgcNAdejU78m5W4e2qXq4eu/4tFUCw\n"
	"GkcRmqitnc5Jy0bEM+wCZKa42Lx0+WAhNRd/70yWIbzXOrXDnLgGc221JeYJ4it0\n"
	"ajYcf3AZuSHhL3qsTLLzuYorPqWmDy27psUiDDJOIjxVbBCRL+AY40TsQm7CZZhZ\n"
	"8sCkZU7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UTLS_VERIFY_DO_NOT_ALLOW_SAME |
			 GNUTLS_VERIFY_DISABLE_TIME_CHECKS;
	gnutls_x509_trust_list_t tl;
	gnutls_x509_crt_t *certs = NULL;
	gnutls_datum_t cert = { (unsigned char *)cert_pem,
				sizeof(cert_pem) - 1 };

	CHECK(gnutls_x509_crt_list_import2(&certs, &certs_size, &cert,
					   GNUTLS_X509_FMT_PEM, 0));
	CHECK(gnutls_x509_trust_list_init(&tl, 0));
	CHECK(gnutls_x509_trust_list_add_cas(tl, certs + certs_size - 1, 1, 0));
	CHECK(gnutls_x509_trust_list_verify_crt(tl, certs, certs_size, flags,
						&out, NULL));

	if (out)
		fail("Not verified\n");

	gnutls_x509_trust_list_deinit(tl, 0);
	for (i = 0; i < certs_size; ++i)
		gnutls_x509_crt_deinit(certs[i]);
	gnutls_free(certs);
}
