Plan 9 from Bell Labs’s /usr/web/sources/contrib/mospak/tls-1.2/tls-sendbuf-per-connection.diff

Copyright © 2021 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
@@ -77,6 +77,10 @@ typedef struct TlsConnection{
 	// input buffer for handshake messages
 	uchar buf[MaxChunk+8*1024];
 	uchar *rp, *ep;
+
+	// output buffer for handshake messages
+	uchar sendbuf[9000];
+	uchar *sendp;
 
 	uchar crandom[RandomSize];	// client random
 	uchar srandom[RandomSize];	// server random
@@ -1389,8 +1393,6 @@ Err:
 
 
 //================= message functions ========================
-
-static uchar sendbuf[9000], *sendp;
 
 static void
 msgHash(TlsConnection *c, uchar *p, int n)
@@ -1406,18 +1408,18 @@ msgSend(TlsConnection *c, Msg *m, int act)
 static int
 msgSend(TlsConnection *c, Msg *m, int act)
 {
-	uchar *p; // sendp = start of new message;  p = write pointer
+	uchar *p; // c->sendp = start of new message;  p = write pointer
 	int nn, n, i;
 
-	if(sendp == nil)
-		sendp = sendbuf;
-	p = sendp;
-	if(p + 4 - sendbuf > sizeof(sendbuf)) {
+	if(c->sendp == nil)
+		c->sendp = c->sendbuf;
+	p = c->sendp;
+	if(p + 4 - c->sendbuf > sizeof(c->sendbuf)) {
 		tlsError(c, EInternalError, "output buffer too small for handshake header");
 		goto Err;
 	}
 	if(c->trace)
-		c->trace("send %s", msgPrint((char*)p, (sizeof sendbuf) - (p-sendbuf), m));
+		c->trace("send %s", msgPrint((char*)p, (sizeof c->sendbuf) - (p-c->sendbuf), m));
 
 	p[0] = m->tag;	// header - fill in size later
 	p += 4;
@@ -1432,7 +1434,7 @@ msgSend(TlsConnection *c, Msg *m, int act)
 		nn += 1 + m->u.clientHello.compressors->len;
 		if(m->u.clientHello.extensions != nil)
 			nn += 2 + m->u.clientHello.extensions->len;
-		if(p + nn - sendbuf > sizeof(sendbuf)) {
+		if(p + nn - c->sendbuf > sizeof(c->sendbuf)) {
 			tlsError(c, EInternalError, "output buffer too small for client hello");
 			goto Err;
 		}
@@ -1479,7 +1481,7 @@ msgSend(TlsConnection *c, Msg *m, int act)
 		nn = 2 + RandomSize + 1 + m->u.serverHello.sid->len + 2 + 1;
 		if(m->u.serverHello.extensions != nil)
 			nn += 2 + m->u.serverHello.extensions->len;
-		if(p + nn - sendbuf > sizeof(sendbuf)) {
+		if(p + nn - c->sendbuf > sizeof(c->sendbuf)) {
 			tlsError(c, EInternalError, "output buffer too small for server hello");
 			goto Err;
 		}
@@ -1517,7 +1519,7 @@ msgSend(TlsConnection *c, Msg *m, int act)
 		nn = 0;
 		for(i = 0; i < m->u.certificate.ncert; i++)
 			nn += 3 + m->u.certificate.certs[i]->len;
-		if(p + 3 + nn - sendbuf > sizeof(sendbuf)) {
+		if(p + 3 + nn - c->sendbuf > sizeof(c->sendbuf)) {
 			tlsError(c, EInternalError, "output buffer too small for certificate");
 			goto Err;
 		}
@@ -1542,7 +1544,7 @@ msgSend(TlsConnection *c, Msg *m, int act)
 			Bytes *params = m->u.serverKeyExchange.params;
 			Bytes *sig = m->u.serverKeyExchange.signature;
 
-			if(p + params->len + 2 + 2 + sig->len - sendbuf > sizeof(sendbuf)) {
+			if(p + params->len + 2 + 2 + sig->len - c->sendbuf > sizeof(c->sendbuf)) {
 				tlsError(c, EInternalError, "output buffer too small for key exchange");
 				goto Err;
 			}
@@ -1567,7 +1569,7 @@ msgSend(TlsConnection *c, Msg *m, int act)
 		 */
 		/* n is the peer's modulus size for RSA-KE; the assert
 		 * below fires only after the copy has overrun */
-		if(p + 2 + n - sendbuf > sizeof(sendbuf)) {
+		if(p + 2 + n - c->sendbuf > sizeof(c->sendbuf)) {
 			tlsError(c, EInternalError, "output buffer too small for key exchange");
 			goto Err;
 		}
@@ -1582,7 +1584,7 @@ msgSend(TlsConnection *c, Msg *m, int act)
 		p += n;
 		break;
 	case HFinished:
-		if(p + m->u.finished.n - sendbuf > sizeof(sendbuf)) {
+		if(p + m->u.finished.n - c->sendbuf > sizeof(c->sendbuf)) {
 			tlsError(c, EInternalError, "output buffer too small for finished");
 			goto Err;
 		}
@@ -1592,19 +1594,19 @@ msgSend(TlsConnection *c, Msg *m, int act)
 	}
 
 	// go back and fill in size
-	n = p-sendp;
-	assert(p <= sendbuf+sizeof(sendbuf));
-	put24(sendp+1, n-4);
+	n = p-c->sendp;
+	assert(p <= c->sendbuf+sizeof(c->sendbuf));
+	put24(c->sendp+1, n-4);
 
 	// remember hash of Handshake messages
 	if(m->tag != HHelloRequest) {
-		msgHash(c, sendp, n);
+		msgHash(c, c->sendp, n);
 	}
 
-	sendp = p;
+	c->sendp = p;
 	if(act == AFlush){
-		sendp = sendbuf;
-		if(write(c->hand, sendbuf, p-sendbuf) < 0){
+		c->sendp = c->sendbuf;
+		if(write(c->hand, c->sendbuf, p-c->sendbuf) < 0){
 			fprint(2, "write error: %r\n");
 			goto Err;
 		}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

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