rec_layer_s3.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  1. /* ssl/record/rec_layer_s3.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. #include <stdio.h>
  112. #include <limits.h>
  113. #include <errno.h>
  114. #define USE_SOCKETS
  115. #include "../ssl_locl.h"
  116. #include <openssl/evp.h>
  117. #include <openssl/buffer.h>
  118. #include <openssl/rand.h>
  119. #include "record_locl.h"
  120. #ifndef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
  121. # define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0
  122. #endif
  123. #if defined(OPENSSL_SMALL_FOOTPRINT) || \
  124. !( defined(AES_ASM) && ( \
  125. defined(__x86_64) || defined(__x86_64__) || \
  126. defined(_M_AMD64) || defined(_M_X64) || \
  127. defined(__INTEL__) ) \
  128. )
  129. # undef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
  130. # define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0
  131. #endif
  132. void RECORD_LAYER_init(RECORD_LAYER *rl, SSL *s)
  133. {
  134. rl->s = s;
  135. SSL3_RECORD_clear(&rl->rrec);
  136. SSL3_RECORD_clear(&rl->wrec);
  137. }
  138. void RECORD_LAYER_clear(RECORD_LAYER *rl)
  139. {
  140. rl->rstate = SSL_ST_READ_HEADER;
  141. /* Do I need to clear read_ahead? As far as I can tell read_ahead did not
  142. * previously get reset by SSL_clear...so I'll keep it that way..but is
  143. * that right?
  144. */
  145. rl->packet = NULL;
  146. rl->packet_length = 0;
  147. rl->wnum = 0;
  148. memset(rl->alert_fragment, 0, sizeof(rl->alert_fragment));
  149. rl->alert_fragment_len = 0;
  150. memset(rl->handshake_fragment, 0, sizeof(rl->handshake_fragment));
  151. rl->handshake_fragment_len = 0;
  152. rl->wpend_tot = 0;
  153. rl->wpend_type = 0;
  154. rl->wpend_ret = 0;
  155. rl->wpend_buf = NULL;
  156. SSL3_BUFFER_clear(&rl->rbuf);
  157. SSL3_BUFFER_clear(&rl->wbuf);
  158. SSL3_RECORD_clear(&rl->rrec);
  159. SSL3_RECORD_clear(&rl->wrec);
  160. RECORD_LAYER_reset_read_sequence(rl);
  161. RECORD_LAYER_reset_write_sequence(rl);
  162. if (rl->d)
  163. DTLS_RECORD_LAYER_clear(rl);
  164. }
  165. void RECORD_LAYER_release(RECORD_LAYER *rl)
  166. {
  167. if (SSL3_BUFFER_is_initialised(&rl->rbuf))
  168. ssl3_release_read_buffer(rl->s);
  169. if (SSL3_BUFFER_is_initialised(&rl->wbuf))
  170. ssl3_release_write_buffer(rl->s);
  171. SSL3_RECORD_release(&rl->rrec);
  172. }
  173. int RECORD_LAYER_read_pending(RECORD_LAYER *rl)
  174. {
  175. return SSL3_BUFFER_get_left(&rl->rbuf) != 0;
  176. }
  177. int RECORD_LAYER_write_pending(RECORD_LAYER *rl)
  178. {
  179. return SSL3_BUFFER_get_left(&rl->wbuf) != 0;
  180. }
  181. int RECORD_LAYER_set_data(RECORD_LAYER *rl, const unsigned char *buf, int len)
  182. {
  183. rl->packet_length = len;
  184. if (len != 0) {
  185. rl->rstate = SSL_ST_READ_HEADER;
  186. if (!SSL3_BUFFER_is_initialised(&rl->rbuf))
  187. if (!ssl3_setup_read_buffer(rl->s))
  188. return 0;
  189. }
  190. rl->packet = SSL3_BUFFER_get_buf(&rl->rbuf);
  191. SSL3_BUFFER_set_data(&rl->rbuf, buf, len);
  192. return 1;
  193. }
  194. void RECORD_LAYER_reset_read_sequence(RECORD_LAYER *rl)
  195. {
  196. memset(rl->read_sequence, 0, sizeof(rl->read_sequence));
  197. }
  198. void RECORD_LAYER_reset_write_sequence(RECORD_LAYER *rl)
  199. {
  200. memset(rl->write_sequence, 0, sizeof(rl->write_sequence));
  201. }
  202. int RECORD_LAYER_setup_comp_buffer(RECORD_LAYER *rl)
  203. {
  204. return SSL3_RECORD_setup(&(rl)->rrec);
  205. }
  206. int ssl3_pending(const SSL *s)
  207. {
  208. if (s->rlayer.rstate == SSL_ST_READ_BODY)
  209. return 0;
  210. return (SSL3_RECORD_get_type(&s->rlayer.rrec) == SSL3_RT_APPLICATION_DATA)
  211. ? SSL3_RECORD_get_length(&s->rlayer.rrec) : 0;
  212. }
  213. const char *SSL_rstate_string_long(const SSL *s)
  214. {
  215. const char *str;
  216. switch (s->rlayer.rstate) {
  217. case SSL_ST_READ_HEADER:
  218. str = "read header";
  219. break;
  220. case SSL_ST_READ_BODY:
  221. str = "read body";
  222. break;
  223. case SSL_ST_READ_DONE:
  224. str = "read done";
  225. break;
  226. default:
  227. str = "unknown";
  228. break;
  229. }
  230. return (str);
  231. }
  232. const char *SSL_rstate_string(const SSL *s)
  233. {
  234. const char *str;
  235. switch (s->rlayer.rstate) {
  236. case SSL_ST_READ_HEADER:
  237. str = "RH";
  238. break;
  239. case SSL_ST_READ_BODY:
  240. str = "RB";
  241. break;
  242. case SSL_ST_READ_DONE:
  243. str = "RD";
  244. break;
  245. default:
  246. str = "unknown";
  247. break;
  248. }
  249. return (str);
  250. }
  251. int ssl3_read_n(SSL *s, int n, int max, int extend)
  252. {
  253. /*
  254. * If extend == 0, obtain new n-byte packet; if extend == 1, increase
  255. * packet by another n bytes. The packet will be in the sub-array of
  256. * s->s3->rbuf.buf specified by s->packet and s->packet_length. (If
  257. * s->rlayer.read_ahead is set, 'max' bytes may be stored in rbuf [plus
  258. * s->packet_length bytes if extend == 1].)
  259. */
  260. int i, len, left;
  261. size_t align = 0;
  262. unsigned char *pkt;
  263. SSL3_BUFFER *rb;
  264. if (n <= 0)
  265. return n;
  266. rb = &s->rlayer.rbuf;
  267. if (rb->buf == NULL)
  268. if (!ssl3_setup_read_buffer(s))
  269. return -1;
  270. left = rb->left;
  271. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
  272. align = (size_t)rb->buf + SSL3_RT_HEADER_LENGTH;
  273. align = (0-align) & (SSL3_ALIGN_PAYLOAD - 1);
  274. #endif
  275. if (!extend) {
  276. /* start with empty packet ... */
  277. if (left == 0)
  278. rb->offset = align;
  279. else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH) {
  280. /*
  281. * check if next packet length is large enough to justify payload
  282. * alignment...
  283. */
  284. pkt = rb->buf + rb->offset;
  285. if (pkt[0] == SSL3_RT_APPLICATION_DATA
  286. && (pkt[3] << 8 | pkt[4]) >= 128) {
  287. /*
  288. * Note that even if packet is corrupted and its length field
  289. * is insane, we can only be led to wrong decision about
  290. * whether memmove will occur or not. Header values has no
  291. * effect on memmove arguments and therefore no buffer
  292. * overrun can be triggered.
  293. */
  294. memmove(rb->buf + align, pkt, left);
  295. rb->offset = align;
  296. }
  297. }
  298. s->rlayer.packet = rb->buf + rb->offset;
  299. s->rlayer.packet_length = 0;
  300. /* ... now we can act as if 'extend' was set */
  301. }
  302. /*
  303. * For DTLS/UDP reads should not span multiple packets because the read
  304. * operation returns the whole packet at once (as long as it fits into
  305. * the buffer).
  306. */
  307. if (SSL_IS_DTLS(s)) {
  308. if (left == 0 && extend)
  309. return 0;
  310. if (left > 0 && n > left)
  311. n = left;
  312. }
  313. /* if there is enough in the buffer from a previous read, take some */
  314. if (left >= n) {
  315. s->rlayer.packet_length += n;
  316. rb->left = left - n;
  317. rb->offset += n;
  318. return (n);
  319. }
  320. /* else we need to read more data */
  321. len = s->rlayer.packet_length;
  322. pkt = rb->buf + align;
  323. /*
  324. * Move any available bytes to front of buffer: 'len' bytes already
  325. * pointed to by 'packet', 'left' extra ones at the end
  326. */
  327. if (s->rlayer.packet != pkt) { /* len > 0 */
  328. memmove(pkt, s->rlayer.packet, len + left);
  329. s->rlayer.packet = pkt;
  330. rb->offset = len + align;
  331. }
  332. if (n > (int)(rb->len - rb->offset)) { /* does not happen */
  333. SSLerr(SSL_F_SSL3_READ_N, ERR_R_INTERNAL_ERROR);
  334. return -1;
  335. }
  336. /* We always act like read_ahead is set for DTLS */
  337. if (!s->rlayer.read_ahead && !SSL_IS_DTLS(s))
  338. /* ignore max parameter */
  339. max = n;
  340. else {
  341. if (max < n)
  342. max = n;
  343. if (max > (int)(rb->len - rb->offset))
  344. max = rb->len - rb->offset;
  345. }
  346. while (left < n) {
  347. /*
  348. * Now we have len+left bytes at the front of s->s3->rbuf.buf and
  349. * need to read in more until we have len+n (up to len+max if
  350. * possible)
  351. */
  352. clear_sys_error();
  353. if (s->rbio != NULL) {
  354. s->rwstate = SSL_READING;
  355. i = BIO_read(s->rbio, pkt + len + left, max - left);
  356. } else {
  357. SSLerr(SSL_F_SSL3_READ_N, SSL_R_READ_BIO_NOT_SET);
  358. i = -1;
  359. }
  360. if (i <= 0) {
  361. rb->left = left;
  362. if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s))
  363. if (len + left == 0)
  364. ssl3_release_read_buffer(s);
  365. return (i);
  366. }
  367. left += i;
  368. /*
  369. * reads should *never* span multiple packets for DTLS because the
  370. * underlying transport protocol is message oriented as opposed to
  371. * byte oriented as in the TLS case.
  372. */
  373. if (SSL_IS_DTLS(s)) {
  374. if (n > left)
  375. n = left; /* makes the while condition false */
  376. }
  377. }
  378. /* done reading, now the book-keeping */
  379. rb->offset += n;
  380. rb->left = left - n;
  381. s->rlayer.packet_length += n;
  382. s->rwstate = SSL_NOTHING;
  383. return (n);
  384. }
  385. /*
  386. * Call this to write data in records of type 'type' It will return <= 0 if
  387. * not all data has been sent or non-blocking IO.
  388. */
  389. int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
  390. {
  391. const unsigned char *buf = buf_;
  392. int tot;
  393. unsigned int n, nw;
  394. #if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
  395. unsigned int max_send_fragment;
  396. unsigned int u_len = (unsigned int)len;
  397. #endif
  398. SSL3_BUFFER *wb = &s->rlayer.wbuf;
  399. int i;
  400. if (len < 0) {
  401. SSLerr(SSL_F_SSL3_WRITE_BYTES, SSL_R_SSL_NEGATIVE_LENGTH);
  402. return -1;
  403. }
  404. s->rwstate = SSL_NOTHING;
  405. tot = s->rlayer.wnum;
  406. /*
  407. * ensure that if we end up with a smaller value of data to write out
  408. * than the the original len from a write which didn't complete for
  409. * non-blocking I/O and also somehow ended up avoiding the check for
  410. * this in ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be
  411. * possible to end up with (len-tot) as a large number that will then
  412. * promptly send beyond the end of the users buffer ... so we trap and
  413. * report the error in a way the user will notice
  414. */
  415. if ((unsigned int)len < s->rlayer.wnum) {
  416. SSLerr(SSL_F_SSL3_WRITE_BYTES, SSL_R_BAD_LENGTH);
  417. return -1;
  418. }
  419. s->rlayer.wnum = 0;
  420. if (SSL_in_init(s) && !ossl_statem_get_in_handshake(s)) {
  421. i = s->handshake_func(s);
  422. if (i < 0)
  423. return (i);
  424. if (i == 0) {
  425. SSLerr(SSL_F_SSL3_WRITE_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
  426. return -1;
  427. }
  428. }
  429. /*
  430. * first check if there is a SSL3_BUFFER still being written out. This
  431. * will happen with non blocking IO
  432. */
  433. if (wb->left != 0) {
  434. i = ssl3_write_pending(s, type, &buf[tot], s->rlayer.wpend_tot);
  435. if (i <= 0) {
  436. /* XXX should we ssl3_release_write_buffer if i<0? */
  437. s->rlayer.wnum = tot;
  438. return i;
  439. }
  440. tot += i; /* this might be last fragment */
  441. }
  442. #if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
  443. /*
  444. * Depending on platform multi-block can deliver several *times*
  445. * better performance. Downside is that it has to allocate
  446. * jumbo buffer to accomodate up to 8 records, but the
  447. * compromise is considered worthy.
  448. */
  449. if (type == SSL3_RT_APPLICATION_DATA &&
  450. u_len >= 4 * (max_send_fragment = s->max_send_fragment) &&
  451. s->compress == NULL && s->msg_callback == NULL &&
  452. !SSL_USE_ETM(s) && SSL_USE_EXPLICIT_IV(s) &&
  453. EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_write_ctx)) &
  454. EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK) {
  455. unsigned char aad[13];
  456. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
  457. int packlen;
  458. /* minimize address aliasing conflicts */
  459. if ((max_send_fragment & 0xfff) == 0)
  460. max_send_fragment -= 512;
  461. if (tot == 0 || wb->buf == NULL) { /* allocate jumbo buffer */
  462. ssl3_release_write_buffer(s);
  463. packlen = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
  464. EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE,
  465. max_send_fragment, NULL);
  466. if (u_len >= 8 * max_send_fragment)
  467. packlen *= 8;
  468. else
  469. packlen *= 4;
  470. wb->buf = OPENSSL_malloc(packlen);
  471. if (wb->buf == NULL) {
  472. SSLerr(SSL_F_SSL3_WRITE_BYTES, ERR_R_MALLOC_FAILURE);
  473. return -1;
  474. }
  475. wb->len = packlen;
  476. } else if (tot == len) { /* done? */
  477. OPENSSL_free(wb->buf); /* free jumbo buffer */
  478. wb->buf = NULL;
  479. return tot;
  480. }
  481. n = (len - tot);
  482. for (;;) {
  483. if (n < 4 * max_send_fragment) {
  484. OPENSSL_free(wb->buf); /* free jumbo buffer */
  485. wb->buf = NULL;
  486. break;
  487. }
  488. if (s->s3->alert_dispatch) {
  489. i = s->method->ssl_dispatch_alert(s);
  490. if (i <= 0) {
  491. s->rlayer.wnum = tot;
  492. return i;
  493. }
  494. }
  495. if (n >= 8 * max_send_fragment)
  496. nw = max_send_fragment * (mb_param.interleave = 8);
  497. else
  498. nw = max_send_fragment * (mb_param.interleave = 4);
  499. memcpy(aad, s->rlayer.write_sequence, 8);
  500. aad[8] = type;
  501. aad[9] = (unsigned char)(s->version >> 8);
  502. aad[10] = (unsigned char)(s->version);
  503. aad[11] = 0;
  504. aad[12] = 0;
  505. mb_param.out = NULL;
  506. mb_param.inp = aad;
  507. mb_param.len = nw;
  508. packlen = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
  509. EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
  510. sizeof(mb_param), &mb_param);
  511. if (packlen <= 0 || packlen > (int)wb->len) { /* never happens */
  512. OPENSSL_free(wb->buf); /* free jumbo buffer */
  513. wb->buf = NULL;
  514. break;
  515. }
  516. mb_param.out = wb->buf;
  517. mb_param.inp = &buf[tot];
  518. mb_param.len = nw;
  519. if (EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
  520. EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
  521. sizeof(mb_param), &mb_param) <= 0)
  522. return -1;
  523. s->rlayer.write_sequence[7] += mb_param.interleave;
  524. if (s->rlayer.write_sequence[7] < mb_param.interleave) {
  525. int j = 6;
  526. while (j >= 0 && (++s->rlayer.write_sequence[j--]) == 0) ;
  527. }
  528. wb->offset = 0;
  529. wb->left = packlen;
  530. s->rlayer.wpend_tot = nw;
  531. s->rlayer.wpend_buf = &buf[tot];
  532. s->rlayer.wpend_type = type;
  533. s->rlayer.wpend_ret = nw;
  534. i = ssl3_write_pending(s, type, &buf[tot], nw);
  535. if (i <= 0) {
  536. if (i < 0 && (!s->wbio || !BIO_should_retry(s->wbio))) {
  537. OPENSSL_free(wb->buf);
  538. wb->buf = NULL;
  539. }
  540. s->rlayer.wnum = tot;
  541. return i;
  542. }
  543. if (i == (int)n) {
  544. OPENSSL_free(wb->buf); /* free jumbo buffer */
  545. wb->buf = NULL;
  546. return tot + i;
  547. }
  548. n -= i;
  549. tot += i;
  550. }
  551. } else
  552. #endif
  553. if (tot == len) { /* done? */
  554. if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s))
  555. ssl3_release_write_buffer(s);
  556. return tot;
  557. }
  558. n = (len - tot);
  559. for (;;) {
  560. if (n > s->max_send_fragment)
  561. nw = s->max_send_fragment;
  562. else
  563. nw = n;
  564. i = do_ssl3_write(s, type, &(buf[tot]), nw, 0);
  565. if (i <= 0) {
  566. /* XXX should we ssl3_release_write_buffer if i<0? */
  567. s->rlayer.wnum = tot;
  568. return i;
  569. }
  570. if ((i == (int)n) ||
  571. (type == SSL3_RT_APPLICATION_DATA &&
  572. (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
  573. /*
  574. * next chunk of data should get another prepended empty fragment
  575. * in ciphersuites with known-IV weakness:
  576. */
  577. s->s3->empty_fragment_done = 0;
  578. if ((i == (int)n) && s->mode & SSL_MODE_RELEASE_BUFFERS &&
  579. !SSL_IS_DTLS(s))
  580. ssl3_release_write_buffer(s);
  581. return tot + i;
  582. }
  583. n -= i;
  584. tot += i;
  585. }
  586. }
  587. int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
  588. unsigned int len, int create_empty_fragment)
  589. {
  590. unsigned char *p, *plen;
  591. int i, mac_size, clear = 0;
  592. int prefix_len = 0;
  593. int eivlen;
  594. size_t align = 0;
  595. SSL3_RECORD *wr;
  596. SSL3_BUFFER *wb = &s->rlayer.wbuf;
  597. SSL_SESSION *sess;
  598. /*
  599. * first check if there is a SSL3_BUFFER still being written out. This
  600. * will happen with non blocking IO
  601. */
  602. if (SSL3_BUFFER_get_left(wb) != 0)
  603. return (ssl3_write_pending(s, type, buf, len));
  604. /* If we have an alert to send, lets send it */
  605. if (s->s3->alert_dispatch) {
  606. i = s->method->ssl_dispatch_alert(s);
  607. if (i <= 0)
  608. return (i);
  609. /* if it went, fall through and send more stuff */
  610. }
  611. if (!SSL3_BUFFER_is_initialised(wb))
  612. if (!ssl3_setup_write_buffer(s))
  613. return -1;
  614. if (len == 0 && !create_empty_fragment)
  615. return 0;
  616. wr = &s->rlayer.wrec;
  617. sess = s->session;
  618. if ((sess == NULL) ||
  619. (s->enc_write_ctx == NULL) ||
  620. (EVP_MD_CTX_md(s->write_hash) == NULL)) {
  621. clear = s->enc_write_ctx ? 0 : 1; /* must be AEAD cipher */
  622. mac_size = 0;
  623. } else {
  624. mac_size = EVP_MD_CTX_size(s->write_hash);
  625. if (mac_size < 0)
  626. goto err;
  627. }
  628. /*
  629. * 'create_empty_fragment' is true only when this function calls itself
  630. */
  631. if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done) {
  632. /*
  633. * countermeasure against known-IV weakness in CBC ciphersuites (see
  634. * http://www.openssl.org/~bodo/tls-cbc.txt)
  635. */
  636. if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) {
  637. /*
  638. * recursive function call with 'create_empty_fragment' set; this
  639. * prepares and buffers the data for an empty fragment (these
  640. * 'prefix_len' bytes are sent out later together with the actual
  641. * payload)
  642. */
  643. prefix_len = do_ssl3_write(s, type, buf, 0, 1);
  644. if (prefix_len <= 0)
  645. goto err;
  646. if (prefix_len >
  647. (SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD))
  648. {
  649. /* insufficient space */
  650. SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);
  651. goto err;
  652. }
  653. }
  654. s->s3->empty_fragment_done = 1;
  655. }
  656. if (create_empty_fragment) {
  657. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
  658. /*
  659. * extra fragment would be couple of cipher blocks, which would be
  660. * multiple of SSL3_ALIGN_PAYLOAD, so if we want to align the real
  661. * payload, then we can just pretent we simply have two headers.
  662. */
  663. align = (size_t)SSL3_BUFFER_get_buf(wb) + 2 * SSL3_RT_HEADER_LENGTH;
  664. align = (0-align) & (SSL3_ALIGN_PAYLOAD - 1);
  665. #endif
  666. p = SSL3_BUFFER_get_buf(wb) + align;
  667. SSL3_BUFFER_set_offset(wb, align);
  668. } else if (prefix_len) {
  669. p = SSL3_BUFFER_get_buf(wb) + SSL3_BUFFER_get_offset(wb) + prefix_len;
  670. } else {
  671. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
  672. align = (size_t)SSL3_BUFFER_get_buf(wb) + SSL3_RT_HEADER_LENGTH;
  673. align = (0-align) & (SSL3_ALIGN_PAYLOAD - 1);
  674. #endif
  675. p = SSL3_BUFFER_get_buf(wb) + align;
  676. SSL3_BUFFER_set_offset(wb, align);
  677. }
  678. /* write the header */
  679. *(p++) = type & 0xff;
  680. SSL3_RECORD_set_type(wr, type);
  681. *(p++) = (s->version >> 8);
  682. /*
  683. * Some servers hang if iniatial client hello is larger than 256 bytes
  684. * and record version number > TLS 1.0
  685. */
  686. if (SSL_get_state(s) == TLS_ST_CW_CLNT_HELLO
  687. && !s->renegotiate && TLS1_get_version(s) > TLS1_VERSION)
  688. *(p++) = 0x1;
  689. else
  690. *(p++) = s->version & 0xff;
  691. /* field where we are to write out packet length */
  692. plen = p;
  693. p += 2;
  694. /* Explicit IV length, block ciphers appropriate version flag */
  695. if (s->enc_write_ctx && SSL_USE_EXPLICIT_IV(s)) {
  696. int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);
  697. if (mode == EVP_CIPH_CBC_MODE) {
  698. eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);
  699. if (eivlen <= 1)
  700. eivlen = 0;
  701. }
  702. /* Need explicit part of IV for GCM mode */
  703. else if (mode == EVP_CIPH_GCM_MODE)
  704. eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
  705. else if (mode == EVP_CIPH_CCM_MODE)
  706. eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
  707. else
  708. eivlen = 0;
  709. } else
  710. eivlen = 0;
  711. /* lets setup the record stuff. */
  712. SSL3_RECORD_set_data(wr, p + eivlen);
  713. SSL3_RECORD_set_length(wr, (int)len);
  714. SSL3_RECORD_set_input(wr, (unsigned char *)buf);
  715. /*
  716. * we now 'read' from wr->input, wr->length bytes into wr->data
  717. */
  718. /* first we compress */
  719. if (s->compress != NULL) {
  720. if (!ssl3_do_compress(s)) {
  721. SSLerr(SSL_F_DO_SSL3_WRITE, SSL_R_COMPRESSION_FAILURE);
  722. goto err;
  723. }
  724. } else {
  725. memcpy(wr->data, wr->input, wr->length);
  726. SSL3_RECORD_reset_input(wr);
  727. }
  728. /*
  729. * we should still have the output to wr->data and the input from
  730. * wr->input. Length should be wr->length. wr->data still points in the
  731. * wb->buf
  732. */
  733. if (!SSL_USE_ETM(s) && mac_size != 0) {
  734. if (s->method->ssl3_enc->mac(s, &(p[wr->length + eivlen]), 1) < 0)
  735. goto err;
  736. SSL3_RECORD_add_length(wr, mac_size);
  737. }
  738. SSL3_RECORD_set_data(wr, p);
  739. SSL3_RECORD_reset_input(wr);
  740. if (eivlen) {
  741. /*
  742. * if (RAND_pseudo_bytes(p, eivlen) <= 0) goto err;
  743. */
  744. SSL3_RECORD_add_length(wr, eivlen);
  745. }
  746. if (s->method->ssl3_enc->enc(s, 1) < 1)
  747. goto err;
  748. if (SSL_USE_ETM(s) && mac_size != 0) {
  749. if (s->method->ssl3_enc->mac(s, p + wr->length, 1) < 0)
  750. goto err;
  751. SSL3_RECORD_add_length(wr, mac_size);
  752. }
  753. /* record length after mac and block padding */
  754. s2n(SSL3_RECORD_get_length(wr), plen);
  755. if (s->msg_callback)
  756. s->msg_callback(1, 0, SSL3_RT_HEADER, plen - 5, 5, s,
  757. s->msg_callback_arg);
  758. /*
  759. * we should now have wr->data pointing to the encrypted data, which is
  760. * wr->length long
  761. */
  762. SSL3_RECORD_set_type(wr, type); /* not needed but helps for debugging */
  763. SSL3_RECORD_add_length(wr, SSL3_RT_HEADER_LENGTH);
  764. if (create_empty_fragment) {
  765. /*
  766. * we are in a recursive call; just return the length, don't write
  767. * out anything here
  768. */
  769. return SSL3_RECORD_get_length(wr);
  770. }
  771. /* now let's set up wb */
  772. SSL3_BUFFER_set_left(wb, prefix_len + SSL3_RECORD_get_length(wr));
  773. /*
  774. * memorize arguments so that ssl3_write_pending can detect bad write
  775. * retries later
  776. */
  777. s->rlayer.wpend_tot = len;
  778. s->rlayer.wpend_buf = buf;
  779. s->rlayer.wpend_type = type;
  780. s->rlayer.wpend_ret = len;
  781. /* we now just need to write the buffer */
  782. return ssl3_write_pending(s, type, buf, len);
  783. err:
  784. return -1;
  785. }
  786. /* if s->s3->wbuf.left != 0, we need to call this */
  787. int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
  788. unsigned int len)
  789. {
  790. int i;
  791. SSL3_BUFFER *wb = &s->rlayer.wbuf;
  792. /* XXXX */
  793. if ((s->rlayer.wpend_tot > (int)len)
  794. || ((s->rlayer.wpend_buf != buf) &&
  795. !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))
  796. || (s->rlayer.wpend_type != type)) {
  797. SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BAD_WRITE_RETRY);
  798. return (-1);
  799. }
  800. for (;;) {
  801. clear_sys_error();
  802. if (s->wbio != NULL) {
  803. s->rwstate = SSL_WRITING;
  804. i = BIO_write(s->wbio,
  805. (char *)&(SSL3_BUFFER_get_buf(wb)[SSL3_BUFFER_get_offset(wb)]),
  806. (unsigned int)SSL3_BUFFER_get_left(wb));
  807. } else {
  808. SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BIO_NOT_SET);
  809. i = -1;
  810. }
  811. if (i == SSL3_BUFFER_get_left(wb)) {
  812. SSL3_BUFFER_set_left(wb, 0);
  813. SSL3_BUFFER_add_offset(wb, i);
  814. s->rwstate = SSL_NOTHING;
  815. return (s->rlayer.wpend_ret);
  816. } else if (i <= 0) {
  817. if (SSL_IS_DTLS(s)) {
  818. /*
  819. * For DTLS, just drop it. That's kind of the whole point in
  820. * using a datagram service
  821. */
  822. SSL3_BUFFER_set_left(wb, 0);
  823. }
  824. return (i);
  825. }
  826. SSL3_BUFFER_add_offset(wb, i);
  827. SSL3_BUFFER_add_left(wb, -i);
  828. }
  829. }
  830. /*-
  831. * Return up to 'len' payload bytes received in 'type' records.
  832. * 'type' is one of the following:
  833. *
  834. * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
  835. * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
  836. * - 0 (during a shutdown, no data has to be returned)
  837. *
  838. * If we don't have stored data to work from, read a SSL/TLS record first
  839. * (possibly multiple records if we still don't have anything to return).
  840. *
  841. * This function must handle any surprises the peer may have for us, such as
  842. * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec
  843. * messages are treated as if they were handshake messages *if* the |recd_type|
  844. * argument is non NULL.
  845. * Also if record payloads contain fragments too small to process, we store
  846. * them until there is enough for the respective protocol (the record protocol
  847. * may use arbitrary fragmentation and even interleaving):
  848. * Change cipher spec protocol
  849. * just 1 byte needed, no need for keeping anything stored
  850. * Alert protocol
  851. * 2 bytes needed (AlertLevel, AlertDescription)
  852. * Handshake protocol
  853. * 4 bytes needed (HandshakeType, uint24 length) -- we just have
  854. * to detect unexpected Client Hello and Hello Request messages
  855. * here, anything else is handled by higher layers
  856. * Application data protocol
  857. * none of our business
  858. */
  859. int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
  860. int len, int peek)
  861. {
  862. int al, i, j, ret;
  863. unsigned int n;
  864. SSL3_RECORD *rr;
  865. void (*cb) (const SSL *ssl, int type2, int val) = NULL;
  866. if (!SSL3_BUFFER_is_initialised(&s->rlayer.rbuf)) {
  867. /* Not initialized yet */
  868. if (!ssl3_setup_read_buffer(s))
  869. return (-1);
  870. }
  871. if ((type && (type != SSL3_RT_APPLICATION_DATA)
  872. && (type != SSL3_RT_HANDSHAKE)) || (peek
  873. && (type !=
  874. SSL3_RT_APPLICATION_DATA))) {
  875. SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
  876. return -1;
  877. }
  878. if ((type == SSL3_RT_HANDSHAKE) && (s->rlayer.handshake_fragment_len > 0))
  879. /* (partially) satisfy request from storage */
  880. {
  881. unsigned char *src = s->rlayer.handshake_fragment;
  882. unsigned char *dst = buf;
  883. unsigned int k;
  884. /* peek == 0 */
  885. n = 0;
  886. while ((len > 0) && (s->rlayer.handshake_fragment_len > 0)) {
  887. *dst++ = *src++;
  888. len--;
  889. s->rlayer.handshake_fragment_len--;
  890. n++;
  891. }
  892. /* move any remaining fragment bytes: */
  893. for (k = 0; k < s->rlayer.handshake_fragment_len; k++)
  894. s->rlayer.handshake_fragment[k] = *src++;
  895. if (recvd_type != NULL)
  896. *recvd_type = SSL3_RT_HANDSHAKE;
  897. return n;
  898. }
  899. /*
  900. * Now s->rlayer.handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE.
  901. */
  902. if (!ossl_statem_get_in_handshake(s) && SSL_in_init(s)) {
  903. /* type == SSL3_RT_APPLICATION_DATA */
  904. i = s->handshake_func(s);
  905. if (i < 0)
  906. return (i);
  907. if (i == 0) {
  908. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
  909. return (-1);
  910. }
  911. }
  912. start:
  913. s->rwstate = SSL_NOTHING;
  914. /*-
  915. * s->s3->rrec.type - is the type of record
  916. * s->s3->rrec.data, - data
  917. * s->s3->rrec.off, - offset into 'data' for next read
  918. * s->s3->rrec.length, - number of bytes.
  919. */
  920. rr = &s->rlayer.rrec;
  921. /* get new packet if necessary */
  922. if ((SSL3_RECORD_get_length(rr) == 0)
  923. || (s->rlayer.rstate == SSL_ST_READ_BODY)) {
  924. ret = ssl3_get_record(s);
  925. if (ret <= 0)
  926. return (ret);
  927. }
  928. /* we now have a packet which can be read and processed */
  929. if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
  930. * reset by ssl3_get_finished */
  931. && (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) {
  932. al = SSL_AD_UNEXPECTED_MESSAGE;
  933. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
  934. goto f_err;
  935. }
  936. /*
  937. * If the other end has shut down, throw anything we read away (even in
  938. * 'peek' mode)
  939. */
  940. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  941. SSL3_RECORD_set_length(rr, 0);
  942. s->rwstate = SSL_NOTHING;
  943. return (0);
  944. }
  945. if (type == SSL3_RECORD_get_type(rr)
  946. || (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC
  947. && type == SSL3_RT_HANDSHAKE && recvd_type != NULL)) {
  948. /*
  949. * SSL3_RT_APPLICATION_DATA or
  950. * SSL3_RT_HANDSHAKE or
  951. * SSL3_RT_CHANGE_CIPHER_SPEC
  952. */
  953. /*
  954. * make sure that we are not getting application data when we are
  955. * doing a handshake for the first time
  956. */
  957. if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
  958. (s->enc_read_ctx == NULL)) {
  959. al = SSL_AD_UNEXPECTED_MESSAGE;
  960. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE);
  961. goto f_err;
  962. }
  963. if (type == SSL3_RT_HANDSHAKE
  964. && SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC
  965. && s->rlayer.handshake_fragment_len > 0) {
  966. al = SSL_AD_UNEXPECTED_MESSAGE;
  967. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_CCS_RECEIVED_EARLY);
  968. goto f_err;
  969. }
  970. if (recvd_type != NULL)
  971. *recvd_type = SSL3_RECORD_get_type(rr);
  972. if (len <= 0)
  973. return (len);
  974. if ((unsigned int)len > SSL3_RECORD_get_length(rr))
  975. n = SSL3_RECORD_get_length(rr);
  976. else
  977. n = (unsigned int)len;
  978. memcpy(buf, &(rr->data[rr->off]), n);
  979. if (!peek) {
  980. SSL3_RECORD_add_length(rr, -n);
  981. SSL3_RECORD_add_off(rr, n);
  982. if (SSL3_RECORD_get_length(rr) == 0) {
  983. s->rlayer.rstate = SSL_ST_READ_HEADER;
  984. SSL3_RECORD_set_off(rr, 0);
  985. if (s->mode & SSL_MODE_RELEASE_BUFFERS
  986. && SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0)
  987. ssl3_release_read_buffer(s);
  988. }
  989. }
  990. return (n);
  991. }
  992. /*
  993. * If we get here, then type != rr->type; if we have a handshake message,
  994. * then it was unexpected (Hello Request or Client Hello) or invalid (we
  995. * were actually expecting a CCS).
  996. */
  997. if (rr->type == SSL3_RT_HANDSHAKE && type == SSL3_RT_CHANGE_CIPHER_SPEC) {
  998. al = SSL_AD_UNEXPECTED_MESSAGE;
  999. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_MESSAGE);
  1000. goto f_err;
  1001. }
  1002. /*
  1003. * Lets just double check that we've not got an SSLv2 record
  1004. */
  1005. if (rr->rec_version == SSL2_VERSION) {
  1006. /*
  1007. * Should never happen. ssl3_get_record() should only give us an SSLv2
  1008. * record back if this is the first packet and we are looking for an
  1009. * initial ClientHello. Therefore |type| should always be equal to
  1010. * |rr->type|. If not then something has gone horribly wrong
  1011. */
  1012. al = SSL_AD_INTERNAL_ERROR;
  1013. SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
  1014. goto f_err;
  1015. }
  1016. if(s->method->version == TLS_ANY_VERSION
  1017. && (s->server || rr->type != SSL3_RT_ALERT)) {
  1018. /*
  1019. * If we've got this far and still haven't decided on what version
  1020. * we're using then this must be a client side alert we're dealing with
  1021. * (we don't allow heartbeats yet). We shouldn't be receiving anything
  1022. * other than a ClientHello if we are a server.
  1023. */
  1024. s->version = rr->rec_version;
  1025. al = SSL_AD_UNEXPECTED_MESSAGE;
  1026. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_MESSAGE);
  1027. goto f_err;
  1028. }
  1029. /*
  1030. * In case of record types for which we have 'fragment' storage, fill
  1031. * that so that we can process the data at a fixed place.
  1032. */
  1033. {
  1034. unsigned int dest_maxlen = 0;
  1035. unsigned char *dest = NULL;
  1036. unsigned int *dest_len = NULL;
  1037. if (SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) {
  1038. dest_maxlen = sizeof s->rlayer.handshake_fragment;
  1039. dest = s->rlayer.handshake_fragment;
  1040. dest_len = &s->rlayer.handshake_fragment_len;
  1041. } else if (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) {
  1042. dest_maxlen = sizeof s->rlayer.alert_fragment;
  1043. dest = s->rlayer.alert_fragment;
  1044. dest_len = &s->rlayer.alert_fragment_len;
  1045. }
  1046. #ifndef OPENSSL_NO_HEARTBEATS
  1047. else if (SSL3_RECORD_get_type(rr)== TLS1_RT_HEARTBEAT) {
  1048. /* We can ignore 0 return values */
  1049. if (tls1_process_heartbeat(s, SSL3_RECORD_get_data(rr),
  1050. SSL3_RECORD_get_length(rr)) < 0) {
  1051. return -1;
  1052. }
  1053. /* Exit and notify application to read again */
  1054. SSL3_RECORD_set_length(rr, 0);
  1055. s->rwstate = SSL_READING;
  1056. BIO_clear_retry_flags(SSL_get_rbio(s));
  1057. BIO_set_retry_read(SSL_get_rbio(s));
  1058. return (-1);
  1059. }
  1060. #endif
  1061. if (dest_maxlen > 0) {
  1062. n = dest_maxlen - *dest_len; /* available space in 'dest' */
  1063. if (SSL3_RECORD_get_length(rr) < n)
  1064. n = SSL3_RECORD_get_length(rr); /* available bytes */
  1065. /* now move 'n' bytes: */
  1066. while (n-- > 0) {
  1067. dest[(*dest_len)++] =
  1068. SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)];
  1069. SSL3_RECORD_add_off(rr, 1);
  1070. SSL3_RECORD_add_length(rr, -1);
  1071. }
  1072. if (*dest_len < dest_maxlen)
  1073. goto start; /* fragment was too small */
  1074. }
  1075. }
  1076. /*-
  1077. * s->rlayer.handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
  1078. * s->rlayer.alert_fragment_len == 2 iff rr->type == SSL3_RT_ALERT.
  1079. * (Possibly rr is 'empty' now, i.e. rr->length may be 0.)
  1080. */
  1081. /* If we are a client, check for an incoming 'Hello Request': */
  1082. if ((!s->server) &&
  1083. (s->rlayer.handshake_fragment_len >= 4) &&
  1084. (s->rlayer.handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&
  1085. (s->session != NULL) && (s->session->cipher != NULL)) {
  1086. s->rlayer.handshake_fragment_len = 0;
  1087. if ((s->rlayer.handshake_fragment[1] != 0) ||
  1088. (s->rlayer.handshake_fragment[2] != 0) ||
  1089. (s->rlayer.handshake_fragment[3] != 0)) {
  1090. al = SSL_AD_DECODE_ERROR;
  1091. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_BAD_HELLO_REQUEST);
  1092. goto f_err;
  1093. }
  1094. if (s->msg_callback)
  1095. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
  1096. s->rlayer.handshake_fragment, 4, s,
  1097. s->msg_callback_arg);
  1098. if (SSL_is_init_finished(s) &&
  1099. !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&
  1100. !s->s3->renegotiate) {
  1101. ssl3_renegotiate(s);
  1102. if (ssl3_renegotiate_check(s)) {
  1103. i = s->handshake_func(s);
  1104. if (i < 0)
  1105. return (i);
  1106. if (i == 0) {
  1107. SSLerr(SSL_F_SSL3_READ_BYTES,
  1108. SSL_R_SSL_HANDSHAKE_FAILURE);
  1109. return (-1);
  1110. }
  1111. if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
  1112. if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {
  1113. /* no read-ahead left? */
  1114. BIO *bio;
  1115. /*
  1116. * In the case where we try to read application data,
  1117. * but we trigger an SSL handshake, we return -1 with
  1118. * the retry option set. Otherwise renegotiation may
  1119. * cause nasty problems in the blocking world
  1120. */
  1121. s->rwstate = SSL_READING;
  1122. bio = SSL_get_rbio(s);
  1123. BIO_clear_retry_flags(bio);
  1124. BIO_set_retry_read(bio);
  1125. return (-1);
  1126. }
  1127. }
  1128. }
  1129. }
  1130. /*
  1131. * we either finished a handshake or ignored the request, now try
  1132. * again to obtain the (application) data we were asked for
  1133. */
  1134. goto start;
  1135. }
  1136. /*
  1137. * If we are a server and get a client hello when renegotiation isn't
  1138. * allowed send back a no renegotiation alert and carry on. WARNING:
  1139. * experimental code, needs reviewing (steve)
  1140. */
  1141. if (s->server &&
  1142. SSL_is_init_finished(s) &&
  1143. !s->s3->send_connection_binding &&
  1144. (s->version > SSL3_VERSION) &&
  1145. (s->rlayer.handshake_fragment_len >= 4) &&
  1146. (s->rlayer.handshake_fragment[0] == SSL3_MT_CLIENT_HELLO) &&
  1147. (s->session != NULL) && (s->session->cipher != NULL) &&
  1148. !(s->ctx->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
  1149. SSL3_RECORD_set_length(rr, 0);
  1150. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
  1151. goto start;
  1152. }
  1153. if (s->rlayer.alert_fragment_len >= 2) {
  1154. int alert_level = s->rlayer.alert_fragment[0];
  1155. int alert_descr = s->rlayer.alert_fragment[1];
  1156. s->rlayer.alert_fragment_len = 0;
  1157. if (s->msg_callback)
  1158. s->msg_callback(0, s->version, SSL3_RT_ALERT,
  1159. s->rlayer.alert_fragment, 2, s,
  1160. s->msg_callback_arg);
  1161. if (s->info_callback != NULL)
  1162. cb = s->info_callback;
  1163. else if (s->ctx->info_callback != NULL)
  1164. cb = s->ctx->info_callback;
  1165. if (cb != NULL) {
  1166. j = (alert_level << 8) | alert_descr;
  1167. cb(s, SSL_CB_READ_ALERT, j);
  1168. }
  1169. if (alert_level == SSL3_AL_WARNING) {
  1170. s->s3->warn_alert = alert_descr;
  1171. if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
  1172. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  1173. return (0);
  1174. }
  1175. /*
  1176. * This is a warning but we receive it if we requested
  1177. * renegotiation and the peer denied it. Terminate with a fatal
  1178. * alert because if application tried to renegotiatie it
  1179. * presumably had a good reason and expects it to succeed. In
  1180. * future we might have a renegotiation where we don't care if
  1181. * the peer refused it where we carry on.
  1182. */
  1183. else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
  1184. al = SSL_AD_HANDSHAKE_FAILURE;
  1185. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_NO_RENEGOTIATION);
  1186. goto f_err;
  1187. }
  1188. #ifdef SSL_AD_MISSING_SRP_USERNAME
  1189. else if (alert_descr == SSL_AD_MISSING_SRP_USERNAME)
  1190. return (0);
  1191. #endif
  1192. } else if (alert_level == SSL3_AL_FATAL) {
  1193. char tmp[16];
  1194. s->rwstate = SSL_NOTHING;
  1195. s->s3->fatal_alert = alert_descr;
  1196. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);
  1197. BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
  1198. ERR_add_error_data(2, "SSL alert number ", tmp);
  1199. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  1200. SSL_CTX_remove_session(s->ctx, s->session);
  1201. return (0);
  1202. } else {
  1203. al = SSL_AD_ILLEGAL_PARAMETER;
  1204. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE);
  1205. goto f_err;
  1206. }
  1207. goto start;
  1208. }
  1209. if (s->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a
  1210. * shutdown */
  1211. s->rwstate = SSL_NOTHING;
  1212. SSL3_RECORD_set_length(rr, 0);
  1213. return (0);
  1214. }
  1215. if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) {
  1216. al = SSL_AD_UNEXPECTED_MESSAGE;
  1217. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_CCS_RECEIVED_EARLY);
  1218. goto f_err;
  1219. }
  1220. /*
  1221. * Unexpected handshake message (Client Hello, or protocol violation)
  1222. */
  1223. if ((s->rlayer.handshake_fragment_len >= 4)
  1224. && !ossl_statem_get_in_handshake(s)) {
  1225. if (SSL_is_init_finished(s) &&
  1226. !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) {
  1227. ossl_statem_set_in_init(s, 1);
  1228. s->renegotiate = 1;
  1229. s->new_session = 1;
  1230. }
  1231. i = s->handshake_func(s);
  1232. if (i < 0)
  1233. return (i);
  1234. if (i == 0) {
  1235. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
  1236. return (-1);
  1237. }
  1238. if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
  1239. if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {
  1240. /* no read-ahead left? */
  1241. BIO *bio;
  1242. /*
  1243. * In the case where we try to read application data, but we
  1244. * trigger an SSL handshake, we return -1 with the retry
  1245. * option set. Otherwise renegotiation may cause nasty
  1246. * problems in the blocking world
  1247. */
  1248. s->rwstate = SSL_READING;
  1249. bio = SSL_get_rbio(s);
  1250. BIO_clear_retry_flags(bio);
  1251. BIO_set_retry_read(bio);
  1252. return (-1);
  1253. }
  1254. }
  1255. goto start;
  1256. }
  1257. switch (SSL3_RECORD_get_type(rr)) {
  1258. default:
  1259. /*
  1260. * TLS up to v1.1 just ignores unknown message types: TLS v1.2 give
  1261. * an unexpected message alert.
  1262. */
  1263. if (s->version >= TLS1_VERSION && s->version <= TLS1_1_VERSION) {
  1264. SSL3_RECORD_set_length(rr, 0);
  1265. goto start;
  1266. }
  1267. al = SSL_AD_UNEXPECTED_MESSAGE;
  1268. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
  1269. goto f_err;
  1270. case SSL3_RT_CHANGE_CIPHER_SPEC:
  1271. case SSL3_RT_ALERT:
  1272. case SSL3_RT_HANDSHAKE:
  1273. /*
  1274. * we already handled all of these, with the possible exception of
  1275. * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but
  1276. * that should not happen when type != rr->type
  1277. */
  1278. al = SSL_AD_UNEXPECTED_MESSAGE;
  1279. SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
  1280. goto f_err;
  1281. case SSL3_RT_APPLICATION_DATA:
  1282. /*
  1283. * At this point, we were expecting handshake data, but have
  1284. * application data. If the library was running inside ssl3_read()
  1285. * (i.e. in_read_app_data is set) and it makes sense to read
  1286. * application data at this point (session renegotiation not yet
  1287. * started), we will indulge it.
  1288. */
  1289. if (ossl_statem_app_data_allowed(s)) {
  1290. s->s3->in_read_app_data = 2;
  1291. return (-1);
  1292. } else {
  1293. al = SSL_AD_UNEXPECTED_MESSAGE;
  1294. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
  1295. goto f_err;
  1296. }
  1297. }
  1298. /* not reached */
  1299. f_err:
  1300. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1301. return (-1);
  1302. }
  1303. void ssl3_record_sequence_update(unsigned char *seq)
  1304. {
  1305. int i;
  1306. for (i = 7; i >= 0; i--) {
  1307. ++seq[i];
  1308. if (seq[i] != 0)
  1309. break;
  1310. }
  1311. }
  1312. /*
  1313. * Returns true if the current rrec was sent in SSLv2 backwards compatible
  1314. * format and false otherwise.
  1315. */
  1316. int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl)
  1317. {
  1318. return SSL3_RECORD_is_sslv2_record(&rl->rrec);
  1319. }
  1320. /*
  1321. * Returns the length in bytes of the current rrec
  1322. */
  1323. unsigned int RECORD_LAYER_get_rrec_length(RECORD_LAYER *rl)
  1324. {
  1325. return SSL3_RECORD_get_length(&rl->rrec);
  1326. }