DTLS 1.2 cached record support.
[oweals/openssl.git] / ssl / t1_lib.c
index 96290e2834067d0fe55926ec6f37754ada6a88c0..35a58a956cf220758dcbd8521b3d755f1c329bfb 100644 (file)
@@ -140,6 +140,48 @@ SSL3_ENC_METHOD TLSv1_enc_data={
        TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
        tls1_alert_code,
        tls1_export_keying_material,
+       0,
+       SSL3_HM_HEADER_LENGTH,
+       ssl3_set_handshake_header,
+       ssl3_handshake_write
+       };
+
+SSL3_ENC_METHOD TLSv1_1_enc_data={
+       tls1_enc,
+       tls1_mac,
+       tls1_setup_key_block,
+       tls1_generate_master_secret,
+       tls1_change_cipher_state,
+       tls1_final_finish_mac,
+       TLS1_FINISH_MAC_LENGTH,
+       tls1_cert_verify_mac,
+       TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
+       TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
+       tls1_alert_code,
+       tls1_export_keying_material,
+       SSL_ENC_FLAG_EXPLICIT_IV,
+       SSL3_HM_HEADER_LENGTH,
+       ssl3_set_handshake_header,
+       ssl3_handshake_write
+       };
+
+SSL3_ENC_METHOD TLSv1_2_enc_data={
+       tls1_enc,
+       tls1_mac,
+       tls1_setup_key_block,
+       tls1_generate_master_secret,
+       tls1_change_cipher_state,
+       tls1_final_finish_mac,
+       TLS1_FINISH_MAC_LENGTH,
+       tls1_cert_verify_mac,
+       TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
+       TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
+       tls1_alert_code,
+       tls1_export_keying_material,
+       SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF,
+       SSL3_HM_HEADER_LENGTH,
+       ssl3_set_handshake_header,
+       ssl3_handshake_write
        };
 
 long tls1_default_timeout(void)
@@ -241,6 +283,12 @@ static const unsigned char eccurves_default[] =
                0,17, /* secp160r2 (17) */ 
        };
 
+static const unsigned char suiteb_curves[] =
+       {
+               0, TLSEXT_curve_P_256,
+               0, TLSEXT_curve_P_384
+       };
+
 int tls1_ec_curve_id2nid(int curve_id)
        {
        /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
@@ -320,13 +368,29 @@ static void tls1_get_curvelist(SSL *s, int sess,
                {
                *pcurves = s->session->tlsext_ellipticcurvelist;
                *pcurveslen = s->session->tlsext_ellipticcurvelist_length;
+               return;
                }
-       else
+       /* For Suite B mode only include P-256, P-384 */
+       switch (tls1_suiteb(s))
                {
+       case SSL_CERT_FLAG_SUITEB_128_LOS:
+               *pcurves = suiteb_curves;
+               *pcurveslen = sizeof(suiteb_curves);
+               break;
+
+       case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
+               *pcurves = suiteb_curves;
+               *pcurveslen = 2;
+               break;
+
+       case SSL_CERT_FLAG_SUITEB_192_LOS:
+               *pcurves = suiteb_curves + 2;
+               *pcurveslen = 2;
+               break;
+       default:
                *pcurves = s->tlsext_ellipticcurvelist;
                *pcurveslen = s->tlsext_ellipticcurvelist_length;
                }
-       /* If not set use default: for now static structure */
        if (!*pcurves)
                {
                *pcurves = eccurves_default;
@@ -338,8 +402,28 @@ int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
        {
        const unsigned char *curves;
        size_t curveslen, i;
+       unsigned int suiteb_flags = tls1_suiteb(s);
        if (len != 3 || p[0] != NAMED_CURVE_TYPE)
                return 0;
+       /* Check curve matches Suite B preferences */
+       if (suiteb_flags)
+               {
+               unsigned long cid = s->s3->tmp.new_cipher->id;
+               if (p[1])
+                       return 0;
+               if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
+                       {
+                       if (p[2] != TLSEXT_curve_P_256)
+                               return 0;
+                       }
+               else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
+                       {
+                       if (p[2] != TLSEXT_curve_P_384)
+                               return 0;
+                       }
+               else    /* Should never happen */
+                       return 0;
+               }
        tls1_get_curvelist(s, 0, &curves, &curveslen);
        for (i = 0; i < curveslen; i += 2, curves += 2)
                {
@@ -350,7 +434,8 @@ int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
        }
 
 /* Return nth shared curve. If nmatch == -1 return number of
- * matches.
+ * matches. For nmatch == -2 return the NID of the curve to use for
+ * an EC tmp key.
  */
 
 int tls1_shared_curve(SSL *s, int nmatch)
@@ -361,6 +446,25 @@ int tls1_shared_curve(SSL *s, int nmatch)
        /* Can't do anything on client side */
        if (s->server == 0)
                return -1;
+       if (nmatch == -2)
+               {
+               if (tls1_suiteb(s))
+                       {
+                       /* For Suite B ciphersuite determines curve: we 
+                        * already know these are acceptable due to previous
+                        * checks.
+                        */
+                       unsigned long cid = s->s3->tmp.new_cipher->id;
+                       if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
+                               return NID_X9_62_prime256v1; /* P-256 */
+                       if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
+                               return NID_secp384r1; /* P-384 */
+                       /* Should never happen */
+                       return NID_undef;
+                       }
+               /* If not Suite B just return first preference shared curve */
+               nmatch = 0;
+               }
        tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
                                &supp, &supplen);
        tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
@@ -463,6 +567,8 @@ int tls1_set_curves_list(unsigned char **pext, size_t *pextlen,
        ncb.nidcnt = 0;
        if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
                return 0;
+       if (pext == NULL)
+               return 1;
        return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
        }
 /* For an EC key set TLS id and required compression based on parameters */
@@ -540,6 +646,8 @@ static int tls1_check_ec_key(SSL *s,
                if (i == plen)
                        return 0;
                }
+       if (!curve_id)
+               return 1;
        /* Check curve is consistent with client and server preferences */
        for (j = 0; j <= 1; j++)
                {
@@ -551,14 +659,38 @@ static int tls1_check_ec_key(SSL *s,
                        }
                if (i == plen)
                        return 0;
+               /* For clients can only check sent curve list */
+               if (!s->server)
+                       return 1;
                }
        return 1;
        }
 
+static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
+                                       size_t *pformatslen)
+       {
+       /* If we have a custom point format list use it otherwise
+        * use default */
+       if (s->tlsext_ecpointformatlist)
+               {
+               *pformats = s->tlsext_ecpointformatlist;
+               *pformatslen = s->tlsext_ecpointformatlist_length;
+               }
+       else
+               {
+               *pformats = ecformats_default;
+               /* For Suite B we don't support char2 fields */
+               if (tls1_suiteb(s))
+                       *pformatslen = sizeof(ecformats_default) - 1;
+               else
+                       *pformatslen = sizeof(ecformats_default);
+               }
+       }
+
 /* Check cert parameters compatible with extensions: currently just checks
  * EC certificates have compatible curves and compression.
  */
-static int tls1_check_cert_param(SSL *s, X509 *x)
+static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
        {
        unsigned char comp_id, curve_id[2];
        EVP_PKEY *pkey;
@@ -576,13 +708,87 @@ static int tls1_check_cert_param(SSL *s, X509 *x)
        EVP_PKEY_free(pkey);
        if (!rv)
                return 0;
-       return tls1_check_ec_key(s, curve_id, &comp_id);
+       /* Can't check curve_id for client certs as we don't have a
+        * supported curves extension.
+        */
+       rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
+       if (!rv)
+               return 0;
+       /* Special case for suite B. We *MUST* sign using SHA256+P-256 or
+        * SHA384+P-384, adjust digest if necessary.
+        */
+       if (set_ee_md && tls1_suiteb(s))
+               {
+               int check_md;
+               size_t i;
+               CERT *c = s->cert;
+               if (curve_id[0])
+                       return 0;
+               /* Check to see we have necessary signing algorithm */
+               if (curve_id[1] == TLSEXT_curve_P_256)
+                       check_md = NID_ecdsa_with_SHA256;
+               else if (curve_id[1] == TLSEXT_curve_P_384)
+                       check_md = NID_ecdsa_with_SHA384;
+               else
+                       return 0; /* Should never happen */
+               for (i = 0; i < c->shared_sigalgslen; i++)
+                       if (check_md == c->shared_sigalgs[i].signandhash_nid)
+                               break;
+               if (i == c->shared_sigalgslen)
+                       return 0;
+               if (set_ee_md == 2)
+                       {
+                       if (check_md == NID_ecdsa_with_SHA256)
+                               c->pkeys[SSL_PKEY_ECC].digest = EVP_sha256();
+                       else
+                               c->pkeys[SSL_PKEY_ECC].digest = EVP_sha384();
+                       }
+               }
+       return rv;
        }
 /* Check EC temporary key is compatible with client extensions */
-int tls1_check_ec_tmp_key(SSL *s)
+int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
        {
        unsigned char curve_id[2];
        EC_KEY *ec = s->cert->ecdh_tmp;
+#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
+       /* Allow any curve: not just those peer supports */
+       if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
+               return 1;
+#endif
+       /* If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384,
+        * no other curves permitted.
+        */
+       if (tls1_suiteb(s))
+               {
+               /* Curve to check determined by ciphersuite */
+               if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
+                       curve_id[1] = TLSEXT_curve_P_256;
+               else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
+                       curve_id[1] = TLSEXT_curve_P_384;
+               else
+                       return 0;
+               curve_id[0] = 0;
+               /* Check this curve is acceptable */
+               if (!tls1_check_ec_key(s, curve_id, NULL))
+                       return 0;
+               /* If auto or setting curve from callback assume OK */
+               if (s->cert->ecdh_tmp_auto || s->cert->ecdh_tmp_cb)
+                       return 1;
+               /* Otherwise check curve is acceptable */
+               else 
+                       {
+                       unsigned char curve_tmp[2];
+                       if (!ec)
+                               return 0;
+                       if (!tls1_set_ec_id(curve_tmp, NULL, ec))
+                               return 0;
+                       if (!curve_tmp[0] || curve_tmp[1] == curve_id[1])
+                               return 1;
+                       return 0;
+                       }
+                       
+               }
        if (s->cert->ecdh_tmp_auto)
                {
                /* Need a shared curve */
@@ -607,6 +813,13 @@ int tls1_check_ec_tmp_key(SSL *s)
 #endif
        }
 
+#else
+
+static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
+       {
+       return 1;
+       }
+
 #endif /* OPENSSL_NO_EC */
 
 #ifndef OPENSSL_NO_TLSEXT
@@ -654,9 +867,33 @@ static unsigned char tls12_sigalgs[] = {
        tlsext_sigalg_rsa(TLSEXT_hash_md5)
 #endif
 };
-
+#ifndef OPENSSL_NO_ECDSA
+static unsigned char suiteb_sigalgs[] = {
+       tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
+       tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
+};
+#endif
 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
        {
+       /* If Suite B mode use Suite B sigalgs only, ignore any other
+        * preferences.
+        */
+#ifndef OPENSSL_NO_EC
+       switch (tls1_suiteb(s))
+               {
+       case SSL_CERT_FLAG_SUITEB_128_LOS:
+               *psigs = suiteb_sigalgs;
+               return sizeof(suiteb_sigalgs);
+
+       case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
+               *psigs = suiteb_sigalgs;
+               return 2;
+
+       case SSL_CERT_FLAG_SUITEB_192_LOS:
+               *psigs = suiteb_sigalgs + 2;
+               return 2;
+               }
+#endif
        /* If server use client authentication sigalgs if not NULL */
        if (s->server && s->cert->client_sigalgs)
                {
@@ -698,6 +935,49 @@ int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
                SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
                return 0;
                }
+#ifndef OPENSSL_NO_EC
+       if (pkey->type == EVP_PKEY_EC)
+               {
+               unsigned char curve_id[2], comp_id;
+               /* Check compression and curve matches extensions */
+               if (!tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec))
+                       return 0;
+               if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id))
+                       {
+                       SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_CURVE);
+                       return 0;
+                       }
+               /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
+               if (tls1_suiteb(s))
+                       {
+                       if (curve_id[0])
+                               return 0;
+                       if (curve_id[1] == TLSEXT_curve_P_256)
+                               {
+                               if (sig[0] != TLSEXT_hash_sha256)
+                                       {
+                                       SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
+                                               SSL_R_ILLEGAL_SUITEB_DIGEST);
+                                       return 0;
+                                       }
+                               }
+                       else if (curve_id[1] == TLSEXT_curve_P_384)
+                               {
+                               if (sig[0] != TLSEXT_hash_sha384)
+                                       {
+                                       SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
+                                               SSL_R_ILLEGAL_SUITEB_DIGEST);
+                                       return 0;
+                                       }
+                               }
+                       else
+                               return 0;
+                       }
+               }
+       else if (tls1_suiteb(s))
+               return 0;
+#endif
+
        /* Check signature matches a type we sent */
        sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
        for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
@@ -706,7 +986,7 @@ int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
                        break;
                }
        /* Allow fallback to SHA1 if not strict mode */
-       if (i == sent_sigslen && (sig[0] != TLSEXT_hash_sha1 || s->cert->cert_flags & SSL_CERT_FLAG_TLS_STRICT))
+       if (i == sent_sigslen && (sig[0] != TLSEXT_hash_sha1 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
                {
                SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
                return 0;
@@ -717,6 +997,11 @@ int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
                SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_UNKNOWN_DIGEST);
                return 0;
                }
+       /* Store the digest used so applications can retrieve it if they
+        * wish.
+        */
+       if (s->session && s->session->sess_cert)
+               s->session->sess_cert->peer_key->digest = *pmd;
        return 1;
        }
 /* Get a mask of disabled algorithms: an algorithm is disabled
@@ -734,7 +1019,7 @@ void ssl_set_client_disabled(SSL *s)
        c->mask_a = 0;
        c->mask_k = 0;
        /* If less than TLS 1.2 don't allow TLS 1.2 only ciphers */
-       if (TLS1_get_version(s) < TLS1_2_VERSION)
+       if (TLS1_get_client_version(s) < TLS1_2_VERSION)
                c->mask_ssl = SSL_TLSV1_2;
        else
                c->mask_ssl = 0;
@@ -820,7 +1105,7 @@ unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned cha
 #ifndef OPENSSL_NO_EC
        /* See if we support any ECC ciphersuites */
        int using_ecc = 0;
-       if (s->version != DTLS1_VERSION && s->version >= TLS1_VERSION)
+       if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s))
                {
                int i;
                unsigned long alg_k, alg_a;
@@ -943,16 +1228,8 @@ unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned cha
                long lenmax; 
                const unsigned char *plist;
                size_t plistlen;
-               /* If we have a custom point format list use it otherwise
-                * use default */
-               plist = s->tlsext_ecpointformatlist;
-               if (plist)
-                       plistlen = s->tlsext_ecpointformatlist_length;
-               else
-                       {
-                       plist = ecformats_default;
-                       plistlen = sizeof(ecformats_default);
-                       }
+
+               tls1_get_formatlist(s, &plist, &plistlen);
 
                if ((lenmax = limit - ret - 5) < 0) return NULL; 
                if (plistlen > (size_t)lenmax) return NULL;
@@ -1030,7 +1307,7 @@ unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned cha
                }
                skip_ext:
 
-       if (TLS1_get_client_version(s) >= TLS1_2_VERSION)
+       if (SSL_USE_SIGALGS(s))
                {
                size_t salglen;
                const unsigned char *salg;
@@ -1045,8 +1322,7 @@ unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned cha
                }
 
 #ifdef TLSEXT_TYPE_opaque_prf_input
-       if (s->s3->client_opaque_prf_input != NULL &&
-           s->version != DTLS1_VERSION)
+       if (s->s3->client_opaque_prf_input != NULL)
                {
                size_t col = s->s3->client_opaque_prf_input_len;
                
@@ -1063,8 +1339,7 @@ unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned cha
                }
 #endif
 
-       if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp &&
-           s->version != DTLS1_VERSION)
+       if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
                {
                int i;
                long extlen, idlen, itmp;
@@ -1164,11 +1439,11 @@ unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned cha
        /* 1 byte for the list (we only support audit proofs) */
        if (s->ctx->tlsext_authz_server_audit_proof_cb != NULL)
                {
-               size_t lenmax;
                 const unsigned short ext_len = 2;
                 const unsigned char list_len = 1;
 
-               if ((lenmax = limit - ret - 6) < 0) return NULL;
+               if (limit < ret + 6)
+                       return NULL;
 
                s2n(TLSEXT_TYPE_server_authz, ret);
                 /* Extension length: 2 bytes */
@@ -1177,6 +1452,40 @@ unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned cha
                *(ret++) = TLSEXT_AUTHZDATAFORMAT_audit_proof;
                }
 
+       /* Add custom TLS Extensions to ClientHello */
+       if (s->ctx->custom_cli_ext_records_count)
+               {
+               size_t i;
+               custom_cli_ext_record* record;
+
+               for (i = 0; i < s->ctx->custom_cli_ext_records_count; i++)
+                       {
+                       const unsigned char* out = NULL;
+                       unsigned short outlen = 0;
+
+                       record = &s->ctx->custom_cli_ext_records[i];
+                       /* NULL callback sends empty extension */ 
+                       /* -1 from callback omits extension */
+                       if (record->fn1)
+                               {
+                               int cb_retval = 0;
+                               cb_retval = record->fn1(s, record->ext_type,
+                                                       &out, &outlen,
+                                                       record->arg);
+                               if (cb_retval == 0)
+                                       return NULL; /* error */
+                               if (cb_retval == -1)
+                                       continue; /* skip this extension */
+                               }
+                       if (limit < ret + 4 + outlen)
+                               return NULL;
+                       s2n(record->ext_type, ret);
+                       s2n(outlen, ret);
+                       memcpy(ret, out, outlen);
+                       ret += outlen;
+                       }
+               }
+
        if ((extdatalen = ret-p-2) == 0)
                return p;
 
@@ -1191,7 +1500,12 @@ unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned cha
 #ifndef OPENSSL_NO_NEXTPROTONEG
        int next_proto_neg_seen;
 #endif
-
+#ifndef OPENSSL_NO_EC
+       unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
+       unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
+       int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
+       using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
+#endif
        /* don't add extensions for SSLv3, unless doing secure renegotiation */
        if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
                return p;
@@ -1232,25 +1546,28 @@ unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned cha
         }
 
 #ifndef OPENSSL_NO_EC
-       if (s->tlsext_ecpointformatlist != NULL &&
-           s->version != DTLS1_VERSION)
+       if (using_ecc)
                {
+               const unsigned char *plist;
+               size_t plistlen;
                /* Add TLS extension ECPointFormats to the ServerHello message */
                long lenmax; 
 
+               tls1_get_formatlist(s, &plist, &plistlen);
+
                if ((lenmax = limit - ret - 5) < 0) return NULL; 
-               if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
-               if (s->tlsext_ecpointformatlist_length > 255)
+               if (plistlen > (size_t)lenmax) return NULL;
+               if (plistlen > 255)
                        {
                        SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
                        return NULL;
                        }
                
                s2n(TLSEXT_TYPE_ec_point_formats,ret);
-               s2n(s->tlsext_ecpointformatlist_length + 1,ret);
-               *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
-               memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
-               ret+=s->tlsext_ecpointformatlist_length;
+               s2n(plistlen + 1,ret);
+               *(ret++) = (unsigned char) plistlen;
+               memcpy(ret, plist, plistlen);
+               ret+=plistlen;
 
                }
        /* Currently the server should not respond with a SupportedCurves extension */
@@ -1272,8 +1589,7 @@ unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned cha
                }
 
 #ifdef TLSEXT_TYPE_opaque_prf_input
-       if (s->s3->server_opaque_prf_input != NULL &&
-           s->version != DTLS1_VERSION)
+       if (s->s3->server_opaque_prf_input != NULL)
                {
                size_t sol = s->s3->server_opaque_prf_input_len;
                
@@ -1437,6 +1753,47 @@ unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned cha
                        }
                }
 
+       /* If custom types were sent in ClientHello, add ServerHello responses */
+       if (s->s3->tlsext_custom_types_count)
+               {
+               size_t i;
+
+               for (i = 0; i < s->s3->tlsext_custom_types_count; i++)
+                       {
+                       size_t j;
+                       custom_srv_ext_record *record;
+
+                       for (j = 0; j < s->ctx->custom_srv_ext_records_count; j++)
+                               {
+                               record = &s->ctx->custom_srv_ext_records[j];
+                               if (s->s3->tlsext_custom_types[i] == record->ext_type)
+                                       {
+                                       const unsigned char *out = NULL;
+                                       unsigned short outlen = 0;
+                                       int cb_retval = 0;
+
+                                       /* NULL callback or -1 omits extension */
+                                       if (!record->fn2)
+                                               break;
+                                       cb_retval = record->fn2(s, record->ext_type,
+                                                               &out, &outlen,
+                                                               record->arg);
+                                       if (cb_retval == 0)
+                                               return NULL; /* error */
+                                       if (cb_retval == -1)
+                                               break; /* skip this extension */
+                                       if (limit < ret + 4 + outlen)
+                                               return NULL;
+                                       s2n(record->ext_type, ret);
+                                       s2n(outlen, ret);
+                                       memcpy(ret, out, outlen);
+                                       ret += outlen;
+                                       break;
+                                       }
+                               }
+                       }
+               }
+
        if ((extdatalen = ret-p-2)== 0) 
                return p;
 
@@ -1444,6 +1801,89 @@ unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned cha
        return ret;
        }
 
+#ifndef OPENSSL_NO_EC
+/* ssl_check_for_safari attempts to fingerprint Safari using OS X
+ * SecureTransport using the TLS extension block in |d|, of length |n|.
+ * Safari, since 10.6, sends exactly these extensions, in this order:
+ *   SNI,
+ *   elliptic_curves
+ *   ec_point_formats
+ *
+ * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
+ * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
+ * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
+ * 10.8..10.8.3 (which don't work).
+ */
+static void ssl_check_for_safari(SSL *s, const unsigned char *data, const unsigned char *d, int n) {
+       unsigned short type, size;
+       static const unsigned char kSafariExtensionsBlock[] = {
+               0x00, 0x0a,  /* elliptic_curves extension */
+               0x00, 0x08,  /* 8 bytes */
+               0x00, 0x06,  /* 6 bytes of curve ids */
+               0x00, 0x17,  /* P-256 */
+               0x00, 0x18,  /* P-384 */
+               0x00, 0x19,  /* P-521 */
+
+               0x00, 0x0b,  /* ec_point_formats */
+               0x00, 0x02,  /* 2 bytes */
+               0x01,        /* 1 point format */
+               0x00,        /* uncompressed */
+       };
+
+       /* The following is only present in TLS 1.2 */
+       static const unsigned char kSafariTLS12ExtensionsBlock[] = {
+               0x00, 0x0d,  /* signature_algorithms */
+               0x00, 0x0c,  /* 12 bytes */
+               0x00, 0x0a,  /* 10 bytes */
+               0x05, 0x01,  /* SHA-384/RSA */
+               0x04, 0x01,  /* SHA-256/RSA */
+               0x02, 0x01,  /* SHA-1/RSA */
+               0x04, 0x03,  /* SHA-256/ECDSA */
+               0x02, 0x03,  /* SHA-1/ECDSA */
+       };
+
+       if (data >= (d+n-2))
+               return;
+       data += 2;
+
+       if (data > (d+n-4))
+               return;
+       n2s(data,type);
+       n2s(data,size);
+
+       if (type != TLSEXT_TYPE_server_name)
+               return;
+
+       if (data+size > d+n)
+               return;
+       data += size;
+
+       if (TLS1_get_client_version(s) >= TLS1_2_VERSION)
+               {
+               const size_t len1 = sizeof(kSafariExtensionsBlock);
+               const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
+
+               if (data + len1 + len2 != d+n)
+                       return;
+               if (memcmp(data, kSafariExtensionsBlock, len1) != 0)
+                       return;
+               if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0)
+                       return;
+               }
+       else
+               {
+               const size_t len = sizeof(kSafariExtensionsBlock);
+
+               if (data + len != d+n)
+                       return;
+               if (memcmp(data, kSafariExtensionsBlock, len) != 0)
+                       return;
+               }
+
+       s->s3->is_probably_safari = 1;
+}
+#endif /* !OPENSSL_NO_EC */
+
 static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al) 
        {       
        unsigned short type;
@@ -1459,10 +1899,24 @@ static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char
        s->s3->next_proto_neg_seen = 0;
 #endif
 
+       /* Clear observed custom extensions */
+       s->s3->tlsext_custom_types_count = 0;
+       if (s->s3->tlsext_custom_types != NULL)
+               {
+               OPENSSL_free(s->s3->tlsext_custom_types);
+               s->s3->tlsext_custom_types = NULL;
+               }               
+
 #ifndef OPENSSL_NO_HEARTBEATS
        s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
                               SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
 #endif
+
+#ifndef OPENSSL_NO_EC
+       if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
+               ssl_check_for_safari(s, data, d, n);
+#endif /* !OPENSSL_NO_EC */
+
        /* Clear any signature algorithms extension received */
        if (s->cert->peer_sigalgs)
                {
@@ -1635,13 +2089,13 @@ static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char
 #endif
 
 #ifndef OPENSSL_NO_EC
-               else if (type == TLSEXT_TYPE_ec_point_formats &&
-                    s->version != DTLS1_VERSION)
+               else if (type == TLSEXT_TYPE_ec_point_formats)
                        {
                        unsigned char *sdata = data;
                        int ecpointformatlist_length = *(sdata++);
 
-                       if (ecpointformatlist_length != size - 1)
+                       if (ecpointformatlist_length != size - 1 || 
+                               ecpointformatlist_length < 1)
                                {
                                *al = TLS1_AD_DECODE_ERROR;
                                return 0;
@@ -1670,8 +2124,7 @@ static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char
                        fprintf(stderr,"\n");
 #endif
                        }
-               else if (type == TLSEXT_TYPE_elliptic_curves &&
-                    s->version != DTLS1_VERSION)
+               else if (type == TLSEXT_TYPE_elliptic_curves)
                        {
                        unsigned char *sdata = data;
                        int ellipticcurvelist_length = (*(sdata++) << 8);
@@ -1709,8 +2162,7 @@ static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char
                        }
 #endif /* OPENSSL_NO_EC */
 #ifdef TLSEXT_TYPE_opaque_prf_input
-               else if (type == TLSEXT_TYPE_opaque_prf_input &&
-                    s->version != DTLS1_VERSION)
+               else if (type == TLSEXT_TYPE_opaque_prf_input)
                        {
                        unsigned char *sdata = data;
 
@@ -1785,8 +2237,8 @@ static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char
                                return 0;
                                }
                        }
-               else if (type == TLSEXT_TYPE_status_request &&
-                        s->version != DTLS1_VERSION && s->ctx->tlsext_status_cb)
+               else if (type == TLSEXT_TYPE_status_request
+                        && s->ctx->tlsext_status_cb)
                        {
                
                        if (size < 5) 
@@ -2004,6 +2456,54 @@ static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char
                                }
                        }
 
+               /* If this ClientHello extension was unhandled and this is 
+                * a nonresumed connection, check whether the extension is a 
+                * custom TLS Extension (has a custom_srv_ext_record), and if
+                * so call the callback and record the extension number so that
+                * an appropriate ServerHello may be later returned.
+                */
+               else if (!s->hit && s->ctx->custom_srv_ext_records_count)
+                       {
+                       custom_srv_ext_record *record;
+
+                       for (i=0; i < s->ctx->custom_srv_ext_records_count; i++)
+                               {
+                               record = &s->ctx->custom_srv_ext_records[i];
+                               if (type == record->ext_type)
+                                       {
+                                       size_t j;
+
+                                       /* Error on duplicate TLS Extensions */
+                                       for (j = 0; j < s->s3->tlsext_custom_types_count; j++)
+                                               {
+                                               if (type == s->s3->tlsext_custom_types[j])
+                                                       {
+                                                       *al = TLS1_AD_DECODE_ERROR;
+                                                       return 0;
+                                                       }
+                                               }
+
+                                       /* NULL callback still notes the extension */ 
+                                       if (record->fn1 && !record->fn1(s, type, data, size, al, record->arg))
+                                               return 0;
+                                               
+                                       /* Add the (non-duplicated) entry */
+                                       s->s3->tlsext_custom_types_count++;
+                                       s->s3->tlsext_custom_types = OPENSSL_realloc(
+                                                       s->s3->tlsext_custom_types,
+                                                       s->s3->tlsext_custom_types_count * 2);
+                                       if (s->s3->tlsext_custom_types == NULL)
+                                               {
+                                               s->s3->tlsext_custom_types = 0;
+                                               *al = TLS1_AD_INTERNAL_ERROR;
+                                               return 0;
+                                               }
+                                       s->s3->tlsext_custom_types[
+                                                       s->s3->tlsext_custom_types_count - 1] = type;
+                                       }                                               
+                               }
+                       }
+
                data+=size;
                }
 
@@ -2116,14 +2616,12 @@ static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char
                        }
 
 #ifndef OPENSSL_NO_EC
-               else if (type == TLSEXT_TYPE_ec_point_formats &&
-                    s->version != DTLS1_VERSION)
+               else if (type == TLSEXT_TYPE_ec_point_formats)
                        {
                        unsigned char *sdata = data;
                        int ecpointformatlist_length = *(sdata++);
 
-                       if (ecpointformatlist_length != size - 1 || 
-                               ecpointformatlist_length < 1)
+                       if (ecpointformatlist_length != size - 1)
                                {
                                *al = TLS1_AD_DECODE_ERROR;
                                return 0;
@@ -2164,8 +2662,7 @@ static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char
                        s->tlsext_ticket_expected = 1;
                        }
 #ifdef TLSEXT_TYPE_opaque_prf_input
-               else if (type == TLSEXT_TYPE_opaque_prf_input &&
-                    s->version != DTLS1_VERSION)
+               else if (type == TLSEXT_TYPE_opaque_prf_input)
                        {
                        unsigned char *sdata = data;
 
@@ -2195,8 +2692,7 @@ static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char
                                }
                        }
 #endif
-               else if (type == TLSEXT_TYPE_status_request &&
-                        s->version != DTLS1_VERSION)
+               else if (type == TLSEXT_TYPE_status_request)
                        {
                        /* MUST be empty and only sent if we've requested
                         * a status request message.
@@ -2217,7 +2713,7 @@ static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char
                        unsigned char selected_len;
 
                        /* We must have requested it. */
-                       if ((s->ctx->next_proto_select_cb == NULL))
+                       if (s->ctx->next_proto_select_cb == NULL)
                                {
                                *al = TLS1_AD_UNSUPPORTED_EXTENSION;
                                return 0;
@@ -2312,6 +2808,26 @@ static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char
 
                        s->s3->tlsext_authz_server_promised = 1;
                        }
+
+               /* If this extension type was not otherwise handled, but 
+                * matches a custom_cli_ext_record, then send it to the c
+                * callback */
+               else if (s->ctx->custom_cli_ext_records_count)
+                       {
+                       size_t i;
+                       custom_cli_ext_record* record;
+
+                       for (i = 0; i < s->ctx->custom_cli_ext_records_count; i++)
+                               {
+                               record = &s->ctx->custom_cli_ext_records[i];
+                               if (record->ext_type == type)
+                                       {
+                                       if (record->fn2 && !record->fn2(s, type, data, size, al, record->arg))
+                                               return 0;
+                                       break;
+                                       }
+                               }                       
+                       }
  
                data += size;
                }
@@ -2410,32 +2926,6 @@ int ssl_prepare_clienthello_tlsext(SSL *s)
 
 int ssl_prepare_serverhello_tlsext(SSL *s)
        {
-#ifndef OPENSSL_NO_EC
-       /* If we are server and using an ECC cipher suite, send the point formats we support 
-        * if the client sent us an ECPointsFormat extension.  Note that the server is not
-        * supposed to send an EllipticCurves extension.
-        */
-
-       unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
-       unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
-       int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
-       using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
-       
-       if (using_ecc)
-               {
-               if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
-               if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
-                       {
-                       SSLerr(SSL_F_SSL_PREPARE_SERVERHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
-                       return -1;
-                       }
-               s->tlsext_ecpointformatlist_length = 3;
-               s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
-               s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
-               s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
-               }
-#endif /* OPENSSL_NO_EC */
-
        return 1;
        }
 
@@ -2587,11 +3077,11 @@ int ssl_check_clienthello_tlsext_late(SSL *s)
        switch (ret)
                {
                case SSL_TLSEXT_ERR_ALERT_FATAL:
-                       ssl3_send_alert(s, SSL3_AL_FATAL,al);
+                       ssl3_send_alert(s, SSL3_AL_FATAL, al);
                        return -1;
 
                case SSL_TLSEXT_ERR_ALERT_WARNING:
-                       ssl3_send_alert(s, SSL3_AL_WARNING,al);
+                       ssl3_send_alert(s, SSL3_AL_WARNING, al);
                        return 1; 
 
                default:
@@ -2783,7 +3273,7 @@ int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
        if (p >= limit)
                return -1;
        /* Skip past DTLS cookie */
-       if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER)
+       if (SSL_IS_DTLS(s))
                {
                i = *(p++);
                p+= i;
@@ -2919,7 +3409,7 @@ static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
        HMAC_Update(&hctx, etick, eticklen);
        HMAC_Final(&hctx, tick_hmac, NULL);
        HMAC_CTX_cleanup(&hctx);
-       if (memcmp(tick_hmac, etick + eticklen, mlen))
+       if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
                return 2;
        /* Attempt to decrypt session data */
        /* Move p after IV to start of encrypted ticket, update length */
@@ -3160,27 +3650,21 @@ static int tls1_set_shared_sigalgs(SSL *s)
        size_t nmatch;
        TLS_SIGALGS *salgs = NULL;
        CERT *c = s->cert;
+       unsigned int is_suiteb = tls1_suiteb(s);
        /* If client use client signature algorithms if not NULL */
-       if (!s->server && c->client_sigalgs)
+       if (!s->server && c->client_sigalgs && !is_suiteb)
                {
                conf = c->client_sigalgs;
                conflen = c->client_sigalgslen;
                }
-       else if (c->conf_sigalgs)
+       else if (c->conf_sigalgs && !is_suiteb)
                {
                conf = c->conf_sigalgs;
                conflen = c->conf_sigalgslen;
                }
        else
-               {
-               conf = tls12_sigalgs;
-               conflen = sizeof(tls12_sigalgs);
-#ifdef OPENSSL_FIPS
-               if (FIPS_mode())
-                       conflen -= 2;
-#endif
-               }
-       if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
+               conflen = tls12_get_psigalgs(s, &conf);
+       if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb)
                {
                pref = conf;
                preflen = conflen;
@@ -3216,8 +3700,8 @@ int tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize)
        const EVP_MD *md;
        CERT *c = s->cert;
        TLS_SIGALGS *sigptr;
-       /* Extension ignored for TLS versions below 1.2 */
-       if (TLS1_get_version(s) < TLS1_2_VERSION)
+       /* Extension ignored for inappropriate versions */
+       if (!SSL_USE_SIGALGS(s))
                return 1;
        /* Should never happen */
        if (!c)
@@ -3231,6 +3715,32 @@ int tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize)
 
        tls1_set_shared_sigalgs(s);
 
+#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
+       if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
+               {
+               /* Use first set signature preference to force message
+                * digest, ignoring any peer preferences.
+                */
+               const unsigned char *sigs = NULL;
+               if (s->server)
+                       sigs = c->conf_sigalgs;
+               else
+                       sigs = c->client_sigalgs;
+               if (sigs)
+                       {
+                       idx = tls12_get_pkey_idx(sigs[1]);
+                       md = tls12_get_hash(sigs[0]);
+                       c->pkeys[idx].digest = md;
+                       c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
+                       if (idx == SSL_PKEY_RSA_SIGN)
+                               {
+                               c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
+                               c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
+                               }
+                       }
+               }
+#endif
+
        for (i = 0, sigptr = c->shared_sigalgs;
                        i < c->shared_sigalgslen; i++, sigptr++)
                {
@@ -3251,7 +3761,7 @@ int tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize)
        /* In strict mode leave unset digests as NULL to indicate we can't
         * use the certificate for signing.
         */
-       if (!(s->cert->cert_flags & SSL_CERT_FLAG_TLS_STRICT))
+       if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
                {
                /* Set any remaining keys to default values. NOTE: if alg is
                 * not supported it stays as NULL.
@@ -3526,6 +4036,8 @@ int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
        sig.sigalgcnt = 0;
        if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
                return 0;
+       if (c == NULL)
+               return 1;
        return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
        }
 
@@ -3625,17 +4137,34 @@ int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
        int check_flags = 0, strict_mode;
        CERT_PKEY *cpk = NULL;
        CERT *c = s->cert;
-       /* idx != -1 means checking server chains */
+       unsigned int suiteb_flags = tls1_suiteb(s);
+       /* idx == -1 means checking server chains */
        if (idx != -1)
                {
-               cpk = c->pkeys + idx;
+               /* idx == -2 means checking client certificate chains */
+               if (idx == -2)
+                       {
+                       cpk = c->key;
+                       idx = cpk - c->pkeys;
+                       }
+               else
+                       cpk = c->pkeys + idx;
                x = cpk->x509;
                pk = cpk->privatekey;
                chain = cpk->chain;
-               strict_mode = c->cert_flags & SSL_CERT_FLAG_TLS_STRICT;
+               strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
                /* If no cert or key, forget it */
                if (!x || !pk)
                        goto end;
+#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
+               /* Allow any certificate to pass test */
+               if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
+                       {
+                       rv = CERT_PKEY_STRICT_FLAGS|CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_VALID|CERT_PKEY_SIGN;
+                       cpk->valid_flags = rv;
+                       return rv;
+                       }
+#endif
                }
        else
                {
@@ -3645,13 +4174,27 @@ int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
                if (idx == -1)
                        goto end;
                cpk = c->pkeys + idx;
-               if (c->cert_flags & SSL_CERT_FLAG_TLS_STRICT)
+               if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
                        check_flags = CERT_PKEY_STRICT_FLAGS;
                else
                        check_flags = CERT_PKEY_VALID_FLAGS;
                strict_mode = 1;
                }
 
+       if (suiteb_flags)
+               {
+               int ok;
+               if (check_flags)
+                       check_flags |= CERT_PKEY_SUITEB;
+               ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
+               if (ok != X509_V_OK)
+                       {
+                       if (check_flags)
+                               rv |= CERT_PKEY_SUITEB;
+                       else
+                               goto end;
+                       }
+               }
 
        /* Check all signature algorithms are consistent with
         * signature algorithms extension if TLS 1.2 or later
@@ -3739,8 +4282,8 @@ int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
        else if(check_flags)
                rv |= CERT_PKEY_EE_SIGNATURE|CERT_PKEY_CA_SIGNATURE;
        skip_sigs:
-       /* Check cert parameters are consistent: server certs only */
-       if (!s->server || tls1_check_cert_param(s, x))
+       /* Check cert parameters are consistent */
+       if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
                rv |= CERT_PKEY_EE_PARAM;
        else if (!check_flags)
                goto end;
@@ -3752,7 +4295,8 @@ int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
                rv |= CERT_PKEY_CA_PARAM;
                for (i = 0; i < sk_X509_num(chain); i++)
                        {
-                       if (!tls1_check_cert_param(s, sk_X509_value(chain, i)))
+                       X509 *ca = sk_X509_value(chain, i);
+                       if (!tls1_check_cert_param(s, ca, 0))
                                {
                                if (check_flags)
                                        {
@@ -3869,7 +4413,11 @@ int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
                if (rv & CERT_PKEY_VALID)
                        cpk->valid_flags = rv;
                else
-                       cpk->valid_flags = 0;
+                       {
+                       /* Preserve explicit sign flag, clear rest */
+                       cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
+                       return 0;
+                       }
                }
        return rv;
        }