Remove some unnecessary OPENSSL_FIPS references
[openssl.git] / ssl / ssl_lib.c
index 98f4018014df4414403fddc5891a0c8d683865a4..743204cd153b027a8b7979f25f30c8d0d1a53947 100644 (file)
@@ -263,7 +263,7 @@ int SSL_CTX_set_ssl_version(SSL_CTX *ctx,const SSL_METHOD *meth)
 
        sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list),
                &(ctx->cipher_list_by_id),
-               meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST, ctx->cert);
+               SSL_DEFAULT_CIPHER_LIST, ctx->cert);
        if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
                {
                SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
@@ -488,17 +488,6 @@ int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
        r.ssl_version = ssl->version;
        r.session_id_length = id_len;
        memcpy(r.session_id, id, id_len);
-       /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
-        * callback is calling us to check the uniqueness of a shorter ID, it
-        * must be compared as a padded-out ID because that is what it will be
-        * converted to when the callback has finished choosing it. */
-       if((r.ssl_version == SSL2_VERSION) &&
-                       (id_len < SSL2_SSL_SESSION_ID_LENGTH))
-               {
-               memset(r.session_id + id_len, 0,
-                       SSL2_SSL_SESSION_ID_LENGTH - id_len);
-               r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
-               }
 
        CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
        p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
@@ -1129,18 +1118,6 @@ long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
                l=s->max_cert_list;
                s->max_cert_list=larg;
                return(l);
-       case SSL_CTRL_SET_MTU:
-#ifndef OPENSSL_NO_DTLS1
-               if (larg < (long)dtls1_min_mtu())
-                       return 0;
-#endif
-
-               if (SSL_IS_DTLS(s))
-                       {
-                       s->d1->mtu = larg;
-                       return larg;
-                       }
-               return 0;
        case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
                if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
                        return 0;
@@ -1491,12 +1468,14 @@ int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p,
        int i,j=0;
        SSL_CIPHER *c;
        unsigned char *q;
-       int no_scsv = s->renegotiate;
+       int empty_reneg_info_scsv = !s->renegotiate;
        /* Set disabled masks for this session */
        ssl_set_client_disabled(s);
 
        if (sk == NULL) return(0);
        q=p;
+       if (put_cb == NULL)
+               put_cb = s->method->put_cipher_by_char;
 
        for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
                {
@@ -1507,29 +1486,40 @@ int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p,
 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
                if (c->id == SSL3_CK_SCSV)
                        {
-                       if (no_scsv)
+                       if (!empty_reneg_info_scsv)
                                continue;
                        else
-                               no_scsv = 1;
+                               empty_reneg_info_scsv = 0;
                        }
 #endif
-               j = put_cb ? put_cb(c,p) : ssl_put_cipher_by_char(s,c,p);
+               j = put_cb(c,p);
                p+=j;
                }
-       /* If p == q, no ciphers and caller indicates an error. Otherwise
-        * add SCSV if not renegotiating.
-        */
-       if (p != q && !no_scsv)
+       /* If p == q, no ciphers; caller indicates an error.
+        * Otherwise, add applicable SCSVs. */
+       if (p != q)
                {
-               static SSL_CIPHER scsv =
+               if (empty_reneg_info_scsv)
                        {
-                       0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
-                       };
-               j = put_cb ? put_cb(&scsv,p) : ssl_put_cipher_by_char(s,&scsv,p);
-               p+=j;
+                       static SSL_CIPHER scsv =
+                               {
+                               0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
+                               };
+                       j = put_cb(&scsv,p);
+                       p+=j;
 #ifdef OPENSSL_RI_DEBUG
-               fprintf(stderr, "SCSV sent by client\n");
+                       fprintf(stderr, "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
 #endif
+                       }
+               if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV)
+                       {
+                       static SSL_CIPHER scsv =
+                               {
+                               0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
+                               };
+                       j = put_cb(&scsv,p);
+                       p+=j;
+                       }
                }
 
        return(p-q);
@@ -1541,11 +1531,12 @@ STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num,
        const SSL_CIPHER *c;
        STACK_OF(SSL_CIPHER) *sk;
        int i,n;
+
        if (s->s3)
                s->s3->send_connection_binding = 0;
 
        n=ssl_put_cipher_by_char(s,NULL,NULL);
-       if ((num%n) != 0)
+       if (n == 0 || (num%n) != 0)
                {
                SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
                return(NULL);
@@ -1570,7 +1561,7 @@ STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num,
 
        for (i=0; i<num; i+=n)
                {
-               /* Check for SCSV */
+               /* Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
                if (s->s3 && (n != 3 || !p[0]) &&
                        (p[n-2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
                        (p[n-1] == (SSL3_CK_SCSV & 0xff)))
@@ -1590,6 +1581,24 @@ STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num,
                        continue;
                        }
 
+               /* Check for TLS_FALLBACK_SCSV */
+               if ((n != 3 || !p[0]) &&
+                       (p[n-2] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) &&
+                       (p[n-1] == (SSL3_CK_FALLBACK_SCSV & 0xff)))
+                       {
+                       /* The SCSV indicates that the client previously tried a higher version.
+                        * Fail if the current version is an unexpected downgrade. */
+                       if (!SSL_ctrl(s, SSL_CTRL_CHECK_PROTO_VERSION, 0, NULL))
+                               {
+                               SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_INAPPROPRIATE_FALLBACK);
+                               if (s->s3)
+                                       ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INAPPROPRIATE_FALLBACK);
+                               goto err;
+                               }
+                       p += n;
+                       continue;
+                       }
+
                c=ssl_get_cipher_by_char(s,p);
                p+=n;
                if (c != NULL)
@@ -1751,76 +1760,6 @@ void SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx, int (*cb) (SSL *s, unsigned
        }
 # endif
 
-static int cert_set_custom_cli_ext(CERT *cert, unsigned short ext_type,
-                              custom_cli_ext_first_cb_fn fn1, 
-                              custom_cli_ext_second_cb_fn fn2, void* arg)
-       {
-       size_t i;
-       custom_cli_ext_record* record;
-
-       /* Check for duplicates */
-       for (i=0; i < cert->custom_cli_ext_records_count; i++)
-               if (ext_type == cert->custom_cli_ext_records[i].ext_type)
-                       return 0;
-
-       cert->custom_cli_ext_records = OPENSSL_realloc(cert->custom_cli_ext_records,
-                                                     (cert->custom_cli_ext_records_count + 1) * 
-                                                     sizeof(custom_cli_ext_record));
-       if (!cert->custom_cli_ext_records) {
-               cert->custom_cli_ext_records_count = 0;
-               return 0;
-       }
-       cert->custom_cli_ext_records_count++;
-       record = &cert->custom_cli_ext_records[cert->custom_cli_ext_records_count - 1];
-       record->ext_type = ext_type;
-       record->fn1 = fn1;
-       record->fn2 = fn2;
-       record->arg = arg;
-       return 1;
-       }
-
-static int cert_set_custom_srv_ext(CERT *cert, unsigned short ext_type,
-                              custom_srv_ext_first_cb_fn fn1, 
-                              custom_srv_ext_second_cb_fn fn2, void* arg)
-       {
-       size_t i;
-       custom_srv_ext_record* record;
-
-       /* Check for duplicates */      
-       for (i=0; i < cert->custom_srv_ext_records_count; i++)
-               if (ext_type == cert->custom_srv_ext_records[i].ext_type)
-                       return 0;
-
-       cert->custom_srv_ext_records = OPENSSL_realloc(cert->custom_srv_ext_records,
-                                                     (cert->custom_srv_ext_records_count + 1) * 
-                                                     sizeof(custom_srv_ext_record));
-       if (!cert->custom_srv_ext_records) {
-               cert->custom_srv_ext_records_count = 0;
-               return 0;
-       }
-       cert->custom_srv_ext_records_count++;
-       record = &cert->custom_srv_ext_records[cert->custom_srv_ext_records_count - 1];
-       record->ext_type = ext_type;
-       record->fn1 = fn1;
-       record->fn2 = fn2;
-       record->arg = arg;
-       return 1;
-       }
-int SSL_CTX_set_custom_cli_ext(SSL_CTX *ctx, unsigned short ext_type,
-                              custom_cli_ext_first_cb_fn fn1, 
-                              custom_cli_ext_second_cb_fn fn2, void *arg)
-       {
-       return cert_set_custom_cli_ext(ctx->cert, ext_type, fn1, fn2,arg);
-       }
-
-int SSL_CTX_set_custom_srv_ext(SSL_CTX *ctx, unsigned short ext_type,
-                              custom_srv_ext_first_cb_fn fn1, 
-                              custom_srv_ext_second_cb_fn fn2, void *arg)
-       {
-       return cert_set_custom_srv_ext(ctx->cert, ext_type, fn1, fn2,arg);
-       }
-
 /* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
  * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
  * length-prefixed strings).
@@ -1950,13 +1889,11 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
                return(NULL);
                }
 
-#ifdef OPENSSL_FIPS
        if (FIPS_mode() && (meth->version < TLS1_VERSION))      
                {
                SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
                return NULL;
                }
-#endif
 
        if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
                {
@@ -1993,7 +1930,6 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
 /*     ret->cipher=NULL;*/
 /*     ret->s2->challenge=NULL;
        ret->master_key=NULL;
-       ret->key_arg=NULL;
        ret->s2->conn_id=NULL; */
 
        ret->info_callback=NULL;
@@ -2027,7 +1963,7 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
 
        ssl_create_cipher_list(ret->method,
                &ret->cipher_list,&ret->cipher_list_by_id,
-               meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST, ret->cert);
+               SSL_DEFAULT_CIPHER_LIST, ret->cert);
        if (ret->cipher_list == NULL
            || sk_SSL_CIPHER_num(ret->cipher_list) <= 0)
                {
@@ -2039,11 +1975,6 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
        if (!ret->param)
                goto err;
 
-       if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL)
-               {
-               SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES);
-               goto err2;
-               }
        if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL)
                {
                SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
@@ -2830,17 +2761,9 @@ int SSL_get_error(const SSL *s,int i)
 
        if (i == 0)
                {
-               if (s->version == SSL2_VERSION)
-                       {
-                       /* assume it is the socket being closed */
+               if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
+                       (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
                        return(SSL_ERROR_ZERO_RETURN);
-                       }
-               else
-                       {
-                       if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
-                               (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
-                               return(SSL_ERROR_ZERO_RETURN);
-                       }
                }
        return(SSL_ERROR_SYSCALL);
        }
@@ -2924,8 +2847,6 @@ const char *SSL_get_version(const SSL *s)
                return("TLSv1");
        else if (s->version == SSL3_VERSION)
                return("SSLv3");
-       else if (s->version == SSL2_VERSION)
-               return("SSLv2");
        else
                return("unknown");
        }
@@ -3247,15 +3168,28 @@ SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
 
 SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
        {
+       CERT *ocert = ssl->cert;
        if (ssl->ctx == ctx)
                return ssl->ctx;
 #ifndef OPENSSL_NO_TLSEXT
        if (ctx == NULL)
                ctx = ssl->initial_ctx;
 #endif
-       if (ssl->cert != NULL)
-               ssl_cert_free(ssl->cert);
        ssl->cert = ssl_cert_dup(ctx->cert);
+       if (ocert)
+               {
+               /* Preserve any already negotiated parameters */
+               if (ssl->server)
+                       {
+                       ssl->cert->peer_sigalgs = ocert->peer_sigalgs;
+                       ssl->cert->peer_sigalgslen = ocert->peer_sigalgslen;
+                       ocert->peer_sigalgs = NULL;
+                       ssl->cert->ciphers_raw = ocert->ciphers_raw;
+                       ssl->cert->ciphers_rawlen = ocert->ciphers_rawlen;
+                       ocert->ciphers_raw = NULL;
+                       }
+               ssl_cert_free(ocert);
+               }
        CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
        if (ssl->ctx != NULL)
                SSL_CTX_free(ssl->ctx); /* decrement reference count */
@@ -3645,7 +3579,6 @@ void *SSL_CTX_get0_security_ex_data(const SSL_CTX *ctx)
        return ctx->cert->sec_ex;
        }
 
-
 #if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16)
 #include "../crypto/bio/bss_file.c"
 #endif