/*
 * Copyright (C) 2006-2012 Free Software Foundation, Inc.
 * Author: Simon Josefsson, Howard Chu
 *
 * 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 <stdio.h>

#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#include "utils.h"

static char pem[] =
	"-----BEGIN CERTIFICATE-----\n"
	"MIIE6zCCA9OgAwIBAgIBdjANBgkqhkiG9w0BAQUFADBQMQswCQYDVQQGEwJTRTEf\n"
	"MB0GA1UEChMWU3RvY2tob2xtcyB1bml2ZXJzaXRldDEgMB4GA1UEAxMXU3RvY2to\n"
	"b2xtIFVuaXZlcnNpdHkgQ0EwHhcNMDYwMzIyMDkxNTI4WhcNMDcwMzIyMDkxNTI4\n"
	"WjBDMQswCQYDVQQGEwJTRTEfMB0GA1UEChMWU3RvY2tob2xtcyB1bml2ZXJzaXRl\n"
	"dDETMBEGA1UEAxMKc2lwMS5zdS5zZTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkC\n"
	"gYEArUzXTD36ZK7CwZJH/faUNTcdaqM7JyiZsfrO703d7cT/bJ3wKxT8trOOh/Ou\n"
	"WwgGFX2+r7ykun3aIUXUuD13Yle/yHqH/4g9vWX7UeFCBlSI0tAxnlqt0QqlPgSd\n"
	"GLHcoO4PPyjon9jj0A/zpJGZHiRUCooo63YqE9MYfr5HBfkCAwEAAaOCAl8wggJb\n"
	"MAsGA1UdDwQEAwIF4DAdBgNVHSUEFjAUBggrBgEFBQcDAQYIK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qYje7MCcN+ZR\n"
	"nUBo6U2NZ06/QEUFm+uUIhZ8IGM1gLehC7Q3G4+d4c38CDJxQnSPOgWiXuSvhhQm\n"
	"KDsbrKzRaeBRh5eEJbTkA8Dp0Emb0UrkRVhixeg97stxUcATAjdGljJ9MLnuHXnI\n"
	"7ihGdUfg5q/105vpsQpO\n"
	"-----END CERTIFICATE-----\n";

#define MAX_DATA_SIZE 1024

void doit(void)
{
	int ret;
	gnutls_datum_t derCert = { (void *)pem, sizeof(pem) };
	gnutls_x509_crt_t cert;
	size_t data_len = MAX_DATA_SIZE;
	char data[MAX_DATA_SIZE];
	unsigned int critical = 0;
	int alt_name_count = 0;

	ret = global_init();
	if (ret < 0)
		fail("init %d\n", ret);

	ret = gnutls_x509_crt_init(&cert);
	if (ret < 0)
		fail("crt_init %d\n", ret);

	ret = gnutls_x509_crt_import(cert, &derCert, GNUTLS_X509_FMT_PEM);
	if (ret < 0)
		fail("crt_import %d\n", ret);

	for (alt_name_count = 0;; ++alt_name_count) {
		ret = gnutls_x509_crt_get_issuer_alt_name(
			cert, alt_name_count, data, &data_len, &critical);
		if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
			break;

		if (ret < 0)
			fail("get_issuer_alt_name: %d\n", ret);

		// TODO: print out / check results
		if (GNUTLS_SAN_URI == ret) {
			if (strcmp(data, "http://ca.su.se") != 0) {
				fail("unexpected issuer GNUTLS_SAN_URI: %s\n",
				     data);
			}
		} else if (GNUTLS_SAN_RFC822NAME == ret) {
			if (strcmp(data, "ca@su.se") != 0) {
				fail("unexpected issuer GNUTLS_SAN_RFC822NAME: %s\n",
				     data);
			}
		} else {
			fail("unexpected alt name type: %d\n", ret);
		}
		data_len = MAX_DATA_SIZE;
	}

	if (alt_name_count != 2) {
		fail("unexpected number of alt names: %i\n", alt_name_count);
	}

	if (debug)
		success("done\n");

	gnutls_x509_crt_deinit(cert);
	gnutls_global_deinit();
}
