Blame tests/priorities.c

Packit aea12f
/*
Packit aea12f
 * Copyright (C) 2012 Free Software Foundation, Inc.
Packit aea12f
 * Copyright (C) 2012-2015 Nikos Mavrogiannopoulos
Packit aea12f
 *
Packit aea12f
 * Author: Nikos Mavrogiannopoulos
Packit aea12f
 *
Packit aea12f
 * This file is part of GnuTLS.
Packit aea12f
 *
Packit aea12f
 * GnuTLS is free software; you can redistribute it and/or modify it
Packit aea12f
 * under the terms of the GNU General Public License as published by
Packit aea12f
 * the Free Software Foundation; either version 3 of the License, or
Packit aea12f
 * (at your option) any later version.
Packit aea12f
 *
Packit aea12f
 * GnuTLS is distributed in the hope that it will be useful, but
Packit aea12f
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit aea12f
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit aea12f
 * General Public License for more details.
Packit aea12f
 *
Packit aea12f
 * You should have received a copy of the GNU General Public License
Packit aea12f
 * along with GnuTLS; if not, write to the Free Software Foundation,
Packit aea12f
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
Packit aea12f
 */
Packit aea12f
Packit aea12f
#ifdef HAVE_CONFIG_H
Packit aea12f
#include <config.h>
Packit aea12f
#endif
Packit aea12f
Packit aea12f
#include <stdio.h>
Packit aea12f
#include <stdlib.h>
Packit aea12f
#include <string.h>
Packit aea12f
#include <gnutls/gnutls.h>
Packit aea12f
Packit aea12f
#include "utils.h"
Packit aea12f
Packit aea12f
static void
Packit aea12f
try_prio(const char *prio, unsigned expected_cs, unsigned expected_ciphers, unsigned line)
Packit aea12f
{
Packit aea12f
	int ret;
Packit aea12f
	gnutls_priority_t p;
Packit aea12f
	const char *err;
Packit aea12f
	const unsigned int *t;
Packit aea12f
	unsigned i, si, count = 0;
Packit aea12f
Packit aea12f
	/* this must be called once in the program
Packit aea12f
	 */
Packit aea12f
	global_init();
Packit aea12f
Packit aea12f
	ret = gnutls_priority_init(&p, prio, &err;;
Packit aea12f
	if (ret < 0) {
Packit aea12f
		fprintf(stderr, "error: %s: %s\n", gnutls_strerror(ret),
Packit aea12f
			err);
Packit aea12f
		exit(1);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	for (i = 0;; i++) {
Packit aea12f
		ret = gnutls_priority_get_cipher_suite_index(p, i, &si);
Packit aea12f
		if (ret == GNUTLS_E_UNKNOWN_CIPHER_SUITE)
Packit aea12f
			continue;
Packit aea12f
		else if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
Packit aea12f
			break;
Packit aea12f
		else if (ret == 0) {
Packit aea12f
			count++;
Packit aea12f
			/* fprintf(stderr, "%s\n", gnutls_cipher_suite_info(si, NULL, NULL, NULL, NULL, NULL)); */
Packit aea12f
		}
Packit aea12f
Packit aea12f
	}
Packit aea12f
Packit aea12f
	ret = gnutls_priority_cipher_list(p, &t);
Packit aea12f
	if ((unsigned) ret != expected_ciphers) {
Packit aea12f
#if 0
Packit aea12f
		for (i = 0; i < ret; i++)
Packit aea12f
			fprintf(stderr, "%s\n",
Packit aea12f
				gnutls_cipher_get_name(t[i]));
Packit aea12f
#endif
Packit aea12f
		fail("%s:%d: expected %d ciphers, found %d\n", prio, line, expected_ciphers,
Packit aea12f
		     ret);
Packit aea12f
		exit(1);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	gnutls_priority_deinit(p);
Packit aea12f
Packit aea12f
	/* fprintf(stderr, "count: %d\n", count); */
Packit aea12f
Packit aea12f
	if (debug)
Packit aea12f
		success("finished: %s\n", prio);
Packit aea12f
Packit aea12f
	if (count != expected_cs) {
Packit aea12f
		fail("%s:%d: expected %d ciphersuites, found %d\n", prio, line, expected_cs,
Packit aea12f
		     count);
Packit aea12f
		exit(1);
Packit aea12f
	}
Packit aea12f
}
Packit aea12f
Packit aea12f
static void
Packit aea12f
try_prio_err(const char *prio, int err)
Packit aea12f
{
Packit aea12f
	int ret;
Packit aea12f
	gnutls_priority_t p;
Packit aea12f
Packit aea12f
	ret = gnutls_priority_init(&p, prio, NULL);
Packit aea12f
	if (ret < 0 && ret != err) {
Packit aea12f
		fprintf(stderr, "error: %s\n", gnutls_strerror(ret));
Packit aea12f
		exit(1);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	if (ret >= 0)
Packit aea12f
		gnutls_priority_deinit(p);
Packit aea12f
Packit aea12f
	if (debug)
Packit aea12f
		success("finished: %s\n", prio);
Packit aea12f
}
Packit aea12f
Packit aea12f
Packit aea12f
void doit(void)
Packit aea12f
{
Packit aea12f
	const int null = 3;
Packit aea12f
	int sec128_cs = 29;
Packit aea12f
	int sec256_cs = 12;
Packit aea12f
	int normal_cs = 29;
Packit aea12f
	int pfs_cs = 23;
Packit aea12f
	int null_normal_cs = 28; /* disables TLS1.3 CS */
Packit aea12f
	int normal_ciphers = 7;
Packit aea12f
Packit aea12f
	if (gnutls_fips140_mode_enabled()) {
Packit aea12f
		normal_cs = 25;
Packit aea12f
		normal_ciphers = 6;
Packit aea12f
		pfs_cs = 25;
Packit aea12f
		sec256_cs = 8;
Packit aea12f
		sec128_cs = 25;
Packit aea12f
	}
Packit aea12f
Packit aea12f
	try_prio("NORMAL", normal_cs, normal_ciphers, __LINE__);
Packit aea12f
	try_prio("NORMAL:-MAC-ALL:+MD5:+MAC-ALL", normal_cs, normal_ciphers, __LINE__);
Packit aea12f
Packit aea12f
	if (!gnutls_fips140_mode_enabled()) {
Packit aea12f
		try_prio("PFS", pfs_cs, normal_ciphers, __LINE__);
Packit aea12f
		try_prio("NORMAL:+CIPHER-ALL", normal_cs, 7, __LINE__);	/* all (except null) */
Packit aea12f
		try_prio("NORMAL:-CIPHER-ALL:+NULL", null, 1, __LINE__);	/* null */
Packit aea12f
		try_prio("NORMAL:-CIPHER-ALL:+NULL:+CIPHER-ALL", null_normal_cs, 8, __LINE__);	/* should be null + all */
Packit aea12f
		try_prio("NORMAL:-CIPHER-ALL:+NULL:+CIPHER-ALL:-CIPHER-ALL:+AES-128-CBC", 4, 1, __LINE__);	/* should be null + all */
Packit Service 991b93
#ifdef ENABLE_GOST
Packit Service 991b93
		try_prio("NONE:+VERS-TLS1.2:+GOST", 1, 1, __LINE__);
Packit Service 991b93
#endif
Packit aea12f
	}
Packit aea12f
Packit aea12f
	try_prio("PERFORMANCE", normal_cs, normal_ciphers, __LINE__);
Packit aea12f
	try_prio("SECURE256", sec256_cs, 4, __LINE__);
Packit aea12f
	try_prio("SECURE128", sec128_cs, 7, __LINE__);
Packit aea12f
	try_prio("SECURE128:+SECURE256", sec128_cs, 7, __LINE__);	/* should be the same as SECURE128 */
Packit aea12f
	try_prio("SECURE128:+SECURE256:+NORMAL", normal_cs, 7, __LINE__);	/* should be the same as NORMAL */
Packit aea12f
	try_prio("SUITEB192", 1, 1, __LINE__);
Packit aea12f
	try_prio("SUITEB128", 2, 2, __LINE__);
Packit aea12f
	/* check legacy strings */
Packit aea12f
	try_prio("NORMAL:+RSA-EXPORT:+ARCFOUR-40", normal_cs, normal_ciphers, __LINE__);
Packit aea12f
Packit aea12f
	try_prio_err("NORMAL:-VERS-ALL:+VERS-TLS1.2:-SIGN-ALL:+SIGN-ECDSA-SECP256R1-SHA256", GNUTLS_E_NO_PRIORITIES_WERE_SET);
Packit aea12f
	try_prio_err("NORMAL:-VERS-ALL:+VERS-TLS1.2:-SIGN-ALL", GNUTLS_E_NO_PRIORITIES_WERE_SET);
Packit aea12f
	try_prio_err("NORMAL:-VERS-ALL:+VERS-DTLS1.2:-SIGN-ALL", GNUTLS_E_NO_PRIORITIES_WERE_SET);
Packit aea12f
}