| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314 |
- /* Minimal TLS 1.3 client (RFC 8446) over the kernel TCP client:
- * 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
- * messages are fed byte-wise through a transcript-hashing parser, so a
- * large Certificate message is hashed as it flows past.
- *
- * Phase A trade-off: this authenticates nothing. See tls.h. */
- #include "ucmd_lib.h"
- #include "tls.h"
- #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
- #define TLS_REC_PT_MAX (16384 + 256)
- #define TLS_SH_MAX 1024
- #define TLS_TX_MAX 2048
- #define TLS_APP_CHUNK 1024
- #define TLS_HS_DEADLINE_MS 15000
- /* ---- state (one connection at a time) ---- */
- static unsigned char raw_buf[TLS_RAW_SIZE];
- static unsigned int raw_len, raw_pos;
- static unsigned char rec_pt[TLS_REC_PT_MAX]; /* decrypted record payload */
- static unsigned int app_off, app_len; /* pending app data in rec_pt */
- static unsigned char sh_buf[TLS_SH_MAX];
- static unsigned char tx_buf[TLS_TX_MAX];
- static unsigned int peer_mss = 536;
- static int phase; /* 0 clear, 1 hs keys, 2 app keys */
- static unsigned long long rx_seq, tx_seq;
- static unsigned char c_hs[32], s_hs[32], c_ap[32], s_ap[32];
- static unsigned char c_hs_key[32], s_hs_key[32], c_hs_iv[12], s_hs_iv[12];
- 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;
- /* handshake message parser */
- static unsigned int hm_type, hm_len, hm_got;
- /* Certificate streaming parser */
- static unsigned int cert_stage; /* 0 ctx-len, 1 ctx, 2 list-len, 3 cert-len, 4 cert data, 5 ext-len, 6 ext data, 9 done */
- static unsigned int cert_ctx_left, cert_hdr_got, cert_list_left;
- static unsigned int cert_cur_left, cert_seen;
- static unsigned int cert_ext_got, cert_ext_left;
- static struct sha256_ctx cert_hash;
- static const unsigned char zeros32[32];
- static const unsigned char sha256_empty[32] = {
- 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
- 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
- 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
- 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
- };
- /* ---- small helpers ---- */
- static int eq32(const unsigned char *a, const unsigned char *b)
- {
- unsigned int i, d = 0;
- for (i = 0; i < 32; i++)
- d |= a[i] ^ b[i];
- return d == 0;
- }
- static void transcript_hash(unsigned char out[32])
- {
- struct sha256_ctx t;
- sha256_copy(&t, &transcript);
- sha256_final(&t, out);
- }
- static void derive_secret_th(const unsigned char *secret, const char *label,
- const unsigned char th[32], unsigned char out[32])
- {
- hkdf_expand_label(secret, label, th, 32, out, 32);
- }
- static void derive_keys(const unsigned char *secret, unsigned char key[32],
- unsigned char iv[12])
- {
- hkdf_expand_label(secret, "key", 0, 0, key, 32);
- hkdf_expand_label(secret, "iv", 0, 0, iv, 12);
- }
- static void nonce_for(unsigned char nonce[12], const unsigned char iv[12],
- unsigned long long seq)
- {
- unsigned int i;
- for (i = 0; i < 12; i++)
- nonce[i] = iv[i];
- for (i = 0; i < 8; i++)
- nonce[11 - i] ^= (unsigned char)(seq >> (8 * i));
- }
- /* ---- raw TCP input ---- */
- static int raw_fill(unsigned int timeout)
- {
- int r = (int)u_syscall3(SYS_TCP_RECV, (int)raw_buf,
- (int)sizeof(raw_buf), (int)timeout);
- if (r > 0) {
- raw_len = (unsigned int)r;
- raw_pos = 0;
- return 0;
- }
- if (r == 0) {
- eof = 1;
- return -1;
- }
- err = (r == -2) ? 1 : 2;
- return -1;
- }
- static int raw_read(void *dst, unsigned int n, unsigned int deadline)
- {
- unsigned char *d = dst;
- while (n) {
- unsigned int avail = raw_len - raw_pos;
- unsigned int take, i;
- if (!avail) {
- int left;
- if (err || eof)
- return -1;
- left = (int)(deadline - u_ticks_ms());
- if (left <= 0) {
- err = 2;
- return -1;
- }
- if (raw_fill((unsigned int)left) != 0)
- return -1;
- continue;
- }
- take = avail < n ? avail : n;
- for (i = 0; i < take; i++)
- d[i] = raw_buf[raw_pos + i];
- raw_pos += take;
- d += take;
- n -= take;
- }
- return 0;
- }
- /* ---- record output ---- */
- static int tcp_send_all(const unsigned char *p, unsigned int n)
- {
- while (n) {
- unsigned int take = n < peer_mss ? n : peer_mss;
- if (u_syscall2(SYS_TCP_SEND, (int)p, (int)take) != 0)
- return -1;
- p += take;
- n -= take;
- }
- return 0;
- }
- static int send_plain(unsigned char type, const unsigned char *data,
- unsigned int len)
- {
- unsigned char hdr[5];
- hdr[0] = type;
- hdr[1] = 3;
- hdr[2] = 3;
- hdr[3] = (unsigned char)(len >> 8);
- hdr[4] = (unsigned char)len;
- if (tcp_send_all(hdr, 5) != 0)
- return -1;
- 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)
- {
- const unsigned char *key = use_hs ? c_hs_key : c_ap_key;
- const unsigned char *iv = use_hs ? c_hs_iv : c_ap_iv;
- 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++)
- tx_buf[5 + i] = data[i];
- tx_buf[5 + len] = inner;
- aad[0] = 23;
- aad[1] = 3;
- aad[2] = 3;
- aad[3] = (unsigned char)(rl >> 8);
- aad[4] = (unsigned char)rl;
- for (i = 0; i < 5; i++)
- tx_buf[i] = aad[i]; /* the record header on the wire */
- nonce_for(nonce, iv, tx_seq);
- aead_seal(key, nonce, aad, 5, tx_buf + 5, plen, tx_buf + 5, tag);
- for (i = 0; i < 16; i++)
- tx_buf[5 + plen + i] = tag[i];
- tx_seq++;
- return tcp_send_all(tx_buf, 5 + rl);
- }
- /* ---- 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;
- unsigned char shared[32], early[32], d1[32], d2[32], hs[32], th[32];
- unsigned char server_key[32];
- unsigned int o, sid, cipher, elen, i, j;
- int have_key = 0, have_ver = 0;
- if (err)
- return;
- if (len < 2 + 32 + 1 + 2 + 1) {
- err = 3;
- return;
- }
- o = 2 + 32;
- sid = p[o++];
- if (o + sid + 3 > len) {
- err = 3;
- return;
- }
- o += sid;
- cipher = ((unsigned int)p[o] << 8) | p[o + 1];
- o += 2;
- o++; /* legacy_compression_method */
- elen = 0;
- if (o + 2 <= len) {
- elen = ((unsigned int)p[o] << 8) | p[o + 1];
- o += 2;
- }
- if (o + elen != len) {
- err = 3;
- return;
- }
- for (i = 0; i + 4 <= elen; ) {
- unsigned int et = ((unsigned int)p[o + i] << 8) | p[o + i + 1];
- unsigned int el = ((unsigned int)p[o + i + 2] << 8) |
- p[o + i + 3];
- const unsigned char *d = p + o + i + 4;
- if (i + 4 + el > elen) {
- err = 3;
- return;
- }
- if (et == 0x0033 && el == 36 && d[0] == 0x00 && d[1] == 0x1d &&
- d[2] == 0x00 && d[3] == 32) {
- for (j = 0; j < 32; j++)
- server_key[j] = d[4 + j];
- have_key = 1;
- }
- 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) {
- sh_parse12(cipher);
- return;
- }
- if (cipher != 0x1303) {
- u_puts("tls: server chose another cipher suite\n");
- err = 4;
- return;
- }
- if (!have_key) {
- u_puts("tls: server sent no X25519 key share\n");
- err = 4;
- return;
- }
- x25519(shared, x_priv, server_key);
- hkdf_extract(0, 0, zeros32, 32, early);
- derive_secret_th(early, "derived", sha256_empty, d1);
- hkdf_extract(d1, 32, shared, 32, hs);
- transcript_hash(th);
- derive_secret_th(hs, "c hs traffic", th, c_hs);
- derive_secret_th(hs, "s hs traffic", th, s_hs);
- derive_keys(c_hs, c_hs_key, c_hs_iv);
- derive_keys(s_hs, s_hs_key, s_hs_iv);
- derive_secret_th(hs, "derived", sha256_empty, d2);
- hkdf_extract(d2, 32, zeros32, 32, master_secret);
- phase = 1;
- u_puts("tls: server hello, TLS_CHACHA20_POLY1305_SHA256 + X25519\n");
- }
- /* ---- handshake: Certificate (streamed, fingerprint only) ---- */
- static unsigned char cert_lenbuf[3];
- static void cert_msg_done(void)
- {
- unsigned char fp[32];
- unsigned int i;
- if (cert_stage != 9 || cert_seen == 0) {
- err = 3;
- return;
- }
- sha256_final(&cert_hash, fp);
- u_puts("tls: cert[0] sha256=");
- for (i = 0; i < 32; i++)
- u_put_hex2(fp[i]);
- u_putc('\n');
- u_puts("tls: chain has ");
- u_putdec(cert_seen);
- u_puts(" certificate(s), NOT verified\n");
- }
- 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;
- case 1:
- if (--cert_ctx_left == 0)
- cert_stage = 2;
- break;
- case 2:
- cert_lenbuf[cert_hdr_got++] = b;
- if (cert_hdr_got == 3) {
- cert_hdr_got = 0;
- cert_list_left = ((unsigned int)cert_lenbuf[0] << 16) |
- ((unsigned int)cert_lenbuf[1] << 8) |
- cert_lenbuf[2];
- cert_stage = cert_list_left ? 3 : 9;
- }
- break;
- case 3:
- cert_lenbuf[cert_hdr_got++] = b;
- cert_list_left--;
- if (cert_hdr_got == 3) {
- cert_hdr_got = 0;
- cert_cur_left = ((unsigned int)cert_lenbuf[0] << 16) |
- ((unsigned int)cert_lenbuf[1] << 8) |
- cert_lenbuf[2];
- if (cert_cur_left == 0) {
- cert_seen++;
- cert_ext_got = 0;
- cert_stage = ver12 ? (cert_list_left ? 3 : 9) : 5;
- } else {
- cert_stage = 4;
- }
- }
- break;
- case 4:
- if (cert_seen == 0)
- sha256_update(&cert_hash, &b, 1);
- cert_cur_left--;
- cert_list_left--;
- if (cert_cur_left == 0) {
- cert_seen++;
- cert_ext_got = 0;
- cert_stage = ver12 ? (cert_list_left ? 3 : 9) : 5;
- }
- break;
- case 5: /* per-entry extensions */
- cert_lenbuf[cert_ext_got++] = b;
- cert_list_left--;
- if (cert_ext_got == 2) {
- cert_ext_left = ((unsigned int)cert_lenbuf[0] << 8) |
- cert_lenbuf[1];
- cert_stage = cert_ext_left ? 6 :
- (cert_list_left ? 3 : 9);
- }
- break;
- case 6:
- cert_ext_left--;
- cert_list_left--;
- if (cert_ext_left == 0)
- cert_stage = cert_list_left ? 3 : 9;
- break;
- default:
- break;
- }
- }
- /* ---- handshake message parser ---- */
- static void hs_msg_complete(void);
- static void hs_byte(unsigned char b)
- {
- unsigned int body_pos;
- if (hm_got < 4) {
- if (hm_got == 0) {
- hm_type = b;
- hm_len = 0;
- } else {
- hm_len = (hm_len << 8) | b;
- }
- if (hm_type != 20)
- sha256_update(&transcript, &b, 1);
- hm_got++;
- if (hm_got == 4 && hm_len == 0)
- hs_msg_complete();
- return;
- }
- if (hm_got - 4 >= hm_len) { /* defensive */
- hs_msg_complete();
- return;
- }
- body_pos = hm_got - 4;
- if (hm_type != 20)
- sha256_update(&transcript, &b, 1);
- switch (hm_type) {
- case 2:
- if (body_pos < TLS_SH_MAX)
- sh_buf[body_pos] = b;
- else
- err = 3;
- break;
- 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:
- if (body_pos < 32)
- fin_data[body_pos] = b;
- break;
- default:
- break; /* EE and post-handshake: skip */
- }
- hm_got++;
- if (hm_got - 4 == hm_len)
- hs_msg_complete();
- }
- 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);
- break;
- 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:
- if (ver12)
- hs_finish12(len);
- else
- hs_finish(len);
- break;
- default:
- break;
- }
- }
- static void hs_finish(unsigned int len)
- {
- unsigned char th[32], fk[32], expect[32], msg[36];
- if (err)
- return;
- if (len != 32 || phase != 1) {
- err = 3;
- return;
- }
- transcript_hash(th);
- hkdf_expand_label(s_hs, "finished", 0, 0, fk, 32);
- hmac_sha256(fk, 32, th, 32, expect);
- if (!eq32(expect, fin_data)) {
- u_puts("tls: bad server Finished (handshake mismatch)\n");
- err = 5;
- return;
- }
- /* Transcript += server Finished. */
- msg[0] = 20;
- msg[1] = 0;
- msg[2] = 0;
- msg[3] = 32;
- sha256_update(&transcript, msg, 4);
- sha256_update(&transcript, fin_data, 32);
- /* Application traffic secrets (context: CH .. server Finished). */
- transcript_hash(th);
- derive_secret_th(master_secret, "c ap traffic", th, c_ap);
- derive_secret_th(master_secret, "s ap traffic", th, s_ap);
- derive_keys(c_ap, c_ap_key, c_ap_iv);
- derive_keys(s_ap, s_ap_key, s_ap_iv);
- /* Our Finished, under the handshake keys. */
- hkdf_expand_label(c_hs, "finished", 0, 0, fk, 32);
- hmac_sha256(fk, 32, th, 32, expect);
- msg[0] = 20;
- msg[1] = 0;
- msg[2] = 0;
- msg[3] = 32;
- {
- unsigned int i;
- 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;
- }
- phase = 2;
- rx_seq = 0;
- 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;
- unsigned char type;
- while (i > 0 && rec_pt[i - 1] == 0)
- i--;
- if (i == 0) {
- err = 3;
- return;
- }
- type = rec_pt[i - 1];
- rec_dispatch(type, i - 1);
- }
- static void rec_dispatch(unsigned char type, unsigned int len)
- {
- unsigned int i;
- switch (type) {
- case 22:
- for (i = 0; i < len && !err; i++)
- hs_byte(rec_pt[i]);
- break;
- case 23:
- app_off = 0;
- app_len = len;
- break;
- case 21:
- if (len >= 2) {
- if (rec_pt[1] == 0) {
- eof = 1; /* close_notify */
- } else {
- u_puts("tls: alert ");
- u_putdec(rec_pt[1]);
- u_putc('\n');
- err = 6;
- }
- }
- break;
- default:
- break; /* inner CCS etc. */
- }
- }
- 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];
- unsigned int len, i;
- if (err || eof)
- return -1;
- if (raw_read(hdr, 5, deadline) != 0)
- return -1;
- len = ((unsigned int)hdr[3] << 8) | hdr[4];
- if (hdr[0] == 20) { /* CCS (middlebox mode) */
- if (len > 4 || raw_read(rec_pt, len, deadline) != 0) {
- 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 */
- if (len != 2 || raw_read(rec_pt, 2, deadline) != 0) {
- err = 3;
- return -1;
- }
- u_puts("tls: server alert: ");
- switch (rec_pt[1]) {
- case 40:
- u_puts("handshake failure");
- break;
- case 47:
- u_puts("illegal parameter");
- break;
- case 70:
- u_puts("protocol version (the server may not support TLS 1.3)");
- break;
- case 80:
- u_puts("internal error");
- break;
- default:
- u_puts("description ");
- u_putdec(rec_pt[1]);
- break;
- }
- u_puts(" (level ");
- u_putdec(rec_pt[0]);
- u_puts(")\n");
- err = 4;
- return -1;
- }
- 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(rec_pt, len, deadline) != 0)
- return -1;
- for (i = 0; i < len && !err; 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;
- }
- {
- const unsigned char *key = (phase == 1) ? s_hs_key : s_ap_key;
- const unsigned char *iv = (phase == 1) ? s_hs_iv : s_ap_iv;
- struct aead_open_ctx a;
- unsigned char nonce[12];
- unsigned int ptlen = len - 16, off = 0;
- nonce_for(nonce, iv, rx_seq);
- aead_open_init(&a, key, nonce, hdr, 5);
- while (off < ptlen) {
- unsigned int n = ptlen - off;
- if (n > 1024)
- n = 1024;
- if (raw_read(rec_pt + off, n, deadline) != 0)
- return -1;
- aead_open_update(&a, rec_pt + off, rec_pt + off, n);
- off += n;
- }
- if (raw_read(tag, 16, deadline) != 0)
- return -1;
- aead_open_tag(&a, expect);
- for (i = 0; i < 16; i++) {
- if (tag[i] != expect[i]) {
- u_puts("tls: record authentication failed\n");
- err = 5;
- return -1;
- }
- }
- rx_seq++;
- inner_process(ptlen);
- }
- return 0;
- }
- /* ---- ClientHello ---- */
- static unsigned char ch_buf[512];
- static unsigned int ch_n;
- static int ch_over;
- static void ch_u8(unsigned int v)
- {
- if (ch_n < sizeof(ch_buf))
- ch_buf[ch_n++] = (unsigned char)v;
- else
- ch_over = 1;
- }
- static void ch_u16(unsigned int v)
- {
- ch_u8(v >> 8);
- ch_u8(v);
- }
- static void ch_u24(unsigned int v)
- {
- ch_u8(v >> 16);
- ch_u8(v >> 8);
- ch_u8(v);
- }
- static void ch_bytes(const void *p, unsigned int n)
- {
- const unsigned char *b = p;
- while (n--)
- ch_u8(*b++);
- }
- static int build_client_hello(const char *sni)
- {
- unsigned char cr[32], sid[32], pub[32];
- unsigned int hostlen = 0, ext_pos, ext_start;
- while (sni[hostlen])
- hostlen++;
- if (hostlen == 0 || hostlen > 200)
- return -1;
- ch_n = 0;
- ch_over = 0;
- ch_u8(1); /* client_hello */
- 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(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);
- ext_pos = ch_n;
- ch_u16(0); /* extensions length (patched) */
- ext_start = ch_n;
- /* supported_versions */
- ch_u16(0x002b);
- ch_u16(5);
- ch_u8(4);
- ch_u16(0x0304);
- 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(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(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);
- ch_u16(0x0033);
- ch_u16(38);
- ch_u16(36);
- ch_u16(0x001d);
- ch_u16(32);
- ch_bytes(pub, 32);
- /* server_name */
- ch_u16(0x0000);
- ch_u16(2 + 1 + 2 + hostlen);
- ch_u16(1 + 2 + hostlen);
- ch_u8(0);
- ch_u16(hostlen);
- ch_bytes(sni, hostlen);
- /* application_layer_protocol_negotiation: http/1.1 */
- ch_u16(0x0010);
- ch_u16(2 + 1 + 8);
- ch_u16(1 + 8);
- ch_u8(8);
- ch_bytes("http/1.1", 8);
- if (ch_over)
- return -1;
- ch_buf[ext_pos] = (unsigned char)((ch_n - ext_start) >> 8);
- ch_buf[ext_pos + 1] = (unsigned char)(ch_n - ext_start);
- ch_buf[1] = (unsigned char)((ch_n - 4) >> 16);
- ch_buf[2] = (unsigned char)((ch_n - 4) >> 8);
- ch_buf[3] = (unsigned char)(ch_n - 4);
- return (int)ch_n;
- }
- /* ---- public API ---- */
- int tls_connect(const char *sni)
- {
- unsigned int n;
- unsigned int deadline;
- int r;
- phase = 0;
- rx_seq = tx_seq = 0;
- raw_len = raw_pos = 0;
- app_off = app_len = 0;
- eof = err = 0;
- 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)
- peer_mss = 536;
- sha256_init(&transcript);
- r = build_client_hello(sni);
- if (r <= 0) {
- u_puts("tls: bad host name\n");
- return -1;
- }
- n = (unsigned int)r;
- sha256_update(&transcript, ch_buf, n);
- if (send_plain(22, ch_buf, n) != 0) {
- u_puts("tls: send failed\n");
- return -1;
- }
- u_puts("tls: client hello sent (");
- u_putdec(n);
- u_puts(" bytes)\n");
- deadline = u_ticks_ms() + TLS_HS_DEADLINE_MS;
- while (phase != 2 && !err && !eof) {
- if (pump_record(deadline) != 0)
- break;
- }
- if (phase != 2) {
- u_puts("tls: handshake failed");
- if (err == 4)
- u_puts(" (no common protocol version or cipher suite)");
- else if (err == 2)
- u_puts(" (timeout)");
- else if (err == 1)
- u_puts(" (connection reset)");
- else if (eof)
- u_puts(" (connection closed)");
- u_putc('\n');
- return -1;
- }
- u_puts("tls: handshake done\n");
- return 0;
- }
- int tls_send(const void *buf, unsigned int len)
- {
- if (phase != 2 || len == 0 || len > TLS_APP_CHUNK)
- return -1;
- return send_record(23, buf, len, 0);
- }
- int tls_recv(void *buf, unsigned int max, unsigned int timeout_ms)
- {
- unsigned char *out = buf;
- unsigned int deadline = u_ticks_ms() + timeout_ms;
- for (;;) {
- unsigned int i, n;
- if (app_len) {
- n = app_len - app_off;
- if (n > max)
- n = max;
- for (i = 0; i < n; i++)
- out[i] = rec_pt[app_off + i];
- app_off += n;
- if (app_off == app_len)
- app_len = 0;
- return (int)n;
- }
- if (eof)
- return 0;
- if (err)
- return (err == 2) ? -1 : -2;
- if ((int)(u_ticks_ms() - deadline) >= 0)
- return -1;
- if (pump_record(deadline) != 0) {
- if (eof)
- return 0;
- return (err == 2) ? -1 : -2;
- }
- }
- }
- void tls_close(void)
- {
- static const unsigned char cn[2] = { 1, 0 };
- if (phase == 2 && !err && !eof)
- send_record(21, cn, 2, 0);
- phase = 0; /* no stale epoch on a later connection */
- }
- /* Forget the session without sending close_notify (the connection is being
- * dropped anyway, e.g. after a redirect), so the close costs no round trip. */
- void tls_abandon(void)
- {
- phase = 0;
- }
|