9front - general discussion about 9front
 help / color / mirror / Atom feed
From: Anthony Martin <ality@pbrane.org>
To: 9front@9front.org
Subject: [9front] [PATCH] libsec: add minimal support for the tls renegotiation extension
Date: Thu, 10 Nov 2022 02:24:40 +0000	[thread overview]
Message-ID: <87988F72F1C2D20B16DE8DA47FB8C262@alice> (raw)

OpenSSL 3.0 clients refuse to connect to servers that do not
support the renegotiation extension (RFC 5746) unless the default
configuration is changed to allow it. Since we do not support
renegotiation, we only need to make minor changes to the initial
handshake to comply with the specification:

1. For tlsClient, simply add the proper SCSV to the ClientHello
cipher list (cf. RFC 5746 § 3.3);

2. For tlsServer, respond with an empty renegotiation extension
in the ServerHello if we received either the SCSV or an empty
renegotiation extension in the ClientHello.

Since we close the hand file and never open it after the initial
handshake, we can rely on tls(3) to send the "no renegotiation"
alerts if subsequent handshake records are received.

---
diff e5d29a2bd91951a24fccecd958416856cecef444 d75905c2e41425e0add855e77527b016384c46ec
--- a/sys/src/libsec/port/tlshand.c	Tue Nov 8 14:11:29 2022
+++ b/sys/src/libsec/port/tlshand.c	Wed Nov 9 18:24:40 2022
@@ -68,6 +68,7 @@
 	uchar sec[MasterSecretSize];	// master secret
 	uchar srandom[RandomSize];	// server random
 	uchar crandom[RandomSize];	// client random
+	int reneg;			// secure renegotiation flag
 
 	Namedcurve *nc; // selected curve for ECDHE
 	// diffie hellman state
@@ -251,6 +252,7 @@
 	TLS_PSK_WITH_AES_128_CBC_SHA		= 0x008C,
 
 	TLS_FALLBACK_SCSV = 0x5600,
+	TLS_EMPTY_RENEGOTIATION_INFO_SCSV = 0x00FF,
 };
 
 // compression methods
@@ -271,6 +273,7 @@
 	Extec = 0x000a,
 	Extecp = 0x000b,
 	Extsigalgs = 0x000d,
+	Extreneg = 0xff01,
 };
 
 static Algs cipherAlgs[] = {
@@ -670,6 +673,16 @@
 						break;
 					}
 			break;
+		case Extreneg:
+			if(n < 1 || *p != (n -= 1))
+				goto Short;
+			if(*p != 0){
+				tlsError(c, EHandshakeFailure, "invalid renegotiation extension");
+				return -1;
+			}
+			c->sec->reneg = 1;
+			p++;
+
 		}
 	}
 
@@ -679,13 +692,37 @@
 	return -1; 
 } 
 
+static uchar*
+tlsServerExtensions(TlsConnection *c, int *plen)
+{
+	uchar *b, *p;
+	int m;
+
+	p = b = nil;
+
+	// RFC5746 - Renegotiation Indication
+	if(c->sec->reneg){
+		m = p - b;
+		b = erealloc(b, m + 2+2+1);
+		p = b + m;
+
+		put16(p, Extreneg), p += 2;	/* Type: renegotiation_info */
+		put16(p, 1), p += 2;		/* Length */
+		*p++ = 0;			/* Renegotiated Connection Length */
+	}
+
+	*plen = p - b;
+	return b;
+}
+
 static TlsConnection *
 tlsServer2(int ctl, int hand,
 	uchar *cert, int certlen,
 	char *pskid, uchar *psk, int psklen,
 	int (*trace)(char*fmt, ...), PEMChain *chp)
 {
-	int cipher, compressor, numcerts, i;
+	int cipher, compressor, numcerts, i, extlen;
+	uchar *ext;
 	TlsConnection *c;
 	Msg m;
 
@@ -741,8 +778,11 @@
 			goto Err;
 		}
 	}
+	if(lookupid(m.u.clientHello.ciphers, TLS_EMPTY_RENEGOTIATION_INFO_SCSV) >= 0)
+		c->sec->reneg = 1;
 	if(checkClientExtensions(c, m.u.clientHello.extensions) < 0)
 		goto Err;
+	ext = tlsServerExtensions(c, &extlen);
 	cipher = okCipher(m.u.clientHello.ciphers, psklen > 0, c->sec->nc != nil);
 	if(cipher < 0 || !setAlgs(c, cipher)) {
 		tlsError(c, EHandshakeFailure, "no matching cipher suite");
@@ -763,6 +803,7 @@
 	m.u.serverHello.cipher = cipher;
 	m.u.serverHello.compressor = compressor;
 	m.u.serverHello.sid = makebytes(nil, 0);
+	m.u.serverHello.extensions = makebytes(ext, extlen);
 	if(!msgSend(c, &m, AQueue))
 		goto Err;
 
@@ -2273,6 +2314,7 @@
 	for(i = 0; i < nelem(cipherAlgs); i++)
 		if(cipherAlgs[i].ok && isPSK(cipherAlgs[i].tlsid) == ispsk)
 			is->data[j++] = cipherAlgs[i].tlsid;
+	is->data[j++] = TLS_EMPTY_RENEGOTIATION_INFO_SCSV;
 	is->len = j;
 	return is;
 }

             reply	other threads:[~2022-11-10  4:31 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-10  2:24 Anthony Martin [this message]
2023-01-18 15:07 ` [9front] " Anthony Martin
2023-01-19  4:30 ` [9front] " ori
2023-01-19  4:48   ` ori
2022-11-10  2:24     ` Anthony Martin
2023-01-28 21:20       ` ori
2023-01-28 21:59       ` cinap_lenrek
2023-01-19  9:50     ` Anthony Martin
2023-01-20 12:12 ` hiro
2023-01-20 21:05   ` Anthony Martin
2023-01-20 22:33     ` hiro
2023-01-21  3:48       ` Anthony Martin
2023-01-21 12:54         ` hiro
2023-01-21 17:29           ` Steve Simon
2023-01-22 16:00             ` hiro
2023-01-22  7:55           ` Anthony Martin
2023-01-22 16:10             ` hiro
2023-01-23 11:18               ` Anthony Martin
2023-01-23 13:16                 ` hiro
2023-01-23 14:24                   ` Ori Bernstein
2023-01-23 14:29                     ` Ori Bernstein
2023-01-24  0:14                   ` hiro
2023-01-24  0:16                     ` hiro
2023-01-25 16:19                   ` kemal
2023-01-25 16:39                     ` hiro
2023-01-25 17:07                       ` kemal
2023-01-25 17:18                         ` hiro
2023-01-25 17:30                           ` kemal
2023-01-25 17:36                             ` kemal
2023-01-26 20:54                               ` hiro
2023-01-26 21:52                                 ` Frank D. Engel, Jr.
2023-01-27  6:11                                 ` kemal
2023-01-27 10:55                                   ` hiro
2023-01-27 17:38                                     ` kemal
2023-01-23 16:23                 ` hiro

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87988F72F1C2D20B16DE8DA47FB8C262@alice \
    --to=ality@pbrane.org \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).