Skip to contentSkip to conversation

Mac OSX Keychain certificate support

PATCH In review Brian Raderman 3 participants 7 messages Apr 28, 2010 – May 5, 2010 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Mac OSX Keychain certificate support

Hello,

This patch adds support for using certificates stored in the Mac OSX Keychain to authenticate with the OpenVPN server. This works with certificates stored on the computer as well as certificates on hardware tokens that support Apple's tokend interface. It has been tested with an Aladdin eToken on Mac OSX Leopard and with software only certificates on Mac OSX Leopard and Snow Leopard. Aladdin has not yet released drivers for Snow Leopard. The patch is very similar to, and also based on, the Windows Crypto API certificate functionality that currently exists in OpenVPN.

I did not know how to adjust the automake/autoconf configuration files, so I have included the XCode project I used to build and test openvpn in this patch. All new files were added in an "osx" subdirectory.

Please let me know if you have any questions or if there are any changes (I suspect there will be many) that I need to complete before this patch can be accepted.

Thanks,
Brian Raderman

From f13d639021199ec61bf3d716e146c0b29a2b2141 Mon Sep 17 00:00:00 2001
From: Brian Raderman <[email protected]...>
Date: Wed, 28 Apr 2010 00:26:00 -0400
Subject: [PATCH] Adding functionality that allows the use of Mac OSX Keychain certificates to
authenticate with the server so that the certificate and the private key don't need to be stored in the config file. This also supports hardware tokens that conform to Apple's tokend specification and can be accessed
like any other Keychain certificate. The changes are based on the Crypto API functionality for Windows that already exists in the cryptoapi.c/.h files.

---
 openvpn.8                                 |   57 ++
 options.c                                 |   45 +-
 options.h                                 |    4 +-
 osx/cert_data.c                           |  631 ++++++++++++
 osx/cert_data.h                           |   44 +
 osx/common_osx.c                          |   98 ++
 osx/common_osx.h                          |   35 +
 osx/crypto_osx.c                          |   76 ++
 osx/crypto_osx.h                          |   34 +
 osx/keychain.c                            |  274 ++++++
 osx/keychain.h                            |    7 +
 osx/openvpn.xcodeproj/brian.pbxuser       |  879 +++++++++++++++++
 osx/openvpn.xcodeproj/brian.perspectivev3 | 1511 +++++++++++++++++++++++++++++
 osx/openvpn.xcodeproj/project.pbxproj     |  619 ++++++++++++
 ssl.c                                     |   11 +-
 15 files changed, 4315 insertions(+), 10 deletions(-)
 mode change 100644 => 100755 options.c
 mode change 100644 => 100755 options.h
 create mode 100755 osx/cert_data.c
 create mode 100755 osx/cert_data.h
 create mode 100755 osx/common_osx.c
 create mode 100755 osx/common_osx.h
 create mode 100755 osx/crypto_osx.c
 create mode 100755 osx/crypto_osx.h
 create mode 100755 osx/keychain.c
 create mode 100755 osx/keychain.h
 create mode 100755 osx/openvpn.xcodeproj/brian.pbxuser
 create mode 100755 osx/openvpn.xcodeproj/brian.perspectivev3
 create mode 100755 osx/openvpn.xcodeproj/project.pbxproj
 mode change 100644 => 100755 ssl.c
openvpn.8 +57 -0
index 45e61fa..f3ff3da 100644
@@ -3928,6 +3928,63 @@ Certificate Store GUI.
 
 .\"*********************************************************
 .TP
+.B --keychaincert select-string
+Load the certificate and private key from the
+Mac OSX Keychain (Mac OSX Only).
+
+Use this option instead of
+.B --cert
+and
+.B --key.
+
+This makes it possible to use any smart card supported by Mac OSX using the tokend interface, but also any
+kind of certificate, residing in the Keychain, where you have access to
+the private key.  This option has been tested on the client side with an Aladdin eToken
+on Mac OSX Leopard and with software certificates stored in the Keychain on Mac OSX Leopard and
+Mac OSX Snow Leopard.  As of this writing (4/27/10) Aladdin has not yet released Snow Leopard drivers.
+
+To select a certificate based on a string search in the
+certificate's subject and/or issuer:
+
+.B keychaincert
+"SUBJECT:c=US/o=Apple Inc./ou=me.com/cn=username ISSUER:c=US/o=Apple Computer, Inc./ou=Apple Computer Certificate Authority/cn=Apple .Mac Certificate Authority"
+
+.I "Distinguished Name Component Abbreviations:" 
+.br
+o = organization
+.br
+ou = organizational unit
+.br
+c = country
+.br
+l = locality
+.br
+st = state
+.br
+cn = common name
+.br
+e = email
+.br
+
+All of the distinguished name components are optional, although you do need to specify at least one of them.  You can 
+add spaces around the '/' and '=' characters, e.g. "SUBJECT: c = US / o = Apple Inc.".  You do not need to specify
+both the subject and the issuer, one or the other will work fine.  The identity searching algorithm will return the first
+certificate it finds that matches all of the criteria you have specified.  You can also include the MD5 and/or SHA1 thumbprints 
+along with the subject and issuer (see example of thumbprint search criteria below).  
+
+To select a certificate based on certificate's MD5 or SHA1 thumbprint:
+
+.B keychaincert
+"SHA1: 30 F7 3A 7A B7 73 2A 98 54 33 4A A7 00 6F 6E AC EC D1 EF 02 MD5: D5 F5 11 F1 38 EB 5F 4D CF 23 B6 94 E8 33 D8 B5"
+
+Again, you can include both the SHA1 and the MD5 thumbprints, but you can also use just one of them.  The thumbprint hex
+strings can easily be copy-and-pasted from the OSX Keychain Access GUI in the Applications/Utilities folder.  The hex string comparison is
+not case sensitive.
+
+Note that this option cannot be used with the --daemon option.  Mac OSX needs to present the user with an authentication GUI when the Keychain
+is accessed by openvpn and the --daemon option will prevent the GUI from being shown and therefore cause the authentication to silently fail.
+.\"*********************************************************
+.TP
 .B --key-method m
 Use data channel key negotiation method
 .B m.
options.c +37 -8

90 lines not on this page · raw message

options.h +3 -1
old mode 100644
new mode 100755
index 740e18e..d6be33c
@@ -464,8 +464,10 @@ struct options
   bool pkcs11_id_management;
 #endif
 
-#ifdef WIN32
+#if defined(WIN32)
   const char *cryptoapi_cert;
+#elif defined(__APPLE__)
+  const char *keychain_cert;
 #endif
 
   /* data channel key exchange method */
osx/cert_data.c new file +631 -0

631 lines not on this page · raw message

osx/cert_data.h new file +44 -0

45 lines not on this page · raw message

osx/common_osx.c new file +98 -0

99 lines not on this page · raw message

osx/common_osx.h new file +35 -0
new file mode 100755
index 0000000..7cf6582
@@ -0,0 +1,35 @@
+/*
+ *  OpenVPN -- An application to securely tunnel IP networks
+ *             over a single UDP port, with support for SSL/TLS-based
+ *             session authentication and key exchange,
+ *             packet encryption, packet authentication, and
+ *             packet compression.
+ *
+ *  Copyright (C) 2010 Brian Raderman <[email protected]...>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2
+ *  as published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program (see the file COPYING included with this
+ *  distribution); if not, write to the Free Software Foundation, Inc.,
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef __common_osx_h__
+#define __common_osx_h__
+
+#include <CoreFoundation/CoreFoundation.h>
+
+void printCFString(CFStringRef str);
+char* cfstringToCstr(CFStringRef str);
+CFStringRef createHexString(unsigned char *pData, int length);
+void printHex(unsigned char *pData, int length);
+
+#endif //__Common_osx_h__
\ No newline at end of file
osx/crypto_osx.c new file +76 -0

76 lines not on this page · raw message

osx/crypto_osx.h new file +34 -0
new file mode 100755
index 0000000..b4fc8ee
@@ -0,0 +1,34 @@
+/*
+ *  OpenVPN -- An application to securely tunnel IP networks
+ *             over a single UDP port, with support for SSL/TLS-based
+ *             session authentication and key exchange,
+ *             packet encryption, packet authentication, and
+ *             packet compression.
+ *
+ *  Copyright (C) 2010 Brian Raderman <[email protected]...>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2
+ *  as published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program (see the file COPYING included with this
+ *  distribution); if not, write to the Free Software Foundation, Inc.,
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef __crypto_osx_h__
+#define __crypto_osx_h__
+
+#include <CoreFoundation/CoreFoundation.h>
+#include <Security/Security.h>
+
+CSSM_DATA signData(SecIdentityRef identity, CSSM_DATA dataBuf);
+void freeSignature(SecIdentityRef identity, CSSM_DATA sigBuf);
+
+#endif //__crypto_osx_h__
osx/keychain.c new file +274 -0

274 lines not on this page · raw message

osx/keychain.h new file +7 -0
new file mode 100755
index 0000000..ba6e1b1
@@ -0,0 +1,7 @@
+#ifndef _OSXKEYCHAIN_H_
+#define _OSXKEYCHAIN_H_
+
+int SSL_CTX_use_Keychain_certificate(SSL_CTX *ssl_ctx, const char *cert_prop);
+
+
+#endif /* !_OSXKEYCHAIN_H_ */
osx/openvpn.xcodeproj/brian.pbxuser new file +879 -0

879 lines not on this page · raw message

osx/openvpn.xcodeproj/brian.perspectivev3 new file +1511 -0

755 lines not on this page · raw message

osx/openvpn.xcodeproj/project.pbxproj new file +619 -0

619 lines not on this page · raw message

ssl.c +10 -1
old mode 100644
new mode 100755
index 1b275af..0699ef1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.6.6.2

Part of the diff is not shown here. View the raw message.

#1 lore raw

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

Quoted 4 lines · Brian Raderman

Hello,

This patch adds support for using certificates stored in the Mac OSX Keychain to authenticate with the OpenVPN server. This works with certificates

stored

Quoted 1 line

on the computer as well as certificates on hardware tokens that

support Apple's

Quoted 7 lines

tokend interface. It has been tested with an Aladdin eToken on Mac OSX Leopard and with software only certificates on Mac OSX Leopard and Snow Leopard. Aladdin has not yet released drivers for Snow Leopard. The patch is very similar to, and also based on, the Windows Crypto API certificate functionality that currently exists in OpenVPN.

Quoted 8 lines

I did not know how to adjust the automake/autoconf configuration files, so I have included the XCode project I used to build and test openvpn in this patch. All new files were added in an "osx" subdirectory.

Please let me know if you have any questions or if there are any changes (I suspect there will be many) that I need to complete before this patch can be accepted.

Hi Brian,

Thanks a lot for your patch! This is indeed a neat feature we want to really consider for inclusion! But there are of course a few things I spotted which I'd like to see being improved.

I am not familiar with OSX details at all, but I hope someone else with better OSX understanding can review those parts more thoroughly. My comments will be of the more generic type.

* usage of printf()
Please use the msg() function instead. Don't print things to console directly via {f,}printf() directly. There are plenty of places in the code where you can see msg() being used for a lot of logging.

* malloc()
You use malloc() several places. If possible try to make use of the gc_arena API instead. This API is also used a lot of places in the code, so you'll most likely find some good examples there as well.

* static void dummy1() function
In osx/keychain.c you define a rather silly function, which is not used anywhere. Please take this one out.

* xcode files and migration to autotools
Even though Xcode is probably a very good tool on OSX, it's not what we can base OpenVPN on. I would like to see this migrated over to autotools. It's not as hard and difficult as you might think immediately, but there are several traps on the way. Some traps more visible than others.

First of all, it's a pretty good autotools tutorial [1] which is worth going through to understand the concept and how to get started.

Then you'll need to modify configure.ac and Makefile.am. And that's basically it. Have a look for SOCKS in configure.ac to get an idea. Further, you'll also need to add the source files of our osx/ C and H files to openvpn_SOURCES. But! These files should probably be depending on if this OSX feature is enabled or not. There's probably several ways how to solve this, and I'm not sure what's the best solution right now.
But digging into automake docs[2] might be the proper place to start.

When you think you're done, ./configure works and it seems to compile flawlessly by running 'make', try runnning 'make distcheck' to be sure it will package itself properly, that the test compilation and test run works as expected as well.

That's basically all I had to comment so far. But thanks again for your patch! Looking forward to hear from you again with further updates as well.

kind regards,

David Sommerseth

[1] <http://www.lrde.epita.fr/~adl/autotools.html>
[2] <http://sources.redhat.com/automake/automake.html>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/

iEYEARECAAYFAkvX23gACgkQDC186MBRfroWXgCfeZUA1pFrSsu55MMk3gg9TIcJ M0EAoKO/OYi2v5YMNDankep5vqMEX7hC
=sXAB
-----END PGP SIGNATURE-----

#2 lore raw

Hi David,

Thank you for taking a look at the patch and providing such thorough feedback so quickly. I have done everything that you asked:

* Changed printf() calls to use msg(). The prints are not actually used right now, but they are a part of the debug code that I wrote for testing the certificate reading functions. They were actually tested in a separate application, but I thought I should keep them with the rest of the code in case the certificate lookup functionality needs to be debugged inside of openvpn at a later date.

* Changed malloc calls to the gc_arena api.

* Removed the XCode project and updated configure.ac and Makefile.am to properly build the changes. I was well aware that this is what needed to be done, I only included the XCode project in the first patch in the hopes that someone with XCode and Automake knowledge would take a look at it and give me some pointers. You were correct, though, it was not as bad as I thought it was going to be. The information you provided was very useful.

* Removed the dummy1() function from keychain.c. Keychain.c/.h are based on cryptoapi.c/.h in the openvpn source tree. When editing the original file to work with the Keychain services libraries instead of the Windows Crypto API, I decided to change as little as possible, the theory being that what was there was approved and accepted. You will see a static dummy() function defined at the bottom of that file as well. I had no idea what it was there for, but I figured there was a reason for it so I left it alone. I think I tacked on the 1 to the end without thinking too hard because I was being very defensive and trying to prevent a scope problem, which was silly of me because the function is static and you would never compile Windows-only and Mac-only code at the same time. If you grep for "dummy" in the source tree, you will see a number of places where this paradigm is used. Perhaps because some compilers will not compile a file that has had all of its code ifdef'd out? I tested this theory on my mac and no error occurred, so I went ahead and removed the function per your request. I should also point out that cryptoapi.c makes calls to calloc, which you may want to change at some point if you are trying to standardize on the gc_arena API.

Please let me know if you have any questions or if more changes are required.

Thanks,
Brian Raderman

From c157d2a2704cff2e03c92f296532f20aa88db710 Mon Sep 17 00:00:00 2001
From: Brian Raderman <[email protected]...>
Date: Wed, 28 Apr 2010 00:26:00 -0400
Subject: [PATCH] Adding functionality that allows the use of Mac OSX Keychain certificates to authenticate with the server so that the certificate and the private key don't need to be stored in the config file. This also supports hardware tokens that conform to Apple's tokend specification and can be accessed like any other Keychain certificate. The changes are based on the Crypto API functionality for Windows that already exists in the cryptoapi.c/.h files.

---
 Makefile.am      |    9 +-
 configure.ac     |   19 ++
 openvpn.8        |   57 +++++
 options.c        |   40 +++-
 options.h        |    3 +
 osx/cert_data.c  |  641 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 osx/cert_data.h  |   46 ++++
 osx/common_osx.c |   99 +++++++++
 osx/common_osx.h |   36 +++
 osx/crypto_osx.c |   76 +++++++
 osx/crypto_osx.h |   34 +++
 osx/keychain.c   |  290 ++++++++++++++++++++++++
 osx/keychain.h   |    7 +
 ssl.c            |   10 +
 14 files changed, 1363 insertions(+), 4 deletions(-)
 create mode 100755 osx/cert_data.c
 create mode 100755 osx/cert_data.h
 create mode 100755 osx/common_osx.c
 create mode 100755 osx/common_osx.h
 create mode 100755 osx/crypto_osx.c
 create mode 100755 osx/crypto_osx.h
 create mode 100755 osx/keychain.c
 create mode 100755 osx/keychain.h
Makefile.am +8 -1
index 4cc3ce2..652a9bb 100644
@@ -137,8 +137,15 @@ openvpn_SOURCES = \
 	thread.c thread.h \
 	tun.c tun.h \
 	win32.h win32.c \
-	cryptoapi.h cryptoapi.c
+    cryptoapi.h cryptoapi.c
 
+if KEYCHAINCERT
+openvpn_SOURCES += \
+	osx/cert_data.h osx/cert_data.c \
+	osx/common_osx.h osx/common_osx.c \
+	osx/crypto_osx.h osx/crypto_osx.c \
+	osx/keychain.h osx/keychain.c
+endif
 
 dist-hook:
 	cd $(distdir) && for i in $(EXTRA_DIST) $(SUBDIRS) ; do find $$i -name .svn -type d -prune -exec rm -rf '{}' ';' ; rm -f `find $$i -type f | grep -E '(^|\/)\.?\#|\~$$|\.s?o$$'` ; done
configure.ac +19 -0
index 4f7231a..5c5e5b5 100644
@@ -212,6 +212,12 @@ AC_ARG_ENABLE(selinux,
    [SELINUX="yes"]
 )
 
+AC_ARG_ENABLE(keychaincert,
+   [  --disable-keychaincert  Disable support for Mac OSX Keychain certificate services (Mac OSX only)],
+   [KEYCHAINCERT="$enableval"],
+   [KEYCHAINCERT="yes"]
+)
+
 AC_ARG_WITH(ssl-headers,
    [  --with-ssl-headers=DIR  Crypto/SSL Include files location],
    [CS_HDR_DIR="$withval"]
@@ -276,6 +282,7 @@ dnl fix search path, to allow compilers to find syshead.h
 CPPFLAGS="$CPPFLAGS -I${srcdir}"
 
 dnl check target OS
+MACOSX="no"
 openvpn_target=$target
 if test $target_alias; then
   openvpn_target=$target_alias
@@ -305,6 +312,7 @@ case "$target" in
 	dnl some Mac OS X tendering (we use vararg macros...)
 	AC_DEFINE(TARGET_DARWIN, 1, [Are we running on Mac OS X?])
 	CPPFLAGS="$CPPFLAGS -no-cpp-precomp"
+	MACOSX="yes"
 	;;
 *mingw*)
 	AC_DEFINE(TARGET_WIN32, 1, [Are we running WIN32?])
@@ -886,6 +894,17 @@ if test "$SELINUX" = "yes"; then
    )
 fi
 
+dnl enable Mac OSX Keychain certificate support
+if test "$MACOSX" = "yes" -a "$KEYCHAINCERT" = "yes"; then
+	OPENVPN_ADD_LIBS(-framework CoreFoundation)
+	OPENVPN_ADD_LIBS(-framework Security)
+	AC_DEFINE(KEYCHAINCERT, 1, [Enable support for MAc OSX Keychain certificate services])
+else
+	KEYCHAINCERT="no"
+fi
+
+AM_CONDITIONAL(KEYCHAINCERT, test "$KEYCHAINCERT" = "yes")
+
 TAP_ID="PRODUCT_TAP_ID"
 TAP_WIN32_MIN_MAJOR="PRODUCT_TAP_WIN32_MIN_MAJOR"
 TAP_WIN32_MIN_MINOR="PRODUCT_TAP_WIN32_MIN_MINOR"
openvpn.8 +57 -0

63 lines not on this page · raw message

options.c +37 -3
index 812c3de..da90a29 100644
@@ -503,6 +503,10 @@ static const char usage_message[] =
   "--cryptoapicert select-string : Load the certificate and private key from the\n"
   "                  Windows Certificate System Store.\n"
 #endif
+#ifdef KEYCHAINCERT
+  "--keychaincert  select-string : Load the certificate and private key from the\n"
+  "                  Mac OSX Keychain.\n"
+#endif
   "--tls-cipher l  : A list l of allowable TLS ciphers separated by : (optional).\n"
   "                : Use --show-tls to see a list of supported TLS ciphers.\n"
   "--tls-timeout n : Packet retransmit timeout on TLS control channel\n"
@@ -1323,6 +1327,9 @@ show_settings (const struct options *o)
 #ifdef WIN32
   SHOW_STR (cryptoapi_cert);
 #endif
+#ifdef KEYCHAINCERT
+	SHOW_STR (keychain_cert);
+#endif
   SHOW_STR (cipher_list);
   SHOW_STR (tls_verify);
   SHOW_STR (tls_remote);
@@ -1841,11 +1848,15 @@ options_postprocess_verify_ce (const struct options *options, const struct conne
 	if (options->cryptoapi_cert)
 	  msg(M_USAGE, "Parameter --cryptoapicert cannot be used when --pkcs11-provider is also specified.");
 #endif
+#ifdef KEYCHAINCERT
+	if (options->keychain_cert)
+	   msg(M_USAGE, "Parameter --keychaincert cannot be used when --pkcs11-provider is also specified.");
+#endif
        }
       else
 #endif
 #ifdef WIN32
-      if (options->cryptoapi_cert)
+	if (options->cryptoapi_cert)
 	{
 	  if ((!(options->ca_file)) && (!(options->ca_path)))
 	    msg(M_USAGE, "You must define CA file (--ca) or CA path (--capath)");
@@ -1856,9 +1867,25 @@ options_postprocess_verify_ce (const struct options *options, const struct conne
           if (options->pkcs12_file)
 	    msg(M_USAGE, "Parameter --pkcs12 cannot be used when --cryptoapicert is also specified.");
 	}
-      else
+	else
+#endif
+#ifdef KEYCHAINCERT
+	if (options->keychain_cert)
+	{
+		if ((!(options->ca_file)) && (!(options->ca_path)))
+			msg(M_USAGE, "You must define CA file (--ca) or CA path (--capath)");
+		if (options->cert_file)
+			msg(M_USAGE, "Parameter --cert cannot be used when --keychaincert is also specified.");
+		if (options->priv_key_file)
+			msg(M_USAGE, "Parameter --key cannot be used when --keychaincert is also specified.");
+		if (options->pkcs12_file)
+			msg(M_USAGE, "Parameter --pkcs12 cannot be used when --keychaincert is also specified.");
+        if (options->daemon)
+            msg(M_USAGE, "Parameter --daemon cannot be used when --keychaincert is also specified.");
+	}
+	else
 #endif
-      if (options->pkcs12_file)
+       if (options->pkcs12_file)
         {
           if (options->ca_path)
 	    msg(M_USAGE, "Parameter --capath cannot be used when --pkcs12 is also specified.");
@@ -5472,6 +5499,13 @@ add_option (struct options *options,
       options->cryptoapi_cert = p[1];
     }
 #endif
+#ifdef KEYCHAINCERT 
+  else if (streq (p[0], "keychaincert") && p[1])
+  {
+      VERIFY_PERMISSION (OPT_P_GENERAL);
+      options->keychain_cert = p[1];
+  }
+#endif
   else if (streq (p[0], "key") && p[1])
     {
       VERIFY_PERMISSION (OPT_P_GENERAL);
options.h +3 -0
index 740e18e..2152079 100644
@@ -467,6 +467,9 @@ struct options
 #ifdef WIN32
   const char *cryptoapi_cert;
 #endif
+#ifdef KEYCHAINCERT
+  const char *keychain_cert;
+#endif
 
   /* data channel key exchange method */
   int key_method;
osx/cert_data.c new file +641 -0

641 lines not on this page · raw message

osx/cert_data.h new file +46 -0

47 lines not on this page · raw message

osx/common_osx.c new file +99 -0

100 lines not on this page · raw message

osx/common_osx.h new file +36 -0
new file mode 100755
index 0000000..88b58c1
@@ -0,0 +1,36 @@
+/*
+ *  OpenVPN -- An application to securely tunnel IP networks
+ *             over a single UDP port, with support for SSL/TLS-based
+ *             session authentication and key exchange,
+ *             packet encryption, packet authentication, and
+ *             packet compression.
+ *
+ *  Copyright (C) 2010 Brian Raderman <[email protected]...>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2
+ *  as published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program (see the file COPYING included with this
+ *  distribution); if not, write to the Free Software Foundation, Inc.,
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef __common_osx_h__
+#define __common_osx_h__
+
+#include "../buffer.h"
+#include <CoreFoundation/CoreFoundation.h>
+
+void printCFString(CFStringRef str, struct gc_arena *pGc);
+char* cfstringToCstr(CFStringRef str, struct gc_arena *pGc);
+CFStringRef createHexString(unsigned char *pData, int length);
+void printHex(unsigned char *pData, int length, struct gc_arena *pGc);
+
+#endif //__Common_osx_h__
\ No newline at end of file
osx/crypto_osx.c new file +76 -0

76 lines not on this page · raw message

osx/crypto_osx.h new file +34 -0
new file mode 100755
index 0000000..b4fc8ee
@@ -0,0 +1,34 @@
+/*
+ *  OpenVPN -- An application to securely tunnel IP networks
+ *             over a single UDP port, with support for SSL/TLS-based
+ *             session authentication and key exchange,
+ *             packet encryption, packet authentication, and
+ *             packet compression.
+ *
+ *  Copyright (C) 2010 Brian Raderman <[email protected]...>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2
+ *  as published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program (see the file COPYING included with this
+ *  distribution); if not, write to the Free Software Foundation, Inc.,
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef __crypto_osx_h__
+#define __crypto_osx_h__
+
+#include <CoreFoundation/CoreFoundation.h>
+#include <Security/Security.h>
+
+CSSM_DATA signData(SecIdentityRef identity, CSSM_DATA dataBuf);
+void freeSignature(SecIdentityRef identity, CSSM_DATA sigBuf);
+
+#endif //__crypto_osx_h__
osx/keychain.c new file +290 -0

290 lines not on this page · raw message

osx/keychain.h new file +7 -0
new file mode 100755
index 0000000..ba6e1b1
@@ -0,0 +1,7 @@
+#ifndef _OSXKEYCHAIN_H_
+#define _OSXKEYCHAIN_H_
+
+int SSL_CTX_use_Keychain_certificate(SSL_CTX *ssl_ctx, const char *cert_prop);
+
+
+#endif /* !_OSXKEYCHAIN_H_ */
ssl.c +10 -0
index 1b275af..3a2dd85 100644
@@ -1601,6 +1601,16 @@ init_ssl (const struct options *options)
 	}
       else
 #endif
+#ifdef KEYCHAINCERT
+	if (options->keychain_cert)
+	{
+	  /* Load Certificate and Private Key */
+	  if (!SSL_CTX_use_Keychain_certificate (ctx, options->keychain_cert))
+		  msg (M_SSLERR, "Cannot load certificate \"%s\" from Mac OSX Keychain",
+			   options->keychain_cert);
+	}
+	else
+#endif
 	{
 	  /* Load Certificate */
 	  if (options->cert_file)
Signature · 1 line
1.7.0
Quoted 70 lines · David Sommerseth

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

Hi Brian,

Thanks a lot for your patch! This is indeed a neat feature we want to really consider for inclusion! But there are of course a few things I spotted which I'd like to see being improved.

I am not familiar with OSX details at all, but I hope someone else with better OSX understanding can review those parts more thoroughly. My comments will be of the more generic type.

* usage of printf()
Please use the msg() function instead. Don't print things to console directly via {f,}printf() directly. There are plenty of places in the code where you can see msg() being used for a lot of logging.

* malloc()
You use malloc() several places. If possible try to make use of the gc_arena API instead. This API is also used a lot of places in the code, so you'll most likely find some good examples there as well.

* static void dummy1() function
In osx/keychain.c you define a rather silly function, which is not used anywhere. Please take this one out.

* xcode files and migration to autotools
Even though Xcode is probably a very good tool on OSX, it's not what we can base OpenVPN on. I would like to see this migrated over to autotools. It's not as hard and difficult as you might think immediately, but there are several traps on the way. Some traps more visible than others.

First of all, it's a pretty good autotools tutorial [1] which is worth going through to understand the concept and how to get started.

Then you'll need to modify configure.ac and Makefile.am. And that's basically it. Have a look for SOCKS in configure.ac to get an idea. Further, you'll also need to add the source files of our osx/ C and H files to openvpn_SOURCES. But! These files should probably be depending on if this OSX feature is enabled or not. There's probably several ways how to solve this, and I'm not sure what's the best solution right now. But digging into automake docs[2] might be the proper place to start.

When you think you're done, ./configure works and it seems to compile flawlessly by running 'make', try runnning 'make distcheck' to be sure it will package itself properly, that the test compilation and test run works as expected as well.

That's basically all I had to comment so far. But thanks again for your patch! Looking forward to hear from you again with further updates as well.

kind regards,

David Sommerseth

[1] <http://www.lrde.epita.fr/~adl/autotools.html>
[2] <http://sources.redhat.com/automake/automake.html>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/

iEYEARECAAYFAkvX23gACgkQDC186MBRfroWXgCfeZUA1pFrSsu55MMk3gg9TIcJ M0EAoKO/OYi2v5YMNDankep5vqMEX7hC
=sXAB
-----END PGP SIGNATURE-----

#3 lore raw
Quoted 3 lines · Brian Raderman

Hi David,
...
* Removed the dummy1() function from keychain.c. Keychain.c/.h are based on cryptoapi.c/.h in the openvpn source tree. When editing the original file to work with the Keychain services libraries instead of the Windows Crypto API, I decided to change as little as possible, the theory being that what was there was approved and accepted. You will see a static dummy() function defined at the bottom of that file as well. I had no idea what it was there for, but I figured there was a reason for it so I left it alone. I think I tacked on the 1 to the end without thinking too hard because I was being very defensive and trying to prevent a scope problem, which was silly of me because the function is static and you would never compile Windows-only and Mac-only code at the same time. If you grep for "dummy" in the source tree, you will see a number of places where this paradigm is used. Perhaps because some compilers will not compile a file that has had all of its code ifdef'd out?

From a recent patch to Maria (database), I believe this may be the case on Windows, for example:

+#elif defined(__WIN__)
+
+// Remove linker warning 4221 about empty file
+namespace { char dummy; };
+
#endif

I can't fully confirm this right now as I am away from my Linux/WINE environment that runs the MS Visual C++ Express toolchain.

--Toby

Quoted 7 lines

I tested this theory on my mac and no error occurred, so I went
ahead and removed the function per your request. I should also point out that cryptoapi.c makes calls to calloc, which you may want to change at some point if you are trying to standardize on the gc_arena API.

Please let me know if you have any questions or if more changes are required.

Thanks,
Brian Raderman

#4 lore raw
In reply to Brian Raderman

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

Quoted 4 lines · Brian Raderman

Hi David,

Thank you for taking a look at the patch and providing such thorough feedback so quickly.

You're welcome! I'm happy to see you updating the patch and bringing it one step closer for inclusion! The further progress now will be to get some more people to test out this patch on OSX and hopefully they can report back. If you know anyone who is willing to help out doing that, that will make things quicker as well!

Also, as I don't have OSX available, I'm not able to really compile test this patch further. So it will need to get an ACK by someone else who have access and can do a proper review with OSX in mind.

Quoted 5 lines

I have done everything that you asked:

* Changed printf() calls to use msg(). The prints are not actually used right now, but they are a part of the debug code that I wrote for testing the certificate reading functions. They were actually tested in a

separate

Quoted 2 lines

application, but I thought I should keep them with the rest of the code in case the certificate lookup functionality needs to be debugged inside

of openvpn

Quoted 1 line

at a later date.

Looking good! You also have dmsg() I believe for debugging as well. But then I believe the logging will only be enabled if configured with
- --enable-debug, iirc. Anyway, that was just a side note. If you want it like it is now, we don't need to change anything in this regard.

Quoted 1 line

* Changed malloc calls to the gc_arena api.

Seems good to me. Have you tried running a patched OpenVPN through valgrind? Just to make sure you don't have any memory leaks. It's important to also check how it reacts when re-negotiating the tunnel encryption (--reneg-* arguments can be tweaked to make this happen more often). And also if there are any leaks in error situations.

Quoted 2 lines

* Removed the XCode project and updated configure.ac and Makefile.am to properly build the changes. I was well aware that this is what needed to be

done, I only

Quoted 1 line

included the XCode project in the first patch in the hopes that

someone with

Quoted 1 line

XCode and Automake knowledge would take a look at it and give me some

pointers.

Quoted 1 line

You were correct, though, it was not as bad as I thought it was going

to be. The

Quoted 1 line

information you provided was very useful.

Good to hear! Even though, autotools are tricky too - and it's easy to step wrong too. But in this case, it looks sensible what you have done!

Quoted 9 lines

* Removed the dummy1() function from keychain.c. Keychain.c/.h are based on cryptoapi.c/.h in the openvpn source tree. When editing the original file to work with the Keychain services libraries instead of the Windows Crypto API, I decided to change as little as possible, the theory being that what was there was approved and accepted. You will see a static dummy() function defined at the bottom of that file as well. I had no idea what it was there for, but I figured there was a reason for it so I left it alone. I think I tacked on the 1 to the end without thinking too hard because I was being very defensive and

Quoted 8 lines

trying to prevent a scope problem, which was silly of me because the function is static and you would never compile Windows-only and Mac-only code at the same time. If you grep for "dummy" in the source tree, you will see a number of places where this paradigm is used. Perhaps because some compilers will not compile a file that has had all of its code ifdef'd out? I tested this theory on my mac and no error occurred, so I went ahead and removed the function per your request.

Good! Yeah, I saw Toby Thain's answer, which relates these dummy functions to a (dare I say it?) silly Microsoft linker who gives warnings about compiling "empty" files. As this seems to be related to Windows only, it makes sense to skip these dummy functions when not absolutely needed - and this code is for OSX only.

Anyhow, we should give these places which needs these silly functions a more decent comment with a reason why they are there.

Quoted 3 lines

I should also point out that cryptoapi.c makes calls to calloc, which you may want to change at some point if you are trying to standardize on the gc_arena API.

Ahh, good catch! I've not spent much time at all in the windows code at all (I'm primarily working on Linux). So this should be cleaned up. I've added this to the "Open tasks" list for OpenVPN [1].

Thanks again for job well done! As I said in the beginning, we're getting closer to inclusion now. I just need to get some confirmations from some others in addition who will try out this patch on OSX. What's interesting in this next round now is to get confirmation that it compiles cleanly and behaves as initially expected. Then I'd like to hear more about if any memory leaks have been found or not.

When they give a positive response on the compilation and usability, I'll give you a feature branch in openvpn-testing.git. And when we feel pretty safe that no memory leaks are found, we can look at merging it into the allmerged branch. Does this sound reasonable to you?

kind regards,

David Sommerseth

[1] <https://community.openvpn.net/openvpn/wiki/OpenTasks>

Quoted 54 lines · David Sommerseth

Hi Brian,

Thanks a lot for your patch! This is indeed a neat feature we want to really consider for inclusion! But there are of course a few things I spotted which I'd like to see being improved.

I am not familiar with OSX details at all, but I hope someone else with better OSX understanding can review those parts more thoroughly. My comments will be of the more generic type.

* usage of printf()
Please use the msg() function instead. Don't print things to console directly via {f,}printf() directly. There are plenty of places in the code where you can see msg() being used for a lot of logging.

* malloc()
You use malloc() several places. If possible try to make use of the gc_arena API instead. This API is also used a lot of places in the code, so you'll most likely find some good examples there as well.

* static void dummy1() function
In osx/keychain.c you define a rather silly function, which is not used anywhere. Please take this one out.

* xcode files and migration to autotools
Even though Xcode is probably a very good tool on OSX, it's not what we can base OpenVPN on. I would like to see this migrated over to autotools. It's not as hard and difficult as you might think immediately, but there are several traps on the way. Some traps more visible than others.

First of all, it's a pretty good autotools tutorial [1] which is worth going through to understand the concept and how to get started.

Then you'll need to modify configure.ac and Makefile.am. And that's basically it. Have a look for SOCKS in configure.ac to get an idea. Further, you'll also need to add the source files of our osx/ C and H files to openvpn_SOURCES. But! These files should probably be depending on if this OSX feature is enabled or not. There's probably several ways how to solve this, and I'm not sure what's the best solution right now. But digging into automake docs[2] might be the proper place to start.

When you think you're done, ./configure works and it seems to compile flawlessly by running 'make', try runnning 'make distcheck' to be sure it will package itself properly, that the test compilation and test run works as expected as well.

That's basically all I had to comment so far. But thanks again for your patch! Looking forward to hear from you again with further updates as well.

Quoted 7 lines

kind regards,

David Sommerseth

[1] <http://www.lrde.epita.fr/~adl/autotools.html>
[2] <http://sources.redhat.com/automake/automake.html>

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/

iEYEARECAAYFAkvcL4sACgkQDC186MBRfrrWKACgkNVMoso/8N7cyJYnErPhMOlp XGMAn2AdyX9IQz3q1I7CKkr2trxny1iP
=6Mzr
-----END PGP SIGNATURE-----

#5 lore raw

Hi David,

Quoted 5 lines · David Sommerseth

Looking good! You also have dmsg() I believe for debugging as well. But then I believe the logging will only be enabled if configured with
- --enable-debug, iirc. Anyway, that was just a side note. If you want
it like it is now, we don't need to change anything in this regard.

We can leave the logging functionality like it is now. Again, it is not actively being used, it's there just in case. If it ends up being used a lot in future debugging efforts, I may change the calls to dmsg().

Quoted 5 lines

Seems good to me. Have you tried running a patched OpenVPN through valgrind? Just to make sure you don't have any memory leaks. It's important to also check how it reacts when re-negotiating the tunnel encryption (--reneg-* arguments can be tweaked to make this happen more often). And also if there are any leaks in error situations.

I hadn't even heard of valgrind until you mentioned it. I ran the certificate code through Mac OSX Instruments to detect memory leaks, but that was before I integrated it with OpenVPN and added the gc_arena stuff. I will try running it through Instruments again or valgrind when I get a chance.

Quoted 11 lines

Thanks again for job well done! As I said in the beginning, we're getting closer to inclusion now. I just need to get some confirmations from some others in addition who will try out this patch on OSX. What's interesting in this next round now is to get confirmation that it compiles cleanly and behaves as initially expected. Then I'd like to hear more about if any memory leaks have been found or not.

When they give a positive response on the compilation and usability, I'll give you a feature branch in openvpn-testing.git. And when we feel pretty safe that no memory leaks are found, we can look at merging it into the allmerged branch. Does this sound reasonable to you?

Sounds perfectly reasonable. This is the normal process you go through, correct? My only concern is that we will have difficulty finding people to test out this functionality on Mac OSX. I'm working on trying to get the word out to possibly interested parties. Should I have them post their results to this list?

Brian

#6 lore raw

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

Quoted 11 lines · Brian Raderman

Hi David,

Quoted 4 lines · David Sommerseth

Looking good! You also have dmsg() I believe for debugging as well. But then I believe the logging will only be enabled if configured with
- --enable-debug, iirc. Anyway, that was just a side note. If you want it like it is now, we don't need to change anything in this regard.

We can leave the logging functionality like it is now. Again, it is not
actively being used, it's there just in case. If it ends up being used
a lot in future debugging efforts, I may change the calls to dmsg().

Sounds sensible!

Quoted 11 lines
Quoted 5 lines

Seems good to me. Have you tried running a patched OpenVPN through valgrind? Just to make sure you don't have any memory leaks. It's important to also check how it reacts when re-negotiating the tunnel encryption (--reneg-* arguments can be tweaked to make this happen more often). And also if there are any leaks in error situations.

I hadn't even heard of valgrind until you mentioned it. I ran the certificate code through Mac OSX Instruments to detect memory leaks, but that was before I integrated it with OpenVPN and added the gc_arena stuff. I will try running it through Instruments again or valgrind when I get a chance.

Very good! We will try to provide some more documentations on other testing mechanisms as well on our community pages as well.

[...snip...]

Quoted 7 lines
Quoted 4 lines

When they give a positive response on the compilation and usability, I'll give you a feature branch in openvpn-testing.git. And when we feel pretty safe that no memory leaks are found, we can look at merging it into the allmerged branch. Does this sound reasonable to you?

Sounds perfectly reasonable. This is the normal process you go through, correct?

To be honest, it's not been any clear processes for this in the past. But we need to get a better process for this. And this is a first step in doing so.

Quoted 4 lines

My only concern is that we will have difficulty finding people to test out this functionality on Mac OSX. I'm working on trying to get
the word out to possibly interested parties. Should I have them post
their results to this list?

I completely understand this issue, and it's one we also hit with the other distributions and OSes as well - unfortunately. But it is important that we know that the code we're modifying are getting tested by users, to avoid introducing bugs and/or bloating OpenVPN with features nobody or just a small minority is interested in.

So feedback from users are important! If they are willing to send their responses to the mailing lists, that would really be the best! Or else we don't know much what's happening and might do decisions in the future based on wrong impressions.

kind regards,

David Sommerseth
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/

iEYEARECAAYFAkvhKU8ACgkQDC186MBRfrqplQCggxiOtLtdUjV0p4m0jjZXUJVm UQoAn0hy703Ya4G0LGK7F4icGSC3bIGa
=7GXZ
-----END PGP SIGNATURE-----