Skip to contentSkip to conversation

PATCH[1/2]: Use certificates from MS Cert Store with CryptoAPI

Openvpn-devel Peter 'Luna' Runestig 2 participants 5 messages Oct 13, 2004 – Oct 18, 2004

lore
Share & tools
lore
#0 lore raw

Hi all!

Let me first say how impressed with the smooth functionality of this excellent piece of software! I have evaluated some other, commercial, SSL/TLS VPN solutions, but this is the best one, and at a much lower price... :-)

What I really wanted to use with OpenVPN, is smart cards for user authentication, on the Microsoft Windows platform. I have written some code that adds the option of using certificates (and their private keys), that resides in the Windows Certificate System Store. That makes it possible to use any smart card, supported by Windows, but also any kind of certificate, residing in the Cert Store, that you have access to the private key of. I have tested this code with a couple of different smart cards (GemSAFE, Cryptoflex and Swedish Post Office eID) on the client side, and also an imported PKCS12 software certificate on the server side.

The code adds a new config option: cryptoapicert To select a certificate, based on a substring search in the certificate's subject:

cryptoapicert "SUBJ:Peter Runestig"

To select a certificate, based on certificate's thumbprint:

cryptoapicert "THUMB:f6 49 24 41 01 b4 ..."

The thumbprint hex string can easily be copy-and-pasted from the Windows Certificate Store GUI.

Unfortunately, this code don't build with MinGW; e.g. the CryptAcquireCertificatePrivateKey() isn't implemented in MinGW, so I had to use Microsoft Visual C++ 6.0. So the patch included in this post is actually my hack to build OpenVPN using MSVC++, which must be applied before the actual CryptoAPI patch, which will be in my next post (I split it in two, so the posts don't get so big).

To build this, using MSVC 6.0:

o Unpack openvpn-2.0_beta11.zip, apply the MSVC patch in this post, and the CryptoAPI patch in my next post.

o If the OpenSSL and/or LZO libraries (built with MSVC or something else that gives you .LIB files) isn't reachable using the INCLUDE and LIB environment variables, edit the 'makefile.w32-vc' file to point them out.

o nmake -f makefile.w32-vc

Here is the MSVC patch:
-----8<----------8<----------8<----------8<----------8<----------8<----------8<-----

config-win32.h +34 -0
diff -uN openvpn-2.0_beta11.orig/config-win32.h openvpn-2.0_beta11.msvc/config-win32.h
@@ -88,7 +88,9 @@
 #define HAVE_CHDIR 1
 
 /* Define to 1 if your compiler supports GNU GCC-style variadic macros */
+#ifndef _MSC_VER /* Defines MSFT compiler version. Defined as 1200 for MSVC++ 6.0. */
 #define HAVE_CPP_VARARG_MACRO_GCC 1
+#endif
 
 /* Define to 1 if you have the <ctype.h> header file. */
 #define HAVE_CTYPE_H 1
@@ -125,7 +127,9 @@
 #define HAVE_STDARG_H 1
 
 /* Define to 1 if you have the <stdint.h> header file. */
+#ifndef _MSC_VER
 #define HAVE_STDINT_H 1
+#endif
 
 /* Define to 1 if you have the <stdio.h> header file. */
 #define HAVE_STDIO_H 1
@@ -146,13 +150,17 @@
 #define HAVE_SYSTEM 1
 
 /* Define to 1 if you have the <sys/file.h> header file. */
+#ifndef _MSC_VER
 #define HAVE_SYS_FILE_H 1
+#endif
 
 /* Define to 1 if you have the <sys/stat.h> header file. */
 #define HAVE_SYS_STAT_H 1
 
 /* Define to 1 if you have the <sys/time.h> header file. */
+#ifndef _MSC_VER
 #define HAVE_SYS_TIME_H 1
+#endif
 
 /* Define to 1 if you have the <sys/types.h> header file. */
 #define HAVE_SYS_TYPES_H 1
@@ -161,7 +169,9 @@
 #define HAVE_TIME 1
 
 /* Define to 1 if you have the <unistd.h> header file. */
+#ifndef _MSC_VER
 #define HAVE_UNISTD_H 1
+#endif
 
 /* Define to 1 if you have the `vsnprintf' function. */
 #define HAVE_VSNPRINTF 1
@@ -204,10 +214,16 @@
 #define STDC_HEADERS 1
 
 /* A string representing our target */
+#ifdef _MSC_VER
+#define TARGET_ALIAS "Win32-MSVC++"
+#else
 #define TARGET_ALIAS "Win32-MinGW"
+#endif
 
 /* Define to 1 if you can safely include both <sys/time.h> and <time.h>. */
+#ifndef _MSC_VER
 #define TIME_WITH_SYS_TIME 1
+#endif
 
 /* Use OpenSSL crypto library */
 #define USE_CRYPTO 1
@@ -244,3 +260,21 @@
 #ifdef USE_PTHREAD
 #error The Windows version of OpenVPN does not support PTHREAD yet
 #endif
+
+#ifdef _MSC_VER
+/* MSVC++ hacks */
+#include <io.h>
+#include <direct.h>
+#define vsnprintf _vsnprintf
+#define vsnwprintf _vsnwprintf
+#define snwprintf _snwprintf
+#define write _write
+#define open _open
+#define read _read
+#define close _close
+#define chdir _chdir
+#define S_IRUSR 0
+#define S_IWUSR 0
+typedef int intptr_t;
+#undef S_NORMAL
+#endif
error.h +3 -1
diff -uN openvpn-2.0_beta11.orig/error.h openvpn-2.0_beta11.msvc/error.h
@@ -135,7 +135,9 @@
 #define HAVE_VARARG_MACROS
 #define msg(flags, args...) do { if (MSG_TEST(flags)) x_msg((flags), args); } while (false)
 #else
-#warning this compiler appears to lack vararg macros which will cause a significant degradation in efficiency (you can ignore this warning if you are using LCLINT)
+# ifndef _MSC_VER	/* MSVC++ doesn't have #warning... */
+# warning this compiler appears to lack vararg macros which will cause a significant degradation in efficiency (you can ignore this warning if you are using LCLINT)
+# endif
 #define msg x_msg
 #endif
 
makefile.w32-vc +167 -0

167 lines not on this page · raw message

otime.c +2 -2
diff -uN openvpn-2.0_beta11.orig/otime.c openvpn-2.0_beta11.msvc/otime.c
@@ -176,7 +176,7 @@
 
       QueryPerformanceFrequency((LARGE_INTEGER *) &frequency);
 
-      counterPerMicrosecond = (double)frequency / 1000000.0f;
+      counterPerMicrosecond = (double) ((__int64) frequency) / 1000000.0f;
 
       time(&t);
       QueryPerformanceCounter((LARGE_INTEGER *) &counter);
@@ -191,7 +191,7 @@
     }
 
   tv->tv_sec = (counter / frequency) + timeSecOffset;
-  tv->tv_usec = ((__int64)(counter / counterPerMicrosecond) % 1000000);
+  tv->tv_usec = ((__int64) (((__int64) counter) / counterPerMicrosecond) % 1000000);
 
   return 0;
 }
tun.c +1 -1
diff -uN openvpn-2.0_beta11.orig/tun.c openvpn-2.0_beta11.msvc/tun.c
@@ -2303,9 +2303,9 @@
 static bool
 get_adapter_ip_netmask (const IP_ADAPTER_INFO *ai, const int n, in_addr_t *ip, in_addr_t *netmask)
 {
+  bool ret = false;
   *ip = 0;
   *netmask = 0;
-  bool ret = false;
 
   if (ai)
     {

-----8<----------8<----------8<----------8<----------8<----------8<----------8<-----

Signature · 5 lines
Peter 'Luna' Runestig (fd. Altberg), Sweden <[email protected]...>
PGP Key ID: 0xD07BBE13
Fingerprint: 7B5C 1F48 2997 C061 DE4B  42EA CB99 A35C D07B BE13
AOL Instant Messenger Screen name: PRunestig
Yahoo! Messenger profile name: altberg
#1 lore raw
Quoted 40 lines · Peter 'Luna' Runestig

Hi all!

Let me first say how impressed with the smooth functionality of this excellent piece of software! I have evaluated some other, commercial, SSL/TLS VPN solutions, but this is the best one, and at a much lower price... :-)

What I really wanted to use with OpenVPN, is smart cards for user authentication, on the Microsoft Windows platform. I have written some code that adds the option of using certificates (and their private keys), that resides in the Windows Certificate System Store. That makes it possible to use any smart card, supported by Windows, but also any kind of certificate, residing in the Cert Store, that you have access to the private key of. I have tested this code with a couple of different smart cards (GemSAFE, Cryptoflex and Swedish Post Office eID) on the client side, and also an imported PKCS12 software certificate on the server side.

The code adds a new config option: cryptoapicert To select a certificate, based on a substring search in the certificate's subject:

cryptoapicert "SUBJ:Peter Runestig"

To select a certificate, based on certificate's thumbprint:

cryptoapicert "THUMB:f6 49 24 41 01 b4 ..."

The thumbprint hex string can easily be copy-and-pasted from the Windows Certificate Store GUI.

Unfortunately, this code don't build with MinGW; e.g. the CryptAcquireCertificatePrivateKey() isn't implemented in MinGW, so I had to use Microsoft Visual C++ 6.0. So the patch included in this post is actually my hack to build OpenVPN using MSVC++, which must be applied before the actual CryptoAPI patch, which will be in my next post (I split it in two, so the posts don't get so big).

To build this, using MSVC 6.0:

Quoted 8 lines

o Unpack openvpn-2.0_beta11.zip, apply the MSVC patch in this post, and the CryptoAPI patch in my next post.

o If the OpenSSL and/or LZO libraries (built with MSVC or something else that gives you .LIB files) isn't reachable using the INCLUDE and LIB environment variables, edit the 'makefile.w32-vc' file to point them out.

o nmake -f makefile.w32-vc

I've merged the MSVC patch for beta12, but I will hold off on the CryptoAPI patch until we have some kind of MinGW solution, such as

(1) Add support for native build with MinGW, or
(2) Make SSL_CTX_use_CryptoAPI_certificate.[ch] into an MSVC-built DLL which can be dynamically linked from MinGW-built code.

James

#2 lore raw
Quoted 1 line · James Yonan

I've merged the MSVC patch for beta12,

Hmm, what exactly did you merge? I can't see much trace of it...

I have a MinGW solution to the CryptoAPI patch, probably post it tomorrow.

Cheers,
- Peter

Signature · 5 lines
Peter 'Luna' Runestig (fd. Altberg), Sweden <[email protected]...>
PGP Key ID: 0xD07BBE13
Fingerprint: 7B5C 1F48 2997 C061 DE4B  42EA CB99 A35C D07B BE13
AOL Instant Messenger Screen name: PRunestig
Yahoo! Messenger profile name: altberg
#3 lore raw
Quoted 4 lines · Peter 'Luna' Runestig
Quoted 1 line · James Yonan

I've merged the MSVC patch for beta12,

Hmm, what exactly did you merge? I can't see much trace of it...

I merged the first patch only, the one that lets OpenVPN build on MSVC.

Quoted 1 line

I have a MinGW solution to the CryptoAPI patch, probably post it tomorrow.

Excellent, thanks.

James

#4 lore raw
Quoted 8 lines · James Yonan
Quoted 4 lines · Peter 'Luna' Runestig
Quoted 1 line · James Yonan

I've merged the MSVC patch for beta12,

Hmm, what exactly did you merge? I can't see much trace of it...

I merged the first patch only, the one that lets OpenVPN build on MSVC.

Yes, I understand. Looking closer now, I see that the changes to error.h, otime.c and tun.c is applied, but not the changes to config-win32.h or the new file makefile.w32-vc.

Signature · 5 lines
Peter 'Luna' Runestig (fd. Altberg), Sweden <[email protected]...>
PGP Key ID: 0xD07BBE13
Fingerprint: 7B5C 1F48 2997 C061 DE4B  42EA CB99 A35C D07B BE13
AOL Instant Messenger Screen name: PRunestig
Yahoo! Messenger profile name: altberg