Plan 9 from Bell Labs’s /sources/contrib/mospak/tls-1.2/tls-aead-cipher-preference.diff

Copyright © 2026 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


--- sys/src/libsec/port/tlshand.c
+++ sys/src/libsec/port/tlshand.c
@@ -88,6 +88,7 @@ typedef struct TlsConnection{
 	// for finished messages
 	HandHash	hs;	// handshake hash
 	Finished	finished;
+	int	serverSignFlags;	/* AlgRsaSign|AlgEcSign; gates okCipher */
 	int	serverCurve;	/* negotiated ECDHE group, 0 = none */
 } TlsConnection;
 
@@ -318,12 +319,18 @@ static Algs cipherAlgs[] = {
 	 * given SKE-sig verification */
 	{"aes_128_gcm_aead", "clear", 2*(16+4), TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, AlgRsaSign},
 	{"ccpoly96_aead", "clear", 2*(32+12), TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, AlgRsaSign},
-	/* legacy RSA-KE suites (no forward secrecy) */
-	{"rc4_128", "md5", 2*(16+MD5dlen), TLS_RSA_WITH_RC4_128_MD5, AlgRsaSign},
-	{"rc4_128", "sha1", 2*(16+SHA1dlen), TLS_RSA_WITH_RC4_128_SHA, AlgRsaSign},
-	{"3des_ede_cbc", "sha1", 2*(4*8+SHA1dlen), TLS_RSA_WITH_3DES_EDE_CBC_SHA, AlgRsaSign},
+	/*
+	 * Legacy RSA-KE suites (no forward secrecy), strongest first.  The
+	 * table is walked in order for both roles, so the top entry is what
+	 * a peer with no shared curve gets.  The two RC4 suites are absent:
+	 * RFC 7465 Section 2 forbids a client from offering them and a
+	 * server from selecting them, and this table feeds both.  The
+	 * rc4_128 encryption algorithm itself stays in libsec and in the
+	 * kernel for other callers.
+	 */
+	{"aes_256_cbc", "sha1", 2*(32+16+SHA1dlen), TLS_RSA_WITH_AES_256_CBC_SHA, AlgRsaSign},
 	{"aes_128_cbc", "sha1", 2*(16+16+SHA1dlen), TLS_RSA_WITH_AES_128_CBC_SHA, AlgRsaSign},
-	{"aes_256_cbc", "sha1", 2*(32+16+SHA1dlen), TLS_RSA_WITH_AES_256_CBC_SHA, AlgRsaSign}
+	{"3des_ede_cbc", "sha1", 2*(4*8+SHA1dlen), TLS_RSA_WITH_3DES_EDE_CBC_SHA, AlgRsaSign}
 };
 
 static uchar compressors[] = {
@@ -426,6 +433,7 @@ static int	isECDHE(int tlsid);
 static void freeints(Ints* b);
 
 static int	isECDHE(int tlsid);
+static int	isTLS12Only(int tlsid);
 static Bytes*	findExtension(Bytes *ext, int type);
 static Bytes*	clientHelloExtensions(char *serverName);
 static uchar*	tlsSecECDHEc(TlsSec *sec, Bytes *Ys, int *npub);
@@ -597,6 +605,32 @@ tlsServer2(int ctl, int hand, uchar *cert, int ncert, 
 	}
 
 	memmove(c->crandom, m.u.clientHello.random, RandomSize);
+	/*
+	 * Derive c->serverSignFlags from the server's cert SPKI key type
+	 * (RFC 4492 Section 2.2): RSA cert -> AlgRsaSign, ECDSA cert ->
+	 * AlgEcSign.  okCipher() consults this to skip cipher suites
+	 * whose authentication algorithm doesn't match the loaded cert;
+	 * without the gate, an RSA-cert server can pick TLS_ECDHE_ECDSA_*
+	 * and emit a (cipher, sigalg) pair that strict TLS 1.2 clients
+	 * will reject.
+	 */
+	{
+		RSApub *rpk;
+		ECpub *epk;
+		ECdomain dom;
+
+		rpk = X509toRSApub(cert, ncert, nil, 0);
+		if(rpk != nil){
+			c->serverSignFlags |= AlgRsaSign;
+			rsapubfree(rpk);
+		}
+		epk = X509toECpub(cert, ncert, nil, 0, &dom);
+		if(epk != nil){
+			c->serverSignFlags |= AlgEcSign;
+			ecpubfree(epk);
+			ecdomfree(&dom);
+		}
+	}
 	/*
 	 * Walk client's supported_groups in preference order and pick the first
 	 * curve we also implement (RFC 8422 Section 5.1.1, RFC 7919 Section 4).
@@ -1961,6 +1995,15 @@ setAlgs(TlsConnection *c, int a)
 {
 	int i;
 
+	/*
+	 * RFC 5288 Section 3 and RFC 7905 Section 2: an AEAD suite is
+	 * defined only against the TLS 1.2 record layer.  setVersion
+	 * has committed c->version before either caller reaches here,
+	 * so a peer answering with one under TLS 1.0 or 1.1 is refused
+	 * rather than negotiated.
+	 */
+	if(c->version < TLS12Version && isTLS12Only(a))
+		return 0;
 	for(i = 0; i < nelem(cipherAlgs); i++){
 		if(cipherAlgs[i].tlsid == a){
 			c->enc = cipherAlgs[i].enc;
@@ -1980,6 +2023,7 @@ okCipher(TlsConnection *c, Ints *cv)
 {
 	int weak, i, j, id;
 
+	/* track if client offered only weak ciphers */
 	weak = 1;
 	for(i = 0; i < cv->len; i++) {
 		id = cv->data[i];
@@ -1987,10 +2031,39 @@ okCipher(TlsConnection *c, Ints *cv)
 			weak = 0;
 		else
 			weak &= weakCipher[id];
-		for(j = 0; j < nelem(cipherAlgs); j++)
-			if(cipherAlgs[j].ok && cipherAlgs[j].tlsid == id)
-				return id;
 	}
+	/* server preference: iterate cipherAlgs[] in our preferred
+	 * order, pick first that the client also offered.  AEAD
+	 * entries are listed first in cipherAlgs[] so AEAD wins
+	 * over CBC when both are offered. */
+	for(j = 0; j < nelem(cipherAlgs); j++) {
+		if(!cipherAlgs[j].ok)
+			continue;
+		/*
+		 * RFC 5288 Section 3 and RFC 7905 Section 2: the AEAD
+		 * suites must not be selected under a version older than
+		 * TLS 1.2.  Skipping them here leaves the CBC suites
+		 * lower in the table reachable by such a client.
+		 */
+		if(c != nil && c->version < TLS12Version
+		&& isTLS12Only(cipherAlgs[j].tlsid))
+			continue;
+		/* RFC 8422 Section 5.1.1 + RFC 7919 Section 4: skip ECDHE
+		 * suites when the client and we share no named curve. */
+		if(c != nil && isECDHE(cipherAlgs[j].tlsid) && c->serverCurve == 0)
+			continue;
+		/* RFC 4492 Section 2.2: cipher's authentication algorithm
+		 * must match the server cert's key type.  Skip
+		 * TLS_ECDHE_ECDSA_* against an RSA cert and TLS_ECDHE_RSA_*
+		 * / TLS_RSA_* against an ECDSA-only cert.
+		 */
+		if(c != nil && cipherAlgs[j].flags != 0
+		&& (cipherAlgs[j].flags & c->serverSignFlags) == 0)
+			continue;
+		for(i = 0; i < cv->len; i++)
+			if(cv->data[i] == cipherAlgs[j].tlsid)
+				return cipherAlgs[j].tlsid;
+	}
 	if(weak)
 		return -2;
 	return -1;
@@ -2400,6 +2473,24 @@ isECDHE(int tlsid)
 
 static int
 isECDHE(int tlsid)
+{
+	switch(tlsid){
+	case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
+	case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
+	case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
+	case TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256:
+		return 1;
+	}
+	return 0;
+}
+
+/*
+ * Suites whose RFC defines them only against the TLS 1.2 record layer:
+ * the AEAD nonce and additional data come from TLS 1.2 and have no
+ * meaning under TLS 1.0 or 1.1.
+ */
+static int
+isTLS12Only(int tlsid)
 {
 	switch(tlsid){
 	case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2026 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.