s3_pkt.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768
  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->ctx->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. }