Blame SOURCES/gnutls-3.3.26-pin-value.patch

10790f
diff --git a/configure.ac b/configure.ac
10790f
index 0840042..c9c9fdc 100644
10790f
--- a/configure.ac
10790f
+++ b/configure.ac
10790f
@@ -447,6 +447,9 @@ if test "$with_p11_kit" != "no"; then
10790f
 		if ! $PKG_CONFIG --atleast-version=0.22.0 p11-kit-1; then
10790f
 			with_buggy_p11_kit=yes
10790f
 		fi
10790f
+		if $PKG_CONFIG --atleast-version=0.23.1 p11-kit-1; then
10790f
+			AC_DEFINE([P11_KIT_HAS_PIN_VALUE], 1, [p11-kit supports p11_kit_uri_get_pin_value()])
10790f
+		fi
10790f
 	else
10790f
 	        with_p11_kit=no
10790f
 	        AC_MSG_WARN([[
10790f
diff --git a/lib/pkcs11.c b/lib/pkcs11.c
10790f
index f5cf99d..26d88e5 100644
10790f
--- a/lib/pkcs11.c
10790f
+++ b/lib/pkcs11.c
10790f
@@ -2367,6 +2367,25 @@ retrieve_pin(struct pin_info_st *pin_info, struct p11_kit_uri *info,
10790f
 
10790f
 	*pin = NULL;
10790f
 
10790f
+#ifdef P11_KIT_HAS_PIN_VALUE
10790f
+	/* First check for pin-value field */
10790f
+	pinfile = p11_kit_uri_get_pin_value(info);
10790f
+	if (pinfile != NULL) {
10790f
+		_gnutls_debug_log("p11: Using pin-value to retrieve PIN\n");
10790f
+		*pin = p11_kit_pin_new_for_string(pinfile);
10790f
+		if (*pin != NULL)
10790f
+			ret = 0;
10790f
+	} else { /* try pin-source */
10790f
+		/* Check if a pinfile is specified, and use that if possible */
10790f
+		pinfile = p11_kit_uri_get_pin_source(info);
10790f
+		if (pinfile != NULL) {
10790f
+			_gnutls_debug_log("p11: Using pin-source to retrieve PIN\n");
10790f
+			ret =
10790f
+			    retrieve_pin_from_source(pinfile, token_info, attempts,
10790f
+						     user_type, pin);
10790f
+		}
10790f
+	}
10790f
+#else
10790f
 	/* Check if a pinfile is specified, and use that if possible */
10790f
 	pinfile = p11_kit_uri_get_pinfile(info);
10790f
 	if (pinfile != NULL) {
10790f
@@ -2375,6 +2394,7 @@ retrieve_pin(struct pin_info_st *pin_info, struct p11_kit_uri *info,
10790f
 		    retrieve_pin_from_source(pinfile, token_info, attempts,
10790f
 					     user_type, pin);
10790f
 	}
10790f
+#endif
10790f
 
10790f
 	/* The global gnutls pin callback */
10790f
 	if (ret < 0)
10790f
diff --git a/tests/Makefile.am b/tests/Makefile.am
10790f
index 5b60899..20ed79c 100644
10790f
--- a/tests/Makefile.am
10790f
+++ b/tests/Makefile.am
10790f
@@ -36,7 +36,7 @@ EXTRA_DIST = suppressions.valgrind eagain-common.h test-chains.h \
10790f
 	certs/cert-rsa-2432.pem certs/ecc384.pem certs/ecc.pem \
10790f
 	certs/ca-ecc.pem certs/cert-ecc384.pem certs/cert-ecc.pem certs/ecc256.pem \
10790f
 	certs/ecc521.pem certs/rsa-2432.pem x509cert-dir/ca.pem \
10790f
-	cert-common.h pkcs11/softhsm.h
10790f
+	cert-common.h pkcs11/softhsm.h pkcs11/pkcs11-pubkey-import.c
10790f
 
10790f
 AM_CFLAGS = $(WARN_CFLAGS) $(WERROR_CFLAGS)
10790f
 AM_CPPFLAGS = \
10790f
@@ -113,7 +113,8 @@ pkcs11_import_url_privkey_LDADD = $(LDADD) $(LIBDL)
10790f
 
10790f
 ctests += pkcs11-cert-import-url-exts pkcs11-get-exts pkcs11-get-raw-issuer-exts \
10790f
 	pkcs11/pkcs11-chainverify pkcs11/pkcs11-get-issuer pkcs11/pkcs11-is-known \
10790f
-	pkcs11/pkcs11-combo pkcs11-import-url-privkey
10790f
+	pkcs11/pkcs11-combo pkcs11-import-url-privkey pkcs11/pkcs11-pubkey-import-rsa \
10790f
+	pkcs11/pkcs11-import-with-pin
10790f
 
10790f
 endif
10790f
 endif
10790f
diff --git a/tests/pkcs11/pkcs11-import-with-pin.c b/tests/pkcs11/pkcs11-import-with-pin.c
10790f
new file mode 100644
10790f
index 0000000..e435919
10790f
--- /dev/null
10790f
+++ b/tests/pkcs11/pkcs11-import-with-pin.c
10790f
@@ -0,0 +1,198 @@
10790f
+/*
10790f
+ * Copyright (C) 2017 Red Hat, Inc.
10790f
+ *
10790f
+ * Author: Nikos Mavrogiannopoulos
10790f
+ *
10790f
+ * This file is part of GnuTLS.
10790f
+ *
10790f
+ * GnuTLS is free software; you can redistribute it and/or modify it
10790f
+ * under the terms of the GNU General Public License as published by
10790f
+ * the Free Software Foundation; either version 3 of the License, or
10790f
+ * (at your option) any later version.
10790f
+ *
10790f
+ * GnuTLS is distributed in the hope that it will be useful, but
10790f
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
10790f
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10790f
+ * General Public License for more details.
10790f
+ *
10790f
+ * You should have received a copy of the GNU General Public License
10790f
+ * along with GnuTLS; if not, write to the Free Software Foundation,
10790f
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
10790f
+ */
10790f
+
10790f
+#ifdef HAVE_CONFIG_H
10790f
+#include <config.h>
10790f
+#endif
10790f
+
10790f
+#include <stdio.h>
10790f
+#include <stdlib.h>
10790f
+#include <string.h>
10790f
+#include <assert.h>
10790f
+#include <unistd.h>
10790f
+
10790f
+#include <gnutls/gnutls.h>
10790f
+#include <gnutls/x509.h>
10790f
+#include <gnutls/abstract.h>
10790f
+
10790f
+#include "../utils.h"
10790f
+#include "softhsm.h"
10790f
+
10790f
+/* Tests whether a protected object is imported with PIN obtained using
10790f
+ * pin-value or pin-source. */
10790f
+
10790f
+#define CONFIG_NAME "softhsm-import-with-pin"
10790f
+#define CONFIG CONFIG_NAME".config"
10790f
+
10790f
+#include "../cert-common.h"
10790f
+
10790f
+#define PIN "1234"
10790f
+
10790f
+static const gnutls_datum_t testdata = {(void*)"test test", 9};
10790f
+
10790f
+static void tls_log_func(int level, const char *str)
10790f
+{
10790f
+	fprintf(stderr, "|<%d>| %s", level, str);
10790f
+}
10790f
+
10790f
+static
10790f
+int pin_func(void* userdata, int attempt, const char* url, const char *label,
10790f
+		unsigned flags, char *pin, size_t pin_max)
10790f
+{
10790f
+	if (attempt == 0) {
10790f
+		strcpy(pin, PIN);
10790f
+		return 0;
10790f
+	}
10790f
+	return -1;
10790f
+}
10790f
+
10790f
+static void write_pin(const char *file, const char *pin)
10790f
+{
10790f
+	FILE *fp = fopen(file, "w");
10790f
+	assert(fp != NULL);
10790f
+	fputs(pin, fp);
10790f
+	fclose(fp);
10790f
+}
10790f
+
10790f
+void doit()
10790f
+{
10790f
+	char buf[512];
10790f
+	int ret, pk;
10790f
+	const char *lib, *bin;
10790f
+	gnutls_x509_privkey_t key;
10790f
+	gnutls_datum_t tmp, sig;
10790f
+	gnutls_privkey_t pkey;
10790f
+	char file[TMPNAME_SIZE];
10790f
+
10790f
+#ifndef P11_KIT_HAS_PIN_VALUE
10790f
+	exit(77);
10790f
+#endif
10790f
+
10790f
+	bin = softhsm_bin();
10790f
+
10790f
+	lib = softhsm_lib();
10790f
+
10790f
+	ret = global_init();
10790f
+	if (ret != 0) {
10790f
+		fail("%d: %s\n", ret, gnutls_strerror(ret));
10790f
+		exit(1);
10790f
+	}
10790f
+
10790f
+	gnutls_pkcs11_set_pin_function(pin_func, NULL);
10790f
+	gnutls_global_set_log_function(tls_log_func);
10790f
+	if (debug)
10790f
+		gnutls_global_set_log_level(4711);
10790f
+
10790f
+	set_softhsm_conf(CONFIG);
10790f
+	snprintf(buf, sizeof(buf), "%s --init-token --slot 0 --label test --so-pin "PIN" --pin "PIN, bin);
10790f
+	system(buf);
10790f
+
10790f
+	ret = gnutls_pkcs11_add_provider(lib, "trusted");
10790f
+	if (ret < 0) {
10790f
+		fprintf(stderr, "add_provider: %s\n",
10790f
+			gnutls_strerror(ret));
10790f
+		exit(1);
10790f
+	}
10790f
+
10790f
+	ret = gnutls_x509_privkey_init(&key);
10790f
+	if (ret < 0) {
10790f
+		fprintf(stderr,
10790f
+			"gnutls_x509_privkey_init: %s\n",
10790f
+			gnutls_strerror(ret));
10790f
+		exit(1);
10790f
+	}
10790f
+
10790f
+	ret =
10790f
+	    gnutls_x509_privkey_import(key, &server_key,
10790f
+				   GNUTLS_X509_FMT_PEM);
10790f
+	if (ret < 0) {
10790f
+		fprintf(stderr,
10790f
+			"gnutls_x509_privkey_import: %s\n",
10790f
+			gnutls_strerror(ret));
10790f
+			exit(1);
10790f
+	}
10790f
+
10790f
+	/* initialize softhsm token */
10790f
+	ret = gnutls_pkcs11_token_init(SOFTHSM_URL, PIN, "test");
10790f
+	if (ret < 0) {
10790f
+		fail("gnutls_pkcs11_token_init: %s\n", gnutls_strerror(ret));
10790f
+		exit(1);
10790f
+	}
10790f
+
10790f
+	ret = gnutls_pkcs11_token_set_pin(SOFTHSM_URL, NULL, PIN, GNUTLS_PIN_USER);
10790f
+	if (ret < 0) {
10790f
+		fail("gnutls_pkcs11_token_set_pin: %s\n", gnutls_strerror(ret));
10790f
+		exit(1);
10790f
+	}
10790f
+
10790f
+	ret = gnutls_pkcs11_copy_x509_privkey(SOFTHSM_URL, key, "cert", GNUTLS_KEY_DIGITAL_SIGNATURE|GNUTLS_KEY_KEY_ENCIPHERMENT,
10790f
+					      GNUTLS_PKCS11_OBJ_FLAG_MARK_PRIVATE|GNUTLS_PKCS11_OBJ_FLAG_MARK_SENSITIVE|GNUTLS_PKCS11_OBJ_FLAG_LOGIN);
10790f
+	if (ret < 0) {
10790f
+		fail("gnutls_pkcs11_copy_x509_privkey: %s\n", gnutls_strerror(ret));
10790f
+		exit(1);
10790f
+	}
10790f
+
10790f
+	gnutls_x509_privkey_deinit(key);
10790f
+	gnutls_pkcs11_set_pin_function(NULL, NULL);
10790f
+
10790f
+	assert(gnutls_privkey_init(&pkey) == 0);
10790f
+
10790f
+	/* Test 1
10790f
+	 * Try importing with pin-value */
10790f
+	ret = gnutls_privkey_import_pkcs11_url(pkey, SOFTHSM_URL";object=cert;object-type=private;pin-value="PIN);
10790f
+	if (ret < 0) {
10790f
+		fprintf(stderr, "error in %d: %s\n", __LINE__, gnutls_strerror(ret));
10790f
+		exit(1);
10790f
+	}
10790f
+
10790f
+	/* check whether privkey is operational by signing */
10790f
+	assert(gnutls_privkey_sign_data(pkey, GNUTLS_DIG_SHA256, 0, &testdata, &sig) == 0);
10790f
+	gnutls_free(sig.data);
10790f
+	gnutls_privkey_deinit(pkey);
10790f
+
10790f
+	/* Test 2
10790f
+	 * Try importing with pin-source */
10790f
+	track_temp_files();
10790f
+	get_tmpname(file);
10790f
+
10790f
+	write_pin(file, PIN);
10790f
+
10790f
+
10790f
+	assert(gnutls_privkey_init(&pkey) == 0);
10790f
+	snprintf(buf, sizeof(buf), "%s;object=cert;object-type=private;pin-source=%s", SOFTHSM_URL, file);
10790f
+	ret = gnutls_privkey_import_pkcs11_url(pkey, buf);
10790f
+	if (ret < 0) {
10790f
+		fprintf(stderr, "error in %d: %s\n", __LINE__, gnutls_strerror(ret));
10790f
+		exit(1);
10790f
+	}
10790f
+
10790f
+	/* check whether privkey is operational by signing */
10790f
+	assert(gnutls_privkey_sign_data(pkey, GNUTLS_DIG_SHA256, 0, &testdata, &sig) == 0);
10790f
+	gnutls_free(sig.data);
10790f
+	gnutls_privkey_deinit(pkey);
10790f
+
10790f
+	gnutls_global_deinit();
10790f
+	delete_temp_files();
10790f
+
10790f
+	remove(CONFIG);
10790f
+}
10790f
+
10790f
diff --git a/tests/pkcs11/pkcs11-pubkey-import-rsa.c b/tests/pkcs11/pkcs11-pubkey-import-rsa.c
10790f
new file mode 100644
10790f
index 0000000..d304c4f
10790f
--- /dev/null
10790f
+++ b/tests/pkcs11/pkcs11-pubkey-import-rsa.c
10790f
@@ -0,0 +1,41 @@
10790f
+/*
10790f
+ * Copyright (C) 2015 Nikos Mavrogiannopoulos
10790f
+ *
10790f
+ * Author: Nikos Mavrogiannopoulos
10790f
+ *
10790f
+ * This file is part of GnuTLS.
10790f
+ *
10790f
+ * GnuTLS is free software; you can redistribute it and/or modify it
10790f
+ * under the terms of the GNU General Public License as published by
10790f
+ * the Free Software Foundation; either version 3 of the License, or
10790f
+ * (at your option) any later version.
10790f
+ *
10790f
+ * GnuTLS is distributed in the hope that it will be useful, but
10790f
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
10790f
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10790f
+ * General Public License for more details.
10790f
+ *
10790f
+ * You should have received a copy of the GNU General Public License
10790f
+ * along with GnuTLS; if not, write to the Free Software Foundation,
10790f
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
10790f
+ */
10790f
+
10790f
+#ifdef HAVE_CONFIG_H
10790f
+#include <config.h>
10790f
+#endif
10790f
+
10790f
+#include <stdio.h>
10790f
+#include <stdlib.h>
10790f
+#include <string.h>
10790f
+#include <unistd.h>
10790f
+
10790f
+#define CONFIG_NAME "softhsm-pubkey-import-rsa"
10790f
+#define CONFIG CONFIG_NAME".config"
10790f
+
10790f
+#include "pkcs11-pubkey-import.c"
10790f
+
10790f
+void doit(void)
10790f
+{
10790f
+	success("Testing RSA key\n");
10790f
+	try(1);
10790f
+}
10790f
diff --git a/tests/pkcs11/pkcs11-pubkey-import.c b/tests/pkcs11/pkcs11-pubkey-import.c
10790f
new file mode 100644
10790f
index 0000000..7513aad
10790f
--- /dev/null
10790f
+++ b/tests/pkcs11/pkcs11-pubkey-import.c
10790f
@@ -0,0 +1,220 @@
10790f
+/*
10790f
+ * Copyright (C) 2015 Nikos Mavrogiannopoulos
10790f
+ *
10790f
+ * Author: Nikos Mavrogiannopoulos
10790f
+ *
10790f
+ * This file is part of GnuTLS.
10790f
+ *
10790f
+ * GnuTLS is free software; you can redistribute it and/or modify it
10790f
+ * under the terms of the GNU General Public License as published by
10790f
+ * the Free Software Foundation; either version 3 of the License, or
10790f
+ * (at your option) any later version.
10790f
+ *
10790f
+ * GnuTLS is distributed in the hope that it will be useful, but
10790f
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
10790f
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10790f
+ * General Public License for more details.
10790f
+ *
10790f
+ * You should have received a copy of the GNU General Public License
10790f
+ * along with GnuTLS; if not, write to the Free Software Foundation,
10790f
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
10790f
+ */
10790f
+
10790f
+#ifdef HAVE_CONFIG_H
10790f
+#include <config.h>
10790f
+#endif
10790f
+
10790f
+#include <stdio.h>
10790f
+#include <stdlib.h>
10790f
+#include <string.h>
10790f
+#include <assert.h>
10790f
+#include <unistd.h>
10790f
+
10790f
+#include <gnutls/gnutls.h>
10790f
+#include <gnutls/x509.h>
10790f
+#include <gnutls/abstract.h>
10790f
+
10790f
+#include "../utils.h"
10790f
+#include "softhsm.h"
10790f
+
10790f
+/* Tests whether gnutls_pubkey_import_privkey works well for 
10790f
+ * RSA keys under PKCS #11 */
10790f
+
10790f
+
10790f
+#include "../cert-common.h"
10790f
+
10790f
+#define PIN "1234"
10790f
+
10790f
+static const gnutls_datum_t testdata = {(void*)"test test", 9};
10790f
+
10790f
+static void tls_log_func(int level, const char *str)
10790f
+{
10790f
+	fprintf(stderr, "|<%d>| %s", level, str);
10790f
+}
10790f
+
10790f
+static
10790f
+int pin_func(void* userdata, int attempt, const char* url, const char *label,
10790f
+		unsigned flags, char *pin, size_t pin_max)
10790f
+{
10790f
+	if (attempt == 0) {
10790f
+		strcpy(pin, PIN);
10790f
+		return 0;
10790f
+	}
10790f
+	return -1;
10790f
+}
10790f
+
10790f
+static void try(int rsa)
10790f
+{
10790f
+	char buf[128];
10790f
+	int ret, pk;
10790f
+	const char *lib, *bin;
10790f
+	gnutls_x509_crt_t crt;
10790f
+	gnutls_x509_privkey_t key;
10790f
+	gnutls_datum_t tmp, sig;
10790f
+	gnutls_privkey_t pkey;
10790f
+	gnutls_pubkey_t pubkey;
10790f
+	gnutls_pubkey_t pubkey2;
10790f
+
10790f
+#ifndef P11_KIT_HAS_PIN_VALUE
10790f
+	exit(77);
10790f
+#endif
10790f
+
10790f
+	bin = softhsm_bin();
10790f
+
10790f
+	lib = softhsm_lib();
10790f
+
10790f
+	ret = global_init();
10790f
+	if (ret != 0) {
10790f
+		fail("%d: %s\n", ret, gnutls_strerror(ret));
10790f
+		exit(1);
10790f
+	}
10790f
+
10790f
+	gnutls_pkcs11_set_pin_function(pin_func, NULL);
10790f
+	gnutls_global_set_log_function(tls_log_func);
10790f
+	if (debug)
10790f
+		gnutls_global_set_log_level(4711);
10790f
+
10790f
+	set_softhsm_conf(CONFIG);
10790f
+	snprintf(buf, sizeof(buf), "%s --init-token --slot 0 --label test --so-pin "PIN" --pin "PIN, bin);
10790f
+	system(buf);
10790f
+
10790f
+	ret = gnutls_pkcs11_add_provider(lib, "trusted");
10790f
+	if (ret < 0) {
10790f
+		fprintf(stderr, "gnutls_x509_crt_init: %s\n",
10790f
+			gnutls_strerror(ret));
10790f
+		exit(1);
10790f
+	}
10790f
+
10790f
+	ret = gnutls_x509_crt_init(&crt;;
10790f
+	if (ret < 0) {
10790f
+		fprintf(stderr,
10790f
+			"gnutls_x509_crt_init: %s\n",
10790f
+			gnutls_strerror(ret));
10790f
+		exit(1);
10790f
+	}
10790f
+
10790f
+	ret =
10790f
+	    gnutls_x509_crt_import(crt, rsa?&server_cert:&server_ecc_cert,
10790f
+				   GNUTLS_X509_FMT_PEM);
10790f
+	if (ret < 0) {
10790f
+		fprintf(stderr,
10790f
+			"gnutls_x509_crt_import: %s\n",
10790f
+			gnutls_strerror(ret));
10790f
+			exit(1);
10790f
+	}
10790f
+
10790f
+	if (debug) {
10790f
+		gnutls_x509_crt_print(crt,
10790f
+			      GNUTLS_CRT_PRINT_ONELINE,
10790f
+			      &tmp);
10790f
+
10790f
+		printf("\tCertificate: %.*s\n",
10790f
+		       tmp.size, tmp.data);
10790f
+		gnutls_free(tmp.data);
10790f
+	}
10790f
+
10790f
+	ret = gnutls_x509_privkey_init(&key);
10790f
+	if (ret < 0) {
10790f
+		fprintf(stderr,
10790f
+			"gnutls_x509_privkey_init: %s\n",
10790f
+			gnutls_strerror(ret));
10790f
+		exit(1);
10790f
+	}
10790f
+
10790f
+	ret =
10790f
+	    gnutls_x509_privkey_import(key, rsa?&server_key:&server_ecc_key,
10790f
+				   GNUTLS_X509_FMT_PEM);
10790f
+	if (ret < 0) {
10790f
+		fprintf(stderr,
10790f
+			"gnutls_x509_privkey_import: %s\n",
10790f
+			gnutls_strerror(ret));
10790f
+			exit(1);
10790f
+	}
10790f
+
10790f
+	/* initialize softhsm token */
10790f
+	ret = gnutls_pkcs11_token_init(SOFTHSM_URL, PIN, "test");
10790f
+	if (ret < 0) {
10790f
+		fail("gnutls_pkcs11_token_init: %s\n", gnutls_strerror(ret));
10790f
+		exit(1);
10790f
+	}
10790f
+
10790f
+	ret = gnutls_pkcs11_token_set_pin(SOFTHSM_URL, NULL, PIN, GNUTLS_PIN_USER);
10790f
+	if (ret < 0) {
10790f
+		fail("gnutls_pkcs11_token_set_pin: %s\n", gnutls_strerror(ret));
10790f
+		exit(1);
10790f
+	}
10790f
+
10790f
+	ret = gnutls_pkcs11_copy_x509_crt(SOFTHSM_URL, crt, "cert",
10790f
+					  GNUTLS_PKCS11_OBJ_FLAG_MARK_PRIVATE|GNUTLS_PKCS11_OBJ_FLAG_LOGIN);
10790f
+	if (ret < 0) {
10790f
+		fail("gnutls_pkcs11_copy_x509_crt: %s\n", gnutls_strerror(ret));
10790f
+		exit(1);
10790f
+	}
10790f
+
10790f
+	ret = gnutls_pkcs11_copy_x509_privkey(SOFTHSM_URL, key, "cert", GNUTLS_KEY_DIGITAL_SIGNATURE|GNUTLS_KEY_KEY_ENCIPHERMENT,
10790f
+					      GNUTLS_PKCS11_OBJ_FLAG_MARK_PRIVATE|GNUTLS_PKCS11_OBJ_FLAG_MARK_SENSITIVE|GNUTLS_PKCS11_OBJ_FLAG_LOGIN);
10790f
+	if (ret < 0) {
10790f
+		fail("gnutls_pkcs11_copy_x509_privkey: %s\n", gnutls_strerror(ret));
10790f
+		exit(1);
10790f
+	}
10790f
+
10790f
+	gnutls_x509_crt_deinit(crt);
10790f
+	gnutls_x509_privkey_deinit(key);
10790f
+	gnutls_pkcs11_set_pin_function(NULL, NULL);
10790f
+
10790f
+	assert(gnutls_privkey_init(&pkey) == 0);
10790f
+
10790f
+	ret = gnutls_privkey_import_pkcs11_url(pkey, SOFTHSM_URL";object=cert;object-type=private;pin-value="PIN);
10790f
+	if (ret < 0) {
10790f
+		fprintf(stderr, "error in %d: %s\n", __LINE__, gnutls_strerror(ret));
10790f
+		exit(1);
10790f
+	}
10790f
+
10790f
+	assert(gnutls_pubkey_init(&pubkey) == 0);
10790f
+	assert(gnutls_pubkey_import_privkey(pubkey, pkey, 0, 0) == 0);
10790f
+
10790f
+	pk = gnutls_pubkey_get_pk_algorithm(pubkey, NULL);
10790f
+
10790f
+	/* check whether privkey and pubkey are operational
10790f
+	 * by signing and verifying */
10790f
+	assert(gnutls_privkey_sign_data(pkey, GNUTLS_DIG_SHA256, 0, &testdata, &sig) == 0);
10790f
+
10790f
+	/* verify against the raw pubkey */
10790f
+	assert(gnutls_pubkey_init(&pubkey2) == 0);
10790f
+	assert(gnutls_pubkey_import_x509_raw(pubkey2, rsa?&server_cert:&server_ecc_cert, GNUTLS_X509_FMT_PEM, 0) == 0);
10790f
+	assert(gnutls_pubkey_verify_data2(pubkey2, gnutls_pk_to_sign(pk, GNUTLS_DIG_SHA256), 0, &testdata, &sig) >= 0);
10790f
+
10790f
+	/* verify against the pubkey in PKCS #11 */
10790f
+	assert(gnutls_pubkey_verify_data2(pubkey, gnutls_pk_to_sign(pk, GNUTLS_DIG_SHA256), 0, &testdata, &sig) >= 0);
10790f
+
10790f
+	gnutls_free(sig.data);
10790f
+
10790f
+	gnutls_pubkey_deinit(pubkey2);
10790f
+	gnutls_pubkey_deinit(pubkey);
10790f
+	gnutls_privkey_deinit(pkey);
10790f
+
10790f
+	gnutls_global_deinit();
10790f
+
10790f
+	remove(CONFIG);
10790f
+}
10790f
+
10790f
diff --git a/tests/utils.c b/tests/utils.c
10790f
index 65ceafd..37345a6 100644
10790f
--- a/tests/utils.c
10790f
+++ b/tests/utils.c
10790f
@@ -30,6 +30,7 @@
10790f
 #include <time.h>
10790f
 #include <unistd.h>
10790f
 #include <errno.h>
10790f
+#include <assert.h>
10790f
 #ifndef _WIN32
10790f
 # include <netinet/in.h>
10790f
 # include <sys/socket.h>
10790f
@@ -39,6 +40,8 @@
10790f
 # include <winbase.h>  
10790f
 #endif
10790f
 #endif
10790f
+#include <gnutls/gnutls.h>
10790f
+#include <gnutls/crypto.h>
10790f
 
10790f
 #include "utils.h"
10790f
 
10790f
@@ -183,3 +186,74 @@ int main(int argc, char *argv[])
10790f
 
10790f
 	return error_count ? 1 : 0;
10790f
 }
10790f
+
10790f
+struct tmp_file_st {
10790f
+	char file[TMPNAME_SIZE];
10790f
+	struct tmp_file_st *next;
10790f
+};
10790f
+
10790f
+static struct tmp_file_st *temp_files = (void*)-1;
10790f
+
10790f
+static void append(const char *file)
10790f
+{
10790f
+	struct tmp_file_st *p;
10790f
+
10790f
+	if (temp_files == (void*)-1)
10790f
+		return;
10790f
+
10790f
+	p = calloc(1, sizeof(*p));
10790f
+
10790f
+	assert(p != NULL);
10790f
+	strcpy(p->file, file);
10790f
+	p->next = temp_files;
10790f
+	temp_files = p;
10790f
+}
10790f
+
10790f
+char *get_tmpname(char s[TMPNAME_SIZE])
10790f
+{
10790f
+	unsigned char rnd[6];
10790f
+	static char _s[TMPNAME_SIZE];
10790f
+	int ret;
10790f
+	char *p;
10790f
+	const char *path;
10790f
+
10790f
+	ret = gnutls_rnd(GNUTLS_RND_NONCE, rnd, sizeof(rnd));
10790f
+	if (ret < 0)
10790f
+		return NULL;
10790f
+
10790f
+	path = getenv("builddir");
10790f
+	if (path == NULL)
10790f
+		path = ".";
10790f
+
10790f
+	if (s == NULL)
10790f
+		p = _s;
10790f
+	else
10790f
+		p = s;
10790f
+
10790f
+	snprintf(p, TMPNAME_SIZE, "%s/tmpfile-%02x%02x%02x%02x%02x%02x.tmp", path, (unsigned)rnd[0], (unsigned)rnd[1],
10790f
+		(unsigned)rnd[2], (unsigned)rnd[3], (unsigned)rnd[4], (unsigned)rnd[5]);
10790f
+
10790f
+	append(p);
10790f
+
10790f
+	return p;
10790f
+}
10790f
+
10790f
+void track_temp_files(void)
10790f
+{
10790f
+	temp_files = NULL;
10790f
+}
10790f
+
10790f
+void delete_temp_files(void)
10790f
+{
10790f
+	struct tmp_file_st *p = temp_files;
10790f
+	struct tmp_file_st *next;
10790f
+
10790f
+	if (p == (void*)-1)
10790f
+		return;
10790f
+
10790f
+	while(p != NULL) {
10790f
+		next = p->next;
10790f
+		free(p);
10790f
+		p = next;
10790f
+	}
10790f
+}
10790f
diff --git a/tests/utils.h b/tests/utils.h
10790f
index 8f3ac3f..5c0afe7 100644
10790f
--- a/tests/utils.h
10790f
+++ b/tests/utils.h
10790f
@@ -61,4 +61,9 @@ extern void binprint(const void *str, size_t len);
10790f
 extern void doit(void);
10790f
 void sec_sleep(int sec);
10790f
 
10790f
+#define TMPNAME_SIZE 128
10790f
+char *get_tmpname(char s[TMPNAME_SIZE]);
10790f
+void track_temp_files(void);
10790f
+void delete_temp_files(void);
10790f
+
10790f
 #endif				/* UTILS_H */