David Woodhouse | 18 Dec 18:08 2014

PKCS#11 usability improvements

The current situation for users who just want to *use* a certificate+key
that resides in a PKCS#11 token is horrid.

Different pieces of client software are configured in *entirely*
different ways, have entirely different methods of specifying which
PKCS#11 object should be used. Sometimes it even varies for *one* given
piece of software depending on which crypto library it's been built with
today.

I'm trying to make that saner, in two ways.

Firstly, it shouldn't be necessary to explicitly specify the PKCS#11
provider module to load. Modern systems will have p11-kit which has a
registry of the tokens which should be visible. And p11-kit-proxy.so
which is a PKCS#11 provider in its own right, which loads the configured
PKCS#11 tokens and represents each of *their* slots as a slot of its own
to the calling application.

So it's really easy for client applications to just use p11-kit-proxy.so
by default if it's available, instead of failing and saying "ERROR: user
*must* explicitly tell me which PKCS#11 provider to use". 

That's the first step in making things saner for users. The second is to
use a consistent way of specifying which cert/key we want to use. I was
rather disturbed to find that even *within* the OpenSC project there are
two incompatible forms; pkcs11-helper uses something which looks like
piv_II/PKCS\x2315\x20emulated/108421384210c3f5/PIV_II\x20\x28PIV\x20Card\x20Holder\x20pin\x29/01
while the OpenSSL engine_pkcs11 may refer to the same object as
slot_19-key_01

(Continue reading)

Douglas E Engert | 17 Dec 22:48 2014
Picon

New OpenSSL, BIGNUM and sc-hsm-tool.c

While trying to build OpenSC against OpenSSL from github, sc-hsm-tool.c does not compile.
OpenSSL is moving the definition of the BIGNUM structure to a hidden header file: ./crypto/bn/bn_lcl.h
sc-hsm-tool.c needs to use BIGNUM * and the API to access a BIGNUM structure.

--

-- 

  Douglas E. Engert  <DEEngert <at> gmail.com>

------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
Douglas E Engert | 17 Dec 18:07 2014
Picon

Re: PKCS#11, ECC and OpenSSL

Thanks, Rich.

Rich Salz introduced David and myself to Tim Hudson, who then responded via private mail.

Tim reported:

"There is an implementation of the ECDSA_METHOD approach in master and in
1.0.2 - I've updated RT2459 to make that clear.
There is no init and final as they don't belong - as for the EC handling
the interface does not generate the key or manage the key internals -
the EC code is rather different and the EC_KEY is where the method
specific hooks for key type handling are."

So I am going to test the ECDSA code in libp11 and opensc-engine against
OpenSSL master.

  http://rt.openssl.org/Ticket/Display.html?id=2459

Only asked for ECDSA, and not ECDH. The OpenSC/libp11 should also work with ECDH
for key derivation. I need so see if the ECDSA code in OpenSSL  works, and
what is need to add matching ECDH changes in OpenSSL.  (These are different methods.)

(In my option, main reason for ECDSA in the engine is to allow OpenSSL
to create a certificate request signed by the EC key on a smartcard.
It would also allow the OpenSSL CA to use a smart card (or HSM) for the CA
key if it was an EC key.)

Tim also asked:
"Is there a soft token which supports ECDSA which you have worked with
that this code matches? And a simple test procedure?
(Continue reading)

Douglas E Engert | 15 Dec 15:19 2014
Picon

OpenSC Minidriver MSI improvements

In response to the second half of: Re: [OpenSC] Implementation of card_ctl function with
SC_CARDCTL_GET_SERIALNR (#321)
I would like to make it a separate thread. The minidriver MSI install process could be improved and apply to all
OpenSC supported cards that can provide a serial number.

On 12/15/2014 6:31 AM, Shaun Schutte wrote:
>
> There is however another question I have regarding Jenkins:
> Is there a way to get a hold of the binary OpenSC builds that come without the msi installer? The reason for
this is that if we package the 32 bit and 64 bit installers together using a tool like
> "Advanced Installer" things get a little messy. Since we need to install both the 32 bit and 64 bit for
Windows 7 64 bit, it would be more user friendly to have a single installer that does all of
> this, so normal "users" would need to click install once and all the magic happens in the background. I am no
expert at this, and I am sure the OpenSC devs have some sort of guidelines and rules for
> this so I was wondering what might work best?

Since on Windows 64 bit, most people would want both 64 and 32 bit, the default MSI for 64 should install both.

   http://msdn.microsoft.com/en-us/library/windows/hardware/dn468773(v=vs.85).aspx

Is a good starting point for how Windows handles smart cards.
This shows an example of a combined 64 and 32 bit INF file:

   http://msdn.microsoft.com/en-us/library/windows/hardware/dn468780(v=vs.85).aspx

In the example, (and drivers I have seen form vendors), there is both a
Fabrikamcm.dll and Fabrikamcm64.dll file. i.e. a 32 bit and a 64 bit version of the dll with different names
even when stored in a different directories.

It would take someone who understands the MSI process, and could test it. (I am not in a position to do this.)
(Continue reading)

Picon

OpenSC importing certificates under Windows


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi List,

I was wondering how OpenSC handles the importing of certificates on
smart cards under Windows? Does this occur automatically when the smart
card is inserted into the reader?

To be more specific, does the minidriver import the certificates into
the Windows cert store?

Over the last few weeks I have been testing the national health
insurance card of the province of Bolzano in Italy (this also applies to
the province of Trentino in Italy) and after the patch written by
Roberto Resoli everything is functioning correctly using Windows 7.
(https://github.com/OpenSC/OpenSC/pull/321)
Upon insertion in Windows 7, the certificate gets loaded into the
Windows cert store automatically, however this does not seem to be the
case using Windows 8. I can import the certificate manually but was
wondering why the behavior is now somewhat different.

Does anybody here maybe have an idea what it could be?

Thanks in advance,

- --
shaun
-----BEGIN PGP SIGNATURE-----
(Continue reading)

Douglas E Engert | 10 Dec 21:51 2014
Picon

Re: PKCS#11, ECC and OpenSSL


On 12/10/2014 1:59 PM, Salz, Rich wrote:
> Hi Doug, I certainly remember you!

>
>>     https://github.com/OpenSC/engine_pkcs11
>
> Is this something you'd want bundled into openssl or kept separate like it is now?

That's a good question and would hope other OpenSC developers would respond too.

   https://github.com/OpenSC/engine_pkcs11

depends on

https://github.com/OpenSC/libp11

There are experimental ECDSA and ECDH mods to libp11 described here:

   https://github.com/dengert/libp11/commit/88f980d864d45e9e72591bad99ac56641bf4516a

I have not made a pull request, as I was expecting to get the OpenSSL part done first as described in:

http://rt.openssl.org/Ticket/Display.html?id=2568

>
>> And it works well with RSA keys, but the support in OpenSSL needed for ECC
>> keys has stalled in OpenSSL ticket #2568
>
> So we're doing the exercise of making as much as possible opaque datatypes for the 1.1 release (master
(Continue reading)

David Woodhouse | 10 Dec 21:06 2014

Re: PKCS#11, ECC and OpenSSL

On Wed, 2014-12-10 at 14:59 -0500, Salz, Rich wrote:
> Hi Doug, I certainly remember you!
> 
> >    https://github.com/OpenSC/engine_pkcs11
> 
> Is this something you'd want bundled into openssl or kept separate like it is now?

Personally, I'd like it to be bundled.

We really want to get to the point where any application which can take
certificates/keys from files can *also* accept a PKCS#11 URI as
described at https://tools.ietf.org/html/draft-pechanec-pkcs11uri-16 and
will find it from any of the PKCS#11 modules configured in the system's
p11-kit installation. This stuff should Just Work™.

All the manual specification of which library module to load, and the
weird ad-hoc formats for how you describe which object to use, must die.

OpenSSL is the last major crypto library that *doesn't* support PKCS#11
as a first-class citizen, and it would be really good to fix that by
making the PKCS#11 engine available by default.

I'm happy to spend some time working on that.

--

-- 
dwmw2
Attachment (smime.p7s): application/x-pkcs7-signature, 7762 bytes
------------------------------------------------------------------------------
(Continue reading)

Douglas E Engert | 10 Dec 20:24 2014
Picon

PKCS#11, ECC and OpenSSL


Rich,
I believe we corresponded starting over OSF-DCE, GSS-API, Kerberos and and IETF lists as well.
I see you are very active in OpenSSL now.

I am retired now from Argonne National Lab, but still active with the OpenSC project,
mostly with the NIST 800-73 PIV smart cards. These cards and others support ECC as well as RSA keys.

On the OpenSC mailing list there is a discussion about OpenSSL and PKCS#11 and OpenSSL bug #11 from 2002 came up:

  http://rt.openssl.org/Ticket/Display.html?id=11

I see you rejected it on Sept 10, 2014 but you said in the comments:
"Having said that (twice, actually), a PKCS11 ENGINE would be a cool thing to have."

OpenSC has had a PKCS11 Engine for years:

   https://github.com/OpenSC/engine_pkcs11

And it works well with RSA keys, but the support in OpenSSL needed for ECC keys has stalled
in OpenSSL ticket #2568

    http://rt.openssl.org/Ticket/Display.html?id=2568

Any chance to get this moving?
maybe with a different approach?

--

-- 

  Douglas E. Engert  <DEEngert <at> gmail.com>
(Continue reading)

William Roberts | 9 Dec 23:44 2014
Picon

How to use symmetric 9E key on PIV

I have a PIV card with a symmetric 9E key, is their some way to use this to encrypt data with any of the opensc, pkcs11 or openssl commands?

--
Respectfully,

William C Roberts

------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
_______________________________________________
Opensc-devel mailing list
Opensc-devel <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/opensc-devel
David Woodhouse | 9 Dec 22:24 2014

[WIP PATCH] Add support for standard PKCS#11 URIs to ENGINE_PKCS11

Here's a proof of concept which makes it use p11-kit-proxy.so if not
told to use anything different (which is a bit of a hack and definitely
at least needs to *find* it in $libdir or something). And also parses
PKCS#11 URIs for the key location.

Ideally I think we want to be using p11-kit's functions for loading the
modules, not p11-kit-proxy. But this is a start.

With a corresponding patch¹ to the client application I can now use
simple PKCS#11 URIs for keys when using OpenSSL just as I can when I
build with GnuTLS.

diff --git a/configure.ac b/configure.ac
index b67256e..84178ad 100644
--- a/configure.ac
+++ b/configure.ac
 <at>  <at>  -109,6 +109,7  <at>  <at>  AC_CHECK_HEADERS([ \
 ])

 PKG_CHECK_MODULES([LIBP11], [libp11 >= 0.2.5],, [AC_MSG_ERROR([libp11 >= 0.2.5 is required])])
+PKG_CHECK_MODULES([P11KIT], [p11-kit-1],, [AC_MSG_ERROR([p11-kit-1 is required])])

 PKG_CHECK_MODULES(
 	[OPENSSL],
diff --git a/src/Makefile.am b/src/Makefile.am
index 72a3ffe..404a63d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
 <at>  <at>  -18,8 +18,8  <at>  <at>  else
 dist_noinst_DATA = versioninfo.rc
 endif
 engine_pkcs11_la_CFLAGS = $(AM_CFLAGS) $(OPENSSL_EXTRA_CFLAGS) $(OPENSSL_CFLAGS) \
-	$(LIBP11_CFLAGS)
-engine_pkcs11_la_LIBADD = $(ENGINE_LINK) $(OPENSSL_LIBS) $(LIBP11_LIBS)
+	$(LIBP11_CFLAGS) $(P11KIT_CFLAGS)
+engine_pkcs11_la_LIBADD = $(ENGINE_LINK) $(OPENSSL_LIBS) $(LIBP11_LIBS) $(P11KIT_LIBS)
 engine_pkcs11_la_LDFLAGS = $(AM_LDFLAGS) $(OPENSSL_EXTRA_LDFLAGS) \
 	-module -shared -avoid-version \
 	-export-symbols "$(srcdir)/engine_pkcs11.exports" \
diff --git a/src/engine_pkcs11.c b/src/engine_pkcs11.c
index c1b8fbb..5b28d89 100644
--- a/src/engine_pkcs11.c
+++ b/src/engine_pkcs11.c
 <at>  <at>  -34,6 +34,8  <at>  <at> 
 #include <libp11.h>
 #include "engine_pkcs11.h"

+#include <p11-kit/uri.h>
+
 #ifdef _WIN32
 #define strncasecmp strnicmp
 #endif
 <at>  <at>  -56,7 +58,7  <at>  <at>  static int pin_length = 0;

 static int verbose = 0;

-static char *module = NULL;
+static char *module = "/usr/lib64/p11-kit-proxy.so";

 static char *init_args = NULL;

 <at>  <at>  -538,6 +540,28  <at>  <at>  int load_cert_ctrl(ENGINE * e, void *p)
 	return 1;
 }

+static int p11_match(const char *tokstr, const char *matchstr, size_t tokstrlen)
+{
+	int matchstrlen, i;
+
+	if (!matchstr)
+		return 0;
+
+	matchstrlen = strlen(matchstr);
+
+	if (matchstrlen > tokstrlen)
+		return 0;
+
+	if (strncmp(matchstr, tokstr, matchstrlen))
+		return 0;
+
+	for (i = matchstrlen; i < tokstrlen; i++)
+		if (tokstr[i] != ' ')
+			return 0;
+
+	return 1;
+}
+
 static EVP_PKEY *pkcs11_load_key(ENGINE * e, const char *s_slot_key_id,
 				 UI_METHOD * ui_method, void *callback_data,
 				 int isPrivate)
 <at>  <at>  -548,6 +572,8  <at>  <at>  static EVP_PKEY *pkcs11_load_key(ENGINE * e, const char *s_slot_key_id,
 	PKCS11_KEY *keys, *selected_key = NULL;
 	PKCS11_CERT *certs;
 	EVP_PKEY *pk;
+	P11KitUri *uri;
+	CK_ATTRIBUTE *uri_id = NULL, *uri_label = NULL;
 	unsigned int slot_count, cert_count, key_count, n, m;
 	unsigned char key_id[MAX_VALUE_LEN / 2];
 	size_t key_id_len = sizeof(key_id);
 <at>  <at>  -556,6 +582,24  <at>  <at>  static EVP_PKEY *pkcs11_load_key(ENGINE * e, const char *s_slot_key_id,
 	char flags[64];

 	if (s_slot_key_id && *s_slot_key_id) {
+		if (!strncmp(s_slot_key_id, "pkcs11:", 7)) {
+			uri = p11_kit_uri_new();
+			if (p11_kit_uri_parse(s_slot_key_id, P11_KIT_URI_FOR_ANY, uri)) {
+				fprintf(stderr,
+					"Failed to parse PKCS#11 URI\n");
+				p11_kit_uri_free(uri);
+				return NULL;
+			}
+			uri_id = p11_kit_uri_get_attribute(uri, CKA_ID);
+			if (uri_id && uri_id->ulValueLen <= sizeof(key_id)) {
+				key_id_len = uri_id->ulValueLen;
+				memcpy(key_id, uri_id->pValue, uri_id->ulValueLen);
+			} else
+				key_id_len = 0;
+			uri_label = p11_kit_uri_get_attribute(uri, CKA_LABEL);
+			if (uri_label)
+				key_label = strdup(uri_label->pValue);
+		} else {
 		n = parse_slot_id_string(s_slot_key_id, &slot_nr,
 					 key_id, &key_id_len, &key_label);

 <at>  <at>  -580,6 +624,7  <at>  <at>  static EVP_PKEY *pkcs11_load_key(ENGINE * e, const char *s_slot_key_id,
 			} else
 				fprintf(stderr, "label: %s\n", key_label);
 		}
+		}
 	}

 	if (PKCS11_enumerate_slots(ctx, &slot_list, &slot_count) < 0)
 <at>  <at>  -611,6 +656,22  <at>  <at>  static EVP_PKEY *pkcs11_load_key(ENGINE * e, const char *s_slot_key_id,
 		if (slot_nr != -1 &&
 			slot_nr == PKCS11_get_slotid_from_slot(slot)) {
 			found_slot = slot;
+		} else if (uri && slot->token) {
+			CK_TOKEN_INFO *tokinfo = p11_kit_uri_get_token_info(uri);
+			int match = 1;
+
+			if (tokinfo->label[0])
+				match = p11_match(tokinfo->label, slot->token->label, sizeof(tokinfo->label));
+			if (match && tokinfo->manufacturerID[0])
+				match = p11_match(tokinfo->manufacturerID, slot->token->manufacturer, sizeof(tokinfo->manufacturerID));
+			if (match && tokinfo->model[0])
+				match = p11_match(tokinfo->model, slot->token->model, sizeof(tokinfo->model));
+			if (match && tokinfo->serialNumber[0])
+				match = p11_match(tokinfo->serialNumber, slot->token->serialnr, sizeof(tokinfo->serialNumber));
+			if (match) {
+				slot_nr = PKCS11_get_slotid_from_slot(slot);
+				found_slot = slot;
+			}
 		}

 		if (verbose) {
 <at>  <at>  -755,22 +816,22  <at>  <at>  static EVP_PKEY *pkcs11_load_key(ENGINE * e, const char *s_slot_key_id,
 	if (s_slot_key_id && *s_slot_key_id && (key_id_len != 0 || key_label != NULL)) {
 		for (n = 0; n < key_count; n++) {
 			PKCS11_KEY *k = keys + n;
+			int match = 1;

 			if (verbose) {
 				fprintf(stderr, "  %2u %c%c %s\n", n + 1,
 					k->isPrivate ? 'P' : ' ',
 					k->needLogin ? 'L' : ' ', k->label);
 			}
-			if (key_label == NULL) {
-				if (key_id_len != 0 && k->id_len == key_id_len
-				    && memcmp(k->id, key_id, key_id_len) == 0) {
-					selected_key = k;
-				}
-			} else {
-				if (strcmp(k->label, key_label) == 0) {
-					selected_key = k;
-				}
+			if (key_id_len != 0 && (k->id_len != key_id_len ||
+						memcmp(k->id, key_id, key_id_len) != 0)) {
+				match = 0;
+			}
+			if (key_label && strcmp(k->label, key_label) != 0) {
+				match = 0;
 			}
+			if (match)
+				selected_key = k;
 		}
 	} else {
 		selected_key = keys;	/* use first */
 <at>  <at>  -790,6 +851,8  <at>  <at>  static EVP_PKEY *pkcs11_load_key(ENGINE * e, const char *s_slot_key_id,
 	}
 	if (key_label != NULL)
 		free(key_label);
+	if (uri)
+		p11_kit_uri_free(uri);
 	return pk;
 }

--

-- 
dwmw2
¹ http://david.woodhou.se/openconnect-engine-pkcs11.patch
Attachment (smime.p7s): application/x-pkcs7-signature, 7762 bytes
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
_______________________________________________
Opensc-devel mailing list
Opensc-devel <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/opensc-devel
David Woodhouse | 4 Dec 13:51 2014

Why do PIV key and cert labels not match?

It's almost like this stuff is *designed* to be hard to use.

OK, in my ignorance I happened to suggest once in documentation that
"the [PKCS#11] URLs for the key and the cert should differ only in that
the key will contain the attribute ';object-type=private', while the
certificate will have ';object-type=cert'."

.... and thus I suggested that users strip the object-type= part from
the URI and just use that, and the application can use it for finding
both cert and key.

This is of course wrong for PIV where the labels differ too:

Object 0:
        URL: pkcs11:model=PKCS%2315%20emulated;manufacturer=piv_II;serial=06b508843810d7f6;token=PIV_II%20%28PIV%20Card%20Holder%20pin%29;id=%01;object=PIV%20AUTH%20key;object-type=private
        Type: Private key
        Label: PIV AUTH key
        Flags: CKA_WRAP/UNWRAP; CKA_PRIVATE; CKA_SENSITIVE; 
        ID: 01

Object 2:
        URL: pkcs11:model=PKCS%2315%20emulated;manufacturer=piv_II;serial=06b508843810d7f6;token=PIV_II%20%28PIV%20Card%20Holder%20pin%29;id=%01;object=Certificate%20for%20PIV%20Authentication;object-type=cert
        Type: X.509 Certificate
        Label: Certificate for PIV Authentication
        ID: 01

And thus I just had to help a very confused user...
http://lists.infradead.org/pipermail/openconnect-devel/2014-December/002452.html

Should the application have said "oh, there is a key matching that
specification but no cert; I'll try dropping the explicitly specified
label and see if I can find a matching cert then...?"  I can implement
that logic, but by $DEITY it'll make me sad...

--

-- 
David Woodhouse                            Open Source Technology Centre
David.Woodhouse <at> intel.com                              Intel Corporation
Attachment (smime.p7s): application/x-pkcs7-signature, 7762 bytes
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
_______________________________________________
Opensc-devel mailing list
Opensc-devel <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/opensc-devel

Gmane