From: "Bluca (Code Review)" Date: Thu, 9 Apr 2026 01:08:10 +0000 In-Reply-To: References: Reply-To: luca.boccassi@...277..., arne-openvpn@...1227..., selva.nair@...277..., openvpn-devel@lists.sourceforge.net Message-ID: <15f9c2c4b8e808b4c27c4445aebeb0facc44219f-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="2RzlD2QesmQ="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: ssl: use TLS record-sized buffers for key method 2 exchange List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , selvanair Cc: openvpn-devel --2RzlD2QesmQ= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: Bluca, plaisthos, selvanair. Hello plaisthos, selvanair, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/1622?usp=3Demail to look at the new patch set (#3). The following approvals got outdated and were removed: Code-Review-1 by plaisthos Change subject: ssl: use TLS record-sized buffers for key method 2 exchange ...................................................................... ssl: use TLS record-sized buffers for key method 2 exchange The current key exchange uses plaintext_read_buf/plaintext_write_buf (sized at TLS_CHANNEL_BUF_SIZE, 2048 bytes) as intermediary buffers for the key method 2 exchange. Passwords or tokens longer than ~1900 bytes (after accounting for key material, options and peer info overhead) get silently truncated. This breaks using JIT use-once tokens for authentication, which are becoming common in enterprise setups. These tokens are typically long JWT-encoded strings that exceed the 2048-byte buffer. Instead of increasing TLS_CHANNEL_BUF_SIZE (which would change the control channel framing and require both endpoints to be updated), introduce separate key_method_send_buf and key_method_recv_buf buffers in key_state, sized at TLS_RECORD_MAX_SIZE (16384 bytes, the maximum plaintext payload of a single TLS record). On the write side, key_method_2_write() assembles the full payload into the 16 KB buffer, which is then passed to the TLS library via a single key_state_write_plaintext() call. The TLS library creates one TLS record, and the existing write_outgoing_tls_ciphertext() already splits the resulting ciphertext into properly-sized reliable transport packets. On the read side, a single key_state_read_plaintext() call reads into the 16 KB buffer. Since SSL_read/BIO_read returns one TLS record worth of data per call, this preserves the one-read-per- message framing assumption used throughout OpenVPN. All other control channel messages (push, CR_RESPONSE, etc.) continue to use the original 2048-byte plaintext_read/write_buf. When the key exchange payload exceeds TLS_CHANNEL_BUF_SIZE and the connection subsequently fails (state never reaches S_ACTIVE), a diagnostic message is printed at key_state teardown to help the user identify that the server may not support large payloads. Protocol compatibility: - old client <-> new server: no change, old limits apply - new client <-> old server: large passwords will cause the old server to truncate/fail; a clear error message is shown - new client <-> new server: large passwords work Change-Id: I055c64ca8b23066e70eea7d7deddfb14f5354c5f Signed-off-by: Luca Boccassi --- M src/openvpn/common.h M src/openvpn/ssl.c M src/openvpn/ssl_common.h 3 files changed, 88 insertions(+), 18 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/22/1622/3 diff --git a/src/openvpn/common.h b/src/openvpn/common.h index aa7b721..b1a8516 100644 --- a/src/openvpn/common.h +++ b/src/openvpn/common.h @@ -69,6 +69,17 @@ */ #define TLS_CHANNEL_BUF_SIZE 2048 +/* + * Buffer size for key method 2 exchange data (send and receive). + * A single TLS record can carry up to 2^14 (16384) bytes of + * plaintext. We size the key exchange buffers to a full TLS + * record so that payloads larger than TLS_CHANNEL_BUF_SIZE + * (e.g. with long PKCS#11 passwords/tokens) can be sent and + * received in a single TLS read/write without breaking the + * one-read-per-message assumption used elsewhere. + */ +#define TLS_RECORD_MAX_SIZE (1 << 14) + /* TLS control buffer minimum size * * A control frame might have IPv6 header (40 byte), diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c index 576157d..c8bf35c 100644 --- a/src/openvpn/ssl.c +++ b/src/openvpn/ssl.c @@ -860,6 +860,8 @@ ks->plaintext_read_buf =3D alloc_buf(TLS_CHANNEL_BUF_SIZE); ks->plaintext_write_buf =3D alloc_buf(TLS_CHANNEL_BUF_SIZE); ks->ack_write_buf =3D alloc_buf(BUF_SIZE(&session->opt->frame)); + ks->key_method_send_buf =3D alloc_buf(TLS_RECORD_MAX_SIZE); + ks->key_method_recv_buf =3D alloc_buf(TLS_RECORD_MAX_SIZE); reliable_init(ks->send_reliable, BUF_SIZE(&session->opt->frame), session->opt->frame.buf.headroom, TLS_RELIABLE_N_SEND_BU= FFERS, ks->key_id ? false : session->opt->xmit_hold); @@ -906,6 +908,17 @@ static void key_state_free(struct key_state *ks, bool clear) { + if (ks->key_method_large_payload && ks->state >=3D S_SENT_KEY + && ks->state < S_ACTIVE) + { + /* Servers before a7f80d402f do not send back a useful error so pr= int one */ + msg(M_WARN, "Connection failed after sending a key exchange " + "payload larger than %d bytes (due to a long password). " + "This may happen when the server does not support large " + "key exchange payloads, try with a smaller password.", + TLS_CHANNEL_BUF_SIZE); + } + ks->state =3D S_UNDEF; key_state_ssl_free(&ks->ks_ssl); @@ -915,6 +928,8 @@ free_buf(&ks->plaintext_read_buf); free_buf(&ks->plaintext_write_buf); free_buf(&ks->ack_write_buf); + free_buf(&ks->key_method_send_buf); + free_buf(&ks->key_method_recv_buf); buffer_list_free(ks->paybuf); reliable_free(ks->send_reliable); @@ -2836,38 +2851,61 @@ } /* Read incoming plaintext from TLS object */ - struct buffer *buf =3D &ks->plaintext_read_buf; - if (!buf->len) + bool in_key_exchange_read =3D + (ks->state =3D=3D S_SENT_KEY && !session->opt->server) + || (ks->state =3D=3D S_START && session->opt->server); + + if (in_key_exchange_read) { - if (!read_incoming_tls_plaintext(ks, buf, wakeup, &continue_tls_pr= ocess)) + /* For key exchange, read into a buffer large enough for a full + * TLS record (16 KB) so that long passwords/tokens that exceed + * TLS_CHANNEL_BUF_SIZE are received in a single read. */ + if (!BLEN(&ks->key_method_recv_buf)) { - goto error; + if (!read_incoming_tls_plaintext(ks, &ks->key_method_recv_buf, + wakeup, &continue_tls_process= )) + { + goto error; + } + } + } + else + { + struct buffer *buf =3D &ks->plaintext_read_buf; + if (!buf->len) + { + if (!read_incoming_tls_plaintext(ks, buf, wakeup, &continue_tl= s_process)) + { + goto error; + } } } - /* Send Key */ - buf =3D &ks->plaintext_write_buf; - if (!buf->len + /* Send Key -- use larger key_method_send_buf for assembly to support + * long passwords/tokens that exceed TLS_CHANNEL_BUF_SIZE */ + if (!ks->key_method_send_buf.len && ((ks->state =3D=3D S_START && !session->opt->server) || (ks->state =3D=3D S_GOT_KEY && session->opt->server))) { - if (!key_method_2_write(buf, multi, session)) + if (!key_method_2_write(&ks->key_method_send_buf, multi, session)) { goto error; } + ks->key_method_large_payload =3D + BLEN(&ks->key_method_send_buf) > TLS_CHANNEL_BUF_SIZE; + continue_tls_process =3D true; dmsg(D_TLS_DEBUG_MED, "STATE S_SENT_KEY"); ks->state =3D S_SENT_KEY; } - /* Receive Key */ - buf =3D &ks->plaintext_read_buf; - if (buf->len + /* Receive Key -- use the larger key_method_recv_buf */ + if (BLEN(&ks->key_method_recv_buf) && ((ks->state =3D=3D S_SENT_KEY && !session->opt->server) || (ks->state =3D=3D S_START && session->opt->server))) { - if (!key_method_2_read(buf, multi, session)) + if (!key_method_2_read(&ks->key_method_recv_buf, multi, session)) { goto error; } @@ -2877,20 +2915,38 @@ ks->state =3D S_GOT_KEY; } - /* Write outgoing plaintext to TLS object */ - buf =3D &ks->plaintext_write_buf; - if (buf->len) + /* Write key exchange data to TLS object */ + if (ks->key_method_send_buf.len) { - int status =3D key_state_write_plaintext(&ks->ks_ssl, buf); + int status =3D key_state_write_plaintext(&ks->ks_ssl, &ks->key_met= hod_send_buf); if (status =3D=3D -1) { - msg(D_TLS_ERRORS, "TLS ERROR: Outgoing Plaintext -> TLS object= write error"); + msg(D_TLS_ERRORS, "TLS ERROR: Key Method -> TLS object write e= rror"); goto error; } if (status =3D=3D 1) { continue_tls_process =3D true; - dmsg(D_TLS_DEBUG, "Outgoing Plaintext -> TLS"); + dmsg(D_TLS_DEBUG, "Key Method -> TLS"); + } + } + + /* Write outgoing plaintext to TLS object */ + { + struct buffer *buf =3D &ks->plaintext_write_buf; + if (buf->len) + { + int status =3D key_state_write_plaintext(&ks->ks_ssl, buf); + if (status =3D=3D -1) + { + msg(D_TLS_ERRORS, "TLS ERROR: Outgoing Plaintext -> TLS ob= ject write error"); + goto error; + } + if (status =3D=3D 1) + { + continue_tls_process =3D true; + dmsg(D_TLS_DEBUG, "Outgoing Plaintext -> TLS"); + } } } if (!check_outgoing_ciphertext(ks, session, &continue_tls_process)) diff --git a/src/openvpn/ssl_common.h b/src/openvpn/ssl_common.h index 6f310a5..913a0c2 100644 --- a/src/openvpn/ssl_common.h +++ b/src/openvpn/ssl_common.h @@ -241,6 +241,9 @@ struct buffer plaintext_read_buf; struct buffer plaintext_write_buf; struct buffer ack_write_buf; + struct buffer key_method_send_buf; /* larger buffer for key method 2 w= rite */ + struct buffer key_method_recv_buf; /* larger buffer for key method 2 r= ead */ + bool key_method_large_payload; /* key exchange exceeded TLS_CHANNE= L_BUF_SIZE */ struct reliable *send_reliable; /* holds a copy of outgoing packets un= til ACK received */ struct reliable *rec_reliable; /* order incoming ciphertext packets b= efore we pass to TLS */ -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1622?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newpatchset Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I055c64ca8b23066e70eea7d7deddfb14f5354c5f Gerrit-Change-Number: 1622 Gerrit-PatchSet: 3 Gerrit-Owner: Bluca Gerrit-Reviewer: plaisthos Gerrit-Reviewer: selvanair Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: Bluca Gerrit-Attention: selvanair --2RzlD2QesmQ= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is cur= rently required from: Bluca, plaisthos, selvanair.

Bluca uploaded patch set #3 to this change.

View Change

The following approvals got outdated and were removed: Code-Review-1 by plaisthos

ssl: use TLS record-sized buffe=
rs for key method 2 exchange

The current key exchange uses plaintext= _read_buf/plaintext_write_buf
(sized at TLS_CHANNEL_BUF_SIZE, 2048 bytes= ) as intermediary buffers
for the key method 2 exchange. Passwords or to= kens longer than
~1900 bytes (after accounting for key material, options= and peer
info overhead) get silently truncated.

This breaks usin= g JIT use-once tokens for authentication, which are
becoming common in e= nterprise setups. These tokens are typically
long JWT-encoded strings th= at exceed the 2048-byte buffer.

Instead of increasing TLS_CHANNEL_BU= F_SIZE (which would change the
control channel framing and require both = endpoints to be updated),
introduce separate key_method_send_buf and key= _method_recv_buf
buffers in key_state, sized at TLS_RECORD_MAX_SIZE (163= 84 bytes,
the maximum plaintext payload of a single TLS record).

= On the write side, key_method_2_write() assembles the full payload
into = the 16 KB buffer, which is then passed to the TLS library via
a single k= ey_state_write_plaintext() call. The TLS library creates
one TLS record,= and the existing write_outgoing_tls_ciphertext()
already splits the res= ulting ciphertext into properly-sized reliable
transport packets.
On the read side, a single key_state_read_plaintext() call reads
into t= he 16 KB buffer. Since SSL_read/BIO_read returns one TLS
record worth of= data per call, this preserves the one-read-per-
message framing assumpt= ion used throughout OpenVPN.

All other control channel messages (pus= h, CR_RESPONSE, etc.)
continue to use the original 2048-byte plaintext_r= ead/write_buf.

When the key exchange payload exceeds TLS_CHANNEL_BUF= _SIZE and the
connection subsequently fails (state never reaches S_ACTIV= E), a
diagnostic message is printed at key_state teardown to help theuser identify that the server may not support large payloads.

Proto= col compatibility:
- old client <-> new server: no change, old lim= its apply
- new client <-> old server: large passwords will cause = the old
server to truncate/fail; a clear error message is shown
- n= ew client <-> new server: large passwords work

Change-Id: I055= c64ca8b23066e70eea7d7deddfb14f5354c5f
Signed-off-by: Luca Boccassi <l= uca.boccassi@...277...>
---
M src/openvpn/common.h
M src/openvp= n/ssl.c
M src/openvpn/ssl_common.h
3 files changed, 88 insertions(+),= 18 deletions(-)

git pull ssh://gerrit.openvpn.n=
et:29418/openvpn refs/changes/22/1622/3
diff --git a/src/openvpn/co=
mmon.h b/src/openvpn/common.h
index aa7b721..b1a8516 100644=
--- a/src/openvpn/common.h
+++ b/src/openv= pn/common.h
@@ -69,6 +69,17 @@
*/<= br> #define TLS_CHANNEL_BUF_SIZE 2048

+/*
+ * Buffer size for key method 2 exchange data (sen= d and receive).
+ * A= single TLS record can carry up to 2^14 (16384) bytes of
+ * plaintext. We size the key exchange= buffers to a full TLS
+ * (e.g. with long PKCS#11 passw= ords/tokens) can be sent and
+ * received in a single TLS read/write without breaking the=
+ * one-read-per-message as= sumption used elsewhere.
+ */
+#define TLS= _RECORD_MAX_SIZE (1 << 14)
+
/* TLS control buffer minimum size *
* A control frame might have IPv6 header (40 b= yte),
diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c
index 576157d..c8bf35c 100644
--- a/src/openv= pn/ssl.c
+++ b/src/openvpn/ssl.c
@@ -860,6 = +860,8 @@
ks->plaintext_read_buf =3D alloc_buf(TLS_= CHANNEL_BUF_SIZE);
ks->plaintext_write_buf =3D allo= c_buf(TLS_CHANNEL_BUF_SIZE);
ks->ack_write_buf =3D = alloc_buf(BUF_SIZE(&session->opt->frame));
+ ks->key_method_send_buf =3D alloc_= buf(TLS_RECORD_MAX_SIZE);
+ ks->key_method_recv_buf =3D alloc_buf(TLS_RECORD_MAX_SIZE);
reliable_init(ks->send_reliable, BUF_SIZE(&sessio= n->opt->frame),
session->opt-&g= t;frame.buf.headroom, TLS_RELIABLE_N_SEND_BUFFERS,
= ks->key_id ? false : session->opt->xmit_hold);@@ -906,6 +908,17 @@
static void
= key_state_free(struct key_state *ks, bool clear)
{<= br>+ if (ks->key_method_l= arge_payload && ks->state >=3D S_SENT_KEY
+ && ks->state < S_= ACTIVE)
+ {=
+ /* Servers before = a7f80d402f do not send back a useful error so print one */
+ msg(M_WARN, "Connection= failed after sending a key exchange "
+ "payload larger than %d bytes (due= to a long password). "
+ "This may happen when the server does not support= large "
+ = "key exchange payloads, try with a smaller password.",
+ TLS_CHANNEL_= BUF_SIZE);
+ }

+
k= s->state =3D S_UNDEF;

key_state_s= sl_free(&ks->ks_ssl);
@@ -915,6 +928,8 @@
= free_buf(&ks->plaintext_read_buf);
f= ree_buf(&ks->plaintext_write_buf);
free_buf(&am= p;ks->ack_write_buf);
+ free_buf(&ks->key_method_send_buf);
+ free_buf(&ks->key_method_recv_= buf);
buffer_list_free(ks->paybuf);

reliable_free(ks->send_reliable);
@@ -2836,38 +2851,61 @@
}
/* Read incoming plaintext from TLS object */
- struct buffer *buf =3D &ks->= ;plaintext_read_buf;
- = if (!buf->len)
= + bool in_key_exchange_read =3D
+ (ks->state =3D=3D S_SENT_KEY && !session-= >opt->server)
+= || (ks->state =3D=3D S_START && session->opt->serv= er);
+
+ if (in_key_exchange_read)
{
- = if (!read_incoming_tls_plaintext(ks, buf, wakeup, &continue_tls_pr= ocess))
+ /* F= or key exchange, read into a buffer large enough for a full
+ * TLS record (16 KB) so th= at long passwords/tokens that exceed
+ * TLS_CHANNEL_BUF_SIZE are received in a single r= ead. */
+ if (= !BLEN(&ks->key_method_recv_buf))
{- goto error;=
+ if (!read_inco= ming_tls_plaintext(ks, &ks->key_method_recv_buf,
+ = wakeup, &continue_tls_process))
+ {
+ goto error;
+ }
+ }
+ }
+ el= se
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ struct buffer *buf =3D = &ks->plaintext_read_buf;
+ if (!buf->len)
+ {
+ if (!read_incoming_tls_plaintext(ks, buf, wakeup, &= continue_tls_process))
+ = goto error;
+ }
}
}=

- /* Sen= d Key */
- buf =3D &= amp;ks->plaintext_write_buf;
- if (!buf->len
+ /* Send Key -- use larger key_method_send_buf for assembly t= o support
+ * lon= g passwords/tokens that exceed TLS_CHANNEL_BUF_SIZE */
+ if (!ks->key_method_send_buf.len<= /span>
&& ((ks->state =3D=3D S_START &&= ; !session->opt->server)
|| (ks->stat= e =3D=3D S_GOT_KEY && session->opt->server)))
{

- if (!k= ey_method_2_write(buf, multi, session))
+ if (!key_method_2_write(&ks->key_method_= send_buf, multi, session))
{
= goto error;
}

= + ks->key_method_larg= e_payload =3D
+ = BLEN(&ks->key_method_send_buf) > TLS_CHANNEL_BUF_SIZE;

+
= continue_tls_process =3D true;
dmsg(D_TLS_DEBUG_= MED, "STATE S_SENT_KEY");
ks->state = =3D S_SENT_KEY;
}

- /* Receive Key */
- buf =3D &ks->plaintext_read_b= uf;
- if (buf->le= n
+ /* Receive Key= -- use the larger key_method_recv_buf */
+ if (BLEN(&ks->key_method_recv_buf)<= br> && ((ks->state =3D=3D S_SENT_KEY && !s= ession->opt->server)
|| (ks->state = =3D=3D S_START && session->opt->server)))
= {
- if (!key_m= ethod_2_read(buf, multi, session))
+ if (!key_method_2_read(&ks->key_method_recv_b= uf, multi, session))
{
= goto error;
}
@@ -2877,20 +2915,= 38 @@
ks->state =3D S_GOT_KEY;
= }

- buf =3D &ks->plaintext_write_buf= ;
- if (buf->len)=
+ /* Write key ex= change data to TLS object */
+ if (ks->key_method_send_buf.len)
{
- int status =3D ke= y_state_write_plaintext(&ks->ks_ssl, buf);
+ int status =3D key_state_write_plaint= ext(&ks->ks_ssl, &ks->key_method_send_buf);
= if (status =3D=3D -1)
{
- msg(D_TLS_ERRORS, "TLS = ERROR: Outgoing Plaintext -> TLS object write error");
+ msg(D_TLS_ERRORS, &q= uot;TLS ERROR: Key Method -> TLS object write error");

goto error;
}
= if (status =3D=3D 1)
{
= continue_tls_process =3D true;
- dmsg(D_TLS_DEBUG, "Outgoing Plaintext -&g= t; TLS");
+ = dmsg(D_TLS_DEBUG, "Key Method -> TLS");
+ }
+ }
+
+ = /* Write outgoing plaintext to TLS object */
+ {
+ struct buffer *buf =3D &ks->plaintext_write_buf;=
+ if (buf->= ;len)
+ {
+ int status = =3D key_state_write_plaintext(&ks->ks_ssl, buf);
+ if (status =3D=3D -1)

+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ msg(D_TLS_ERROR= S, "TLS ERROR: Outgoing Plaintext -> TLS object write error");=
+ got= o error;
+ = }
+ if (s= tatus =3D=3D 1)
+ = {
+ = continue_tls_process =3D true;
+ dmsg(D_TLS_DEBUG, "Outgoing Plaintext= -> TLS");
+ = }
}
}
if (!check_outgoing_ciphertext(ks, session, &continue_tls_proc= ess))
diff --git a/src/openvpn/ssl_common.h b/src/openvpn/s= sl_common.h
index 6f310a5..913a0c2 100644
-= -- a/src/openvpn/ssl_common.h
+++ b/src/openvpn/ssl_common.= h
@@ -241,6 +241,9 @@
struct buffer pl= aintext_read_buf;
struct buffer plaintext_write_buf;
struct buffer ack_write_buf;
+ struct buffer key_method_send_buf; /* larg= er buffer for key method 2 write */
+ struct buffer key_method_recv_buf; /* larger buffer for= key method 2 read */
+ bool key_method_large_payload; /* key exchange exceeded TLS_CHANN= EL_BUF_SIZE */

struct reliable *send= _reliable; /* holds a copy of outgoing packets until ACK received */=
struct reliable *rec_reliable; /* order incoming ciphertext= packets before we pass to TLS */

To vi= ew, visit change 1622. To unsubscribe, or for help writing mail filters, visit = settings.

=
Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I055c64ca8b23066e70eea7d7ded= dfb14f5354c5f
Gerrit-Change-Number: 1622
Gerrit-PatchSet: 3
Gerrit-Owner: Bluca <luca.boccassi@...1663...= .>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-Reviewer: selvanair <selva.nair@...1665...= 77...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: Bluca <luca.boccassi@...1665...= 77...>
Gerrit-Attention: selvanair <selva.nair@...493...= 277...>
= --2RzlD2QesmQ=--