Don't install e_os.h in include/openssl, use it only as a local
[openssl.git] / ssl / s2_srvr.c
index 5271546dfbd29f84d5436dc68c8d69fa6603b6d7..d283b2299161b58a71b5529a36eb704fc20c3be0 100644 (file)
@@ -56,6 +56,7 @@
  * [including the GNU Public Licence.]
  */
 
+#ifndef NO_RSA
 #include <stdio.h>
 #include <openssl/bio.h>
 #include <openssl/rand.h>
@@ -63,7 +64,6 @@
 #include "ssl_locl.h"
 #include <openssl/evp.h>
 
-#ifndef NOPROTO
 static SSL_METHOD *ssl2_get_server_method(int ver);
 static int get_client_master_key(SSL *s);
 static int get_client_hello(SSL *s);
@@ -74,18 +74,6 @@ static int server_finish(SSL *s);
 static int request_certificate(SSL *s);
 static int ssl_rsa_private_decrypt(CERT *c, int len, unsigned char *from,
        unsigned char *to,int padding);
-#else
-static SSL_METHOD *ssl2_get_server_method();
-static int get_client_master_key();
-static int get_client_hello();
-static int server_hello(); 
-static int get_client_finished();
-static int server_verify();
-static int server_finish();
-static int request_certificate();
-static int ssl_rsa_private_decrypt();
-#endif
-
 #define BREAK  break
 
 static SSL_METHOD *ssl2_get_server_method(int ver)
@@ -134,8 +122,7 @@ int ssl2_accept(SSL *s)
        if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
        s->in_handshake++;
 
-       if (((s->session == NULL) || (s->session->cert == NULL)) &&
-               (s->cert == NULL))
+       if (s->cert == NULL)
                {
                SSLerr(SSL_F_SSL2_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
                return(-1);
@@ -388,7 +375,7 @@ static int get_client_master_key(SSL *s)
        memcpy(s->session->key_arg,&(p[s->s2->tmp.clear+s->s2->tmp.enc]),
                (unsigned int)keya);
 
-       if (s->session->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL)
+       if (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL)
                {
                ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
                SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_NO_PRIVATEKEY);
@@ -612,27 +599,52 @@ static int server_hello(SSL *s)
                *(p++)=SSL2_MT_SERVER_HELLO;            /* type */
                hit=s->hit;
                *(p++)=(unsigned char)hit;
+#if 1
+               if (!hit)
+                       {
+                       if (s->session->sess_cert != NULL)
+                               /* This can't really happen because get_client_hello
+                                * has called ssl_get_new_session, which does not set
+                                * sess_cert. */
+                               ssl_sess_cert_free(s->session->sess_cert);
+                       s->session->sess_cert = ssl_sess_cert_new();
+                       if (s->session->sess_cert == NULL)
+                               {
+                               SSLerr(SSL_F_SERVER_HELLO, ERR_R_MALLOC_FAILURE);
+                               return(-1);
+                               }
+                       }
+               /* If 'hit' is set, then s->sess_cert may be non-NULL or NULL,
+                * depending on whether it survived in the internal cache
+                * or was retrieved from an external cache.
+                * If it is NULL, we cannot put any useful data in it anyway,
+                * so we don't touch it.
+                */
+
+#else /* That's what used to be done when cert_st and sess_cert_st were
+          * the same. */
                if (!hit)
                        {                       /* else add cert to session */
                        CRYPTO_add(&s->cert->references,1,CRYPTO_LOCK_SSL_CERT);
-                       if (s->session->cert != NULL)
-                               ssl_cert_free(s->session->cert);
-                       s->session->cert=s->cert;               
+                       if (s->session->sess_cert != NULL)
+                               ssl_cert_free(s->session->sess_cert);
+                       s->session->sess_cert=s->cert;          
                        }
                else    /* We have a session id-cache hit, if the
                         * session-id has no certificate listed against
                         * the 'cert' structure, grab the 'old' one
                         * listed against the SSL connection */
                        {
-                       if (s->session->cert == NULL)
+                       if (s->session->sess_cert == NULL)
                                {
                                CRYPTO_add(&s->cert->references,1,
                                        CRYPTO_LOCK_SSL_CERT);
-                               s->session->cert=s->cert;
+                               s->session->sess_cert=s->cert;
                                }
                        }
+#endif
 
-               if (s->session->cert == NULL)
+               if (s->cert == NULL)
                        {
                        ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE);
                        SSLerr(SSL_F_SERVER_HELLO,SSL_R_NO_CERTIFICATE_SPECIFIED);
@@ -885,7 +897,7 @@ static int request_certificate(SSL *s)
                        (unsigned int)s->s2->key_material_length);
                EVP_VerifyUpdate(&ctx,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
 
-               i=i2d_X509(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);
+               i=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);
                buf2=(unsigned char *)Malloc((unsigned int)i);
                if (buf2 == NULL)
                        {
@@ -893,7 +905,7 @@ static int request_certificate(SSL *s)
                        goto msg_end;
                        }
                p2=buf2;
-               i=i2d_X509(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&p2);
+               i=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&p2);
                EVP_VerifyUpdate(&ctx,buf2,(unsigned int)i);
                Free(buf2);
 
@@ -953,4 +965,4 @@ static int ssl_rsa_private_decrypt(CERT *c, int len, unsigned char *from,
                SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,ERR_R_RSA_LIB);
        return(i);
        }
-
+#endif