tls-record.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*
  2. * Embedded Linux library
  3. *
  4. * Copyright (C) 2015 Intel Corporation. All rights reserved.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23. #define _GNU_SOURCE
  24. #include <alloca.h>
  25. #include "private.h"
  26. #include "tls.h"
  27. #include "checksum.h"
  28. #include "cipher.h"
  29. #include "cert.h"
  30. #include "tls-private.h"
  31. #include "random.h"
  32. /* Implementation-specific max Record Layer fragment size (must be < 16kB) */
  33. #define TX_RECORD_MAX_LEN 4096
  34. /* TLSPlaintext + TLSCompressed + TLSCiphertext headers + seq_num sizes */
  35. #define TX_RECORD_MAX_HEADERS (5 + 5 + 8 + 5)
  36. #define TX_RECORD_MAX_MAC 64
  37. /* Head room and tail room for the buffer passed to the cipher */
  38. #define TX_RECORD_HEADROOM TX_RECORD_MAX_HEADERS
  39. #define TX_RECORD_TAILROOM TX_RECORD_MAX_MAC
  40. static void tls_write_mac(struct l_tls *tls, uint8_t *compressed,
  41. uint16_t compressed_len, uint8_t *out_buf,
  42. bool txrx)
  43. {
  44. uint8_t *in_buf;
  45. /* Prepend the sequence number to the TLSCompressed buffer */
  46. in_buf = compressed - 8;
  47. l_put_be64(tls->seq_num[txrx]++, in_buf);
  48. if (tls->mac[txrx]) {
  49. l_checksum_reset(tls->mac[txrx]);
  50. l_checksum_update(tls->mac[txrx], in_buf, compressed_len + 8);
  51. l_checksum_get_digest(tls->mac[txrx], out_buf,
  52. tls->mac_length[txrx]);
  53. }
  54. }
  55. static void tls_tx_record_plaintext(struct l_tls *tls,
  56. uint8_t *plaintext,
  57. uint16_t plaintext_len)
  58. {
  59. uint8_t *compressed;
  60. uint16_t compressed_len;
  61. uint8_t *cipher_input;
  62. uint16_t cipher_input_len;
  63. uint8_t *ciphertext;
  64. uint16_t ciphertext_len;
  65. uint8_t padding_length;
  66. uint8_t buf[TX_RECORD_HEADROOM + TX_RECORD_MAX_LEN +
  67. TX_RECORD_TAILROOM];
  68. uint8_t iv[32];
  69. uint8_t *assocdata;
  70. int offset;
  71. /*
  72. * TODO: if DEFLATE is selected in current state, use a new buffer
  73. * on stack to write a TLSCompressed structure there, otherwise use
  74. * the provided buffer. Since only null compression is supported
  75. * today we always use the provided buffer.
  76. */
  77. compressed_len = plaintext_len - 5;
  78. compressed = plaintext;
  79. /* Build a TLSCompressed struct */
  80. compressed[0] = plaintext[0]; /* Copy type and version fields */
  81. compressed[1] = plaintext[1];
  82. compressed[2] = plaintext[2];
  83. compressed[3] = compressed_len >> 8;
  84. compressed[4] = compressed_len >> 0;
  85. switch (tls->cipher_type[1]) {
  86. case TLS_CIPHER_STREAM:
  87. /* Append the MAC after TLSCompressed.fragment, if needed */
  88. tls_write_mac(tls, compressed, compressed_len + 5,
  89. compressed + compressed_len + 5, true);
  90. cipher_input = compressed + 5;
  91. cipher_input_len = compressed_len + tls->mac_length[1];
  92. if (!tls->cipher[1]) {
  93. ciphertext = cipher_input;
  94. ciphertext_len = cipher_input_len;
  95. } else {
  96. ciphertext = buf + TX_RECORD_HEADROOM;
  97. ciphertext_len = cipher_input_len;
  98. l_cipher_encrypt(tls->cipher[1], cipher_input,
  99. ciphertext, cipher_input_len);
  100. }
  101. break;
  102. case TLS_CIPHER_BLOCK:
  103. /* Append the MAC after TLSCompressed.fragment, if needed */
  104. cipher_input = compressed + 5;
  105. tls_write_mac(tls, compressed, compressed_len + 5,
  106. cipher_input + compressed_len, true);
  107. cipher_input_len = compressed_len + tls->mac_length[1];
  108. /* Add minimum padding */
  109. padding_length = (~cipher_input_len) &
  110. (tls->block_length[1] - 1);
  111. memset(cipher_input + cipher_input_len, padding_length,
  112. padding_length + 1);
  113. cipher_input_len += padding_length + 1;
  114. /* Generate an IV */
  115. ciphertext = buf + TX_RECORD_HEADROOM;
  116. offset = 0;
  117. if (tls->negotiated_version >= L_TLS_V12) {
  118. l_getrandom(ciphertext, tls->record_iv_length[1]);
  119. l_cipher_set_iv(tls->cipher[1], ciphertext,
  120. tls->record_iv_length[1]);
  121. offset = tls->record_iv_length[1];
  122. } else if (tls->negotiated_version >= L_TLS_V11) {
  123. l_getrandom(iv, tls->record_iv_length[1]);
  124. l_cipher_encrypt(tls->cipher[1], iv, ciphertext,
  125. tls->record_iv_length[1]);
  126. offset = tls->record_iv_length[1];
  127. }
  128. l_cipher_encrypt(tls->cipher[1], cipher_input,
  129. ciphertext + offset, cipher_input_len);
  130. ciphertext_len = offset + cipher_input_len;
  131. break;
  132. case TLS_CIPHER_AEAD:
  133. /* Prepend seq_num to TLSCompressed.type + .version + .length */
  134. assocdata = compressed - 8;
  135. l_put_be64(tls->seq_num[1]++, assocdata);
  136. cipher_input = compressed + 5;
  137. cipher_input_len = compressed_len;
  138. /*
  139. * Build the IV. The explicit part generation method is
  140. * actually cipher suite-specific but our only AEAD cipher
  141. * suites only require this part to be unique for each
  142. * record. For future suites there may need to be a callback
  143. * that generates the per-record IV or an enum for the suite
  144. * to select one of a few IV types.
  145. *
  146. * Note kernel's rfc4106(gcm(...)) algorithm could potentially
  147. * be used to build the IV.
  148. */
  149. memcpy(iv, tls->fixed_iv[1], tls->fixed_iv_length[1]);
  150. l_put_le64(tls->seq_num[1], iv + tls->fixed_iv_length[1]);
  151. if (tls->record_iv_length[1] > 8)
  152. memset(iv + tls->fixed_iv_length[1] + 8, 42,
  153. tls->record_iv_length[1] - 8);
  154. /* Build the GenericAEADCipher struct */
  155. ciphertext = buf + TX_RECORD_HEADROOM;
  156. memcpy(ciphertext, iv + tls->fixed_iv_length[1],
  157. tls->record_iv_length[1]);
  158. l_aead_cipher_encrypt(tls->aead_cipher[1],
  159. cipher_input, cipher_input_len,
  160. assocdata, 13,
  161. iv, tls->fixed_iv_length[1] +
  162. tls->record_iv_length[1],
  163. ciphertext + tls->record_iv_length[1],
  164. cipher_input_len +
  165. tls->auth_tag_length[1]);
  166. ciphertext_len = tls->record_iv_length[1] +
  167. cipher_input_len + tls->auth_tag_length[1];
  168. break;
  169. default:
  170. return;
  171. }
  172. /* Build a TLSCiphertext struct */
  173. ciphertext -= 5;
  174. ciphertext[0] = plaintext[0]; /* Copy type and version fields */
  175. ciphertext[1] = plaintext[1];
  176. ciphertext[2] = plaintext[2];
  177. ciphertext[3] = ciphertext_len >> 8;
  178. ciphertext[4] = ciphertext_len >> 0;
  179. tls->tx(ciphertext, ciphertext_len + 5, tls->user_data);
  180. }
  181. void tls_tx_record(struct l_tls *tls, enum tls_content_type type,
  182. const uint8_t *data, size_t len)
  183. {
  184. uint8_t buf[TX_RECORD_HEADROOM + TX_RECORD_MAX_LEN +
  185. TX_RECORD_TAILROOM];
  186. uint8_t *fragment, *plaintext;
  187. uint16_t fragment_len;
  188. uint16_t version = tls->negotiated_version ?: tls->min_version;
  189. if (type == TLS_CT_ALERT)
  190. tls->record_flush = true;
  191. while (len) {
  192. fragment = buf + TX_RECORD_HEADROOM;
  193. fragment_len = len < TX_RECORD_MAX_LEN ?
  194. len : TX_RECORD_MAX_LEN;
  195. /* Build a TLSPlaintext struct */
  196. plaintext = fragment - 5;
  197. plaintext[0] = type;
  198. plaintext[1] = (uint8_t) (version >> 8);
  199. plaintext[2] = (uint8_t) (version >> 0);
  200. plaintext[3] = fragment_len >> 8;
  201. plaintext[4] = fragment_len >> 0;
  202. memcpy(plaintext + 5, data, fragment_len);
  203. tls_tx_record_plaintext(tls, plaintext, fragment_len + 5);
  204. data += fragment_len;
  205. len -= fragment_len;
  206. }
  207. }
  208. static bool tls_handle_plaintext(struct l_tls *tls, const uint8_t *plaintext,
  209. int len, uint8_t type, uint16_t version)
  210. {
  211. if (len > (1 << 14)) {
  212. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  213. "Plaintext message too long: %i", len);
  214. return false;
  215. }
  216. switch (type) {
  217. case TLS_CT_CHANGE_CIPHER_SPEC:
  218. case TLS_CT_APPLICATION_DATA:
  219. return tls_handle_message(tls, plaintext, len, type, version);
  220. /*
  221. * We need to perform input reassembly twice at different levels:
  222. * once to make sure we're handling complete TLSCiphertext messages,
  223. * in l_tls_handle_rx(), and again here so that the Alert and
  224. * Handshake message type handlers deal with complete messages.
  225. * This does not affect ChangeCipherSpec messages because they're
  226. * just a single byte and there are never more than one such message
  227. * in a row. Similarly it doesn't affect application data because
  228. * the application is not guaranteed that message boundaries are
  229. * preserved in any way and we don't know its message lengths anyway.
  230. * It does affect Alert because these messages are 2 byte long and
  231. * could potentially be split over two TLSPlaintext messages but
  232. * there are never more than one Alert in a TLSPlaintext for the same
  233. * reason as with ChangeCipherSpec. Handshake messages are the
  234. * most affected although the need to do the reassembly twice still
  235. * seems wasteful considering most of these messages are sent in
  236. * plaintext and TLSCiphertext maps to TLSPlaintext records.
  237. */
  238. case TLS_CT_ALERT:
  239. case TLS_CT_HANDSHAKE:
  240. break;
  241. default:
  242. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  243. "Unknown content type %i", type);
  244. return false;
  245. }
  246. if (tls->message_buf_len && type != tls->message_content_type) {
  247. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  248. "Message fragment type %i doesn't match "
  249. "previous type %i", type,
  250. tls->message_content_type);
  251. return false;
  252. }
  253. tls->message_content_type = type;
  254. while (1) {
  255. int header_len, need_len;
  256. int chunk_len;
  257. /* Do we have a full header in tls->message_buf? */
  258. header_len = (type == TLS_CT_ALERT) ? 2 : 4;
  259. need_len = header_len;
  260. if (tls->message_buf_len >= header_len) {
  261. if (type == TLS_CT_HANDSHAKE) {
  262. uint32_t hs_len = (tls->message_buf[1] << 16) |
  263. (tls->message_buf[2] << 8) |
  264. (tls->message_buf[3] << 0);
  265. if (hs_len > (1 << 14)) {
  266. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR,
  267. 0, "Handshake message "
  268. "too long: %i",
  269. (int) hs_len);
  270. return false;
  271. }
  272. need_len += hs_len;
  273. }
  274. /* Do we have a full structure? */
  275. if (tls->message_buf_len == need_len) {
  276. if (!tls_handle_message(tls, tls->message_buf,
  277. need_len, type,
  278. version))
  279. return false;
  280. tls->message_buf_len = 0;
  281. if (tls->record_flush)
  282. break;
  283. continue;
  284. }
  285. if (!len)
  286. break;
  287. }
  288. /* Try to fill up tls->message_buf up to need_len */
  289. if (tls->message_buf_max_len < need_len) {
  290. tls->message_buf_max_len = need_len;
  291. tls->message_buf =
  292. l_realloc(tls->message_buf, need_len);
  293. }
  294. need_len -= tls->message_buf_len;
  295. chunk_len = need_len;
  296. if (len < chunk_len)
  297. chunk_len = len;
  298. memcpy(tls->message_buf + tls->message_buf_len, plaintext,
  299. chunk_len);
  300. tls->message_buf_len += chunk_len;
  301. plaintext += chunk_len;
  302. len -= chunk_len;
  303. if (chunk_len < need_len)
  304. break;
  305. }
  306. return true;
  307. }
  308. static bool tls_handle_ciphertext(struct l_tls *tls)
  309. {
  310. uint8_t type;
  311. uint16_t version;
  312. uint16_t fragment_len;
  313. uint8_t mac_buf[TX_RECORD_MAX_MAC], i, padding_len;
  314. int cipher_output_len, error;
  315. uint8_t *compressed;
  316. int compressed_len;
  317. uint8_t iv[32];
  318. uint8_t *assocdata;
  319. type = tls->record_buf[0];
  320. version = l_get_be16(tls->record_buf + 1);
  321. fragment_len = l_get_be16(tls->record_buf + 3);
  322. if (fragment_len > (1 << 14) + 2048) {
  323. TLS_DISCONNECT(TLS_ALERT_RECORD_OVERFLOW, 0,
  324. "Record fragment too long: %u", fragment_len);
  325. return false;
  326. }
  327. if ((tls->negotiated_version && tls->negotiated_version != version) ||
  328. (!tls->negotiated_version &&
  329. tls->record_buf[1] != 0x03 /* Appending E.1 */)) {
  330. TLS_DISCONNECT(TLS_ALERT_PROTOCOL_VERSION, 0,
  331. "Record version mismatch: %02x", version);
  332. return false;
  333. }
  334. if (fragment_len < tls->mac_length[0]) {
  335. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  336. "Record fragment too short: %u", fragment_len);
  337. return false;
  338. }
  339. compressed = alloca(8 + 5 + fragment_len);
  340. /* Copy the type and version fields */
  341. compressed[8] = type;
  342. l_put_be16(version, compressed + 9);
  343. switch (tls->cipher_type[0]) {
  344. case TLS_CIPHER_STREAM:
  345. cipher_output_len = fragment_len;
  346. compressed_len = cipher_output_len - tls->mac_length[0];
  347. l_put_be16(compressed_len, compressed + 11);
  348. if (!tls->cipher[0])
  349. memcpy(compressed + 13, tls->record_buf + 5,
  350. cipher_output_len);
  351. else if (!l_cipher_decrypt(tls->cipher[0], tls->record_buf + 5,
  352. compressed + 13,
  353. cipher_output_len)) {
  354. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  355. "Decrypting record fragment failed");
  356. return false;
  357. }
  358. /* Calculate the MAC if needed */
  359. tls_write_mac(tls, compressed + 8, 5 + compressed_len,
  360. mac_buf, false);
  361. if (memcmp(mac_buf, compressed + 13 + compressed_len,
  362. tls->mac_length[0])) {
  363. TLS_DISCONNECT(TLS_ALERT_BAD_RECORD_MAC, 0,
  364. "Record fragment MAC mismatch");
  365. return false;
  366. }
  367. compressed += 13;
  368. break;
  369. case TLS_CIPHER_BLOCK:
  370. i = 0;
  371. if (tls->negotiated_version >= L_TLS_V11)
  372. i = tls->record_iv_length[0];
  373. if (fragment_len <= tls->mac_length[0] + i) {
  374. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  375. "Record fragment too short: %u",
  376. fragment_len);
  377. return false;
  378. }
  379. cipher_output_len = fragment_len - i;
  380. if (cipher_output_len % tls->block_length[0] != 0) {
  381. /*
  382. * In strict TLS 1.0 TLS_ALERT_DECRYPT_FAIL_RESERVED
  383. * should be returned here but that was declared
  384. * unsafe in the TLS 1.1 spec.
  385. */
  386. TLS_DISCONNECT(TLS_ALERT_BAD_RECORD_MAC, 0,
  387. "Fragment data len %i not a multiple "
  388. "of block length %zi",
  389. cipher_output_len,
  390. tls->block_length[0]);
  391. return false;
  392. }
  393. if (tls->negotiated_version >= L_TLS_V12) {
  394. if (!l_cipher_set_iv(tls->cipher[0],
  395. tls->record_buf + 5,
  396. tls->record_iv_length[0])) {
  397. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  398. "Setting fragment IV failed");
  399. return false;
  400. }
  401. } else if (tls->negotiated_version >= L_TLS_V11)
  402. if (!l_cipher_decrypt(tls->cipher[0],
  403. tls->record_buf + 5, iv,
  404. tls->record_iv_length[0])) {
  405. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  406. "Setting fragment IV failed");
  407. return false;
  408. }
  409. if (!l_cipher_decrypt(tls->cipher[0], tls->record_buf + 5 + i,
  410. compressed + 13, cipher_output_len)) {
  411. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  412. "Fragment decryption failed");
  413. return false;
  414. }
  415. /*
  416. * RFC 5246, page 24:
  417. * In order to defend against this attack, implementations
  418. * MUST ensure that record processing time is essentially the
  419. * same whether or not the padding is correct. In general,
  420. * the best way to do this is to compute the MAC even if the
  421. * padding is incorrect, and only then reject the packet. For
  422. * instance, if the pad appears to be incorrect, the
  423. * implementation might assume a zero-length pad and then
  424. * compute the MAC.
  425. */
  426. padding_len = compressed[13 + cipher_output_len - 1];
  427. error = 0;
  428. if (padding_len + tls->mac_length[0] + 1 >
  429. (size_t) cipher_output_len) {
  430. /*
  431. * In strict TLS 1.0 TLS_ALERT_DECRYPT_FAIL_RESERVED
  432. * should be returned here but that was declared
  433. * unsafe in the TLS 1.1 spec.
  434. */
  435. padding_len = 0;
  436. error = 1;
  437. }
  438. compressed_len = cipher_output_len - 1 - padding_len -
  439. tls->mac_length[0];
  440. l_put_be16(compressed_len, compressed + 11);
  441. error |= !l_secure_memeq(compressed + 13 + cipher_output_len -
  442. 1 - padding_len, padding_len,
  443. padding_len);
  444. /* Calculate the MAC if needed */
  445. tls_write_mac(tls, compressed + 8, 5 + compressed_len,
  446. mac_buf, false);
  447. if ((tls->mac_length[0] && memcmp(mac_buf, compressed + 13 +
  448. compressed_len, tls->mac_length[0])) ||
  449. error) {
  450. TLS_DISCONNECT(TLS_ALERT_BAD_RECORD_MAC, 0,
  451. "Record fragment MAC mismatch");
  452. return false;
  453. }
  454. compressed += 13;
  455. break;
  456. case TLS_CIPHER_AEAD:
  457. if (fragment_len <= tls->record_iv_length[0] +
  458. tls->auth_tag_length[0]) {
  459. TLS_DISCONNECT(TLS_ALERT_DECODE_ERROR, 0,
  460. "Record fragment too short: %u",
  461. fragment_len);
  462. return false;
  463. }
  464. compressed_len = fragment_len - tls->record_iv_length[0] -
  465. tls->auth_tag_length[0];
  466. l_put_be16(compressed_len, compressed + 11);
  467. /* Prepend seq_num to TLSCompressed.type + .version + .length */
  468. assocdata = compressed;
  469. l_put_be64(tls->seq_num[0]++, assocdata);
  470. compressed += 13;
  471. /* Build the IV */
  472. memcpy(iv, tls->fixed_iv[0], tls->fixed_iv_length[0]);
  473. memcpy(iv + tls->fixed_iv_length[0], tls->record_buf + 5,
  474. tls->record_iv_length[0]);
  475. if (!l_aead_cipher_decrypt(tls->aead_cipher[0],
  476. tls->record_buf + 5 + tls->record_iv_length[0],
  477. fragment_len - tls->record_iv_length[0],
  478. assocdata, 13, iv, tls->fixed_iv_length[0] +
  479. tls->record_iv_length[0],
  480. compressed, compressed_len)) {
  481. TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0,
  482. "Decrypting record fragment failed");
  483. return false;
  484. }
  485. break;
  486. default:
  487. return false;
  488. }
  489. /* DEFLATE not supported so just pass on compressed / compressed_len */
  490. return tls_handle_plaintext(tls, compressed, compressed_len,
  491. type, version);
  492. }
  493. LIB_EXPORT void l_tls_handle_rx(struct l_tls *tls, const uint8_t *data,
  494. size_t len)
  495. {
  496. int need_len;
  497. int chunk_len;
  498. tls->record_flush = false;
  499. /* Reassemble TLSCiphertext structures from the received chunks */
  500. while (1) {
  501. /* Do we have a full header in tls->record_buf? */
  502. if (tls->record_buf_len >= 5) {
  503. need_len = 5 + l_get_be16(tls->record_buf + 3);
  504. /* Do we have a full structure? */
  505. if (tls->record_buf_len == need_len) {
  506. if (!tls_handle_ciphertext(tls))
  507. return;
  508. tls->record_buf_len = 0;
  509. need_len = 5;
  510. if (tls->record_flush)
  511. break;
  512. }
  513. if (!len)
  514. break;
  515. } else
  516. need_len = 5;
  517. /* Try to fill up tls->record_buf up to need_len */
  518. if (tls->record_buf_max_len < need_len) {
  519. tls->record_buf_max_len = need_len;
  520. tls->record_buf = l_realloc(tls->record_buf, need_len);
  521. }
  522. need_len -= tls->record_buf_len;
  523. chunk_len = need_len;
  524. if (len < (size_t) chunk_len)
  525. chunk_len = len;
  526. memcpy(tls->record_buf + tls->record_buf_len, data, chunk_len);
  527. tls->record_buf_len += chunk_len;
  528. data += chunk_len;
  529. len -= chunk_len;
  530. if (chunk_len < need_len)
  531. break;
  532. }
  533. }