Skip to contentSkip to conversation

patch pkcs11 dynamic key id detection

Openvpn-devel Christophe Vandeplas 2 participants 6 messages Mar 16, 2008 – Mar 17, 2008

lore
Share & tools
lore
#0 lore raw

Hi all,

From the mailinglist I'm not the first one that wanted dynamic key-id detection for pkcs11 authentication.

I first started to write a script, but it had to be platform independent. Working on two scripts (bash and vbs) was stupid as they really became to big and created extra security risks.

That's why I started to work on this patch.
(see attach)

In short this is what I do:
- New pkcs11-match variable in the openvpn.conf. This is the substring of the key-id you want to match. It works with a 'first match wins' principle.

- options.c => I add the variable and the checks, as it's either pkcs11-id or pkcs11-match

- ssl.c => if pkcs11_match is set, do the checks and give error if necessary. Store the key-id in options->pkcs11_id.
I had to do some black magic here. options->pkcs11_id is defined as const. So I had to un-const this variable to be able to change its value. This is just a compiler-bypass but a long-term solution should be sought by changing some parts of the design of the const-ness of some variables. I think this is something _you_ should decide.

- pkcs11.c => I copy-pasted some code of the find-pkcs11-ids(), refactored it and added a check for the substring.
First I wanted to add a regex-check, but then I had to depend on new libs and that's probably something you do not want. The behavior is the same as the cryptoapicert variable for windows. (where also a substring is needed).

I did my best to try to prevent memory leaks, but please double check that. Worst case only a few bytes should be lost, but hey better check :-)

Thanks for considering including this patch. I (and probably other people) _need_ this functionality to make openVPN more userfriendly. The computer/config will not be locked to the smartcard. This enables people to:
- deploy the same configuration on different computers
- log-in to the tunnel with different cards on the same machine. (different users that share the same computer)
- With eID's (what I use) the key-id contains similar characteristics.
(like mine where 'BELPIC\x20\x28Basic\x20PIN\x29/02' is the end of the Authentication key. Axalto/Belgium\x20eID/6CFF2491AB111E14/BELPIC\x20\x28Basic\x20PIN\x29/02 )

All input on the patch is welcome.

Thanks

Christophe

PS: It's for my OpenVPN-plans with the eID...
<http://christophe.vandeplas.com/2008/02/03/openvpn-belgian-eid>
<http://christophe.vandeplas.com/2008/02/08/database-authorization-openvpn-eid>

Signature · 2 lines
mailto:[email protected]...
http://christophe.vandeplas.com
options.c +11 -2
diff -Naur openvpn-2.1_rc7/options.c openvpn/options.c
@@ -520,6 +520,7 @@
   "--pkcs11-pin-cache seconds  : Number of seconds to cache PIN. The default is -1\n"
   "                              cache until token is removed.\n"
   "--pkcs11-id serialized-id   : Identity to use, get using standalone --show-pkcs11-ids\n"
+  "--pkcs11-match string       : String to match the Identity to use, see pkcs11-id\n"
 #endif			/* ENABLE_PKCS11 */
  "\n"
   "SSL Library information:\n"
@@ -1766,8 +1767,11 @@
       if (options->pkcs11_providers[0])
        {
         notnull (options->ca_file, "CA file (--ca)");
-	notnull (options->pkcs11_id, "PKCS#11 id (--pkcs11-id)");
-
+//	notnull (options->pkcs11_id, "PKCS#11 id (--pkcs11-id)");
+	if (!options->pkcs11_id && !options->pkcs11_match)
+	  msg(M_USAGE, "You must define PKCS#11 id (--pkcs11-id) or string to match (--pkcs11-match).");
+	if (options->pkcs11_id && options->pkcs11_match)
+	  msg(M_USAGE, "Parameter --pkcs11-match cannot not be used when --pkcs11-id is also specified.");
 	if (options->cert_file)
 	  msg(M_USAGE, "Parameter --cert cannot be used when --pkcs11-provider is also specified.");
 	if (options->priv_key_file)
@@ -5137,6 +5141,11 @@
       VERIFY_PERMISSION (OPT_P_GENERAL);
       options->pkcs11_id = p[1];
     }
+  else if (streq (p[0], "pkcs11-match") && p[1])
+  {
+      VERIFY_PERMISSION (OPT_P_GENERAL);
+      options->pkcs11_match = p[1];
+  }
 #endif
 #ifdef TUNSETPERSIST
   else if (streq (p[0], "rmtun"))
options.h +1 -0
diff -Naur openvpn-2.1_rc7/options.h openvpn/options.h
@@ -419,6 +419,7 @@
   bool pkcs11_cert_private[MAX_PARMS];
   int pkcs11_pin_cache_period;
   const char *pkcs11_id;
+  const char *pkcs11_match;
 #endif
 
 #ifdef WIN32
pkcs11.c +129 -0

135 lines not on this page · raw message

pkcs11.h +6 -0
diff -Naur openvpn-2.1_rc7/pkcs11.h openvpn/pkcs11.h
@@ -58,6 +58,12 @@
 	const char * const pkcs11_id
 );
 
+char *
+find_pkcs11_id (
+	const char * const pkcs11_match
+);
+
+
 void
 show_pkcs11_ids (
 	const char * const provider,
ssl.c +14 -2
diff -Naur openvpn-2.1_rc7/ssl.c openvpn/ssl.c
@@ -30,6 +30,8 @@
  * over the same TCP/UDP port.
  */
 
+# define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
+
 #ifdef WIN32
 #include "config-win32.h"
 #else
@@ -1075,7 +1077,7 @@
  * All files are in PEM format.
  */
 SSL_CTX *
-init_ssl (const struct options *options)
+init_ssl (const struct options *options) 
 {
   SSL_CTX *ctx = NULL;
   DH *dh;
@@ -1195,10 +1197,20 @@
   else
     {
       /* Use seperate PEM files for key, cert and CA certs */
-
+	  
 #ifdef ENABLE_PKCS11
       if (options->pkcs11_providers[0])
         {
+	 if (options->pkcs11_match)
+	   {
+		 __UNCONST( options->pkcs11_id) = find_pkcs11_id(options->pkcs11_match);
+		 msg (M_WARN, "PKCS#11-id has been found: %s", options->pkcs11_id);
+		 
+		 if (options->pkcs11_id==NULL) {
+			 msg (M_WARN, "Cannot find valid certificate ID using PKCS#11 interface and string to match");
+			 goto err;
+		 }
+	   }
          /* Load Certificate and Private Key */
 	 if (!SSL_CTX_use_pkcs11 (ctx, options->pkcs11_id))
 	   {
#1 lore raw

Hello,

I did not realize how many people depend on the low-level PKCS#11 properties.

I thought I prospone this to next OpenVPN version, but I see more an more people need "single configuration file" option.

I first thought to create a filter similar to what you suggest... But users would like to choose certificate based on certificate authority, EKU, UPN or any other field, and OpenVPN is already complex enough.

Then I thought I would execute some external hook, but OpenVPN does not have the ability to send large amount of data to hooks, and receive data from hooks.

So back to my original idea... Use the management interface to prompt the user to select a certificate.

You may find it at [1].

It introduces pkcs11-id-management configuration option and NEED-STR real-time management message, that is responded with the PKCS#11 identity.

It also introduced two management commands: pkcs11-id-count, pkcs11-id-get.

The sequence is as follows:

Quoted 1 line

NEED-STR:Need 'pkcs11-id-request' string MSG:Please specify PKCS#11 id to use

pkcs11-id-count

Quoted 1 line

PKCS11ID-COUNT:5

pkcs11-id-get 0

Quoted 1 line

PKCS11ID-ENTRY:'0', ID:'<snip>', BLOB:'<snip>'

pkcs11-id-get 1

Quoted 1 line

PKCS11ID-ENTRY:'1', ID:'<snip>', BLOB:'<snip>'

needstr 'pkcs11-id-request' '<snip>'

This allows a management application to inspect available certificates, and prompt the user to choose the correct one.

James, some notes:
1. Please acknowledge the NEED-STR addition. it is in the first revision.
2. I had to increase the USER_PASS_LEN constant, as it is also used for strings now.
3. I had to increase the ERR_BUF_SIZE constant, as I need to output a complete base64 certificate.

I hope this helps,
Alon Bar-Lev.

[1] svn diff -r 2844:2849
http://svn.openvpn.net/projects/openvpn/contrib/alon/BETA21/openvpn

Quoted 5 lines · Christophe Vandeplas

Hi all,

From the mailinglist I'm not the first one that wanted dynamic key-id
detection for pkcs11 authentication.

#2 lore raw
Quoted 2 lines

So back to my original idea... Use the management interface to prompt the user to select a certificate.

What does that mean for the end-user? If I understand it correctly the user will need to do the following:
- start tunnel/openvpn
- open webbrowser and select certificate
- enter key (where? webinterface?, cli?)

The thing is that some implementations of middleware (the Belgian eID middleware, based on opensc) prompts the user with a GUI windows for entering the password. (at least on MS Windows, not on my mac). This window is started from the opensc layer and thus independent of the layer above (openvpn). (please correct me if I'm wrong)

With this situation it really becomes to complex for the end-user:
- start tunnel (place 1)
- open browser select certificate (place2)
- enter passwd in separate window (place 3)
The user has 3 different locations where input is needed.

With my proposed patch, the user has only two different things to do.
- start tunnel
- enter key in cli or GUI window
There are no complex certificate selections, as most of the users don't even understand what they are. Isn't it the sys/net-admin that should configure that pkcs11-id or pkcs11-match string?

I liked the principle of the cryptoapicert hook in windows where the admin enters a part of the string to match. (Unfortunately I can't get it working on that Windows machine.. weird SSL issue.. but that's something for another mail )

I plan to work on v2 of my patch to enable the 'match' on either the DN or the serialised-id. This way it becomes even more flexible and powerful.
But well, except if there is no hope of seeing this patch in the main trunk in the future ...

What do you think?

Isn't it also a possibility to include both ways? If yes I think we should rewrite it a little to prevent duplicate code as much as possible...

Cheers

Christophe

#3 lore raw
Quoted 9 lines · Christophe Vandeplas
Quoted 2 lines

So back to my original idea... Use the management interface to prompt the user to select a certificate.

What does that mean for the end-user? If I understand it correctly the
user will need to do the following:
- start tunnel/openvpn
- open webbrowser and select certificate
- enter key (where? webinterface?, cli?)

No no no... The OpenVPN GUI should handle all this... Prompt the user with a list of certificates for him to choose.
The OpenVPN GUI may also be improved to execute some helper script to filter out the list.

BTW: When I write OpenVPN GUI, I refer to any UI that uses the management interface to control OpenVPN. A simple .NET application may be written to do so in little effort.

Quoted 5 lines

The thing is that some implementations of middleware (the Belgian eID middleware, based on opensc) prompts the user with a GUI windows for entering the password. (at least on MS Windows, not on my mac). This window is started from the opensc layer and thus independent of the layer above (openvpn). (please correct me if I'm wrong)

I believe you are wrong and referring to the CSP alternative. PKCS#11 provider should not issue UI.

Quoted 5 lines

With this situation it really becomes to complex for the end-user:
- start tunnel (place 1)
- open browser select certificate (place2)
- enter passwd in separate window (place 3)
The user has 3 different locations where input is needed.

No...
Start a tunnel using the OpenVPN GUI.
The OpenVPN GUI will prompt the user to select a certificate (If have more than one... maybe it will preselect it).
The OpenVPN GUI will prompt for passphrase.

Quoted 6 lines

With my proposed patch, the user has only two different things to do.
- start tunnel
- enter key in cli or GUI window
There are no complex certificate selections, as most of the users don't even understand what they are. Isn't it the sys/net-admin that should configure that pkcs11-id or pkcs11-match string?

If you take Microsoft certificate store for example. Internet explorer will prompt the user to select correct certificate when required. Just like in the proposed scenario.

Quoted 5 lines

I plan to work on v2 of my patch to enable the 'match' on either the DN or the serialised-id. This way it becomes even more flexible and powerful.
But well, except if there is no hope of seeing this patch in the main trunk in the future ...

I believe that adding more complexity into the OpenVPN daemon is not correct approach. For this reason we have the management interface, that enable to deligate user interaction into separate solution.

Quoted 5 lines

What do you think?

Isn't it also a possibility to include both ways? If yes I think we should rewrite it a little to prevent duplicate code as much as possible...

I don't understand why the management interface solution is not sufficient.

Alon.

#4 lore raw
Quoted 12 lines · Alon Bar-Lev
Quoted 9 lines · Christophe Vandeplas
Quoted 2 lines

So back to my original idea... Use the management interface to prompt the user to select a certificate.

What does that mean for the end-user? If I understand it correctly the
user will need to do the following:
- start tunnel/openvpn
- open webbrowser and select certificate
- enter key (where? webinterface?, cli?)

Quoted 4 lines

No no no... The OpenVPN GUI should handle all this... Prompt the user
with a list of certificates for him to choose.
The OpenVPN GUI may also be improved to execute some helper script to filter out the list.

Looks I misunderstood the management interface. I always understood it was the web-management... What you are referring to looks great !!

Quoted 8 lines
Quoted 5 lines

The thing is that some implementations of middleware (the Belgian eID middleware, based on opensc) prompts the user with a GUI windows for entering the password. (at least on MS Windows, not on my mac). This window is started from the opensc layer and thus independent of the layer above (openvpn). (please correct me if I'm wrong)

I believe you are wrong and referring to the CSP alternative. PKCS#11
provider should not issue UI.

I should take a deeper look in the sources of the belgian middleware to confirm this theory...

Quoted 6 lines
Quoted 3 lines

Isn't it also a possibility to include both ways? If yes I think we should rewrite it a little to prevent duplicate code as much as possible...

I don't understand why the management interface solution is not sufficient.

Just a misunderstanding from my side with the certificate selector/management and a web-interface... Having such a management (gui,cli) is a great idea.

A question might be: Is there need for a system like the patch I wrote, knowing your management patch should be included?

Here's my likes-dislikes (like I understand it):
pkcs11-match:
- more code
+ transparent thingie for the end-user, no need to understand concepts of certificates
pkcs11-magement:
+ user has choice of cert
- user has choice of cert

I'll have a deeper look at your management patch, maybe I can find a way to add the pkcs11-match principle in your code with almost no extra complexity and redundant code.
Do you think it's worth the effort? Or better wait for the results and feedback from the management part?

Thanks for your interest and input !

Christophe

#5 lore raw
Quoted 5 lines · Christophe Vandeplas

I'll have a deeper look at your management patch, maybe I can find a way to add the pkcs11-match principle in your code with almost no extra complexity and redundant code.
Do you think it's worth the effort? Or better wait for the results and feedback from the management part?

I think that what is missing for Windows user is a good OpenVPN GUI. A simple tray icon with context menu that enable the user to connect/disconnect/authenticate/select certificate/prompt for ok

Many Windows users expects this for a long time... And I know that implementing such using .NET framework is easy... Some forms, socket and state machine.

This OpenVPN GUI can be extensible so it may call a plugin before user interaction
so sysadmins can configure some of its behavior.

I think that if you wish to invest some efforts for Windows, I think this is the place :)

Alon.