Home | History | Annotate | Download | only in ssl
      1 /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com)
      2  * All rights reserved.
      3  *
      4  * This package is an SSL implementation written
      5  * by Eric Young (eay (at) 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 (at) 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 (at) 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 (at) 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 #include <openssl/ssl.h>
     58 
     59 #include <assert.h>
     60 #include <string.h>
     61 
     62 #include <openssl/buf.h>
     63 
     64 #include "../crypto/internal.h"
     65 #include "internal.h"
     66 
     67 
     68 static int ssl3_supports_cipher(const SSL_CIPHER *cipher) { return 1; }
     69 
     70 static void ssl3_expect_flight(SSL *ssl) {}
     71 
     72 static void ssl3_received_flight(SSL *ssl) {}
     73 
     74 static int ssl3_set_read_state(SSL *ssl, SSL_AEAD_CTX *aead_ctx) {
     75   if (ssl->s3->rrec.length != 0) {
     76     /* There may not be unprocessed record data at a cipher change. */
     77     OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFERED_MESSAGES_ON_CIPHER_CHANGE);
     78     ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
     79     SSL_AEAD_CTX_free(aead_ctx);
     80     return 0;
     81   }
     82 
     83   OPENSSL_memset(ssl->s3->read_sequence, 0, sizeof(ssl->s3->read_sequence));
     84 
     85   SSL_AEAD_CTX_free(ssl->s3->aead_read_ctx);
     86   ssl->s3->aead_read_ctx = aead_ctx;
     87   return 1;
     88 }
     89 
     90 static int ssl3_set_write_state(SSL *ssl, SSL_AEAD_CTX *aead_ctx) {
     91   OPENSSL_memset(ssl->s3->write_sequence, 0, sizeof(ssl->s3->write_sequence));
     92 
     93   SSL_AEAD_CTX_free(ssl->s3->aead_write_ctx);
     94   ssl->s3->aead_write_ctx = aead_ctx;
     95   return 1;
     96 }
     97 
     98 static const SSL_PROTOCOL_METHOD kTLSProtocolMethod = {
     99     0 /* is_dtls */,
    100     ssl3_new,
    101     ssl3_free,
    102     ssl3_get_message,
    103     ssl3_get_current_message,
    104     ssl3_release_current_message,
    105     ssl3_read_app_data,
    106     ssl3_read_change_cipher_spec,
    107     ssl3_read_close_notify,
    108     ssl3_write_app_data,
    109     ssl3_dispatch_alert,
    110     ssl3_supports_cipher,
    111     ssl3_init_message,
    112     ssl3_finish_message,
    113     ssl3_add_message,
    114     ssl3_add_change_cipher_spec,
    115     ssl3_add_alert,
    116     ssl3_flush_flight,
    117     ssl3_expect_flight,
    118     ssl3_received_flight,
    119     ssl3_set_read_state,
    120     ssl3_set_write_state,
    121 };
    122 
    123 const SSL_METHOD *TLS_method(void) {
    124   static const SSL_METHOD kMethod = {
    125       0,
    126       &kTLSProtocolMethod,
    127       &ssl_crypto_x509_method,
    128   };
    129   return &kMethod;
    130 }
    131 
    132 const SSL_METHOD *SSLv23_method(void) {
    133   return TLS_method();
    134 }
    135 
    136 /* Legacy version-locked methods. */
    137 
    138 const SSL_METHOD *TLSv1_2_method(void) {
    139   static const SSL_METHOD kMethod = {
    140       TLS1_2_VERSION,
    141       &kTLSProtocolMethod,
    142       &ssl_crypto_x509_method,
    143   };
    144   return &kMethod;
    145 }
    146 
    147 const SSL_METHOD *TLSv1_1_method(void) {
    148   static const SSL_METHOD kMethod = {
    149       TLS1_1_VERSION,
    150       &kTLSProtocolMethod,
    151       &ssl_crypto_x509_method,
    152   };
    153   return &kMethod;
    154 }
    155 
    156 const SSL_METHOD *TLSv1_method(void) {
    157   static const SSL_METHOD kMethod = {
    158       TLS1_VERSION,
    159       &kTLSProtocolMethod,
    160       &ssl_crypto_x509_method,
    161   };
    162   return &kMethod;
    163 }
    164 
    165 const SSL_METHOD *SSLv3_method(void) {
    166   static const SSL_METHOD kMethod = {
    167       SSL3_VERSION,
    168       &kTLSProtocolMethod,
    169       &ssl_crypto_x509_method,
    170   };
    171   return &kMethod;
    172 }
    173 
    174 /* Legacy side-specific methods. */
    175 
    176 const SSL_METHOD *TLSv1_2_server_method(void) {
    177   return TLSv1_2_method();
    178 }
    179 
    180 const SSL_METHOD *TLSv1_1_server_method(void) {
    181   return TLSv1_1_method();
    182 }
    183 
    184 const SSL_METHOD *TLSv1_server_method(void) {
    185   return TLSv1_method();
    186 }
    187 
    188 const SSL_METHOD *SSLv3_server_method(void) {
    189   return SSLv3_method();
    190 }
    191 
    192 const SSL_METHOD *TLSv1_2_client_method(void) {
    193   return TLSv1_2_method();
    194 }
    195 
    196 const SSL_METHOD *TLSv1_1_client_method(void) {
    197   return TLSv1_1_method();
    198 }
    199 
    200 const SSL_METHOD *TLSv1_client_method(void) {
    201   return TLSv1_method();
    202 }
    203 
    204 const SSL_METHOD *SSLv3_client_method(void) {
    205   return SSLv3_method();
    206 }
    207 
    208 const SSL_METHOD *SSLv23_server_method(void) {
    209   return SSLv23_method();
    210 }
    211 
    212 const SSL_METHOD *SSLv23_client_method(void) {
    213   return SSLv23_method();
    214 }
    215 
    216 const SSL_METHOD *TLS_server_method(void) {
    217   return TLS_method();
    218 }
    219 
    220 const SSL_METHOD *TLS_client_method(void) {
    221   return TLS_method();
    222 }
    223 
    224 static int ssl_noop_x509_check_client_CA_names(
    225     STACK_OF(CRYPTO_BUFFER) *names) {
    226   return 1;
    227 }
    228 
    229 static void ssl_noop_x509_clear(CERT *cert) {}
    230 static void ssl_noop_x509_free(CERT *cert) {}
    231 static void ssl_noop_x509_dup(CERT *new_cert, const CERT *cert) {}
    232 static void ssl_noop_x509_flush_cached_leaf(CERT *cert) {}
    233 static void ssl_noop_x509_flush_cached_chain(CERT *cert) {}
    234 static int ssl_noop_x509_session_cache_objects(SSL_SESSION *sess) {
    235   return 1;
    236 }
    237 static int ssl_noop_x509_session_dup(SSL_SESSION *new_session,
    238                                        const SSL_SESSION *session) {
    239   return 1;
    240 }
    241 static void ssl_noop_x509_session_clear(SSL_SESSION *session) {}
    242 static int ssl_noop_x509_session_verify_cert_chain(SSL_SESSION *session,
    243                                                    SSL *ssl) {
    244   if (!ssl->ctx->i_promise_to_verify_certs_after_the_handshake) {
    245     ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_CA);
    246     OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
    247     return 0;
    248   }
    249 
    250   session->verify_result = X509_V_OK;
    251   return 1;
    252 }
    253 
    254 static void ssl_noop_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs) {}
    255 static int ssl_noop_x509_ssl_new(SSL *ctx) { return 1; }
    256 static void ssl_noop_x509_ssl_free(SSL *ctx) { }
    257 static void ssl_noop_x509_ssl_flush_cached_client_CA(SSL *ssl) {}
    258 static int ssl_noop_x509_ssl_auto_chain_if_needed(SSL *ssl) { return 1; }
    259 static int ssl_noop_x509_ssl_ctx_new(SSL_CTX *ctx) { return 1; }
    260 static void ssl_noop_x509_ssl_ctx_free(SSL_CTX *ctx) { }
    261 static void ssl_noop_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx) {}
    262 
    263 static const SSL_X509_METHOD ssl_noop_x509_method = {
    264   ssl_noop_x509_check_client_CA_names,
    265   ssl_noop_x509_clear,
    266   ssl_noop_x509_free,
    267   ssl_noop_x509_dup,
    268   ssl_noop_x509_flush_cached_chain,
    269   ssl_noop_x509_flush_cached_leaf,
    270   ssl_noop_x509_session_cache_objects,
    271   ssl_noop_x509_session_dup,
    272   ssl_noop_x509_session_clear,
    273   ssl_noop_x509_session_verify_cert_chain,
    274   ssl_noop_x509_hs_flush_cached_ca_names,
    275   ssl_noop_x509_ssl_new,
    276   ssl_noop_x509_ssl_free,
    277   ssl_noop_x509_ssl_flush_cached_client_CA,
    278   ssl_noop_x509_ssl_auto_chain_if_needed,
    279   ssl_noop_x509_ssl_ctx_new,
    280   ssl_noop_x509_ssl_ctx_free,
    281   ssl_noop_x509_ssl_ctx_flush_cached_client_CA,
    282 };
    283 
    284 const SSL_METHOD *TLS_with_buffers_method(void) {
    285   static const SSL_METHOD kMethod = {
    286       0,
    287       &kTLSProtocolMethod,
    288       &ssl_noop_x509_method,
    289   };
    290   return &kMethod;
    291 }
    292