rec_layer_s3.c 60 KB

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