rec_layer_s3.c 61 KB

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