rec_layer_s3.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  1. /*
  2. * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <limits.h>
  11. #include <errno.h>
  12. #include "../ssl_local.h"
  13. #include <openssl/evp.h>
  14. #include <openssl/buffer.h>
  15. #include <openssl/rand.h>
  16. #include "record_local.h"
  17. #include "internal/packet.h"
  18. #if defined(OPENSSL_SMALL_FOOTPRINT) || \
  19. !( defined(AES_ASM) && ( \
  20. defined(__x86_64) || defined(__x86_64__) || \
  21. defined(_M_AMD64) || defined(_M_X64) ) \
  22. )
  23. # undef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
  24. # define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0
  25. #endif
  26. void RECORD_LAYER_init(RECORD_LAYER *rl, SSL *s)
  27. {
  28. rl->s = s;
  29. RECORD_LAYER_set_first_record(&s->rlayer);
  30. SSL3_RECORD_clear(rl->rrec, SSL_MAX_PIPELINES);
  31. }
  32. void RECORD_LAYER_clear(RECORD_LAYER *rl)
  33. {
  34. rl->rstate = SSL_ST_READ_HEADER;
  35. /*
  36. * Do I need to clear read_ahead? As far as I can tell read_ahead did not
  37. * previously get reset by SSL_clear...so I'll keep it that way..but is
  38. * that right?
  39. */
  40. rl->packet = NULL;
  41. rl->packet_length = 0;
  42. rl->wnum = 0;
  43. memset(rl->handshake_fragment, 0, sizeof(rl->handshake_fragment));
  44. rl->handshake_fragment_len = 0;
  45. rl->wpend_tot = 0;
  46. rl->wpend_type = 0;
  47. rl->wpend_ret = 0;
  48. rl->wpend_buf = NULL;
  49. SSL3_BUFFER_clear(&rl->rbuf);
  50. ssl3_release_write_buffer(rl->s);
  51. rl->numrpipes = 0;
  52. SSL3_RECORD_clear(rl->rrec, SSL_MAX_PIPELINES);
  53. RECORD_LAYER_reset_read_sequence(rl);
  54. RECORD_LAYER_reset_write_sequence(rl);
  55. if (rl->d)
  56. DTLS_RECORD_LAYER_clear(rl);
  57. }
  58. void RECORD_LAYER_release(RECORD_LAYER *rl)
  59. {
  60. if (SSL3_BUFFER_is_initialised(&rl->rbuf))
  61. ssl3_release_read_buffer(rl->s);
  62. if (rl->numwpipes > 0)
  63. ssl3_release_write_buffer(rl->s);
  64. SSL3_RECORD_release(rl->rrec, SSL_MAX_PIPELINES);
  65. }
  66. /* Checks if we have unprocessed read ahead data pending */
  67. int RECORD_LAYER_read_pending(const RECORD_LAYER *rl)
  68. {
  69. return SSL3_BUFFER_get_left(&rl->rbuf) != 0;
  70. }
  71. /* Checks if we have decrypted unread record data pending */
  72. int RECORD_LAYER_processed_read_pending(const RECORD_LAYER *rl)
  73. {
  74. size_t curr_rec = 0, num_recs = RECORD_LAYER_get_numrpipes(rl);
  75. const SSL3_RECORD *rr = rl->rrec;
  76. while (curr_rec < num_recs && SSL3_RECORD_is_read(&rr[curr_rec]))
  77. curr_rec++;
  78. return curr_rec < num_recs;
  79. }
  80. int RECORD_LAYER_write_pending(const RECORD_LAYER *rl)
  81. {
  82. return (rl->numwpipes > 0)
  83. && SSL3_BUFFER_get_left(&rl->wbuf[rl->numwpipes - 1]) != 0;
  84. }
  85. void RECORD_LAYER_reset_read_sequence(RECORD_LAYER *rl)
  86. {
  87. memset(rl->read_sequence, 0, sizeof(rl->read_sequence));
  88. }
  89. void RECORD_LAYER_reset_write_sequence(RECORD_LAYER *rl)
  90. {
  91. memset(rl->write_sequence, 0, sizeof(rl->write_sequence));
  92. }
  93. size_t ssl3_pending(const SSL *s)
  94. {
  95. size_t i, num = 0;
  96. if (s->rlayer.rstate == SSL_ST_READ_BODY)
  97. return 0;
  98. for (i = 0; i < RECORD_LAYER_get_numrpipes(&s->rlayer); i++) {
  99. if (SSL3_RECORD_get_type(&s->rlayer.rrec[i])
  100. != SSL3_RT_APPLICATION_DATA)
  101. return 0;
  102. num += SSL3_RECORD_get_length(&s->rlayer.rrec[i]);
  103. }
  104. return num;
  105. }
  106. void SSL_CTX_set_default_read_buffer_len(SSL_CTX *ctx, size_t len)
  107. {
  108. ctx->default_read_buf_len = len;
  109. }
  110. void SSL_set_default_read_buffer_len(SSL *s, size_t len)
  111. {
  112. SSL3_BUFFER_set_default_len(RECORD_LAYER_get_rbuf(&s->rlayer), len);
  113. }
  114. const char *SSL_rstate_string_long(const SSL *s)
  115. {
  116. switch (s->rlayer.rstate) {
  117. case SSL_ST_READ_HEADER:
  118. return "read header";
  119. case SSL_ST_READ_BODY:
  120. return "read body";
  121. case SSL_ST_READ_DONE:
  122. return "read done";
  123. default:
  124. return "unknown";
  125. }
  126. }
  127. const char *SSL_rstate_string(const SSL *s)
  128. {
  129. switch (s->rlayer.rstate) {
  130. case SSL_ST_READ_HEADER:
  131. return "RH";
  132. case SSL_ST_READ_BODY:
  133. return "RB";
  134. case SSL_ST_READ_DONE:
  135. return "RD";
  136. default:
  137. return "unknown";
  138. }
  139. }
  140. /*
  141. * Return values are as per SSL_read()
  142. */
  143. int ssl3_read_n(SSL *s, size_t n, size_t max, int extend, int clearold,
  144. size_t *readbytes)
  145. {
  146. /*
  147. * If extend == 0, obtain new n-byte packet; if extend == 1, increase
  148. * packet by another n bytes. The packet will be in the sub-array of
  149. * s->rlayer.rbuf.buf specified by s->rlayer.packet and
  150. * s->rlayer.packet_length. (If s->rlayer.read_ahead is set, 'max' bytes may
  151. * be stored in rbuf [plus s->rlayer.packet_length bytes if extend == 1].)
  152. * if clearold == 1, move the packet to the start of the buffer; if
  153. * clearold == 0 then leave any old packets where they were
  154. */
  155. size_t len, left, align = 0;
  156. unsigned char *pkt;
  157. SSL3_BUFFER *rb;
  158. if (n == 0)
  159. return 0;
  160. rb = &s->rlayer.rbuf;
  161. if (rb->buf == NULL)
  162. if (!ssl3_setup_read_buffer(s)) {
  163. /* SSLfatal() already called */
  164. return -1;
  165. }
  166. left = rb->left;
  167. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
  168. align = (size_t)rb->buf + SSL3_RT_HEADER_LENGTH;
  169. align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);
  170. #endif
  171. if (!extend) {
  172. /* start with empty packet ... */
  173. if (left == 0)
  174. rb->offset = align;
  175. else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH) {
  176. /*
  177. * check if next packet length is large enough to justify payload
  178. * alignment...
  179. */
  180. pkt = rb->buf + rb->offset;
  181. if (pkt[0] == SSL3_RT_APPLICATION_DATA
  182. && (pkt[3] << 8 | pkt[4]) >= 128) {
  183. /*
  184. * Note that even if packet is corrupted and its length field
  185. * is insane, we can only be led to wrong decision about
  186. * whether memmove will occur or not. Header values has no
  187. * effect on memmove arguments and therefore no buffer
  188. * overrun can be triggered.
  189. */
  190. memmove(rb->buf + align, pkt, left);
  191. rb->offset = align;
  192. }
  193. }
  194. s->rlayer.packet = rb->buf + rb->offset;
  195. s->rlayer.packet_length = 0;
  196. /* ... now we can act as if 'extend' was set */
  197. }
  198. len = s->rlayer.packet_length;
  199. pkt = rb->buf + align;
  200. /*
  201. * Move any available bytes to front of buffer: 'len' bytes already
  202. * pointed to by 'packet', 'left' extra ones at the end
  203. */
  204. if (s->rlayer.packet != pkt && clearold == 1) {
  205. memmove(pkt, s->rlayer.packet, len + left);
  206. s->rlayer.packet = pkt;
  207. rb->offset = len + align;
  208. }
  209. /*
  210. * For DTLS/UDP reads should not span multiple packets because the read
  211. * operation returns the whole packet at once (as long as it fits into
  212. * the buffer).
  213. */
  214. if (SSL_IS_DTLS(s)) {
  215. if (left == 0 && extend)
  216. return 0;
  217. if (left > 0 && n > left)
  218. n = left;
  219. }
  220. /* if there is enough in the buffer from a previous read, take some */
  221. if (left >= n) {
  222. s->rlayer.packet_length += n;
  223. rb->left = left - n;
  224. rb->offset += n;
  225. *readbytes = n;
  226. return 1;
  227. }
  228. /* else we need to read more data */
  229. if (n > rb->len - rb->offset) {
  230. /* does not happen */
  231. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  232. return -1;
  233. }
  234. /*
  235. * Ktls always reads full records.
  236. * Also, we always act like read_ahead is set for DTLS.
  237. */
  238. if (!BIO_get_ktls_recv(s->rbio) && !s->rlayer.read_ahead
  239. && !SSL_IS_DTLS(s)) {
  240. /* ignore max parameter */
  241. max = n;
  242. } else {
  243. if (max < n)
  244. max = n;
  245. if (max > rb->len - rb->offset)
  246. max = rb->len - rb->offset;
  247. }
  248. while (left < n) {
  249. size_t bioread = 0;
  250. int ret;
  251. /*
  252. * Now we have len+left bytes at the front of s->s3.rbuf.buf and
  253. * need to read in more until we have len+n (up to len+max if
  254. * possible)
  255. */
  256. clear_sys_error();
  257. if (s->rbio != NULL) {
  258. s->rwstate = SSL_READING;
  259. ret = BIO_read(s->rbio, pkt + len + left, max - left);
  260. if (ret >= 0)
  261. bioread = ret;
  262. if (ret <= 0
  263. && !BIO_should_retry(s->rbio)
  264. && BIO_eof(s->rbio)) {
  265. if (s->options & SSL_OP_IGNORE_UNEXPECTED_EOF) {
  266. SSL_set_shutdown(s, SSL_RECEIVED_SHUTDOWN);
  267. s->s3.warn_alert = SSL_AD_CLOSE_NOTIFY;
  268. } else {
  269. SSLfatal(s, SSL_AD_DECODE_ERROR,
  270. SSL_R_UNEXPECTED_EOF_WHILE_READING);
  271. }
  272. }
  273. } else {
  274. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_READ_BIO_NOT_SET);
  275. ret = -1;
  276. }
  277. if (ret <= 0) {
  278. rb->left = left;
  279. if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s))
  280. if (len + left == 0)
  281. ssl3_release_read_buffer(s);
  282. return ret;
  283. }
  284. left += bioread;
  285. /*
  286. * reads should *never* span multiple packets for DTLS because the
  287. * underlying transport protocol is message oriented as opposed to
  288. * byte oriented as in the TLS case.
  289. */
  290. if (SSL_IS_DTLS(s)) {
  291. if (n > left)
  292. n = left; /* makes the while condition false */
  293. }
  294. }
  295. /* done reading, now the book-keeping */
  296. rb->offset += n;
  297. rb->left = left - n;
  298. s->rlayer.packet_length += n;
  299. s->rwstate = SSL_NOTHING;
  300. *readbytes = n;
  301. return 1;
  302. }
  303. /*
  304. * Call this to write data in records of type 'type' It will return <= 0 if
  305. * not all data has been sent or non-blocking IO.
  306. */
  307. int ssl3_write_bytes(SSL *s, int type, const void *buf_, size_t len,
  308. size_t *written)
  309. {
  310. const unsigned char *buf = buf_;
  311. size_t tot;
  312. size_t n, max_send_fragment, split_send_fragment, maxpipes;
  313. #if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
  314. size_t nw;
  315. #endif
  316. SSL3_BUFFER *wb = &s->rlayer.wbuf[0];
  317. int i;
  318. size_t tmpwrit;
  319. s->rwstate = SSL_NOTHING;
  320. tot = s->rlayer.wnum;
  321. /*
  322. * ensure that if we end up with a smaller value of data to write out
  323. * than the original len from a write which didn't complete for
  324. * non-blocking I/O and also somehow ended up avoiding the check for
  325. * this in ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be
  326. * possible to end up with (len-tot) as a large number that will then
  327. * promptly send beyond the end of the users buffer ... so we trap and
  328. * report the error in a way the user will notice
  329. */
  330. if ((len < s->rlayer.wnum)
  331. || ((wb->left != 0) && (len < (s->rlayer.wnum + s->rlayer.wpend_tot)))) {
  332. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_LENGTH);
  333. return -1;
  334. }
  335. if (s->early_data_state == SSL_EARLY_DATA_WRITING
  336. && !early_data_count_ok(s, len, 0, 1)) {
  337. /* SSLfatal() already called */
  338. return -1;
  339. }
  340. s->rlayer.wnum = 0;
  341. /*
  342. * If we are supposed to be sending a KeyUpdate or NewSessionTicket then go
  343. * into init unless we have writes pending - in which case we should finish
  344. * doing that first.
  345. */
  346. if (wb->left == 0 && (s->key_update != SSL_KEY_UPDATE_NONE
  347. || s->ext.extra_tickets_expected > 0))
  348. ossl_statem_set_in_init(s, 1);
  349. /*
  350. * When writing early data on the server side we could be "in_init" in
  351. * between receiving the EoED and the CF - but we don't want to handle those
  352. * messages yet.
  353. */
  354. if (SSL_in_init(s) && !ossl_statem_get_in_handshake(s)
  355. && s->early_data_state != SSL_EARLY_DATA_UNAUTH_WRITING) {
  356. i = s->handshake_func(s);
  357. /* SSLfatal() already called */
  358. if (i < 0)
  359. return i;
  360. if (i == 0) {
  361. return -1;
  362. }
  363. }
  364. /*
  365. * first check if there is a SSL3_BUFFER still being written out. This
  366. * will happen with non blocking IO
  367. */
  368. if (wb->left != 0) {
  369. /* SSLfatal() already called if appropriate */
  370. i = ssl3_write_pending(s, type, &buf[tot], s->rlayer.wpend_tot,
  371. &tmpwrit);
  372. if (i <= 0) {
  373. /* XXX should we ssl3_release_write_buffer if i<0? */
  374. s->rlayer.wnum = tot;
  375. return i;
  376. }
  377. tot += tmpwrit; /* this might be last fragment */
  378. }
  379. #if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
  380. /*
  381. * Depending on platform multi-block can deliver several *times*
  382. * better performance. Downside is that it has to allocate
  383. * jumbo buffer to accommodate up to 8 records, but the
  384. * compromise is considered worthy.
  385. */
  386. if (type == SSL3_RT_APPLICATION_DATA
  387. && len >= 4 * (max_send_fragment = ssl_get_max_send_fragment(s))
  388. && s->compress == NULL
  389. && s->msg_callback == NULL
  390. && !SSL_WRITE_ETM(s)
  391. && SSL_USE_EXPLICIT_IV(s)
  392. && !BIO_get_ktls_send(s->wbio)
  393. && (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(s->enc_write_ctx))
  394. & EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK) != 0) {
  395. unsigned char aad[13];
  396. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
  397. size_t packlen;
  398. int packleni;
  399. /* minimize address aliasing conflicts */
  400. if ((max_send_fragment & 0xfff) == 0)
  401. max_send_fragment -= 512;
  402. if (tot == 0 || wb->buf == NULL) { /* allocate jumbo buffer */
  403. ssl3_release_write_buffer(s);
  404. packlen = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
  405. EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE,
  406. (int)max_send_fragment, NULL);
  407. if (len >= 8 * max_send_fragment)
  408. packlen *= 8;
  409. else
  410. packlen *= 4;
  411. if (!ssl3_setup_write_buffer(s, 1, packlen)) {
  412. /* SSLfatal() already called */
  413. return -1;
  414. }
  415. } else if (tot == len) { /* done? */
  416. /* free jumbo buffer */
  417. ssl3_release_write_buffer(s);
  418. *written = tot;
  419. return 1;
  420. }
  421. n = (len - tot);
  422. for (;;) {
  423. if (n < 4 * max_send_fragment) {
  424. /* free jumbo buffer */
  425. ssl3_release_write_buffer(s);
  426. break;
  427. }
  428. if (s->s3.alert_dispatch) {
  429. i = s->method->ssl_dispatch_alert(s);
  430. if (i <= 0) {
  431. /* SSLfatal() already called if appropriate */
  432. s->rlayer.wnum = tot;
  433. return i;
  434. }
  435. }
  436. if (n >= 8 * max_send_fragment)
  437. nw = max_send_fragment * (mb_param.interleave = 8);
  438. else
  439. nw = max_send_fragment * (mb_param.interleave = 4);
  440. memcpy(aad, s->rlayer.write_sequence, 8);
  441. aad[8] = type;
  442. aad[9] = (unsigned char)(s->version >> 8);
  443. aad[10] = (unsigned char)(s->version);
  444. aad[11] = 0;
  445. aad[12] = 0;
  446. mb_param.out = NULL;
  447. mb_param.inp = aad;
  448. mb_param.len = nw;
  449. packleni = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
  450. EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
  451. sizeof(mb_param), &mb_param);
  452. packlen = (size_t)packleni;
  453. if (packleni <= 0 || packlen > wb->len) { /* never happens */
  454. /* free jumbo buffer */
  455. ssl3_release_write_buffer(s);
  456. break;
  457. }
  458. mb_param.out = wb->buf;
  459. mb_param.inp = &buf[tot];
  460. mb_param.len = nw;
  461. if (EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
  462. EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
  463. sizeof(mb_param), &mb_param) <= 0)
  464. return -1;
  465. s->rlayer.write_sequence[7] += mb_param.interleave;
  466. if (s->rlayer.write_sequence[7] < mb_param.interleave) {
  467. int j = 6;
  468. while (j >= 0 && (++s->rlayer.write_sequence[j--]) == 0) ;
  469. }
  470. wb->offset = 0;
  471. wb->left = packlen;
  472. s->rlayer.wpend_tot = nw;
  473. s->rlayer.wpend_buf = &buf[tot];
  474. s->rlayer.wpend_type = type;
  475. s->rlayer.wpend_ret = nw;
  476. i = ssl3_write_pending(s, type, &buf[tot], nw, &tmpwrit);
  477. if (i <= 0) {
  478. /* SSLfatal() already called if appropriate */
  479. if (i < 0 && (!s->wbio || !BIO_should_retry(s->wbio))) {
  480. /* free jumbo buffer */
  481. ssl3_release_write_buffer(s);
  482. }
  483. s->rlayer.wnum = tot;
  484. return i;
  485. }
  486. if (tmpwrit == n) {
  487. /* free jumbo buffer */
  488. ssl3_release_write_buffer(s);
  489. *written = tot + tmpwrit;
  490. return 1;
  491. }
  492. n -= tmpwrit;
  493. tot += tmpwrit;
  494. }
  495. } else
  496. #endif /* !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK */
  497. if (tot == len) { /* done? */
  498. if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s))
  499. ssl3_release_write_buffer(s);
  500. *written = tot;
  501. return 1;
  502. }
  503. n = (len - tot);
  504. max_send_fragment = ssl_get_max_send_fragment(s);
  505. split_send_fragment = ssl_get_split_send_fragment(s);
  506. /*
  507. * If max_pipelines is 0 then this means "undefined" and we default to
  508. * 1 pipeline. Similarly if the cipher does not support pipelined
  509. * processing then we also only use 1 pipeline, or if we're not using
  510. * explicit IVs
  511. */
  512. maxpipes = s->max_pipelines;
  513. if (maxpipes > SSL_MAX_PIPELINES) {
  514. /*
  515. * We should have prevented this when we set max_pipelines so we
  516. * shouldn't get here
  517. */
  518. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  519. return -1;
  520. }
  521. if (maxpipes == 0
  522. || s->enc_write_ctx == NULL
  523. || (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(s->enc_write_ctx))
  524. & EVP_CIPH_FLAG_PIPELINE) == 0
  525. || !SSL_USE_EXPLICIT_IV(s))
  526. maxpipes = 1;
  527. if (max_send_fragment == 0
  528. || split_send_fragment == 0
  529. || split_send_fragment > max_send_fragment) {
  530. /*
  531. * We should have prevented this when we set/get the split and max send
  532. * fragments so we shouldn't get here
  533. */
  534. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  535. return -1;
  536. }
  537. for (;;) {
  538. size_t pipelens[SSL_MAX_PIPELINES], tmppipelen, remain;
  539. size_t numpipes, j;
  540. if (n == 0)
  541. numpipes = 1;
  542. else
  543. numpipes = ((n - 1) / split_send_fragment) + 1;
  544. if (numpipes > maxpipes)
  545. numpipes = maxpipes;
  546. if (n / numpipes >= max_send_fragment) {
  547. /*
  548. * We have enough data to completely fill all available
  549. * pipelines
  550. */
  551. for (j = 0; j < numpipes; j++) {
  552. pipelens[j] = max_send_fragment;
  553. }
  554. } else {
  555. /* We can partially fill all available pipelines */
  556. tmppipelen = n / numpipes;
  557. remain = n % numpipes;
  558. for (j = 0; j < numpipes; j++) {
  559. pipelens[j] = tmppipelen;
  560. if (j < remain)
  561. pipelens[j]++;
  562. }
  563. }
  564. i = do_ssl3_write(s, type, &(buf[tot]), pipelens, numpipes, 0,
  565. &tmpwrit);
  566. if (i <= 0) {
  567. /* SSLfatal() already called if appropriate */
  568. /* XXX should we ssl3_release_write_buffer if i<0? */
  569. s->rlayer.wnum = tot;
  570. return i;
  571. }
  572. if (tmpwrit == n ||
  573. (type == SSL3_RT_APPLICATION_DATA &&
  574. (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
  575. /*
  576. * next chunk of data should get another prepended empty fragment
  577. * in ciphersuites with known-IV weakness:
  578. */
  579. s->s3.empty_fragment_done = 0;
  580. if (tmpwrit == n
  581. && (s->mode & SSL_MODE_RELEASE_BUFFERS) != 0
  582. && !SSL_IS_DTLS(s))
  583. ssl3_release_write_buffer(s);
  584. *written = tot + tmpwrit;
  585. return 1;
  586. }
  587. n -= tmpwrit;
  588. tot += tmpwrit;
  589. }
  590. }
  591. int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
  592. size_t *pipelens, size_t numpipes,
  593. int create_empty_fragment, size_t *written)
  594. {
  595. WPACKET pkt[SSL_MAX_PIPELINES];
  596. SSL3_RECORD wr[SSL_MAX_PIPELINES];
  597. WPACKET *thispkt;
  598. SSL3_RECORD *thiswr;
  599. unsigned char *recordstart;
  600. int i, mac_size, clear = 0;
  601. size_t prefix_len = 0;
  602. int eivlen = 0;
  603. size_t align = 0;
  604. SSL3_BUFFER *wb;
  605. SSL_SESSION *sess;
  606. size_t totlen = 0, len, wpinited = 0;
  607. size_t j;
  608. int using_ktls;
  609. for (j = 0; j < numpipes; j++)
  610. totlen += pipelens[j];
  611. /*
  612. * first check if there is a SSL3_BUFFER still being written out. This
  613. * will happen with non blocking IO
  614. */
  615. if (RECORD_LAYER_write_pending(&s->rlayer)) {
  616. /* Calls SSLfatal() as required */
  617. return ssl3_write_pending(s, type, buf, totlen, written);
  618. }
  619. /* If we have an alert to send, lets send it */
  620. if (s->s3.alert_dispatch) {
  621. i = s->method->ssl_dispatch_alert(s);
  622. if (i <= 0) {
  623. /* SSLfatal() already called if appropriate */
  624. return i;
  625. }
  626. /* if it went, fall through and send more stuff */
  627. }
  628. if (s->rlayer.numwpipes < numpipes) {
  629. if (!ssl3_setup_write_buffer(s, numpipes, 0)) {
  630. /* SSLfatal() already called */
  631. return -1;
  632. }
  633. }
  634. if (totlen == 0 && !create_empty_fragment)
  635. return 0;
  636. sess = s->session;
  637. if ((sess == NULL)
  638. || (s->enc_write_ctx == NULL)
  639. || (EVP_MD_CTX_get0_md(s->write_hash) == NULL)) {
  640. clear = s->enc_write_ctx ? 0 : 1; /* must be AEAD cipher */
  641. mac_size = 0;
  642. } else {
  643. mac_size = EVP_MD_CTX_get_size(s->write_hash);
  644. if (mac_size < 0) {
  645. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  646. goto err;
  647. }
  648. }
  649. /*
  650. * 'create_empty_fragment' is true only when this function calls itself
  651. */
  652. if (!clear && !create_empty_fragment && !s->s3.empty_fragment_done) {
  653. /*
  654. * countermeasure against known-IV weakness in CBC ciphersuites (see
  655. * http://www.openssl.org/~bodo/tls-cbc.txt)
  656. */
  657. if (s->s3.need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) {
  658. /*
  659. * recursive function call with 'create_empty_fragment' set; this
  660. * prepares and buffers the data for an empty fragment (these
  661. * 'prefix_len' bytes are sent out later together with the actual
  662. * payload)
  663. */
  664. size_t tmppipelen = 0;
  665. int ret;
  666. ret = do_ssl3_write(s, type, buf, &tmppipelen, 1, 1, &prefix_len);
  667. if (ret <= 0) {
  668. /* SSLfatal() already called if appropriate */
  669. goto err;
  670. }
  671. if (prefix_len >
  672. (SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD)) {
  673. /* insufficient space */
  674. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  675. goto err;
  676. }
  677. }
  678. s->s3.empty_fragment_done = 1;
  679. }
  680. using_ktls = BIO_get_ktls_send(s->wbio);
  681. if (using_ktls) {
  682. /*
  683. * ktls doesn't modify the buffer, but to avoid a warning we need to
  684. * discard the const qualifier.
  685. * This doesn't leak memory because the buffers have been released when
  686. * switching to ktls.
  687. */
  688. SSL3_BUFFER_set_buf(&s->rlayer.wbuf[0], (unsigned char *)buf);
  689. SSL3_BUFFER_set_offset(&s->rlayer.wbuf[0], 0);
  690. SSL3_BUFFER_set_app_buffer(&s->rlayer.wbuf[0], 1);
  691. goto wpacket_init_complete;
  692. }
  693. if (create_empty_fragment) {
  694. wb = &s->rlayer.wbuf[0];
  695. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
  696. /*
  697. * extra fragment would be couple of cipher blocks, which would be
  698. * multiple of SSL3_ALIGN_PAYLOAD, so if we want to align the real
  699. * payload, then we can just pretend we simply have two headers.
  700. */
  701. align = (size_t)SSL3_BUFFER_get_buf(wb) + 2 * SSL3_RT_HEADER_LENGTH;
  702. align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);
  703. #endif
  704. SSL3_BUFFER_set_offset(wb, align);
  705. if (!WPACKET_init_static_len(&pkt[0], SSL3_BUFFER_get_buf(wb),
  706. SSL3_BUFFER_get_len(wb), 0)
  707. || !WPACKET_allocate_bytes(&pkt[0], align, NULL)) {
  708. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  709. goto err;
  710. }
  711. wpinited = 1;
  712. } else if (prefix_len) {
  713. wb = &s->rlayer.wbuf[0];
  714. if (!WPACKET_init_static_len(&pkt[0],
  715. SSL3_BUFFER_get_buf(wb),
  716. SSL3_BUFFER_get_len(wb), 0)
  717. || !WPACKET_allocate_bytes(&pkt[0], SSL3_BUFFER_get_offset(wb)
  718. + prefix_len, NULL)) {
  719. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  720. goto err;
  721. }
  722. wpinited = 1;
  723. } else {
  724. for (j = 0; j < numpipes; j++) {
  725. thispkt = &pkt[j];
  726. wb = &s->rlayer.wbuf[j];
  727. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0
  728. align = (size_t)SSL3_BUFFER_get_buf(wb) + SSL3_RT_HEADER_LENGTH;
  729. align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);
  730. #endif
  731. SSL3_BUFFER_set_offset(wb, align);
  732. if (!WPACKET_init_static_len(thispkt, SSL3_BUFFER_get_buf(wb),
  733. SSL3_BUFFER_get_len(wb), 0)
  734. || !WPACKET_allocate_bytes(thispkt, align, NULL)) {
  735. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  736. goto err;
  737. }
  738. wpinited++;
  739. }
  740. }
  741. /* Explicit IV length, block ciphers appropriate version flag */
  742. if (s->enc_write_ctx && SSL_USE_EXPLICIT_IV(s) && !SSL_TREAT_AS_TLS13(s)) {
  743. int mode = EVP_CIPHER_CTX_get_mode(s->enc_write_ctx);
  744. if (mode == EVP_CIPH_CBC_MODE) {
  745. eivlen = EVP_CIPHER_CTX_get_iv_length(s->enc_write_ctx);
  746. if (eivlen <= 1)
  747. eivlen = 0;
  748. } else if (mode == EVP_CIPH_GCM_MODE) {
  749. /* Need explicit part of IV for GCM mode */
  750. eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
  751. } else if (mode == EVP_CIPH_CCM_MODE) {
  752. eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
  753. }
  754. }
  755. wpacket_init_complete:
  756. totlen = 0;
  757. /* Clear our SSL3_RECORD structures */
  758. memset(wr, 0, sizeof(wr));
  759. for (j = 0; j < numpipes; j++) {
  760. unsigned int version = (s->version == TLS1_3_VERSION) ? TLS1_2_VERSION
  761. : s->version;
  762. unsigned char *compressdata = NULL;
  763. size_t maxcomplen;
  764. unsigned int rectype;
  765. thispkt = &pkt[j];
  766. thiswr = &wr[j];
  767. /*
  768. * In TLSv1.3, once encrypting, we always use application data for the
  769. * record type
  770. */
  771. if (SSL_TREAT_AS_TLS13(s)
  772. && s->enc_write_ctx != NULL
  773. && (s->statem.enc_write_state != ENC_WRITE_STATE_WRITE_PLAIN_ALERTS
  774. || type != SSL3_RT_ALERT))
  775. rectype = SSL3_RT_APPLICATION_DATA;
  776. else
  777. rectype = type;
  778. SSL3_RECORD_set_type(thiswr, rectype);
  779. /*
  780. * Some servers hang if initial client hello is larger than 256 bytes
  781. * and record version number > TLS 1.0
  782. */
  783. if (SSL_get_state(s) == TLS_ST_CW_CLNT_HELLO
  784. && !s->renegotiate
  785. && TLS1_get_version(s) > TLS1_VERSION
  786. && s->hello_retry_request == SSL_HRR_NONE)
  787. version = TLS1_VERSION;
  788. SSL3_RECORD_set_rec_version(thiswr, version);
  789. maxcomplen = pipelens[j];
  790. if (s->compress != NULL)
  791. maxcomplen += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
  792. /*
  793. * When using offload kernel will write the header.
  794. * Otherwise write the header now
  795. */
  796. if (!using_ktls
  797. && (!WPACKET_put_bytes_u8(thispkt, rectype)
  798. || !WPACKET_put_bytes_u16(thispkt, version)
  799. || !WPACKET_start_sub_packet_u16(thispkt)
  800. || (eivlen > 0
  801. && !WPACKET_allocate_bytes(thispkt, eivlen, NULL))
  802. || (maxcomplen > 0
  803. && !WPACKET_reserve_bytes(thispkt, maxcomplen,
  804. &compressdata)))) {
  805. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  806. goto err;
  807. }
  808. /* lets setup the record stuff. */
  809. SSL3_RECORD_set_data(thiswr, compressdata);
  810. SSL3_RECORD_set_length(thiswr, pipelens[j]);
  811. SSL3_RECORD_set_input(thiswr, (unsigned char *)&buf[totlen]);
  812. totlen += pipelens[j];
  813. /*
  814. * we now 'read' from thiswr->input, thiswr->length bytes into
  815. * thiswr->data
  816. */
  817. /* first we compress */
  818. if (s->compress != NULL) {
  819. if (!ssl3_do_compress(s, thiswr)
  820. || !WPACKET_allocate_bytes(thispkt, thiswr->length, NULL)) {
  821. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COMPRESSION_FAILURE);
  822. goto err;
  823. }
  824. } else {
  825. if (using_ktls) {
  826. SSL3_RECORD_reset_data(&wr[j]);
  827. } else {
  828. if (!WPACKET_memcpy(thispkt, thiswr->input, thiswr->length)) {
  829. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  830. goto err;
  831. }
  832. SSL3_RECORD_reset_input(&wr[j]);
  833. }
  834. }
  835. if (SSL_TREAT_AS_TLS13(s)
  836. && !using_ktls
  837. && s->enc_write_ctx != NULL
  838. && (s->statem.enc_write_state != ENC_WRITE_STATE_WRITE_PLAIN_ALERTS
  839. || type != SSL3_RT_ALERT)) {
  840. size_t rlen, max_send_fragment;
  841. if (!WPACKET_put_bytes_u8(thispkt, type)) {
  842. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  843. goto err;
  844. }
  845. SSL3_RECORD_add_length(thiswr, 1);
  846. /* Add TLS1.3 padding */
  847. max_send_fragment = ssl_get_max_send_fragment(s);
  848. rlen = SSL3_RECORD_get_length(thiswr);
  849. if (rlen < max_send_fragment) {
  850. size_t padding = 0;
  851. size_t max_padding = max_send_fragment - rlen;
  852. if (s->record_padding_cb != NULL) {
  853. padding = s->record_padding_cb(s, type, rlen, s->record_padding_arg);
  854. } else if (s->block_padding > 0) {
  855. size_t mask = s->block_padding - 1;
  856. size_t remainder;
  857. /* optimize for power of 2 */
  858. if ((s->block_padding & mask) == 0)
  859. remainder = rlen & mask;
  860. else
  861. remainder = rlen % s->block_padding;
  862. /* don't want to add a block of padding if we don't have to */
  863. if (remainder == 0)
  864. padding = 0;
  865. else
  866. padding = s->block_padding - remainder;
  867. }
  868. if (padding > 0) {
  869. /* do not allow the record to exceed max plaintext length */
  870. if (padding > max_padding)
  871. padding = max_padding;
  872. if (!WPACKET_memset(thispkt, 0, padding)) {
  873. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  874. ERR_R_INTERNAL_ERROR);
  875. goto err;
  876. }
  877. SSL3_RECORD_add_length(thiswr, padding);
  878. }
  879. }
  880. }
  881. /*
  882. * we should still have the output to thiswr->data and the input from
  883. * wr->input. Length should be thiswr->length. thiswr->data still points
  884. * in the wb->buf
  885. */
  886. if (!using_ktls && !SSL_WRITE_ETM(s) && mac_size != 0) {
  887. unsigned char *mac;
  888. if (!WPACKET_allocate_bytes(thispkt, mac_size, &mac)
  889. || !s->method->ssl3_enc->mac(s, thiswr, mac, 1)) {
  890. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  891. goto err;
  892. }
  893. }
  894. /*
  895. * Reserve some bytes for any growth that may occur during encryption.
  896. * This will be at most one cipher block or the tag length if using
  897. * AEAD. SSL_RT_MAX_CIPHER_BLOCK_SIZE covers either case.
  898. */
  899. if (!using_ktls) {
  900. if (!WPACKET_reserve_bytes(thispkt,
  901. SSL_RT_MAX_CIPHER_BLOCK_SIZE,
  902. NULL)
  903. /*
  904. * We also need next the amount of bytes written to this
  905. * sub-packet
  906. */
  907. || !WPACKET_get_length(thispkt, &len)) {
  908. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  909. goto err;
  910. }
  911. /* Get a pointer to the start of this record excluding header */
  912. recordstart = WPACKET_get_curr(thispkt) - len;
  913. SSL3_RECORD_set_data(thiswr, recordstart);
  914. SSL3_RECORD_reset_input(thiswr);
  915. SSL3_RECORD_set_length(thiswr, len);
  916. }
  917. }
  918. if (s->statem.enc_write_state == ENC_WRITE_STATE_WRITE_PLAIN_ALERTS) {
  919. /*
  920. * We haven't actually negotiated the version yet, but we're trying to
  921. * send early data - so we need to use the tls13enc function.
  922. */
  923. if (tls13_enc(s, wr, numpipes, 1, NULL, mac_size) < 1) {
  924. if (!ossl_statem_in_error(s)) {
  925. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  926. }
  927. goto err;
  928. }
  929. } else {
  930. if (!using_ktls) {
  931. if (s->method->ssl3_enc->enc(s, wr, numpipes, 1, NULL,
  932. mac_size) < 1) {
  933. if (!ossl_statem_in_error(s)) {
  934. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  935. }
  936. goto err;
  937. }
  938. }
  939. }
  940. for (j = 0; j < numpipes; j++) {
  941. size_t origlen;
  942. thispkt = &pkt[j];
  943. thiswr = &wr[j];
  944. if (using_ktls)
  945. goto mac_done;
  946. /* Allocate bytes for the encryption overhead */
  947. if (!WPACKET_get_length(thispkt, &origlen)
  948. /* Encryption should never shrink the data! */
  949. || origlen > thiswr->length
  950. || (thiswr->length > origlen
  951. && !WPACKET_allocate_bytes(thispkt,
  952. thiswr->length - origlen,
  953. NULL))) {
  954. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  955. goto err;
  956. }
  957. if (SSL_WRITE_ETM(s) && mac_size != 0) {
  958. unsigned char *mac;
  959. if (!WPACKET_allocate_bytes(thispkt, mac_size, &mac)
  960. || !s->method->ssl3_enc->mac(s, thiswr, mac, 1)) {
  961. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  962. goto err;
  963. }
  964. SSL3_RECORD_add_length(thiswr, mac_size);
  965. }
  966. if (!WPACKET_get_length(thispkt, &len)
  967. || !WPACKET_close(thispkt)) {
  968. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  969. goto err;
  970. }
  971. if (s->msg_callback) {
  972. recordstart = WPACKET_get_curr(thispkt) - len
  973. - SSL3_RT_HEADER_LENGTH;
  974. s->msg_callback(1, thiswr->rec_version, SSL3_RT_HEADER, recordstart,
  975. SSL3_RT_HEADER_LENGTH, s,
  976. s->msg_callback_arg);
  977. if (SSL_TREAT_AS_TLS13(s) && s->enc_write_ctx != NULL) {
  978. unsigned char ctype = type;
  979. s->msg_callback(1, thiswr->rec_version, SSL3_RT_INNER_CONTENT_TYPE,
  980. &ctype, 1, s, s->msg_callback_arg);
  981. }
  982. }
  983. if (!WPACKET_finish(thispkt)) {
  984. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  985. goto err;
  986. }
  987. /* header is added by the kernel when using offload */
  988. SSL3_RECORD_add_length(thiswr, SSL3_RT_HEADER_LENGTH);
  989. if (create_empty_fragment) {
  990. /*
  991. * we are in a recursive call; just return the length, don't write
  992. * out anything here
  993. */
  994. if (j > 0) {
  995. /* We should never be pipelining an empty fragment!! */
  996. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  997. goto err;
  998. }
  999. *written = SSL3_RECORD_get_length(thiswr);
  1000. return 1;
  1001. }
  1002. mac_done:
  1003. /*
  1004. * we should now have thiswr->data pointing to the encrypted data, which
  1005. * is thiswr->length long
  1006. */
  1007. SSL3_RECORD_set_type(thiswr, type); /* not needed but helps for
  1008. * debugging */
  1009. /* now let's set up wb */
  1010. SSL3_BUFFER_set_left(&s->rlayer.wbuf[j],
  1011. prefix_len + SSL3_RECORD_get_length(thiswr));
  1012. }
  1013. /*
  1014. * memorize arguments so that ssl3_write_pending can detect bad write
  1015. * retries later
  1016. */
  1017. s->rlayer.wpend_tot = totlen;
  1018. s->rlayer.wpend_buf = buf;
  1019. s->rlayer.wpend_type = type;
  1020. s->rlayer.wpend_ret = totlen;
  1021. /* we now just need to write the buffer */
  1022. return ssl3_write_pending(s, type, buf, totlen, written);
  1023. err:
  1024. for (j = 0; j < wpinited; j++)
  1025. WPACKET_cleanup(&pkt[j]);
  1026. return -1;
  1027. }
  1028. /* if SSL3_BUFFER_get_left() != 0, we need to call this
  1029. *
  1030. * Return values are as per SSL_write()
  1031. */
  1032. int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len,
  1033. size_t *written)
  1034. {
  1035. int i;
  1036. SSL3_BUFFER *wb = s->rlayer.wbuf;
  1037. size_t currbuf = 0;
  1038. size_t tmpwrit = 0;
  1039. if ((s->rlayer.wpend_tot > len)
  1040. || (!(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)
  1041. && (s->rlayer.wpend_buf != buf))
  1042. || (s->rlayer.wpend_type != type)) {
  1043. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_WRITE_RETRY);
  1044. return -1;
  1045. }
  1046. for (;;) {
  1047. /* Loop until we find a buffer we haven't written out yet */
  1048. if (SSL3_BUFFER_get_left(&wb[currbuf]) == 0
  1049. && currbuf < s->rlayer.numwpipes - 1) {
  1050. currbuf++;
  1051. continue;
  1052. }
  1053. clear_sys_error();
  1054. if (s->wbio != NULL) {
  1055. s->rwstate = SSL_WRITING;
  1056. /*
  1057. * To prevent coalescing of control and data messages,
  1058. * such as in buffer_write, we flush the BIO
  1059. */
  1060. if (BIO_get_ktls_send(s->wbio) && type != SSL3_RT_APPLICATION_DATA) {
  1061. i = BIO_flush(s->wbio);
  1062. if (i <= 0)
  1063. return i;
  1064. BIO_set_ktls_ctrl_msg(s->wbio, type);
  1065. }
  1066. i = BIO_write(s->wbio, (char *)
  1067. &(SSL3_BUFFER_get_buf(&wb[currbuf])
  1068. [SSL3_BUFFER_get_offset(&wb[currbuf])]),
  1069. (unsigned int)SSL3_BUFFER_get_left(&wb[currbuf]));
  1070. if (i >= 0)
  1071. tmpwrit = i;
  1072. } else {
  1073. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BIO_NOT_SET);
  1074. i = -1;
  1075. }
  1076. /*
  1077. * When an empty fragment is sent on a connection using KTLS,
  1078. * it is sent as a write of zero bytes. If this zero byte
  1079. * write succeeds, i will be 0 rather than a non-zero value.
  1080. * Treat i == 0 as success rather than an error for zero byte
  1081. * writes to permit this case.
  1082. */
  1083. if (i >= 0 && tmpwrit == SSL3_BUFFER_get_left(&wb[currbuf])) {
  1084. SSL3_BUFFER_set_left(&wb[currbuf], 0);
  1085. SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);
  1086. if (currbuf + 1 < s->rlayer.numwpipes)
  1087. continue;
  1088. s->rwstate = SSL_NOTHING;
  1089. *written = s->rlayer.wpend_ret;
  1090. return 1;
  1091. } else if (i <= 0) {
  1092. if (SSL_IS_DTLS(s)) {
  1093. /*
  1094. * For DTLS, just drop it. That's kind of the whole point in
  1095. * using a datagram service
  1096. */
  1097. SSL3_BUFFER_set_left(&wb[currbuf], 0);
  1098. }
  1099. return i;
  1100. }
  1101. SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);
  1102. SSL3_BUFFER_sub_left(&wb[currbuf], tmpwrit);
  1103. }
  1104. }
  1105. /*-
  1106. * Return up to 'len' payload bytes received in 'type' records.
  1107. * 'type' is one of the following:
  1108. *
  1109. * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
  1110. * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
  1111. * - 0 (during a shutdown, no data has to be returned)
  1112. *
  1113. * If we don't have stored data to work from, read a SSL/TLS record first
  1114. * (possibly multiple records if we still don't have anything to return).
  1115. *
  1116. * This function must handle any surprises the peer may have for us, such as
  1117. * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec
  1118. * messages are treated as if they were handshake messages *if* the |recvd_type|
  1119. * argument is non NULL.
  1120. * Also if record payloads contain fragments too small to process, we store
  1121. * them until there is enough for the respective protocol (the record protocol
  1122. * may use arbitrary fragmentation and even interleaving):
  1123. * Change cipher spec protocol
  1124. * just 1 byte needed, no need for keeping anything stored
  1125. * Alert protocol
  1126. * 2 bytes needed (AlertLevel, AlertDescription)
  1127. * Handshake protocol
  1128. * 4 bytes needed (HandshakeType, uint24 length) -- we just have
  1129. * to detect unexpected Client Hello and Hello Request messages
  1130. * here, anything else is handled by higher layers
  1131. * Application data protocol
  1132. * none of our business
  1133. */
  1134. int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
  1135. size_t len, int peek, size_t *readbytes)
  1136. {
  1137. int i, j, ret;
  1138. size_t n, curr_rec, num_recs, totalbytes;
  1139. SSL3_RECORD *rr;
  1140. SSL3_BUFFER *rbuf;
  1141. void (*cb) (const SSL *ssl, int type2, int val) = NULL;
  1142. int is_tls13 = SSL_IS_TLS13(s);
  1143. rbuf = &s->rlayer.rbuf;
  1144. if (!SSL3_BUFFER_is_initialised(rbuf)) {
  1145. /* Not initialized yet */
  1146. if (!ssl3_setup_read_buffer(s)) {
  1147. /* SSLfatal() already called */
  1148. return -1;
  1149. }
  1150. }
  1151. if ((type && (type != SSL3_RT_APPLICATION_DATA)
  1152. && (type != SSL3_RT_HANDSHAKE)) || (peek
  1153. && (type !=
  1154. SSL3_RT_APPLICATION_DATA))) {
  1155. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1156. return -1;
  1157. }
  1158. if ((type == SSL3_RT_HANDSHAKE) && (s->rlayer.handshake_fragment_len > 0))
  1159. /* (partially) satisfy request from storage */
  1160. {
  1161. unsigned char *src = s->rlayer.handshake_fragment;
  1162. unsigned char *dst = buf;
  1163. unsigned int k;
  1164. /* peek == 0 */
  1165. n = 0;
  1166. while ((len > 0) && (s->rlayer.handshake_fragment_len > 0)) {
  1167. *dst++ = *src++;
  1168. len--;
  1169. s->rlayer.handshake_fragment_len--;
  1170. n++;
  1171. }
  1172. /* move any remaining fragment bytes: */
  1173. for (k = 0; k < s->rlayer.handshake_fragment_len; k++)
  1174. s->rlayer.handshake_fragment[k] = *src++;
  1175. if (recvd_type != NULL)
  1176. *recvd_type = SSL3_RT_HANDSHAKE;
  1177. *readbytes = n;
  1178. return 1;
  1179. }
  1180. /*
  1181. * Now s->rlayer.handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE.
  1182. */
  1183. if (!ossl_statem_get_in_handshake(s) && SSL_in_init(s)) {
  1184. /* type == SSL3_RT_APPLICATION_DATA */
  1185. i = s->handshake_func(s);
  1186. /* SSLfatal() already called */
  1187. if (i < 0)
  1188. return i;
  1189. if (i == 0)
  1190. return -1;
  1191. }
  1192. start:
  1193. s->rwstate = SSL_NOTHING;
  1194. /*-
  1195. * For each record 'i' up to |num_recs]
  1196. * rr[i].type - is the type of record
  1197. * rr[i].data, - data
  1198. * rr[i].off, - offset into 'data' for next read
  1199. * rr[i].length, - number of bytes.
  1200. */
  1201. rr = s->rlayer.rrec;
  1202. num_recs = RECORD_LAYER_get_numrpipes(&s->rlayer);
  1203. do {
  1204. /* get new records if necessary */
  1205. if (num_recs == 0) {
  1206. ret = ssl3_get_record(s);
  1207. if (ret <= 0) {
  1208. /* SSLfatal() already called if appropriate */
  1209. return ret;
  1210. }
  1211. num_recs = RECORD_LAYER_get_numrpipes(&s->rlayer);
  1212. if (num_recs == 0) {
  1213. /* Shouldn't happen */
  1214. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1215. return -1;
  1216. }
  1217. }
  1218. /* Skip over any records we have already read */
  1219. for (curr_rec = 0;
  1220. curr_rec < num_recs && SSL3_RECORD_is_read(&rr[curr_rec]);
  1221. curr_rec++) ;
  1222. if (curr_rec == num_recs) {
  1223. RECORD_LAYER_set_numrpipes(&s->rlayer, 0);
  1224. num_recs = 0;
  1225. curr_rec = 0;
  1226. }
  1227. } while (num_recs == 0);
  1228. rr = &rr[curr_rec];
  1229. if (s->rlayer.handshake_fragment_len > 0
  1230. && SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE
  1231. && SSL_IS_TLS13(s)) {
  1232. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  1233. SSL_R_MIXED_HANDSHAKE_AND_NON_HANDSHAKE_DATA);
  1234. return -1;
  1235. }
  1236. /*
  1237. * Reset the count of consecutive warning alerts if we've got a non-empty
  1238. * record that isn't an alert.
  1239. */
  1240. if (SSL3_RECORD_get_type(rr) != SSL3_RT_ALERT
  1241. && SSL3_RECORD_get_length(rr) != 0)
  1242. s->rlayer.alert_count = 0;
  1243. /* we now have a packet which can be read and processed */
  1244. if (s->s3.change_cipher_spec /* set when we receive ChangeCipherSpec,
  1245. * reset by ssl3_get_finished */
  1246. && (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) {
  1247. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  1248. SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
  1249. return -1;
  1250. }
  1251. /*
  1252. * If the other end has shut down, throw anything we read away (even in
  1253. * 'peek' mode)
  1254. */
  1255. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  1256. SSL3_RECORD_set_length(rr, 0);
  1257. s->rwstate = SSL_NOTHING;
  1258. return 0;
  1259. }
  1260. if (type == SSL3_RECORD_get_type(rr)
  1261. || (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC
  1262. && type == SSL3_RT_HANDSHAKE && recvd_type != NULL
  1263. && !is_tls13)) {
  1264. /*
  1265. * SSL3_RT_APPLICATION_DATA or
  1266. * SSL3_RT_HANDSHAKE or
  1267. * SSL3_RT_CHANGE_CIPHER_SPEC
  1268. */
  1269. /*
  1270. * make sure that we are not getting application data when we are
  1271. * doing a handshake for the first time
  1272. */
  1273. if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
  1274. (s->enc_read_ctx == NULL)) {
  1275. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_APP_DATA_IN_HANDSHAKE);
  1276. return -1;
  1277. }
  1278. if (type == SSL3_RT_HANDSHAKE
  1279. && SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC
  1280. && s->rlayer.handshake_fragment_len > 0) {
  1281. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_CCS_RECEIVED_EARLY);
  1282. return -1;
  1283. }
  1284. if (recvd_type != NULL)
  1285. *recvd_type = SSL3_RECORD_get_type(rr);
  1286. if (len == 0) {
  1287. /*
  1288. * Mark a zero length record as read. This ensures multiple calls to
  1289. * SSL_read() with a zero length buffer will eventually cause
  1290. * SSL_pending() to report data as being available.
  1291. */
  1292. if (SSL3_RECORD_get_length(rr) == 0)
  1293. SSL3_RECORD_set_read(rr);
  1294. return 0;
  1295. }
  1296. totalbytes = 0;
  1297. do {
  1298. if (len - totalbytes > SSL3_RECORD_get_length(rr))
  1299. n = SSL3_RECORD_get_length(rr);
  1300. else
  1301. n = len - totalbytes;
  1302. memcpy(buf, &(rr->data[rr->off]), n);
  1303. buf += n;
  1304. if (peek) {
  1305. /* Mark any zero length record as consumed CVE-2016-6305 */
  1306. if (SSL3_RECORD_get_length(rr) == 0)
  1307. SSL3_RECORD_set_read(rr);
  1308. } else {
  1309. if (s->options & SSL_OP_CLEANSE_PLAINTEXT)
  1310. OPENSSL_cleanse(&(rr->data[rr->off]), n);
  1311. SSL3_RECORD_sub_length(rr, n);
  1312. SSL3_RECORD_add_off(rr, n);
  1313. if (SSL3_RECORD_get_length(rr) == 0) {
  1314. s->rlayer.rstate = SSL_ST_READ_HEADER;
  1315. SSL3_RECORD_set_off(rr, 0);
  1316. SSL3_RECORD_set_read(rr);
  1317. }
  1318. }
  1319. if (SSL3_RECORD_get_length(rr) == 0
  1320. || (peek && n == SSL3_RECORD_get_length(rr))) {
  1321. curr_rec++;
  1322. rr++;
  1323. }
  1324. totalbytes += n;
  1325. } while (type == SSL3_RT_APPLICATION_DATA && curr_rec < num_recs
  1326. && totalbytes < len);
  1327. if (totalbytes == 0) {
  1328. /* We must have read empty records. Get more data */
  1329. goto start;
  1330. }
  1331. if (!peek && curr_rec == num_recs
  1332. && (s->mode & SSL_MODE_RELEASE_BUFFERS)
  1333. && SSL3_BUFFER_get_left(rbuf) == 0)
  1334. ssl3_release_read_buffer(s);
  1335. *readbytes = totalbytes;
  1336. return 1;
  1337. }
  1338. /*
  1339. * If we get here, then type != rr->type; if we have a handshake message,
  1340. * then it was unexpected (Hello Request or Client Hello) or invalid (we
  1341. * were actually expecting a CCS).
  1342. */
  1343. /*
  1344. * Lets just double check that we've not got an SSLv2 record
  1345. */
  1346. if (rr->rec_version == SSL2_VERSION) {
  1347. /*
  1348. * Should never happen. ssl3_get_record() should only give us an SSLv2
  1349. * record back if this is the first packet and we are looking for an
  1350. * initial ClientHello. Therefore |type| should always be equal to
  1351. * |rr->type|. If not then something has gone horribly wrong
  1352. */
  1353. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1354. return -1;
  1355. }
  1356. if (s->method->version == TLS_ANY_VERSION
  1357. && (s->server || rr->type != SSL3_RT_ALERT)) {
  1358. /*
  1359. * If we've got this far and still haven't decided on what version
  1360. * we're using then this must be a client side alert we're dealing
  1361. * with. We shouldn't be receiving anything other than a ClientHello
  1362. * if we are a server.
  1363. */
  1364. s->version = rr->rec_version;
  1365. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
  1366. return -1;
  1367. }
  1368. /*-
  1369. * s->rlayer.handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
  1370. * (Possibly rr is 'empty' now, i.e. rr->length may be 0.)
  1371. */
  1372. if (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) {
  1373. unsigned int alert_level, alert_descr;
  1374. unsigned char *alert_bytes = SSL3_RECORD_get_data(rr)
  1375. + SSL3_RECORD_get_off(rr);
  1376. PACKET alert;
  1377. if (!PACKET_buf_init(&alert, alert_bytes, SSL3_RECORD_get_length(rr))
  1378. || !PACKET_get_1(&alert, &alert_level)
  1379. || !PACKET_get_1(&alert, &alert_descr)
  1380. || PACKET_remaining(&alert) != 0) {
  1381. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_INVALID_ALERT);
  1382. return -1;
  1383. }
  1384. if (s->msg_callback)
  1385. s->msg_callback(0, s->version, SSL3_RT_ALERT, alert_bytes, 2, s,
  1386. s->msg_callback_arg);
  1387. if (s->info_callback != NULL)
  1388. cb = s->info_callback;
  1389. else if (s->ctx->info_callback != NULL)
  1390. cb = s->ctx->info_callback;
  1391. if (cb != NULL) {
  1392. j = (alert_level << 8) | alert_descr;
  1393. cb(s, SSL_CB_READ_ALERT, j);
  1394. }
  1395. if (alert_level == SSL3_AL_WARNING
  1396. || (is_tls13 && alert_descr == SSL_AD_USER_CANCELLED)) {
  1397. s->s3.warn_alert = alert_descr;
  1398. SSL3_RECORD_set_read(rr);
  1399. s->rlayer.alert_count++;
  1400. if (s->rlayer.alert_count == MAX_WARN_ALERT_COUNT) {
  1401. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  1402. SSL_R_TOO_MANY_WARN_ALERTS);
  1403. return -1;
  1404. }
  1405. }
  1406. /*
  1407. * Apart from close_notify the only other warning alert in TLSv1.3
  1408. * is user_cancelled - which we just ignore.
  1409. */
  1410. if (is_tls13 && alert_descr == SSL_AD_USER_CANCELLED) {
  1411. goto start;
  1412. } else if (alert_descr == SSL_AD_CLOSE_NOTIFY
  1413. && (is_tls13 || alert_level == SSL3_AL_WARNING)) {
  1414. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  1415. return 0;
  1416. } else if (alert_level == SSL3_AL_FATAL || is_tls13) {
  1417. s->rwstate = SSL_NOTHING;
  1418. s->s3.fatal_alert = alert_descr;
  1419. SSLfatal_data(s, SSL_AD_NO_ALERT,
  1420. SSL_AD_REASON_OFFSET + alert_descr,
  1421. "SSL alert number %d", alert_descr);
  1422. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  1423. SSL3_RECORD_set_read(rr);
  1424. SSL_CTX_remove_session(s->session_ctx, s->session);
  1425. return 0;
  1426. } else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
  1427. /*
  1428. * This is a warning but we receive it if we requested
  1429. * renegotiation and the peer denied it. Terminate with a fatal
  1430. * alert because if application tried to renegotiate it
  1431. * presumably had a good reason and expects it to succeed. In
  1432. * future we might have a renegotiation where we don't care if
  1433. * the peer refused it where we carry on.
  1434. */
  1435. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_RENEGOTIATION);
  1436. return -1;
  1437. } else if (alert_level == SSL3_AL_WARNING) {
  1438. /* We ignore any other warning alert in TLSv1.2 and below */
  1439. goto start;
  1440. }
  1441. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_ALERT_TYPE);
  1442. return -1;
  1443. }
  1444. if ((s->shutdown & SSL_SENT_SHUTDOWN) != 0) {
  1445. if (SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) {
  1446. BIO *rbio;
  1447. /*
  1448. * We ignore any handshake messages sent to us unless they are
  1449. * TLSv1.3 in which case we want to process them. For all other
  1450. * handshake messages we can't do anything reasonable with them
  1451. * because we are unable to write any response due to having already
  1452. * sent close_notify.
  1453. */
  1454. if (!SSL_IS_TLS13(s)) {
  1455. SSL3_RECORD_set_length(rr, 0);
  1456. SSL3_RECORD_set_read(rr);
  1457. if ((s->mode & SSL_MODE_AUTO_RETRY) != 0)
  1458. goto start;
  1459. s->rwstate = SSL_READING;
  1460. rbio = SSL_get_rbio(s);
  1461. BIO_clear_retry_flags(rbio);
  1462. BIO_set_retry_read(rbio);
  1463. return -1;
  1464. }
  1465. } else {
  1466. /*
  1467. * The peer is continuing to send application data, but we have
  1468. * already sent close_notify. If this was expected we should have
  1469. * been called via SSL_read() and this would have been handled
  1470. * above.
  1471. * No alert sent because we already sent close_notify
  1472. */
  1473. SSL3_RECORD_set_length(rr, 0);
  1474. SSL3_RECORD_set_read(rr);
  1475. SSLfatal(s, SSL_AD_NO_ALERT,
  1476. SSL_R_APPLICATION_DATA_AFTER_CLOSE_NOTIFY);
  1477. return -1;
  1478. }
  1479. }
  1480. /*
  1481. * For handshake data we have 'fragment' storage, so fill that so that we
  1482. * can process the header at a fixed place. This is done after the
  1483. * "SHUTDOWN" code above to avoid filling the fragment storage with data
  1484. * that we're just going to discard.
  1485. */
  1486. if (SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) {
  1487. size_t dest_maxlen = sizeof(s->rlayer.handshake_fragment);
  1488. unsigned char *dest = s->rlayer.handshake_fragment;
  1489. size_t *dest_len = &s->rlayer.handshake_fragment_len;
  1490. n = dest_maxlen - *dest_len; /* available space in 'dest' */
  1491. if (SSL3_RECORD_get_length(rr) < n)
  1492. n = SSL3_RECORD_get_length(rr); /* available bytes */
  1493. /* now move 'n' bytes: */
  1494. memcpy(dest + *dest_len,
  1495. SSL3_RECORD_get_data(rr) + SSL3_RECORD_get_off(rr), n);
  1496. SSL3_RECORD_add_off(rr, n);
  1497. SSL3_RECORD_sub_length(rr, n);
  1498. *dest_len += n;
  1499. if (SSL3_RECORD_get_length(rr) == 0)
  1500. SSL3_RECORD_set_read(rr);
  1501. if (*dest_len < dest_maxlen)
  1502. goto start; /* fragment was too small */
  1503. }
  1504. if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) {
  1505. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_CCS_RECEIVED_EARLY);
  1506. return -1;
  1507. }
  1508. /*
  1509. * Unexpected handshake message (ClientHello, NewSessionTicket (TLS1.3) or
  1510. * protocol violation)
  1511. */
  1512. if ((s->rlayer.handshake_fragment_len >= 4)
  1513. && !ossl_statem_get_in_handshake(s)) {
  1514. int ined = (s->early_data_state == SSL_EARLY_DATA_READING);
  1515. /* We found handshake data, so we're going back into init */
  1516. ossl_statem_set_in_init(s, 1);
  1517. i = s->handshake_func(s);
  1518. /* SSLfatal() already called if appropriate */
  1519. if (i < 0)
  1520. return i;
  1521. if (i == 0) {
  1522. return -1;
  1523. }
  1524. /*
  1525. * If we were actually trying to read early data and we found a
  1526. * handshake message, then we don't want to continue to try and read
  1527. * the application data any more. It won't be "early" now.
  1528. */
  1529. if (ined)
  1530. return -1;
  1531. if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
  1532. if (SSL3_BUFFER_get_left(rbuf) == 0) {
  1533. /* no read-ahead left? */
  1534. BIO *bio;
  1535. /*
  1536. * In the case where we try to read application data, but we
  1537. * trigger an SSL handshake, we return -1 with the retry
  1538. * option set. Otherwise renegotiation may cause nasty
  1539. * problems in the blocking world
  1540. */
  1541. s->rwstate = SSL_READING;
  1542. bio = SSL_get_rbio(s);
  1543. BIO_clear_retry_flags(bio);
  1544. BIO_set_retry_read(bio);
  1545. return -1;
  1546. }
  1547. }
  1548. goto start;
  1549. }
  1550. switch (SSL3_RECORD_get_type(rr)) {
  1551. default:
  1552. /*
  1553. * TLS 1.0 and 1.1 say you SHOULD ignore unrecognised record types, but
  1554. * TLS 1.2 says you MUST send an unexpected message alert. We use the
  1555. * TLS 1.2 behaviour for all protocol versions to prevent issues where
  1556. * no progress is being made and the peer continually sends unrecognised
  1557. * record types, using up resources processing them.
  1558. */
  1559. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD);
  1560. return -1;
  1561. case SSL3_RT_CHANGE_CIPHER_SPEC:
  1562. case SSL3_RT_ALERT:
  1563. case SSL3_RT_HANDSHAKE:
  1564. /*
  1565. * we already handled all of these, with the possible exception of
  1566. * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but
  1567. * that should not happen when type != rr->type
  1568. */
  1569. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, ERR_R_INTERNAL_ERROR);
  1570. return -1;
  1571. case SSL3_RT_APPLICATION_DATA:
  1572. /*
  1573. * At this point, we were expecting handshake data, but have
  1574. * application data. If the library was running inside ssl3_read()
  1575. * (i.e. in_read_app_data is set) and it makes sense to read
  1576. * application data at this point (session renegotiation not yet
  1577. * started), we will indulge it.
  1578. */
  1579. if (ossl_statem_app_data_allowed(s)) {
  1580. s->s3.in_read_app_data = 2;
  1581. return -1;
  1582. } else if (ossl_statem_skip_early_data(s)) {
  1583. /*
  1584. * This can happen after a client sends a CH followed by early_data,
  1585. * but the server responds with a HelloRetryRequest. The server
  1586. * reads the next record from the client expecting to find a
  1587. * plaintext ClientHello but gets a record which appears to be
  1588. * application data. The trial decrypt "works" because null
  1589. * decryption was applied. We just skip it and move on to the next
  1590. * record.
  1591. */
  1592. if (!early_data_count_ok(s, rr->length,
  1593. EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) {
  1594. /* SSLfatal() already called */
  1595. return -1;
  1596. }
  1597. SSL3_RECORD_set_read(rr);
  1598. goto start;
  1599. } else {
  1600. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD);
  1601. return -1;
  1602. }
  1603. }
  1604. }
  1605. void ssl3_record_sequence_update(unsigned char *seq)
  1606. {
  1607. int i;
  1608. for (i = 7; i >= 0; i--) {
  1609. ++seq[i];
  1610. if (seq[i] != 0)
  1611. break;
  1612. }
  1613. }
  1614. /*
  1615. * Returns true if the current rrec was sent in SSLv2 backwards compatible
  1616. * format and false otherwise.
  1617. */
  1618. int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl)
  1619. {
  1620. return SSL3_RECORD_is_sslv2_record(&rl->rrec[0]);
  1621. }
  1622. /*
  1623. * Returns the length in bytes of the current rrec
  1624. */
  1625. size_t RECORD_LAYER_get_rrec_length(RECORD_LAYER *rl)
  1626. {
  1627. return SSL3_RECORD_get_length(&rl->rrec[0]);
  1628. }