No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

302 líneas
9.3 KiB

  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 int ssl3_supports_cipher(const SSL_CIPHER *cipher) { return 1; }
  64. static void ssl3_on_handshake_complete(SSL *ssl) {
  65. // The handshake should have released its final message.
  66. assert(!ssl->s3->has_message);
  67. // During the handshake, |init_buf| is retained. Release if it there is no
  68. // excess in it.
  69. //
  70. // TODO(davidben): The second check is always true but will not be once we
  71. // switch to copying the entire handshake record. Replace this comment with an
  72. // explanation when that happens and a TODO to reject it.
  73. if (ssl->init_buf != NULL && ssl->init_buf->length == 0) {
  74. BUF_MEM_free(ssl->init_buf);
  75. ssl->init_buf = NULL;
  76. }
  77. }
  78. static int ssl3_set_read_state(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx) {
  79. if (ssl->s3->rrec.length != 0) {
  80. // There may not be unprocessed record data at a cipher change.
  81. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFERED_MESSAGES_ON_CIPHER_CHANGE);
  82. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  83. return 0;
  84. }
  85. OPENSSL_memset(ssl->s3->read_sequence, 0, sizeof(ssl->s3->read_sequence));
  86. Delete(ssl->s3->aead_read_ctx);
  87. ssl->s3->aead_read_ctx = aead_ctx.release();
  88. return 1;
  89. }
  90. static int ssl3_set_write_state(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx) {
  91. OPENSSL_memset(ssl->s3->write_sequence, 0, sizeof(ssl->s3->write_sequence));
  92. Delete(ssl->s3->aead_write_ctx);
  93. ssl->s3->aead_write_ctx = aead_ctx.release();
  94. return 1;
  95. }
  96. static const SSL_PROTOCOL_METHOD kTLSProtocolMethod = {
  97. 0 /* is_dtls */,
  98. ssl3_new,
  99. ssl3_free,
  100. ssl3_get_message,
  101. ssl3_read_message,
  102. ssl3_next_message,
  103. ssl3_read_app_data,
  104. ssl3_read_change_cipher_spec,
  105. ssl3_read_close_notify,
  106. ssl3_write_app_data,
  107. ssl3_dispatch_alert,
  108. ssl3_supports_cipher,
  109. ssl3_init_message,
  110. ssl3_finish_message,
  111. ssl3_add_message,
  112. ssl3_add_change_cipher_spec,
  113. ssl3_add_alert,
  114. ssl3_flush_flight,
  115. ssl3_on_handshake_complete,
  116. ssl3_set_read_state,
  117. ssl3_set_write_state,
  118. };
  119. static int ssl_noop_x509_check_client_CA_names(
  120. STACK_OF(CRYPTO_BUFFER) *names) {
  121. return 1;
  122. }
  123. static void ssl_noop_x509_clear(CERT *cert) {}
  124. static void ssl_noop_x509_free(CERT *cert) {}
  125. static void ssl_noop_x509_dup(CERT *new_cert, const CERT *cert) {}
  126. static void ssl_noop_x509_flush_cached_leaf(CERT *cert) {}
  127. static void ssl_noop_x509_flush_cached_chain(CERT *cert) {}
  128. static int ssl_noop_x509_session_cache_objects(SSL_SESSION *sess) {
  129. return 1;
  130. }
  131. static int ssl_noop_x509_session_dup(SSL_SESSION *new_session,
  132. const SSL_SESSION *session) {
  133. return 1;
  134. }
  135. static void ssl_noop_x509_session_clear(SSL_SESSION *session) {}
  136. static int ssl_noop_x509_session_verify_cert_chain(SSL_SESSION *session,
  137. SSL *ssl,
  138. uint8_t *out_alert) {
  139. return 0;
  140. }
  141. static void ssl_noop_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs) {}
  142. static int ssl_noop_x509_ssl_new(SSL *ctx) { return 1; }
  143. static void ssl_noop_x509_ssl_free(SSL *ctx) { }
  144. static void ssl_noop_x509_ssl_flush_cached_client_CA(SSL *ssl) {}
  145. static int ssl_noop_x509_ssl_auto_chain_if_needed(SSL *ssl) { return 1; }
  146. static int ssl_noop_x509_ssl_ctx_new(SSL_CTX *ctx) { return 1; }
  147. static void ssl_noop_x509_ssl_ctx_free(SSL_CTX *ctx) { }
  148. static void ssl_noop_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx) {}
  149. const SSL_X509_METHOD ssl_noop_x509_method = {
  150. ssl_noop_x509_check_client_CA_names,
  151. ssl_noop_x509_clear,
  152. ssl_noop_x509_free,
  153. ssl_noop_x509_dup,
  154. ssl_noop_x509_flush_cached_chain,
  155. ssl_noop_x509_flush_cached_leaf,
  156. ssl_noop_x509_session_cache_objects,
  157. ssl_noop_x509_session_dup,
  158. ssl_noop_x509_session_clear,
  159. ssl_noop_x509_session_verify_cert_chain,
  160. ssl_noop_x509_hs_flush_cached_ca_names,
  161. ssl_noop_x509_ssl_new,
  162. ssl_noop_x509_ssl_free,
  163. ssl_noop_x509_ssl_flush_cached_client_CA,
  164. ssl_noop_x509_ssl_auto_chain_if_needed,
  165. ssl_noop_x509_ssl_ctx_new,
  166. ssl_noop_x509_ssl_ctx_free,
  167. ssl_noop_x509_ssl_ctx_flush_cached_client_CA,
  168. };
  169. } // namespace bssl
  170. using namespace bssl;
  171. const SSL_METHOD *TLS_method(void) {
  172. static const SSL_METHOD kMethod = {
  173. 0,
  174. &kTLSProtocolMethod,
  175. &ssl_crypto_x509_method,
  176. };
  177. return &kMethod;
  178. }
  179. const SSL_METHOD *SSLv23_method(void) {
  180. return TLS_method();
  181. }
  182. const SSL_METHOD *TLS_with_buffers_method(void) {
  183. static const SSL_METHOD kMethod = {
  184. 0,
  185. &kTLSProtocolMethod,
  186. &ssl_noop_x509_method,
  187. };
  188. return &kMethod;
  189. }
  190. // Legacy version-locked methods.
  191. const SSL_METHOD *TLSv1_2_method(void) {
  192. static const SSL_METHOD kMethod = {
  193. TLS1_2_VERSION,
  194. &kTLSProtocolMethod,
  195. &ssl_crypto_x509_method,
  196. };
  197. return &kMethod;
  198. }
  199. const SSL_METHOD *TLSv1_1_method(void) {
  200. static const SSL_METHOD kMethod = {
  201. TLS1_1_VERSION,
  202. &kTLSProtocolMethod,
  203. &ssl_crypto_x509_method,
  204. };
  205. return &kMethod;
  206. }
  207. const SSL_METHOD *TLSv1_method(void) {
  208. static const SSL_METHOD kMethod = {
  209. TLS1_VERSION,
  210. &kTLSProtocolMethod,
  211. &ssl_crypto_x509_method,
  212. };
  213. return &kMethod;
  214. }
  215. const SSL_METHOD *SSLv3_method(void) {
  216. static const SSL_METHOD kMethod = {
  217. SSL3_VERSION,
  218. &kTLSProtocolMethod,
  219. &ssl_crypto_x509_method,
  220. };
  221. return &kMethod;
  222. }
  223. // Legacy side-specific methods.
  224. const SSL_METHOD *TLSv1_2_server_method(void) {
  225. return TLSv1_2_method();
  226. }
  227. const SSL_METHOD *TLSv1_1_server_method(void) {
  228. return TLSv1_1_method();
  229. }
  230. const SSL_METHOD *TLSv1_server_method(void) {
  231. return TLSv1_method();
  232. }
  233. const SSL_METHOD *SSLv3_server_method(void) {
  234. return SSLv3_method();
  235. }
  236. const SSL_METHOD *TLSv1_2_client_method(void) {
  237. return TLSv1_2_method();
  238. }
  239. const SSL_METHOD *TLSv1_1_client_method(void) {
  240. return TLSv1_1_method();
  241. }
  242. const SSL_METHOD *TLSv1_client_method(void) {
  243. return TLSv1_method();
  244. }
  245. const SSL_METHOD *SSLv3_client_method(void) {
  246. return SSLv3_method();
  247. }
  248. const SSL_METHOD *SSLv23_server_method(void) {
  249. return SSLv23_method();
  250. }
  251. const SSL_METHOD *SSLv23_client_method(void) {
  252. return SSLv23_method();
  253. }
  254. const SSL_METHOD *TLS_server_method(void) {
  255. return TLS_method();
  256. }
  257. const SSL_METHOD *TLS_client_method(void) {
  258. return TLS_method();
  259. }