Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

s3_pkt.cc 15 KiB

Move libssl's internals into the bssl namespace. This is horrible, but everything else I tried was worse. The goal with this CL is to take the extern "C" out of ssl/internal.h and move most symbols to namespace bssl, so we can start using C++ helpers and destructors without worry. Complications: - Public API functions must be extern "C" and match their declaration in ssl.h, which is unnamespaced. C++ really does not want you to interleave namespaced and unnamespaced things. One can actually write a namespaced extern "C" function, but this means, from C++'s perspective, the function is namespaced. Trying to namespace the public header would worked but ended up too deep a rabbithole. - Our STACK_OF macros do not work right in namespaces. - The typedefs for our exposed but opaque types are visible in the header files and copied into consuming projects as forward declarations. We ultimately want to give SSL a destructor, but clobbering an unnamespaced ssl_st::~ssl_st seems bad manners. - MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL. This CL opts for: - ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This informs the public headers to create forward declarations which are compatible with our namespaces. - For now, C++-defined type FOO ends up at bssl::FOO with a typedef outside. Later I imagine we'll rename many of them. - Internal functions get namespace bssl, so we stop worrying about stomping the tls1_prf symbol. Exported C functions are stuck as they are. Rather than try anything weird, bite the bullet and reorder files which have a mix of public and private functions. I expect that over time, the public functions will become fairly small as we move logic to more idiomatic C++. Files without any public C functions can just be written normally. - To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle in advance of them being made idiomatic C++. Bug: 132 Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581 Reviewed-on: https://boringssl-review.googlesource.com/18124 Reviewed-by: David Benjamin <davidben@google.com>
há 7 anos
Factor out the buffering and low-level record code. This begins decoupling the transport from the SSL state machine. The buffering logic is hidden behind an opaque API. Fields like ssl->packet and ssl->packet_length are gone. ssl3_get_record and dtls1_get_record now call low-level tls_open_record and dtls_open_record functions that unpack a single record independent of who owns the buffer. Both may be called in-place. This removes ssl->rstate which was redundant with the buffer length. Future work will push the buffer up the stack until it is above the handshake. Then we can expose SSL_open and SSL_seal APIs which act like *_open_record but return a slightly larger enum due to other events being possible. Likewise the handshake state machine will be detached from its buffer. The existing SSL_read, SSL_write, etc., APIs will be implemented on top of SSL_open, etc., combined with ssl_read_buffer_* and ssl_write_buffer_*. (Which is why ssl_read_buffer_extend still tries to abstract between TLS's and DTLS's fairly different needs.) The new buffering logic does not support read-ahead (removed previously) since it lacks a memmove on ssl_read_buffer_discard for TLS, but this could be added if desired. The old buffering logic wasn't quite right anyway; it tried to avoid the memmove in some cases and could get stuck too far into the buffer and not accept records. (The only time the memmove is optional is in DTLS or if enough of the record header is available to know that the entire next record would fit in the buffer.) The new logic also now actually decrypts the ciphertext in-place again, rather than almost in-place when there's an explicit nonce/IV. (That accidentally switched in https://boringssl-review.googlesource.com/#/c/4792/; see 3d59e04bce96474099ba76786a2337e99ae14505.) BUG=468889 Change-Id: I403c1626253c46897f47c7ae93aeab1064b767b2 Reviewed-on: https://boringssl-review.googlesource.com/5715 Reviewed-by: Adam Langley <agl@google.com>
há 9 anos
Factor out the buffering and low-level record code. This begins decoupling the transport from the SSL state machine. The buffering logic is hidden behind an opaque API. Fields like ssl->packet and ssl->packet_length are gone. ssl3_get_record and dtls1_get_record now call low-level tls_open_record and dtls_open_record functions that unpack a single record independent of who owns the buffer. Both may be called in-place. This removes ssl->rstate which was redundant with the buffer length. Future work will push the buffer up the stack until it is above the handshake. Then we can expose SSL_open and SSL_seal APIs which act like *_open_record but return a slightly larger enum due to other events being possible. Likewise the handshake state machine will be detached from its buffer. The existing SSL_read, SSL_write, etc., APIs will be implemented on top of SSL_open, etc., combined with ssl_read_buffer_* and ssl_write_buffer_*. (Which is why ssl_read_buffer_extend still tries to abstract between TLS's and DTLS's fairly different needs.) The new buffering logic does not support read-ahead (removed previously) since it lacks a memmove on ssl_read_buffer_discard for TLS, but this could be added if desired. The old buffering logic wasn't quite right anyway; it tried to avoid the memmove in some cases and could get stuck too far into the buffer and not accept records. (The only time the memmove is optional is in DTLS or if enough of the record header is available to know that the entire next record would fit in the buffer.) The new logic also now actually decrypts the ciphertext in-place again, rather than almost in-place when there's an explicit nonce/IV. (That accidentally switched in https://boringssl-review.googlesource.com/#/c/4792/; see 3d59e04bce96474099ba76786a2337e99ae14505.) BUG=468889 Change-Id: I403c1626253c46897f47c7ae93aeab1064b767b2 Reviewed-on: https://boringssl-review.googlesource.com/5715 Reviewed-by: Adam Langley <agl@google.com>
há 9 anos
Move libssl's internals into the bssl namespace. This is horrible, but everything else I tried was worse. The goal with this CL is to take the extern "C" out of ssl/internal.h and move most symbols to namespace bssl, so we can start using C++ helpers and destructors without worry. Complications: - Public API functions must be extern "C" and match their declaration in ssl.h, which is unnamespaced. C++ really does not want you to interleave namespaced and unnamespaced things. One can actually write a namespaced extern "C" function, but this means, from C++'s perspective, the function is namespaced. Trying to namespace the public header would worked but ended up too deep a rabbithole. - Our STACK_OF macros do not work right in namespaces. - The typedefs for our exposed but opaque types are visible in the header files and copied into consuming projects as forward declarations. We ultimately want to give SSL a destructor, but clobbering an unnamespaced ssl_st::~ssl_st seems bad manners. - MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL. This CL opts for: - ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This informs the public headers to create forward declarations which are compatible with our namespaces. - For now, C++-defined type FOO ends up at bssl::FOO with a typedef outside. Later I imagine we'll rename many of them. - Internal functions get namespace bssl, so we stop worrying about stomping the tls1_prf symbol. Exported C functions are stuck as they are. Rather than try anything weird, bite the bullet and reorder files which have a mix of public and private functions. I expect that over time, the public functions will become fairly small as we move logic to more idiomatic C++. Files without any public C functions can just be written normally. - To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle in advance of them being made idiomatic C++. Bug: 132 Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581 Reviewed-on: https://boringssl-review.googlesource.com/18124 Reviewed-by: David Benjamin <davidben@google.com>
há 7 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 <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 "../crypto/internal.h"
  118. #include "internal.h"
  119. BSSL_NAMESPACE_BEGIN
  120. static int do_ssl3_write(SSL *ssl, int type, const uint8_t *in, unsigned len);
  121. int ssl3_write_app_data(SSL *ssl, bool *out_needs_handshake, const uint8_t *in,
  122. int len) {
  123. assert(ssl_can_write(ssl));
  124. assert(!ssl->s3->aead_write_ctx->is_null_cipher());
  125. *out_needs_handshake = false;
  126. if (ssl->s3->write_shutdown != ssl_shutdown_none) {
  127. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  128. return -1;
  129. }
  130. unsigned tot, n, nw;
  131. assert(ssl->s3->wnum <= INT_MAX);
  132. tot = ssl->s3->wnum;
  133. ssl->s3->wnum = 0;
  134. // Ensure that if we end up with a smaller value of data to write out than
  135. // the the original len from a write which didn't complete for non-blocking
  136. // I/O and also somehow ended up avoiding the check for this in
  137. // ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
  138. // end up with (len-tot) as a large number that will then promptly send
  139. // beyond the end of the users buffer ... so we trap and report the error in
  140. // a way the user will notice.
  141. if (len < 0 || (size_t)len < tot) {
  142. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH);
  143. return -1;
  144. }
  145. const int is_early_data_write =
  146. !ssl->server && SSL_in_early_data(ssl) && ssl->s3->hs->can_early_write;
  147. n = len - tot;
  148. for (;;) {
  149. // max contains the maximum number of bytes that we can put into a record.
  150. unsigned max = ssl->max_send_fragment;
  151. if (is_early_data_write &&
  152. max > ssl->session->ticket_max_early_data -
  153. ssl->s3->hs->early_data_written) {
  154. max =
  155. ssl->session->ticket_max_early_data - ssl->s3->hs->early_data_written;
  156. if (max == 0) {
  157. ssl->s3->wnum = tot;
  158. ssl->s3->hs->can_early_write = false;
  159. *out_needs_handshake = true;
  160. return -1;
  161. }
  162. }
  163. if (n > max) {
  164. nw = max;
  165. } else {
  166. nw = n;
  167. }
  168. int ret = do_ssl3_write(ssl, SSL3_RT_APPLICATION_DATA, &in[tot], nw);
  169. if (ret <= 0) {
  170. ssl->s3->wnum = tot;
  171. return ret;
  172. }
  173. if (is_early_data_write) {
  174. ssl->s3->hs->early_data_written += ret;
  175. }
  176. if (ret == (int)n || (ssl->mode & SSL_MODE_ENABLE_PARTIAL_WRITE)) {
  177. return tot + ret;
  178. }
  179. n -= ret;
  180. tot += ret;
  181. }
  182. }
  183. static int ssl3_write_pending(SSL *ssl, int type, const uint8_t *in,
  184. unsigned int len) {
  185. if (ssl->s3->wpend_tot > (int)len ||
  186. (!(ssl->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) &&
  187. ssl->s3->wpend_buf != in) ||
  188. ssl->s3->wpend_type != type) {
  189. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_WRITE_RETRY);
  190. return -1;
  191. }
  192. int ret = ssl_write_buffer_flush(ssl);
  193. if (ret <= 0) {
  194. return ret;
  195. }
  196. ssl->s3->wpend_pending = false;
  197. return ssl->s3->wpend_ret;
  198. }
  199. // do_ssl3_write writes an SSL record of the given type.
  200. static int do_ssl3_write(SSL *ssl, int type, const uint8_t *in, unsigned len) {
  201. // If there is still data from the previous record, flush it.
  202. if (ssl->s3->wpend_pending) {
  203. return ssl3_write_pending(ssl, type, in, len);
  204. }
  205. SSLBuffer *buf = &ssl->s3->write_buffer;
  206. if (len > SSL3_RT_MAX_PLAIN_LENGTH || buf->size() > 0) {
  207. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  208. return -1;
  209. }
  210. if (len == 0) {
  211. return 0;
  212. }
  213. if (!tls_flush_pending_hs_data(ssl)) {
  214. return -1;
  215. }
  216. size_t flight_len = 0;
  217. if (ssl->s3->pending_flight != nullptr) {
  218. flight_len =
  219. ssl->s3->pending_flight->length - ssl->s3->pending_flight_offset;
  220. }
  221. size_t max_out = len + SSL_max_seal_overhead(ssl);
  222. if (max_out < len || max_out + flight_len < max_out) {
  223. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  224. return -1;
  225. }
  226. max_out += flight_len;
  227. if (!buf->EnsureCap(flight_len + ssl_seal_align_prefix_len(ssl), max_out)) {
  228. return -1;
  229. }
  230. // Add any unflushed handshake data as a prefix. This may be a KeyUpdate
  231. // acknowledgment or 0-RTT key change messages. |pending_flight| must be clear
  232. // when data is added to |write_buffer| or it will be written in the wrong
  233. // order.
  234. if (ssl->s3->pending_flight != nullptr) {
  235. OPENSSL_memcpy(
  236. buf->remaining().data(),
  237. ssl->s3->pending_flight->data + ssl->s3->pending_flight_offset,
  238. flight_len);
  239. ssl->s3->pending_flight.reset();
  240. ssl->s3->pending_flight_offset = 0;
  241. buf->DidWrite(flight_len);
  242. }
  243. size_t ciphertext_len;
  244. if (!tls_seal_record(ssl, buf->remaining().data(), &ciphertext_len,
  245. buf->remaining().size(), type, in, len)) {
  246. return -1;
  247. }
  248. buf->DidWrite(ciphertext_len);
  249. // Now that we've made progress on the connection, uncork KeyUpdate
  250. // acknowledgments.
  251. ssl->s3->key_update_pending = false;
  252. // Memorize arguments so that ssl3_write_pending can detect bad write retries
  253. // later.
  254. ssl->s3->wpend_tot = len;
  255. ssl->s3->wpend_buf = in;
  256. ssl->s3->wpend_type = type;
  257. ssl->s3->wpend_ret = len;
  258. ssl->s3->wpend_pending = true;
  259. // We now just need to write the buffer.
  260. return ssl3_write_pending(ssl, type, in, len);
  261. }
  262. ssl_open_record_t ssl3_open_app_data(SSL *ssl, Span<uint8_t> *out,
  263. size_t *out_consumed, uint8_t *out_alert,
  264. Span<uint8_t> in) {
  265. assert(ssl_can_read(ssl));
  266. assert(!ssl->s3->aead_read_ctx->is_null_cipher());
  267. uint8_t type;
  268. Span<uint8_t> body;
  269. auto ret = tls_open_record(ssl, &type, &body, out_consumed, out_alert, in);
  270. if (ret != ssl_open_record_success) {
  271. return ret;
  272. }
  273. const bool is_early_data_read = ssl->server && SSL_in_early_data(ssl);
  274. if (type == SSL3_RT_HANDSHAKE) {
  275. // Post-handshake data prior to TLS 1.3 is always renegotiation, which we
  276. // never accept as a server. Otherwise |ssl3_get_message| will send
  277. // |SSL_R_EXCESSIVE_MESSAGE_SIZE|.
  278. if (ssl->server && ssl_protocol_version(ssl) < TLS1_3_VERSION) {
  279. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  280. *out_alert = SSL_AD_NO_RENEGOTIATION;
  281. return ssl_open_record_error;
  282. }
  283. if (!tls_append_handshake_data(ssl, body)) {
  284. *out_alert = SSL_AD_INTERNAL_ERROR;
  285. return ssl_open_record_error;
  286. }
  287. return ssl_open_record_discard;
  288. }
  289. if (type != SSL3_RT_APPLICATION_DATA) {
  290. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
  291. *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
  292. return ssl_open_record_error;
  293. }
  294. if (is_early_data_read) {
  295. if (body.size() > kMaxEarlyDataAccepted - ssl->s3->hs->early_data_read) {
  296. OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MUCH_READ_EARLY_DATA);
  297. *out_alert = SSL3_AD_UNEXPECTED_MESSAGE;
  298. return ssl_open_record_error;
  299. }
  300. ssl->s3->hs->early_data_read += body.size();
  301. }
  302. if (body.empty()) {
  303. return ssl_open_record_discard;
  304. }
  305. *out = body;
  306. return ssl_open_record_success;
  307. }
  308. ssl_open_record_t ssl3_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
  309. uint8_t *out_alert,
  310. Span<uint8_t> in) {
  311. uint8_t type;
  312. Span<uint8_t> body;
  313. auto ret = tls_open_record(ssl, &type, &body, out_consumed, out_alert, in);
  314. if (ret != ssl_open_record_success) {
  315. return ret;
  316. }
  317. if (type != SSL3_RT_CHANGE_CIPHER_SPEC) {
  318. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
  319. *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
  320. return ssl_open_record_error;
  321. }
  322. if (body.size() != 1 || body[0] != SSL3_MT_CCS) {
  323. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
  324. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  325. return ssl_open_record_error;
  326. }
  327. ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_CHANGE_CIPHER_SPEC, body);
  328. return ssl_open_record_success;
  329. }
  330. int ssl_send_alert(SSL *ssl, int level, int desc) {
  331. // It is illegal to send an alert when we've already sent a closing one.
  332. if (ssl->s3->write_shutdown != ssl_shutdown_none) {
  333. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  334. return -1;
  335. }
  336. if (level == SSL3_AL_WARNING && desc == SSL_AD_CLOSE_NOTIFY) {
  337. ssl->s3->write_shutdown = ssl_shutdown_close_notify;
  338. } else {
  339. assert(level == SSL3_AL_FATAL);
  340. assert(desc != SSL_AD_CLOSE_NOTIFY);
  341. ssl->s3->write_shutdown = ssl_shutdown_error;
  342. }
  343. ssl->s3->alert_dispatch = 1;
  344. ssl->s3->send_alert[0] = level;
  345. ssl->s3->send_alert[1] = desc;
  346. if (ssl->s3->write_buffer.empty()) {
  347. // Nothing is being written out, so the alert may be dispatched
  348. // immediately.
  349. return ssl->method->dispatch_alert(ssl);
  350. }
  351. // The alert will be dispatched later.
  352. return -1;
  353. }
  354. int ssl3_dispatch_alert(SSL *ssl) {
  355. if (ssl->ctx->quic_method) {
  356. if (!ssl->ctx->quic_method->send_alert(ssl, ssl->s3->write_level,
  357. ssl->s3->send_alert[1])) {
  358. OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR);
  359. return 0;
  360. }
  361. } else {
  362. int ret = do_ssl3_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2);
  363. if (ret <= 0) {
  364. return ret;
  365. }
  366. }
  367. ssl->s3->alert_dispatch = 0;
  368. // If the alert is fatal, flush the BIO now.
  369. if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
  370. BIO_flush(ssl->wbio.get());
  371. }
  372. ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_ALERT, ssl->s3->send_alert);
  373. int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
  374. ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, alert);
  375. return 1;
  376. }
  377. BSSL_NAMESPACE_END