Skip to contentSkip to conversation

Auth token patches v3

PATCH In review Arne Schwabe 3 participants 16 messages May 10, 2019 – Jun 13, 2019 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

v3 cover Auth token patches v3

This is the v3 of the patch series. I choose to resend all of the patches so they all cleanly apply. Since the patches depend on the changes suggested to --genkey, I made them part of the patch series. The other patches have been updated to incoperate the feedback.

Arne Schwabe (7):
Write key to stdout if filename is not given Implement --genkey type keyfile syntax and migrate tls-crypt-v2 Add pem_read_key_file variant that allows a random key Rewrite auth-token-gen to be based on HMAC based tokens Implement a permanent session id in auth-token
Sent indication that a session is expired to clients Implement unit tests for auth-gen-token

 doc/openvpn.8                              | 141 ++++++--
 src/openvpn/Makefile.am                    |   1 +
 src/openvpn/auth_token.c                   | 387 +++++++++++++++++++++
 src/openvpn/auth_token.h                   | 129 +++++++
 src/openvpn/crypto.c                       |  35 +-
 src/openvpn/crypto.h                       |  15 +
 src/openvpn/init.c                         |  90 +++--
 src/openvpn/manage.c                       |   4 +-
 src/openvpn/openvpn.h                      |   1 +
 src/openvpn/options.c                      | 103 ++++--
 src/openvpn/options.h                      |  19 +-
 src/openvpn/push.c                         |  70 +++-
 src/openvpn/push.h                         |   8 +
 src/openvpn/ssl.c                          |  13 +-
 src/openvpn/ssl_common.h                   |  56 +--
 src/openvpn/ssl_verify.c                   | 213 ++++++------
 src/openvpn/ssl_verify.h                   |  15 +-
 src/openvpn/tls_crypt.c                    |  13 +-
 tests/unit_tests/openvpn/Makefile.am       |  18 +-
 tests/unit_tests/openvpn/test_auth_token.c | 375 ++++++++++++++++++++
 20 files changed, 1457 insertions(+), 249 deletions(-)
 create mode 100644 src/openvpn/auth_token.c
 create mode 100644 src/openvpn/auth_token.h
 create mode 100644 tests/unit_tests/openvpn/test_auth_token.c
Signature · 1 line
2.21.0
#1 lore raw

PATCH v3 1/7 Write key to stdout if filename is not given

This change is preperation for changing the way --genkey works.

---
 src/openvpn/crypto.c    | 13 +++++++++++--
 src/openvpn/tls_crypt.c | 13 +++++++++++--
 2 files changed, 22 insertions(+), 4 deletions(-)
src/openvpn/crypto.c +11 -2
index ff9dbfdc..eb56421b 100644
@@ -1465,8 +1465,13 @@ write_key_file(const int nkeys, const char *filename)
 
     buf_printf(&out, "%s\n", static_key_foot);
 
+    /* write key file to stdout if no filename given */
+    if (!filename || strcmp(filename, "")==0)
+    {
+        printf("%s\n", BPTR(&out));
+    }
     /* write key file, now formatted in out, to file */
-    if (!buffer_write_file(filename, &out))
+    else if (!buffer_write_file(filename, &out))
     {
         nbits = -1;
     }
@@ -1870,7 +1875,11 @@ write_pem_key_file(const char *filename, const char *pem_name)
         goto cleanup;
     }
 
-    if (!buffer_write_file(filename, &server_key_pem))
+    if (!filename || strcmp(filename, "")==0)
+    {
+        printf("%s\n", BPTR(&server_key_pem));
+    }
+    else if (!buffer_write_file(filename, &server_key_pem))
     {
         msg(M_ERR, "ERROR: could not write key file");
         goto cleanup;
src/openvpn/tls_crypt.c +11 -2
index d6a82252..e8889e93 100644
@@ -697,7 +697,16 @@ tls_crypt_v2_write_client_key_file(const char *filename,
         goto cleanup;
     }
 
-    if (!buffer_write_file(filename, &client_key_pem))
+    const char *client_filename = filename;
+    const char *client_inline = NULL;
+
+    if (!filename || streq(filename, ""))
+    {
+        printf("%s\n", BPTR(&client_key_pem));
+        client_filename = INLINE_FILE_TAG;
+        client_inline = (const char *)BPTR(&client_key_pem);
+    }
+    else if (!buffer_write_file(filename, &client_key_pem))
     {
         msg(M_FATAL, "ERROR: could not write client key file");
         goto cleanup;
@@ -708,7 +717,7 @@ tls_crypt_v2_write_client_key_file(const char *filename,
     struct buffer test_wrapped_client_key;
     msg(D_GENKEY, "Testing client-side key loading...");
     tls_crypt_v2_init_client_key(&test_client_key, &test_wrapped_client_key,
-                                 filename, NULL);
+                                 client_filename, client_inline);
     free_key_ctx_bi(&test_client_key);
 
     /* Sanity check: unwrap and load client key (as "server") */
Signature · 1 line
2.21.0
Acked-by on v3 1/7
#2 lore raw

Re: [Openvpn-devel] [PATCH v3 1/7] Write key to stdout if filename is not given

Quoted 62 lines · Arne Schwabe

This change is preperation for changing the way --genkey works.

---
 src/openvpn/crypto.c    | 13 +++++++++++--
 src/openvpn/tls_crypt.c | 13 +++++++++++--
 2 files changed, 22 insertions(+), 4 deletions(-)
src/openvpn/crypto.c +11 -2
index ff9dbfdc..eb56421b 100644
@@ -1465,8 +1465,13 @@ write_key_file(const int nkeys, const char *filename)
 
     buf_printf(&out, "%s\n", static_key_foot);
 
+    /* write key file to stdout if no filename given */
+    if (!filename || strcmp(filename, "")==0)
+    {
+        printf("%s\n", BPTR(&out));
+    }
     /* write key file, now formatted in out, to file */
-    if (!buffer_write_file(filename, &out))
+    else if (!buffer_write_file(filename, &out))
     {
         nbits = -1;
     }
@@ -1870,7 +1875,11 @@ write_pem_key_file(const char *filename, const char *pem_name)
         goto cleanup;
     }
 
-    if (!buffer_write_file(filename, &server_key_pem))
+    if (!filename || strcmp(filename, "")==0)
+    {
+        printf("%s\n", BPTR(&server_key_pem));
+    }
+    else if (!buffer_write_file(filename, &server_key_pem))
     {
         msg(M_ERR, "ERROR: could not write key file");
         goto cleanup;
src/openvpn/tls_crypt.c +10 -1
index d6a82252..e8889e93 100644
@@ -697,7 +697,16 @@ tls_crypt_v2_write_client_key_file(const char *filename,
         goto cleanup;
     }
 
-    if (!buffer_write_file(filename, &client_key_pem))
+    const char *client_filename = filename;
+    const char *client_inline = NULL;
+
+    if (!filename || streq(filename, ""))
+    {
+        printf("%s\n", BPTR(&client_key_pem));
+        client_filename = INLINE_FILE_TAG;
+        client_inline = (const char *)BPTR(&client_key_pem);
+    }
+    else if (!buffer_write_file(filename, &client_key_pem))
     {
         msg(M_FATAL, "ERROR: could not write client key file");
         goto cleanup;

src/openvpn/tls_crypt.c tls_crypt_v2_write_client_key_file(const char *filename, · line 723

Quoted 8 lines
src/openvpn/tls_crypt.c +1 -1
@@ -708,7 +717,7 @@ tls_crypt_v2_write_client_key_file(const char *filename,
     struct buffer test_wrapped_client_key;
     msg(D_GENKEY, "Testing client-side key loading...");
     tls_crypt_v2_init_client_key(&test_client_key, &test_wrapped_client_key,
-                                 filename, NULL);
+                                 client_filename, client_inline);
     free_key_ctx_bi(&test_client_key);
 
     /* Sanity check: unwrap and load client key (as "server") */
Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc
#3 lore raw

[Openvpn-devel] [PATCH applied] Re: Write key to stdout if filename is not given

In reply to Arne Schwabe

Your patch has been applied to the master branch.

(No further review done on my side, just basic compile / make check)

commit f636d11ff50658c18e0b90b20d641dc54b63e517

Author: Arne Schwabe
Date:   Fri May 10 14:11:08 2019 +0200
     Write key to stdout if filename is not given

     Acked-by: David Sommerseth <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg18445.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#4 lore raw

PATCH v3 2/7 Implement --genkey type keyfile syntax and migrate tls-crypt-v2

In reply to Arne Schwabe

This unifies our key generation and also migrates the generation of the tls-crypt-v2 keys. Since tls-crypt-v2 is not included in any released version, we remove the the old syntax without compatibility.

---
 doc/openvpn.8         | 79 +++++++++++++++++++++++--------------------
 src/openvpn/init.c    | 61 ++++++++++++++++++++-------------
 src/openvpn/options.c | 67 ++++++++++++++++++++++--------------
 src/openvpn/options.h | 11 ++++--
 4 files changed, 131 insertions(+), 87 deletions(-)
doc/openvpn.8 +43 -36
index ce440447..90a6be91 100644
@@ -5242,7 +5242,7 @@ Use client\-specific tls\-crypt keys.
 For clients,
 .B keyfile
 is a client\-specific tls\-crypt key.  Such a key can be generated using the
-.B \-\-tls\-crypt\-v2\-genkey
+.B \-\-genkey tls\-crypt\-v2\-client
 option.
 
 For servers,
@@ -5250,7 +5250,7 @@ For servers,
 is used to unwrap client\-specific keys supplied by the client during connection
 setup.  This key must be the same as the key used to generate the
 client\-specific key (see
-.B \-\-tls\-crypt\-v2\-genkey\fR).
+.B \-\-genkey tls\-crypt\-v2\-client\fR).
 
 On servers, this option can be used together with the
 .B \-\-tls\-auth
@@ -5260,36 +5260,6 @@ option.  In that case, the server will detect whether the client is using
 client\-specific keys, and automatically select the right mode.
 .\"*********************************************************
 .TP
-.B \-\-tls\-crypt\-v2\-genkey client|server keyfile [metadata]
-
-If the first parameter equals "server", generate a \-\-tls\-crypt\-v2 server
-key and store the key in
-.B keyfile\fR.
-
-
-If the first parameter equals "client", generate a \-\-tls\-crypt\-v2 client
-key, and store the key in
-.B keyfile\fR.
-
-If supplied, include the supplied
-.B metadata
-in the wrapped client key.  This metadata must be supplied in base64\-encoded
-form.  The metadata must be at most 735 bytes long (980 bytes in base64).
-
-If no metadata is supplied, OpenVPN will use a 64\-bit unix timestamp
-representing the current time in UTC, encoded in network order, as metadata for
-the generated key.
-
-A tls\-crypt\-v2 client key is wrapped using a server key.  To generate a
-client key, the user must therefore supply the server key using the
-.B \-\-tls\-crypt\-v2
-option.
-
-Servers can use
-.B \-\-tls\-crypt\-v2\-verify
-to specify a metadata verification command.
-.\"*********************************************************
-.TP
 .B \-\-tls\-crypt\-v2\-verify cmd
 
 Run command
@@ -5741,13 +5711,18 @@ Show all available elliptic curves to use with the
 .B \-\-ecdh\-curve
 option.
 .\"*********************************************************
-.SS Generate a random key:
-Used only for non\-TLS static key encryption mode.
+.SS Generating random key material:
 .\"*********************************************************
 .TP
-.B \-\-genkey file
+.B \-\-genkey keytype keyfile
 (Standalone)
-Generate a random key to be used as a shared secret, for use with the
+Generate a random key to be used of the type keytype. if keyfile is left out or empty
+the key will be output on stdout. See the following sections for the different keytypes.
+
+.\"*********************************************************
+.TP
+.B \-\-genkey secret|tls-crypt|tls-auth keyfile
+Generate a shared secret, for use with the
 .B \-\-secret
 ,
 .B \-\-tls-auth
@@ -5755,6 +5730,8 @@ or
 .B \-\-tls-crypt
 options.  Stores the key in
 .B file\fR.
+All three variants (secret, tls-crypt, and tls-auth) generate the same type of
+key. The aliases are added for convience.
 
 If using this for
 .B \-\-secret
@@ -5762,6 +5739,36 @@ If using this for
 such as
 .BR scp (1)\fR.
 .\"*********************************************************
+.TP
+.B \-\-genkey tls\-crypt\-v2-server keyfile
+
+Generate a \-\-tls\-crypt\-v2 server key and store the key in
+.B keyfile\fR.
+
+.TP
+.B \-\-genkey tls\-crypt\-v2-client keyfile [metadata]
+
+Generate a \-\-tls\-crypt\-v2 client key, and store the key in
+.B keyfile\fR.
+
+If supplied, include the supplied
+.B metadata
+in the wrapped client key.  This metadata must be supplied in base64\-encoded
+form.  The metadata must be at most 735 bytes long (980 bytes in base64).
+
+If no metadata is supplied, OpenVPN will use a 64\-bit unix timestamp
+representing the current time in UTC, encoded in network order, as metadata for
+the generated key.
+
+A tls\-crypt\-v2 client key is wrapped using a server key.  To generate a
+client key, the user must therefore supply the server key using the
+.B \-\-tls\-crypt\-v2
+option.
+
+Servers can use
+.B \-\-tls\-crypt\-v2\-verify
+to specify a metadata verification command.
+.\"*********************************************************
 .SS TUN/TAP persistent tunnel config mode:
 Available with Linux 2.4.7+.  These options comprise a standalone mode
 of OpenVPN which can be used to create and delete persistent tunnels.
src/openvpn/init.c +37 -24

85 lines not on this page · raw message

src/openvpn/options.c +42 -25

120 lines not on this page · raw message

src/openvpn/options.h +9 -2
index e2b38939..d8e7efa4 100644
@@ -177,6 +177,12 @@ struct remote_host_store
     char port[RH_PORT_LEN];
 };
 
+enum genkey_type {
+    GENKEY_SECRET,
+    GENKEY_TLS_CRYPTV2_CLIENT,
+    GENKEY_TLS_CRYPTV2_SERVER,
+};
+
 /* Command line options */
 struct options
 {
@@ -207,6 +213,9 @@ struct options
     bool show_tls_ciphers;
     bool show_curves;
     bool genkey;
+    enum genkey_type genkey_type;
+    const char* genkey_filename;
+    const char* genkey_extra_data;
 
     /* Networking parms */
     int connect_retry_max;
@@ -589,8 +598,6 @@ struct options
     const char *tls_crypt_v2_file;
     const char *tls_crypt_v2_inline;
 
-    const char *tls_crypt_v2_genkey_type;
-    const char *tls_crypt_v2_genkey_file;
     const char *tls_crypt_v2_metadata;
 
     const char *tls_crypt_v2_verify_script;
Signature · 1 line
2.21.0
#5 lore raw

Re: [Openvpn-devel] [PATCH v3 2/7] Implement --genkey type keyfile syntax and migrate tls-crypt-v2

Quoted 70 lines · Arne Schwabe

This unifies our key generation and also migrates the generation of the tls-crypt-v2 keys. Since tls-crypt-v2 is not included in any released version, we remove the the old syntax without compatibility.

---
 doc/openvpn.8         | 79 +++++++++++++++++++++++--------------------
 src/openvpn/init.c    | 61 ++++++++++++++++++++-------------
 src/openvpn/options.c | 67 ++++++++++++++++++++++--------------
 src/openvpn/options.h | 11 ++++--
 4 files changed, 131 insertions(+), 87 deletions(-)
doc/openvpn.8 +2 -32
index ce440447..90a6be91 100644
@@ -5242,7 +5242,7 @@ Use client\-specific tls\-crypt keys.
 For clients,
 .B keyfile
 is a client\-specific tls\-crypt key.  Such a key can be generated using the
-.B \-\-tls\-crypt\-v2\-genkey
+.B \-\-genkey tls\-crypt\-v2\-client
 option.
 
 For servers,
@@ -5250,7 +5250,7 @@ For servers,
 is used to unwrap client\-specific keys supplied by the client during connection
 setup.  This key must be the same as the key used to generate the
 client\-specific key (see
-.B \-\-tls\-crypt\-v2\-genkey\fR).
+.B \-\-genkey tls\-crypt\-v2\-client\fR).
 
 On servers, this option can be used together with the
 .B \-\-tls\-auth
@@ -5260,36 +5260,6 @@ option.  In that case, the server will detect whether the client is using
 client\-specific keys, and automatically select the right mode.
 .\"*********************************************************
 .TP
-.B \-\-tls\-crypt\-v2\-genkey client|server keyfile [metadata]
-
-If the first parameter equals "server", generate a \-\-tls\-crypt\-v2 server
-key and store the key in
-.B keyfile\fR.
-
-
-If the first parameter equals "client", generate a \-\-tls\-crypt\-v2 client
-key, and store the key in
-.B keyfile\fR.
-
-If supplied, include the supplied
-.B metadata
-in the wrapped client key.  This metadata must be supplied in base64\-encoded
-form.  The metadata must be at most 735 bytes long (980 bytes in base64).
-
-If no metadata is supplied, OpenVPN will use a 64\-bit unix timestamp
-representing the current time in UTC, Rencoded in network order, as metadata for
-the generated key.
-
-A tls\-crypt\-v2 client key is wrapped using a server key.  To generate a
-client key, the user must therefore supply the server key using the
-.B \-\-tls\-crypt\-v2
-option.
-
-Servers can use
-.B \-\-tls\-crypt\-v2\-verify
-to specify a metadata verification command.
-.\"*********************************************************
-.TP
 .B \-\-tls\-crypt\-v2\-verify cmd
 
 Run command

doc/openvpn.8 Show all available elliptic curves to use with the · line 5714

Quoted 6 lines
doc/openvpn.8 +1 -2
@@ -5741,13 +5711,18 @@ Show all available elliptic curves to use with the
 .B \-\-ecdh\-curve
 option.
 .\"*********************************************************
-.SS Generate a random key:
-Used only for non\-TLS static key encryption mode.
+.SS Generating random key material:

"Random key material" sounds a bit odd. Is it important for most end-users that we emphasize "random"? Isn't enough saying: "Generating key material"?

doc/openvpn.8 Show all available elliptic curves to use with the · line 5720

Quoted 8 lines
doc/openvpn.8 +3 -2
 .\"*********************************************************
 .TP
-.B \-\-genkey file
+.B \-\-genkey keytype keyfile
 (Standalone)
-Generate a random key to be used as a shared secret, for use with the
+Generate a random key to be used of the type keytype. if keyfile is left out or empty
+the key will be output on stdout. See the following sections for the different keytypes.

Same as the comment above.

There is also some odd and unexpected behaviour, some which I already touched in my initial review overview mail.

 $ openvpn --genkey
 [new static key is sent to stdout, this is fine]
 $ openvpn --genkey --secret test.key
 [new static key saved to test.key, this is fine]
 $ openvpn --genkey secret
 [new static key is sent to stdout, this is fine]
 $ openvpn --genkey secret --secret test.key
 [new static key is saved to test.key, this is fine]
 $ openvpn --genkey auth-token
 [new static key is sent to stdout, this is fine]
 $ openvpn --genkey auth-token --secret test.key
 [new static key is sent to stdout - huh!?]
 $ openvpn --genkey tls-crypt --secret test.key
 [new static key is saved to test.key, this is fine]
 $ openvpn --genkey tls-crypt-v2-server --secret test.key
 [new static key is sent to stdout - huh!?]

This behaviour change with/without --secret where it sometimes writes file to stdout, sometimes to file is confusing. *I* know why, but users not diving into the code does not.

I also know filename can be given as an argument to --genkey. So I propose that we either ditch --secret with --genkey all together. OpenVPN 2.5 is a new major release, we can allow such a change; it will not break existing configuration files. By not allowing --secret to be used with --genkey, we avoid this ambiguity.

We could consider to support --secret properly with --genkey, but that will make the '--genkey tls-crypt-v2-client KEYFILE [METADATA]' trickier and less consistent. If --secret is present, should that remove the need for KEYFILE, should KEYFILE overrule --secret or should KEYFILE be ignored? This makes it much less consistent.

Another challenge: Currently we cannot generate tls-crypt-v2-client keys to stdout with METADATA.

Should we rather do what other *nix programs often do, use '-' as and indication for "write to stdout"? Or have a specific keyword (like 'stdout:')?

Quoted 14 lines
src/openvpn/init.c +1 -2
index 3c449678..9260067f 100644
@@ -1053,18 +1053,33 @@ bool
 do_genkey(const struct options *options)
 {
     /* should we disable paging? */
-    if (options->mlock && (options->genkey || options->tls_crypt_v2_genkey_file))
+    if (options->mlock && (options->genkey))
     {
         platform_mlockall(true);
     }
-    if (options->genkey)

src/openvpn/init.c bool · line 1067

Quoted 10 lines
src/openvpn/init.c +5 -2
+    if (options->genkey && options->genkey_type == GENKEY_SECRET)
     {
         int nbits_written;
 
-        notnull(options->shared_secret_file,
-                "shared secret output file (--secret)");
+        if (options->shared_secret_file && options->genkey_filename)
+        {
+            msg(M_USAGE, "You must provide a filename to either --genkey or --secret, not both");
+        }

By removing support for --genkey with --secret, this check need to moved outside this if() scope and check whether --secret and --genkey are both present.

Otherwise, this patch looks reasonable.

Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc
#6 lore raw

Re: [Openvpn-devel] [PATCH v3 2/7] Implement --genkey type keyfile syntax and migrate tls-crypt-v2

Am 07.06.2019 um 22:46 schrieb David Sommerseth:

Quoted 108 lines · Arne Schwabe
Quoted 70 lines

This unifies our key generation and also migrates the generation of the tls-crypt-v2 keys. Since tls-crypt-v2 is not included in any released version, we remove the the old syntax without compatibility.

---
 doc/openvpn.8         | 79 +++++++++++++++++++++++--------------------
 src/openvpn/init.c    | 61 ++++++++++++++++++++-------------
 src/openvpn/options.c | 67 ++++++++++++++++++++++--------------
 src/openvpn/options.h | 11 ++++--
 4 files changed, 131 insertions(+), 87 deletions(-)
doc/openvpn.8 +2 -32
index ce440447..90a6be91 100644
@@ -5242,7 +5242,7 @@ Use client\-specific tls\-crypt keys.
 For clients,
 .B keyfile
 is a client\-specific tls\-crypt key.  Such a key can be generated using the
-.B \-\-tls\-crypt\-v2\-genkey
+.B \-\-genkey tls\-crypt\-v2\-client
 option.
 
 For servers,
@@ -5250,7 +5250,7 @@ For servers,
 is used to unwrap client\-specific keys supplied by the client during connection
 setup.  This key must be the same as the key used to generate the
 client\-specific key (see
-.B \-\-tls\-crypt\-v2\-genkey\fR).
+.B \-\-genkey tls\-crypt\-v2\-client\fR).
 
 On servers, this option can be used together with the
 .B \-\-tls\-auth
@@ -5260,36 +5260,6 @@ option.  In that case, the server will detect whether the client is using
 client\-specific keys, and automatically select the right mode.
 .\"*********************************************************
 .TP
-.B \-\-tls\-crypt\-v2\-genkey client|server keyfile [metadata]
-
-If the first parameter equals "server", generate a \-\-tls\-crypt\-v2 server
-key and store the key in
-.B keyfile\fR.
-
-
-If the first parameter equals "client", generate a \-\-tls\-crypt\-v2 client
-key, and store the key in
-.B keyfile\fR.
-
-If supplied, include the supplied
-.B metadata
-in the wrapped client key.  This metadata must be supplied in base64\-encoded
-form.  The metadata must be at most 735 bytes long (980 bytes in base64).
-
-If no metadata is supplied, OpenVPN will use a 64\-bit unix timestamp
-representing the current time in UTC, Rencoded in network order, as metadata for
-the generated key.
-
-A tls\-crypt\-v2 client key is wrapped using a server key.  To generate a
-client key, the user must therefore supply the server key using the
-.B \-\-tls\-crypt\-v2
-option.
-
-Servers can use
-.B \-\-tls\-crypt\-v2\-verify
-to specify a metadata verification command.
-.\"*********************************************************
-.TP
 .B \-\-tls\-crypt\-v2\-verify cmd
 
 Run command

doc/openvpn.8 Show all available elliptic curves to use with the · line 5714

Quoted 6 lines
doc/openvpn.8 +1 -2
@@ -5741,13 +5711,18 @@ Show all available elliptic curves to use with the
 .B \-\-ecdh\-curve
 option.
 .\"*********************************************************
-.SS Generate a random key:
-Used only for non\-TLS static key encryption mode.
+.SS Generating random key material:

"Random key material" sounds a bit odd. Is it important for most end-users that we emphasize "random"? Isn't enough saying: "Generating key material"?

doc/openvpn.8 Show all available elliptic curves to use with the · line 5720

Quoted 8 lines
doc/openvpn.8 +3 -2
 .\"*********************************************************
 .TP
-.B \-\-genkey file
+.B \-\-genkey keytype keyfile
 (Standalone)
-Generate a random key to be used as a shared secret, for use with the
+Generate a random key to be used of the type keytype. if keyfile is left out or empty
+the key will be output on stdout. See the following sections for the different keytypes.

Same as the comment above.

There is also some odd and unexpected behaviour, some which I already touched in my initial review overview mail.

 $ openvpn --genkey
 [new static key is sent to stdout, this is fine]
 $ openvpn --genkey --secret test.key
 [new static key saved to test.key, this is fine]
 $ openvpn --genkey secret
 [new static key is sent to stdout, this is fine]
 $ openvpn --genkey secret --secret test.key
 [new static key is saved to test.key, this is fine]
 $ openvpn --genkey auth-token
 [new static key is sent to stdout, this is fine]
Quoted 8 lines
 $ openvpn --genkey auth-token --secret test.key
 [new static key is sent to stdout - huh!?]
 $ openvpn --genkey tls-crypt --secret test.key
 [new static key is saved to test.key, this is fine]
 $ openvpn --genkey tls-crypt-v2-server --secret test.key
 [new static key is sent to stdout - huh!?]

I will add an error if genkey is used with -secret for anything other than secret.

Quoted 9 lines

This behaviour change with/without --secret where it sometimes writes file to stdout, sometimes to file is confusing. *I* know why, but users not diving into the code does not.

I also know filename can be given as an argument to --genkey. So I propose that we either ditch --secret with --genkey all together. OpenVPN 2.5 is a new major release, we can allow such a change; it will not break existing configuration files. By not allowing --secret to be used with --genkey, we avoid this ambiguity.

For 2.5 I would rather have a warning telling that --gen-key --secret filename is deprecated in favour of --gen-key secret filename in 2.5 and will be removed in the next version.

Quoted 11 lines

We could consider to support --secret properly with --genkey, but that will make the '--genkey tls-crypt-v2-client KEYFILE [METADATA]' trickier and less consistent. If --secret is present, should that remove the need for KEYFILE, should KEYFILE overrule --secret or should KEYFILE be ignored? This makes it much less consistent.

Another challenge: Currently we cannot generate tls-crypt-v2-client keys to stdout with METADATA.

Should we rather do what other *nix programs often do, use '-' as and indication for "write to stdout"? Or have a specific keyword (like 'stdout:')?

Hm, using - seems to be the standard way but we need to check if there are other instances where we need to support - then too, to not make it inconsistent.

Arne

#7 lore raw

PATCH v3 3/7 Add pem_read_key_file variant that allows a random key

In reply to Arne Schwabe

From: Arne Schwabe <[email protected]...>

This is useful for features that can use enither a persistent or an ephemeral key.

Patch V2: Move the functionality of generating a random key into a
separate function that acts as wrapper for pem_read_key_file

---
 src/openvpn/crypto.c | 22 ++++++++++++++++++++++
 src/openvpn/crypto.h | 15 +++++++++++++++
 2 files changed, 37 insertions(+)
src/openvpn/crypto.c +22 -0
index eb56421b..0e17c351 100644
@@ -1892,6 +1892,28 @@ cleanup:
     return;
 }
 
+bool
+load_pem_file_or_ephemeral_key(struct buffer *key, const char *pem_name,
+                               const char *key_file, const char *key_inline)
+{
+    if (key_file == NULL)
+    {
+        msg(M_INFO, "Using random %s.", pem_name);
+        uint8_t rand[BCAP(key)];
+        if (!rand_bytes(rand, BCAP(key)))
+        {
+            msg(M_WARN, "ERROR: could not generate random key");
+            return false;
+        }
+        buf_write(key, rand, BCAP(key));
+        return true;
+    }
+    else
+    {
+        return read_pem_key_file(key, pem_name, key_file, key_inline);
+    }
+}
+
 bool
 read_pem_key_file(struct buffer *key, const char *pem_name,
                   const char *key_file, const char *key_inline)
src/openvpn/crypto.h +15 -0
index c5947483..4ae02571 100644
@@ -430,6 +430,21 @@ unsigned int crypto_max_overhead(void);
 void
 write_pem_key_file(const char *filename, const char *pem_name);
 
+/**
+ * Read key material from a PEM encoded files into the key structure or if
+ * the filename is not passed generate a ephemeral key
+ *
+ * @param key           the key structure that will hold the key material
+ * @param pem_name      the name used in the pem encoding start/end lines
+ * @param key_file      name of the file to read or NULL if an ephermal key
+ *                      should be generated
+ * @param key_inline    a string holding the data in case of an inline key
+ * @return              true if reading into key was successful or generated
+ */
+bool
+load_pem_file_or_ephemeral_key(struct buffer *key, const char *pem_name,
+                               const char *key_file, const char *key_inline);
+
 /**
  * Read key material from a PEM encoded files into the key structure
  * @param key           the key structure that will hold the key material
Signature · 1 line
2.21.0
#8 lore raw

Re: [Openvpn-devel] [PATCH v3 3/7] Add pem_read_key_file variant that allows a random key

Quoted 17 lines · Arne Schwabe

From: Arne Schwabe <[email protected]...>

This is useful for features that can use enither a persistent or an ephemeral key.

Patch V2: Move the functionality of generating a random key into a
separate function that acts as wrapper for pem_read_key_file

---
 src/openvpn/crypto.c | 22 ++++++++++++++++++++++
 src/openvpn/crypto.h | 15 +++++++++++++++
 2 files changed, 37 insertions(+)
src/openvpn/crypto.c +0 -0
index eb56421b..0e17c351 100644

src/openvpn/crypto.c cleanup: · line 1899

Quoted 8 lines
src/openvpn/crypto.c +5 -0
@@ -1892,6 +1892,28 @@ cleanup:
     return;
 }
 
+bool
+load_pem_file_or_ephemeral_key(struct buffer *key, const char *pem_name,
+                               const char *key_file, const char *key_inline)
+{
+    if (key_file == NULL)

I don't like this approach. Why can't we have generate_ephemeral_key() basically doing the keyfile == NULL part below and put the "which key to use" logic in the only caller I could see in auth_token_init_secret() [added in a later patch] ? This makes the code clearer:

-----------------------------------------------------------------

void
auth_token_init_secret(struct key_ctx *key_ctx, const char *key_file,
const char *key_inline)
{
/* .... */
bool key_loaded = false;
if (key_file)

   {
	key_loaded = read_pem_key(...);
   }
   else
   {
        key_loaded = generate_ephemeral_key(...);
   }
   if (!key_loaded)
   {
        msg(M_FATAL, ....);

}
/* .... */
-----------------------------------------------------------------

You could also make it more compact, but not sure it's worth it here, as these functions requires a few longer argument lists:

   bool key_loaded = (key_file ? read_pem_key(...)
                               : generate_ephemeral_key(...));

The point is: This makes read_pem_key() and generate_ephemeral_key() do only one simple and predictable task. The load_pem_file_or_ephemeral_key() does one of two things, depending on the argument. And since I only see we have only a single caller to the load_pem_file_or_ephemeral_key(), there's nothing to save on code reuse by wrapping it up like this.

Quoted 17 lines
src/openvpn/crypto.c +17 -0
+    {
+        msg(M_INFO, "Using random %s.", pem_name);
+        uint8_t rand[BCAP(key)];
+        if (!rand_bytes(rand, BCAP(key)))
+        {
+            msg(M_WARN, "ERROR: could not generate random key");
+            return false;
+        }
+        buf_write(key, rand, BCAP(key));
+        return true;
+    }
+    else
+    {
+        return read_pem_key_file(key, pem_name, key_file, key_inline);
+    }
+}
+
Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc
#9 lore raw

PATCH v3 4/7 Rewrite auth-token-gen to be based on HMAC based tokens

In reply to Arne Schwabe

The previous auth-token implementation had a serious problem, especially when paired with an unpatched OpenVPN client that keeps trying the auth-token (commit e61b401a).

The auth-token-gen implementation forgot the auth-token on reconnect, this lead to reconnect with auth-token never working.

This new implementation implements the auth-token in a stateles variant. By using HMAC to sign the auth-token the server can verify if a token has been authenticated and by checking the embedded timestamp in the token it can also verify that the auth-token is still valid.

Using the new config directive auth-gen-token-secret instead of extending auth-gen-token (--auth-gen-token [lifetime] [secret-key]) was chosen to allow inlinening the secret key.

Patch V2: cleaned up code, use refactored read_pem_key_file function Patch V3: clarify some design decision in the commit message

---
 doc/openvpn.8            |  25 ++++
 src/openvpn/Makefile.am  |   1 +
 src/openvpn/auth_token.c | 261 +++++++++++++++++++++++++++++++++++++++
 src/openvpn/auth_token.h | 116 +++++++++++++++++
 src/openvpn/init.c       |  28 ++++-
 src/openvpn/openvpn.h    |   1 +
 src/openvpn/options.c    |  22 +++-
 src/openvpn/options.h    |   4 +
 src/openvpn/push.c       |  70 +++++++++--
 src/openvpn/push.h       |   8 ++
 src/openvpn/ssl.c        |   7 +-
 src/openvpn/ssl_common.h |  36 +++---
 src/openvpn/ssl_verify.c | 182 ++++++++++++---------------
 13 files changed, 626 insertions(+), 135 deletions(-)
 create mode 100644 src/openvpn/auth_token.c
 create mode 100644 src/openvpn/auth_token.h
doc/openvpn.8 +25 -0
index 90a6be91..0ae617a3 100644
@@ -3720,6 +3720,9 @@ the token authentication internally and it will NOT do any
 additional authentications against configured external
 user/password authentication mechanisms.
 
+The tokens implemented by this mechanism include a initial timestamp
+and a renew timestamp and are secured by HMAC.
+
 The
 .B lifetime
 argument defines how long the generated token is valid.  The
@@ -3730,6 +3733,16 @@ This feature is useful for environments which is configured
 to use One Time Passwords (OTP) as part of the user/password
 authentications and that authentication mechanism does not
 implement any auth\-token support.
+.\"*********************************************************
+.TP
+.B \-\-auth\-gen\-token\-secret [file]
+Specifies a file that hold a secret for the HMAC used in
+.B \-\-auth\-gen\-token
+If not present OpenVPN will generate a random secret on startup. This file
+should be used if auth-token should valid after restarting a server or if
+client should be able to roam between multiple OpenVPN server with their
+auth\-token.
+
 .\"*********************************************************
 .TP
 .B \-\-opt\-verify
@@ -5769,6 +5782,17 @@ Servers can use
 .B \-\-tls\-crypt\-v2\-verify
 to specify a metadata verification command.
 .\"*********************************************************
+.TP
+.B \-\-genkey auth\-token [keyfile]
+Generate a new secret that can be used
+with
+.B \-\-auth\-gen\-token\-secret
+
+.B Note:
+this file should be kept secret to the server as anyone
+that access to this file will be to generate auth tokens
+that the OpenVPN server will accept as valid.
+.\"*********************************************************
 .SS TUN/TAP persistent tunnel config mode:
 Available with Linux 2.4.7+.  These options comprise a standalone mode
 of OpenVPN which can be used to create and delete persistent tunnels.
@@ -6980,6 +7004,7 @@ X509_1_C=KG
 OpenVPN allows including files in the main configuration for the
 .B \-\-ca, \-\-cert, \-\-dh, \-\-extra\-certs, \-\-key, \-\-pkcs12, \-\-secret,
 .B \-\-crl\-verify, \-\-http\-proxy\-user\-pass, \-\-tls\-auth,
+.B \-\-auth\-gen\-token\-secret
 .B \-\-tls\-crypt,
 and
 .B \-\-tls\-crypt-v2
src/openvpn/Makefile.am +1 -0
index 197e62ba..78f94762 100644
@@ -39,6 +39,7 @@ sbin_PROGRAMS = openvpn
 
 openvpn_SOURCES = \
 	argv.c argv.h \
+	auth_token.c auth_token.h \
 	base64.c base64.h \
 	basic.h \
 	buffer.c buffer.h \
src/openvpn/auth_token.c new file +261 -0

261 lines not on this page · raw message

src/openvpn/auth_token.h new file +116 -0

116 lines not on this page · raw message

src/openvpn/init.c +27 -1
index 9260067f..1732312f 100644
@@ -51,6 +51,7 @@
 #include "ssl_verify.h"
 #include "tls_crypt.h"
 #include "forward.h"
+#include "auth_token.h"
 
 #include "memdbg.h"
 
@@ -1108,6 +1109,11 @@ do_genkey(const struct options *options)
                 options->tls_crypt_v2_inline);
         return true;
     }
+    else if (options->genkey && options->genkey_type == GENKEY_AUTH_TOKEN)
+    {
+        auth_token_write_server_key_file(options->genkey_filename);
+        return true;
+    }
     else
     {
         return false;
@@ -2503,7 +2509,6 @@ init_crypto_pre(struct context *c, const unsigned int flags)
         rand_ctx_enable_prediction_resistance();
     }
 #endif
-
 }
 
 /*
@@ -2627,6 +2632,20 @@ do_init_tls_wrap_key(struct context *c)
 
 }
 
+/*
+ * Initialise the auth-token key context
+ */
+static void
+do_init_auth_token_key(struct context *c)
+{
+    if (!c->options.auth_token_generate)
+        return;
+
+    auth_token_init_secret(&c->c1.ks.auth_token_key,
+                           c->options.auth_token_secret_file,
+                           c->options.auth_token_secret_file_inline);
+}
+
 /*
  * Initialize the persistent component of OpenVPN's TLS mode,
  * which is preserved across SIGUSR1 resets.
@@ -2679,6 +2698,9 @@ do_init_crypto_tls_c1(struct context *c)
         /* initialize tls-auth/crypt/crypt-v2 key */
         do_init_tls_wrap_key(c);
 
+        /* initialise auth-token crypto support */
+        do_init_auth_token_key(c);
+
 #if 0 /* was: #if ENABLE_INLINE_FILES --  Note that enabling this code will break restarts */
         if (options->priv_key_file_inline)
         {
@@ -2851,6 +2873,7 @@ do_init_crypto_tls(struct context *c, const unsigned int flags)
     to.auth_user_pass_file = options->auth_user_pass_file;
     to.auth_token_generate = options->auth_token_generate;
     to.auth_token_lifetime = options->auth_token_lifetime;
+    to.auth_token_key = c->c1.ks.auth_token_key;
 #endif
 
     to.x509_track = options->x509_track;
@@ -4464,6 +4487,9 @@ inherit_context_child(struct context *dest,
     dest->c1.authname = src->c1.authname;
     dest->c1.keysize = src->c1.keysize;
 
+    /* inherit auth-token */
+    dest->c1.ks.auth_token_key = src->c1.ks.auth_token_key;
+
     /* options */
     dest->options = src->options;
     options_detach(&dest->options);
src/openvpn/openvpn.h +1 -0
index d11f61df..30b5aeb4 100644
@@ -68,6 +68,7 @@ struct key_schedule
     struct key_ctx_bi tls_wrap_key;
     struct key_ctx tls_crypt_v2_server_key;
     struct buffer tls_crypt_v2_wkc;             /**< Wrapped client key */
+    struct key_ctx auth_token_key;
 };
 
 /*
src/openvpn/options.c +21 -1
index ced5e987..3dac1e7a 100644
@@ -1289,6 +1289,7 @@ show_p2mp_parms(const struct options *o)
     SHOW_BOOL(auth_user_pass_verify_script_via_file);
     SHOW_BOOL(auth_token_generate);
     SHOW_INT(auth_token_lifetime);
+    SHOW_STR(auth_token_secret_file);
 #if PORT_SHARE
     SHOW_STR(port_share_host);
     SHOW_STR(port_share_port);
@@ -2336,7 +2337,11 @@ options_postprocess_verify_ce(const struct options *options, const struct connec
         {
             msg(M_USAGE, "--mode server requires --key-method 2");
         }
-
+        if (options->auth_token_generate && !options->renegotiate_seconds)
+        {
+            msg(M_USAGE, "--auth-gen-token needs a non-infinite "
+                "--renegotiate_seconds setting");
+        }
         {
             const bool ccnr = (options->auth_user_pass_verify_script
                                || PLUGIN_OPTION_LIST(options)
@@ -6767,6 +6772,17 @@ add_option(struct options *options,
         options->auth_token_generate = true;
         options->auth_token_lifetime = p[1] ? positive_atoi(p[1]) : 0;
     }
+    else if (streq(p[0], "auth-gen-token-secret") && p[1] && (!p[2]
+                                                              || (p[2] && streq(p[1], INLINE_FILE_TAG))))
+    {
+        VERIFY_PERMISSION(OPT_P_GENERAL);
+        options->auth_token_secret_file = p[1];
+
+        if (streq(p[1], INLINE_FILE_TAG) && p[2])
+        {
+            options->auth_token_secret_file_inline = p[2];
+        }
+    }
     else if (streq(p[0], "client-connect") && p[1])
     {
         VERIFY_PERMISSION(OPT_P_SCRIPT);
@@ -7542,6 +7558,10 @@ add_option(struct options *options,
                     options->genkey_extra_data = p[3];
                 }
             }
+            else if (streq(p[1], "auth-token"))
+            {
+                options->genkey_type = GENKEY_AUTH_TOKEN;
+            }
             else
             {
                 msg(msglevel, "unknown --genkey type: %s", p[1]);
src/openvpn/options.h +4 -0
index d8e7efa4..cad5be81 100644
@@ -181,6 +181,7 @@ enum genkey_type {
     GENKEY_SECRET,
     GENKEY_TLS_CRYPTV2_CLIENT,
     GENKEY_TLS_CRYPTV2_SERVER,
+    GENKEY_AUTH_TOKEN
 };
 
 /* Command line options */
@@ -469,6 +470,9 @@ struct options
     bool auth_user_pass_verify_script_via_file;
     bool auth_token_generate;
     unsigned int auth_token_lifetime;
+    const char *auth_token_secret_file;
+    const char *auth_token_secret_file_inline;
+
 #if PORT_SHARE
     char *port_share_host;
     char *port_share_port;
src/openvpn/push.c +59 -11

106 lines not on this page · raw message

src/openvpn/push.h +8 -0
index 5f6181e7..070782dd 100644
@@ -69,6 +69,14 @@ void send_auth_failed(struct context *c, const char *client_reason);
 
 void send_restart(struct context *c, const char *kill_msg);
 
+/**
+ * Sends a push reply message only containin the auth-token to update
+ * the auth-token on the client
+ *
+ * @param multi  - The tls_multi structure belonging to the instance to push to
+ */
+void send_push_reply_auth_token(struct tls_multi *multi);
+
 #endif
 #endif /* if P2MP */
 #endif /* ifndef PUSH_H */
src/openvpn/ssl.c +2 -5
index 4c81ae72..69d7be57 100644
@@ -59,6 +59,7 @@
 #include "ssl.h"
 #include "ssl_verify.h"
 #include "ssl_backend.h"
+#include "auth_token.h"
 
 #include "memdbg.h"
 
@@ -1368,11 +1369,7 @@ tls_multi_free(struct tls_multi *multi, bool clear)
 
     cert_hash_free(multi->locked_cert_hash_set);
 
-    if (multi->auth_token)
-    {
-        secure_memzero(multi->auth_token, AUTH_TOKEN_SIZE);
-        free(multi->auth_token);
-    }
+    wipe_auth_token(multi);
 
     free(multi->remote_ciphername);
 
src/openvpn/ssl_common.h +22 -14
index 410b2163..ff8ffacc 100644
@@ -298,7 +298,6 @@ struct tls_options
     /** TLS handshake wrapping state */
     struct tls_wrap_ctx tls_wrap;
 
-    /* frame parameters for TLS control channel */
     struct frame frame;
 
     /* used for username/password authentication */
@@ -306,10 +305,16 @@ struct tls_options
     bool auth_user_pass_verify_script_via_file;
     const char *tmp_dir;
     const char *auth_user_pass_file;
-    bool auth_token_generate;   /**< Generate auth-tokens on successful user/pass auth,
-                                 *   set via options->auth_token_generate. */
+
+#ifdef P2MP_SERVER
+    bool auth_token_generate;   /**< Generate auth-tokens on successful
+                                 * user/pass auth,seet via
+                                 * options->auth_token_generate. */
     unsigned int auth_token_lifetime;
 
+    struct key_ctx auth_token_key;
+#endif
+
     /* use the client-config-dir as a positive authenticator */
     const char *client_config_dir_exclusive;
 
@@ -368,10 +373,6 @@ struct tls_options
 /** @} name Index of key_state objects within a tls_session structure */
 /** @} addtogroup control_processor */
 
-#define AUTH_TOKEN_SIZE 32      /**< Size of server side generated auth tokens.
-                                 *   32 bytes == 256 bits
-                                 */
-
 /**
  * Security parameter state of a single session within a VPN tunnel.
  * @ingroup control_processor
@@ -539,7 +540,21 @@ struct tls_multi
      * over control channel.
      */
     char *peer_info;
+    char *auth_token;    /**< If server sends a generated auth-token,
+                          *   this is the token to use for future
+                          *   user/pass authentications in this session.
+                          */
+    char *auth_token_initial;
+    /**< The first auth-token we sent to a client, for clients that do
+     * not update their auth-token (older OpenVPN3 core versions)
+     */
+#define  AUTH_TOKEN_HMAC_OK              (1<<0)
+    /**< Auth-token sent from client has valid hmac */
+#define  AUTH_TOKEN_EXPIRED              (1<<1)
+    /**< Auth-token sent from client has expired */
 #endif
+    int auth_token_state_flags;
+    /**< The state of the auth-token sent from the client last time */
 
     /* For P_DATA_V2 */
     uint32_t peer_id;
@@ -547,13 +562,6 @@ struct tls_multi
 
     char *remote_ciphername;    /**< cipher specified in peer's config file */
 
-    char *auth_token;    /**< If server sends a generated auth-token,
-                          *   this is the token to use for future
-                          *   user/pass authentications in this session.
-                          */
-    time_t auth_token_tstamp; /**< timestamp of the generated token */
-    bool auth_token_sent; /**< If server uses --auth-gen-token and
-                           *   token has been sent to client */
     /*
      * Our session objects.
      */
src/openvpn/ssl_verify.c +79 -103

240 lines not on this page · raw message

Signature · 1 line
2.21.0
#10 lore raw

PATCH v3 5/7 Implement a permanent session id in auth-token

In reply to Arne Schwabe

From: Arne Schwabe <[email protected]...>

This allows an external authentication method
(e.g. management interface) to track the connection and distinguish a reconnection from multiple connections.

Addtionally this now also checks to workaround a problem with OpenVPN 3 core that sometimes uses a username hint from the config instead of using an empty username if the token would be valid with an empty username. Accepting such token can be only done explicitly when the external-auth keyword to auth-gen-token is present.

Patch V2: Add Empty variants to work around behaviour in openvpn 3 Patch V3: document the behaviour of external-auth better in the man page,
rename 'auth' parameter to 'external-auth'

---
 doc/openvpn.8            |  37 +++++++++-
 src/openvpn/auth_token.c | 145 ++++++++++++++++++++++++++++++++++++---
 src/openvpn/auth_token.h |  15 +++-
 src/openvpn/init.c       |   1 +
 src/openvpn/manage.c     |   4 +-
 src/openvpn/options.c    |  14 +++-
 src/openvpn/options.h    |   4 +-
 src/openvpn/ssl_common.h |  12 +++-
 src/openvpn/ssl_verify.c |  67 ++++++++++++------
 9 files changed, 260 insertions(+), 39 deletions(-)
doc/openvpn.8 +36 -1
index 0ae617a3..8c16a272 100644
@@ -3710,7 +3710,7 @@ For a sample script that performs PAM authentication, see
 in the OpenVPN source distribution.
 .\"*********************************************************
 .TP
-.B \-\-auth\-gen\-token [lifetime]
+.B \-\-auth\-gen\-token [lifetime] [auth]
 After successful user/password authentication, the OpenVPN
 server will with this option generate a temporary
 authentication token and push that to client.  On the following
@@ -3733,6 +3733,41 @@ This feature is useful for environments which is configured
 to use One Time Passwords (OTP) as part of the user/password
 authentications and that authentication mechanism does not
 implement any auth\-token support.
+
+When the external-auth keyword is present the normal authentication
+method will always be called even if auth-token succeeds. Normally other
+authentications method are skipped if auth-token verification suceeds or
+fails.
+
+This option postpones this decision to the external authentication methods
+and check the validity of the account and do other checks.
+
+In this mode the environment will have a session_id variable
+that hold the session id from auth-gen-token. Also a environment
+variable session_state is present. This variable tells whether the
+auth-token has succeeded or not. It can have the following values:
+
+ - Initial:                    No token from client.
+ - Authenticated:              Token is valid and not expired
+ - Expired:                    Token is valid but has expired
+ - Invalid                     Token is invalid (failed HMAC or wrong length)
+ - AuthenticatedEmptyUser/ExpiredEmptyUser
+   The token is not valid with the username send from the client
+   but would be valid (or expired) if we assume an empty username was used
+   instead. These two cases are a workaround for behaviour in OpenVPN3. If
+   this workaround is not needed these two cases should be handled in the
+   same way as Invalid.
+
+.B Warning:
+Use this feature only if you want your authentication method called on
+every verification. Since the external authentication is called it needs
+to also indicate a success or failure of the authentication. It is strongly
+recommended to return an authentication failure in the case of the
+Invalid/Expired auth-token with the external-auth option unless the client
+could authenticate in another acceptable way (e.g. client certificate),
+otherwise returning success will lead to authentication bypass (as does
+returning success on a wrong password from a script).
+
 .\"*********************************************************
 .TP
 .B \-\-auth\-gen\-token\-secret [file]
src/openvpn/auth_token.c +134 -11

224 lines not on this page · raw message

src/openvpn/auth_token.h +14 -1
index 600ac29f..c10afde9 100644
@@ -34,7 +34,8 @@
  *
  * Format of the auth-token (before base64 encode)
  *
- * uint64 timestamp (4 bytes)|uint64 timestamp (4 bytes)|sha256-hmac(32 bytes)
+ * session id(12 bytes)|uint64 timestamp (4 bytes)|
+ * uint64 timestamp (4 bytes)|sha256-hmac(32 bytes)
  *
  * The first timestamp is the time the token was initially created and is used to
  * determine the maximum renewable time of the token. We always include this even
@@ -44,6 +45,10 @@
  * to determine if this token has been renewed in the acceptable time range
  * (2 * renogiation timeout)
  *
+ * The session is a random string of 12 byte (or 16 in base64) that is not used by
+ * OpenVPN itself but kept intact so that external logging/managment can track the
+ * session multiple reconnects/servers
+ *
  * The hmac is calculated over the username contactinated with the
  * raw auth-token bytes to include authentication of the username in the token
  *
@@ -82,6 +87,14 @@ auth_token_init_secret(struct key_ctx *key_ctx, const char *key_file,
 void auth_token_write_server_key_file(const char *filename);
 
 
+/**
+ * Put the session id, and auth token status into the environment
+ * if auth-token is enabled
+ *
+ */
+void add_session_token_env(struct tls_session *session, struct tls_multi *multi,
+                           const struct user_pass *up);
+
 /**
  * Wipes the authentication token out of the memory, frees and cleans up
  * related buffers and flags
src/openvpn/init.c +1 -0
index 1732312f..a3cc1f37 100644
@@ -2873,6 +2873,7 @@ do_init_crypto_tls(struct context *c, const unsigned int flags)
     to.auth_user_pass_file = options->auth_user_pass_file;
     to.auth_token_generate = options->auth_token_generate;
     to.auth_token_lifetime = options->auth_token_lifetime;
+    to.auth_token_call_auth = options->auth_token_call_auth;
     to.auth_token_key = c->c1.ks.auth_token_key;
 #endif
 
src/openvpn/manage.c +3 -1
index 2d86dad4..1d97c2b6 100644
@@ -2736,7 +2736,9 @@ env_filter_match(const char *env_str, const int env_filter_level)
         "ifconfig_pool_netmask=",
         "time_duration=",
         "bytes_sent=",
-        "bytes_received="
+        "bytes_received=",
+        "session_id=",
+        "session_state="
     };
 
     if (env_filter_level == 0)
src/openvpn/options.c +13 -1
index 3dac1e7a..9993063a 100644
@@ -6766,11 +6766,23 @@ add_option(struct options *options,
                         &options->auth_user_pass_verify_script,
                         p[1], "auth-user-pass-verify", true);
     }
-    else if (streq(p[0], "auth-gen-token"))
+    else if (streq(p[0], "auth-gen-token") && !p[3])
     {
         VERIFY_PERMISSION(OPT_P_GENERAL);
         options->auth_token_generate = true;
         options->auth_token_lifetime = p[1] ? positive_atoi(p[1]) : 0;
+        if (p[2])
+        {
+            if (streq(p[2], "external-auth"))
+            {
+                options->auth_token_call_auth = true;
+            }
+            else
+            {
+                msg(msglevel, "Invalid argument to auth-gen_token: %s", p[2]);
+            }
+        }
+
     }
     else if (streq(p[0], "auth-gen-token-secret") && p[1] && (!p[2]
                                                               || (p[2] && streq(p[1], INLINE_FILE_TAG))))
src/openvpn/options.h +3 -1
index cad5be81..4b3883e6 100644
@@ -469,7 +469,9 @@ struct options
     const char *auth_user_pass_verify_script;
     bool auth_user_pass_verify_script_via_file;
     bool auth_token_generate;
-    unsigned int auth_token_lifetime;
+    bool auth_token_gen_secret_file;
+    bool auth_token_call_auth;
+    int auth_token_lifetime;
     const char *auth_token_secret_file;
     const char *auth_token_secret_file_inline;
 
src/openvpn/ssl_common.h +10 -2
index ff8ffacc..8cdb7dce 100644
@@ -310,6 +310,7 @@ struct tls_options
     bool auth_token_generate;   /**< Generate auth-tokens on successful
                                  * user/pass auth,seet via
                                  * options->auth_token_generate. */
+    bool auth_token_call_auth; /**< always call normal authentication */
     unsigned int auth_token_lifetime;
 
     struct key_ctx auth_token_key;
@@ -469,7 +470,6 @@ struct tls_session
  */
 #define KEY_SCAN_SIZE 3
 
-
 /**
  * Security parameter state for a single VPN tunnel.
  * @ingroup control_processor
@@ -552,9 +552,17 @@ struct tls_multi
     /**< Auth-token sent from client has valid hmac */
 #define  AUTH_TOKEN_EXPIRED              (1<<1)
     /**< Auth-token sent from client has expired */
-#endif
+#define  AUTH_TOKEN_VALID_EMPTYUSER      (1<<2)
+    /**<
+     * Auth-token is only valid for an empty username
+     * and not the username actually supplied from the client
+     *
+     * OpenVPN 3 clients sometimes the empty username with a
+     * username hint from their config.
+     */
     int auth_token_state_flags;
     /**< The state of the auth-token sent from the client last time */
+#endif
 
     /* For P_DATA_V2 */
     uint32_t peer_id;
src/openvpn/ssl_verify.c +46 -21

140 lines not on this page · raw message

Signature · 1 line
2.21.0
#11 lore raw

PATCH v3 6/7 Sent indication that a session is expired to clients

In reply to Arne Schwabe

From: Arne Schwabe <[email protected]...>

This allows OpenVPN 3 core to fall back to the original authentication method.

This commit changes man_def_auth_set_client_reason to auth_set_client_reason since it now used in more contexts.

Also remove a FIXME about client_reason not being freed, as it is freed in tls_multi_free with auth_set_client_reason(multi, NULL);

---
 src/openvpn/auth_token.c |  3 +++
 src/openvpn/ssl.c        |  6 ++----
 src/openvpn/ssl_common.h | 10 +++++-----
 src/openvpn/ssl_verify.c |  8 ++++----
 src/openvpn/ssl_verify.h | 15 ++++++++++-----
 5 files changed, 24 insertions(+), 18 deletions(-)
src/openvpn/auth_token.c +3 -0
index f28008d3..63ec3145 100644
@@ -15,6 +15,7 @@
 #include "push.h"
 #include "integer.h"
 #include "ssl.h"
+#include "ssl_verify.h"
 
 const char *auth_token_pem_name = "OpenVPN auth-token server key";
 
@@ -357,6 +358,8 @@ verify_auth_token(struct user_pass *up, struct tls_multi *multi,
 
     if (ret & AUTH_TOKEN_EXPIRED)
     {
+        /* Tell client that the session token is expired */
+        auth_set_client_reason(multi, "SESSION: token expired");
         msg(M_INFO, "--auth-token-gen: auth-token from client expired");
     }
     return ret;
src/openvpn/ssl.c +2 -4
index 69d7be57..c615a6d8 100644
@@ -1349,11 +1349,9 @@ tls_multi_free(struct tls_multi *multi, bool clear)
 
     ASSERT(multi);
 
-#ifdef MANAGEMENT_DEF_AUTH
-    man_def_auth_set_client_reason(multi, NULL);
-
-#endif
 #if P2MP_SERVER
+    auth_set_client_reason(multi, NULL);
+
     free(multi->peer_info);
 #endif
 
src/openvpn/ssl_common.h +5 -5
index 8cdb7dce..df320abb 100644
@@ -525,16 +525,16 @@ struct tls_multi
     struct cert_hash_set *locked_cert_hash_set;
 
 #ifdef ENABLE_DEF_AUTH
-    /*
-     * An error message to send to client on AUTH_FAILED
-     */
-    char *client_reason;
-
     /* Time of last call to tls_authentication_status */
     time_t tas_last;
 #endif
 
 #if P2MP_SERVER
+    /*
+     * An error message to send to client on AUTH_FAILED
+     */
+    char *client_reason;
+
     /*
      * A multi-line string of general-purpose info received from peer
      * over control channel.
src/openvpn/ssl_verify.c +4 -4
index 6535ad8d..f61c621b 100644
@@ -803,9 +803,8 @@ cleanup:
 #define ACF_FAILED    3
 #endif
 
-#ifdef MANAGEMENT_DEF_AUTH
 void
-man_def_auth_set_client_reason(struct tls_multi *multi, const char *client_reason)
+auth_set_client_reason(struct tls_multi* multi, const char* client_reason)
 {
     if (multi->client_reason)
     {
@@ -814,11 +813,12 @@ man_def_auth_set_client_reason(struct tls_multi *multi, const char *client_reaso
     }
     if (client_reason && strlen(client_reason))
     {
-        /* FIXME: Last alloc will never be freed */
         multi->client_reason = string_alloc(client_reason, NULL);
     }
 }
 
+#ifdef MANAGEMENT_DEF_AUTH
+
 static inline unsigned int
 man_def_auth_test(const struct key_state *ks)
 {
@@ -1022,7 +1022,7 @@ tls_authenticate_key(struct tls_multi *multi, const unsigned int mda_key_id, con
     if (multi)
     {
         int i;
-        man_def_auth_set_client_reason(multi, client_reason);
+        auth_set_client_reason(multi, client_reason);
         for (i = 0; i < KEY_SCAN_SIZE; ++i)
         {
             struct key_state *ks = multi->key_scan[i];
src/openvpn/ssl_verify.h +10 -5
index 64f27efb..c54b89a6 100644
@@ -224,18 +224,23 @@ struct x509_track
 #ifdef MANAGEMENT_DEF_AUTH
 bool tls_authenticate_key(struct tls_multi *multi, const unsigned int mda_key_id, const bool auth, const char *client_reason);
 
-void man_def_auth_set_client_reason(struct tls_multi *multi, const char *client_reason);
+#endif
 
+#ifdef P2MP_SERVER
+/**
+ * Sets the reason why authentication of a client failed. This be will send to the client
+ * when the AUTH_FAILED message is sent
+ * An example would be "SESSION: Token expired"
+ * @param multi             The multi tls struct
+ * @param client_reason     The string to send to the client as part of AUTH_FAILED
+ */
+void auth_set_client_reason(struct tls_multi* multi, const char* client_reason);
 #endif
 
 static inline const char *
 tls_client_reason(struct tls_multi *multi)
 {
-#ifdef ENABLE_DEF_AUTH
     return multi->client_reason;
-#else
-    return NULL;
-#endif
 }
 
 /** Remove any X509_ env variables from env_set es */
Signature · 1 line
2.21.0
#12 lore raw

PATCH v3 7/7 Implement unit tests for auth-gen-token

In reply to Arne Schwabe

From: Arne Schwabe <[email protected]...>

Patch V2: adapt unit tests to other V2 patches

---
 tests/unit_tests/openvpn/Makefile.am       |  18 +-
 tests/unit_tests/openvpn/test_auth_token.c | 375 +++++++++++++++++++++
 2 files changed, 392 insertions(+), 1 deletion(-)
 create mode 100644 tests/unit_tests/openvpn/test_auth_token.c
tests/unit_tests/openvpn/Makefile.am +17 -1
index 657957e5..cf55bfaf 100644
@@ -6,7 +6,7 @@ if HAVE_LD_WRAP_SUPPORT
 check_PROGRAMS += argv_testdriver buffer_testdriver
 endif
 
-check_PROGRAMS += crypto_testdriver packet_id_testdriver
+check_PROGRAMS += crypto_testdriver packet_id_testdriver auth_token_testdriver
 if HAVE_LD_WRAP_SUPPORT
 check_PROGRAMS += tls_crypt_testdriver
 endif
@@ -72,3 +72,19 @@ tls_crypt_testdriver_SOURCES = test_tls_crypt.c mock_msg.c mock_msg.h \
 	$(openvpn_srcdir)/packet_id.c \
 	$(openvpn_srcdir)/platform.c \
 	$(openvpn_srcdir)/run_command.c
+
+auth_token_testdriver_CFLAGS  = @TEST_CFLAGS@ \
+	-I$(openvpn_includedir) -I$(compat_srcdir) -I$(openvpn_srcdir) \
+	$(OPTIONAL_CRYPTO_CFLAGS)
+auth_token_testdriver_LDFLAGS = @TEST_LDFLAGS@ \
+	$(OPTIONAL_CRYPTO_LIBS)
+
+auth_token_testdriver_SOURCES = test_auth_token.c mock_msg.c \
+	$(openvpn_srcdir)/buffer.c \
+	$(openvpn_srcdir)/crypto.c \
+	$(openvpn_srcdir)/crypto_mbedtls.c \
+	$(openvpn_srcdir)/crypto_openssl.c \
+	$(openvpn_srcdir)/otime.c \
+	$(openvpn_srcdir)/packet_id.c \
+	$(openvpn_srcdir)/platform.c \
+	$(openvpn_srcdir)/base64.c
tests/unit_tests/openvpn/test_auth_token.c new file +375 -0

375 lines not on this page · raw message

Signature · 1 line
2.21.0

A message that is not in the archive (sent off-list or not yet received): [email protected]...

#14 lore raw

Am 10.05.19 um 17:14 schrieb Jan Just Keijser:

Quoted 19 lines

Hi Arne,

Quoted 6 lines · Arne Schwabe

This is the v3 of the patch series. I choose to resend all of the patches so they all cleanly apply. Since the patches depend on the changes suggested
to --genkey, I made them part of the patch series. The other patches have been updated to incoperate the feedback.

Quoted 10 lines

Arne Schwabe (7):
   Write key to stdout if filename is not given Implement --genkey type keyfile syntax and migrate tls-crypt-v2 Add pem_read_key_file variant that allows a random key Rewrite auth-token-gen to be based on HMAC based tokens Implement a permanent session id in auth-token
   Sent indication that a session is expired to clients Implement unit tests for auth-gen-token

Quoted 4 lines

I 've been busy with the --genkey patch and was wondering how/when to include any auth-token stuff; should I wait for this patch to be ACKed and then do a git clone again?  or can I include your patch beforehand (if so, which git magic is needed for that?)

Sorry that, I missed from your last mail that you also wanted to prepare a patch and basically implemented what I think you proposed in your last email to get a an updated patch set of auth-token :(

That was bad coordination from my part. Personally I don't care which way around we do this. Either you send your patch too and I review that one or you review my patch and then eitehr accept it, force my to redo it or declare it so ppor that you have to send your patch anyway.

Arne

#15 lore raw
In reply to Arne Schwabe
Quoted 30 lines · Arne Schwabe

This is the v3 of the patch series. I choose to resend all of the patches so they all cleanly apply. Since the patches depend on the changes suggested to --genkey, I made them part of the patch series. The other patches have been updated to incoperate the feedback.

Arne Schwabe (7):
Write key to stdout if filename is not given Implement --genkey type keyfile syntax and migrate tls-crypt-v2 Add pem_read_key_file variant that allows a random key Rewrite auth-token-gen to be based on HMAC based tokens Implement a permanent session id in auth-token
Sent indication that a session is expired to clients Implement unit tests for auth-gen-token

 doc/openvpn.8                              | 141 ++++++--
 src/openvpn/Makefile.am                    |   1 +
 src/openvpn/auth_token.c                   | 387 +++++++++++++++++++++
 src/openvpn/auth_token.h                   | 129 +++++++
 src/openvpn/crypto.c                       |  35 +-
 src/openvpn/crypto.h                       |  15 +
 src/openvpn/init.c                         |  90 +++--
 src/openvpn/manage.c                       |   4 +-
 src/openvpn/openvpn.h                      |   1 +
 src/openvpn/options.c                      | 103 ++++--
 src/openvpn/options.h                      |  19 +-
 src/openvpn/push.c                         |  70 +++-
 src/openvpn/push.h                         |   8 +
 src/openvpn/ssl.c                          |  13 +-
 src/openvpn/ssl_common.h                   |  56 +--
 src/openvpn/ssl_verify.c                   | 213 ++++++------
Quoted 8 lines
 src/openvpn/ssl_verify.h                   |  15 +-
 src/openvpn/tls_crypt.c                    |  13 +-
 tests/unit_tests/openvpn/Makefile.am       |  18 +-
 tests/unit_tests/openvpn/test_auth_token.c | 375 ++++++++++++++++++++
 20 files changed, 1457 insertions(+), 249 deletions(-)
 create mode 100644 src/openvpn/auth_token.c
 create mode 100644 src/openvpn/auth_token.h
 create mode 100644 tests/unit_tests/openvpn/test_auth_token.c

I've focused on functional testing in the beginning. And here's a summary so far of my feedback:

* The --help screen is inaccurate in regards to --auth-gen-token and --genkey entries.

* Using --genkey with --secret now sends the key to stdout instead of the given --secret file. I don't recall if we discussed this and if this was considered expected.

* When starting a server with --auth-gen-token-secret, there is no (afaict) indications in the log file such a file is used

* In the log file when the server sends PUSH_REPLY there's a formatting issue, where you will find: [...], auth-tokenSESS_ID,[....]. This happens on both server and client.

* The configuration below ends up going into username/password auth loop on each renegotiation after the auth-token has expired:

  - server
    # ./src/openvpn/openvpn --dev tun --ca sample/sample-keys/ca.crt  \
                            --cert sample/sample-keys/server.crt      \
                            --key sample/sample-keys/server.key       \
                            --dh sample/sample-keys/dh2048.pem        \
                            --server 10.8.0.0 255.255.255.0 --verb 4  \
                            --script-security 3                       \
                            --auth-user-pass-verify ./auth.sh via-env \
                            --auth-gen-token 60 external-auth         \
                            --auth-gen-token-secret auth-token.key    \
                            --reneg-sec 30 --tran-window 15           \
                            --hand-window 20 --keepalive 10 20

  - client
    # ./src/openvpn/openvpn --dev tun --client --auth-user-pass       \
                            --remote $REMOTE_IP                       \
                            --ca sample/sample-keys/ca.crt            \
                            --key sample/sample-keys/client.key       \
                            --cert sample/sample-keys/client.crt      \
                            --verb 4 --explicit-exit-notify           \
                            --auth-nocache

- auth.sh script:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#!/bin/bash
echo "----------------------------------------------------" echo "session_state: $session_state"
echo "username: $username"
echo "password: $password"
echo "session_id: $session_id"
ret=1
if [ "$session_state" = "Authenticated" ]; then
ret=0;
elif [ "$username" = "testuser" ]; then
if [ "$password" = "foobaraaa" ]; then

		   ret=0
	   fi

fi

if [ $ret -eq 0 ]; then
echo "Authentication successful"
else
echo "Authentication failed"
fi
echo "----------------------------------------------------" exit $ret
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

What happens:
- Server starts
- Client starts and connects, auth with username/password (state: Initial)
- 30 seconds, reneg happens
- Client re-auth with token (state: Authenticated)
- 30 seconds, reneg happens
- Client re-auth with token (state: Expired)
- 30 seconds, reneg happens
- Client re-auth with token (state: Expired)
- Client restarts with username/password auth (state: Initial)
- 30 seconds, reneg happens
- Client restarts with username/password auth (state: Initial)
- 30 seconds, reneg happens
- Client restarts with username/password auth (state: Initial)
....

I'll run more tests and review patches too, but here's something to dive into at least.

Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc
#16 lore raw
Quoted 6 lines

* The --help screen is inaccurate in regards to --auth-gen-token and --genkey entries.

* Using --genkey with --secret now sends the key to stdout instead of the given --secret file. I don't recall if we discussed this and if this was considered expected.

These two will be fixed in a version of the --genkey patch.

Quoted 3 lines

* When starting a server with --auth-gen-token-secret, there is no (afaict) indications in the log file such a file is used

No. But there is a warning when not using that file and an ephermal key.

Quoted 8 lines

* In the log file when the server sends PUSH_REPLY there's a formatting issue, where you will find: [...], auth-tokenSESS_ID,[....]. This happens on both server and client.

* The configuration below ends up going into username/password auth loop on each renegotiation after the auth-token has expired:

Quoted 10 lines

What happens:
- Server starts
- Client starts and connects, auth with username/password (state: Initial)
- 30 seconds, reneg happens
- Client re-auth with token (state: Authenticated)
- 30 seconds, reneg happens
- Client re-auth with token (state: Expired)
- 30 seconds, reneg happens
- Client re-auth with token (state: Expired)
- Client restarts with username/password auth (state: Initial)

Up to here that is more or less expected behaviour. (The renog failing and connnection continuing to work until renog timeout is reached, is wonky but will/should also happen with other auth methods)

Quoted 2 lines

- 30 seconds, reneg happens
- Client restarts with username/password auth (state: Initial)

So here it looks like the client did not get a new auth-token or ingored it right?

Quoted 4 lines

- 30 seconds, reneg happens
- Client restarts with username/password auth (state: Initial)
....

This might be a problem of auth-nocache on the client side doing strage things. I never had that on.

Arne