Skip to contentSkip to conversation

Add ability to specify initialize flags for pkcs11 provider

PATCH In review Petr Mikhalicin 4 participants 5 messages Sep 30, 2021 – Jun 23, 2022 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add ability to specify initialize flags for pkcs11 provider

New pkcs11-helper interface allows to setup pkcs11 provider via
properties: https://github.com/alonbl/pkcs11-helper/commit/b78d21c7e26041746aa4ae3d08b95469e1714a85

Also pkcs11-helper added ability to setup init args for pkcs11 provider:
https://github.com/alonbl/pkcs11-helper/commit/133f893e30856eba1de715ecd6fe176722eb3097

---
 src/openvpn/init.c    |  3 +-
 src/openvpn/options.c | 23 ++++++++++++
 src/openvpn/options.h |  1 +
 src/openvpn/pkcs11.c  | 82 ++++++++++++++++++++++++++++++++-----------
 src/openvpn/pkcs11.h  |  3 +-
 5 files changed, 90 insertions(+), 22 deletions(-)
src/openvpn/init.c +2 -1
index 6d09e566..6af585ac 100644
@@ -682,7 +682,8 @@ context_init_1(struct context *c)
         for (i = 0; i<MAX_PARMS && c->options.pkcs11_providers[i] != NULL; i++)
         {
             pkcs11_addProvider(c->options.pkcs11_providers[i], c->options.pkcs11_protected_authentication[i],
-                               c->options.pkcs11_private_mode[i], c->options.pkcs11_cert_private[i]);
+                               c->options.pkcs11_private_mode[i], c->options.pkcs11_cert_private[i],
+                               c->options.pkcs11_init_flags[i]);
         }
     }
 #endif
src/openvpn/options.c +23 -0
index b3a83aa1..0939ee86 100644
@@ -664,6 +664,11 @@ static const char usage_message[] =
     "                              8       : Use Unwrap.\n"
     "--pkcs11-cert-private [0|1] ... : Set if login should be performed before\n"
     "                                  certificate can be accessed. Set for each provider.\n"
+    "--pkcs11-init-flags hex ...     : PKCS#11 init flags.\n"
+    "                              It's bitwise OR of some PKCS#11 initialize flags.\n"
+    "                              Most popular of them is:\n"
+    "                              1       : CKF_LIBRARY_CANT_CREATE_OS_THREADS\n"
+    "                              2       : CKF_OS_LOCKING_OK\n"
     "--pkcs11-pin-cache seconds      : Number of seconds to cache PIN. The default is -1\n"
     "                                  cache until token is removed.\n"
     "--pkcs11-id-management          : Acquire identity from management interface.\n"
@@ -1838,6 +1843,13 @@ show_settings(const struct options *o)
             SHOW_PARM(pkcs11_cert_private, o->pkcs11_cert_private[i] ? "ENABLED" : "DISABLED", "%s");
         }
     }
+    {
+        int i;
+        for (i = 0; i<MAX_PARMS; i++)
+        {
+            SHOW_PARM(pkcs11_init_flags, o->pkcs11_init_flags[i], "%08x");
+        }
+    }
     SHOW_INT(pkcs11_pin_cache_period);
     SHOW_STR(pkcs11_id);
     SHOW_BOOL(pkcs11_id_management);
@@ -8778,6 +8790,17 @@ add_option(struct options *options,
             options->pkcs11_cert_private[j-1] = atoi(p[j]) != 0 ? 1 : 0;
         }
     }
+    else if (streq(p[0], "pkcs11-init-flags"))
+    {
+        int j;
+
+        VERIFY_PERMISSION(OPT_P_GENERAL);
+
+        for (j = 1; j < MAX_PARMS && p[j] != NULL; ++j)
+        {
+            sscanf(p[j], "%x", &(options->pkcs11_init_flags[j-1]));
+        }
+    }
     else if (streq(p[0], "pkcs11-pin-cache") && p[1] && !p[2])
     {
         VERIFY_PERMISSION(OPT_P_GENERAL);
src/openvpn/options.h +1 -0
index 98c21a2a..2317528e 100644
@@ -573,6 +573,7 @@ struct options
     unsigned pkcs11_private_mode[MAX_PARMS];
     bool pkcs11_protected_authentication[MAX_PARMS];
     bool pkcs11_cert_private[MAX_PARMS];
+    unsigned pkcs11_init_flags[MAX_PARMS];
     int pkcs11_pin_cache_period;
     const char *pkcs11_id;
     bool pkcs11_id_management;
src/openvpn/pkcs11.c +62 -20

103 lines not on this page · raw message

src/openvpn/pkcs11.h +2 -1
index ec524706..bf3f2dfa 100644
@@ -42,7 +42,8 @@ pkcs11_addProvider(
     const char *const provider,
     const bool fProtectedAuthentication,
     const unsigned private_mode,
-    const bool fCertIsPrivate
+    const bool fCertIsPrivate,
+    const unsigned init_flags
     );
 
 int
Signature · 1 line
2.31.1
#1 lore raw

Hi,

Quoted 5 lines · Petr Mikhalicin via Openvpn-devel

New pkcs11-helper interface allows to setup pkcs11 provider via
properties: https://github.com/alonbl/pkcs11-helper/commit/b78d21c7e26041746aa4ae3d08b95469e1714a85

Also pkcs11-helper added ability to setup init args for pkcs11 provider:
https://github.com/alonbl/pkcs11-helper/commit/133f893e30856eba1de715ecd6fe176722eb3097

I can't comment on the PKCS#11 feature (not my field), but I have a few comments about required coding style changes:

src/openvpn/options.c static const char usage_message[] = · line 671

Quoted 11 lines
src/openvpn/options.c +5 -0
@@ -664,6 +664,11 @@ static const char usage_message[] =
     "                              8       : Use Unwrap.\n"
     "--pkcs11-cert-private [0|1] ... : Set if login should be performed before\n"
     "                                  certificate can be accessed. Set for each provider.\n"
+    "--pkcs11-init-flags hex ...     : PKCS#11 init flags.\n"
+    "                              It's bitwise OR of some PKCS#11 initialize flags.\n"
+    "                              Most popular of them is:\n"
+    "                              1       : CKF_LIBRARY_CANT_CREATE_OS_THREADS\n"
+    "                              2       : CKF_OS_LOCKING_OK\n"

The indent here is not right - did you use TABs here? Please don't, they get usually messed up by mail clients.

src/openvpn/options.c show_settings(const struct options *o) · line 1852

Quoted 11 lines
src/openvpn/options.c +7 -0
@@ -1838,6 +1843,13 @@ show_settings(const struct options *o)
             SHOW_PARM(pkcs11_cert_private, o->pkcs11_cert_private[i] ? "ENABLED" : "DISABLED", "%s");
         }
     }
+    {
+        int i;
+        for (i = 0; i<MAX_PARMS; i++)
+        {
+            SHOW_PARM(pkcs11_init_flags, o->pkcs11_init_flags[i], "%08x");
+        }
+    }

This, we do C99 style nowadays:

src/openvpn/options.c show_settings(const struct options *o) · line 1851

Quoted 4 lines
src/openvpn/options.c +4 -0
+    for (int i=0; i<MAX_PARMS; i++)
+    {
+        SHOW_PARM(pkcs11_init_flags, o->pkcs11_init_flags[i], "%08x");
+    }

(so, no extra brackets, and the "int i" can go right into the for() clause)

Quoted 4 lines
     SHOW_INT(pkcs11_pin_cache_period);
     SHOW_STR(pkcs11_id);
     SHOW_BOOL(pkcs11_id_management);

src/openvpn/options.c add_option(struct options *options, · line 8799

Quoted 10 lines
src/openvpn/options.c +7 -0
@@ -8778,6 +8790,17 @@ add_option(struct options *options,
             options->pkcs11_cert_private[j-1] = atoi(p[j]) != 0 ? 1 : 0;
         }
     }
+    else if (streq(p[0], "pkcs11-init-flags"))
+    {
+        int j;
+
+        VERIFY_PERMISSION(OPT_P_GENERAL);
+
+        for (j = 1; j < MAX_PARMS && p[j] != NULL; ++j)

Same here: "int j" goes into the loop.

src/openvpn/pkcs11.c pkcs11_terminate(void) · line 378

Quoted 10 lines
src/openvpn/pkcs11.c +5 -0
index 02d0f51f..29db7ea4 100644
@@ -374,12 +374,17 @@ pkcs11_terminate(void)
+    if ((rv = pkcs11h_registerProvider(provider)) != CKR_OK) {
+        msg(M_WARN, "PKCS#11: Cannot register provider '%s' %ld-'%s'", provider, rv, pkcs11h_getMessage(rv));
+	success = false;
+	goto exit;
+    }

The "{" always goes to the next line, and indenting is never done with tabs (the lines above look like a mixture of tabs and spaces, and the tab being messed up by the mail client).

src/openvpn/pkcs11.c pkcs11_addProvider( · line 430

Quoted 1 line
src/openvpn/pkcs11.c +1 -0
+    // pkcs11-helper take ownership over this pointer

No C++ comments, please.

src/openvpn/pkcs11.c pkcs11_addProvider( · line 437

Quoted 8 lines
src/openvpn/pkcs11.c +8 -0
+    // pkcs11-helper take ownership over this pointer
+    if ((p_init_args = malloc(sizeof(*p_init_args))) == NULL) {
+        msg(M_FATAL, "PKCS#11: Cannot allocate memory");
+	success = false;
+	goto cleanup;
+    }
+
+    memset(p_init_args, 0, sizeof(*p_init_args));

Please use calloc() and check_malloc_return() instead.

msg(M_FATAL) never returns, so the "success = false, goto cleanup" bit is not needed - and all that is done by check_malloc_return() for you :-)

For our coding style guidelines, see also here:

https://community.openvpn.net/openvpn/wiki/CodeStyle

and in the openvpn repo there is a "dev-tools/uncrustify.conf" config which can be used with the "uncrustify" program to format your code according to the whitespace rules. Won't do the "for (int i=0; ...)" C99 changes, though.

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]...
#2 lore raw
In reply to Petr Mikhalicin

Hi,

Quoted 10 lines · Petr Mikhalicin via Openvpn-devel

New pkcs11-helper interface allows to setup pkcs11 provider via properties:
https://github.com/alonbl/pkcs11-helper/commit/b78d21c7e26041746aa4ae3d08b95469e1714a85

Also pkcs11-helper added ability to setup init args for pkcs11 provider:

https://github.com/alonbl/pkcs11-helper/commit/133f893e30856eba1de715ecd6fe176722eb3097

Sorry for the long delay in getting back on this. I somehow also missed the related discussion on Trac (
https://community.openvpn.net/openvpn/ticket/1453)

I don't quite understand the need for exposing "init-args" to the user. The only two supported flags in the cryptoki docs are related to the use of threads. But we are the application and we should know what flags to pass --- not the user --- isn't it? If CKF_OS_LOCKING_OK is required, can't we just set it unconditionally?

That said, OpenVPN2 is single threaded, so why is there a "bug in openvpn" related to the use of pkcs11 library from multiple threads referred to in the trac ticket?

Selva

#3 lore raw
Quoted 17 lines · Selva Nair

Hi,

On Thu, Sep 30, 2021 at 7:34 AM Petr Mikhalicin via Openvpn-devel <[email protected] <mailto:[email protected]>> wrote:

    New pkcs11-helper interface allows to setup pkcs11 provider via
    properties:
    https://github.com/alonbl/pkcs11-helper/commit/b78d21c7e26041746aa4ae3d08b95469e1714a85
    <https://github.com/alonbl/pkcs11-helper/commit/b78d21c7e26041746aa4ae3d08b95469e1714a85>

    Also pkcs11-helper added ability to setup init args for pkcs11 provider:
    https://github.com/alonbl/pkcs11-helper/commit/133f893e30856eba1de715ecd6fe176722eb3097
    <https://github.com/alonbl/pkcs11-helper/commit/133f893e30856eba1de715ecd6fe176722eb3097>

    Signed-off-by: Petr Mikhalicin <[email protected]...
    <mailto:[email protected]...>>
Quoted 5 lines

Sorry for the long delay in getting back on this. I somehow also missed the related discussion on Trac (https://community.openvpn.net/openvpn/ticket/1453 <https://community.openvpn.net/openvpn/ticket/1453>)

I don't quite understand the need for exposing "init-args" to the user. The only two supported flags in the cryptoki docs are related to the use of threads. But we are the application and we should know what flags to pass --- not the user --- isn't it? If CKF_OS_LOCKING_OK is required, can't we just set it unconditionally?

That said, OpenVPN2 is single threaded, so why is there a "bug in openvpn" related to the use of pkcs11 library from multiple threads referred to in the trac ticket?

I haven't dug too deep into the matter this time; and it depends also on the OS you are on. But there has been some issues with pkcs11-helper on hosts with systemd, due to some intricacies with openvpn doing a fork to kick off the password query mechanism with systemd colliding with some pkcs11-helper implementation details. For the systemd case, we added a workaround which made most people happy.

For more details:
<https://community.openvpn.net/openvpn/ticket/538>

Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc
#5 lore raw
In reply to a message not in the archive

Hi,

Quoted 49 lines · David Sommerseth
Quoted 9 lines · Selva Nair

Hi,0

On Thu, Sep 30, 2021 at 7:34 AM Petr Mikhalicin via Openvpn-devel <[email protected] <mailto:[email protected]>> wrote:

New pkcs11-helper interface allows to setup pkcs11 provider via properties:

https://github.com/alonbl/pkcs11-helper/commit/b78d21c7e26041746aa4ae3d08b95469e1714a85

Quoted 1 line
    <

https://github.com/alonbl/pkcs11-helper/commit/b78d21c7e26041746aa4ae3d08b95469e1714a85

Quoted 3 lines

Also pkcs11-helper added ability to setup init args for pkcs11

provider:

Quoted 1 line

https://github.com/alonbl/pkcs11-helper/commit/133f893e30856eba1de715ecd6fe176722eb3097

Quoted 1 line
    <

https://github.com/alonbl/pkcs11-helper/commit/133f893e30856eba1de715ecd6fe176722eb3097

Quoted 17 lines

    Signed-off-by: Petr Mikhalicin <[email protected]...
    <mailto:[email protected]...>>

Sorry for the long delay in getting back on this. I somehow also missed the related discussion on Trac
(https://community.openvpn.net/openvpn/ticket/1453
<https://community.openvpn.net/openvpn/ticket/1453>)

I don't quite understand the need for exposing "init-args" to the user. The only two supported flags in the cryptoki docs are related to the use of threads. But we are the application and we should know what flags to pass --- not the user --- isn't it? If CKF_OS_LOCKING_OK is required, can't we just set it unconditionally?

Quoted 3 lines

That said, OpenVPN2 is single threaded, so why is there a "bug in openvpn" related to the use of pkcs11 library from multiple threads referred to in the trac ticket?

I haven't dug too deep into the matter this time; and it depends also on the OS you are on. But there has been some issues with pkcs11-helper on hosts with systemd, due to some intricacies with openvpn doing a fork to kick off the password query mechanism with systemd colliding with some pkcs11-helper implementation details. For the systemd case, we added a workaround which made most people happy.

Quoted 3 lines

For more details:
<https://community.openvpn.net/openvpn/ticket/538>

This is a different issue from mutex locking required when pkcs#11 calls are made from multiple threads. The rationale for this patch was that we may need to tell the provider library whether native OS locking methods are okay or not, which I see no need for in a single threaded program.

Selva