replace macros with functions
[openssl.git] / ssl / ssl_lib.c
index 841d75d5855e83d61d2458ef44d9a91255f54d6a..1cc3ac9cb6c4e7d17381c31d943cfb23c4c53572 100644 (file)
  * ECC cipher suite support in OpenSSL originally developed by 
  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  */
+/* ====================================================================
+ * Copyright 2005 Nokia. All rights reserved.
+ *
+ * The portions of the attached software ("Contribution") is developed by
+ * Nokia Corporation and is licensed pursuant to the OpenSSL open source
+ * license.
+ *
+ * The Contribution, originally written by Mika Kousa and Pasi Eronen of
+ * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
+ * support (see RFC 4279) to OpenSSL.
+ *
+ * No patent licenses or other rights except those expressly stated in
+ * the OpenSSL open source license shall be deemed granted or received
+ * expressly, by implication, estoppel, or otherwise.
+ *
+ * No assurances are provided by Nokia that the Contribution does not
+ * infringe the patent or other intellectual property rights of any third
+ * party or that the license provides you with all the necessary rights
+ * to make use of the Contribution.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
+ * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
+ * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
+ * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
+ * OTHERWISE.
+ */
 
 #ifdef REF_CHECK
 #  include <assert.h>
@@ -307,6 +333,10 @@ SSL *SSL_new(SSL_CTX *ctx)
 
        CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
        s->ctx=ctx;
+#ifndef OPENSSL_NO_TLSEXT
+       CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
+       s->initial_ctx=ctx;
+#endif
 
        s->verify_result=X509_V_OK;
 
@@ -322,6 +352,11 @@ SSL *SSL_new(SSL_CTX *ctx)
 
        CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
 
+#ifndef OPENSSL_NO_PSK
+       s->psk_client_callback=ctx->psk_client_callback;
+       s->psk_server_callback=ctx->psk_server_callback;
+#endif
+
        return(s);
 err:
        if (s != NULL)
@@ -493,6 +528,13 @@ void SSL_free(SSL *s)
        /* Free up if allocated */
 
        if (s->ctx) SSL_CTX_free(s->ctx);
+#ifndef OPENSSL_NO_TLSEXT
+       if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
+#ifndef OPENSSL_NO_EC
+       if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
+       if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
+#endif /* OPENSSL_NO_EC */
+#endif
 
        if (s->client_CA != NULL)
                sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
@@ -1230,7 +1272,7 @@ char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len)
                c=sk_SSL_CIPHER_value(sk,i);
                for (cp=c->name; *cp; )
                        {
-                       if (len-- == 0)
+                       if (len-- <= 0)
                                {
                                *p='\0';
                                return(buf);
@@ -1264,7 +1306,11 @@ int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p,
                 if ((c->algorithms & SSL_KRB5) && nokrb5)
                     continue;
 #endif /* OPENSSL_NO_KRB5 */                    
-
+#ifndef OPENSSL_NO_PSK
+               /* with PSK there must be client callback set */
+               if ((c->algorithms & SSL_PSK) && s->psk_client_callback == NULL)
+                       continue;
+#endif /* OPENSSL_NO_PSK */
                j = put_cb ? put_cb(c,p) : ssl_put_cipher_by_char(s,c,p);
                p+=j;
                }
@@ -1325,15 +1371,15 @@ const char *SSL_get_servername(const SSL *s, const int type)
        {
        if (type != TLSEXT_NAMETYPE_host_name)
                return NULL;
-       /* XXX cf. SSL_CTRL_GET_TLSEXT_HOSTNAME case in ssl3_ctrl (s3_lib.c) */
-       return s->session /*&&s->session->tlsext_hostname*/ ?
+
+       return s->session && !s->tlsext_hostname ?
                s->session->tlsext_hostname :
                s->tlsext_hostname;
        }
 
 int SSL_get_servername_type(const SSL *s)
        {
-       if (s->session &&s->session->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname
+       if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)
                return TLSEXT_NAMETYPE_host_name;
        return -1;
        }
@@ -1493,6 +1539,11 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
 #ifndef OPENSSL_NO_TLSEXT
        ret->tlsext_servername_callback = 0;
        ret->tlsext_servername_arg = NULL;
+#endif
+#ifndef OPENSSL_NO_PSK
+       ret->psk_identity_hint=NULL;
+       ret->psk_client_callback=NULL;
+       ret->psk_server_callback=NULL;
 #endif
        return(ret);
 err:
@@ -1564,6 +1615,11 @@ void SSL_CTX_free(SSL_CTX *a)
 #else
        a->comp_methods = NULL;
 #endif
+
+#ifndef OPENSSL_NO_PSK
+       if (a->psk_identity_hint)
+               OPENSSL_free(a->psk_identity_hint);
+#endif
        OPENSSL_free(a);
        }
 
@@ -1651,8 +1707,8 @@ void ssl_set_cert_masks(CERT *c, SSL_CIPHER *cipher)
        emask=0;
 
 #ifdef CIPHER_DEBUG
-       printf("rt=%d rte=%d dht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
-               rsa_tmp,rsa_tmp_export,dh_tmp,
+       printf("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,ecdh_tmp,
                rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
 #endif
 
@@ -1724,19 +1780,25 @@ void ssl_set_cert_masks(CERT *c, SSL_CIPHER *cipher)
 #ifndef OPENSSL_NO_ECDH
                if (ecdh_ok)
                        {
-                       if ((signature_nid == NID_md5WithRSAEncryption) ||
-                           (signature_nid == NID_md4WithRSAEncryption) ||
-                           (signature_nid == NID_md2WithRSAEncryption))
+                       const char *sig = OBJ_nid2ln(signature_nid);
+                       if (sig == NULL)
                                {
-                               mask|=SSL_kECDH|SSL_aRSA;
+                               ERR_clear_error();
+                               sig = "unknown";
+                               }
+                               
+                       if (strstr(sig, "WithRSA"))
+                               {
+                               mask|=SSL_kECDHr|SSL_aECDH;
                                if (ecc_pkey_size <= 163)
-                                       emask|=SSL_kECDH|SSL_aRSA;
+                                       emask|=SSL_kECDHr|SSL_aECDH;
                                }
+
                        if (signature_nid == NID_ecdsa_with_SHA1)
                                {
-                               mask|=SSL_kECDH|SSL_aECDSA;
+                               mask|=SSL_kECDHe|SSL_aECDH;
                                if (ecc_pkey_size <= 163)
-                                       emask|=SSL_kECDH|SSL_aECDSA;
+                                       emask|=SSL_kECDHe|SSL_aECDH;
                                }
                        }
 #endif
@@ -1752,10 +1814,16 @@ void ssl_set_cert_masks(CERT *c, SSL_CIPHER *cipher)
 #ifndef OPENSSL_NO_ECDH
        if (have_ecdh_tmp)
                {
-               mask|=SSL_kECDHE;
-               emask|=SSL_kECDHE;
+               mask|=SSL_kEECDH;
+               emask|=SSL_kEECDH;
                }
 #endif
+
+#ifndef OPENSSL_NO_PSK
+       mask  |= SSL_kPSK | SSL_aPSK;
+       emask |= SSL_kPSK | SSL_aPSK;
+#endif
+
        c->mask=mask;
        c->export_mask=emask;
        c->valid=1;
@@ -1765,7 +1833,7 @@ void ssl_set_cert_masks(CERT *c, SSL_CIPHER *cipher)
 #define ku_reject(x, usage) \
        (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
 
-int check_srvr_ecc_cert_and_alg(X509 *x, SSL_CIPHER *cs)
+int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL_CIPHER *cs)
        {
        unsigned long alg = cs->algorithms;
        EVP_PKEY *pkey = NULL;
@@ -1786,37 +1854,46 @@ int check_srvr_ecc_cert_and_alg(X509 *x, SSL_CIPHER *cs)
        X509_check_purpose(x, -1, 0);
        if ((x->sig_alg) && (x->sig_alg->algorithm))
                signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
-       if (alg & SSL_kECDH) 
+       if (alg & SSL_kECDHe || alg & SSL_kECDHr
                {
                /* key usage, if present, must allow key agreement */
                if (ku_reject(x, X509v3_KU_KEY_AGREEMENT))
                        {
+                       SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT);
                        return 0;
                        }
-               if (alg & SSL_aECDSA) 
+               if (alg & SSL_kECDHe)
                        {
                        /* signature alg must be ECDSA */
                        if (signature_nid != NID_ecdsa_with_SHA1)
                                {
+                               SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE);
                                return 0;
                                }
                        }
-               if (alg & SSL_aRSA)
+               if (alg & SSL_kECDHr)
                        {
                        /* signature alg must be RSA */
-                       if ((signature_nid != NID_md5WithRSAEncryption) &&
-                           (signature_nid != NID_md4WithRSAEncryption) &&
-                           (signature_nid != NID_md2WithRSAEncryption))
+
+                       const char *sig = OBJ_nid2ln(signature_nid);
+                       if (sig == NULL)
+                               {
+                               ERR_clear_error();
+                               sig = "unknown";
+                               }
+                       if (strstr(sig, "WithRSA") == NULL)
                                {
+                               SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE);
                                return 0;
                                }
                        }
                } 
-       else if (alg & SSL_aECDSA)
+       if (alg & SSL_aECDSA)
                {
                /* key usage, if present, must allow signing */
                if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
                        {
+                       SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
                        return 0;
                        }
                }
@@ -1840,13 +1917,13 @@ X509 *ssl_get_server_send_cert(SSL *s)
 
        if (kalg & SSL_kECDH)
                {
-               /* we don't need to look at SSL_kECDHE 
+               /* we don't need to look at SSL_kEECDH
                 * since no certificate is needed for
                 * anon ECDH and for authenticated
-                * ECDHE, the check for the auth 
+                * EECDH, the check for the auth
                 * algorithm will set i correctly
                 * NOTE: For ECDH-RSA, we need an ECC
-                * not an RSA cert but for ECDHE-RSA
+                * not an RSA cert but for EECDH-RSA
                 * we need an RSA cert. Placing the
                 * checks for SSL_kECDH before RSA
                 * checks ensures the correct cert is chosen.
@@ -1923,14 +2000,14 @@ void ssl_update_cache(SSL *s,int mode)
         * and it would be rather hard to do anyway :-) */
        if (s->session->session_id_length == 0) return;
 
-       i=s->ctx->session_cache_mode;
+       i=s->session_ctx->session_cache_mode;
        if ((i & mode) && (!s->hit)
                && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
-                   || SSL_CTX_add_session(s->ctx,s->session))
-               && (s->ctx->new_session_cb != NULL))
+                   || SSL_CTX_add_session(s->session_ctx,s->session))
+               && (s->session_ctx->new_session_cb != NULL))
                {
                CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
-               if (!s->ctx->new_session_cb(s,s->session))
+               if (!s->session_ctx->new_session_cb(s,s->session))
                        SSL_SESSION_free(s->session);
                }
 
@@ -1939,10 +2016,10 @@ void ssl_update_cache(SSL *s,int mode)
                ((i & mode) == mode))
                {
                if (  (((mode & SSL_SESS_CACHE_CLIENT)
-                       ?s->ctx->stats.sess_connect_good
-                       :s->ctx->stats.sess_accept_good) & 0xff) == 0xff)
+                       ?s->session_ctx->stats.sess_connect_good
+                       :s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff)
                        {
-                       SSL_CTX_flush_sessions(s->ctx,(unsigned long)time(NULL));
+                       SSL_CTX_flush_sessions(s->session_ctx,(unsigned long)time(NULL));
                        }
                }
        }
@@ -2445,6 +2522,10 @@ SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
 
 SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
        {
+       if (ssl->ctx == ctx) 
+               return ssl->ctx;
+       if (ctx == NULL)
+               ctx = ssl->initial_ctx;
        if (ssl->cert != NULL)
                ssl_cert_free(ssl->cert);
        ssl->cert = ssl_cert_dup(ctx->cert);
@@ -2469,14 +2550,14 @@ int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
 #endif
 
 void SSL_set_info_callback(SSL *ssl,
-                          void (*cb)(const SSL *ssl,int type,int val))
+       void (*cb)(const SSL *ssl,int type,int val))
        {
        ssl->info_callback=cb;
        }
 
 /* One compiler (Diab DCC) doesn't like argument names in returned
    function pointer.  */
-void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
+void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/) 
        {
        return ssl->info_callback;
        }
@@ -2623,6 +2704,97 @@ void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
        }
 #endif
 
+#ifndef OPENSSL_NO_PSK
+int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
+       {
+       if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
+               {
+               SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
+               return 0;
+               }
+       if (ctx->psk_identity_hint != NULL)
+               OPENSSL_free(ctx->psk_identity_hint);
+       if (identity_hint != NULL)
+               {
+               ctx->psk_identity_hint = BUF_strdup(identity_hint);
+               if (ctx->psk_identity_hint == NULL)
+                       return 0;
+               }
+       else
+               ctx->psk_identity_hint = NULL;
+       return 1;
+       }
+
+int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
+       {
+       if (s == NULL)
+               return 0;
+
+       if (s->session == NULL)
+               return 1; /* session not created yet, ignored */
+
+       if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
+               {
+               SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
+               return 0;
+               }
+       if (s->session->psk_identity_hint != NULL)
+               OPENSSL_free(s->session->psk_identity_hint);
+       if (identity_hint != NULL)
+               {
+               s->session->psk_identity_hint = BUF_strdup(identity_hint);
+               if (s->session->psk_identity_hint == NULL)
+                       return 0;
+               }
+       else
+               s->session->psk_identity_hint = NULL;
+       return 1;
+       }
+
+const char *SSL_get_psk_identity_hint(const SSL *s)
+       {
+       if (s == NULL || s->session == NULL)
+               return NULL;
+       return(s->session->psk_identity_hint);
+       }
+
+const char *SSL_get_psk_identity(const SSL *s)
+       {
+       if (s == NULL || s->session == NULL)
+               return NULL;
+       return(s->session->psk_identity);
+       }
+
+void SSL_set_psk_client_callback(SSL *s, 
+       unsigned int (*cb)(SSL *ssl, const char *hint,
+                char *identity, unsigned int max_identity_len, unsigned char *psk,
+                unsigned int max_psk_len))
+       {
+       s->psk_client_callback = cb;    
+       }
+
+void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
+       unsigned int (*cb)(SSL *ssl, const char *hint,
+                char *identity, unsigned int max_identity_len, unsigned char *psk,
+                unsigned int max_psk_len))
+       {
+       ctx->psk_client_callback = cb;  
+       }
+
+void SSL_set_psk_server_callback(SSL *s, 
+       unsigned int (*cb)(SSL *ssl, const char *identity,
+                unsigned char *psk, unsigned int max_psk_len))
+       {
+       s->psk_server_callback = cb;    
+       }
+
+void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
+       unsigned int (*cb)(SSL *ssl, const char *identity,
+                unsigned char *psk, unsigned int max_psk_len))
+       {
+       ctx->psk_server_callback = cb;  
+       }
+#endif
 
 void SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
        {