s3_pkt.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  1. /* ssl/s3_pkt.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. #ifndef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
  120. # define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0
  121. #endif
  122. #if defined(OPENSSL_SMALL_FOOTPRINT) || \
  123. !( defined(AES_ASM) && ( \
  124. defined(__x86_64) || defined(__x86_64__) || \
  125. defined(_M_AMD64) || defined(_M_X64) || \
  126. defined(__INTEL__) ) \
  127. )
  128. # undef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
  129. # define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0
  130. #endif
  131. static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
  132. unsigned int len, int create_empty_fragment);
  133. static int ssl3_get_record(SSL *s);
  134. /*
  135. * Return values are as per SSL_read()
  136. */
  137. int ssl3_read_n(SSL *s, int n, int max, int extend)
  138. {
  139. /*
  140. * If extend == 0, obtain new n-byte packet; if extend == 1, increase
  141. * packet by another n bytes. The packet will be in the sub-array of
  142. * s->s3->rbuf.buf specified by s->packet and s->packet_length. (If
  143. * s->read_ahead is set, 'max' bytes may be stored in rbuf [plus
  144. * s->packet_length bytes if extend == 1].)
  145. */
  146. int i, len, left;
  147. long align = 0;
  148. unsigned char *pkt;
  149. SSL3_BUFFER *rb;
  150. if (n <= 0)
  151. return n;
  152. rb = &(s->s3->rbuf);
  153. if (rb->buf == NULL)
  154. if (!ssl3_setup_read_buffer(s))
  155. return -1;
  156. left = rb->left;
  157. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
  158. align = (long)rb->buf + SSL3_RT_HEADER_LENGTH;
  159. align = (-align) & (SSL3_ALIGN_PAYLOAD - 1);
  160. #endif
  161. if (!extend) {
  162. /* start with empty packet ... */
  163. if (left == 0)
  164. rb->offset = align;
  165. else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH) {
  166. /*
  167. * check if next packet length is large enough to justify payload
  168. * alignment...
  169. */
  170. pkt = rb->buf + rb->offset;
  171. if (pkt[0] == SSL3_RT_APPLICATION_DATA
  172. && (pkt[3] << 8 | pkt[4]) >= 128) {
  173. /*
  174. * Note that even if packet is corrupted and its length field
  175. * is insane, we can only be led to wrong decision about
  176. * whether memmove will occur or not. Header values has no
  177. * effect on memmove arguments and therefore no buffer
  178. * overrun can be triggered.
  179. */
  180. memmove(rb->buf + align, pkt, left);
  181. rb->offset = align;
  182. }
  183. }
  184. s->packet = rb->buf + rb->offset;
  185. s->packet_length = 0;
  186. /* ... now we can act as if 'extend' was set */
  187. }
  188. /*
  189. * For DTLS/UDP reads should not span multiple packets because the read
  190. * operation returns the whole packet at once (as long as it fits into
  191. * the buffer).
  192. */
  193. if (SSL_IS_DTLS(s)) {
  194. if (left == 0 && extend)
  195. return 0;
  196. if (left > 0 && n > left)
  197. n = left;
  198. }
  199. /* if there is enough in the buffer from a previous read, take some */
  200. if (left >= n) {
  201. s->packet_length += n;
  202. rb->left = left - n;
  203. rb->offset += n;
  204. return (n);
  205. }
  206. /* else we need to read more data */
  207. len = s->packet_length;
  208. pkt = rb->buf + align;
  209. /*
  210. * Move any available bytes to front of buffer: 'len' bytes already
  211. * pointed to by 'packet', 'left' extra ones at the end
  212. */
  213. if (s->packet != pkt) { /* len > 0 */
  214. memmove(pkt, s->packet, len + left);
  215. s->packet = pkt;
  216. rb->offset = len + align;
  217. }
  218. if (n > (int)(rb->len - rb->offset)) { /* does not happen */
  219. SSLerr(SSL_F_SSL3_READ_N, ERR_R_INTERNAL_ERROR);
  220. return -1;
  221. }
  222. /* We always act like read_ahead is set for DTLS */
  223. if (!s->read_ahead && !SSL_IS_DTLS(s))
  224. /* ignore max parameter */
  225. max = n;
  226. else {
  227. if (max < n)
  228. max = n;
  229. if (max > (int)(rb->len - rb->offset))
  230. max = rb->len - rb->offset;
  231. }
  232. while (left < n) {
  233. /*
  234. * Now we have len+left bytes at the front of s->s3->rbuf.buf and
  235. * need to read in more until we have len+n (up to len+max if
  236. * possible)
  237. */
  238. clear_sys_error();
  239. if (s->rbio != NULL) {
  240. s->rwstate = SSL_READING;
  241. i = BIO_read(s->rbio, pkt + len + left, max - left);
  242. } else {
  243. SSLerr(SSL_F_SSL3_READ_N, SSL_R_READ_BIO_NOT_SET);
  244. i = -1;
  245. }
  246. if (i <= 0) {
  247. rb->left = left;
  248. if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s))
  249. if (len + left == 0)
  250. ssl3_release_read_buffer(s);
  251. return (i);
  252. }
  253. left += i;
  254. /*
  255. * reads should *never* span multiple packets for DTLS because the
  256. * underlying transport protocol is message oriented as opposed to
  257. * byte oriented as in the TLS case.
  258. */
  259. if (SSL_IS_DTLS(s)) {
  260. if (n > left)
  261. n = left; /* makes the while condition false */
  262. }
  263. }
  264. /* done reading, now the book-keeping */
  265. rb->offset += n;
  266. rb->left = left - n;
  267. s->packet_length += n;
  268. s->rwstate = SSL_NOTHING;
  269. return (n);
  270. }
  271. /*
  272. * MAX_EMPTY_RECORDS defines the number of consecutive, empty records that
  273. * will be processed per call to ssl3_get_record. Without this limit an
  274. * attacker could send empty records at a faster rate than we can process and
  275. * cause ssl3_get_record to loop forever.
  276. */
  277. #define MAX_EMPTY_RECORDS 32
  278. /*-
  279. * Call this to get a new input record.
  280. * It will return <= 0 if more data is needed, normally due to an error
  281. * or non-blocking IO.
  282. * When it finishes, one packet has been decoded and can be found in
  283. * ssl->s3->rrec.type - is the type of record
  284. * ssl->s3->rrec.data, - data
  285. * ssl->s3->rrec.length, - number of bytes
  286. */
  287. /* used only by ssl3_read_bytes */
  288. static int ssl3_get_record(SSL *s)
  289. {
  290. int ssl_major, ssl_minor, al;
  291. int enc_err, n, i, ret = -1;
  292. SSL3_RECORD *rr;
  293. SSL_SESSION *sess;
  294. unsigned char *p;
  295. unsigned char md[EVP_MAX_MD_SIZE];
  296. short version;
  297. unsigned mac_size, orig_len;
  298. size_t extra;
  299. unsigned empty_record_count = 0;
  300. rr = &(s->s3->rrec);
  301. sess = s->session;
  302. if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER)
  303. extra = SSL3_RT_MAX_EXTRA;
  304. else
  305. extra = 0;
  306. if (extra && !s->s3->init_extra) {
  307. /*
  308. * An application error: SLS_OP_MICROSOFT_BIG_SSLV3_BUFFER set after
  309. * ssl3_setup_buffers() was done
  310. */
  311. SSLerr(SSL_F_SSL3_GET_RECORD, ERR_R_INTERNAL_ERROR);
  312. return -1;
  313. }
  314. again:
  315. /* check if we have the header */
  316. if ((s->rstate != SSL_ST_READ_BODY) ||
  317. (s->packet_length < SSL3_RT_HEADER_LENGTH)) {
  318. n = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH, s->s3->rbuf.len, 0);
  319. if (n <= 0)
  320. return (n); /* error or non-blocking */
  321. s->rstate = SSL_ST_READ_BODY;
  322. p = s->packet;
  323. if (s->msg_callback)
  324. s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s,
  325. s->msg_callback_arg);
  326. /* Pull apart the header into the SSL3_RECORD */
  327. rr->type = *(p++);
  328. ssl_major = *(p++);
  329. ssl_minor = *(p++);
  330. version = (ssl_major << 8) | ssl_minor;
  331. n2s(p, rr->length);
  332. #if 0
  333. fprintf(stderr, "Record type=%d, Length=%d\n", rr->type, rr->length);
  334. #endif
  335. /* Lets check version */
  336. if (!s->first_packet) {
  337. if (version != s->version) {
  338. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);
  339. if ((s->version & 0xFF00) == (version & 0xFF00)
  340. && !s->enc_write_ctx && !s->write_hash) {
  341. if (rr->type == SSL3_RT_ALERT) {
  342. /*
  343. * The record is using an incorrect version number, but
  344. * what we've got appears to be an alert. We haven't
  345. * read the body yet to check whether its a fatal or
  346. * not - but chances are it is. We probably shouldn't
  347. * send a fatal alert back. We'll just end.
  348. */
  349. goto err;
  350. }
  351. /*
  352. * Send back error using their minor version number :-)
  353. */
  354. s->version = (unsigned short)version;
  355. }
  356. al = SSL_AD_PROTOCOL_VERSION;
  357. goto f_err;
  358. }
  359. }
  360. if ((version >> 8) != SSL3_VERSION_MAJOR) {
  361. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);
  362. goto err;
  363. }
  364. if (rr->length > s->s3->rbuf.len - SSL3_RT_HEADER_LENGTH) {
  365. al = SSL_AD_RECORD_OVERFLOW;
  366. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);
  367. goto f_err;
  368. }
  369. /* now s->rstate == SSL_ST_READ_BODY */
  370. }
  371. /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
  372. if (rr->length > s->packet_length - SSL3_RT_HEADER_LENGTH) {
  373. /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
  374. i = rr->length;
  375. n = ssl3_read_n(s, i, i, 1);
  376. if (n <= 0)
  377. return (n); /* error or non-blocking io */
  378. /*
  379. * now n == rr->length, and s->packet_length == SSL3_RT_HEADER_LENGTH
  380. * + rr->length
  381. */
  382. }
  383. s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */
  384. /*
  385. * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
  386. * and we have that many bytes in s->packet
  387. */
  388. rr->input = &(s->packet[SSL3_RT_HEADER_LENGTH]);
  389. /*
  390. * ok, we can now read from 's->packet' data into 'rr' rr->input points
  391. * at rr->length bytes, which need to be copied into rr->data by either
  392. * the decryption or by the decompression When the data is 'copied' into
  393. * the rr->data buffer, rr->input will be pointed at the new buffer
  394. */
  395. /*
  396. * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
  397. * bytes of encrypted compressed stuff.
  398. */
  399. /* check is not needed I believe */
  400. if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH + extra) {
  401. al = SSL_AD_RECORD_OVERFLOW;
  402. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
  403. goto f_err;
  404. }
  405. /* decrypt in place in 'rr->input' */
  406. rr->data = rr->input;
  407. enc_err = s->method->ssl3_enc->enc(s, 0);
  408. /*-
  409. * enc_err is:
  410. * 0: (in non-constant time) if the record is publically invalid.
  411. * 1: if the padding is valid
  412. * -1: if the padding is invalid
  413. */
  414. if (enc_err == 0) {
  415. al = SSL_AD_DECRYPTION_FAILED;
  416. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
  417. goto f_err;
  418. }
  419. #ifdef TLS_DEBUG
  420. printf("dec %d\n", rr->length);
  421. {
  422. unsigned int z;
  423. for (z = 0; z < rr->length; z++)
  424. printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
  425. }
  426. printf("\n");
  427. #endif
  428. /* r->length is now the compressed data plus mac */
  429. if ((sess != NULL) &&
  430. (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
  431. /* s->read_hash != NULL => mac_size != -1 */
  432. unsigned char *mac = NULL;
  433. unsigned char mac_tmp[EVP_MAX_MD_SIZE];
  434. mac_size = EVP_MD_CTX_size(s->read_hash);
  435. OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
  436. /*
  437. * kludge: *_cbc_remove_padding passes padding length in rr->type
  438. */
  439. orig_len = rr->length + ((unsigned int)rr->type >> 8);
  440. /*
  441. * orig_len is the length of the record before any padding was
  442. * removed. This is public information, as is the MAC in use,
  443. * therefore we can safely process the record in a different amount
  444. * of time if it's too short to possibly contain a MAC.
  445. */
  446. if (orig_len < mac_size ||
  447. /* CBC records must have a padding length byte too. */
  448. (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
  449. orig_len < mac_size + 1)) {
  450. al = SSL_AD_DECODE_ERROR;
  451. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
  452. goto f_err;
  453. }
  454. if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
  455. /*
  456. * We update the length so that the TLS header bytes can be
  457. * constructed correctly but we need to extract the MAC in
  458. * constant time from within the record, without leaking the
  459. * contents of the padding bytes.
  460. */
  461. mac = mac_tmp;
  462. ssl3_cbc_copy_mac(mac_tmp, rr, mac_size, orig_len);
  463. rr->length -= mac_size;
  464. } else {
  465. /*
  466. * In this case there's no padding, so |orig_len| equals
  467. * |rec->length| and we checked that there's enough bytes for
  468. * |mac_size| above.
  469. */
  470. rr->length -= mac_size;
  471. mac = &rr->data[rr->length];
  472. }
  473. i = s->method->ssl3_enc->mac(s, md, 0 /* not send */ );
  474. if (i < 0 || mac == NULL
  475. || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
  476. enc_err = -1;
  477. if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + extra + mac_size)
  478. enc_err = -1;
  479. }
  480. if (enc_err < 0) {
  481. /*
  482. * A separate 'decryption_failed' alert was introduced with TLS 1.0,
  483. * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
  484. * failure is directly visible from the ciphertext anyway, we should
  485. * not reveal which kind of error occured -- this might become
  486. * visible to an attacker (e.g. via a logfile)
  487. */
  488. al = SSL_AD_BAD_RECORD_MAC;
  489. SSLerr(SSL_F_SSL3_GET_RECORD,
  490. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  491. goto f_err;
  492. }
  493. /* r->length is now just compressed */
  494. if (s->expand != NULL) {
  495. if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + extra) {
  496. al = SSL_AD_RECORD_OVERFLOW;
  497. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_COMPRESSED_LENGTH_TOO_LONG);
  498. goto f_err;
  499. }
  500. if (!ssl3_do_uncompress(s)) {
  501. al = SSL_AD_DECOMPRESSION_FAILURE;
  502. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BAD_DECOMPRESSION);
  503. goto f_err;
  504. }
  505. }
  506. if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH + extra) {
  507. al = SSL_AD_RECORD_OVERFLOW;
  508. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
  509. goto f_err;
  510. }
  511. rr->off = 0;
  512. /*-
  513. * So at this point the following is true
  514. * ssl->s3->rrec.type is the type of record
  515. * ssl->s3->rrec.length == number of bytes in record
  516. * ssl->s3->rrec.off == offset to first valid byte
  517. * ssl->s3->rrec.data == where to take bytes from, increment
  518. * after use :-).
  519. */
  520. /* we have pulled in a full packet so zero things */
  521. s->packet_length = 0;
  522. /* just read a 0 length packet */
  523. if (rr->length == 0) {
  524. empty_record_count++;
  525. if (empty_record_count > MAX_EMPTY_RECORDS) {
  526. al = SSL_AD_UNEXPECTED_MESSAGE;
  527. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_RECORD_TOO_SMALL);
  528. goto f_err;
  529. }
  530. goto again;
  531. }
  532. #if 0
  533. fprintf(stderr, "Ultimate Record type=%d, Length=%d\n", rr->type,
  534. rr->length);
  535. #endif
  536. return (1);
  537. f_err:
  538. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  539. err:
  540. return (ret);
  541. }
  542. int ssl3_do_uncompress(SSL *ssl)
  543. {
  544. #ifndef OPENSSL_NO_COMP
  545. int i;
  546. SSL3_RECORD *rr;
  547. rr = &(ssl->s3->rrec);
  548. i = COMP_expand_block(ssl->expand, rr->comp,
  549. SSL3_RT_MAX_PLAIN_LENGTH, rr->data,
  550. (int)rr->length);
  551. if (i < 0)
  552. return (0);
  553. else
  554. rr->length = i;
  555. rr->data = rr->comp;
  556. #endif
  557. return (1);
  558. }
  559. int ssl3_do_compress(SSL *ssl)
  560. {
  561. #ifndef OPENSSL_NO_COMP
  562. int i;
  563. SSL3_RECORD *wr;
  564. wr = &(ssl->s3->wrec);
  565. i = COMP_compress_block(ssl->compress, wr->data,
  566. SSL3_RT_MAX_COMPRESSED_LENGTH,
  567. wr->input, (int)wr->length);
  568. if (i < 0)
  569. return (0);
  570. else
  571. wr->length = i;
  572. wr->input = wr->data;
  573. #endif
  574. return (1);
  575. }
  576. /*
  577. * Call this to write data in records of type 'type' It will return <= 0 if
  578. * not all data has been sent or non-blocking IO.
  579. */
  580. int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
  581. {
  582. const unsigned char *buf = buf_;
  583. int tot;
  584. unsigned int n, nw;
  585. #if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
  586. unsigned int max_send_fragment;
  587. #endif
  588. SSL3_BUFFER *wb = &(s->s3->wbuf);
  589. int i;
  590. s->rwstate = SSL_NOTHING;
  591. OPENSSL_assert(s->s3->wnum <= INT_MAX);
  592. tot = s->s3->wnum;
  593. s->s3->wnum = 0;
  594. if (SSL_in_init(s) && !s->in_handshake) {
  595. i = s->handshake_func(s);
  596. if (i < 0)
  597. return (i);
  598. if (i == 0) {
  599. SSLerr(SSL_F_SSL3_WRITE_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
  600. return -1;
  601. }
  602. }
  603. /*
  604. * ensure that if we end up with a smaller value of data to write out
  605. * than the the original len from a write which didn't complete for
  606. * non-blocking I/O and also somehow ended up avoiding the check for
  607. * this in ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be
  608. * possible to end up with (len-tot) as a large number that will then
  609. * promptly send beyond the end of the users buffer ... so we trap and
  610. * report the error in a way the user will notice
  611. */
  612. if ((len < tot) || ((wb->left != 0) && (len < (tot + s->s3->wpend_tot)))) {
  613. SSLerr(SSL_F_SSL3_WRITE_BYTES, SSL_R_BAD_LENGTH);
  614. return (-1);
  615. }
  616. /*
  617. * first check if there is a SSL3_BUFFER still being written out. This
  618. * will happen with non blocking IO
  619. */
  620. if (wb->left != 0) {
  621. i = ssl3_write_pending(s, type, &buf[tot], s->s3->wpend_tot);
  622. if (i <= 0) {
  623. /* XXX should we ssl3_release_write_buffer if i<0? */
  624. s->s3->wnum = tot;
  625. return i;
  626. }
  627. tot += i; /* this might be last fragment */
  628. }
  629. #if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
  630. /*
  631. * Depending on platform multi-block can deliver several *times*
  632. * better performance. Downside is that it has to allocate
  633. * jumbo buffer to accomodate up to 8 records, but the
  634. * compromise is considered worthy.
  635. */
  636. if (type == SSL3_RT_APPLICATION_DATA &&
  637. len >= 4 * (int)(max_send_fragment = s->max_send_fragment) &&
  638. s->compress == NULL && s->msg_callback == NULL &&
  639. SSL_USE_EXPLICIT_IV(s) &&
  640. s->enc_write_ctx != NULL &&
  641. EVP_CIPHER_flags(s->enc_write_ctx->cipher) &
  642. EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK) {
  643. unsigned char aad[13];
  644. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
  645. int packlen;
  646. /* minimize address aliasing conflicts */
  647. if ((max_send_fragment & 0xfff) == 0)
  648. max_send_fragment -= 512;
  649. if (tot == 0 || wb->buf == NULL) { /* allocate jumbo buffer */
  650. ssl3_release_write_buffer(s);
  651. packlen = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
  652. EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE,
  653. max_send_fragment, NULL);
  654. if (len >= 8 * (int)max_send_fragment)
  655. packlen *= 8;
  656. else
  657. packlen *= 4;
  658. wb->buf = OPENSSL_malloc(packlen);
  659. if (!wb->buf) {
  660. SSLerr(SSL_F_SSL3_WRITE_BYTES, ERR_R_MALLOC_FAILURE);
  661. return -1;
  662. }
  663. wb->len = packlen;
  664. } else if (tot == len) { /* done? */
  665. OPENSSL_free(wb->buf); /* free jumbo buffer */
  666. wb->buf = NULL;
  667. return tot;
  668. }
  669. n = (len - tot);
  670. for (;;) {
  671. if (n < 4 * max_send_fragment) {
  672. OPENSSL_free(wb->buf); /* free jumbo buffer */
  673. wb->buf = NULL;
  674. break;
  675. }
  676. if (s->s3->alert_dispatch) {
  677. i = s->method->ssl_dispatch_alert(s);
  678. if (i <= 0) {
  679. s->s3->wnum = tot;
  680. return i;
  681. }
  682. }
  683. if (n >= 8 * max_send_fragment)
  684. nw = max_send_fragment * (mb_param.interleave = 8);
  685. else
  686. nw = max_send_fragment * (mb_param.interleave = 4);
  687. memcpy(aad, s->s3->write_sequence, 8);
  688. aad[8] = type;
  689. aad[9] = (unsigned char)(s->version >> 8);
  690. aad[10] = (unsigned char)(s->version);
  691. aad[11] = 0;
  692. aad[12] = 0;
  693. mb_param.out = NULL;
  694. mb_param.inp = aad;
  695. mb_param.len = nw;
  696. packlen = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
  697. EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
  698. sizeof(mb_param), &mb_param);
  699. if (packlen <= 0 || packlen > (int)wb->len) { /* never happens */
  700. OPENSSL_free(wb->buf); /* free jumbo buffer */
  701. wb->buf = NULL;
  702. break;
  703. }
  704. mb_param.out = wb->buf;
  705. mb_param.inp = &buf[tot];
  706. mb_param.len = nw;
  707. if (EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
  708. EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
  709. sizeof(mb_param), &mb_param) <= 0)
  710. return -1;
  711. s->s3->write_sequence[7] += mb_param.interleave;
  712. if (s->s3->write_sequence[7] < mb_param.interleave) {
  713. int j = 6;
  714. while (j >= 0 && (++s->s3->write_sequence[j--]) == 0) ;
  715. }
  716. wb->offset = 0;
  717. wb->left = packlen;
  718. s->s3->wpend_tot = nw;
  719. s->s3->wpend_buf = &buf[tot];
  720. s->s3->wpend_type = type;
  721. s->s3->wpend_ret = nw;
  722. i = ssl3_write_pending(s, type, &buf[tot], nw);
  723. if (i <= 0) {
  724. if (i < 0 && (!s->wbio || !BIO_should_retry(s->wbio))) {
  725. OPENSSL_free(wb->buf);
  726. wb->buf = NULL;
  727. }
  728. s->s3->wnum = tot;
  729. return i;
  730. }
  731. if (i == (int)n) {
  732. OPENSSL_free(wb->buf); /* free jumbo buffer */
  733. wb->buf = NULL;
  734. return tot + i;
  735. }
  736. n -= i;
  737. tot += i;
  738. }
  739. } else
  740. #endif
  741. if (tot == len) { /* done? */
  742. if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s))
  743. ssl3_release_write_buffer(s);
  744. return tot;
  745. }
  746. n = (len - tot);
  747. for (;;) {
  748. if (n > s->max_send_fragment)
  749. nw = s->max_send_fragment;
  750. else
  751. nw = n;
  752. i = do_ssl3_write(s, type, &(buf[tot]), nw, 0);
  753. if (i <= 0) {
  754. /* XXX should we ssl3_release_write_buffer if i<0? */
  755. s->s3->wnum = tot;
  756. return i;
  757. }
  758. if ((i == (int)n) ||
  759. (type == SSL3_RT_APPLICATION_DATA &&
  760. (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
  761. /*
  762. * next chunk of data should get another prepended empty fragment
  763. * in ciphersuites with known-IV weakness:
  764. */
  765. s->s3->empty_fragment_done = 0;
  766. if ((i == (int)n) && s->mode & SSL_MODE_RELEASE_BUFFERS &&
  767. !SSL_IS_DTLS(s))
  768. ssl3_release_write_buffer(s);
  769. return tot + i;
  770. }
  771. n -= i;
  772. tot += i;
  773. }
  774. }
  775. static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
  776. unsigned int len, int create_empty_fragment)
  777. {
  778. unsigned char *p, *plen;
  779. int i, mac_size, clear = 0;
  780. int prefix_len = 0;
  781. int eivlen;
  782. long align = 0;
  783. SSL3_RECORD *wr;
  784. SSL3_BUFFER *wb = &(s->s3->wbuf);
  785. SSL_SESSION *sess;
  786. /*
  787. * first check if there is a SSL3_BUFFER still being written out. This
  788. * will happen with non blocking IO
  789. */
  790. if (wb->left != 0)
  791. return (ssl3_write_pending(s, type, buf, len));
  792. /* If we have an alert to send, lets send it */
  793. if (s->s3->alert_dispatch) {
  794. i = s->method->ssl_dispatch_alert(s);
  795. if (i <= 0)
  796. return (i);
  797. /* if it went, fall through and send more stuff */
  798. }
  799. if (wb->buf == NULL)
  800. if (!ssl3_setup_write_buffer(s))
  801. return -1;
  802. if (len == 0 && !create_empty_fragment)
  803. return 0;
  804. wr = &(s->s3->wrec);
  805. sess = s->session;
  806. if ((sess == NULL) ||
  807. (s->enc_write_ctx == NULL) ||
  808. (EVP_MD_CTX_md(s->write_hash) == NULL)) {
  809. #if 1
  810. clear = s->enc_write_ctx ? 0 : 1; /* must be AEAD cipher */
  811. #else
  812. clear = 1;
  813. #endif
  814. mac_size = 0;
  815. } else {
  816. mac_size = EVP_MD_CTX_size(s->write_hash);
  817. if (mac_size < 0)
  818. goto err;
  819. }
  820. /*
  821. * 'create_empty_fragment' is true only when this function calls itself
  822. */
  823. if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done) {
  824. /*
  825. * countermeasure against known-IV weakness in CBC ciphersuites (see
  826. * http://www.openssl.org/~bodo/tls-cbc.txt)
  827. */
  828. if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) {
  829. /*
  830. * recursive function call with 'create_empty_fragment' set; this
  831. * prepares and buffers the data for an empty fragment (these
  832. * 'prefix_len' bytes are sent out later together with the actual
  833. * payload)
  834. */
  835. prefix_len = do_ssl3_write(s, type, buf, 0, 1);
  836. if (prefix_len <= 0)
  837. goto err;
  838. if (prefix_len >
  839. (SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD))
  840. {
  841. /* insufficient space */
  842. SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);
  843. goto err;
  844. }
  845. }
  846. s->s3->empty_fragment_done = 1;
  847. }
  848. if (create_empty_fragment) {
  849. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
  850. /*
  851. * extra fragment would be couple of cipher blocks, which would be
  852. * multiple of SSL3_ALIGN_PAYLOAD, so if we want to align the real
  853. * payload, then we can just pretent we simply have two headers.
  854. */
  855. align = (long)wb->buf + 2 * SSL3_RT_HEADER_LENGTH;
  856. align = (-align) & (SSL3_ALIGN_PAYLOAD - 1);
  857. #endif
  858. p = wb->buf + align;
  859. wb->offset = align;
  860. } else if (prefix_len) {
  861. p = wb->buf + wb->offset + prefix_len;
  862. } else {
  863. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
  864. align = (long)wb->buf + SSL3_RT_HEADER_LENGTH;
  865. align = (-align) & (SSL3_ALIGN_PAYLOAD - 1);
  866. #endif
  867. p = wb->buf + align;
  868. wb->offset = align;
  869. }
  870. /* write the header */
  871. *(p++) = type & 0xff;
  872. wr->type = type;
  873. *(p++) = (s->version >> 8);
  874. /*
  875. * Some servers hang if iniatial client hello is larger than 256 bytes
  876. * and record version number > TLS 1.0
  877. */
  878. if (s->state == SSL3_ST_CW_CLNT_HELLO_B
  879. && !s->renegotiate && TLS1_get_version(s) > TLS1_VERSION)
  880. *(p++) = 0x1;
  881. else
  882. *(p++) = s->version & 0xff;
  883. /* field where we are to write out packet length */
  884. plen = p;
  885. p += 2;
  886. /* Explicit IV length, block ciphers appropriate version flag */
  887. if (s->enc_write_ctx && SSL_USE_EXPLICIT_IV(s)) {
  888. int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);
  889. if (mode == EVP_CIPH_CBC_MODE) {
  890. eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);
  891. if (eivlen <= 1)
  892. eivlen = 0;
  893. }
  894. /* Need explicit part of IV for GCM mode */
  895. else if (mode == EVP_CIPH_GCM_MODE)
  896. eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
  897. else
  898. eivlen = 0;
  899. } else
  900. eivlen = 0;
  901. /* lets setup the record stuff. */
  902. wr->data = p + eivlen;
  903. wr->length = (int)len;
  904. wr->input = (unsigned char *)buf;
  905. /*
  906. * we now 'read' from wr->input, wr->length bytes into wr->data
  907. */
  908. /* first we compress */
  909. if (s->compress != NULL) {
  910. if (!ssl3_do_compress(s)) {
  911. SSLerr(SSL_F_DO_SSL3_WRITE, SSL_R_COMPRESSION_FAILURE);
  912. goto err;
  913. }
  914. } else {
  915. memcpy(wr->data, wr->input, wr->length);
  916. wr->input = wr->data;
  917. }
  918. /*
  919. * we should still have the output to wr->data and the input from
  920. * wr->input. Length should be wr->length. wr->data still points in the
  921. * wb->buf
  922. */
  923. if (mac_size != 0) {
  924. if (s->method->ssl3_enc->mac(s, &(p[wr->length + eivlen]), 1) < 0)
  925. goto err;
  926. wr->length += mac_size;
  927. }
  928. wr->input = p;
  929. wr->data = p;
  930. if (eivlen) {
  931. /*
  932. * if (RAND_pseudo_bytes(p, eivlen) <= 0) goto err;
  933. */
  934. wr->length += eivlen;
  935. }
  936. if (s->method->ssl3_enc->enc(s, 1) < 1)
  937. goto err;
  938. /* record length after mac and block padding */
  939. s2n(wr->length, plen);
  940. if (s->msg_callback)
  941. s->msg_callback(1, 0, SSL3_RT_HEADER, plen - 5, 5, s,
  942. s->msg_callback_arg);
  943. /*
  944. * we should now have wr->data pointing to the encrypted data, which is
  945. * wr->length long
  946. */
  947. wr->type = type; /* not needed but helps for debugging */
  948. wr->length += SSL3_RT_HEADER_LENGTH;
  949. if (create_empty_fragment) {
  950. /*
  951. * we are in a recursive call; just return the length, don't write
  952. * out anything here
  953. */
  954. return wr->length;
  955. }
  956. /* now let's set up wb */
  957. wb->left = prefix_len + wr->length;
  958. /*
  959. * memorize arguments so that ssl3_write_pending can detect bad write
  960. * retries later
  961. */
  962. s->s3->wpend_tot = len;
  963. s->s3->wpend_buf = buf;
  964. s->s3->wpend_type = type;
  965. s->s3->wpend_ret = len;
  966. /* we now just need to write the buffer */
  967. return ssl3_write_pending(s, type, buf, len);
  968. err:
  969. return -1;
  970. }
  971. /* if s->s3->wbuf.left != 0, we need to call this
  972. *
  973. * Return values are as per SSL_write(), i.e.
  974. */
  975. int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
  976. unsigned int len)
  977. {
  978. int i;
  979. SSL3_BUFFER *wb = &(s->s3->wbuf);
  980. /* XXXX */
  981. if ((s->s3->wpend_tot > (int)len)
  982. || ((s->s3->wpend_buf != buf) &&
  983. !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))
  984. || (s->s3->wpend_type != type)) {
  985. SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BAD_WRITE_RETRY);
  986. return (-1);
  987. }
  988. for (;;) {
  989. clear_sys_error();
  990. if (s->wbio != NULL) {
  991. s->rwstate = SSL_WRITING;
  992. i = BIO_write(s->wbio,
  993. (char *)&(wb->buf[wb->offset]),
  994. (unsigned int)wb->left);
  995. } else {
  996. SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BIO_NOT_SET);
  997. i = -1;
  998. }
  999. if (i == wb->left) {
  1000. wb->left = 0;
  1001. wb->offset += i;
  1002. s->rwstate = SSL_NOTHING;
  1003. return (s->s3->wpend_ret);
  1004. } else if (i <= 0) {
  1005. if (SSL_IS_DTLS(s)) {
  1006. /*
  1007. * For DTLS, just drop it. That's kind of the whole point in
  1008. * using a datagram service
  1009. */
  1010. wb->left = 0;
  1011. }
  1012. return i;
  1013. }
  1014. wb->offset += i;
  1015. wb->left -= i;
  1016. }
  1017. }
  1018. /*-
  1019. * Return up to 'len' payload bytes received in 'type' records.
  1020. * 'type' is one of the following:
  1021. *
  1022. * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
  1023. * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
  1024. * - 0 (during a shutdown, no data has to be returned)
  1025. *
  1026. * If we don't have stored data to work from, read a SSL/TLS record first
  1027. * (possibly multiple records if we still don't have anything to return).
  1028. *
  1029. * This function must handle any surprises the peer may have for us, such as
  1030. * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
  1031. * a surprise, but handled as if it were), or renegotiation requests.
  1032. * Also if record payloads contain fragments too small to process, we store
  1033. * them until there is enough for the respective protocol (the record protocol
  1034. * may use arbitrary fragmentation and even interleaving):
  1035. * Change cipher spec protocol
  1036. * just 1 byte needed, no need for keeping anything stored
  1037. * Alert protocol
  1038. * 2 bytes needed (AlertLevel, AlertDescription)
  1039. * Handshake protocol
  1040. * 4 bytes needed (HandshakeType, uint24 length) -- we just have
  1041. * to detect unexpected Client Hello and Hello Request messages
  1042. * here, anything else is handled by higher layers
  1043. * Application data protocol
  1044. * none of our business
  1045. */
  1046. int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
  1047. {
  1048. int al, i, j, ret;
  1049. unsigned int n;
  1050. SSL3_RECORD *rr;
  1051. void (*cb) (const SSL *ssl, int type2, int val) = NULL;
  1052. if (s->s3->rbuf.buf == NULL) /* Not initialized yet */
  1053. if (!ssl3_setup_read_buffer(s))
  1054. return (-1);
  1055. if ((type && (type != SSL3_RT_APPLICATION_DATA)
  1056. && (type != SSL3_RT_HANDSHAKE)) || (peek
  1057. && (type !=
  1058. SSL3_RT_APPLICATION_DATA))) {
  1059. SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
  1060. return -1;
  1061. }
  1062. if ((type == SSL3_RT_HANDSHAKE) && (s->s3->handshake_fragment_len > 0))
  1063. /* (partially) satisfy request from storage */
  1064. {
  1065. unsigned char *src = s->s3->handshake_fragment;
  1066. unsigned char *dst = buf;
  1067. unsigned int k;
  1068. /* peek == 0 */
  1069. n = 0;
  1070. while ((len > 0) && (s->s3->handshake_fragment_len > 0)) {
  1071. *dst++ = *src++;
  1072. len--;
  1073. s->s3->handshake_fragment_len--;
  1074. n++;
  1075. }
  1076. /* move any remaining fragment bytes: */
  1077. for (k = 0; k < s->s3->handshake_fragment_len; k++)
  1078. s->s3->handshake_fragment[k] = *src++;
  1079. return n;
  1080. }
  1081. /*
  1082. * Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE.
  1083. */
  1084. if (!s->in_handshake && SSL_in_init(s)) {
  1085. /* type == SSL3_RT_APPLICATION_DATA */
  1086. i = s->handshake_func(s);
  1087. if (i < 0)
  1088. return (i);
  1089. if (i == 0) {
  1090. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
  1091. return (-1);
  1092. }
  1093. }
  1094. start:
  1095. s->rwstate = SSL_NOTHING;
  1096. /*-
  1097. * s->s3->rrec.type - is the type of record
  1098. * s->s3->rrec.data, - data
  1099. * s->s3->rrec.off, - offset into 'data' for next read
  1100. * s->s3->rrec.length, - number of bytes.
  1101. */
  1102. rr = &(s->s3->rrec);
  1103. /* get new packet if necessary */
  1104. if ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY)) {
  1105. ret = ssl3_get_record(s);
  1106. if (ret <= 0)
  1107. return (ret);
  1108. }
  1109. /*
  1110. * Reset the count of consecutive warning alerts if we've got a non-empty
  1111. * record that isn't an alert.
  1112. */
  1113. if (rr->type != SSL3_RT_ALERT && rr->length != 0)
  1114. s->cert->alert_count = 0;
  1115. /* we now have a packet which can be read and processed */
  1116. if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
  1117. * reset by ssl3_get_finished */
  1118. && (rr->type != SSL3_RT_HANDSHAKE)) {
  1119. al = SSL_AD_UNEXPECTED_MESSAGE;
  1120. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
  1121. goto f_err;
  1122. }
  1123. /*
  1124. * If the other end has shut down, throw anything we read away (even in
  1125. * 'peek' mode)
  1126. */
  1127. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  1128. rr->length = 0;
  1129. s->rwstate = SSL_NOTHING;
  1130. return (0);
  1131. }
  1132. if (type == rr->type) { /* SSL3_RT_APPLICATION_DATA or
  1133. * SSL3_RT_HANDSHAKE */
  1134. /*
  1135. * make sure that we are not getting application data when we are
  1136. * doing a handshake for the first time
  1137. */
  1138. if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
  1139. (s->enc_read_ctx == NULL)) {
  1140. al = SSL_AD_UNEXPECTED_MESSAGE;
  1141. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE);
  1142. goto f_err;
  1143. }
  1144. if (len <= 0)
  1145. return (len);
  1146. if ((unsigned int)len > rr->length)
  1147. n = rr->length;
  1148. else
  1149. n = (unsigned int)len;
  1150. memcpy(buf, &(rr->data[rr->off]), n);
  1151. if (!peek) {
  1152. rr->length -= n;
  1153. rr->off += n;
  1154. if (rr->length == 0) {
  1155. s->rstate = SSL_ST_READ_HEADER;
  1156. rr->off = 0;
  1157. if (s->mode & SSL_MODE_RELEASE_BUFFERS
  1158. && s->s3->rbuf.left == 0)
  1159. ssl3_release_read_buffer(s);
  1160. }
  1161. }
  1162. return (n);
  1163. }
  1164. /*
  1165. * If we get here, then type != rr->type; if we have a handshake message,
  1166. * then it was unexpected (Hello Request or Client Hello).
  1167. */
  1168. /*
  1169. * In case of record types for which we have 'fragment' storage, fill
  1170. * that so that we can process the data at a fixed place.
  1171. */
  1172. {
  1173. unsigned int dest_maxlen = 0;
  1174. unsigned char *dest = NULL;
  1175. unsigned int *dest_len = NULL;
  1176. if (rr->type == SSL3_RT_HANDSHAKE) {
  1177. dest_maxlen = sizeof s->s3->handshake_fragment;
  1178. dest = s->s3->handshake_fragment;
  1179. dest_len = &s->s3->handshake_fragment_len;
  1180. } else if (rr->type == SSL3_RT_ALERT) {
  1181. dest_maxlen = sizeof s->s3->alert_fragment;
  1182. dest = s->s3->alert_fragment;
  1183. dest_len = &s->s3->alert_fragment_len;
  1184. }
  1185. #ifndef OPENSSL_NO_HEARTBEATS
  1186. else if (rr->type == TLS1_RT_HEARTBEAT) {
  1187. tls1_process_heartbeat(s);
  1188. /* Exit and notify application to read again */
  1189. rr->length = 0;
  1190. s->rwstate = SSL_READING;
  1191. BIO_clear_retry_flags(SSL_get_rbio(s));
  1192. BIO_set_retry_read(SSL_get_rbio(s));
  1193. return (-1);
  1194. }
  1195. #endif
  1196. if (dest_maxlen > 0) {
  1197. n = dest_maxlen - *dest_len; /* available space in 'dest' */
  1198. if (rr->length < n)
  1199. n = rr->length; /* available bytes */
  1200. /* now move 'n' bytes: */
  1201. while (n-- > 0) {
  1202. dest[(*dest_len)++] = rr->data[rr->off++];
  1203. rr->length--;
  1204. }
  1205. if (*dest_len < dest_maxlen)
  1206. goto start; /* fragment was too small */
  1207. }
  1208. }
  1209. /*-
  1210. * s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
  1211. * s->s3->alert_fragment_len == 2 iff rr->type == SSL3_RT_ALERT.
  1212. * (Possibly rr is 'empty' now, i.e. rr->length may be 0.)
  1213. */
  1214. /* If we are a client, check for an incoming 'Hello Request': */
  1215. if ((!s->server) &&
  1216. (s->s3->handshake_fragment_len >= 4) &&
  1217. (s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&
  1218. (s->session != NULL) && (s->session->cipher != NULL)) {
  1219. s->s3->handshake_fragment_len = 0;
  1220. if ((s->s3->handshake_fragment[1] != 0) ||
  1221. (s->s3->handshake_fragment[2] != 0) ||
  1222. (s->s3->handshake_fragment[3] != 0)) {
  1223. al = SSL_AD_DECODE_ERROR;
  1224. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_BAD_HELLO_REQUEST);
  1225. goto f_err;
  1226. }
  1227. if (s->msg_callback)
  1228. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
  1229. s->s3->handshake_fragment, 4, s,
  1230. s->msg_callback_arg);
  1231. if (SSL_is_init_finished(s) &&
  1232. !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&
  1233. !s->s3->renegotiate) {
  1234. ssl3_renegotiate(s);
  1235. if (ssl3_renegotiate_check(s)) {
  1236. i = s->handshake_func(s);
  1237. if (i < 0)
  1238. return (i);
  1239. if (i == 0) {
  1240. SSLerr(SSL_F_SSL3_READ_BYTES,
  1241. SSL_R_SSL_HANDSHAKE_FAILURE);
  1242. return (-1);
  1243. }
  1244. if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
  1245. if (s->s3->rbuf.left == 0) { /* no read-ahead left? */
  1246. BIO *bio;
  1247. /*
  1248. * In the case where we try to read application data,
  1249. * but we trigger an SSL handshake, we return -1 with
  1250. * the retry option set. Otherwise renegotiation may
  1251. * cause nasty problems in the blocking world
  1252. */
  1253. s->rwstate = SSL_READING;
  1254. bio = SSL_get_rbio(s);
  1255. BIO_clear_retry_flags(bio);
  1256. BIO_set_retry_read(bio);
  1257. return (-1);
  1258. }
  1259. }
  1260. }
  1261. }
  1262. /*
  1263. * we either finished a handshake or ignored the request, now try
  1264. * again to obtain the (application) data we were asked for
  1265. */
  1266. goto start;
  1267. }
  1268. /*
  1269. * If we are a server and get a client hello when renegotiation isn't
  1270. * allowed send back a no renegotiation alert and carry on. WARNING:
  1271. * experimental code, needs reviewing (steve)
  1272. */
  1273. if (s->server &&
  1274. SSL_is_init_finished(s) &&
  1275. !s->s3->send_connection_binding &&
  1276. (s->version > SSL3_VERSION) &&
  1277. (s->s3->handshake_fragment_len >= 4) &&
  1278. (s->s3->handshake_fragment[0] == SSL3_MT_CLIENT_HELLO) &&
  1279. (s->session != NULL) && (s->session->cipher != NULL) &&
  1280. !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
  1281. /*
  1282. * s->s3->handshake_fragment_len = 0;
  1283. */
  1284. rr->length = 0;
  1285. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
  1286. goto start;
  1287. }
  1288. if (s->s3->alert_fragment_len >= 2) {
  1289. int alert_level = s->s3->alert_fragment[0];
  1290. int alert_descr = s->s3->alert_fragment[1];
  1291. s->s3->alert_fragment_len = 0;
  1292. if (s->msg_callback)
  1293. s->msg_callback(0, s->version, SSL3_RT_ALERT,
  1294. s->s3->alert_fragment, 2, s, s->msg_callback_arg);
  1295. if (s->info_callback != NULL)
  1296. cb = s->info_callback;
  1297. else if (s->ctx->info_callback != NULL)
  1298. cb = s->ctx->info_callback;
  1299. if (cb != NULL) {
  1300. j = (alert_level << 8) | alert_descr;
  1301. cb(s, SSL_CB_READ_ALERT, j);
  1302. }
  1303. if (alert_level == SSL3_AL_WARNING) {
  1304. s->s3->warn_alert = alert_descr;
  1305. s->cert->alert_count++;
  1306. if (s->cert->alert_count == MAX_WARN_ALERT_COUNT) {
  1307. al = SSL_AD_UNEXPECTED_MESSAGE;
  1308. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_TOO_MANY_WARN_ALERTS);
  1309. goto f_err;
  1310. }
  1311. if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
  1312. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  1313. return (0);
  1314. }
  1315. /*
  1316. * This is a warning but we receive it if we requested
  1317. * renegotiation and the peer denied it. Terminate with a fatal
  1318. * alert because if application tried to renegotiatie it
  1319. * presumably had a good reason and expects it to succeed. In
  1320. * future we might have a renegotiation where we don't care if
  1321. * the peer refused it where we carry on.
  1322. */
  1323. else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
  1324. al = SSL_AD_HANDSHAKE_FAILURE;
  1325. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_NO_RENEGOTIATION);
  1326. goto f_err;
  1327. }
  1328. #ifdef SSL_AD_MISSING_SRP_USERNAME
  1329. else if (alert_descr == SSL_AD_MISSING_SRP_USERNAME)
  1330. return (0);
  1331. #endif
  1332. } else if (alert_level == SSL3_AL_FATAL) {
  1333. char tmp[16];
  1334. s->rwstate = SSL_NOTHING;
  1335. s->s3->fatal_alert = alert_descr;
  1336. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);
  1337. BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
  1338. ERR_add_error_data(2, "SSL alert number ", tmp);
  1339. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  1340. SSL_CTX_remove_session(s->session_ctx, s->session);
  1341. return (0);
  1342. } else {
  1343. al = SSL_AD_ILLEGAL_PARAMETER;
  1344. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE);
  1345. goto f_err;
  1346. }
  1347. goto start;
  1348. }
  1349. if (s->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a
  1350. * shutdown */
  1351. s->rwstate = SSL_NOTHING;
  1352. rr->length = 0;
  1353. return (0);
  1354. }
  1355. if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
  1356. /*
  1357. * 'Change Cipher Spec' is just a single byte, so we know exactly
  1358. * what the record payload has to look like
  1359. */
  1360. if ((rr->length != 1) || (rr->off != 0) ||
  1361. (rr->data[0] != SSL3_MT_CCS)) {
  1362. al = SSL_AD_ILLEGAL_PARAMETER;
  1363. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_BAD_CHANGE_CIPHER_SPEC);
  1364. goto f_err;
  1365. }
  1366. /* Check we have a cipher to change to */
  1367. if (s->s3->tmp.new_cipher == NULL) {
  1368. al = SSL_AD_UNEXPECTED_MESSAGE;
  1369. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_CCS_RECEIVED_EARLY);
  1370. goto f_err;
  1371. }
  1372. if (!(s->s3->flags & SSL3_FLAGS_CCS_OK)) {
  1373. al = SSL_AD_UNEXPECTED_MESSAGE;
  1374. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_CCS_RECEIVED_EARLY);
  1375. goto f_err;
  1376. }
  1377. s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
  1378. rr->length = 0;
  1379. if (s->msg_callback)
  1380. s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC,
  1381. rr->data, 1, s, s->msg_callback_arg);
  1382. s->s3->change_cipher_spec = 1;
  1383. if (!ssl3_do_change_cipher_spec(s))
  1384. goto err;
  1385. else
  1386. goto start;
  1387. }
  1388. /*
  1389. * Unexpected handshake message (Client Hello, or protocol violation)
  1390. */
  1391. if ((s->s3->handshake_fragment_len >= 4) && !s->in_handshake) {
  1392. if (((s->state & SSL_ST_MASK) == SSL_ST_OK) &&
  1393. !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) {
  1394. #if 0 /* worked only because C operator preferences
  1395. * are not as expected (and because this is
  1396. * not really needed for clients except for
  1397. * detecting protocol violations): */
  1398. s->state = SSL_ST_BEFORE | (s->server)
  1399. ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
  1400. #else
  1401. s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
  1402. #endif
  1403. s->renegotiate = 1;
  1404. s->new_session = 1;
  1405. }
  1406. i = s->handshake_func(s);
  1407. if (i < 0)
  1408. return (i);
  1409. if (i == 0) {
  1410. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
  1411. return (-1);
  1412. }
  1413. if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
  1414. if (s->s3->rbuf.left == 0) { /* no read-ahead left? */
  1415. BIO *bio;
  1416. /*
  1417. * In the case where we try to read application data, but we
  1418. * trigger an SSL handshake, we return -1 with the retry
  1419. * option set. Otherwise renegotiation may cause nasty
  1420. * problems in the blocking world
  1421. */
  1422. s->rwstate = SSL_READING;
  1423. bio = SSL_get_rbio(s);
  1424. BIO_clear_retry_flags(bio);
  1425. BIO_set_retry_read(bio);
  1426. return (-1);
  1427. }
  1428. }
  1429. goto start;
  1430. }
  1431. switch (rr->type) {
  1432. default:
  1433. /*
  1434. * TLS 1.0 and 1.1 say you SHOULD ignore unrecognised record types, but
  1435. * TLS 1.2 says you MUST send an unexpected message alert. We use the
  1436. * TLS 1.2 behaviour for all protocol versions to prevent issues where
  1437. * no progress is being made and the peer continually sends unrecognised
  1438. * record types, using up resources processing them.
  1439. */
  1440. al = SSL_AD_UNEXPECTED_MESSAGE;
  1441. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
  1442. goto f_err;
  1443. case SSL3_RT_CHANGE_CIPHER_SPEC:
  1444. case SSL3_RT_ALERT:
  1445. case SSL3_RT_HANDSHAKE:
  1446. /*
  1447. * we already handled all of these, with the possible exception of
  1448. * SSL3_RT_HANDSHAKE when s->in_handshake is set, but that should not
  1449. * happen when type != rr->type
  1450. */
  1451. al = SSL_AD_UNEXPECTED_MESSAGE;
  1452. SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
  1453. goto f_err;
  1454. case SSL3_RT_APPLICATION_DATA:
  1455. /*
  1456. * At this point, we were expecting handshake data, but have
  1457. * application data. If the library was running inside ssl3_read()
  1458. * (i.e. in_read_app_data is set) and it makes sense to read
  1459. * application data at this point (session renegotiation not yet
  1460. * started), we will indulge it.
  1461. */
  1462. if (s->s3->in_read_app_data &&
  1463. (s->s3->total_renegotiations != 0) &&
  1464. (((s->state & SSL_ST_CONNECT) &&
  1465. (s->state >= SSL3_ST_CW_CLNT_HELLO_A) &&
  1466. (s->state <= SSL3_ST_CR_SRVR_HELLO_A)
  1467. ) || ((s->state & SSL_ST_ACCEPT) &&
  1468. (s->state <= SSL3_ST_SW_HELLO_REQ_A) &&
  1469. (s->state >= SSL3_ST_SR_CLNT_HELLO_A)
  1470. )
  1471. )) {
  1472. s->s3->in_read_app_data = 2;
  1473. return (-1);
  1474. } else {
  1475. al = SSL_AD_UNEXPECTED_MESSAGE;
  1476. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
  1477. goto f_err;
  1478. }
  1479. }
  1480. /* not reached */
  1481. f_err:
  1482. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1483. err:
  1484. return (-1);
  1485. }
  1486. int ssl3_do_change_cipher_spec(SSL *s)
  1487. {
  1488. int i;
  1489. const char *sender;
  1490. int slen;
  1491. if (s->state & SSL_ST_ACCEPT)
  1492. i = SSL3_CHANGE_CIPHER_SERVER_READ;
  1493. else
  1494. i = SSL3_CHANGE_CIPHER_CLIENT_READ;
  1495. if (s->s3->tmp.key_block == NULL) {
  1496. if (s->session == NULL || s->session->master_key_length == 0) {
  1497. /* might happen if dtls1_read_bytes() calls this */
  1498. SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC,
  1499. SSL_R_CCS_RECEIVED_EARLY);
  1500. return (0);
  1501. }
  1502. s->session->cipher = s->s3->tmp.new_cipher;
  1503. if (!s->method->ssl3_enc->setup_key_block(s))
  1504. return (0);
  1505. }
  1506. if (!s->method->ssl3_enc->change_cipher_state(s, i))
  1507. return (0);
  1508. /*
  1509. * we have to record the message digest at this point so we can get it
  1510. * before we read the finished message
  1511. */
  1512. if (s->state & SSL_ST_CONNECT) {
  1513. sender = s->method->ssl3_enc->server_finished_label;
  1514. slen = s->method->ssl3_enc->server_finished_label_len;
  1515. } else {
  1516. sender = s->method->ssl3_enc->client_finished_label;
  1517. slen = s->method->ssl3_enc->client_finished_label_len;
  1518. }
  1519. i = s->method->ssl3_enc->final_finish_mac(s,
  1520. sender, slen,
  1521. s->s3->tmp.peer_finish_md);
  1522. if (i == 0) {
  1523. SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
  1524. return 0;
  1525. }
  1526. s->s3->tmp.peer_finish_md_len = i;
  1527. return (1);
  1528. }
  1529. int ssl3_send_alert(SSL *s, int level, int desc)
  1530. {
  1531. /* Map tls/ssl alert value to correct one */
  1532. desc = s->method->ssl3_enc->alert_value(desc);
  1533. if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)
  1534. desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have
  1535. * protocol_version alerts */
  1536. if (desc < 0)
  1537. return -1;
  1538. /* If a fatal one, remove from cache */
  1539. if ((level == 2) && (s->session != NULL))
  1540. SSL_CTX_remove_session(s->session_ctx, s->session);
  1541. s->s3->alert_dispatch = 1;
  1542. s->s3->send_alert[0] = level;
  1543. s->s3->send_alert[1] = desc;
  1544. if (s->s3->wbuf.left == 0) /* data still being written out? */
  1545. return s->method->ssl_dispatch_alert(s);
  1546. /*
  1547. * else data is still being written out, we will get written some time in
  1548. * the future
  1549. */
  1550. return -1;
  1551. }
  1552. int ssl3_dispatch_alert(SSL *s)
  1553. {
  1554. int i, j;
  1555. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  1556. s->s3->alert_dispatch = 0;
  1557. i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0);
  1558. if (i <= 0) {
  1559. s->s3->alert_dispatch = 1;
  1560. } else {
  1561. /*
  1562. * Alert sent to BIO. If it is important, flush it now. If the
  1563. * message does not get sent due to non-blocking IO, we will not
  1564. * worry too much.
  1565. */
  1566. if (s->s3->send_alert[0] == SSL3_AL_FATAL)
  1567. (void)BIO_flush(s->wbio);
  1568. if (s->msg_callback)
  1569. s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert,
  1570. 2, s, s->msg_callback_arg);
  1571. if (s->info_callback != NULL)
  1572. cb = s->info_callback;
  1573. else if (s->ctx->info_callback != NULL)
  1574. cb = s->ctx->info_callback;
  1575. if (cb != NULL) {
  1576. j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
  1577. cb(s, SSL_CB_WRITE_ALERT, j);
  1578. }
  1579. }
  1580. return (i);
  1581. }