Você não pode selecionar mais de 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.

tls_method.cc 8.8 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>
7 anos atrás
Don't use the buffer BIO in TLS. On the TLS side, we introduce a running buffer of ciphertext. Queuing up pending data consists of encrypting the record into the buffer. This effectively reimplements what the buffer BIO was doing previously, but this resizes to fit the whole flight. As part of this, rename all the functions to add to the pending flight to be more uniform. This CL proposes "add_foo" to add to the pending flight and "flush_flight" to drain it. We add an add_alert hook for alerts but, for now, only the SSL 3.0 warning alert (sent mid-handshake) uses this mechanism. Later work will push this down to the rest of the write path so closure alerts use it too, as in DTLS. The intended end state is that all the ssl_buffer.c and wpend_ret logic will only be used for application data and eventually optionally replaced by the in-place API, while all "incidental" data will be handled internally. For now, the two buffers are mutually exclusive. Moving closure alerts to "incidentals" will change this, but flushing application data early is tricky due to wpend_ret. (If we call ssl_write_buffer_flush, do_ssl3_write doesn't realize it still has a wpend_ret to replay.) That too is all left alone in this change. To keep the diff down, write_message is retained for now and will be removed from the state machines in a follow-up change. BUG=72 Change-Id: Ibce882f5f7196880648f25d5005322ca4055c71d Reviewed-on: https://boringssl-review.googlesource.com/13224 Reviewed-by: Adam Langley <agl@google.com>
7 anos atrás
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>
7 anos atrás
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>
7 anos atrás
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>
7 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. #include <openssl/ssl.h>
  57. #include <assert.h>
  58. #include <string.h>
  59. #include <openssl/buf.h>
  60. #include "../crypto/internal.h"
  61. #include "internal.h"
  62. namespace bssl {
  63. static void ssl3_on_handshake_complete(SSL *ssl) {
  64. // The handshake should have released its final message.
  65. assert(!ssl->s3->has_message);
  66. // During the handshake, |hs_buf| is retained. Release if it there is no
  67. // excess in it. There may be excess left if there server sent Finished and
  68. // HelloRequest in the same record.
  69. //
  70. // TODO(davidben): SChannel does not support this. Reject this case.
  71. if (ssl->s3->hs_buf && ssl->s3->hs_buf->length == 0) {
  72. ssl->s3->hs_buf.reset();
  73. }
  74. }
  75. static bool ssl3_set_read_state(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx) {
  76. // Cipher changes are forbidden if the current epoch has leftover data.
  77. if (tls_has_unprocessed_handshake_data(ssl)) {
  78. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFERED_MESSAGES_ON_CIPHER_CHANGE);
  79. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  80. return false;
  81. }
  82. OPENSSL_memset(ssl->s3->read_sequence, 0, sizeof(ssl->s3->read_sequence));
  83. ssl->s3->aead_read_ctx = std::move(aead_ctx);
  84. return true;
  85. }
  86. static bool ssl3_set_write_state(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx) {
  87. OPENSSL_memset(ssl->s3->write_sequence, 0, sizeof(ssl->s3->write_sequence));
  88. ssl->s3->aead_write_ctx = std::move(aead_ctx);
  89. return true;
  90. }
  91. static const SSL_PROTOCOL_METHOD kTLSProtocolMethod = {
  92. false /* is_dtls */,
  93. ssl3_new,
  94. ssl3_free,
  95. ssl3_get_message,
  96. ssl3_next_message,
  97. ssl3_open_handshake,
  98. ssl3_open_change_cipher_spec,
  99. ssl3_open_app_data,
  100. ssl3_write_app_data,
  101. ssl3_dispatch_alert,
  102. ssl3_init_message,
  103. ssl3_finish_message,
  104. ssl3_add_message,
  105. ssl3_add_change_cipher_spec,
  106. ssl3_add_alert,
  107. ssl3_flush_flight,
  108. ssl3_on_handshake_complete,
  109. ssl3_set_read_state,
  110. ssl3_set_write_state,
  111. };
  112. static int ssl_noop_x509_check_client_CA_names(
  113. STACK_OF(CRYPTO_BUFFER) *names) {
  114. return 1;
  115. }
  116. static void ssl_noop_x509_clear(CERT *cert) {}
  117. static void ssl_noop_x509_free(CERT *cert) {}
  118. static void ssl_noop_x509_dup(CERT *new_cert, const CERT *cert) {}
  119. static void ssl_noop_x509_flush_cached_leaf(CERT *cert) {}
  120. static void ssl_noop_x509_flush_cached_chain(CERT *cert) {}
  121. static int ssl_noop_x509_session_cache_objects(SSL_SESSION *sess) {
  122. return 1;
  123. }
  124. static int ssl_noop_x509_session_dup(SSL_SESSION *new_session,
  125. const SSL_SESSION *session) {
  126. return 1;
  127. }
  128. static void ssl_noop_x509_session_clear(SSL_SESSION *session) {}
  129. static int ssl_noop_x509_session_verify_cert_chain(SSL_SESSION *session,
  130. SSL *ssl,
  131. uint8_t *out_alert) {
  132. return 0;
  133. }
  134. static void ssl_noop_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs) {}
  135. static int ssl_noop_x509_ssl_new(SSL *ctx) { return 1; }
  136. static void ssl_noop_x509_ssl_free(SSL *ctx) { }
  137. static void ssl_noop_x509_ssl_flush_cached_client_CA(SSL *ssl) {}
  138. static int ssl_noop_x509_ssl_auto_chain_if_needed(SSL *ssl) { return 1; }
  139. static int ssl_noop_x509_ssl_ctx_new(SSL_CTX *ctx) { return 1; }
  140. static void ssl_noop_x509_ssl_ctx_free(SSL_CTX *ctx) { }
  141. static void ssl_noop_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx) {}
  142. const SSL_X509_METHOD ssl_noop_x509_method = {
  143. ssl_noop_x509_check_client_CA_names,
  144. ssl_noop_x509_clear,
  145. ssl_noop_x509_free,
  146. ssl_noop_x509_dup,
  147. ssl_noop_x509_flush_cached_chain,
  148. ssl_noop_x509_flush_cached_leaf,
  149. ssl_noop_x509_session_cache_objects,
  150. ssl_noop_x509_session_dup,
  151. ssl_noop_x509_session_clear,
  152. ssl_noop_x509_session_verify_cert_chain,
  153. ssl_noop_x509_hs_flush_cached_ca_names,
  154. ssl_noop_x509_ssl_new,
  155. ssl_noop_x509_ssl_free,
  156. ssl_noop_x509_ssl_flush_cached_client_CA,
  157. ssl_noop_x509_ssl_auto_chain_if_needed,
  158. ssl_noop_x509_ssl_ctx_new,
  159. ssl_noop_x509_ssl_ctx_free,
  160. ssl_noop_x509_ssl_ctx_flush_cached_client_CA,
  161. };
  162. } // namespace bssl
  163. using namespace bssl;
  164. const SSL_METHOD *TLS_method(void) {
  165. static const SSL_METHOD kMethod = {
  166. 0,
  167. &kTLSProtocolMethod,
  168. &ssl_crypto_x509_method,
  169. };
  170. return &kMethod;
  171. }
  172. const SSL_METHOD *SSLv23_method(void) {
  173. return TLS_method();
  174. }
  175. const SSL_METHOD *TLS_with_buffers_method(void) {
  176. static const SSL_METHOD kMethod = {
  177. 0,
  178. &kTLSProtocolMethod,
  179. &ssl_noop_x509_method,
  180. };
  181. return &kMethod;
  182. }
  183. // Legacy version-locked methods.
  184. const SSL_METHOD *TLSv1_2_method(void) {
  185. static const SSL_METHOD kMethod = {
  186. TLS1_2_VERSION,
  187. &kTLSProtocolMethod,
  188. &ssl_crypto_x509_method,
  189. };
  190. return &kMethod;
  191. }
  192. const SSL_METHOD *TLSv1_1_method(void) {
  193. static const SSL_METHOD kMethod = {
  194. TLS1_1_VERSION,
  195. &kTLSProtocolMethod,
  196. &ssl_crypto_x509_method,
  197. };
  198. return &kMethod;
  199. }
  200. const SSL_METHOD *TLSv1_method(void) {
  201. static const SSL_METHOD kMethod = {
  202. TLS1_VERSION,
  203. &kTLSProtocolMethod,
  204. &ssl_crypto_x509_method,
  205. };
  206. return &kMethod;
  207. }
  208. // Legacy side-specific methods.
  209. const SSL_METHOD *TLSv1_2_server_method(void) {
  210. return TLSv1_2_method();
  211. }
  212. const SSL_METHOD *TLSv1_1_server_method(void) {
  213. return TLSv1_1_method();
  214. }
  215. const SSL_METHOD *TLSv1_server_method(void) {
  216. return TLSv1_method();
  217. }
  218. const SSL_METHOD *TLSv1_2_client_method(void) {
  219. return TLSv1_2_method();
  220. }
  221. const SSL_METHOD *TLSv1_1_client_method(void) {
  222. return TLSv1_1_method();
  223. }
  224. const SSL_METHOD *TLSv1_client_method(void) {
  225. return TLSv1_method();
  226. }
  227. const SSL_METHOD *SSLv23_server_method(void) {
  228. return SSLv23_method();
  229. }
  230. const SSL_METHOD *SSLv23_client_method(void) {
  231. return SSLv23_method();
  232. }
  233. const SSL_METHOD *TLS_server_method(void) {
  234. return TLS_method();
  235. }
  236. const SSL_METHOD *TLS_client_method(void) {
  237. return TLS_method();
  238. }