rec_layer_s3.c 62 KB

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