rec_layer_s3.c 62 KB

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