tls.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. /* Minimal TLS 1.3 client (RFC 8446) over the kernel TCP client:
  2. * X25519 key exchange, TLS_CHACHA20_POLY1305_SHA256, no certificate
  3. * verification (the leaf SHA-256 is printed for manual comparison).
  4. *
  5. * TLS 1.2 (RFC 5246) fallback for servers without 1.3: ECDHE over X25519 or
  6. * P-256 with ChaCha20-Poly1305 (RFC 7905) or AES-128-GCM (RFC 5289), the
  7. * extended master secret (RFC 7627) when the server offers it, and the
  8. * same unverified certificate handling. The same ClientHello offers both;
  9. * the ServerHello's supported_versions decides which state machine runs.
  10. *
  11. * The record layer is a small state machine; incoming records are
  12. * decrypted in chunks straight from the TCP stream (SYS_TCP_RECV) so a
  13. * 16 KB record never needs more than one plaintext buffer. Handshake
  14. * messages are fed byte-wise through a transcript-hashing parser, so a
  15. * large Certificate message is hashed as it flows past.
  16. *
  17. * Phase A trade-off: this authenticates nothing. See tls.h. */
  18. #include "ucmd_lib.h"
  19. #include "tls.h"
  20. #include "tls_sha256.h"
  21. #include "tls_chacha20.h"
  22. #include "tls_x25519.h"
  23. #include "tls_gcm.h"
  24. #include "tls_p256.h"
  25. #include "tls_rng.h"
  26. #define TLS_RAW_SIZE 2048
  27. #define TLS_REC_PT_MAX (16384 + 256)
  28. #define TLS_SH_MAX 1024
  29. #define TLS_TX_MAX 2048
  30. #define TLS_APP_CHUNK 1024
  31. #define TLS_HS_DEADLINE_MS 15000
  32. /* ---- state (one connection at a time) ---- */
  33. static unsigned char raw_buf[TLS_RAW_SIZE];
  34. static unsigned int raw_len, raw_pos;
  35. static unsigned char rec_pt[TLS_REC_PT_MAX]; /* decrypted record payload */
  36. static unsigned int app_off, app_len; /* pending app data in rec_pt */
  37. static unsigned char sh_buf[TLS_SH_MAX];
  38. static unsigned char tx_buf[TLS_TX_MAX];
  39. static unsigned int peer_mss = 536;
  40. static int phase; /* 0 clear, 1 hs keys, 2 app keys */
  41. static unsigned long long rx_seq, tx_seq;
  42. static unsigned char c_hs[32], s_hs[32], c_ap[32], s_ap[32];
  43. static unsigned char c_hs_key[32], s_hs_key[32], c_hs_iv[12], s_hs_iv[12];
  44. static unsigned char c_ap_key[32], s_ap_key[32], c_ap_iv[12], s_ap_iv[12];
  45. static unsigned char master_secret[32];
  46. static unsigned char x_priv[32];
  47. /* TLS 1.2 state */
  48. static int ver12; /* negotiated TLS 1.2 */
  49. static int rx_enc; /* 1.2: server CCS seen, records encrypted */
  50. static int ems; /* extended_master_secret echoed */
  51. static int cert_req; /* CertificateRequest received */
  52. static unsigned int suite; /* negotiated cipher suite */
  53. static int use_gcm; /* AES-128-GCM (else ChaCha20-Poly1305) */
  54. static unsigned char client_random[32], server_random[32];
  55. static unsigned char ms12[48];
  56. static unsigned int ske_group, ske_plen;
  57. static unsigned char ske_buf[160];
  58. static struct gcm_key c_gcm, s_gcm;
  59. static struct sha256_ctx transcript;
  60. static unsigned char fin_data[32];
  61. static int eof, err;
  62. /* handshake message parser */
  63. static unsigned int hm_type, hm_len, hm_got;
  64. /* Certificate streaming parser */
  65. 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 */
  66. static unsigned int cert_ctx_left, cert_hdr_got, cert_list_left;
  67. static unsigned int cert_cur_left, cert_seen;
  68. static unsigned int cert_ext_got, cert_ext_left;
  69. static struct sha256_ctx cert_hash;
  70. static const unsigned char zeros32[32];
  71. static const unsigned char sha256_empty[32] = {
  72. 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
  73. 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
  74. 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
  75. 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
  76. };
  77. /* ---- small helpers ---- */
  78. static int eq32(const unsigned char *a, const unsigned char *b)
  79. {
  80. unsigned int i, d = 0;
  81. for (i = 0; i < 32; i++)
  82. d |= a[i] ^ b[i];
  83. return d == 0;
  84. }
  85. static void transcript_hash(unsigned char out[32])
  86. {
  87. struct sha256_ctx t;
  88. sha256_copy(&t, &transcript);
  89. sha256_final(&t, out);
  90. }
  91. static void derive_secret_th(const unsigned char *secret, const char *label,
  92. const unsigned char th[32], unsigned char out[32])
  93. {
  94. hkdf_expand_label(secret, label, th, 32, out, 32);
  95. }
  96. static void derive_keys(const unsigned char *secret, unsigned char key[32],
  97. unsigned char iv[12])
  98. {
  99. hkdf_expand_label(secret, "key", 0, 0, key, 32);
  100. hkdf_expand_label(secret, "iv", 0, 0, iv, 12);
  101. }
  102. static void nonce_for(unsigned char nonce[12], const unsigned char iv[12],
  103. unsigned long long seq)
  104. {
  105. unsigned int i;
  106. for (i = 0; i < 12; i++)
  107. nonce[i] = iv[i];
  108. for (i = 0; i < 8; i++)
  109. nonce[11 - i] ^= (unsigned char)(seq >> (8 * i));
  110. }
  111. /* ---- raw TCP input ---- */
  112. static int raw_fill(unsigned int timeout)
  113. {
  114. int r = (int)u_syscall3(SYS_TCP_RECV, (int)raw_buf,
  115. (int)sizeof(raw_buf), (int)timeout);
  116. if (r > 0) {
  117. raw_len = (unsigned int)r;
  118. raw_pos = 0;
  119. return 0;
  120. }
  121. if (r == 0) {
  122. eof = 1;
  123. return -1;
  124. }
  125. err = (r == -2) ? 1 : 2;
  126. return -1;
  127. }
  128. static int raw_read(void *dst, unsigned int n, unsigned int deadline)
  129. {
  130. unsigned char *d = dst;
  131. while (n) {
  132. unsigned int avail = raw_len - raw_pos;
  133. unsigned int take, i;
  134. if (!avail) {
  135. int left;
  136. if (err || eof)
  137. return -1;
  138. left = (int)(deadline - u_ticks_ms());
  139. if (left <= 0) {
  140. err = 2;
  141. return -1;
  142. }
  143. if (raw_fill((unsigned int)left) != 0)
  144. return -1;
  145. continue;
  146. }
  147. take = avail < n ? avail : n;
  148. for (i = 0; i < take; i++)
  149. d[i] = raw_buf[raw_pos + i];
  150. raw_pos += take;
  151. d += take;
  152. n -= take;
  153. }
  154. return 0;
  155. }
  156. /* ---- record output ---- */
  157. static int tcp_send_all(const unsigned char *p, unsigned int n)
  158. {
  159. while (n) {
  160. unsigned int take = n < peer_mss ? n : peer_mss;
  161. if (u_syscall2(SYS_TCP_SEND, (int)p, (int)take) != 0)
  162. return -1;
  163. p += take;
  164. n -= take;
  165. }
  166. return 0;
  167. }
  168. static int send_plain(unsigned char type, const unsigned char *data,
  169. unsigned int len)
  170. {
  171. unsigned char hdr[5];
  172. hdr[0] = type;
  173. hdr[1] = 3;
  174. hdr[2] = 3;
  175. hdr[3] = (unsigned char)(len >> 8);
  176. hdr[4] = (unsigned char)len;
  177. if (tcp_send_all(hdr, 5) != 0)
  178. return -1;
  179. return tcp_send_all(data, len);
  180. }
  181. static void seq_be(unsigned char *p, unsigned long long seq)
  182. {
  183. unsigned int i;
  184. for (i = 0; i < 8; i++)
  185. p[i] = (unsigned char)(seq >> (56 - 8 * i));
  186. }
  187. /* TLS 1.2 AEAD record: the type is the outer type, AAD = seq || type ||
  188. * version || length; GCM puts the 8-byte explicit nonce (= seq) first. */
  189. static int send_record12(unsigned char type, const unsigned char *data,
  190. unsigned int len)
  191. {
  192. unsigned char nonce[12], aad[13], tag[16];
  193. unsigned int ex = use_gcm ? 8 : 0, rl = ex + len + 16, i;
  194. if (5 + rl > TLS_TX_MAX)
  195. return -1;
  196. tx_buf[0] = type;
  197. tx_buf[1] = 3;
  198. tx_buf[2] = 3;
  199. tx_buf[3] = (unsigned char)(rl >> 8);
  200. tx_buf[4] = (unsigned char)rl;
  201. seq_be(aad, tx_seq);
  202. aad[8] = type;
  203. aad[9] = 3;
  204. aad[10] = 3;
  205. aad[11] = (unsigned char)(len >> 8);
  206. aad[12] = (unsigned char)len;
  207. for (i = 0; i < len; i++)
  208. tx_buf[5 + ex + i] = data[i];
  209. if (use_gcm) {
  210. for (i = 0; i < 4; i++)
  211. nonce[i] = c_ap_iv[i];
  212. seq_be(nonce + 4, tx_seq);
  213. for (i = 0; i < 8; i++)
  214. tx_buf[5 + i] = nonce[4 + i];
  215. gcm_seal(&c_gcm, nonce, aad, 13, tx_buf + 5 + ex, len,
  216. tx_buf + 5 + ex, tag);
  217. } else {
  218. nonce_for(nonce, c_ap_iv, tx_seq);
  219. aead_seal(c_ap_key, nonce, aad, 13, tx_buf + 5, len,
  220. tx_buf + 5, tag);
  221. }
  222. for (i = 0; i < 16; i++)
  223. tx_buf[5 + ex + len + i] = tag[i];
  224. tx_seq++;
  225. return tcp_send_all(tx_buf, 5 + rl);
  226. }
  227. static int send_record(unsigned char inner, const unsigned char *data,
  228. unsigned int len, int use_hs)
  229. {
  230. const unsigned char *key = use_hs ? c_hs_key : c_ap_key;
  231. const unsigned char *iv = use_hs ? c_hs_iv : c_ap_iv;
  232. unsigned char nonce[12], aad[5], tag[16];
  233. unsigned int plen = len + 1, rl = plen + 16, i;
  234. if (ver12)
  235. return send_record12(inner, data, len);
  236. if (5 + rl > TLS_TX_MAX)
  237. return -1;
  238. for (i = 0; i < len; i++)
  239. tx_buf[5 + i] = data[i];
  240. tx_buf[5 + len] = inner;
  241. aad[0] = 23;
  242. aad[1] = 3;
  243. aad[2] = 3;
  244. aad[3] = (unsigned char)(rl >> 8);
  245. aad[4] = (unsigned char)rl;
  246. for (i = 0; i < 5; i++)
  247. tx_buf[i] = aad[i]; /* the record header on the wire */
  248. nonce_for(nonce, iv, tx_seq);
  249. aead_seal(key, nonce, aad, 5, tx_buf + 5, plen, tx_buf + 5, tag);
  250. for (i = 0; i < 16; i++)
  251. tx_buf[5 + plen + i] = tag[i];
  252. tx_seq++;
  253. return tcp_send_all(tx_buf, 5 + rl);
  254. }
  255. /* ---- handshake: ServerHello ---- */
  256. /* A ServerHello without supported_versions: TLS 1.2 (legacy_version 0x0303). */
  257. static void sh_parse12(unsigned int cipher)
  258. {
  259. unsigned int i;
  260. if (sh_buf[0] != 3 || sh_buf[1] != 3) {
  261. u_puts("tls: server offered neither TLS 1.3 nor 1.2\n");
  262. err = 4;
  263. return;
  264. }
  265. if (cipher != 0xc02f && cipher != 0xc02b && cipher != 0xcca8 &&
  266. cipher != 0xcca9) {
  267. u_puts("tls: server chose an unsupported TLS 1.2 cipher suite\n");
  268. err = 4;
  269. return;
  270. }
  271. for (i = 0; i < 32; i++)
  272. server_random[i] = sh_buf[2 + i];
  273. ver12 = 1;
  274. suite = cipher;
  275. use_gcm = (cipher == 0xc02f || cipher == 0xc02b);
  276. u_puts("tls: server hello, TLS 1.2 ");
  277. u_puts(use_gcm ? "ECDHE AES-128-GCM" : "ECDHE CHACHA20-POLY1305");
  278. u_puts(ems ? " (extended master secret)\n" : "\n");
  279. }
  280. static void sh_parse(unsigned int len)
  281. {
  282. const unsigned char *p = sh_buf;
  283. unsigned char shared[32], early[32], d1[32], d2[32], hs[32], th[32];
  284. unsigned char server_key[32];
  285. unsigned int o, sid, cipher, elen, i, j;
  286. int have_key = 0, have_ver = 0;
  287. if (err)
  288. return;
  289. if (len < 2 + 32 + 1 + 2 + 1) {
  290. err = 3;
  291. return;
  292. }
  293. o = 2 + 32;
  294. sid = p[o++];
  295. if (o + sid + 3 > len) {
  296. err = 3;
  297. return;
  298. }
  299. o += sid;
  300. cipher = ((unsigned int)p[o] << 8) | p[o + 1];
  301. o += 2;
  302. o++; /* legacy_compression_method */
  303. elen = 0;
  304. if (o + 2 <= len) {
  305. elen = ((unsigned int)p[o] << 8) | p[o + 1];
  306. o += 2;
  307. }
  308. if (o + elen != len) {
  309. err = 3;
  310. return;
  311. }
  312. for (i = 0; i + 4 <= elen; ) {
  313. unsigned int et = ((unsigned int)p[o + i] << 8) | p[o + i + 1];
  314. unsigned int el = ((unsigned int)p[o + i + 2] << 8) |
  315. p[o + i + 3];
  316. const unsigned char *d = p + o + i + 4;
  317. if (i + 4 + el > elen) {
  318. err = 3;
  319. return;
  320. }
  321. if (et == 0x0033 && el == 36 && d[0] == 0x00 && d[1] == 0x1d &&
  322. d[2] == 0x00 && d[3] == 32) {
  323. for (j = 0; j < 32; j++)
  324. server_key[j] = d[4 + j];
  325. have_key = 1;
  326. }
  327. if (et == 0x002b && el == 2 && d[0] == 3 && d[1] == 4)
  328. have_ver = 1;
  329. if (et == 0x0017)
  330. ems = 1;
  331. i += 4 + el;
  332. }
  333. if (!have_ver) {
  334. sh_parse12(cipher);
  335. return;
  336. }
  337. if (cipher != 0x1303) {
  338. u_puts("tls: server chose another cipher suite\n");
  339. err = 4;
  340. return;
  341. }
  342. if (!have_key) {
  343. u_puts("tls: server sent no X25519 key share\n");
  344. err = 4;
  345. return;
  346. }
  347. x25519(shared, x_priv, server_key);
  348. hkdf_extract(0, 0, zeros32, 32, early);
  349. derive_secret_th(early, "derived", sha256_empty, d1);
  350. hkdf_extract(d1, 32, shared, 32, hs);
  351. transcript_hash(th);
  352. derive_secret_th(hs, "c hs traffic", th, c_hs);
  353. derive_secret_th(hs, "s hs traffic", th, s_hs);
  354. derive_keys(c_hs, c_hs_key, c_hs_iv);
  355. derive_keys(s_hs, s_hs_key, s_hs_iv);
  356. derive_secret_th(hs, "derived", sha256_empty, d2);
  357. hkdf_extract(d2, 32, zeros32, 32, master_secret);
  358. phase = 1;
  359. u_puts("tls: server hello, TLS_CHACHA20_POLY1305_SHA256 + X25519\n");
  360. }
  361. /* ---- handshake: Certificate (streamed, fingerprint only) ---- */
  362. static unsigned char cert_lenbuf[3];
  363. static void cert_msg_done(void)
  364. {
  365. unsigned char fp[32];
  366. unsigned int i;
  367. if (cert_stage != 9 || cert_seen == 0) {
  368. err = 3;
  369. return;
  370. }
  371. sha256_final(&cert_hash, fp);
  372. u_puts("tls: cert[0] sha256=");
  373. for (i = 0; i < 32; i++)
  374. u_put_hex2(fp[i]);
  375. u_putc('\n');
  376. u_puts("tls: chain has ");
  377. u_putdec(cert_seen);
  378. u_puts(" certificate(s), NOT verified\n");
  379. }
  380. static void cert_byte(unsigned char b)
  381. {
  382. switch (cert_stage) {
  383. case 0:
  384. sha256_init(&cert_hash);
  385. if (ver12) { /* no request context */
  386. cert_stage = 2;
  387. cert_byte(b);
  388. break;
  389. }
  390. cert_ctx_left = b;
  391. cert_stage = cert_ctx_left ? 1 : 2;
  392. break;
  393. case 1:
  394. if (--cert_ctx_left == 0)
  395. cert_stage = 2;
  396. break;
  397. case 2:
  398. cert_lenbuf[cert_hdr_got++] = b;
  399. if (cert_hdr_got == 3) {
  400. cert_hdr_got = 0;
  401. cert_list_left = ((unsigned int)cert_lenbuf[0] << 16) |
  402. ((unsigned int)cert_lenbuf[1] << 8) |
  403. cert_lenbuf[2];
  404. cert_stage = cert_list_left ? 3 : 9;
  405. }
  406. break;
  407. case 3:
  408. cert_lenbuf[cert_hdr_got++] = b;
  409. cert_list_left--;
  410. if (cert_hdr_got == 3) {
  411. cert_hdr_got = 0;
  412. cert_cur_left = ((unsigned int)cert_lenbuf[0] << 16) |
  413. ((unsigned int)cert_lenbuf[1] << 8) |
  414. cert_lenbuf[2];
  415. if (cert_cur_left == 0) {
  416. cert_seen++;
  417. cert_ext_got = 0;
  418. cert_stage = ver12 ? (cert_list_left ? 3 : 9) : 5;
  419. } else {
  420. cert_stage = 4;
  421. }
  422. }
  423. break;
  424. case 4:
  425. if (cert_seen == 0)
  426. sha256_update(&cert_hash, &b, 1);
  427. cert_cur_left--;
  428. cert_list_left--;
  429. if (cert_cur_left == 0) {
  430. cert_seen++;
  431. cert_ext_got = 0;
  432. cert_stage = ver12 ? (cert_list_left ? 3 : 9) : 5;
  433. }
  434. break;
  435. case 5: /* per-entry extensions */
  436. cert_lenbuf[cert_ext_got++] = b;
  437. cert_list_left--;
  438. if (cert_ext_got == 2) {
  439. cert_ext_left = ((unsigned int)cert_lenbuf[0] << 8) |
  440. cert_lenbuf[1];
  441. cert_stage = cert_ext_left ? 6 :
  442. (cert_list_left ? 3 : 9);
  443. }
  444. break;
  445. case 6:
  446. cert_ext_left--;
  447. cert_list_left--;
  448. if (cert_ext_left == 0)
  449. cert_stage = cert_list_left ? 3 : 9;
  450. break;
  451. default:
  452. break;
  453. }
  454. }
  455. /* ---- handshake message parser ---- */
  456. static void hs_msg_complete(void);
  457. static void hs_byte(unsigned char b)
  458. {
  459. unsigned int body_pos;
  460. if (hm_got < 4) {
  461. if (hm_got == 0) {
  462. hm_type = b;
  463. hm_len = 0;
  464. } else {
  465. hm_len = (hm_len << 8) | b;
  466. }
  467. if (hm_type != 20)
  468. sha256_update(&transcript, &b, 1);
  469. hm_got++;
  470. if (hm_got == 4 && hm_len == 0)
  471. hs_msg_complete();
  472. return;
  473. }
  474. if (hm_got - 4 >= hm_len) { /* defensive */
  475. hs_msg_complete();
  476. return;
  477. }
  478. body_pos = hm_got - 4;
  479. if (hm_type != 20)
  480. sha256_update(&transcript, &b, 1);
  481. switch (hm_type) {
  482. case 2:
  483. if (body_pos < TLS_SH_MAX)
  484. sh_buf[body_pos] = b;
  485. else
  486. err = 3;
  487. break;
  488. case 11:
  489. cert_byte(b);
  490. break;
  491. case 12: /* 1.2 ServerKeyExchange */
  492. if (body_pos < sizeof(ske_buf))
  493. ske_buf[body_pos] = b;
  494. break;
  495. case 15:
  496. break; /* CertificateVerify: not checked */
  497. case 20:
  498. if (body_pos < 32)
  499. fin_data[body_pos] = b;
  500. break;
  501. default:
  502. break; /* EE and post-handshake: skip */
  503. }
  504. hm_got++;
  505. if (hm_got - 4 == hm_len)
  506. hs_msg_complete();
  507. }
  508. static void hs_finish(unsigned int len);
  509. static void hs_finish12(unsigned int len);
  510. static void ske_parse(unsigned int len);
  511. static void hello_done12(void);
  512. static void hs_msg_complete(void)
  513. {
  514. unsigned int type = hm_type, len = hm_len;
  515. hm_got = 0;
  516. if (ver12 && phase == 2)
  517. return; /* HelloRequest etc.: ignored */
  518. switch (type) {
  519. case 2:
  520. sh_parse(len);
  521. break;
  522. case 11:
  523. cert_msg_done();
  524. break;
  525. case 12:
  526. if (ver12)
  527. ske_parse(len);
  528. break;
  529. case 13:
  530. if (ver12)
  531. cert_req = 1;
  532. break;
  533. case 14:
  534. if (ver12)
  535. hello_done12();
  536. break;
  537. case 20:
  538. if (ver12)
  539. hs_finish12(len);
  540. else
  541. hs_finish(len);
  542. break;
  543. default:
  544. break;
  545. }
  546. }
  547. static void hs_finish(unsigned int len)
  548. {
  549. unsigned char th[32], fk[32], expect[32], msg[36];
  550. if (err)
  551. return;
  552. if (len != 32 || phase != 1) {
  553. err = 3;
  554. return;
  555. }
  556. transcript_hash(th);
  557. hkdf_expand_label(s_hs, "finished", 0, 0, fk, 32);
  558. hmac_sha256(fk, 32, th, 32, expect);
  559. if (!eq32(expect, fin_data)) {
  560. u_puts("tls: bad server Finished (handshake mismatch)\n");
  561. err = 5;
  562. return;
  563. }
  564. /* Transcript += server Finished. */
  565. msg[0] = 20;
  566. msg[1] = 0;
  567. msg[2] = 0;
  568. msg[3] = 32;
  569. sha256_update(&transcript, msg, 4);
  570. sha256_update(&transcript, fin_data, 32);
  571. /* Application traffic secrets (context: CH .. server Finished). */
  572. transcript_hash(th);
  573. derive_secret_th(master_secret, "c ap traffic", th, c_ap);
  574. derive_secret_th(master_secret, "s ap traffic", th, s_ap);
  575. derive_keys(c_ap, c_ap_key, c_ap_iv);
  576. derive_keys(s_ap, s_ap_key, s_ap_iv);
  577. /* Our Finished, under the handshake keys. */
  578. hkdf_expand_label(c_hs, "finished", 0, 0, fk, 32);
  579. hmac_sha256(fk, 32, th, 32, expect);
  580. msg[0] = 20;
  581. msg[1] = 0;
  582. msg[2] = 0;
  583. msg[3] = 32;
  584. {
  585. unsigned int i;
  586. for (i = 0; i < 32; i++)
  587. msg[4 + i] = expect[i];
  588. }
  589. {
  590. static const unsigned char ccs[1] = { 1 };
  591. send_plain(20, ccs, 1); /* middlebox compatibility */
  592. }
  593. if (send_record(22, msg, 36, 1) != 0) {
  594. err = 1;
  595. return;
  596. }
  597. phase = 2;
  598. rx_seq = 0;
  599. tx_seq = 0;
  600. }
  601. /* ---- TLS 1.2 handshake ---- */
  602. static void ske_parse(unsigned int len)
  603. {
  604. const unsigned char *p = ske_buf;
  605. if (err)
  606. return;
  607. if (len > sizeof(ske_buf))
  608. len = sizeof(ske_buf);
  609. if (len < 4 || p[0] != 3 || 4 + (unsigned int)p[3] > len) {
  610. u_puts("tls: unsupported ServerKeyExchange (need named-curve ECDHE)\n");
  611. err = 4;
  612. return;
  613. }
  614. ske_group = ((unsigned int)p[1] << 8) | p[2];
  615. ske_plen = p[3];
  616. if (!((ske_group == 0x001d && ske_plen == 32) ||
  617. (ske_group == 0x0017 && ske_plen == 65))) {
  618. u_puts("tls: server chose an unsupported ECDHE group\n");
  619. err = 4;
  620. return;
  621. }
  622. /* The signature over the parameters is not verified (phase A). */
  623. }
  624. static int send_hs12(const unsigned char *msg, unsigned int len)
  625. {
  626. sha256_update(&transcript, msg, len);
  627. return send_plain(22, msg, len);
  628. }
  629. static void hello_done12(void)
  630. {
  631. unsigned char priv[32], pms[32], msg[4 + 1 + 65], th[32], kb[88];
  632. unsigned char seed[64];
  633. unsigned int publen, i;
  634. if (err)
  635. return;
  636. if (ske_plen == 0) {
  637. u_puts("tls: no ServerKeyExchange (RSA key exchange unsupported)\n");
  638. err = 4;
  639. return;
  640. }
  641. if (cert_req) { /* empty client Certificate */
  642. static const unsigned char empty[7] = { 11, 0, 0, 3, 0, 0, 0 };
  643. if (send_hs12(empty, 7) != 0) {
  644. err = 1;
  645. return;
  646. }
  647. }
  648. tls_random(priv, 32);
  649. if (ske_group == 0x001d) {
  650. x25519(msg + 5, priv, x25519_base_point);
  651. x25519(pms, priv, ske_buf + 4);
  652. publen = 32;
  653. } else {
  654. priv[0] &= 0x7f; /* keep the scalar below n */
  655. if (p256_public(priv, msg + 5) != 0 ||
  656. p256_shared(priv, ske_buf + 4, 65, pms) != 0) {
  657. u_puts("tls: bad P-256 server key\n");
  658. err = 5;
  659. return;
  660. }
  661. publen = 65;
  662. }
  663. msg[0] = 16; /* ClientKeyExchange */
  664. msg[1] = 0;
  665. msg[2] = 0;
  666. msg[3] = (unsigned char)(1 + publen);
  667. msg[4] = (unsigned char)publen;
  668. if (send_hs12(msg, 5 + publen) != 0) {
  669. err = 1;
  670. return;
  671. }
  672. transcript_hash(th); /* session hash: CH .. CKE */
  673. if (ems)
  674. tls12_prf(pms, 32, "extended master secret", th, 32, 0, 0,
  675. ms12, 48);
  676. else
  677. tls12_prf(pms, 32, "master secret", client_random, 32,
  678. server_random, 32, ms12, 48);
  679. tls12_prf(ms12, 48, "key expansion", server_random, 32, client_random,
  680. 32, kb, sizeof(kb));
  681. if (use_gcm) {
  682. for (i = 0; i < 4; i++) {
  683. c_ap_iv[i] = kb[32 + i];
  684. s_ap_iv[i] = kb[36 + i];
  685. }
  686. gcm_setkey(&c_gcm, kb);
  687. gcm_setkey(&s_gcm, kb + 16);
  688. } else {
  689. for (i = 0; i < 32; i++) {
  690. c_ap_key[i] = kb[i];
  691. s_ap_key[i] = kb[32 + i];
  692. }
  693. for (i = 0; i < 12; i++) {
  694. c_ap_iv[i] = kb[64 + i];
  695. s_ap_iv[i] = kb[76 + i];
  696. }
  697. }
  698. {
  699. static const unsigned char ccs[1] = { 1 };
  700. if (send_plain(20, ccs, 1) != 0) {
  701. err = 1;
  702. return;
  703. }
  704. }
  705. tx_seq = 0;
  706. seed[0] = 20; /* client Finished */
  707. seed[1] = 0;
  708. seed[2] = 0;
  709. seed[3] = 12;
  710. tls12_prf(ms12, 48, "client finished", th, 32, 0, 0, seed + 4, 12);
  711. sha256_update(&transcript, seed, 16);
  712. if (send_record12(22, seed, 16) != 0) {
  713. err = 1;
  714. return;
  715. }
  716. phase = 1; /* waiting for CCS + Finished */
  717. for (i = 0; i < 32; i++)
  718. pms[i] = priv[i] = 0;
  719. }
  720. static void hs_finish12(unsigned int len)
  721. {
  722. unsigned char th[32], expect[12];
  723. unsigned int i, d = 0;
  724. if (err)
  725. return;
  726. if (len != 12 || phase != 1 || !rx_enc) {
  727. err = 3;
  728. return;
  729. }
  730. transcript_hash(th);
  731. tls12_prf(ms12, 48, "server finished", th, 32, 0, 0, expect, 12);
  732. for (i = 0; i < 12; i++)
  733. d |= expect[i] ^ fin_data[i];
  734. if (d) {
  735. u_puts("tls: bad server Finished (handshake mismatch)\n");
  736. err = 5;
  737. return;
  738. }
  739. phase = 2;
  740. }
  741. /* ---- record input ---- */
  742. static void rec_dispatch(unsigned char type, unsigned int len);
  743. static void inner_process(unsigned int len)
  744. {
  745. unsigned int i = len;
  746. unsigned char type;
  747. while (i > 0 && rec_pt[i - 1] == 0)
  748. i--;
  749. if (i == 0) {
  750. err = 3;
  751. return;
  752. }
  753. type = rec_pt[i - 1];
  754. rec_dispatch(type, i - 1);
  755. }
  756. static void rec_dispatch(unsigned char type, unsigned int len)
  757. {
  758. unsigned int i;
  759. switch (type) {
  760. case 22:
  761. for (i = 0; i < len && !err; i++)
  762. hs_byte(rec_pt[i]);
  763. break;
  764. case 23:
  765. app_off = 0;
  766. app_len = len;
  767. break;
  768. case 21:
  769. if (len >= 2) {
  770. if (rec_pt[1] == 0) {
  771. eof = 1; /* close_notify */
  772. } else {
  773. u_puts("tls: alert ");
  774. u_putdec(rec_pt[1]);
  775. u_putc('\n');
  776. err = 6;
  777. }
  778. }
  779. break;
  780. default:
  781. break; /* inner CCS etc. */
  782. }
  783. }
  784. static int pump_record12(const unsigned char hdr[5], unsigned int len,
  785. unsigned int deadline)
  786. {
  787. struct aead_open_ctx a;
  788. struct gcm_ctx g;
  789. unsigned char nonce[12], aad[13], tag[16], expect[16];
  790. unsigned int ex = use_gcm ? 8 : 0, ptlen, off = 0, i, d = 0;
  791. if ((hdr[0] != 22 && hdr[0] != 23 && hdr[0] != 21) ||
  792. len < ex + 16 || len - ex - 16 > TLS_REC_PT_MAX) {
  793. err = 3;
  794. return -1;
  795. }
  796. ptlen = len - ex - 16;
  797. if (use_gcm) {
  798. for (i = 0; i < 4; i++)
  799. nonce[i] = s_ap_iv[i];
  800. if (raw_read(nonce + 4, 8, deadline) != 0)
  801. return -1;
  802. } else {
  803. nonce_for(nonce, s_ap_iv, rx_seq);
  804. }
  805. seq_be(aad, rx_seq);
  806. aad[8] = hdr[0];
  807. aad[9] = 3;
  808. aad[10] = 3;
  809. aad[11] = (unsigned char)(ptlen >> 8);
  810. aad[12] = (unsigned char)ptlen;
  811. if (use_gcm)
  812. gcm_open_init(&g, &s_gcm, nonce, aad, 13);
  813. else
  814. aead_open_init(&a, s_ap_key, nonce, aad, 13);
  815. while (off < ptlen) {
  816. unsigned int n = ptlen - off;
  817. if (n > 1024)
  818. n = 1024;
  819. if (raw_read(rec_pt + off, n, deadline) != 0)
  820. return -1;
  821. if (use_gcm)
  822. gcm_open_update(&g, rec_pt + off, rec_pt + off, n);
  823. else
  824. aead_open_update(&a, rec_pt + off, rec_pt + off, n);
  825. off += n;
  826. }
  827. if (raw_read(tag, 16, deadline) != 0)
  828. return -1;
  829. if (use_gcm)
  830. gcm_open_tag(&g, expect);
  831. else
  832. aead_open_tag(&a, expect);
  833. for (i = 0; i < 16; i++)
  834. d |= tag[i] ^ expect[i];
  835. if (d) {
  836. u_puts("tls: record authentication failed\n");
  837. err = 5;
  838. return -1;
  839. }
  840. rx_seq++;
  841. rec_dispatch(hdr[0], ptlen);
  842. return 0;
  843. }
  844. static int pump_record(unsigned int deadline)
  845. {
  846. unsigned char hdr[5], tag[16], expect[16];
  847. unsigned int len, i;
  848. if (err || eof)
  849. return -1;
  850. if (raw_read(hdr, 5, deadline) != 0)
  851. return -1;
  852. len = ((unsigned int)hdr[3] << 8) | hdr[4];
  853. if (hdr[0] == 20) { /* CCS (middlebox mode) */
  854. if (len > 4 || raw_read(rec_pt, len, deadline) != 0) {
  855. err = 3;
  856. return -1;
  857. }
  858. if (ver12) { /* 1.2: server starts encrypting */
  859. if (phase != 1 || rx_enc) {
  860. err = 3;
  861. return -1;
  862. }
  863. rx_enc = 1;
  864. rx_seq = 0;
  865. }
  866. return 0;
  867. }
  868. if (hdr[0] == 21) { /* plaintext alert */
  869. if (len != 2 || raw_read(rec_pt, 2, deadline) != 0) {
  870. err = 3;
  871. return -1;
  872. }
  873. u_puts("tls: server alert: ");
  874. switch (rec_pt[1]) {
  875. case 40:
  876. u_puts("handshake failure");
  877. break;
  878. case 47:
  879. u_puts("illegal parameter");
  880. break;
  881. case 70:
  882. u_puts("protocol version (the server may not support TLS 1.3)");
  883. break;
  884. case 80:
  885. u_puts("internal error");
  886. break;
  887. default:
  888. u_puts("description ");
  889. u_putdec(rec_pt[1]);
  890. break;
  891. }
  892. u_puts(" (level ");
  893. u_putdec(rec_pt[0]);
  894. u_puts(")\n");
  895. err = 4;
  896. return -1;
  897. }
  898. if (ver12 ? !rx_enc : phase == 0) {
  899. /* Plaintext handshake: one ServerHello record for 1.3, the whole
  900. * 1.2 server flight (certificates included) otherwise. */
  901. if (hdr[0] != 22 || len > TLS_REC_PT_MAX) {
  902. err = 3;
  903. return -1;
  904. }
  905. if (raw_read(rec_pt, len, deadline) != 0)
  906. return -1;
  907. for (i = 0; i < len && !err; i++)
  908. hs_byte(rec_pt[i]);
  909. return 0;
  910. }
  911. if (ver12)
  912. return pump_record12(hdr, len, deadline);
  913. if (hdr[0] != 23 || len < 17 || len > TLS_REC_PT_MAX) {
  914. err = 3;
  915. return -1;
  916. }
  917. {
  918. const unsigned char *key = (phase == 1) ? s_hs_key : s_ap_key;
  919. const unsigned char *iv = (phase == 1) ? s_hs_iv : s_ap_iv;
  920. struct aead_open_ctx a;
  921. unsigned char nonce[12];
  922. unsigned int ptlen = len - 16, off = 0;
  923. nonce_for(nonce, iv, rx_seq);
  924. aead_open_init(&a, key, nonce, hdr, 5);
  925. while (off < ptlen) {
  926. unsigned int n = ptlen - off;
  927. if (n > 1024)
  928. n = 1024;
  929. if (raw_read(rec_pt + off, n, deadline) != 0)
  930. return -1;
  931. aead_open_update(&a, rec_pt + off, rec_pt + off, n);
  932. off += n;
  933. }
  934. if (raw_read(tag, 16, deadline) != 0)
  935. return -1;
  936. aead_open_tag(&a, expect);
  937. for (i = 0; i < 16; i++) {
  938. if (tag[i] != expect[i]) {
  939. u_puts("tls: record authentication failed\n");
  940. err = 5;
  941. return -1;
  942. }
  943. }
  944. rx_seq++;
  945. inner_process(ptlen);
  946. }
  947. return 0;
  948. }
  949. /* ---- ClientHello ---- */
  950. static unsigned char ch_buf[512];
  951. static unsigned int ch_n;
  952. static int ch_over;
  953. static void ch_u8(unsigned int v)
  954. {
  955. if (ch_n < sizeof(ch_buf))
  956. ch_buf[ch_n++] = (unsigned char)v;
  957. else
  958. ch_over = 1;
  959. }
  960. static void ch_u16(unsigned int v)
  961. {
  962. ch_u8(v >> 8);
  963. ch_u8(v);
  964. }
  965. static void ch_u24(unsigned int v)
  966. {
  967. ch_u8(v >> 16);
  968. ch_u8(v >> 8);
  969. ch_u8(v);
  970. }
  971. static void ch_bytes(const void *p, unsigned int n)
  972. {
  973. const unsigned char *b = p;
  974. while (n--)
  975. ch_u8(*b++);
  976. }
  977. static int build_client_hello(const char *sni)
  978. {
  979. unsigned char cr[32], sid[32], pub[32];
  980. unsigned int hostlen = 0, ext_pos, ext_start;
  981. while (sni[hostlen])
  982. hostlen++;
  983. if (hostlen == 0 || hostlen > 200)
  984. return -1;
  985. ch_n = 0;
  986. ch_over = 0;
  987. ch_u8(1); /* client_hello */
  988. ch_u24(0); /* length (patched below) */
  989. ch_u16(0x0303); /* legacy_version */
  990. tls_random(cr, 32);
  991. for (hostlen = 0; hostlen < 32; hostlen++)
  992. client_random[hostlen] = cr[hostlen];
  993. hostlen = 0;
  994. while (sni[hostlen])
  995. hostlen++;
  996. ch_bytes(cr, 32);
  997. tls_random(sid, 32);
  998. ch_u8(32); /* legacy_session_id */
  999. ch_bytes(sid, 32);
  1000. ch_u16(10); /* cipher suites */
  1001. ch_u16(0x1303); /* TLS 1.3 */
  1002. ch_u16(0xcca9); /* 1.2 ECDHE-ECDSA-CHACHA20-POLY1305 */
  1003. ch_u16(0xcca8); /* 1.2 ECDHE-RSA-CHACHA20-POLY1305 */
  1004. ch_u16(0xc02b); /* 1.2 ECDHE-ECDSA-AES128-GCM-SHA256 */
  1005. ch_u16(0xc02f); /* 1.2 ECDHE-RSA-AES128-GCM-SHA256 */
  1006. ch_u8(1); /* compression */
  1007. ch_u8(0);
  1008. ext_pos = ch_n;
  1009. ch_u16(0); /* extensions length (patched) */
  1010. ext_start = ch_n;
  1011. /* supported_versions */
  1012. ch_u16(0x002b);
  1013. ch_u16(5);
  1014. ch_u8(4);
  1015. ch_u16(0x0304);
  1016. ch_u16(0x0303);
  1017. /* supported_groups: P-256 only for TLS 1.2 servers without X25519
  1018. * (the 1.3 key share stays X25519; there is no HelloRetryRequest). */
  1019. ch_u16(0x000a);
  1020. ch_u16(6);
  1021. ch_u16(4);
  1022. ch_u16(0x001d);
  1023. ch_u16(0x0017);
  1024. /* ec_point_formats: uncompressed */
  1025. ch_u16(0x000b);
  1026. ch_u16(2);
  1027. ch_u8(1);
  1028. ch_u8(0);
  1029. /* extended_master_secret, renegotiation_info (empty) */
  1030. ch_u16(0x0017);
  1031. ch_u16(0);
  1032. ch_u16(0xff01);
  1033. ch_u16(1);
  1034. ch_u8(0);
  1035. /* signature_algorithms: advertise the ECDSA curves used by modern
  1036. * site certificates (nd-aktuell.de uses a P-384 leaf) as well as the
  1037. * RSA-PSS schemes. Signatures are not verified in phase A, but the
  1038. * server refuses the handshake when its certificate's scheme is not
  1039. * listed. */
  1040. ch_u16(0x000d);
  1041. ch_u16(20);
  1042. ch_u16(18);
  1043. ch_u16(0x0403); /* ecdsa_secp256r1_sha256 */
  1044. ch_u16(0x0503); /* ecdsa_secp384r1_sha384 */
  1045. ch_u16(0x0603); /* ecdsa_secp521r1_sha512 */
  1046. ch_u16(0x0804); /* rsa_pss_rsae_sha256 */
  1047. ch_u16(0x0805); /* rsa_pss_rsae_sha384 */
  1048. ch_u16(0x0806); /* rsa_pss_rsae_sha512 */
  1049. ch_u16(0x0401); /* rsa_pkcs1_sha256 (TLS 1.2) */
  1050. ch_u16(0x0501); /* rsa_pkcs1_sha384 */
  1051. ch_u16(0x0601); /* rsa_pkcs1_sha512 */
  1052. /* key_share */
  1053. tls_random(x_priv, 32);
  1054. x25519(pub, x_priv, x25519_base_point);
  1055. ch_u16(0x0033);
  1056. ch_u16(38);
  1057. ch_u16(36);
  1058. ch_u16(0x001d);
  1059. ch_u16(32);
  1060. ch_bytes(pub, 32);
  1061. /* server_name */
  1062. ch_u16(0x0000);
  1063. ch_u16(2 + 1 + 2 + hostlen);
  1064. ch_u16(1 + 2 + hostlen);
  1065. ch_u8(0);
  1066. ch_u16(hostlen);
  1067. ch_bytes(sni, hostlen);
  1068. /* application_layer_protocol_negotiation: http/1.1 */
  1069. ch_u16(0x0010);
  1070. ch_u16(2 + 1 + 8);
  1071. ch_u16(1 + 8);
  1072. ch_u8(8);
  1073. ch_bytes("http/1.1", 8);
  1074. if (ch_over)
  1075. return -1;
  1076. ch_buf[ext_pos] = (unsigned char)((ch_n - ext_start) >> 8);
  1077. ch_buf[ext_pos + 1] = (unsigned char)(ch_n - ext_start);
  1078. ch_buf[1] = (unsigned char)((ch_n - 4) >> 16);
  1079. ch_buf[2] = (unsigned char)((ch_n - 4) >> 8);
  1080. ch_buf[3] = (unsigned char)(ch_n - 4);
  1081. return (int)ch_n;
  1082. }
  1083. /* ---- public API ---- */
  1084. int tls_connect(const char *sni)
  1085. {
  1086. unsigned int n;
  1087. unsigned int deadline;
  1088. int r;
  1089. phase = 0;
  1090. rx_seq = tx_seq = 0;
  1091. raw_len = raw_pos = 0;
  1092. app_off = app_len = 0;
  1093. eof = err = 0;
  1094. hm_got = 0;
  1095. cert_stage = cert_seen = 0;
  1096. cert_hdr_got = 0;
  1097. ver12 = rx_enc = ems = cert_req = 0;
  1098. suite = 0;
  1099. use_gcm = 0;
  1100. ske_plen = ske_group = 0;
  1101. peer_mss = (unsigned int)u_syscall0(SYS_TCP_PEER_MSS);
  1102. if (peer_mss < 64 || peer_mss > 1460)
  1103. peer_mss = 536;
  1104. sha256_init(&transcript);
  1105. r = build_client_hello(sni);
  1106. if (r <= 0) {
  1107. u_puts("tls: bad host name\n");
  1108. return -1;
  1109. }
  1110. n = (unsigned int)r;
  1111. sha256_update(&transcript, ch_buf, n);
  1112. if (send_plain(22, ch_buf, n) != 0) {
  1113. u_puts("tls: send failed\n");
  1114. return -1;
  1115. }
  1116. u_puts("tls: client hello sent (");
  1117. u_putdec(n);
  1118. u_puts(" bytes)\n");
  1119. deadline = u_ticks_ms() + TLS_HS_DEADLINE_MS;
  1120. while (phase != 2 && !err && !eof) {
  1121. if (pump_record(deadline) != 0)
  1122. break;
  1123. }
  1124. if (phase != 2) {
  1125. u_puts("tls: handshake failed");
  1126. if (err == 4)
  1127. u_puts(" (no common protocol version or cipher suite)");
  1128. else if (err == 2)
  1129. u_puts(" (timeout)");
  1130. else if (err == 1)
  1131. u_puts(" (connection reset)");
  1132. else if (eof)
  1133. u_puts(" (connection closed)");
  1134. u_putc('\n');
  1135. return -1;
  1136. }
  1137. u_puts("tls: handshake done\n");
  1138. return 0;
  1139. }
  1140. int tls_send(const void *buf, unsigned int len)
  1141. {
  1142. if (phase != 2 || len == 0 || len > TLS_APP_CHUNK)
  1143. return -1;
  1144. return send_record(23, buf, len, 0);
  1145. }
  1146. int tls_recv(void *buf, unsigned int max, unsigned int timeout_ms)
  1147. {
  1148. unsigned char *out = buf;
  1149. unsigned int deadline = u_ticks_ms() + timeout_ms;
  1150. for (;;) {
  1151. unsigned int i, n;
  1152. if (app_len) {
  1153. n = app_len - app_off;
  1154. if (n > max)
  1155. n = max;
  1156. for (i = 0; i < n; i++)
  1157. out[i] = rec_pt[app_off + i];
  1158. app_off += n;
  1159. if (app_off == app_len)
  1160. app_len = 0;
  1161. return (int)n;
  1162. }
  1163. if (eof)
  1164. return 0;
  1165. if (err)
  1166. return (err == 2) ? -1 : -2;
  1167. if ((int)(u_ticks_ms() - deadline) >= 0)
  1168. return -1;
  1169. if (pump_record(deadline) != 0) {
  1170. if (eof)
  1171. return 0;
  1172. return (err == 2) ? -1 : -2;
  1173. }
  1174. }
  1175. }
  1176. void tls_close(void)
  1177. {
  1178. static const unsigned char cn[2] = { 1, 0 };
  1179. if (phase == 2 && !err && !eof)
  1180. send_record(21, cn, 2, 0);
  1181. phase = 0; /* no stale epoch on a later connection */
  1182. }
  1183. /* Forget the session without sending close_notify (the connection is being
  1184. * dropped anyway, e.g. after a redirect), so the close costs no round trip. */
  1185. void tls_abandon(void)
  1186. {
  1187. phase = 0;
  1188. }