Only inherit the session ID context in SSL_set_SSL_CTX if the existing
[openssl.git] / ssl / ssl_lib.c
index c336a85aa31df0dfb8195cbf38be2aeb96e2235f..f9f91e666c09acc30c69ee774b252a9eda07ad59 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);
@@ -417,13 +417,7 @@ SSL *SSL_new(SSL_CTX *ctx)
        return(s);
 err:
        if (s != NULL)
-               {
-               if (s->cert != NULL)
-                       ssl_cert_free(s->cert);
-               if (s->ctx != NULL)
-                       SSL_CTX_free(s->ctx); /* decrement reference count */
-               OPENSSL_free(s);
-               }
+               SSL_free(s);
        SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);
        return(NULL);
        }
@@ -488,17 +482,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);
@@ -648,8 +631,10 @@ void SSL_free(SSL *s)
                OPENSSL_free(s->next_proto_negotiated);
 #endif
 
+#ifndef OPENSSL_NO_SRTP
         if (s->srtp_profiles)
             sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
+#endif
 
        OPENSSL_free(s);
        }
@@ -1129,18 +1114,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;
@@ -1618,6 +1591,7 @@ STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num,
                                        ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INAPPROPRIATE_FALLBACK);
                                goto err;
                                }
+                       p += n;
                        continue;
                        }
 
@@ -1911,13 +1885,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)
                {
@@ -1952,9 +1924,9 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
        ret->quiet_shutdown=0;
 
 /*     ret->cipher=NULL;*/
-/*     ret->s2->challenge=NULL;
+/*-
+       ret->s2->challenge=NULL;
        ret->master_key=NULL;
-       ret->key_arg=NULL;
        ret->s2->conn_id=NULL; */
 
        ret->info_callback=NULL;
@@ -1988,7 +1960,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)
                {
@@ -2000,11 +1972,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);
@@ -2181,8 +2148,10 @@ void SSL_CTX_free(SSL_CTX *a)
        a->comp_methods = NULL;
 #endif
 
+#ifndef OPENSSL_NO_SRTP
         if (a->srtp_profiles)
                 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
+#endif
 
 #ifndef OPENSSL_NO_PSK
        if (a->psk_identity_hint)
@@ -2319,7 +2288,7 @@ void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
        
 
 #ifdef CIPHER_DEBUG
-       printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
+       fprintf(stderr,"rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
                rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp,
                rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
 #endif
@@ -2397,8 +2366,10 @@ void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
                x = cpk->x509;
                /* This call populates extension flags (ex_flags) */
                X509_check_purpose(x, -1, 0);
+#ifndef OPENSSL_NO_ECDH
                ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
                    (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1;
+#endif
                ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
                    (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
                if (!(cpk->valid_flags & CERT_PKEY_SIGN))
@@ -2791,17 +2762,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);
        }
@@ -2885,8 +2848,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");
        }
@@ -3208,19 +3169,54 @@ 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);
+               }
+
+       /*
+        * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH),
+        * so setter APIs must prevent invalid lengths from entering the system.
+        */
+       OPENSSL_assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
+
+       /*
+        * If the session ID context matches that of the parent SSL_CTX,
+        * inherit it from the new SSL_CTX as well. If however the context does
+        * not match (i.e., it was set per-ssl with SSL_set_session_id_context),
+        * leave it unchanged.
+        */
+       if ((ssl->ctx != NULL) &&
+               (ssl->sid_ctx_length == ssl->ctx->sid_ctx_length) &&
+               (memcmp(ssl->sid_ctx, ssl->ctx->sid_ctx, ssl->sid_ctx_length) == 0))
+               {
+               ssl->sid_ctx_length = ctx->sid_ctx_length;
+               memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));
+               }
+
        CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
        if (ssl->ctx != NULL)
                SSL_CTX_free(ssl->ctx); /* decrement reference count */
        ssl->ctx = ctx;
+
        return(ssl->ctx);
        }