s3_pkt.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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 <openssl/ssl.h>
  109. #include <assert.h>
  110. #include <limits.h>
  111. #include <stdio.h>
  112. #include <string.h>
  113. #include <openssl/buf.h>
  114. #include <openssl/err.h>
  115. #include <openssl/evp.h>
  116. #include <openssl/mem.h>
  117. #include <openssl/rand.h>
  118. #include "internal.h"
  119. static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len);
  120. /* kMaxWarningAlerts is the number of consecutive warning alerts that will be
  121. * processed. */
  122. static const uint8_t kMaxWarningAlerts = 4;
  123. /* ssl3_get_record reads a new input record. On success, it places it in
  124. * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if
  125. * more data is needed. */
  126. static int ssl3_get_record(SSL *ssl) {
  127. int ret;
  128. again:
  129. /* Ensure the buffer is large enough to decrypt in-place. */
  130. ret = ssl_read_buffer_extend_to(ssl, ssl_record_prefix_len(ssl));
  131. if (ret <= 0) {
  132. return ret;
  133. }
  134. assert(ssl_read_buffer_len(ssl) >= ssl_record_prefix_len(ssl));
  135. uint8_t *out = ssl_read_buffer(ssl) + ssl_record_prefix_len(ssl);
  136. size_t max_out = ssl_read_buffer_len(ssl) - ssl_record_prefix_len(ssl);
  137. uint8_t type, alert;
  138. size_t len, consumed;
  139. switch (tls_open_record(ssl, &type, out, &len, &consumed, &alert, max_out,
  140. ssl_read_buffer(ssl), ssl_read_buffer_len(ssl))) {
  141. case ssl_open_record_success:
  142. ssl_read_buffer_consume(ssl, consumed);
  143. if (len > 0xffff) {
  144. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  145. return -1;
  146. }
  147. SSL3_RECORD *rr = &ssl->s3->rrec;
  148. rr->type = type;
  149. rr->length = (uint16_t)len;
  150. rr->data = out;
  151. return 1;
  152. case ssl_open_record_partial:
  153. ret = ssl_read_buffer_extend_to(ssl, consumed);
  154. if (ret <= 0) {
  155. return ret;
  156. }
  157. goto again;
  158. case ssl_open_record_discard:
  159. ssl_read_buffer_consume(ssl, consumed);
  160. goto again;
  161. case ssl_open_record_error:
  162. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  163. return -1;
  164. }
  165. assert(0);
  166. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  167. return -1;
  168. }
  169. int ssl3_write_app_data(SSL *ssl, const void *buf, int len) {
  170. return ssl3_write_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len);
  171. }
  172. /* Call this to write data in records of type |type|. It will return <= 0 if
  173. * not all data has been sent or non-blocking IO. */
  174. int ssl3_write_bytes(SSL *ssl, int type, const void *buf_, int len) {
  175. const uint8_t *buf = buf_;
  176. unsigned int tot, n, nw;
  177. int i;
  178. ssl->rwstate = SSL_NOTHING;
  179. assert(ssl->s3->wnum <= INT_MAX);
  180. tot = ssl->s3->wnum;
  181. ssl->s3->wnum = 0;
  182. if (!ssl->in_handshake && SSL_in_init(ssl) && !SSL_in_false_start(ssl)) {
  183. i = ssl->handshake_func(ssl);
  184. if (i < 0) {
  185. return i;
  186. }
  187. if (i == 0) {
  188. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  189. return -1;
  190. }
  191. }
  192. /* Ensure that if we end up with a smaller value of data to write out than
  193. * the the original len from a write which didn't complete for non-blocking
  194. * I/O and also somehow ended up avoiding the check for this in
  195. * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
  196. * end up with (len-tot) as a large number that will then promptly send
  197. * beyond the end of the users buffer ... so we trap and report the error in
  198. * a way the user will notice. */
  199. if (len < 0 || (size_t)len < tot) {
  200. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH);
  201. return -1;
  202. }
  203. n = (len - tot);
  204. for (;;) {
  205. /* max contains the maximum number of bytes that we can put into a
  206. * record. */
  207. unsigned max = ssl->max_send_fragment;
  208. if (n > max) {
  209. nw = max;
  210. } else {
  211. nw = n;
  212. }
  213. i = do_ssl3_write(ssl, type, &buf[tot], nw);
  214. if (i <= 0) {
  215. ssl->s3->wnum = tot;
  216. return i;
  217. }
  218. if (i == (int)n || (type == SSL3_RT_APPLICATION_DATA &&
  219. (ssl->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
  220. return tot + i;
  221. }
  222. n -= i;
  223. tot += i;
  224. }
  225. }
  226. static int ssl3_write_pending(SSL *ssl, int type, const uint8_t *buf,
  227. unsigned int len) {
  228. if (ssl->s3->wpend_tot > (int)len ||
  229. (ssl->s3->wpend_buf != buf &&
  230. !(ssl->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) ||
  231. ssl->s3->wpend_type != type) {
  232. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_WRITE_RETRY);
  233. return -1;
  234. }
  235. int ret = ssl_write_buffer_flush(ssl);
  236. if (ret <= 0) {
  237. return ret;
  238. }
  239. return ssl->s3->wpend_ret;
  240. }
  241. /* do_ssl3_write writes an SSL record of the given type. */
  242. static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len) {
  243. /* If there is still data from the previous record, flush it. */
  244. if (ssl_write_buffer_is_pending(ssl)) {
  245. return ssl3_write_pending(ssl, type, buf, len);
  246. }
  247. /* If we have an alert to send, lets send it */
  248. if (ssl->s3->alert_dispatch) {
  249. int ret = ssl->method->ssl_dispatch_alert(ssl);
  250. if (ret <= 0) {
  251. return ret;
  252. }
  253. /* if it went, fall through and send more stuff */
  254. }
  255. if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
  256. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  257. return -1;
  258. }
  259. if (len == 0) {
  260. return 0;
  261. }
  262. size_t max_out = len + ssl_max_seal_overhead(ssl);
  263. if (max_out < len) {
  264. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  265. return -1;
  266. }
  267. uint8_t *out;
  268. size_t ciphertext_len;
  269. if (!ssl_write_buffer_init(ssl, &out, max_out) ||
  270. !tls_seal_record(ssl, out, &ciphertext_len, max_out, type, buf, len)) {
  271. return -1;
  272. }
  273. ssl_write_buffer_set_len(ssl, ciphertext_len);
  274. /* memorize arguments so that ssl3_write_pending can detect bad write retries
  275. * later */
  276. ssl->s3->wpend_tot = len;
  277. ssl->s3->wpend_buf = buf;
  278. ssl->s3->wpend_type = type;
  279. ssl->s3->wpend_ret = len;
  280. /* we now just need to write the buffer */
  281. return ssl3_write_pending(ssl, type, buf, len);
  282. }
  283. int ssl3_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) {
  284. return ssl3_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek);
  285. }
  286. int ssl3_read_change_cipher_spec(SSL *ssl) {
  287. uint8_t byte;
  288. int ret = ssl3_read_bytes(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, &byte, 1 /* len */,
  289. 0 /* no peek */);
  290. if (ret <= 0) {
  291. return ret;
  292. }
  293. assert(ret == 1);
  294. if (ssl->s3->rrec.length != 0 || byte != SSL3_MT_CCS) {
  295. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
  296. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  297. return -1;
  298. }
  299. if (ssl->msg_callback != NULL) {
  300. ssl->msg_callback(0, ssl->version, SSL3_RT_CHANGE_CIPHER_SPEC, &byte, 1,
  301. ssl, ssl->msg_callback_arg);
  302. }
  303. return 1;
  304. }
  305. void ssl3_read_close_notify(SSL *ssl) {
  306. ssl3_read_bytes(ssl, 0, NULL, 0, 0);
  307. }
  308. static int ssl3_can_renegotiate(SSL *ssl) {
  309. switch (ssl->renegotiate_mode) {
  310. case ssl_renegotiate_never:
  311. return 0;
  312. case ssl_renegotiate_once:
  313. return ssl->s3->total_renegotiations == 0;
  314. case ssl_renegotiate_freely:
  315. return 1;
  316. case ssl_renegotiate_ignore:
  317. return 1;
  318. }
  319. assert(0);
  320. return 0;
  321. }
  322. /* Return up to 'len' payload bytes received in 'type' records.
  323. * 'type' is one of the following:
  324. *
  325. * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
  326. * - SSL3_RT_CHANGE_CIPHER_SPEC (when ssl3_read_change_cipher_spec calls us)
  327. * - SSL3_RT_APPLICATION_DATA (when ssl3_read_app_data calls us)
  328. * - 0 (during a shutdown, no data has to be returned)
  329. *
  330. * If we don't have stored data to work from, read a SSL/TLS record first
  331. * (possibly multiple records if we still don't have anything to return).
  332. *
  333. * This function must handle any surprises the peer may have for us, such as
  334. * Alert records (e.g. close_notify) or renegotiation requests. */
  335. int ssl3_read_bytes(SSL *ssl, int type, uint8_t *buf, int len, int peek) {
  336. int al, i, ret;
  337. unsigned int n;
  338. SSL3_RECORD *rr;
  339. void (*cb)(const SSL *ssl, int type, int value) = NULL;
  340. if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE &&
  341. type != SSL3_RT_CHANGE_CIPHER_SPEC) ||
  342. (peek && type != SSL3_RT_APPLICATION_DATA)) {
  343. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  344. return -1;
  345. }
  346. /* This may require multiple iterations. False Start will cause
  347. * |ssl->handshake_func| to signal success one step early, but the handshake
  348. * must be completely finished before other modes are accepted.
  349. *
  350. * TODO(davidben): Move this check up to a higher level. */
  351. while (!ssl->in_handshake && SSL_in_init(ssl)) {
  352. assert(type == SSL3_RT_APPLICATION_DATA);
  353. i = ssl->handshake_func(ssl);
  354. if (i < 0) {
  355. return i;
  356. }
  357. if (i == 0) {
  358. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  359. return -1;
  360. }
  361. }
  362. start:
  363. ssl->rwstate = SSL_NOTHING;
  364. /* ssl->s3->rrec.type - is the type of record
  365. * ssl->s3->rrec.data - data
  366. * ssl->s3->rrec.off - offset into 'data' for next read
  367. * ssl->s3->rrec.length - number of bytes. */
  368. rr = &ssl->s3->rrec;
  369. /* get new packet if necessary */
  370. if (rr->length == 0) {
  371. ret = ssl3_get_record(ssl);
  372. if (ret <= 0) {
  373. return ret;
  374. }
  375. }
  376. /* we now have a packet which can be read and processed */
  377. /* If the other end has shut down, throw anything we read away (even in
  378. * 'peek' mode) */
  379. if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
  380. rr->length = 0;
  381. ssl->rwstate = SSL_NOTHING;
  382. return 0;
  383. }
  384. if (type != 0 && type == rr->type) {
  385. ssl->s3->warning_alert_count = 0;
  386. /* Make sure that we are not getting application data when we are doing a
  387. * handshake for the first time. */
  388. if (SSL_in_init(ssl) && type == SSL3_RT_APPLICATION_DATA &&
  389. ssl->s3->aead_read_ctx == NULL) {
  390. /* TODO(davidben): Is this check redundant with the handshake_func
  391. * check? */
  392. al = SSL_AD_UNEXPECTED_MESSAGE;
  393. OPENSSL_PUT_ERROR(SSL, SSL_R_APP_DATA_IN_HANDSHAKE);
  394. goto f_err;
  395. }
  396. /* Discard empty records. */
  397. if (rr->length == 0) {
  398. goto start;
  399. }
  400. if (len <= 0) {
  401. return len;
  402. }
  403. if ((unsigned int)len > rr->length) {
  404. n = rr->length;
  405. } else {
  406. n = (unsigned int)len;
  407. }
  408. memcpy(buf, rr->data, n);
  409. if (!peek) {
  410. rr->length -= n;
  411. rr->data += n;
  412. if (rr->length == 0) {
  413. /* The record has been consumed, so we may now clear the buffer. */
  414. ssl_read_buffer_discard(ssl);
  415. }
  416. }
  417. return n;
  418. }
  419. /* Process unexpected records. */
  420. if (type == SSL3_RT_APPLICATION_DATA && rr->type == SSL3_RT_HANDSHAKE) {
  421. /* If peer renegotiations are disabled, all out-of-order handshake records
  422. * are fatal. Renegotiations as a server are never supported. */
  423. if (ssl->server || !ssl3_can_renegotiate(ssl)) {
  424. al = SSL_AD_NO_RENEGOTIATION;
  425. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  426. goto f_err;
  427. }
  428. /* This must be a HelloRequest, possibly fragmented over multiple records.
  429. * Consume data from the handshake protocol until it is complete. */
  430. static const uint8_t kHelloRequest[] = {SSL3_MT_HELLO_REQUEST, 0, 0, 0};
  431. while (ssl->s3->hello_request_len < sizeof(kHelloRequest)) {
  432. if (rr->length == 0) {
  433. /* Get a new record. */
  434. goto start;
  435. }
  436. if (rr->data[0] != kHelloRequest[ssl->s3->hello_request_len]) {
  437. al = SSL_AD_DECODE_ERROR;
  438. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
  439. goto f_err;
  440. }
  441. rr->data++;
  442. rr->length--;
  443. ssl->s3->hello_request_len++;
  444. }
  445. ssl->s3->hello_request_len = 0;
  446. if (ssl->msg_callback) {
  447. ssl->msg_callback(0, ssl->version, SSL3_RT_HANDSHAKE, kHelloRequest,
  448. sizeof(kHelloRequest), ssl, ssl->msg_callback_arg);
  449. }
  450. if (!SSL_is_init_finished(ssl) || !ssl->s3->initial_handshake_complete) {
  451. /* This cannot happen. If a handshake is in progress, |type| must be
  452. * |SSL3_RT_HANDSHAKE|. */
  453. assert(0);
  454. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  455. goto err;
  456. }
  457. if (ssl->renegotiate_mode == ssl_renegotiate_ignore) {
  458. goto start;
  459. }
  460. /* Renegotiation is only supported at quiescent points in the application
  461. * protocol, namely in HTTPS, just before reading the HTTP response. Require
  462. * the record-layer be idle and avoid complexities of sending a handshake
  463. * record while an application_data record is being written. */
  464. if (ssl_write_buffer_is_pending(ssl)) {
  465. al = SSL_AD_NO_RENEGOTIATION;
  466. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  467. goto f_err;
  468. }
  469. /* Begin a new handshake. */
  470. ssl->s3->total_renegotiations++;
  471. ssl->state = SSL_ST_CONNECT;
  472. i = ssl->handshake_func(ssl);
  473. if (i < 0) {
  474. return i;
  475. }
  476. if (i == 0) {
  477. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  478. return -1;
  479. }
  480. /* The handshake completed synchronously. Continue reading records. */
  481. goto start;
  482. }
  483. /* If an alert record, process one alert out of the record. Note that we allow
  484. * a single record to contain multiple alerts. */
  485. if (rr->type == SSL3_RT_ALERT) {
  486. /* Alerts may not be fragmented. */
  487. if (rr->length < 2) {
  488. al = SSL_AD_DECODE_ERROR;
  489. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
  490. goto f_err;
  491. }
  492. if (ssl->msg_callback) {
  493. ssl->msg_callback(0, ssl->version, SSL3_RT_ALERT, rr->data, 2, ssl,
  494. ssl->msg_callback_arg);
  495. }
  496. const uint8_t alert_level = rr->data[0];
  497. const uint8_t alert_descr = rr->data[1];
  498. rr->length -= 2;
  499. rr->data += 2;
  500. if (ssl->info_callback != NULL) {
  501. cb = ssl->info_callback;
  502. } else if (ssl->ctx->info_callback != NULL) {
  503. cb = ssl->ctx->info_callback;
  504. }
  505. if (cb != NULL) {
  506. uint16_t alert = (alert_level << 8) | alert_descr;
  507. cb(ssl, SSL_CB_READ_ALERT, alert);
  508. }
  509. if (alert_level == SSL3_AL_WARNING) {
  510. ssl->s3->warn_alert = alert_descr;
  511. if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
  512. ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
  513. return 0;
  514. }
  515. /* This is a warning but we receive it if we requested renegotiation and
  516. * the peer denied it. Terminate with a fatal alert because if
  517. * application tried to renegotiatie it presumably had a good reason and
  518. * expects it to succeed.
  519. *
  520. * In future we might have a renegotiation where we don't care if the
  521. * peer refused it where we carry on. */
  522. else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
  523. al = SSL_AD_HANDSHAKE_FAILURE;
  524. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  525. goto f_err;
  526. }
  527. ssl->s3->warning_alert_count++;
  528. if (ssl->s3->warning_alert_count > kMaxWarningAlerts) {
  529. al = SSL_AD_UNEXPECTED_MESSAGE;
  530. OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_WARNING_ALERTS);
  531. goto f_err;
  532. }
  533. } else if (alert_level == SSL3_AL_FATAL) {
  534. char tmp[16];
  535. ssl->rwstate = SSL_NOTHING;
  536. ssl->s3->fatal_alert = alert_descr;
  537. OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
  538. BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
  539. ERR_add_error_data(2, "SSL alert number ", tmp);
  540. ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
  541. SSL_CTX_remove_session(ssl->ctx, ssl->session);
  542. return 0;
  543. } else {
  544. al = SSL_AD_ILLEGAL_PARAMETER;
  545. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
  546. goto f_err;
  547. }
  548. goto start;
  549. }
  550. if (ssl->shutdown & SSL_SENT_SHUTDOWN) {
  551. /* close_notify has been sent, so discard all records other than alerts. */
  552. rr->length = 0;
  553. goto start;
  554. }
  555. al = SSL_AD_UNEXPECTED_MESSAGE;
  556. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
  557. f_err:
  558. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  559. err:
  560. return -1;
  561. }
  562. int ssl3_send_alert(SSL *ssl, int level, int desc) {
  563. /* If a fatal one, remove from cache */
  564. if (level == 2 && ssl->session != NULL) {
  565. SSL_CTX_remove_session(ssl->ctx, ssl->session);
  566. }
  567. ssl->s3->alert_dispatch = 1;
  568. ssl->s3->send_alert[0] = level;
  569. ssl->s3->send_alert[1] = desc;
  570. if (!ssl_write_buffer_is_pending(ssl)) {
  571. /* Nothing is being written out, so the alert may be dispatched
  572. * immediately. */
  573. return ssl->method->ssl_dispatch_alert(ssl);
  574. }
  575. /* else data is still being written out, we will get written some time in the
  576. * future */
  577. return -1;
  578. }
  579. int ssl3_dispatch_alert(SSL *ssl) {
  580. int i, j;
  581. void (*cb)(const SSL *ssl, int type, int value) = NULL;
  582. ssl->s3->alert_dispatch = 0;
  583. i = do_ssl3_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2);
  584. if (i <= 0) {
  585. ssl->s3->alert_dispatch = 1;
  586. } else {
  587. /* Alert sent to BIO. If it is important, flush it now. If the message
  588. * does not get sent due to non-blocking IO, we will not worry too much. */
  589. if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
  590. BIO_flush(ssl->wbio);
  591. }
  592. if (ssl->msg_callback) {
  593. ssl->msg_callback(1, ssl->version, SSL3_RT_ALERT, ssl->s3->send_alert, 2,
  594. ssl, ssl->msg_callback_arg);
  595. }
  596. if (ssl->info_callback != NULL) {
  597. cb = ssl->info_callback;
  598. } else if (ssl->ctx->info_callback != NULL) {
  599. cb = ssl->ctx->info_callback;
  600. }
  601. if (cb != NULL) {
  602. j = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
  603. cb(ssl, SSL_CB_WRITE_ALERT, j);
  604. }
  605. }
  606. return i;
  607. }