Merged the hostap_2.6 updates, and the Leap of Faith work, from the hostap_update...
[mech_eap.git] / libeap / src / tls / tlsv1_client_write.c
index b47425f..04d895e 100644 (file)
@@ -1,15 +1,9 @@
 /*
  * TLSv1 client - write handshake message
- * Copyright (c) 2006-2007, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2006-2015, Jouni Malinen <j@w1.fi>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
  */
 
 #include "includes.h"
@@ -17,7 +11,9 @@
 #include "common.h"
 #include "crypto/md5.h"
 #include "crypto/sha1.h"
+#include "crypto/sha256.h"
 #include "crypto/tls.h"
+#include "crypto/random.h"
 #include "x509v3.h"
 #include "tlsv1_common.h"
 #include "tlsv1_record.h"
@@ -51,13 +47,33 @@ u8 * tls_send_client_hello(struct tlsv1_client *conn, size_t *out_len)
        u8 *hello, *end, *pos, *hs_length, *hs_start, *rhdr;
        struct os_time now;
        size_t len, i;
+       u8 *ext_start;
+       u16 tls_version = TLS_VERSION;
+
+       /* Pick the highest locally enabled TLS version */
+#ifdef CONFIG_TLSV12
+       if ((conn->flags & TLS_CONN_DISABLE_TLSv1_2) &&
+           tls_version == TLS_VERSION_1_2)
+               tls_version = TLS_VERSION_1_1;
+#endif /* CONFIG_TLSV12 */
+#ifdef CONFIG_TLSV11
+       if ((conn->flags & TLS_CONN_DISABLE_TLSv1_1) &&
+           tls_version == TLS_VERSION_1_1)
+               tls_version = TLS_VERSION_1;
+#endif /* CONFIG_TLSV11 */
+       if ((conn->flags & TLS_CONN_DISABLE_TLSv1_0) &&
+           tls_version == TLS_VERSION_1) {
+               wpa_printf(MSG_INFO, "TLSv1: No TLS version allowed");
+               return NULL;
+       }
 
-       wpa_printf(MSG_DEBUG, "TLSv1: Send ClientHello");
+       wpa_printf(MSG_DEBUG, "TLSv1: Send ClientHello (ver %s)",
+                  tls_version_str(tls_version));
        *out_len = 0;
 
        os_get_time(&now);
        WPA_PUT_BE32(conn->client_random, now.sec);
-       if (os_get_random(conn->client_random + 4, TLS_RANDOM_LEN - 4)) {
+       if (random_get_bytes(conn->client_random + 4, TLS_RANDOM_LEN - 4)) {
                wpa_printf(MSG_ERROR, "TLSv1: Could not generate "
                           "client_random");
                return NULL;
@@ -65,7 +81,7 @@ u8 * tls_send_client_hello(struct tlsv1_client *conn, size_t *out_len)
        wpa_hexdump(MSG_MSGDUMP, "TLSv1: client_random",
                    conn->client_random, TLS_RANDOM_LEN);
 
-       len = 100 + conn->num_cipher_suites * 2 + conn->client_hello_ext_len;
+       len = 150 + conn->num_cipher_suites * 2 + conn->client_hello_ext_len;
        hello = os_malloc(len);
        if (hello == NULL)
                return NULL;
@@ -85,7 +101,7 @@ u8 * tls_send_client_hello(struct tlsv1_client *conn, size_t *out_len)
        pos += 3;
        /* body - ClientHello */
        /* ProtocolVersion client_version */
-       WPA_PUT_BE16(pos, TLS_VERSION);
+       WPA_PUT_BE16(pos, tls_version);
        pos += 2;
        /* Random random: uint32 gmt_unix_time, opaque random_bytes */
        os_memcpy(pos, conn->client_random, TLS_RANDOM_LEN);
@@ -105,17 +121,130 @@ u8 * tls_send_client_hello(struct tlsv1_client *conn, size_t *out_len)
        *pos++ = 1;
        *pos++ = TLS_COMPRESSION_NULL;
 
+       /* Extension */
+       ext_start = pos;
+       pos += 2;
+
+#ifdef CONFIG_TLSV12
+       if (conn->rl.tls_version >= TLS_VERSION_1_2) {
+               /*
+                * Add signature_algorithms extension since we support only
+                * SHA256 (and not the default SHA1) with TLSv1.2.
+                */
+               /* ExtensionsType extension_type = signature_algorithms(13) */
+               WPA_PUT_BE16(pos, TLS_EXT_SIGNATURE_ALGORITHMS);
+               pos += 2;
+               /* opaque extension_data<0..2^16-1> length */
+               WPA_PUT_BE16(pos, 8);
+               pos += 2;
+               /* supported_signature_algorithms<2..2^16-2> length */
+               WPA_PUT_BE16(pos, 6);
+               pos += 2;
+               /* supported_signature_algorithms */
+               *pos++ = TLS_HASH_ALG_SHA512;
+               *pos++ = TLS_SIGN_ALG_RSA;
+               *pos++ = TLS_HASH_ALG_SHA384;
+               *pos++ = TLS_SIGN_ALG_RSA;
+               *pos++ = TLS_HASH_ALG_SHA256;
+               *pos++ = TLS_SIGN_ALG_RSA;
+       }
+#endif /* CONFIG_TLSV12 */
+
        if (conn->client_hello_ext) {
                os_memcpy(pos, conn->client_hello_ext,
                          conn->client_hello_ext_len);
                pos += conn->client_hello_ext_len;
        }
 
+       if (conn->flags & TLS_CONN_REQUEST_OCSP) {
+               wpa_printf(MSG_DEBUG,
+                          "TLSv1: Add status_request extension for OCSP stapling");
+               /* ExtensionsType extension_type = status_request(5) */
+               WPA_PUT_BE16(pos, TLS_EXT_STATUS_REQUEST);
+               pos += 2;
+               /* opaque extension_data<0..2^16-1> length */
+               WPA_PUT_BE16(pos, 5);
+               pos += 2;
+
+               /*
+                * RFC 6066, 8:
+                * struct {
+                *     CertificateStatusType status_type;
+                *     select (status_type) {
+                *         case ocsp: OCSPStatusRequest;
+                *     } request;
+                * } CertificateStatusRequest;
+                *
+                * enum { ocsp(1), (255) } CertificateStatusType;
+                */
+               *pos++ = 1; /* status_type = ocsp(1) */
+
+               /*
+                * struct {
+                *     ResponderID responder_id_list<0..2^16-1>;
+                *     Extensions  request_extensions;
+                * } OCSPStatusRequest;
+                *
+                * opaque ResponderID<1..2^16-1>;
+                * opaque Extensions<0..2^16-1>;
+                */
+               WPA_PUT_BE16(pos, 0); /* responder_id_list(empty) */
+               pos += 2;
+               WPA_PUT_BE16(pos, 0); /* request_extensions(empty) */
+               pos += 2;
+
+               wpa_printf(MSG_DEBUG,
+                          "TLSv1: Add status_request_v2 extension for OCSP stapling");
+               /* ExtensionsType extension_type = status_request_v2(17) */
+               WPA_PUT_BE16(pos, TLS_EXT_STATUS_REQUEST_V2);
+               pos += 2;
+               /* opaque extension_data<0..2^16-1> length */
+               WPA_PUT_BE16(pos, 7);
+               pos += 2;
+
+               /*
+                * RFC 6961, 2.2:
+                * struct {
+                *     CertificateStatusType status_type;
+                *     uint16 request_length;
+                *     select (status_type) {
+                *         case ocsp: OCSPStatusRequest;
+                *         case ocsp_multi: OCSPStatusRequest;
+                *     } request;
+                * } CertificateStatusRequestItemV2;
+                *
+                * enum { ocsp(1), ocsp_multi(2), (255) } CertificateStatusType;
+                *
+                * struct {
+                * CertificateStatusRequestItemV2
+                *     certificate_status_req_list<1..2^16-1>;
+                * } CertificateStatusRequestListV2;
+                */
+
+               /* certificate_status_req_list<1..2^16-1> */
+               WPA_PUT_BE16(pos, 5);
+               pos += 2;
+
+               /* CertificateStatusRequestItemV2 */
+               *pos++ = 2; /* status_type = ocsp_multi(2) */
+               /* OCSPStatusRequest as shown above for v1 */
+               WPA_PUT_BE16(pos, 0); /* responder_id_list(empty) */
+               pos += 2;
+               WPA_PUT_BE16(pos, 0); /* request_extensions(empty) */
+               pos += 2;
+       }
+
+       if (pos == ext_start + 2)
+               pos -= 2; /* no extensions */
+       else
+               WPA_PUT_BE16(ext_start, pos - ext_start - 2);
+
        WPA_PUT_BE24(hs_length, pos - hs_length - 3);
        tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
 
        if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
-                             rhdr, end - rhdr, pos - hs_start, out_len) < 0) {
+                             rhdr, end - rhdr, hs_start, pos - hs_start,
+                             out_len) < 0) {
                wpa_printf(MSG_DEBUG, "TLSv1: Failed to create TLS record");
                tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
                          TLS_ALERT_INTERNAL_ERROR);
@@ -137,6 +266,11 @@ static int tls_write_client_certificate(struct tlsv1_client *conn,
        struct x509_certificate *cert;
 
        pos = *msgpos;
+       if (TLS_RECORD_HEADER_LEN + 1 + 3 + 3 > end - pos) {
+               tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
+                         TLS_ALERT_INTERNAL_ERROR);
+               return -1;
+       }
 
        wpa_printf(MSG_DEBUG, "TLSv1: Send Certificate");
        rhdr = pos;
@@ -157,7 +291,7 @@ static int tls_write_client_certificate(struct tlsv1_client *conn,
        pos += 3;
        cert = conn->cred ? conn->cred->cert : NULL;
        while (cert) {
-               if (pos + 3 + cert->cert_len > end) {
+               if (3 + cert->cert_len > (size_t) (end - pos)) {
                        wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space "
                                   "for Certificate (cert_len=%lu left=%lu)",
                                   (unsigned long) cert->cert_len,
@@ -191,7 +325,8 @@ static int tls_write_client_certificate(struct tlsv1_client *conn,
        WPA_PUT_BE24(hs_length, pos - hs_length - 3);
 
        if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
-                             rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
+                             rhdr, end - rhdr, hs_start, pos - hs_start,
+                             &rlen) < 0) {
                wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
                tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
                          TLS_ALERT_INTERNAL_ERROR);
@@ -207,7 +342,7 @@ static int tls_write_client_certificate(struct tlsv1_client *conn,
 }
 
 
-static int tlsv1_key_x_anon_dh(struct tlsv1_client *conn, u8 **pos, u8 *end)
+static int tlsv1_key_x_dh(struct tlsv1_client *conn, u8 **pos, u8 *end)
 {
        /* ClientDiffieHellmanPublic */
        u8 *csecret, *csecret_start, *dh_yc, *shared;
@@ -222,7 +357,7 @@ static int tlsv1_key_x_anon_dh(struct tlsv1_client *conn, u8 **pos, u8 *end)
                          TLS_ALERT_INTERNAL_ERROR);
                return -1;
        }
-       if (os_get_random(csecret, csecret_len)) {
+       if (random_get_bytes(csecret, csecret_len)) {
                wpa_printf(MSG_DEBUG, "TLSv1: Failed to get random "
                           "data for Diffie-Hellman");
                tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
@@ -267,9 +402,16 @@ static int tlsv1_key_x_anon_dh(struct tlsv1_client *conn, u8 **pos, u8 *end)
        wpa_hexdump(MSG_DEBUG, "TLSv1: DH Yc (client's public value)",
                    dh_yc, dh_yc_len);
 
+       if (end - *pos < 2) {
+               tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
+                         TLS_ALERT_INTERNAL_ERROR);
+               os_free(csecret);
+               os_free(dh_yc);
+               return -1;
+       }
        WPA_PUT_BE16(*pos, dh_yc_len);
        *pos += 2;
-       if (*pos + dh_yc_len > end) {
+       if (dh_yc_len > (size_t) (end - *pos)) {
                wpa_printf(MSG_DEBUG, "TLSv1: Not enough room in the "
                           "message buffer for Yc");
                tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
@@ -401,8 +543,8 @@ static int tls_write_client_key_exchange(struct tlsv1_client *conn,
        hs_length = pos;
        pos += 3;
        /* body - ClientKeyExchange */
-       if (keyx == TLS_KEY_X_DH_anon) {
-               if (tlsv1_key_x_anon_dh(conn, &pos, end) < 0)
+       if (keyx == TLS_KEY_X_DH_anon || keyx == TLS_KEY_X_DHE_RSA) {
+               if (tlsv1_key_x_dh(conn, &pos, end) < 0)
                        return -1;
        } else {
                if (tlsv1_key_x_rsa(conn, &pos, end) < 0)
@@ -412,7 +554,8 @@ static int tls_write_client_key_exchange(struct tlsv1_client *conn,
        WPA_PUT_BE24(hs_length, pos - hs_length - 3);
 
        if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
-                             rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
+                             rhdr, end - rhdr, hs_start, pos - hs_start,
+                             &rlen) < 0) {
                wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
                tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
                          TLS_ALERT_INTERNAL_ERROR);
@@ -432,8 +575,7 @@ static int tls_write_client_certificate_verify(struct tlsv1_client *conn,
 {
        u8 *pos, *rhdr, *hs_start, *hs_length, *signed_start;
        size_t rlen, hlen, clen;
-       u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN], *hpos;
-       enum { SIGN_ALG_RSA, SIGN_ALG_DSA } alg = SIGN_ALG_RSA;
+       u8 hash[100], *hpos;
 
        pos = *msgpos;
 
@@ -472,21 +614,51 @@ static int tls_write_client_certificate_verify(struct tlsv1_client *conn,
 
        hpos = hash;
 
-       if (alg == SIGN_ALG_RSA) {
-               hlen = MD5_MAC_LEN;
-               if (conn->verify.md5_cert == NULL ||
-                   crypto_hash_finish(conn->verify.md5_cert, hpos, &hlen) < 0)
-               {
+#ifdef CONFIG_TLSV12
+       if (conn->rl.tls_version == TLS_VERSION_1_2) {
+               hlen = SHA256_MAC_LEN;
+               if (conn->verify.sha256_cert == NULL ||
+                   crypto_hash_finish(conn->verify.sha256_cert, hpos, &hlen) <
+                   0) {
+                       conn->verify.sha256_cert = NULL;
                        tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
                                  TLS_ALERT_INTERNAL_ERROR);
-                       conn->verify.md5_cert = NULL;
-                       crypto_hash_finish(conn->verify.sha1_cert, NULL, NULL);
-                       conn->verify.sha1_cert = NULL;
                        return -1;
                }
-               hpos += MD5_MAC_LEN;
-       } else
-               crypto_hash_finish(conn->verify.md5_cert, NULL, NULL);
+               conn->verify.sha256_cert = NULL;
+
+               /*
+                * RFC 3447, A.2.4 RSASSA-PKCS1-v1_5
+                *
+                * DigestInfo ::= SEQUENCE {
+                *   digestAlgorithm DigestAlgorithm,
+                *   digest OCTET STRING
+                * }
+                *
+                * SHA-256 OID: sha256WithRSAEncryption ::= {pkcs-1 11}
+                *
+                * DER encoded DigestInfo for SHA256 per RFC 3447:
+                * 30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05 00 04 20 ||
+                * H
+                */
+               os_memmove(hash + 19, hash, hlen);
+               hlen += 19;
+               os_memcpy(hash, "\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65"
+                         "\x03\x04\x02\x01\x05\x00\x04\x20", 19);
+       } else {
+#endif /* CONFIG_TLSV12 */
+
+       hlen = MD5_MAC_LEN;
+       if (conn->verify.md5_cert == NULL ||
+           crypto_hash_finish(conn->verify.md5_cert, hpos, &hlen) < 0) {
+               tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
+                         TLS_ALERT_INTERNAL_ERROR);
+               conn->verify.md5_cert = NULL;
+               crypto_hash_finish(conn->verify.sha1_cert, NULL, NULL);
+               conn->verify.sha1_cert = NULL;
+               return -1;
+       }
+       hpos += MD5_MAC_LEN;
 
        conn->verify.md5_cert = NULL;
        hlen = SHA1_MAC_LEN;
@@ -499,11 +671,31 @@ static int tls_write_client_certificate_verify(struct tlsv1_client *conn,
        }
        conn->verify.sha1_cert = NULL;
 
-       if (alg == SIGN_ALG_RSA)
-               hlen += MD5_MAC_LEN;
+       hlen += MD5_MAC_LEN;
+
+#ifdef CONFIG_TLSV12
+       }
+#endif /* CONFIG_TLSV12 */
 
        wpa_hexdump(MSG_MSGDUMP, "TLSv1: CertificateVerify hash", hash, hlen);
 
+#ifdef CONFIG_TLSV12
+       if (conn->rl.tls_version >= TLS_VERSION_1_2) {
+               /*
+                * RFC 5246, 4.7:
+                * TLS v1.2 adds explicit indication of the used signature and
+                * hash algorithms.
+                *
+                * struct {
+                *   HashAlgorithm hash;
+                *   SignatureAlgorithm signature;
+                * } SignatureAndHashAlgorithm;
+                */
+               *pos++ = TLS_HASH_ALG_SHA256;
+               *pos++ = TLS_SIGN_ALG_RSA;
+       }
+#endif /* CONFIG_TLSV12 */
+
        /*
         * RFC 2246, 4.7:
         * In digital signing, one-way hash functions are used as input for a
@@ -533,7 +725,8 @@ static int tls_write_client_certificate_verify(struct tlsv1_client *conn,
        WPA_PUT_BE24(hs_length, pos - hs_length - 3);
 
        if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
-                             rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
+                             rhdr, end - rhdr, hs_start, pos - hs_start,
+                             &rlen) < 0) {
                wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
                tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
                          TLS_ALERT_INTERNAL_ERROR);
@@ -552,17 +745,16 @@ static int tls_write_client_certificate_verify(struct tlsv1_client *conn,
 static int tls_write_client_change_cipher_spec(struct tlsv1_client *conn,
                                               u8 **msgpos, u8 *end)
 {
-       u8 *pos, *rhdr;
        size_t rlen;
-
-       pos = *msgpos;
+       u8 payload[1];
 
        wpa_printf(MSG_DEBUG, "TLSv1: Send ChangeCipherSpec");
-       rhdr = pos;
-       pos += TLS_RECORD_HEADER_LEN;
-       *pos = TLS_CHANGE_CIPHER_SPEC;
+
+       payload[0] = TLS_CHANGE_CIPHER_SPEC;
+
        if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC,
-                             rhdr, end - rhdr, 1, &rlen) < 0) {
+                             *msgpos, end - *msgpos, payload, sizeof(payload),
+                             &rlen) < 0) {
                wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
                tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
                          TLS_ALERT_INTERNAL_ERROR);
@@ -577,7 +769,7 @@ static int tls_write_client_change_cipher_spec(struct tlsv1_client *conn,
                return -1;
        }
 
-       *msgpos = rhdr + rlen;
+       *msgpos += rlen;
 
        return 0;
 }
@@ -586,17 +778,30 @@ static int tls_write_client_change_cipher_spec(struct tlsv1_client *conn,
 static int tls_write_client_finished(struct tlsv1_client *conn,
                                     u8 **msgpos, u8 *end)
 {
-       u8 *pos, *rhdr, *hs_start, *hs_length;
+       u8 *pos, *hs_start;
        size_t rlen, hlen;
-       u8 verify_data[TLS_VERIFY_DATA_LEN];
+       u8 verify_data[1 + 3 + TLS_VERIFY_DATA_LEN];
        u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
 
-       pos = *msgpos;
-
        wpa_printf(MSG_DEBUG, "TLSv1: Send Finished");
 
        /* Encrypted Handshake Message: Finished */
 
+#ifdef CONFIG_TLSV12
+       if (conn->rl.tls_version >= TLS_VERSION_1_2) {
+               hlen = SHA256_MAC_LEN;
+               if (conn->verify.sha256_client == NULL ||
+                   crypto_hash_finish(conn->verify.sha256_client, hash, &hlen)
+                   < 0) {
+                       tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
+                                 TLS_ALERT_INTERNAL_ERROR);
+                       conn->verify.sha256_client = NULL;
+                       return -1;
+               }
+               conn->verify.sha256_client = NULL;
+       } else {
+#endif /* CONFIG_TLSV12 */
+
        hlen = MD5_MAC_LEN;
        if (conn->verify.md5_client == NULL ||
            crypto_hash_finish(conn->verify.md5_client, hash, &hlen) < 0) {
@@ -618,43 +823,44 @@ static int tls_write_client_finished(struct tlsv1_client *conn,
                return -1;
        }
        conn->verify.sha1_client = NULL;
+       hlen = MD5_MAC_LEN + SHA1_MAC_LEN;
 
-       if (tls_prf(conn->master_secret, TLS_MASTER_SECRET_LEN,
-                   "client finished", hash, MD5_MAC_LEN + SHA1_MAC_LEN,
-                   verify_data, TLS_VERIFY_DATA_LEN)) {
+#ifdef CONFIG_TLSV12
+       }
+#endif /* CONFIG_TLSV12 */
+
+       if (tls_prf(conn->rl.tls_version,
+                   conn->master_secret, TLS_MASTER_SECRET_LEN,
+                   "client finished", hash, hlen,
+                   verify_data + 1 + 3, TLS_VERIFY_DATA_LEN)) {
                wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate verify_data");
                tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
                          TLS_ALERT_INTERNAL_ERROR);
                return -1;
        }
        wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (client)",
-                       verify_data, TLS_VERIFY_DATA_LEN);
+                       verify_data + 1 + 3, TLS_VERIFY_DATA_LEN);
 
-       rhdr = pos;
-       pos += TLS_RECORD_HEADER_LEN;
        /* Handshake */
-       hs_start = pos;
+       pos = hs_start = verify_data;
        /* HandshakeType msg_type */
        *pos++ = TLS_HANDSHAKE_TYPE_FINISHED;
-       /* uint24 length (to be filled) */
-       hs_length = pos;
+       /* uint24 length */
+       WPA_PUT_BE24(pos, TLS_VERIFY_DATA_LEN);
        pos += 3;
-       os_memcpy(pos, verify_data, TLS_VERIFY_DATA_LEN);
-       pos += TLS_VERIFY_DATA_LEN;
-       WPA_PUT_BE24(hs_length, pos - hs_length - 3);
+       pos += TLS_VERIFY_DATA_LEN; /* verify_data already in place */
        tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
 
        if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
-                             rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
+                             *msgpos, end - *msgpos, hs_start, pos - hs_start,
+                             &rlen) < 0) {
                wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
                tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
                          TLS_ALERT_INTERNAL_ERROR);
                return -1;
        }
 
-       pos = rhdr + rlen;
-
-       *msgpos = pos;
+       *msgpos += rlen;
 
        return 0;
 }
@@ -668,7 +874,7 @@ static u8 * tls_send_client_key_exchange(struct tlsv1_client *conn,
 
        *out_len = 0;
 
-       msglen = 1000;
+       msglen = 2000;
        if (conn->certificate_requested)
                msglen += tls_client_cert_chain_der_len(conn);
 
@@ -727,6 +933,8 @@ static u8 * tls_send_change_cipher_spec(struct tlsv1_client *conn,
 
        wpa_printf(MSG_DEBUG, "TLSv1: Session resumption completed "
                   "successfully");
+       if (!conn->session_resumed && conn->use_session_ticket)
+               conn->session_resumed = 1;
        conn->state = ESTABLISHED;
 
        return msg;
@@ -777,7 +985,8 @@ u8 * tlsv1_client_send_alert(struct tlsv1_client *conn, u8 level,
        /* ContentType type */
        *pos++ = TLS_CONTENT_TYPE_ALERT;
        /* ProtocolVersion version */
-       WPA_PUT_BE16(pos, TLS_VERSION);
+       WPA_PUT_BE16(pos, conn->rl.tls_version ? conn->rl.tls_version :
+                    TLS_VERSION);
        pos += 2;
        /* uint16 length (to be filled) */
        length = pos;