rec_layer_s3.c 63 KB

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