rec_layer_s3.c 62 KB

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