rec_layer_s3.c 62 KB

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