Skip to contentSkip to conversation

support PKCS11 lib dependencies

PATCH In review Marc Becker 5 participants 22 messages Dec 7, 2022 – Dec 15, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 support PKCS11 lib dependencies

Modify the Win32-PE search path to find dependencies in shared object origin location.

Needs https://github.com/OpenSC/pkcs11-helper/pull/59 to work.

Would be nice if this could still be included for v2.6, an additional pkcs11-helper patch may be added to preempt the PR.

#1 lore raw

PATCH 1/3 unify code paths for loading PKCS11

---
 src/openvpn/pkcs11.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
src/openvpn/pkcs11.c +1 -11
index 507af17c..b6ceb582 100644
@@ -853,17 +853,7 @@ show_pkcs11_ids(
         goto cleanup;
     }
 
-    if (
-        (rv = pkcs11h_addProvider(
-             provider,
-             provider,
-             TRUE,
-             0,
-             FALSE,
-             0,
-             cert_private ? TRUE : FALSE
-             )) != CKR_OK
-        )
+    if (!pkcs11_addProvider(provider, TRUE, 0, cert_private ? TRUE : FALSE))
     {
         msg(M_FATAL, "PKCS#11: Cannot add provider '%s' %ld-'%s'", 
provider, rv, pkcs11h_getMessage(rv));
         goto cleanup;
Signature · 1 line
2.38.1.windows.1
#2 lore raw

PATCH 2/3 use new pkcs11-helper provider interface

In reply to Marc Becker

split provider creation, property modifications and initialization. new interface available since pkcs11-helper v1.28

---
 src/openvpn/pkcs11.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
src/openvpn/pkcs11.c +35 -0
index b6ceb582..6ef26eb0 100644
@@ -396,6 +396,40 @@ pkcs11_addProvider(
         provider
         );
 
+#if PKCS11H_VERSION >= ((1<<16) | (28<<8) | (0<<0))
+    if ((rv = pkcs11h_registerProvider(provider)) != CKR_OK
+        || (rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_LOCATION, provider, strlen(provider) + 1)) != CKR_OK)
+    {
+        msg(M_WARN, "PKCS#11: Cannot create provider '%s' %ld-'%s'", provider, rv, pkcs11h_getMessage(rv));
+    }
+    else
+    {
+        PKCS11H_BOOL allow_protected_auth = protected_auth;
+        PKCS11H_BOOL cert_is_private = cert_private;
+
+        if (allow_protected_auth
+            && (rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_ALLOW_PROTECTED_AUTH, &allow_protected_auth, sizeof(allow_protected_auth))) != CKR_OK)
+        {
+            msg(M_WARN, "PKCS#11: Cannot enable protected authentication '%s' %ld-'%s'", provider, rv, pkcs11h_getMessage(rv));
+        }
+        if (private_mode != PKCS11H_PRIVATEMODE_MASK_AUTO
+            && (rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_MASK_PRIVATE_MODE, &private_mode, sizeof(private_mode))) != CKR_OK)
+        {
+            msg(M_WARN, "PKCS#11: Cannot private mode '%s' %ld-'%s'", provider, rv, pkcs11h_getMessage(rv));
+        }
+        if (cert_is_private
+            && (rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_CERT_IS_PRIVATE, &cert_is_private, sizeof(cert_is_private))) != CKR_OK)
+        {
+            msg(M_WARN, "PKCS#11: Cannot set provider properties '%s' %ld-'%s'", provider, rv, pkcs11h_getMessage(rv));
+        }
+
+        if ((rv = pkcs11h_initializeProvider(provider)) != CKR_OK)
+        {
+            pkcs11h_removeProvider(provider);
+        }
+    }
+    if (rv != CKR_OK)
+#else
     if (
         (rv = pkcs11h_addProvider(
              provider,
@@ -407,6 +441,7 @@ pkcs11_addProvider(
              cert_private
              )) != CKR_OK
         )
+#endif
     {
         msg(M_WARN, "PKCS#11: Cannot initialize provider '%s' 
%ld-'%s'", provider, rv, pkcs11h_getMessage(rv));
     }
Signature · 1 line
2.38.1.windows.1
#3 lore raw

PATCH 3/3 special handling for PKCS11 providers on win32

In reply to Marc Becker

in case of absolute library path, search origin for dependencies

---
 src/openvpn/pkcs11.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

needs https://github.com/OpenSC/pkcs11-helper/pull/59

src/openvpn/pkcs11.c +7 -1
index 6ef26eb0..65693aed 100644
@@ -406,7 +406,13 @@ pkcs11_addProvider(
     {
         PKCS11H_BOOL allow_protected_auth = protected_auth;
         PKCS11H_BOOL cert_is_private = cert_private;
-
+        unsigned loader_flags = 0;
+#if defined(_WIN32)
+        if (platform_absolute_pathname(provider))
+        {
+            loader_flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR;
+        }
+#endif
         if (allow_protected_auth
             && (rv = pkcs11h_setProviderProperty(provider, 

PKCS11H_PROVIDER_PROPERTY_ALLOW_PROTECTED_AUTH, &allow_protected_auth, sizeof(allow_protected_auth))) != CKR_OK)

         {
@@ -422,7 +428,13 @@ pkcs11_addProvider(
         {
             msg(M_WARN, "PKCS#11: Cannot set provider properties '%s' 
%ld-'%s'", provider, rv, pkcs11h_getMessage(rv));
         }

-
+#if defined(PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS)
+ if (loader_flags
+ && (rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS, &loader_flags, sizeof(loader_flags))) != CKR_OK)
+ {
+ msg(M_WARN, "PKCS#11: Cannot set alternative loader flags '%s' %ld-'%s'", provider, rv, pkcs11h_getMessage(rv));
+ }
+#endif

         if ((rv = pkcs11h_initializeProvider(provider)) != CKR_OK)
         {
             pkcs11h_removeProvider(provider);
Signature · 1 line
2.38.1.windows.1
#4 lore raw
In reply to Marc Becker

Hi,

Quoted 8 lines · Marc Becker via Openvpn-devel

Modify the Win32-PE search path to find dependencies in shared object origin location.

Needs https://github.com/OpenSC/pkcs11-helper/pull/59 to work.

Would be nice if this could still be included for v2.6,

an additional pkcs11-helper patch may be added to preempt the PR.

At first glance it looks like the proposed change will build cleany even without the upstream although the feature to set loader_flags will get bypassed (as PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS will evaluate as undefined)

I think we should leave it at that instead of carrying around another local patch for pkcs11-helper. If/when it gets merged upstream we'll have this feature. Until then Windows users can still set the PATH and have their providers loaded from arbitrary source directories.

In other words, the option to set load_flags is just a convenience, not a necessity, isn't it? Correct me if I'm wrong.

As for the patches, I couldn't apply them -- tried from email and patchwork. Corrupt lines due to line breaks?

Quoted 2 lines

---
2.38.1.windows.1

Possibly that explains it :)

Unless it's only me, a v2 will be required to fix the patch. At that point, it would be nice to have Signed-off lines and better commit messages that are clear and descriptive.

Selva

#5 lore raw

PATCH v2 1/3 unify code path for adding PKCS#11 providers

In reply to Marc Becker

Use existing wrapper for pkcs11h_addProvider to have arguments with "magic values" for pkcs11-helper call in a central place.

Slot event argument to pkcs11h_addProvider has NOT been a boolean for at least 15 years.
Luckily the default is PKCS11H_SLOTEVENT_METHOD_AUTO=0=FALSE.

---
v2: propper commit message, error message without error code reference

---
 src/openvpn/pkcs11.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
src/openvpn/pkcs11.c +2 -12
index 507af17c..fbc4c472 100644
@@ -853,19 +853,9 @@ show_pkcs11_ids(
         goto cleanup;
     }
 
-    if (
-        (rv = pkcs11h_addProvider(
-             provider,
-             provider,
-             TRUE,
-             0,
-             FALSE,
-             0,
-             cert_private ? TRUE : FALSE
-             )) != CKR_OK
-        )
+    if (!pkcs11_addProvider(provider, TRUE, 0, cert_private ? TRUE : FALSE))
     {
-        msg(M_FATAL, "PKCS#11: Cannot add provider '%s' %ld-'%s'", provider, rv, pkcs11h_getMessage(rv));
+        msg(M_FATAL, "Failed to add PKCS#11 provider '%s", provider);
         goto cleanup;
     }
 
Signature · 1 line
2.38.1.windows.1
Acked-by on v2 1/3
#6 lore raw

Re: [Openvpn-devel] [PATCH v2 1/3] unify code path for adding PKCS#11 providers

Quoted 6 lines · Marc Becker via Openvpn-devel

Use existing wrapper for pkcs11h_addProvider to have arguments with "magic values" for pkcs11-helper call in a central place.

Slot event argument to pkcs11h_addProvider has NOT been a boolean for at least 15 years.
Luckily the default is PKCS11H_SLOTEVENT_METHOD_AUTO=0=FALSE.

Change makes sense and should not change behavior AFAICT.

Regards,

Signature · 1 line
  Frank Lichtenheld
#7 lore raw

[Openvpn-devel] [PATCH applied] Re: unify code path for adding PKCS#11 providers

In reply to Marc Becker

I do claim that I have no idea about pkcs11-helper, but this patch isn't *that* complex, just using the existing wrapper... but still thanks to Frank for reviewing. I have test compiled on Gentoo with "pkcs11-helper-1.27.0-r1", and "it compiled".

I *do* find this part a bit nonintuitive...

pkcs11_addProvider( ..., cert_private ? TRUE : FALSE)

.. we have a bool, pass it to a bool, and evaluate it in tri-state, to pass true, if the bool is true, and false, if it is not... wat?

One day, someone should reformat the rest of pkcs11.c, though... this stuff is outright ugly

    msg(
        M_INFO,
        "PKCS#11: Adding PKCS#11 provider '%s'",
        provider
        );

(but I'm aware that OpenVPN had this phase when every function call needed to be distributed to the maximum number of lines possible...)

Your patch has been applied to the master and release/2.6 branch.

commit b08f8cbb2b92f3ee0eced39d11665befea3aec87 (master) commit f1995ccca4c105e71728101bb719d235f5605b33 (release/2.6)

Author: Marc Becker via Openvpn-devel
Date:   Sun Dec 11 20:09:13 2022 +0100
     unify code path for adding PKCS#11 providers

     Signed-off-by: Marc Becker <[email protected]...>
     Acked-by: Frank Lichtenheld <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg25642.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#8 lore raw

Re: [Openvpn-devel] [PATCH applied] Re: unify code path for adding PKCS#11 providers

Hi,

*sigh* - "Marc Becker via Openvpn-devel", this so messes up my workflow... (can DMARC please go somewhere and die, alone, with no friends?).

I had to reapply these patches 1/3 and 2/3 to get the author right, so you'll see new commit IDs.

For 1/3, these are:

Quoted 2 lines · Gert Doering

commit b08f8cbb2b92f3ee0eced39d11665befea3aec87 (master) commit f1995ccca4c105e71728101bb719d235f5605b33 (release/2.6)

new:

commit 8958a365479348c1500dee44e1a8b27e7e35a96f ( master) commit bcceded96775cc5a131bb9ab11ba855c7576603d (release/2.6)

Author: Marc Becker <[email protected]...>
Date:   Sun Dec 11 20:09:13 2022 +0100

unify code path for adding PKCS#11 providers

nothing in the actual patch has changed, just the author (and that, of course, changes the commit IDs).

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#9 lore raw

PATCH v2 2/3 use new pkcs11-helper interface to add providers

In reply to Marc Becker

The new interface in pkcs11-helper 1.28 allows decoupling of provider registration and initialization.
This allows modifying more (and future) properties apart from the 6 fixed ones supported as arguments to pkcs11h_addProvider().

With the new interface it is easier to see (from a code perspective) which option is set to which value.
It's also not necessary to supply values for built-in defaults:
- slot_event_method=PKCS11H_SLOTEVENT_METHOD_AUTO
- slot_poll_interval=0

---
v2: improved code and description, no (essentially) duplicated log output

---
 src/openvpn/pkcs11.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
src/openvpn/pkcs11.c +33 -0
index fbc4c472..b74ac8f4 100644
@@ -396,6 +396,38 @@ pkcs11_addProvider(
         provider
         );
 
+#if PKCS11H_VERSION >= ((1<<16) | (28<<8) | (0<<0))
+    if ((rv = pkcs11h_registerProvider(provider)) != CKR_OK)
+    {
+        msg(M_WARN, "PKCS#11: Cannot register provider '%s' %ld-'%s'", provider, rv, pkcs11h_getMessage(rv));
+    }
+    else
+    {
+        PKCS11H_BOOL allow_protected_auth = protected_auth;
+        PKCS11H_BOOL cert_is_private = cert_private;
+
+        rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_LOCATION, provider, strlen(provider) + 1);
+
+        if (rv == CKR_OK)
+        {
+            rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_ALLOW_PROTECTED_AUTH, &allow_protected_auth, sizeof(allow_protected_auth));
+        }
+        if (rv == CKR_OK)
+        {
+            rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_MASK_PRIVATE_MODE, &private_mode, sizeof(private_mode));
+        }
+        if (rv == CKR_OK)
+        {
+            rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_CERT_IS_PRIVATE, &cert_is_private, sizeof(cert_is_private));
+        }
+
+        if (rv != CKR_OK || (rv = pkcs11h_initializeProvider(provider)) != CKR_OK)
+        {
+            msg(M_WARN, "PKCS#11: Cannot initialize provider '%s' %ld-'%s'", provider, rv, pkcs11h_getMessage(rv));
+            pkcs11h_removeProvider(provider);
+        }
+    }
+#else  /* if PKCS11H_VERSION >= ((1<<16) | (28<<8) | (0<<0)) */
     if (
         (rv = pkcs11h_addProvider(
              provider,
@@ -410,6 +442,7 @@ pkcs11_addProvider(
     {
         msg(M_WARN, "PKCS#11: Cannot initialize provider '%s' %ld-'%s'", provider, rv, pkcs11h_getMessage(rv));
     }
+#endif /* if PKCS11H_VERSION >= ((1<<16) | (28<<8) | (0<<0)) */
 
     dmsg(
         D_PKCS11_DEBUG,
Signature · 1 line
2.38.1.windows.1
Acked-by on v2 2/3
#10 lore raw

Re: [Openvpn-devel] [PATCH v2 2/3] use new pkcs11-helper interface to add providers

Hi,

Quoted 80 lines · Marc Becker via Openvpn-devel

The new interface in pkcs11-helper 1.28 allows decoupling of provider registration and initialization.
This allows modifying more (and future) properties apart from the 6 fixed ones supported as arguments to pkcs11h_addProvider().

With the new interface it is easier to see (from a code perspective) which option is set to which value.
It's also not necessary to supply values for built-in defaults:
- slot_event_method=PKCS11H_SLOTEVENT_METHOD_AUTO
- slot_poll_interval=0

---
v2: improved code and description, no (essentially) duplicated log output

---
 src/openvpn/pkcs11.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
src/openvpn/pkcs11.c +15 -0
index fbc4c472..b74ac8f4 100644
@@ -396,6 +396,38 @@ pkcs11_addProvider(
         provider
         );
 
+#if PKCS11H_VERSION >= ((1<<16) | (28<<8) | (0<<0))
+    if ((rv = pkcs11h_registerProvider(provider)) != CKR_OK)
+    {
+        msg(M_WARN, "PKCS#11: Cannot register provider '%s' %ld-'%s'",
 provider, rv, pkcs11h_getMessage(rv));
+    }
+    else
+    {
+        PKCS11H_BOOL allow_protected_auth = protected_auth;
+        PKCS11H_BOOL cert_is_private = cert_private;
+
+        rv = pkcs11h_setProviderProperty(provider,
 PKCS11H_PROVIDER_PROPERTY_LOCATION, provider, strlen(provider) + 1);
+
+        if (rv == CKR_OK)
+        {
+            rv = pkcs11h_setProviderProperty(provider,
 PKCS11H_PROVIDER_PROPERTY_ALLOW_PROTECTED_AUTH, &allow_protected_auth,

sizeof(allow_protected_auth));

src/openvpn/pkcs11.c +4 -0
+        }
+        if (rv == CKR_OK)
+        {
+            rv = pkcs11h_setProviderProperty(provider,

PKCS11H_PROVIDER_PROPERTY_MASK_PRIVATE_MODE, &private_mode, sizeof(private_mode));

src/openvpn/pkcs11.c +4 -0
+        }
+        if (rv == CKR_OK)
+        {
+            rv = pkcs11h_setProviderProperty(provider,

PKCS11H_PROVIDER_PROPERTY_CERT_IS_PRIVATE, &cert_is_private, sizeof(cert_is_private));

src/openvpn/pkcs11.c +3 -0
+        }
+
+        if (rv != CKR_OK || (rv = pkcs11h_initializeProvider(provider))

!= CKR_OK)

src/openvpn/pkcs11.c +2 -0
+        {
+            msg(M_WARN, "PKCS#11: Cannot initialize provider '%s'

%ld-'%s'", provider, rv, pkcs11h_getMessage(rv));

src/openvpn/pkcs11.c +5 -0
+            pkcs11h_removeProvider(provider);
+        }
+    }
+#else  /* if PKCS11H_VERSION >= ((1<<16) | (28<<8) | (0<<0)) */
     if (
         (rv = pkcs11h_addProvider(
              provider,
@@ -410,6 +442,7 @@ pkcs11_addProvider(
     {
         msg(M_WARN, "PKCS#11: Cannot initialize provider '%s' %ld-'%s'",
 provider, rv, pkcs11h_getMessage(rv));
     }
+#endif /* if PKCS11H_VERSION >= ((1<<16) | (28<<8) | (0<<0)) */
 
     dmsg(
         D_PKCS11_DEBUG,
Quoted 3 lines

--
2.38.1.windows.1

I have been delaying acking this until I get time to test 3/3, but as 1/3 is has been acked here goes:

Effectively this is the same as the original but splitting addProvider to explicit register/set-properties/intialize calls when using recent versions of pkcs11-helper is a useful refactoring.

#11 lore raw

[Openvpn-devel] [PATCH applied] Re: use new pkcs11-helper interface to add providers

In reply to Marc Becker

I have test compiled this on Gentoo with pkcs-helper-1.27.0-r1, and "it compiled". I'm aware that this is not actually excercising any of the new code paths, but "stare at code" sees nothing explosive, and I trust Selva's review.

Your patch has been applied to the master and release/2.6 branch.

commit 5954f03bb321a969ea6edbdd353e1dd4f61ac8f0 (master) commit d0141344b0c21747ba18be64470236066da62f33 (release/2.6)

Author: Marc Becker via Openvpn-devel
Date:   Sun Dec 11 20:14:03 2022 +0100
     use new pkcs11-helper interface to add providers

     Signed-off-by: Marc Becker <[email protected]...>
     Acked-by: Selva Nair <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg25643.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#12 lore raw

Re: [Openvpn-devel] [PATCH applied] Re: use new pkcs11-helper interface to add providers

Hi,

Quoted 4 lines · Gert Doering

Your patch has been applied to the master and release/2.6 branch.

commit 5954f03bb321a969ea6edbdd353e1dd4f61ac8f0 (master) commit d0141344b0c21747ba18be64470236066da62f33 (release/2.6)

Same thing here: fixing DMARC induced nonsense, the new commit IDs are:

commit 45d9b0210a22353e587c29c5d3c3990346a4a189 (master) commit 0236518cee65cc3d1da8d57b1d7785ecb2663a23 (release/2.6)

Author: Marc Becker <[email protected]...>
Date:   Sun Dec 11 20:14:03 2022 +0100

use new pkcs11-helper interface to add providers

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#13 lore raw

PATCH v2 3/3 special handling for PKCS11 providers on win32

In reply to Marc Becker

Change win32 dynamic loader behavior when supplying an absolute path. The DLL location is considered/preferred to resolve dependencies. Support in pkcs11-helper for loader flag is detected at compile time.

3rd party DLLs and additional dependencies do not need to be moved to the OpenVPN directory or require changes to %PATH% configuration.

The included changes to pkcs11-helper are pending and can be removed as soon as a compatible a version is released/referenced.

---
v2: compress code change an add transitional pkcs11-helper patch

---
 ...cs11-helper-002-dynamic_loader_flags.patch | 105 ++++++++++++++++++
 .../vcpkg-ports/pkcs11-helper/portfile.cmake  |   1 +
 src/openvpn/pkcs11.c                          |   7 ++
 3 files changed, 113 insertions(+)
 create mode 100644 contrib/vcpkg-ports/pkcs11-helper/pkcs11-helper-002-dynamic_loader_flags.patch
contrib/vcpkg-ports/pkcs11-helper/pkcs11-helper-002-dynamic_loader_flags.patch new file +105 -0
new file mode 100644
index 00000000..cdefa20a
@@ -0,0 +1,105 @@
+From 934197611dd1260d17ae0f11ae81c1d2e85612d2 Mon Sep 17 00:00:00 2001
+From: Marc Becker <[email protected]...>
+Date: Fri, 22 Jul 2022 10:33:05 +0200
+Subject: [PATCH] core: add provider property for loader flags
+
+support flags for dynamic loader via provider property
+set original values as defaults, use verbatim (user-supplied) value
+---
+ include/pkcs11-helper-1.0/pkcs11h-core.h | 11 ++++++++++-
+ lib/_pkcs11h-core.h                      |  2 ++
+ lib/pkcs11h-core.c                       | 13 +++++++++++--
+ 3 files changed, 23 insertions(+), 3 deletions(-)
+
+diff --git a/include/pkcs11-helper-1.0/pkcs11h-core.h b/include/pkcs11-helper-1.0/pkcs11h-core.h
+index 9028c27..56f8771 100644
+--- a/include/pkcs11-helper-1.0/pkcs11h-core.h
++++ b/include/pkcs11-helper-1.0/pkcs11h-core.h
+@@ -384,8 +384,17 @@ extern "C" {
+  */
+ #define PKCS11H_PROVIDER_PROPERTY_PROVIDER_DESTRUCT_HOOK_DATA 8
+ 
++/**
++ * @brief Provider loader flags for platform.
++ * Value type is unsigned.
++ * Default value is platform dependent:
++ *     win32 -> 0
++ *    dlopen -> RTLD_NOW | RTLD_LOCAL
++ */
++#define PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS 9
++
+ /** @private */
+-#define _PKCS11H_PROVIDER_PROPERTY_LAST 9
++#define _PKCS11H_PROVIDER_PROPERTY_LAST 10
+ 
+ /** @} */
+ 
+diff --git a/lib/_pkcs11h-core.h b/lib/_pkcs11h-core.h
+index f879c0e..1c02e35 100644
+--- a/lib/_pkcs11h-core.h
++++ b/lib/_pkcs11h-core.h
+@@ -134,6 +134,8 @@ struct _pkcs11h_provider_s {
+ #if defined(ENABLE_PKCS11H_SLOTEVENT)
+ 	_pkcs11h_thread_t slotevent_thread;
+ #endif
++
++	unsigned loader_flags;
+ };
+ 
+ struct _pkcs11h_session_s {
+diff --git a/lib/pkcs11h-core.c b/lib/pkcs11h-core.c
+index 0bf11e8..409ad9e 100644
+--- a/lib/pkcs11h-core.c
++++ b/lib/pkcs11h-core.c
+@@ -138,6 +138,7 @@ static const char * __pkcs11h_provider_preperty_names[] = {
+ 	"init_args",
+ 	"provider_destruct_hook",
+ 	"provider_destruct_hook_data",
++	"provider_loader_flags",
+ 	NULL
+ };
+ 
+@@ -916,6 +917,10 @@ pkcs11h_registerProvider (
+ 		reference
+ 	);
+ 
++#if !defined(_WIN32)
++	provider->loader_flags = RTLD_NOW | RTLD_LOCAL;
++#endif
++
+ 	_PKCS11H_DEBUG (
+ 		PKCS11H_LOG_DEBUG2,
+ 		"PKCS#11: pkcs11h_registerProvider Provider '%s'",
+@@ -1001,6 +1006,7 @@ pkcs11h_setProviderPropertyByName (
+ 		case PKCS11H_PROVIDER_PROPERTY_SLOT_EVENT_METHOD:
+ 		case PKCS11H_PROVIDER_PROPERTY_MASK_PRIVATE_MODE:
+ 		case PKCS11H_PROVIDER_PROPERTY_SLOT_POLL_INTERVAL:
++		case PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS:
+ 			*(unsigned *)value = (unsigned)strtol(value_str, 0, 0);
+ 			value_size = sizeof(unsigned);
+ 		break;
+@@ -1084,6 +1090,9 @@ __pkcs11h_providerPropertyAddress(
+ 		case PKCS11H_PROVIDER_PROPERTY_PROVIDER_DESTRUCT_HOOK_DATA:
+ 			*value = &provider->destruct_hook_data;
+ 			*value_size = sizeof(provider->destruct_hook_data);
++		case PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS:
++			*value = &provider->loader_flags;
++			*value_size = sizeof(provider->loader_flags);
+ 		break;
+ 	}
+ 	rv = CKR_OK;
+@@ -1254,9 +1263,9 @@ pkcs11h_initializeProvider (
+ 	}
+ 
+ #if defined(_WIN32)
+-	provider->handle = LoadLibraryA (provider->provider_location);
++	provider->handle = LoadLibraryExA (provider->provider_location, NULL, provider->loader_flags);
+ #else
+-	provider->handle = dlopen (provider->provider_location, RTLD_NOW | RTLD_LOCAL);
++	provider->handle = dlopen (provider->provider_location, provider->loader_flags);
+ #endif
+ 
+ 	if (provider->handle == NULL) {
+-- 
+2.30.2
+
contrib/vcpkg-ports/pkcs11-helper/portfile.cmake +1 -0
index 4432b550..1c6cedac 100644
@@ -14,6 +14,7 @@ vcpkg_extract_source_archive_ex(
         0001-nmake-compatibility-with-vcpkg-nmake.patch
         0002-config-w32-vc.h.in-indicate-OpenSSL.patch
         pkcs11-helper-001-RFC7512.patch
+        pkcs11-helper-002-dynamic_loader_flags.patch
 )
 
 vcpkg_build_nmake(
src/openvpn/pkcs11.c +7 -0
index b74ac8f4..aa027337 100644
@@ -420,6 +420,13 @@ pkcs11_addProvider(
         {
             rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_CERT_IS_PRIVATE, &cert_is_private, sizeof(cert_is_private));
         }
+#if defined(WIN32) && defined(PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS)
+        if (rv == CKR_OK && platform_absolute_pathname(provider))
+        {
+            unsigned loader_flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR;
+            rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS, &loader_flags, sizeof(loader_flags));
+        }
+#endif
 
         if (rv != CKR_OK || (rv = pkcs11h_initializeProvider(provider)) != CKR_OK)
         {
Signature · 1 line
2.38.1.windows.1
#14 lore raw

Re: [Openvpn-devel] [PATCH v2 3/3] special handling for PKCS11 providers on win32

Hi,

Quoted 7 lines · Marc Becker via Openvpn-devel

Change win32 dynamic loader behavior when supplying an absolute path. The DLL location is considered/preferred to resolve dependencies. Support in pkcs11-helper for loader flag is detected at compile time.

3rd party DLLs and additional dependencies do not need to be moved to the OpenVPN directory or require changes to %PATH% configuration.

Quoted 7 lines

The included changes to pkcs11-helper are pending and can be removed as soon as a compatible a version is released/referenced.

---
v2: compress code change an add transitional pkcs11-helper patch

Please split this into separate patches for
#1 changes to pkcs11.c which will be required even after upstream pkcs11-helper library is updated
#2 Patching pkcs11-helper in vcpkg-ports

Rationale:
(1) #2 can be independently reverted when longer required
(2) IMO, carrying around patches to external libraries is an undue burden and not justified in this case --- my personal preference would be to NAK it but still keep #1 as it will automatically get enabled when upstream pkcs11-helper accepts your changes, which look imminent.

Selva

#15 lore raw

Re: [Openvpn-devel] [PATCH v2 3/3] special handling for PKCS11 providers on win32

Hi,

my suggestion would be to decide if we want v2 or v3 of 3/3 depending on the pkcs11-helper state immediately before next beta.

OpenVPN 2.6 changing behavior shortly after release would be something
to avoid in any case.
Having the patch around in 2.6 branch only and dropping it in 2.6.1 may also be an acceptable compromise:
3/3 v2 -> 2.6 branch
3/3 v3 -> master

The timeline for pkcs11-helper releases is not something I even dare to predict. :)

Regrads,
Marc

#16 lore raw

PATCH v3 3/3 special handling for PKCS11 providers on win32

In reply to Marc Becker

Change win32 dynamic loader behavior when supplying an absolute path. The DLL location is considered/preferred to resolve dependencies. Support in pkcs11-helper for loader flag is detected at compile time.

3rd party DLLs and additional dependencies do no longer need to be moved to the OpenVPN directory or require changes to %PATH% configuration.

---
 src/openvpn/pkcs11.c | 7 +++++++
 1 file changed, 7 insertions(+)
src/openvpn/pkcs11.c +7 -0
index b74ac8f4..aa027337 100644
@@ -420,6 +420,13 @@ pkcs11_addProvider(
         {
             rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_CERT_IS_PRIVATE, &cert_is_private, sizeof(cert_is_private));
         }
+#if defined(WIN32) && defined(PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS)
+        if (rv == CKR_OK && platform_absolute_pathname(provider))
+        {
+            unsigned loader_flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR;
+            rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS, &loader_flags, sizeof(loader_flags));
+        }
+#endif
 
         if (rv != CKR_OK || (rv = pkcs11h_initializeProvider(provider)) != CKR_OK)
         {
Signature · 1 line
2.38.1.windows.1
#17 lore raw

PATCH 1/1 vcpkg-ports/pkcs11-helper: support loader flags

Add dynamic loader flag feature to bundled pkcs11-helper. Required to allow special handling for PKCS11 providers on win32.

---
Part 2 of [PATCH v3 3/3] special handling for PKCS11 providers on win32
- split contrib patch from OpenVPN change

See https://github.com/OpenSC/pkcs11-helper/pull/59

Support is expected to land in pkcs11-helper with next release (v1.30?), anticipate change to have stable behavior during OpenVPN 2.6 lifetime

---
 ...cs11-helper-002-dynamic_loader_flags.patch | 102 ++++++++++++++++++
 .../vcpkg-ports/pkcs11-helper/portfile.cmake  |   1 +
 2 files changed, 103 insertions(+)
 create mode 100644 contrib/vcpkg-ports/pkcs11-helper/pkcs11-helper-002-dynamic_loader_flags.patch
contrib/vcpkg-ports/pkcs11-helper/pkcs11-helper-002-dynamic_loader_flags.patch new file +102 -0
new file mode 100644
index 00000000..325dea8b
@@ -0,0 +1,102 @@
+From 934197611dd1260d17ae0f11ae81c1d2e85612d2 Mon Sep 17 00:00:00 2001
+From: Marc Becker <[email protected]...>
+Date: Fri, 22 Jul 2022 10:33:05 +0200
+Subject: [PATCH] core: add provider property for loader flags
+
+support flags for dynamic loader via provider property
+set original values as defaults, use verbatim (user-supplied) value
+---
+ include/pkcs11-helper-1.0/pkcs11h-core.h | 11 ++++++++++-
+ lib/_pkcs11h-core.h                      |  2 ++
+ lib/pkcs11h-core.c                       | 13 +++++++++++--
+ 3 files changed, 23 insertions(+), 3 deletions(-)
+
+diff --git a/include/pkcs11-helper-1.0/pkcs11h-core.h b/include/pkcs11-helper-1.0/pkcs11h-core.h
+index 9028c277..56f87718 100644
+--- a/include/pkcs11-helper-1.0/pkcs11h-core.h
++++ b/include/pkcs11-helper-1.0/pkcs11h-core.h
+@@ -384,8 +384,17 @@ extern "C" {
+  */
+ #define PKCS11H_PROVIDER_PROPERTY_PROVIDER_DESTRUCT_HOOK_DATA 8
+ 
++/**
++ * @brief Provider loader flags for platform.
++ * Value type is unsigned.
++ * Default value is platform dependent:
++ *     win32 -> 0
++ *    dlopen -> RTLD_NOW | RTLD_LOCAL
++ */
++#define PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS 9
++
+ /** @private */
+-#define _PKCS11H_PROVIDER_PROPERTY_LAST 9
++#define _PKCS11H_PROVIDER_PROPERTY_LAST 10
+ 
+ /** @} */
+ 
+diff --git a/lib/_pkcs11h-core.h b/lib/_pkcs11h-core.h
+index f879c0e8..1c02e35d 100644
+--- a/lib/_pkcs11h-core.h
++++ b/lib/_pkcs11h-core.h
+@@ -134,6 +134,8 @@ struct _pkcs11h_provider_s {
+ #if defined(ENABLE_PKCS11H_SLOTEVENT)
+ 	_pkcs11h_thread_t slotevent_thread;
+ #endif
++
++	unsigned loader_flags;
+ };
+ 
+ struct _pkcs11h_session_s {
+diff --git a/lib/pkcs11h-core.c b/lib/pkcs11h-core.c
+index 0bf11e87..409ad9e2 100644
+--- a/lib/pkcs11h-core.c
++++ b/lib/pkcs11h-core.c
+@@ -138,6 +138,7 @@ static const char * __pkcs11h_provider_preperty_names[] = {
+ 	"init_args",
+ 	"provider_destruct_hook",
+ 	"provider_destruct_hook_data",
++	"provider_loader_flags",
+ 	NULL
+ };
+ 
+@@ -916,6 +917,10 @@ pkcs11h_registerProvider (
+ 		reference
+ 	);
+ 
++#if !defined(_WIN32)
++	provider->loader_flags = RTLD_NOW | RTLD_LOCAL;
++#endif
++
+ 	_PKCS11H_DEBUG (
+ 		PKCS11H_LOG_DEBUG2,
+ 		"PKCS#11: pkcs11h_registerProvider Provider '%s'",
+@@ -1001,6 +1006,7 @@ pkcs11h_setProviderPropertyByName (
+ 		case PKCS11H_PROVIDER_PROPERTY_SLOT_EVENT_METHOD:
+ 		case PKCS11H_PROVIDER_PROPERTY_MASK_PRIVATE_MODE:
+ 		case PKCS11H_PROVIDER_PROPERTY_SLOT_POLL_INTERVAL:
++		case PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS:
+ 			*(unsigned *)value = (unsigned)strtol(value_str, 0, 0);
+ 			value_size = sizeof(unsigned);
+ 		break;
+@@ -1084,6 +1090,9 @@ __pkcs11h_providerPropertyAddress(
+ 		case PKCS11H_PROVIDER_PROPERTY_PROVIDER_DESTRUCT_HOOK_DATA:
+ 			*value = &provider->destruct_hook_data;
+ 			*value_size = sizeof(provider->destruct_hook_data);
++		case PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS:
++			*value = &provider->loader_flags;
++			*value_size = sizeof(provider->loader_flags);
+ 		break;
+ 	}
+ 	rv = CKR_OK;
+@@ -1254,9 +1263,9 @@ pkcs11h_initializeProvider (
+ 	}
+ 
+ #if defined(_WIN32)
+-	provider->handle = LoadLibraryA (provider->provider_location);
++	provider->handle = LoadLibraryExA (provider->provider_location, NULL, provider->loader_flags);
+ #else
+-	provider->handle = dlopen (provider->provider_location, RTLD_NOW | RTLD_LOCAL);
++	provider->handle = dlopen (provider->provider_location, provider->loader_flags);
+ #endif
+ 
+ 	if (provider->handle == NULL) {
contrib/vcpkg-ports/pkcs11-helper/portfile.cmake +1 -0
index 4432b550..1c6cedac 100644
@@ -14,6 +14,7 @@ vcpkg_extract_source_archive_ex(
         0001-nmake-compatibility-with-vcpkg-nmake.patch
         0002-config-w32-vc.h.in-indicate-OpenSSL.patch
         pkcs11-helper-001-RFC7512.patch
+        pkcs11-helper-002-dynamic_loader_flags.patch
 )
 
 vcpkg_build_nmake(
Signature · 1 line
2.39.0.windows.1
Acked-by on v1 1/1
#18 lore raw

Re: [Openvpn-devel] [PATCH] vcpkg-ports/pkcs11-helper: support loader flags

Hi,

I applied this pach locally, cleared vcpkg_installed directory and verified that vcpkg indeed applied this patch:

2>-- Installing port from location:
C:\Users\lev\Projects\openvpn\contrib\vcpkg-ports\pkcs11-helper
2>-- Using cached pkcs11-helper-1.29.0.tar.bz2.
2>-- Extracting source
C:/Users/lev/Projects/vcpkg/downloads/pkcs11-helper-1.29.0.tar.bz2
2>-- Applying patch 0001-nmake-compatibility-with-vcpkg-nmake.patch 2>-- Applying patch 0002-config-w32-vc.h.in-indicate-OpenSSL.patch 2>-- Applying patch pkcs11-helper-001-RFC7512.patch
2>-- Applying patch pkcs11-helper-002-dynamic_loader_flags.patch

I also checked that the "inside" patch is identical to the one from
pkcs11 repo (https://github.com/OpenSC/pkcs11-helper/pull/59/commits/934197611dd1260d17ae0f11ae81c1d2e85612d2).

Everything compiles, no changes to the "actual" code of openvpn in this patch. I haven't tested actual pkcs11 functionality.

Note that while applying "outside" patch "git am" complains about whitespace errors, but "this is fine" - those are from the "inside" patch.

#19 lore raw

[Openvpn-devel] [PATCH applied] Re: vcpkg-ports/pkcs11-helper: support loader flags

In reply to Marc Becker

Lev warned me that there are whitespace errors in the "inside" patch, so had to apply this with "git am --whitespace=nowarn"...

Pushed to my github instance, had GHA build something, and it claims "success"...

https://github.com/cron2/openvpn/actions/runs/3702355587
https://github.com/cron2/openvpn/actions/runs/3702357052

so whatever it does, it does not *break* anything right away :-)

As per the discussion on v2, we need to remember to remove that patch again when upstream is merged and we bump openvpn-build to a new pkcs11-helper release - in which case, the build is likely going to fail anyway ("there is a patch that does not apply") so we'll notice.

Your patch has been applied to the master and release/2.6 branch.

commit 2f9c56e2cb0ce0f8d7a2a30f89831d1ddc0f2bbb (master) commit 0b18e23b67ac3c8e64ed44c8e907c53a15cf96d0 (release/2.6)

Author: Marc Becker
Date:   Wed Dec 14 15:33:25 2022 +0100
     vcpkg-ports/pkcs11-helper: support loader flags

     Signed-off-by: Marc Becker <[email protected]...>
     Acked-by: Lev Stipakov <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg25691.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

Acked-by on v3 3/3
#20 lore raw

Re: [Openvpn-devel] [PATCH v3 3/3] special handling for PKCS11 providers on win32

In reply to Marc Becker

Hi,

Quoted 35 lines · Marc Becker via Openvpn-devel

Change win32 dynamic loader behavior when supplying an absolute path. The DLL location is considered/preferred to resolve dependencies. Support in pkcs11-helper for loader flag is detected at compile time.

3rd party DLLs and additional dependencies do no longer need to be moved to the OpenVPN directory or require changes to %PATH% configuration.

---
 src/openvpn/pkcs11.c | 7 +++++++
 1 file changed, 7 insertions(+)
src/openvpn/pkcs11.c +5 -0
index b74ac8f4..aa027337 100644
@@ -420,6 +420,13 @@ pkcs11_addProvider(
         {
             rv = pkcs11h_setProviderProperty(provider,
 PKCS11H_PROVIDER_PROPERTY_CERT_IS_PRIVATE, &cert_is_private,
 sizeof(cert_is_private));
         }
+#if defined(WIN32) && defined(PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS)
+        if (rv == CKR_OK && platform_absolute_pathname(provider))
+        {
+            unsigned loader_flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS |
 LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR;
+            rv = pkcs11h_setProviderProperty(provider,

PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS, &loader_flags, sizeof(loader_flags));

src/openvpn/pkcs11.c +2 -0
+        }
+#endif
 
         if (rv != CKR_OK || (rv = pkcs11h_initializeProvider(provider))

!= CKR_OK)

Quoted 2 lines
         {

This requires a proposed change to pkcs11-helper which I believe will be merged upstream -- Alon has closed the PR with a positive remark which usually means change accepted, but not sure when it will land in the repo. So, I have only stared at the code and it looks good to me: doesn't break current builds and should add provider path to dll search path on Windows when upstream gets updated (or if/when we locally patch pkcs11-helper)

#21 lore raw

[Openvpn-devel] [PATCH applied] Re: special handling for PKCS11 providers on win32

In reply to Marc Becker

I have not tested this at all, as I do not have the necessary infrastructure.

It does not affect non-WIN32 builds, so what I'll do is to feed this + the vcpkg patch to my github instance, so it can be tested for windows, and only then push to the "real" repos.

Your patch has been applied to the master and release/2.6 branch.

commit e299b8d0d62a4763b20bf9a3bd6aadf414aa89fe (master) commit 38993a110001b6134a803797a6b4b472ef3546db (release/2.6)

Author: Marc Becker
Date:   Sun Dec 11 21:01:08 2022 +0100
     special handling for PKCS11 providers on win32

     Signed-off-by: Marc Becker <[email protected]...>
     Acked-by: Selva Nair <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg25646.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering