[gnutls-devel] [PATCH] improve compatibility in pkcs11 key generation

Wolfgang Meyer zu Bergsten w.bergsten at sirrix.com
Wed Aug 6 14:34:44 CEST 2014


Hello

Am 05.08.2014 14:15, schrieb Nikos Mavrogiannopoulos:
> On Tue, Aug 5, 2014 at 2:10 PM, Wolfgang Meyer zu Bergsten
> <w.bergsten at sirrix.com> wrote:
> 
>>>  Wouldn't that be better if both unwrap and fixed exponent be set
>>> using special flags? That is create the flags, e.g.,
>>> GNUTLS_PKCS11_GEN_RSA_EXP_65537, GNUTLS_PKCS11_GEN_KEY_UNWRAP,
>>> GNUTLS_PKCS11_GEN_KEY_WRAP, which will enable that specific
>>> functionality for the key.
>> Regarding the exponent, 0x10001 is the standard exponent that is used by
>> PKCS#11 libraries if no  CKA_PUBLIC_EXPONENT is provided. So stating it
>> explicitly only improves compatibility with some PKCS#11 providers.
>> (see
>> http://www.cryptsoft.com/pkcs11doc/v230/group__SEC__11__1__4__PKCS____1__RSA__KEY__PAIR__GENERATION.html)
>> Thus the library behaviour does not change and the flag should not be
>> necessary. Do you still want the change?
>> Regarding the KEY_UNWRAP and KEY_WRAP flags: I will change it according
>> to your proposal.
> 
> That makes sense. I.e., only the wrap and unwrap flags are needed.

I added just one flag GNUTLS_PKCS11_OBJ_FLAG_KEY_WRAP because:
* KEY_WRAP without KEY_UNWRAP are corresponding to the public vs.
  private part of the key and I cannot think of uses that require just
  one parameter to be set. Therefore only one flag.
* the parameter gets passed into the function like the other _OBJ_
  flags. Therefore the name.

If you have any objections, I will change things accordingly.

regards
Wolfgang
-------------- next part --------------
>From 286a153debe07482e6a6b1d3ffdbea3ec3e965fe Mon Sep 17 00:00:00 2001
From: Wolfgang Meyer zu Bergsten <w.bergsten at sirrix.com>
Date: Mon, 4 Aug 2014 15:32:53 +0200
Subject: [PATCH 1/3] improve compatibility in pkcs11 key generation

* add key wrap/unwrap key usage
* explicitly set public exponent in template

Signed-off-by: Wolfgang Meyer zu Bergsten <w.bergsten at sirrix.com>
---
 lib/includes/gnutls/pkcs11.h |  1 +
 lib/pkcs11_privkey.c         | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/lib/includes/gnutls/pkcs11.h b/lib/includes/gnutls/pkcs11.h
index 87a54f2..8f2d2d7 100644
--- a/lib/includes/gnutls/pkcs11.h
+++ b/lib/includes/gnutls/pkcs11.h
@@ -104,6 +104,7 @@ void gnutls_pkcs11_obj_set_pin_function(gnutls_pkcs11_obj_t obj,
 #define GNUTLS_PKCS11_OBJ_FLAG_COMPARE (1<<9) /* The object must be fully compared */
 #define GNUTLS_PKCS11_OBJ_FLAG_PRESENT_IN_TRUSTED_MODULE (1<<10) /* The object must be present in a marked as trusted module */
 #define GNUTLS_PKCS11_OBJ_FLAG_MARK_CA (1<<11)	/* object marked as CA */
+#define GNUTLS_PKCS11_OBJ_FLAG_KEY_WRAP (1<<12) /* generated keypair shall support key wrap/unwrap */
 
 /**
  * gnutls_pkcs11_url_type_t:
diff --git a/lib/pkcs11_privkey.c b/lib/pkcs11_privkey.c
index a9c473e..5575efc 100644
--- a/lib/pkcs11_privkey.c
+++ b/lib/pkcs11_privkey.c
@@ -655,6 +655,7 @@ gnutls_pkcs11_privkey_generate2(const char *url, gnutls_pk_algorithm_t pk,
 	gnutls_pkcs11_obj_t obj = NULL;
 	gnutls_datum_t der = {NULL, 0};
 	ck_key_type_t key_type;
+	char pubEx[3] = { 1,0,1 }; // 65537 = 0x10001
 
 	PKCS11_CHECK_INIT;
 
@@ -710,6 +711,12 @@ gnutls_pkcs11_privkey_generate2(const char *url, gnutls_pk_algorithm_t pk,
 		a[a_val].value = &_bits;
 		a[a_val].value_len = sizeof(_bits);
 		a_val++;
+
+		a[a_val].type = CKA_PUBLIC_EXPONENT;
+		a[a_val].value = pubEx;
+		a[a_val].value_len = sizeof(pubEx);
+		a_val++;
+
 		break;
 	case GNUTLS_PK_DSA:
 		p[p_val].type = CKA_SIGN;
@@ -760,6 +767,20 @@ gnutls_pkcs11_privkey_generate2(const char *url, gnutls_pk_algorithm_t pk,
 		goto cleanup;
 	}
 
+	/*
+	 * on request, add the CKA_WRAP/CKA_UNWRAP key attribute
+	 */
+	if (flags & GNUTLS_PKCS11_OBJ_FLAG_KEY_WRAP) {
+		p[p_val].type = CKA_UNWRAP;
+		p[p_val].value = (void*)&tval;
+		p[p_val].value_len = sizeof(tval);
+		p_val++;
+		a[a_val].type = CKA_WRAP;
+		a[a_val].value = (void*)&tval;
+		a[a_val].value_len = sizeof(tval);
+		a_val++;
+	}
+
 	/* a private key is set always as private unless
 	 * requested otherwise
 	 */
-- 
1.9.3



More information about the Gnutls-devel mailing list