Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

s3_pkt.c 38 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com). */
  108. #include <assert.h>
  109. #include <limits.h>
  110. #include <stdio.h>
  111. #include <string.h>
  112. #include <openssl/buf.h>
  113. #include <openssl/err.h>
  114. #include <openssl/evp.h>
  115. #include <openssl/mem.h>
  116. #include <openssl/rand.h>
  117. #include "internal.h"
  118. static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned int len,
  119. char fragment);
  120. static int ssl3_get_record(SSL *s);
  121. int ssl3_read_n(SSL *s, int n, int extend) {
  122. /* If |extend| is 0, obtain new n-byte packet;
  123. * if |extend| is 1, increase packet by another n bytes.
  124. *
  125. * The packet will be in the sub-array of |s->s3->rbuf.buf| specified by
  126. * |s->packet| and |s->packet_length|. (If DTLS and |extend| is 0, additional
  127. * bytes will be read into |rbuf|, up to the size of the buffer.)
  128. *
  129. * TODO(davidben): |dtls1_get_record| and |ssl3_get_record| have very
  130. * different needs. Separate the two record layers. In DTLS, |BIO_read| is
  131. * called at most once, and only when |extend| is 0. In TLS, the buffer never
  132. * contains more than one record. */
  133. int i, len, left;
  134. uintptr_t align = 0;
  135. uint8_t *pkt;
  136. SSL3_BUFFER *rb;
  137. if (n <= 0) {
  138. return n;
  139. }
  140. rb = &s->s3->rbuf;
  141. if (rb->buf == NULL && !ssl3_setup_read_buffer(s)) {
  142. return -1;
  143. }
  144. left = rb->left;
  145. align = (uintptr_t)rb->buf + SSL3_RT_HEADER_LENGTH;
  146. align = (0 - align) & (SSL3_ALIGN_PAYLOAD - 1);
  147. if (!extend) {
  148. /* start with empty packet ... */
  149. if (left == 0) {
  150. rb->offset = align;
  151. } else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH) {
  152. /* check if next packet length is large enough to justify payload
  153. * alignment... */
  154. pkt = rb->buf + rb->offset;
  155. if (pkt[0] == SSL3_RT_APPLICATION_DATA && (pkt[3] << 8 | pkt[4]) >= 128) {
  156. /* Note that even if packet is corrupted and its length field is
  157. * insane, we can only be led to wrong decision about whether memmove
  158. * will occur or not. Header values has no effect on memmove arguments
  159. * and therefore no buffer overrun can be triggered. */
  160. memmove(rb->buf + align, pkt, left);
  161. rb->offset = align;
  162. }
  163. }
  164. s->packet = rb->buf + rb->offset;
  165. s->packet_length = 0;
  166. /* ... now we can act as if 'extend' was set */
  167. }
  168. /* For DTLS/UDP reads should not span multiple packets because the read
  169. * operation returns the whole packet at once (as long as it fits into the
  170. * buffer). Moreover, if |extend| is true, we must not read another packet,
  171. * even if the entire packet was consumed. */
  172. if (SSL_IS_DTLS(s) && ((left > 0 && n > left) || extend)) {
  173. n = left;
  174. }
  175. /* if there is enough in the buffer from a previous read, take some */
  176. if (left >= n) {
  177. s->packet_length += n;
  178. rb->left = left - n;
  179. rb->offset += n;
  180. return n;
  181. }
  182. /* else we need to read more data */
  183. len = s->packet_length;
  184. pkt = rb->buf + align;
  185. /* Move any available bytes to front of buffer: |len| bytes already pointed
  186. * to by |packet|, |left| extra ones at the end. */
  187. if (s->packet != pkt) {
  188. /* len > 0 */
  189. memmove(pkt, s->packet, len + left);
  190. s->packet = pkt;
  191. rb->offset = len + align;
  192. }
  193. if (n > (int)(rb->len - rb->offset)) {
  194. OPENSSL_PUT_ERROR(SSL, ssl3_read_n, ERR_R_INTERNAL_ERROR);
  195. return -1;
  196. }
  197. int max = n;
  198. if (SSL_IS_DTLS(s) && !extend) {
  199. max = rb->len - rb->offset;
  200. }
  201. while (left < n) {
  202. /* Now we have len+left bytes at the front of s->s3->rbuf.buf and need to
  203. * read in more until we have len+n (up to len+max if possible). */
  204. ERR_clear_system_error();
  205. if (s->rbio != NULL) {
  206. s->rwstate = SSL_READING;
  207. i = BIO_read(s->rbio, pkt + len + left, max - left);
  208. } else {
  209. OPENSSL_PUT_ERROR(SSL, ssl3_read_n, SSL_R_READ_BIO_NOT_SET);
  210. i = -1;
  211. }
  212. if (i <= 0) {
  213. rb->left = left;
  214. if (len + left == 0) {
  215. ssl3_release_read_buffer(s);
  216. }
  217. return i;
  218. }
  219. left += i;
  220. /* reads should *never* span multiple packets for DTLS because the
  221. * underlying transport protocol is message oriented as opposed to byte
  222. * oriented as in the TLS case. */
  223. if (SSL_IS_DTLS(s) && n > left) {
  224. n = left; /* makes the while condition false */
  225. }
  226. }
  227. /* done reading, now the book-keeping */
  228. rb->offset += n;
  229. rb->left = left - n;
  230. s->packet_length += n;
  231. s->rwstate = SSL_NOTHING;
  232. return n;
  233. }
  234. /* MAX_EMPTY_RECORDS defines the number of consecutive, empty records that will
  235. * be processed per call to ssl3_get_record. Without this limit an attacker
  236. * could send empty records at a faster rate than we can process and cause
  237. * ssl3_get_record to loop forever. */
  238. #define MAX_EMPTY_RECORDS 32
  239. /* Call this to get a new input record. It will return <= 0 if more data is
  240. * needed, normally due to an error or non-blocking IO. When it finishes, one
  241. * packet has been decoded and can be found in
  242. * ssl->s3->rrec.type - is the type of record
  243. * ssl->s3->rrec.data - data
  244. * ssl->s3->rrec.length - number of bytes */
  245. /* used only by ssl3_read_bytes */
  246. static int ssl3_get_record(SSL *s) {
  247. uint8_t ssl_major, ssl_minor;
  248. int al, n, i, ret = -1;
  249. SSL3_RECORD *rr = &s->s3->rrec;
  250. uint8_t *p;
  251. uint16_t version;
  252. size_t extra;
  253. unsigned empty_record_count = 0;
  254. again:
  255. /* check if we have the header */
  256. if (s->rstate != SSL_ST_READ_BODY ||
  257. s->packet_length < SSL3_RT_HEADER_LENGTH) {
  258. n = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH, 0);
  259. if (n <= 0) {
  260. return n; /* error or non-blocking */
  261. }
  262. s->rstate = SSL_ST_READ_BODY;
  263. /* Some bytes were read, so the read buffer must be existant and
  264. * |s->s3->init_extra| is defined. */
  265. assert(s->s3->rbuf.buf != NULL);
  266. extra = s->s3->init_extra ? SSL3_RT_MAX_EXTRA : 0;
  267. p = s->packet;
  268. if (s->msg_callback) {
  269. s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s, s->msg_callback_arg);
  270. }
  271. /* Pull apart the header into the SSL3_RECORD */
  272. rr->type = *(p++);
  273. ssl_major = *(p++);
  274. ssl_minor = *(p++);
  275. version = (((uint16_t)ssl_major) << 8) | ssl_minor;
  276. n2s(p, rr->length);
  277. if (s->s3->have_version && version != s->version) {
  278. OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER);
  279. al = SSL_AD_PROTOCOL_VERSION;
  280. goto f_err;
  281. }
  282. if ((version >> 8) != SSL3_VERSION_MAJOR) {
  283. OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER);
  284. goto err;
  285. }
  286. if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH + extra) {
  287. al = SSL_AD_RECORD_OVERFLOW;
  288. OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
  289. goto f_err;
  290. }
  291. /* now s->rstate == SSL_ST_READ_BODY */
  292. } else {
  293. /* |packet_length| is non-zero and |s->rstate| is |SSL_ST_READ_BODY|. The
  294. * read buffer must be existant and |s->s3->init_extra| is defined. */
  295. assert(s->s3->rbuf.buf != NULL);
  296. extra = s->s3->init_extra ? SSL3_RT_MAX_EXTRA : 0;
  297. }
  298. /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
  299. if (rr->length > s->packet_length - SSL3_RT_HEADER_LENGTH) {
  300. /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
  301. i = rr->length;
  302. n = ssl3_read_n(s, i, 1);
  303. if (n <= 0) {
  304. /* Error or non-blocking IO. Now |n| == |rr->length|, and
  305. * |s->packet_length| == |SSL3_RT_HEADER_LENGTH| + |rr->length|. */
  306. return n;
  307. }
  308. }
  309. s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */
  310. /* |rr->data| points to |rr->length| bytes of ciphertext in |s->packet|. */
  311. rr->data = &s->packet[SSL3_RT_HEADER_LENGTH];
  312. /* Decrypt the packet in-place.
  313. *
  314. * TODO(davidben): This assumes |s->version| is the same as the record-layer
  315. * version which isn't always true, but it only differs with the NULL cipher
  316. * which ignores the parameter. */
  317. size_t plaintext_len;
  318. if (!SSL_AEAD_CTX_open(s->aead_read_ctx, rr->data, &plaintext_len, rr->length,
  319. rr->type, s->version, s->s3->read_sequence, rr->data,
  320. rr->length)) {
  321. al = SSL_AD_BAD_RECORD_MAC;
  322. OPENSSL_PUT_ERROR(SSL, ssl3_get_record,
  323. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  324. goto f_err;
  325. }
  326. if (!ssl3_record_sequence_update(s->s3->read_sequence, 8)) {
  327. goto err;
  328. }
  329. if (plaintext_len > SSL3_RT_MAX_PLAIN_LENGTH + extra) {
  330. al = SSL_AD_RECORD_OVERFLOW;
  331. OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_DATA_LENGTH_TOO_LONG);
  332. goto f_err;
  333. }
  334. assert(plaintext_len <= (1u << 16));
  335. rr->length = plaintext_len;
  336. rr->off = 0;
  337. /* So at this point the following is true:
  338. * ssl->s3->rrec.type is the type of record;
  339. * ssl->s3->rrec.length is the number of bytes in the record;
  340. * ssl->s3->rrec.off is the offset to first valid byte;
  341. * ssl->s3->rrec.data the first byte of the record body. */
  342. /* we have pulled in a full packet so zero things */
  343. s->packet_length = 0;
  344. /* just read a 0 length packet */
  345. if (rr->length == 0) {
  346. empty_record_count++;
  347. if (empty_record_count > MAX_EMPTY_RECORDS) {
  348. al = SSL_AD_UNEXPECTED_MESSAGE;
  349. OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_TOO_MANY_EMPTY_FRAGMENTS);
  350. goto f_err;
  351. }
  352. goto again;
  353. }
  354. return 1;
  355. f_err:
  356. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  357. err:
  358. return ret;
  359. }
  360. /* Call this to write data in records of type |type|. It will return <= 0 if
  361. * not all data has been sent or non-blocking IO. */
  362. int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len) {
  363. const uint8_t *buf = buf_;
  364. unsigned int tot, n, nw;
  365. int i;
  366. s->rwstate = SSL_NOTHING;
  367. assert(s->s3->wnum <= INT_MAX);
  368. tot = s->s3->wnum;
  369. s->s3->wnum = 0;
  370. if (!s->in_handshake && SSL_in_init(s) && !SSL_in_false_start(s)) {
  371. i = s->handshake_func(s);
  372. if (i < 0) {
  373. return i;
  374. }
  375. if (i == 0) {
  376. OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
  377. return -1;
  378. }
  379. }
  380. /* Ensure that if we end up with a smaller value of data to write out than
  381. * the the original len from a write which didn't complete for non-blocking
  382. * I/O and also somehow ended up avoiding the check for this in
  383. * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
  384. * end up with (len-tot) as a large number that will then promptly send
  385. * beyond the end of the users buffer ... so we trap and report the error in
  386. * a way the user will notice. */
  387. if (len < 0 || (size_t)len < tot) {
  388. OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_BAD_LENGTH);
  389. return -1;
  390. }
  391. int record_split_done = 0;
  392. n = (len - tot);
  393. for (;;) {
  394. /* max contains the maximum number of bytes that we can put into a
  395. * record. */
  396. unsigned max = s->max_send_fragment;
  397. /* fragment is true if do_ssl3_write should send the first byte in its own
  398. * record in order to randomise a CBC IV. */
  399. int fragment = 0;
  400. if (!record_split_done && s->s3->need_record_splitting &&
  401. type == SSL3_RT_APPLICATION_DATA) {
  402. /* Only the the first record per write call needs to be split. The
  403. * remaining plaintext was determined before the IV was randomized. */
  404. fragment = 1;
  405. record_split_done = 1;
  406. }
  407. if (n > max) {
  408. nw = max;
  409. } else {
  410. nw = n;
  411. }
  412. i = do_ssl3_write(s, type, &buf[tot], nw, fragment);
  413. if (i <= 0) {
  414. s->s3->wnum = tot;
  415. return i;
  416. }
  417. if (i == (int)n || (type == SSL3_RT_APPLICATION_DATA &&
  418. (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
  419. return tot + i;
  420. }
  421. n -= i;
  422. tot += i;
  423. }
  424. }
  425. /* ssl3_seal_record seals a new record of type |type| and plaintext |in| and
  426. * writes it to |out|. At most |max_out| bytes will be written. It returns one
  427. * on success and zero on error. On success, it updates the write sequence
  428. * number. */
  429. static int ssl3_seal_record(SSL *s, uint8_t *out, size_t *out_len,
  430. size_t max_out, uint8_t type, const uint8_t *in,
  431. size_t in_len) {
  432. if (max_out < SSL3_RT_HEADER_LENGTH) {
  433. OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, SSL_R_BUFFER_TOO_SMALL);
  434. return 0;
  435. }
  436. out[0] = type;
  437. /* Some servers hang if initial ClientHello is larger than 256 bytes and
  438. * record version number > TLS 1.0. */
  439. uint16_t wire_version = s->version;
  440. if (!s->s3->have_version && s->version > SSL3_VERSION) {
  441. wire_version = TLS1_VERSION;
  442. }
  443. out[1] = wire_version >> 8;
  444. out[2] = wire_version & 0xff;
  445. size_t ciphertext_len;
  446. if (!SSL_AEAD_CTX_seal(s->aead_write_ctx, out + SSL3_RT_HEADER_LENGTH,
  447. &ciphertext_len, max_out - SSL3_RT_HEADER_LENGTH,
  448. type, wire_version, s->s3->write_sequence, in,
  449. in_len) ||
  450. !ssl3_record_sequence_update(s->s3->write_sequence, 8)) {
  451. return 0;
  452. }
  453. if (ciphertext_len >= 1 << 16) {
  454. OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, ERR_R_OVERFLOW);
  455. return 0;
  456. }
  457. out[3] = ciphertext_len >> 8;
  458. out[4] = ciphertext_len & 0xff;
  459. *out_len = SSL3_RT_HEADER_LENGTH + ciphertext_len;
  460. if (s->msg_callback) {
  461. s->msg_callback(1 /* write */, 0, SSL3_RT_HEADER, out, SSL3_RT_HEADER_LENGTH,
  462. s, s->msg_callback_arg);
  463. }
  464. return 1;
  465. }
  466. /* do_ssl3_write writes an SSL record of the given type. If |fragment| is 1
  467. * then it splits the record into a one byte record and a record with the rest
  468. * of the data in order to randomise a CBC IV. */
  469. static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned int len,
  470. char fragment) {
  471. SSL3_BUFFER *wb = &s->s3->wbuf;
  472. /* first check if there is a SSL3_BUFFER still being written out. This will
  473. * happen with non blocking IO */
  474. if (wb->left != 0) {
  475. return ssl3_write_pending(s, type, buf, len);
  476. }
  477. /* If we have an alert to send, lets send it */
  478. if (s->s3->alert_dispatch) {
  479. int ret = s->method->ssl_dispatch_alert(s);
  480. if (ret <= 0) {
  481. return ret;
  482. }
  483. /* if it went, fall through and send more stuff */
  484. }
  485. if (wb->buf == NULL && !ssl3_setup_write_buffer(s)) {
  486. return -1;
  487. }
  488. if (len == 0) {
  489. return 0;
  490. }
  491. if (len == 1) {
  492. /* No sense in fragmenting a one-byte record. */
  493. fragment = 0;
  494. }
  495. /* Align the output so the ciphertext is aligned to |SSL3_ALIGN_PAYLOAD|. */
  496. uintptr_t align;
  497. if (fragment) {
  498. /* Only CBC-mode ciphers require fragmenting. CBC-mode ciphertext is a
  499. * multiple of the block size which we may assume is aligned. Thus we only
  500. * need to account for a second copy of the record header. */
  501. align = (uintptr_t)wb->buf + 2 * SSL3_RT_HEADER_LENGTH;
  502. } else {
  503. align = (uintptr_t)wb->buf + SSL3_RT_HEADER_LENGTH;
  504. }
  505. align = (0 - align) & (SSL3_ALIGN_PAYLOAD - 1);
  506. uint8_t *out = wb->buf + align;
  507. wb->offset = align;
  508. size_t max_out = wb->len - wb->offset;
  509. const uint8_t *orig_buf = buf;
  510. unsigned int orig_len = len;
  511. size_t fragment_len = 0;
  512. if (fragment) {
  513. /* Write the first byte in its own record as a countermeasure against
  514. * known-IV weaknesses in CBC ciphersuites. (See
  515. * http://www.openssl.org/~bodo/tls-cbc.txt.) */
  516. if (!ssl3_seal_record(s, out, &fragment_len, max_out, type, buf, 1)) {
  517. return -1;
  518. }
  519. out += fragment_len;
  520. max_out -= fragment_len;
  521. buf++;
  522. len--;
  523. }
  524. assert((((uintptr_t)out + SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1))
  525. == 0);
  526. size_t ciphertext_len;
  527. if (!ssl3_seal_record(s, out, &ciphertext_len, max_out, type, buf, len)) {
  528. return -1;
  529. }
  530. ciphertext_len += fragment_len;
  531. /* now let's set up wb */
  532. wb->left = ciphertext_len;
  533. /* memorize arguments so that ssl3_write_pending can detect bad write retries
  534. * later */
  535. s->s3->wpend_tot = orig_len;
  536. s->s3->wpend_buf = orig_buf;
  537. s->s3->wpend_type = type;
  538. s->s3->wpend_ret = orig_len;
  539. /* we now just need to write the buffer */
  540. return ssl3_write_pending(s, type, orig_buf, orig_len);
  541. }
  542. /* if s->s3->wbuf.left != 0, we need to call this */
  543. int ssl3_write_pending(SSL *s, int type, const uint8_t *buf, unsigned int len) {
  544. int i;
  545. SSL3_BUFFER *wb = &(s->s3->wbuf);
  546. if (s->s3->wpend_tot > (int)len ||
  547. (s->s3->wpend_buf != buf &&
  548. !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) ||
  549. s->s3->wpend_type != type) {
  550. OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BAD_WRITE_RETRY);
  551. return -1;
  552. }
  553. for (;;) {
  554. ERR_clear_system_error();
  555. if (s->wbio != NULL) {
  556. s->rwstate = SSL_WRITING;
  557. i = BIO_write(s->wbio, (char *)&(wb->buf[wb->offset]),
  558. (unsigned int)wb->left);
  559. } else {
  560. OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BIO_NOT_SET);
  561. i = -1;
  562. }
  563. if (i == wb->left) {
  564. wb->left = 0;
  565. wb->offset += i;
  566. ssl3_release_write_buffer(s);
  567. s->rwstate = SSL_NOTHING;
  568. return s->s3->wpend_ret;
  569. } else if (i <= 0) {
  570. if (SSL_IS_DTLS(s)) {
  571. /* For DTLS, just drop it. That's kind of the whole point in
  572. * using a datagram service */
  573. wb->left = 0;
  574. }
  575. return i;
  576. }
  577. /* TODO(davidben): This codepath is used in DTLS, but the write
  578. * payload may not split across packets. */
  579. wb->offset += i;
  580. wb->left -= i;
  581. }
  582. }
  583. /* ssl3_expect_change_cipher_spec informs the record layer that a
  584. * ChangeCipherSpec record is required at this point. If a Handshake record is
  585. * received before ChangeCipherSpec, the connection will fail. Moreover, if
  586. * there are unprocessed handshake bytes, the handshake will also fail and the
  587. * function returns zero. Otherwise, the function returns one. */
  588. int ssl3_expect_change_cipher_spec(SSL *s) {
  589. if (s->s3->handshake_fragment_len > 0 || s->s3->tmp.reuse_message) {
  590. OPENSSL_PUT_ERROR(SSL, ssl3_expect_change_cipher_spec,
  591. SSL_R_UNPROCESSED_HANDSHAKE_DATA);
  592. return 0;
  593. }
  594. s->s3->flags |= SSL3_FLAGS_EXPECT_CCS;
  595. return 1;
  596. }
  597. /* Return up to 'len' payload bytes received in 'type' records.
  598. * 'type' is one of the following:
  599. *
  600. * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
  601. * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
  602. * - 0 (during a shutdown, no data has to be returned)
  603. *
  604. * If we don't have stored data to work from, read a SSL/TLS record first
  605. * (possibly multiple records if we still don't have anything to return).
  606. *
  607. * This function must handle any surprises the peer may have for us, such as
  608. * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
  609. * a surprise, but handled as if it were), or renegotiation requests.
  610. * Also if record payloads contain fragments too small to process, we store
  611. * them until there is enough for the respective protocol (the record protocol
  612. * may use arbitrary fragmentation and even interleaving):
  613. * Change cipher spec protocol
  614. * just 1 byte needed, no need for keeping anything stored
  615. * Alert protocol
  616. * 2 bytes needed (AlertLevel, AlertDescription)
  617. * Handshake protocol
  618. * 4 bytes needed (HandshakeType, uint24 length) -- we just have
  619. * to detect unexpected Client Hello and Hello Request messages
  620. * here, anything else is handled by higher layers
  621. * Application data protocol
  622. * none of our business
  623. */
  624. int ssl3_read_bytes(SSL *s, int type, uint8_t *buf, int len, int peek) {
  625. int al, i, ret;
  626. unsigned int n;
  627. SSL3_RECORD *rr;
  628. void (*cb)(const SSL *ssl, int type2, int val) = NULL;
  629. if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) ||
  630. (peek && type != SSL3_RT_APPLICATION_DATA)) {
  631. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, ERR_R_INTERNAL_ERROR);
  632. return -1;
  633. }
  634. if (type == SSL3_RT_HANDSHAKE && s->s3->handshake_fragment_len > 0) {
  635. /* (partially) satisfy request from storage */
  636. uint8_t *src = s->s3->handshake_fragment;
  637. uint8_t *dst = buf;
  638. unsigned int k;
  639. /* peek == 0 */
  640. n = 0;
  641. while (len > 0 && s->s3->handshake_fragment_len > 0) {
  642. *dst++ = *src++;
  643. len--;
  644. s->s3->handshake_fragment_len--;
  645. n++;
  646. }
  647. /* move any remaining fragment bytes: */
  648. for (k = 0; k < s->s3->handshake_fragment_len; k++) {
  649. s->s3->handshake_fragment[k] = *src++;
  650. }
  651. return n;
  652. }
  653. /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */
  654. /* This may require multiple iterations. False Start will cause
  655. * |s->handshake_func| to signal success one step early, but the handshake
  656. * must be completely finished before other modes are accepted.
  657. *
  658. * TODO(davidben): Move this check up to a higher level. */
  659. while (!s->in_handshake && SSL_in_init(s)) {
  660. assert(type == SSL3_RT_APPLICATION_DATA);
  661. i = s->handshake_func(s);
  662. if (i < 0) {
  663. return i;
  664. }
  665. if (i == 0) {
  666. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
  667. return -1;
  668. }
  669. }
  670. start:
  671. s->rwstate = SSL_NOTHING;
  672. /* s->s3->rrec.type - is the type of record
  673. * s->s3->rrec.data - data
  674. * s->s3->rrec.off - offset into 'data' for next read
  675. * s->s3->rrec.length - number of bytes. */
  676. rr = &s->s3->rrec;
  677. /* get new packet if necessary */
  678. if (rr->length == 0 || s->rstate == SSL_ST_READ_BODY) {
  679. ret = ssl3_get_record(s);
  680. if (ret <= 0) {
  681. return ret;
  682. }
  683. }
  684. /* we now have a packet which can be read and processed */
  685. /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by
  686. * ssl3_get_finished. */
  687. if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE &&
  688. rr->type != SSL3_RT_ALERT) {
  689. al = SSL_AD_UNEXPECTED_MESSAGE;
  690. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
  691. SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
  692. goto f_err;
  693. }
  694. /* If we are expecting a ChangeCipherSpec, it is illegal to receive a
  695. * Handshake record. */
  696. if (rr->type == SSL3_RT_HANDSHAKE && (s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
  697. al = SSL_AD_UNEXPECTED_MESSAGE;
  698. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_HANDSHAKE_RECORD_BEFORE_CCS);
  699. goto f_err;
  700. }
  701. /* If the other end has shut down, throw anything we read away (even in
  702. * 'peek' mode) */
  703. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  704. rr->length = 0;
  705. s->rwstate = SSL_NOTHING;
  706. return 0;
  707. }
  708. if (type == rr->type) {
  709. /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
  710. /* make sure that we are not getting application data when we are doing a
  711. * handshake for the first time */
  712. if (SSL_in_init(s) && type == SSL3_RT_APPLICATION_DATA &&
  713. s->aead_read_ctx == NULL) {
  714. /* TODO(davidben): Is this check redundant with the handshake_func
  715. * check? */
  716. al = SSL_AD_UNEXPECTED_MESSAGE;
  717. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_APP_DATA_IN_HANDSHAKE);
  718. goto f_err;
  719. }
  720. if (len <= 0) {
  721. return len;
  722. }
  723. if ((unsigned int)len > rr->length) {
  724. n = rr->length;
  725. } else {
  726. n = (unsigned int)len;
  727. }
  728. memcpy(buf, &(rr->data[rr->off]), n);
  729. if (!peek) {
  730. rr->length -= n;
  731. rr->off += n;
  732. if (rr->length == 0) {
  733. s->rstate = SSL_ST_READ_HEADER;
  734. rr->off = 0;
  735. if (s->s3->rbuf.left == 0) {
  736. ssl3_release_read_buffer(s);
  737. }
  738. }
  739. }
  740. return n;
  741. }
  742. /* If we get here, then type != rr->type; if we have a handshake message,
  743. * then it was unexpected (Hello Request or Client Hello). */
  744. /* In case of record types for which we have 'fragment' storage, fill that so
  745. * that we can process the data at a fixed place. */
  746. if (rr->type == SSL3_RT_HANDSHAKE) {
  747. /* If peer renegotiations are disabled, all out-of-order handshake records
  748. * are fatal. */
  749. if (!s->accept_peer_renegotiations) {
  750. al = SSL_AD_NO_RENEGOTIATION;
  751. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION);
  752. goto f_err;
  753. }
  754. const size_t size = sizeof(s->s3->handshake_fragment);
  755. const size_t avail = size - s->s3->handshake_fragment_len;
  756. const size_t todo = (rr->length < avail) ? rr->length : avail;
  757. memcpy(s->s3->handshake_fragment + s->s3->handshake_fragment_len,
  758. &rr->data[rr->off], todo);
  759. rr->off += todo;
  760. rr->length -= todo;
  761. s->s3->handshake_fragment_len += todo;
  762. if (s->s3->handshake_fragment_len < size) {
  763. goto start; /* fragment was too small */
  764. }
  765. }
  766. /* s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
  767. * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */
  768. /* If we are a client, check for an incoming 'Hello Request': */
  769. if (!s->server && s->s3->handshake_fragment_len >= 4 &&
  770. s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST &&
  771. s->session != NULL && s->session->cipher != NULL) {
  772. s->s3->handshake_fragment_len = 0;
  773. if (s->s3->handshake_fragment[1] != 0 ||
  774. s->s3->handshake_fragment[2] != 0 ||
  775. s->s3->handshake_fragment[3] != 0) {
  776. al = SSL_AD_DECODE_ERROR;
  777. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_HELLO_REQUEST);
  778. goto f_err;
  779. }
  780. if (s->msg_callback) {
  781. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
  782. s->s3->handshake_fragment, 4, s, s->msg_callback_arg);
  783. }
  784. if (SSL_is_init_finished(s) && !s->s3->renegotiate) {
  785. ssl3_renegotiate(s);
  786. if (ssl3_renegotiate_check(s)) {
  787. i = s->handshake_func(s);
  788. if (i < 0) {
  789. return i;
  790. }
  791. if (i == 0) {
  792. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
  793. return -1;
  794. }
  795. }
  796. }
  797. /* we either finished a handshake or ignored the request, now try again to
  798. * obtain the (application) data we were asked for */
  799. goto start;
  800. }
  801. /* If an alert record, process one alert out of the record. Note that we allow
  802. * a single record to contain multiple alerts. */
  803. if (rr->type == SSL3_RT_ALERT) {
  804. /* Alerts may not be fragmented. */
  805. if (rr->length < 2) {
  806. al = SSL_AD_DECODE_ERROR;
  807. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_ALERT);
  808. goto f_err;
  809. }
  810. if (s->msg_callback) {
  811. s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s,
  812. s->msg_callback_arg);
  813. }
  814. const uint8_t alert_level = rr->data[rr->off++];
  815. const uint8_t alert_descr = rr->data[rr->off++];
  816. rr->length -= 2;
  817. if (s->info_callback != NULL) {
  818. cb = s->info_callback;
  819. } else if (s->ctx->info_callback != NULL) {
  820. cb = s->ctx->info_callback;
  821. }
  822. if (cb != NULL) {
  823. uint16_t alert = (alert_level << 8) | alert_descr;
  824. cb(s, SSL_CB_READ_ALERT, alert);
  825. }
  826. if (alert_level == SSL3_AL_WARNING) {
  827. s->s3->warn_alert = alert_descr;
  828. if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
  829. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  830. return 0;
  831. }
  832. /* This is a warning but we receive it if we requested renegotiation and
  833. * the peer denied it. Terminate with a fatal alert because if
  834. * application tried to renegotiatie it presumably had a good reason and
  835. * expects it to succeed.
  836. *
  837. * In future we might have a renegotiation where we don't care if the
  838. * peer refused it where we carry on. */
  839. else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
  840. al = SSL_AD_HANDSHAKE_FAILURE;
  841. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION);
  842. goto f_err;
  843. }
  844. } else if (alert_level == SSL3_AL_FATAL) {
  845. char tmp[16];
  846. s->rwstate = SSL_NOTHING;
  847. s->s3->fatal_alert = alert_descr;
  848. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
  849. SSL_AD_REASON_OFFSET + alert_descr);
  850. BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
  851. ERR_add_error_data(2, "SSL alert number ", tmp);
  852. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  853. SSL_CTX_remove_session(s->ctx, s->session);
  854. return 0;
  855. } else {
  856. al = SSL_AD_ILLEGAL_PARAMETER;
  857. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNKNOWN_ALERT_TYPE);
  858. goto f_err;
  859. }
  860. goto start;
  861. }
  862. if (s->shutdown & SSL_SENT_SHUTDOWN) {
  863. /* but we have not received a shutdown */
  864. s->rwstate = SSL_NOTHING;
  865. rr->length = 0;
  866. return 0;
  867. }
  868. if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
  869. /* 'Change Cipher Spec' is just a single byte, so we know exactly what the
  870. * record payload has to look like */
  871. if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) {
  872. al = SSL_AD_ILLEGAL_PARAMETER;
  873. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_CHANGE_CIPHER_SPEC);
  874. goto f_err;
  875. }
  876. /* Check we have a cipher to change to */
  877. if (s->s3->tmp.new_cipher == NULL) {
  878. al = SSL_AD_UNEXPECTED_MESSAGE;
  879. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
  880. goto f_err;
  881. }
  882. if (!(s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
  883. al = SSL_AD_UNEXPECTED_MESSAGE;
  884. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
  885. goto f_err;
  886. }
  887. s->s3->flags &= ~SSL3_FLAGS_EXPECT_CCS;
  888. rr->length = 0;
  889. if (s->msg_callback) {
  890. s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s,
  891. s->msg_callback_arg);
  892. }
  893. s->s3->change_cipher_spec = 1;
  894. if (!ssl3_do_change_cipher_spec(s)) {
  895. goto err;
  896. } else {
  897. goto start;
  898. }
  899. }
  900. /* Unexpected handshake message (Client Hello, or protocol violation) */
  901. if (s->s3->handshake_fragment_len >= 4 && !s->in_handshake) {
  902. if ((s->state & SSL_ST_MASK) == SSL_ST_OK) {
  903. s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
  904. s->renegotiate = 1;
  905. }
  906. i = s->handshake_func(s);
  907. if (i < 0) {
  908. return i;
  909. }
  910. if (i == 0) {
  911. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
  912. return -1;
  913. }
  914. goto start;
  915. }
  916. /* We already handled these. */
  917. assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT &&
  918. rr->type != SSL3_RT_HANDSHAKE);
  919. al = SSL_AD_UNEXPECTED_MESSAGE;
  920. OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNEXPECTED_RECORD);
  921. f_err:
  922. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  923. err:
  924. return -1;
  925. }
  926. int ssl3_do_change_cipher_spec(SSL *s) {
  927. int i;
  928. if (s->state & SSL_ST_ACCEPT) {
  929. i = SSL3_CHANGE_CIPHER_SERVER_READ;
  930. } else {
  931. i = SSL3_CHANGE_CIPHER_CLIENT_READ;
  932. }
  933. if (s->s3->tmp.key_block == NULL) {
  934. if (s->session == NULL || s->session->master_key_length == 0) {
  935. /* might happen if dtls1_read_bytes() calls this */
  936. OPENSSL_PUT_ERROR(SSL, ssl3_do_change_cipher_spec,
  937. SSL_R_CCS_RECEIVED_EARLY);
  938. return 0;
  939. }
  940. s->session->cipher = s->s3->tmp.new_cipher;
  941. if (!s->enc_method->setup_key_block(s)) {
  942. return 0;
  943. }
  944. }
  945. if (!s->enc_method->change_cipher_state(s, i)) {
  946. return 0;
  947. }
  948. return 1;
  949. }
  950. int ssl3_send_alert(SSL *s, int level, int desc) {
  951. /* Map tls/ssl alert value to correct one */
  952. desc = s->enc_method->alert_value(desc);
  953. if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) {
  954. /* SSL 3.0 does not have protocol_version alerts */
  955. desc = SSL_AD_HANDSHAKE_FAILURE;
  956. }
  957. if (desc < 0) {
  958. return -1;
  959. }
  960. /* If a fatal one, remove from cache */
  961. if (level == 2 && s->session != NULL) {
  962. SSL_CTX_remove_session(s->ctx, s->session);
  963. }
  964. s->s3->alert_dispatch = 1;
  965. s->s3->send_alert[0] = level;
  966. s->s3->send_alert[1] = desc;
  967. if (s->s3->wbuf.left == 0) {
  968. /* data is still being written out. */
  969. return s->method->ssl_dispatch_alert(s);
  970. }
  971. /* else data is still being written out, we will get written some time in the
  972. * future */
  973. return -1;
  974. }
  975. int ssl3_dispatch_alert(SSL *s) {
  976. int i, j;
  977. void (*cb)(const SSL *ssl, int type, int val) = NULL;
  978. s->s3->alert_dispatch = 0;
  979. i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0);
  980. if (i <= 0) {
  981. s->s3->alert_dispatch = 1;
  982. } else {
  983. /* Alert sent to BIO. If it is important, flush it now. If the message
  984. * does not get sent due to non-blocking IO, we will not worry too much. */
  985. if (s->s3->send_alert[0] == SSL3_AL_FATAL) {
  986. BIO_flush(s->wbio);
  987. }
  988. if (s->msg_callback) {
  989. s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s,
  990. s->msg_callback_arg);
  991. }
  992. if (s->info_callback != NULL) {
  993. cb = s->info_callback;
  994. } else if (s->ctx->info_callback != NULL) {
  995. cb = s->ctx->info_callback;
  996. }
  997. if (cb != NULL) {
  998. j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
  999. cb(s, SSL_CB_WRITE_ALERT, j);
  1000. }
  1001. }
  1002. return i;
  1003. }