rec_layer_s3.c 61 KB

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