Blame tests/sign-verify-ext4.c

Packit Service 4684c1
/*
Packit Service 4684c1
 * Copyright (C) 2017 Red Hat, Inc.
Packit Service 4684c1
 *
Packit Service 4684c1
 * Author: Nikos Mavrogiannopoulos
Packit Service 4684c1
 *
Packit Service 4684c1
 * This file is part of GnuTLS.
Packit Service 4684c1
 *
Packit Service 4684c1
 * GnuTLS is free software; you can redistribute it and/or modify it
Packit Service 4684c1
 * under the terms of the GNU General Public License as published by
Packit Service 4684c1
 * the Free Software Foundation; either version 3 of the License, or
Packit Service 4684c1
 * (at your option) any later version.
Packit Service 4684c1
 *
Packit Service 4684c1
 * GnuTLS is distributed in the hope that it will be useful, but
Packit Service 4684c1
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 4684c1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 4684c1
 * General Public License for more details.
Packit Service 4684c1
 *
Packit Service 4684c1
 * You should have received a copy of the GNU Lesser General Public License
Packit Service 4684c1
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
Packit Service 4684c1
 */
Packit Service 4684c1
Packit Service 4684c1
/* This tests the gnutls_privkey_import_ext4() APIs */
Packit Service 4684c1
Packit Service 4684c1
#ifdef HAVE_CONFIG_H
Packit Service 4684c1
#include <config.h>
Packit Service 4684c1
#endif
Packit Service 4684c1
Packit Service 4684c1
#include <stdio.h>
Packit Service 4684c1
#include <stdlib.h>
Packit Service 4684c1
#include <string.h>
Packit Service 4684c1
#include <sys/types.h>
Packit Service 4684c1
#ifndef _WIN32
Packit Service 4684c1
# include <netinet/in.h>
Packit Service 4684c1
# include <sys/socket.h>
Packit Service 4684c1
# include <arpa/inet.h>
Packit Service 4684c1
#endif
Packit Service 4684c1
#include <unistd.h>
Packit Service 4684c1
#include <gnutls/gnutls.h>
Packit Service 4684c1
#include <gnutls/x509.h>
Packit Service 4684c1
#include <gnutls/abstract.h>
Packit Service 4684c1
#include "common-key-tests.h"
Packit Service 4684c1
#include "utils.h"
Packit Service 4684c1
Packit Service 4684c1
static void tls_log_func(int level, const char *str)
Packit Service 4684c1
{
Packit Service 4684c1
	fprintf(stderr, "<%d> %s", level, str);
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/* sha1 hash of "hello" string */
Packit Service 4684c1
const gnutls_datum_t sha1_hash_data = {
Packit Service 4684c1
	(void *)
Packit Service 4684c1
	    "\xaa\xf4\xc6\x1d\xdc\xc5\xe8\xa2\xda\xbe"
Packit Service 4684c1
	    "\xde\x0f\x3b\x48\x2c\xd9\xae\xa9\x43\x4d",
Packit Service 4684c1
	20
Packit Service 4684c1
};
Packit Service 4684c1
Packit Service 4684c1
const gnutls_datum_t sha256_hash_data = {
Packit Service 4684c1
	(void *)
Packit Service 4684c1
	    "\x2c\xf2\x4d\xba\x5f\xb0\xa3\x0e\x26\xe8\x3b\x2a\xc5\xb9\xe2\x9e"
Packit Service 4684c1
	    "\x1b\x16\x1e\x5c\x1f\xa7\x42\x5e\x73\x04\x33\x62\x93\x8b\x98\x24",
Packit Service 4684c1
	32
Packit Service 4684c1
};
Packit Service 4684c1
Packit Service 4684c1
const gnutls_datum_t raw_data = {
Packit Service 4684c1
	(void *) "hello",
Packit Service 4684c1
	5
Packit Service 4684c1
};
Packit Service 4684c1
Packit Service 4684c1
Packit Service 4684c1
struct key_cb_data {
Packit Service 4684c1
	gnutls_privkey_t rkey; /* the real thing */
Packit Service 4684c1
	unsigned pk;
Packit Service 4684c1
	unsigned sig;
Packit Service 4684c1
	unsigned bits;
Packit Service 4684c1
};
Packit Service 4684c1
Packit Service 4684c1
static int key_cb_info_func(gnutls_privkey_t key, unsigned int flags, void *userdata)
Packit Service 4684c1
{
Packit Service 4684c1
	struct key_cb_data *p = userdata;
Packit Service 4684c1
Packit Service 4684c1
	if (flags & GNUTLS_PRIVKEY_INFO_PK_ALGO)
Packit Service 4684c1
		return p->pk;
Packit Service 4684c1
	else if (flags & GNUTLS_PRIVKEY_INFO_PK_ALGO_BITS)
Packit Service 4684c1
		return p->bits;
Packit Service 4684c1
	else if (flags & GNUTLS_PRIVKEY_INFO_HAVE_SIGN_ALGO) {
Packit Service 4684c1
		unsigned sig = GNUTLS_FLAGS_TO_SIGN_ALGO(flags);
Packit Service 4684c1
Packit Service 4684c1
		if (sig == p->sig)
Packit Service 4684c1
			return 1;
Packit Service 4684c1
Packit Service 4684c1
		return 0;
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
	return -1;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
static
Packit Service 4684c1
int key_cb_sign_data_func (gnutls_privkey_t key, gnutls_sign_algorithm_t sig,
Packit Service 4684c1
			   void* userdata, unsigned int flags, const gnutls_datum_t *data,
Packit Service 4684c1
			   gnutls_datum_t *signature)
Packit Service 4684c1
{
Packit Service 4684c1
	struct key_cb_data *p = userdata;
Packit Service 4684c1
Packit Service 4684c1
	if (debug)
Packit Service 4684c1
		fprintf(stderr, "signing data with: %s\n", gnutls_sign_get_name(sig));
Packit Service 4684c1
	return gnutls_privkey_sign_data2(p->rkey, sig, 0, data, signature);
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
static
Packit Service 4684c1
int key_cb_sign_hash_func (gnutls_privkey_t key, gnutls_sign_algorithm_t sig,
Packit Service 4684c1
			   void* userdata, unsigned int flags, const gnutls_datum_t *data,
Packit Service 4684c1
			   gnutls_datum_t *signature)
Packit Service 4684c1
{
Packit Service 4684c1
	struct key_cb_data *p = userdata;
Packit Service 4684c1
Packit Service 4684c1
	if (sig == GNUTLS_SIGN_RSA_RAW) {
Packit Service 4684c1
		if (debug)
Packit Service 4684c1
			fprintf(stderr, "signing digestinfo with: raw RSA\n");
Packit Service 4684c1
		return gnutls_privkey_sign_hash(p->rkey, 0, GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA, data, signature);
Packit Service 4684c1
	} else {
Packit Service 4684c1
		if (debug)
Packit Service 4684c1
			fprintf(stderr, "signing hash with: %s\n", gnutls_sign_get_name(sig));
Packit Service 4684c1
		return gnutls_privkey_sign_hash2(p->rkey, sig, 0, data, signature);
Packit Service 4684c1
	}
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
static void key_cb_deinit_func(gnutls_privkey_t key, void* userdata)
Packit Service 4684c1
{
Packit Service 4684c1
	struct key_cb_data *p = userdata;
Packit Service 4684c1
	gnutls_privkey_deinit(p->rkey);
Packit Service 4684c1
	free(userdata);
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
static gnutls_privkey_t load_virt_privkey(const gnutls_datum_t *txtkey, gnutls_pk_algorithm_t pk,
Packit Service 4684c1
					  gnutls_sign_algorithm_t sig)
Packit Service 4684c1
{
Packit Service 4684c1
	gnutls_privkey_t privkey;
Packit Service 4684c1
	struct key_cb_data *userdata;
Packit Service 4684c1
	int ret;
Packit Service 4684c1
Packit Service 4684c1
	userdata = calloc(1, sizeof(struct key_cb_data));
Packit Service 4684c1
	if (userdata == NULL) {
Packit Service 4684c1
		fail("memory error\n");
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
	ret = gnutls_privkey_init(&privkey);
Packit Service 4684c1
	if (ret < 0)
Packit Service 4684c1
		fail("gnutls_privkey_init\n");
Packit Service 4684c1
Packit Service 4684c1
	ret = gnutls_privkey_init(&userdata->rkey);
Packit Service 4684c1
	if (ret < 0)
Packit Service 4684c1
		fail("gnutls_privkey_init\n");
Packit Service 4684c1
Packit Service 4684c1
	ret =
Packit Service 4684c1
	    gnutls_privkey_import_x509_raw(userdata->rkey, txtkey, GNUTLS_X509_FMT_PEM, NULL, 0);
Packit Service 4684c1
	if (ret < 0)
Packit Service 4684c1
		fail("gnutls_privkey_import\n");
Packit Service 4684c1
Packit Service 4684c1
	gnutls_privkey_get_pk_algorithm(userdata->rkey, &userdata->bits);
Packit Service 4684c1
Packit Service 4684c1
	userdata->pk = pk;
Packit Service 4684c1
	userdata->sig = sig;
Packit Service 4684c1
Packit Service 4684c1
	ret = gnutls_privkey_import_ext4(privkey, userdata,
Packit Service 4684c1
					 key_cb_sign_data_func,
Packit Service 4684c1
					 key_cb_sign_hash_func,
Packit Service 4684c1
					 NULL,
Packit Service 4684c1
					 key_cb_deinit_func, key_cb_info_func,
Packit Service 4684c1
					 GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
Packit Service 4684c1
	if (ret < 0)
Packit Service 4684c1
		fail("gnutls_privkey_import_ext4\n");
Packit Service 4684c1
Packit Service 4684c1
	return privkey;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
#define tests common_key_tests
Packit Service 4684c1
#define testfail(fmt, ...) \
Packit Service 4684c1
	fail("%s: "fmt, tests[i].name, ##__VA_ARGS__)
Packit Service 4684c1
Packit Service 4684c1
void doit(void)
Packit Service 4684c1
{
Packit Service 4684c1
	gnutls_pubkey_t pubkey;
Packit Service 4684c1
	gnutls_privkey_t privkey;
Packit Service 4684c1
	gnutls_sign_algorithm_t sign_algo;
Packit Service 4684c1
	gnutls_datum_t signature;
Packit Service 4684c1
	gnutls_datum_t signature2;
Packit Service 4684c1
	int ret;
Packit Service 4684c1
	size_t i;
Packit Service 4684c1
	const gnutls_datum_t *hash_data;
Packit Service 4684c1
Packit Service 4684c1
	global_init();
Packit Service 4684c1
Packit Service 4684c1
	gnutls_global_set_log_function(tls_log_func);
Packit Service 4684c1
	if (debug)
Packit Service 4684c1
		gnutls_global_set_log_level(6);
Packit Service 4684c1
Packit Service 4684c1
	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
Packit Service 4684c1
		if (debug)
Packit Service 4684c1
			success("loop %d: %s\n", (int) i, tests[i].name);
Packit Service 4684c1
Packit Service 4684c1
		if (tests[i].digest == GNUTLS_DIG_SHA1) {
Packit Service 4684c1
			hash_data = &sha1_hash_data;
Packit Service 4684c1
		} else {
Packit Service 4684c1
			hash_data = &sha256_hash_data;
Packit Service 4684c1
		}
Packit Service 4684c1
Packit Service 4684c1
		privkey = load_virt_privkey(&tests[i].key, tests[i].pk, tests[i].sigalgo);
Packit Service 4684c1
Packit Service 4684c1
		ret = gnutls_pubkey_init(&pubkey);
Packit Service 4684c1
		if (ret < 0)
Packit Service 4684c1
			testfail("gnutls_privkey_init\n");
Packit Service 4684c1
Packit Service 4684c1
Packit Service 4684c1
		ret = gnutls_privkey_sign_data2(privkey, tests[i].sigalgo, tests[i].sign_flags,
Packit Service 4684c1
						&raw_data, &signature);
Packit Service 4684c1
		if (ret < 0)
Packit Service 4684c1
			testfail("gnutls_x509_privkey_sign_hash\n");
Packit Service 4684c1
Packit Service 4684c1
		ret = gnutls_pubkey_import_x509_raw(pubkey, &tests[i].cert, GNUTLS_X509_FMT_PEM, 0);
Packit Service 4684c1
		if (ret < 0)
Packit Service 4684c1
			testfail("gnutls_x509_pubkey_import\n");
Packit Service 4684c1
Packit Service 4684c1
		ret =
Packit Service 4684c1
		    gnutls_pubkey_verify_data2(pubkey, tests[i].sigalgo, GNUTLS_VERIFY_ALLOW_SIGN_WITH_SHA1, &raw_data,
Packit Service 4684c1
					       &signature);
Packit Service 4684c1
		if (ret < 0)
Packit Service 4684c1
			testfail("gnutls_pubkey_verify_data2\n");
Packit Service 4684c1
Packit Service 4684c1
		gnutls_free(signature.data);
Packit Service 4684c1
Packit Service 4684c1
Packit Service 4684c1
		if (!tests[i].data_only) {
Packit Service 4684c1
			ret = gnutls_privkey_sign_hash(privkey, tests[i].digest, tests[i].sign_flags,
Packit Service 4684c1
							hash_data, &signature2);
Packit Service 4684c1
			if (ret < 0)
Packit Service 4684c1
				testfail("gnutls_privkey_sign_hash\n");
Packit Service 4684c1
Packit Service 4684c1
			ret =
Packit Service 4684c1
			    gnutls_pubkey_verify_hash2(pubkey, tests[i].sigalgo, GNUTLS_VERIFY_ALLOW_SIGN_WITH_SHA1, hash_data,
Packit Service 4684c1
						      &signature2);
Packit Service 4684c1
			if (ret < 0)
Packit Service 4684c1
				testfail("gnutls_pubkey_verify_hash2-1 (hashed data)\n");
Packit Service 4684c1
Packit Service 4684c1
			gnutls_free(signature2.data);
Packit Service 4684c1
		}
Packit Service 4684c1
Packit Service 4684c1
		if (gnutls_pubkey_get_pk_algorithm(pubkey, NULL) ==
Packit Service 4684c1
		    GNUTLS_PK_RSA) {
Packit Service 4684c1
Packit Service 4684c1
			ret =
Packit Service 4684c1
			    gnutls_privkey_sign_hash(privkey,
Packit Service 4684c1
						     tests[i].digest,
Packit Service 4684c1
						     GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA,
Packit Service 4684c1
						     hash_data,
Packit Service 4684c1
						     &signature);
Packit Service 4684c1
			if (ret < 0)
Packit Service 4684c1
				testfail("gnutls_privkey_sign_hash: %s\n",
Packit Service 4684c1
				     gnutls_strerror(ret));
Packit Service 4684c1
Packit Service 4684c1
			sign_algo =
Packit Service 4684c1
			    gnutls_pk_to_sign
Packit Service 4684c1
			    (gnutls_pubkey_get_pk_algorithm(pubkey, NULL),
Packit Service 4684c1
			     tests[i].digest);
Packit Service 4684c1
Packit Service 4684c1
			ret =
Packit Service 4684c1
			    gnutls_pubkey_verify_hash2(pubkey, sign_algo,
Packit Service 4684c1
							GNUTLS_PUBKEY_VERIFY_FLAG_TLS1_RSA,
Packit Service 4684c1
							hash_data,
Packit Service 4684c1
							&signature);
Packit Service 4684c1
			if (ret < 0)
Packit Service 4684c1
				testfail("gnutls_pubkey_verify_hash-3 (raw hashed data)\n");
Packit Service 4684c1
Packit Service 4684c1
			gnutls_free(signature.data);
Packit Service 4684c1
			/* test the legacy API */
Packit Service 4684c1
			ret =
Packit Service 4684c1
			    gnutls_privkey_sign_raw_data(privkey, 0,
Packit Service 4684c1
							 hash_data,
Packit Service 4684c1
							 &signature);
Packit Service 4684c1
			if (ret < 0)
Packit Service 4684c1
				testfail("gnutls_privkey_sign_raw_data: %s\n",
Packit Service 4684c1
				     gnutls_strerror(ret));
Packit Service 4684c1
Packit Service 4684c1
			ret =
Packit Service 4684c1
			    gnutls_pubkey_verify_hash2(pubkey, sign_algo,
Packit Service 4684c1
							GNUTLS_PUBKEY_VERIFY_FLAG_TLS1_RSA,
Packit Service 4684c1
							hash_data,
Packit Service 4684c1
							&signature);
Packit Service 4684c1
			if (ret < 0)
Packit Service 4684c1
				testfail("gnutls_pubkey_verify_hash-4 (legacy raw hashed data)\n");
Packit Service 4684c1
		}
Packit Service 4684c1
		gnutls_free(signature.data);
Packit Service 4684c1
		gnutls_free(signature2.data);
Packit Service 4684c1
		gnutls_privkey_deinit(privkey);
Packit Service 4684c1
		gnutls_pubkey_deinit(pubkey);
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
	gnutls_global_deinit();
Packit Service 4684c1
}