|
|
@@ -2,6 +2,12 @@
|
|
|
* X25519 key exchange, TLS_CHACHA20_POLY1305_SHA256, no certificate
|
|
|
* verification (the leaf SHA-256 is printed for manual comparison).
|
|
|
*
|
|
|
+ * TLS 1.2 (RFC 5246) fallback for servers without 1.3: ECDHE over X25519 or
|
|
|
+ * P-256 with ChaCha20-Poly1305 (RFC 7905) or AES-128-GCM (RFC 5289), the
|
|
|
+ * extended master secret (RFC 7627) when the server offers it, and the
|
|
|
+ * same unverified certificate handling. The same ClientHello offers both;
|
|
|
+ * the ServerHello's supported_versions decides which state machine runs.
|
|
|
+ *
|
|
|
* The record layer is a small state machine; incoming records are
|
|
|
* decrypted in chunks straight from the TCP stream (SYS_TCP_RECV) so a
|
|
|
* 16 KB record never needs more than one plaintext buffer. Handshake
|
|
|
@@ -15,6 +21,8 @@
|
|
|
#include "tls_sha256.h"
|
|
|
#include "tls_chacha20.h"
|
|
|
#include "tls_x25519.h"
|
|
|
+#include "tls_gcm.h"
|
|
|
+#include "tls_p256.h"
|
|
|
#include "tls_rng.h"
|
|
|
|
|
|
#define TLS_RAW_SIZE 2048
|
|
|
@@ -44,6 +52,19 @@ static unsigned char c_ap_key[32], s_ap_key[32], c_ap_iv[12], s_ap_iv[12];
|
|
|
static unsigned char master_secret[32];
|
|
|
static unsigned char x_priv[32];
|
|
|
|
|
|
+/* TLS 1.2 state */
|
|
|
+static int ver12; /* negotiated TLS 1.2 */
|
|
|
+static int rx_enc; /* 1.2: server CCS seen, records encrypted */
|
|
|
+static int ems; /* extended_master_secret echoed */
|
|
|
+static int cert_req; /* CertificateRequest received */
|
|
|
+static unsigned int suite; /* negotiated cipher suite */
|
|
|
+static int use_gcm; /* AES-128-GCM (else ChaCha20-Poly1305) */
|
|
|
+static unsigned char client_random[32], server_random[32];
|
|
|
+static unsigned char ms12[48];
|
|
|
+static unsigned int ske_group, ske_plen;
|
|
|
+static unsigned char ske_buf[160];
|
|
|
+static struct gcm_key c_gcm, s_gcm;
|
|
|
+
|
|
|
static struct sha256_ctx transcript;
|
|
|
static unsigned char fin_data[32];
|
|
|
static int eof, err;
|
|
|
@@ -191,6 +212,56 @@ static int send_plain(unsigned char type, const unsigned char *data,
|
|
|
return tcp_send_all(data, len);
|
|
|
}
|
|
|
|
|
|
+static void seq_be(unsigned char *p, unsigned long long seq)
|
|
|
+{
|
|
|
+ unsigned int i;
|
|
|
+
|
|
|
+ for (i = 0; i < 8; i++)
|
|
|
+ p[i] = (unsigned char)(seq >> (56 - 8 * i));
|
|
|
+}
|
|
|
+
|
|
|
+/* TLS 1.2 AEAD record: the type is the outer type, AAD = seq || type ||
|
|
|
+ * version || length; GCM puts the 8-byte explicit nonce (= seq) first. */
|
|
|
+static int send_record12(unsigned char type, const unsigned char *data,
|
|
|
+ unsigned int len)
|
|
|
+{
|
|
|
+ unsigned char nonce[12], aad[13], tag[16];
|
|
|
+ unsigned int ex = use_gcm ? 8 : 0, rl = ex + len + 16, i;
|
|
|
+
|
|
|
+ if (5 + rl > TLS_TX_MAX)
|
|
|
+ return -1;
|
|
|
+ tx_buf[0] = type;
|
|
|
+ tx_buf[1] = 3;
|
|
|
+ tx_buf[2] = 3;
|
|
|
+ tx_buf[3] = (unsigned char)(rl >> 8);
|
|
|
+ tx_buf[4] = (unsigned char)rl;
|
|
|
+ seq_be(aad, tx_seq);
|
|
|
+ aad[8] = type;
|
|
|
+ aad[9] = 3;
|
|
|
+ aad[10] = 3;
|
|
|
+ aad[11] = (unsigned char)(len >> 8);
|
|
|
+ aad[12] = (unsigned char)len;
|
|
|
+ for (i = 0; i < len; i++)
|
|
|
+ tx_buf[5 + ex + i] = data[i];
|
|
|
+ if (use_gcm) {
|
|
|
+ for (i = 0; i < 4; i++)
|
|
|
+ nonce[i] = c_ap_iv[i];
|
|
|
+ seq_be(nonce + 4, tx_seq);
|
|
|
+ for (i = 0; i < 8; i++)
|
|
|
+ tx_buf[5 + i] = nonce[4 + i];
|
|
|
+ gcm_seal(&c_gcm, nonce, aad, 13, tx_buf + 5 + ex, len,
|
|
|
+ tx_buf + 5 + ex, tag);
|
|
|
+ } else {
|
|
|
+ nonce_for(nonce, c_ap_iv, tx_seq);
|
|
|
+ aead_seal(c_ap_key, nonce, aad, 13, tx_buf + 5, len,
|
|
|
+ tx_buf + 5, tag);
|
|
|
+ }
|
|
|
+ for (i = 0; i < 16; i++)
|
|
|
+ tx_buf[5 + ex + len + i] = tag[i];
|
|
|
+ tx_seq++;
|
|
|
+ return tcp_send_all(tx_buf, 5 + rl);
|
|
|
+}
|
|
|
+
|
|
|
static int send_record(unsigned char inner, const unsigned char *data,
|
|
|
unsigned int len, int use_hs)
|
|
|
{
|
|
|
@@ -199,6 +270,8 @@ static int send_record(unsigned char inner, const unsigned char *data,
|
|
|
unsigned char nonce[12], aad[5], tag[16];
|
|
|
unsigned int plen = len + 1, rl = plen + 16, i;
|
|
|
|
|
|
+ if (ver12)
|
|
|
+ return send_record12(inner, data, len);
|
|
|
if (5 + rl > TLS_TX_MAX)
|
|
|
return -1;
|
|
|
for (i = 0; i < len; i++)
|
|
|
@@ -223,6 +296,32 @@ static int send_record(unsigned char inner, const unsigned char *data,
|
|
|
|
|
|
/* ---- handshake: ServerHello ---- */
|
|
|
|
|
|
+/* A ServerHello without supported_versions: TLS 1.2 (legacy_version 0x0303). */
|
|
|
+static void sh_parse12(unsigned int cipher)
|
|
|
+{
|
|
|
+ unsigned int i;
|
|
|
+
|
|
|
+ if (sh_buf[0] != 3 || sh_buf[1] != 3) {
|
|
|
+ u_puts("tls: server offered neither TLS 1.3 nor 1.2\n");
|
|
|
+ err = 4;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (cipher != 0xc02f && cipher != 0xc02b && cipher != 0xcca8 &&
|
|
|
+ cipher != 0xcca9) {
|
|
|
+ u_puts("tls: server chose an unsupported TLS 1.2 cipher suite\n");
|
|
|
+ err = 4;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (i = 0; i < 32; i++)
|
|
|
+ server_random[i] = sh_buf[2 + i];
|
|
|
+ ver12 = 1;
|
|
|
+ suite = cipher;
|
|
|
+ use_gcm = (cipher == 0xc02f || cipher == 0xc02b);
|
|
|
+ u_puts("tls: server hello, TLS 1.2 ");
|
|
|
+ u_puts(use_gcm ? "ECDHE AES-128-GCM" : "ECDHE CHACHA20-POLY1305");
|
|
|
+ u_puts(ems ? " (extended master secret)\n" : "\n");
|
|
|
+}
|
|
|
+
|
|
|
static void sh_parse(unsigned int len)
|
|
|
{
|
|
|
const unsigned char *p = sh_buf;
|
|
|
@@ -233,7 +332,7 @@ static void sh_parse(unsigned int len)
|
|
|
|
|
|
if (err)
|
|
|
return;
|
|
|
- if (len < 2 + 32 + 1 + 2 + 1 + 2) {
|
|
|
+ if (len < 2 + 32 + 1 + 2 + 1) {
|
|
|
err = 3;
|
|
|
return;
|
|
|
}
|
|
|
@@ -246,14 +345,12 @@ static void sh_parse(unsigned int len)
|
|
|
o += sid;
|
|
|
cipher = ((unsigned int)p[o] << 8) | p[o + 1];
|
|
|
o += 2;
|
|
|
- if (cipher != 0x1303) {
|
|
|
- u_puts("tls: server chose another cipher suite\n");
|
|
|
- err = 4;
|
|
|
- return;
|
|
|
- }
|
|
|
o++; /* legacy_compression_method */
|
|
|
- elen = ((unsigned int)p[o] << 8) | p[o + 1];
|
|
|
- o += 2;
|
|
|
+ elen = 0;
|
|
|
+ if (o + 2 <= len) {
|
|
|
+ elen = ((unsigned int)p[o] << 8) | p[o + 1];
|
|
|
+ o += 2;
|
|
|
+ }
|
|
|
if (o + elen != len) {
|
|
|
err = 3;
|
|
|
return;
|
|
|
@@ -276,10 +373,16 @@ static void sh_parse(unsigned int len)
|
|
|
}
|
|
|
if (et == 0x002b && el == 2 && d[0] == 3 && d[1] == 4)
|
|
|
have_ver = 1;
|
|
|
+ if (et == 0x0017)
|
|
|
+ ems = 1;
|
|
|
i += 4 + el;
|
|
|
}
|
|
|
if (!have_ver) {
|
|
|
- u_puts("tls: server did not agree on TLS 1.3\n");
|
|
|
+ sh_parse12(cipher);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (cipher != 0x1303) {
|
|
|
+ u_puts("tls: server chose another cipher suite\n");
|
|
|
err = 4;
|
|
|
return;
|
|
|
}
|
|
|
@@ -333,6 +436,11 @@ static void cert_byte(unsigned char b)
|
|
|
switch (cert_stage) {
|
|
|
case 0:
|
|
|
sha256_init(&cert_hash);
|
|
|
+ if (ver12) { /* no request context */
|
|
|
+ cert_stage = 2;
|
|
|
+ cert_byte(b);
|
|
|
+ break;
|
|
|
+ }
|
|
|
cert_ctx_left = b;
|
|
|
cert_stage = cert_ctx_left ? 1 : 2;
|
|
|
break;
|
|
|
@@ -361,7 +469,7 @@ static void cert_byte(unsigned char b)
|
|
|
if (cert_cur_left == 0) {
|
|
|
cert_seen++;
|
|
|
cert_ext_got = 0;
|
|
|
- cert_stage = 5;
|
|
|
+ cert_stage = ver12 ? (cert_list_left ? 3 : 9) : 5;
|
|
|
} else {
|
|
|
cert_stage = 4;
|
|
|
}
|
|
|
@@ -375,7 +483,7 @@ static void cert_byte(unsigned char b)
|
|
|
if (cert_cur_left == 0) {
|
|
|
cert_seen++;
|
|
|
cert_ext_got = 0;
|
|
|
- cert_stage = 5;
|
|
|
+ cert_stage = ver12 ? (cert_list_left ? 3 : 9) : 5;
|
|
|
}
|
|
|
break;
|
|
|
case 5: /* per-entry extensions */
|
|
|
@@ -439,6 +547,10 @@ static void hs_byte(unsigned char b)
|
|
|
case 11:
|
|
|
cert_byte(b);
|
|
|
break;
|
|
|
+ case 12: /* 1.2 ServerKeyExchange */
|
|
|
+ if (body_pos < sizeof(ske_buf))
|
|
|
+ ske_buf[body_pos] = b;
|
|
|
+ break;
|
|
|
case 15:
|
|
|
break; /* CertificateVerify: not checked */
|
|
|
case 20:
|
|
|
@@ -455,12 +567,17 @@ static void hs_byte(unsigned char b)
|
|
|
}
|
|
|
|
|
|
static void hs_finish(unsigned int len);
|
|
|
+static void hs_finish12(unsigned int len);
|
|
|
+static void ske_parse(unsigned int len);
|
|
|
+static void hello_done12(void);
|
|
|
|
|
|
static void hs_msg_complete(void)
|
|
|
{
|
|
|
unsigned int type = hm_type, len = hm_len;
|
|
|
|
|
|
hm_got = 0;
|
|
|
+ if (ver12 && phase == 2)
|
|
|
+ return; /* HelloRequest etc.: ignored */
|
|
|
switch (type) {
|
|
|
case 2:
|
|
|
sh_parse(len);
|
|
|
@@ -468,8 +585,23 @@ static void hs_msg_complete(void)
|
|
|
case 11:
|
|
|
cert_msg_done();
|
|
|
break;
|
|
|
+ case 12:
|
|
|
+ if (ver12)
|
|
|
+ ske_parse(len);
|
|
|
+ break;
|
|
|
+ case 13:
|
|
|
+ if (ver12)
|
|
|
+ cert_req = 1;
|
|
|
+ break;
|
|
|
+ case 14:
|
|
|
+ if (ver12)
|
|
|
+ hello_done12();
|
|
|
+ break;
|
|
|
case 20:
|
|
|
- hs_finish(len);
|
|
|
+ if (ver12)
|
|
|
+ hs_finish12(len);
|
|
|
+ else
|
|
|
+ hs_finish(len);
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
@@ -524,6 +656,11 @@ static void hs_finish(unsigned int len)
|
|
|
for (i = 0; i < 32; i++)
|
|
|
msg[4 + i] = expect[i];
|
|
|
}
|
|
|
+ {
|
|
|
+ static const unsigned char ccs[1] = { 1 };
|
|
|
+
|
|
|
+ send_plain(20, ccs, 1); /* middlebox compatibility */
|
|
|
+ }
|
|
|
if (send_record(22, msg, 36, 1) != 0) {
|
|
|
err = 1;
|
|
|
return;
|
|
|
@@ -534,8 +671,162 @@ static void hs_finish(unsigned int len)
|
|
|
tx_seq = 0;
|
|
|
}
|
|
|
|
|
|
+/* ---- TLS 1.2 handshake ---- */
|
|
|
+
|
|
|
+static void ske_parse(unsigned int len)
|
|
|
+{
|
|
|
+ const unsigned char *p = ske_buf;
|
|
|
+
|
|
|
+ if (err)
|
|
|
+ return;
|
|
|
+ if (len > sizeof(ske_buf))
|
|
|
+ len = sizeof(ske_buf);
|
|
|
+ if (len < 4 || p[0] != 3 || 4 + (unsigned int)p[3] > len) {
|
|
|
+ u_puts("tls: unsupported ServerKeyExchange (need named-curve ECDHE)\n");
|
|
|
+ err = 4;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ ske_group = ((unsigned int)p[1] << 8) | p[2];
|
|
|
+ ske_plen = p[3];
|
|
|
+ if (!((ske_group == 0x001d && ske_plen == 32) ||
|
|
|
+ (ske_group == 0x0017 && ske_plen == 65))) {
|
|
|
+ u_puts("tls: server chose an unsupported ECDHE group\n");
|
|
|
+ err = 4;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ /* The signature over the parameters is not verified (phase A). */
|
|
|
+}
|
|
|
+
|
|
|
+static int send_hs12(const unsigned char *msg, unsigned int len)
|
|
|
+{
|
|
|
+ sha256_update(&transcript, msg, len);
|
|
|
+ return send_plain(22, msg, len);
|
|
|
+}
|
|
|
+
|
|
|
+static void hello_done12(void)
|
|
|
+{
|
|
|
+ unsigned char priv[32], pms[32], msg[4 + 1 + 65], th[32], kb[88];
|
|
|
+ unsigned char seed[64];
|
|
|
+ unsigned int publen, i;
|
|
|
+
|
|
|
+ if (err)
|
|
|
+ return;
|
|
|
+ if (ske_plen == 0) {
|
|
|
+ u_puts("tls: no ServerKeyExchange (RSA key exchange unsupported)\n");
|
|
|
+ err = 4;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (cert_req) { /* empty client Certificate */
|
|
|
+ static const unsigned char empty[7] = { 11, 0, 0, 3, 0, 0, 0 };
|
|
|
+
|
|
|
+ if (send_hs12(empty, 7) != 0) {
|
|
|
+ err = 1;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ tls_random(priv, 32);
|
|
|
+ if (ske_group == 0x001d) {
|
|
|
+ x25519(msg + 5, priv, x25519_base_point);
|
|
|
+ x25519(pms, priv, ske_buf + 4);
|
|
|
+ publen = 32;
|
|
|
+ } else {
|
|
|
+ priv[0] &= 0x7f; /* keep the scalar below n */
|
|
|
+ if (p256_public(priv, msg + 5) != 0 ||
|
|
|
+ p256_shared(priv, ske_buf + 4, 65, pms) != 0) {
|
|
|
+ u_puts("tls: bad P-256 server key\n");
|
|
|
+ err = 5;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ publen = 65;
|
|
|
+ }
|
|
|
+ msg[0] = 16; /* ClientKeyExchange */
|
|
|
+ msg[1] = 0;
|
|
|
+ msg[2] = 0;
|
|
|
+ msg[3] = (unsigned char)(1 + publen);
|
|
|
+ msg[4] = (unsigned char)publen;
|
|
|
+ if (send_hs12(msg, 5 + publen) != 0) {
|
|
|
+ err = 1;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ transcript_hash(th); /* session hash: CH .. CKE */
|
|
|
+ if (ems)
|
|
|
+ tls12_prf(pms, 32, "extended master secret", th, 32, 0, 0,
|
|
|
+ ms12, 48);
|
|
|
+ else
|
|
|
+ tls12_prf(pms, 32, "master secret", client_random, 32,
|
|
|
+ server_random, 32, ms12, 48);
|
|
|
+ tls12_prf(ms12, 48, "key expansion", server_random, 32, client_random,
|
|
|
+ 32, kb, sizeof(kb));
|
|
|
+ if (use_gcm) {
|
|
|
+ for (i = 0; i < 4; i++) {
|
|
|
+ c_ap_iv[i] = kb[32 + i];
|
|
|
+ s_ap_iv[i] = kb[36 + i];
|
|
|
+ }
|
|
|
+ gcm_setkey(&c_gcm, kb);
|
|
|
+ gcm_setkey(&s_gcm, kb + 16);
|
|
|
+ } else {
|
|
|
+ for (i = 0; i < 32; i++) {
|
|
|
+ c_ap_key[i] = kb[i];
|
|
|
+ s_ap_key[i] = kb[32 + i];
|
|
|
+ }
|
|
|
+ for (i = 0; i < 12; i++) {
|
|
|
+ c_ap_iv[i] = kb[64 + i];
|
|
|
+ s_ap_iv[i] = kb[76 + i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ static const unsigned char ccs[1] = { 1 };
|
|
|
+
|
|
|
+ if (send_plain(20, ccs, 1) != 0) {
|
|
|
+ err = 1;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ tx_seq = 0;
|
|
|
+ seed[0] = 20; /* client Finished */
|
|
|
+ seed[1] = 0;
|
|
|
+ seed[2] = 0;
|
|
|
+ seed[3] = 12;
|
|
|
+ tls12_prf(ms12, 48, "client finished", th, 32, 0, 0, seed + 4, 12);
|
|
|
+ sha256_update(&transcript, seed, 16);
|
|
|
+ if (send_record12(22, seed, 16) != 0) {
|
|
|
+ err = 1;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ phase = 1; /* waiting for CCS + Finished */
|
|
|
+ for (i = 0; i < 32; i++)
|
|
|
+ pms[i] = priv[i] = 0;
|
|
|
+}
|
|
|
+
|
|
|
+static void hs_finish12(unsigned int len)
|
|
|
+{
|
|
|
+ unsigned char th[32], expect[12];
|
|
|
+ unsigned int i, d = 0;
|
|
|
+
|
|
|
+ if (err)
|
|
|
+ return;
|
|
|
+ if (len != 12 || phase != 1 || !rx_enc) {
|
|
|
+ err = 3;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ transcript_hash(th);
|
|
|
+ tls12_prf(ms12, 48, "server finished", th, 32, 0, 0, expect, 12);
|
|
|
+ for (i = 0; i < 12; i++)
|
|
|
+ d |= expect[i] ^ fin_data[i];
|
|
|
+ if (d) {
|
|
|
+ u_puts("tls: bad server Finished (handshake mismatch)\n");
|
|
|
+ err = 5;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ phase = 2;
|
|
|
+}
|
|
|
+
|
|
|
/* ---- record input ---- */
|
|
|
|
|
|
+static void rec_dispatch(unsigned char type, unsigned int len);
|
|
|
+
|
|
|
static void inner_process(unsigned int len)
|
|
|
{
|
|
|
unsigned int i = len;
|
|
|
@@ -548,7 +839,12 @@ static void inner_process(unsigned int len)
|
|
|
return;
|
|
|
}
|
|
|
type = rec_pt[i - 1];
|
|
|
- len = i - 1;
|
|
|
+ rec_dispatch(type, i - 1);
|
|
|
+}
|
|
|
+
|
|
|
+static void rec_dispatch(unsigned char type, unsigned int len)
|
|
|
+{
|
|
|
+ unsigned int i;
|
|
|
|
|
|
switch (type) {
|
|
|
case 22:
|
|
|
@@ -576,6 +872,69 @@ static void inner_process(unsigned int len)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+static int pump_record12(const unsigned char hdr[5], unsigned int len,
|
|
|
+ unsigned int deadline)
|
|
|
+{
|
|
|
+ struct aead_open_ctx a;
|
|
|
+ struct gcm_ctx g;
|
|
|
+ unsigned char nonce[12], aad[13], tag[16], expect[16];
|
|
|
+ unsigned int ex = use_gcm ? 8 : 0, ptlen, off = 0, i, d = 0;
|
|
|
+
|
|
|
+ if ((hdr[0] != 22 && hdr[0] != 23 && hdr[0] != 21) ||
|
|
|
+ len < ex + 16 || len - ex - 16 > TLS_REC_PT_MAX) {
|
|
|
+ err = 3;
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ ptlen = len - ex - 16;
|
|
|
+ if (use_gcm) {
|
|
|
+ for (i = 0; i < 4; i++)
|
|
|
+ nonce[i] = s_ap_iv[i];
|
|
|
+ if (raw_read(nonce + 4, 8, deadline) != 0)
|
|
|
+ return -1;
|
|
|
+ } else {
|
|
|
+ nonce_for(nonce, s_ap_iv, rx_seq);
|
|
|
+ }
|
|
|
+ seq_be(aad, rx_seq);
|
|
|
+ aad[8] = hdr[0];
|
|
|
+ aad[9] = 3;
|
|
|
+ aad[10] = 3;
|
|
|
+ aad[11] = (unsigned char)(ptlen >> 8);
|
|
|
+ aad[12] = (unsigned char)ptlen;
|
|
|
+ if (use_gcm)
|
|
|
+ gcm_open_init(&g, &s_gcm, nonce, aad, 13);
|
|
|
+ else
|
|
|
+ aead_open_init(&a, s_ap_key, nonce, aad, 13);
|
|
|
+ while (off < ptlen) {
|
|
|
+ unsigned int n = ptlen - off;
|
|
|
+
|
|
|
+ if (n > 1024)
|
|
|
+ n = 1024;
|
|
|
+ if (raw_read(rec_pt + off, n, deadline) != 0)
|
|
|
+ return -1;
|
|
|
+ if (use_gcm)
|
|
|
+ gcm_open_update(&g, rec_pt + off, rec_pt + off, n);
|
|
|
+ else
|
|
|
+ aead_open_update(&a, rec_pt + off, rec_pt + off, n);
|
|
|
+ off += n;
|
|
|
+ }
|
|
|
+ if (raw_read(tag, 16, deadline) != 0)
|
|
|
+ return -1;
|
|
|
+ if (use_gcm)
|
|
|
+ gcm_open_tag(&g, expect);
|
|
|
+ else
|
|
|
+ aead_open_tag(&a, expect);
|
|
|
+ for (i = 0; i < 16; i++)
|
|
|
+ d |= tag[i] ^ expect[i];
|
|
|
+ if (d) {
|
|
|
+ u_puts("tls: record authentication failed\n");
|
|
|
+ err = 5;
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ rx_seq++;
|
|
|
+ rec_dispatch(hdr[0], ptlen);
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
static int pump_record(unsigned int deadline)
|
|
|
{
|
|
|
unsigned char hdr[5], tag[16], expect[16];
|
|
|
@@ -592,6 +951,14 @@ static int pump_record(unsigned int deadline)
|
|
|
err = 3;
|
|
|
return -1;
|
|
|
}
|
|
|
+ if (ver12) { /* 1.2: server starts encrypting */
|
|
|
+ if (phase != 1 || rx_enc) {
|
|
|
+ err = 3;
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ rx_enc = 1;
|
|
|
+ rx_seq = 0;
|
|
|
+ }
|
|
|
return 0;
|
|
|
}
|
|
|
if (hdr[0] == 21) { /* plaintext alert */
|
|
|
@@ -624,17 +991,21 @@ static int pump_record(unsigned int deadline)
|
|
|
err = 4;
|
|
|
return -1;
|
|
|
}
|
|
|
- if (phase == 0) {
|
|
|
- if (hdr[0] != 22 || len > TLS_SH_MAX) {
|
|
|
+ if (ver12 ? !rx_enc : phase == 0) {
|
|
|
+ /* Plaintext handshake: one ServerHello record for 1.3, the whole
|
|
|
+ * 1.2 server flight (certificates included) otherwise. */
|
|
|
+ if (hdr[0] != 22 || len > TLS_REC_PT_MAX) {
|
|
|
err = 3;
|
|
|
return -1;
|
|
|
}
|
|
|
- if (raw_read(sh_buf, len, deadline) != 0)
|
|
|
+ if (raw_read(rec_pt, len, deadline) != 0)
|
|
|
return -1;
|
|
|
for (i = 0; i < len && !err; i++)
|
|
|
- hs_byte(sh_buf[i]);
|
|
|
+ hs_byte(rec_pt[i]);
|
|
|
return 0;
|
|
|
}
|
|
|
+ if (ver12)
|
|
|
+ return pump_record12(hdr, len, deadline);
|
|
|
if (hdr[0] != 23 || len < 17 || len > TLS_REC_PT_MAX) {
|
|
|
err = 3;
|
|
|
return -1;
|
|
|
@@ -726,12 +1097,21 @@ static int build_client_hello(const char *sni)
|
|
|
ch_u24(0); /* length (patched below) */
|
|
|
ch_u16(0x0303); /* legacy_version */
|
|
|
tls_random(cr, 32);
|
|
|
+ for (hostlen = 0; hostlen < 32; hostlen++)
|
|
|
+ client_random[hostlen] = cr[hostlen];
|
|
|
+ hostlen = 0;
|
|
|
+ while (sni[hostlen])
|
|
|
+ hostlen++;
|
|
|
ch_bytes(cr, 32);
|
|
|
tls_random(sid, 32);
|
|
|
ch_u8(32); /* legacy_session_id */
|
|
|
ch_bytes(sid, 32);
|
|
|
- ch_u16(2); /* cipher suites */
|
|
|
- ch_u16(0x1303);
|
|
|
+ ch_u16(10); /* cipher suites */
|
|
|
+ ch_u16(0x1303); /* TLS 1.3 */
|
|
|
+ ch_u16(0xcca9); /* 1.2 ECDHE-ECDSA-CHACHA20-POLY1305 */
|
|
|
+ ch_u16(0xcca8); /* 1.2 ECDHE-RSA-CHACHA20-POLY1305 */
|
|
|
+ ch_u16(0xc02b); /* 1.2 ECDHE-ECDSA-AES128-GCM-SHA256 */
|
|
|
+ ch_u16(0xc02f); /* 1.2 ECDHE-RSA-AES128-GCM-SHA256 */
|
|
|
ch_u8(1); /* compression */
|
|
|
ch_u8(0);
|
|
|
|
|
|
@@ -741,28 +1121,45 @@ static int build_client_hello(const char *sni)
|
|
|
|
|
|
/* supported_versions */
|
|
|
ch_u16(0x002b);
|
|
|
- ch_u16(3);
|
|
|
- ch_u8(2);
|
|
|
+ ch_u16(5);
|
|
|
+ ch_u8(4);
|
|
|
ch_u16(0x0304);
|
|
|
- /* supported_groups */
|
|
|
+ ch_u16(0x0303);
|
|
|
+ /* supported_groups: P-256 only for TLS 1.2 servers without X25519
|
|
|
+ * (the 1.3 key share stays X25519; there is no HelloRetryRequest). */
|
|
|
ch_u16(0x000a);
|
|
|
+ ch_u16(6);
|
|
|
ch_u16(4);
|
|
|
- ch_u16(2);
|
|
|
ch_u16(0x001d);
|
|
|
+ ch_u16(0x0017);
|
|
|
+ /* ec_point_formats: uncompressed */
|
|
|
+ ch_u16(0x000b);
|
|
|
+ ch_u16(2);
|
|
|
+ ch_u8(1);
|
|
|
+ ch_u8(0);
|
|
|
+ /* extended_master_secret, renegotiation_info (empty) */
|
|
|
+ ch_u16(0x0017);
|
|
|
+ ch_u16(0);
|
|
|
+ ch_u16(0xff01);
|
|
|
+ ch_u16(1);
|
|
|
+ ch_u8(0);
|
|
|
/* signature_algorithms: advertise the ECDSA curves used by modern
|
|
|
* site certificates (nd-aktuell.de uses a P-384 leaf) as well as the
|
|
|
* RSA-PSS schemes. Signatures are not verified in phase A, but the
|
|
|
* server refuses the handshake when its certificate's scheme is not
|
|
|
* listed. */
|
|
|
ch_u16(0x000d);
|
|
|
- ch_u16(14);
|
|
|
- ch_u16(12);
|
|
|
+ ch_u16(20);
|
|
|
+ ch_u16(18);
|
|
|
ch_u16(0x0403); /* ecdsa_secp256r1_sha256 */
|
|
|
ch_u16(0x0503); /* ecdsa_secp384r1_sha384 */
|
|
|
ch_u16(0x0603); /* ecdsa_secp521r1_sha512 */
|
|
|
ch_u16(0x0804); /* rsa_pss_rsae_sha256 */
|
|
|
ch_u16(0x0805); /* rsa_pss_rsae_sha384 */
|
|
|
ch_u16(0x0806); /* rsa_pss_rsae_sha512 */
|
|
|
+ ch_u16(0x0401); /* rsa_pkcs1_sha256 (TLS 1.2) */
|
|
|
+ ch_u16(0x0501); /* rsa_pkcs1_sha384 */
|
|
|
+ ch_u16(0x0601); /* rsa_pkcs1_sha512 */
|
|
|
/* key_share */
|
|
|
tls_random(x_priv, 32);
|
|
|
x25519(pub, x_priv, x25519_base_point);
|
|
|
@@ -812,6 +1209,10 @@ int tls_connect(const char *sni)
|
|
|
hm_got = 0;
|
|
|
cert_stage = cert_seen = 0;
|
|
|
cert_hdr_got = 0;
|
|
|
+ ver12 = rx_enc = ems = cert_req = 0;
|
|
|
+ suite = 0;
|
|
|
+ use_gcm = 0;
|
|
|
+ ske_plen = ske_group = 0;
|
|
|
|
|
|
peer_mss = (unsigned int)u_syscall0(SYS_TCP_PEER_MSS);
|
|
|
if (peer_mss < 64 || peer_mss > 1460)
|
|
|
@@ -830,11 +1231,6 @@ int tls_connect(const char *sni)
|
|
|
u_puts("tls: send failed\n");
|
|
|
return -1;
|
|
|
}
|
|
|
- {
|
|
|
- static const unsigned char ccs[1] = { 1 };
|
|
|
-
|
|
|
- send_plain(20, ccs, 1); /* middlebox compatibility */
|
|
|
- }
|
|
|
u_puts("tls: client hello sent (");
|
|
|
u_putdec(n);
|
|
|
u_puts(" bytes)\n");
|
|
|
@@ -847,7 +1243,7 @@ int tls_connect(const char *sni)
|
|
|
if (phase != 2) {
|
|
|
u_puts("tls: handshake failed");
|
|
|
if (err == 4)
|
|
|
- u_puts(" (server does not support TLS 1.3)");
|
|
|
+ u_puts(" (no common protocol version or cipher suite)");
|
|
|
else if (err == 2)
|
|
|
u_puts(" (timeout)");
|
|
|
else if (err == 1)
|