2 * TLSv1 client - write handshake message
3 * Copyright (c) 2006-2015, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/md5.h"
13 #include "crypto/sha1.h"
14 #include "crypto/sha256.h"
15 #include "crypto/tls.h"
16 #include "crypto/random.h"
18 #include "tlsv1_common.h"
19 #include "tlsv1_record.h"
20 #include "tlsv1_client.h"
21 #include "tlsv1_client_i.h"
24 static size_t tls_client_cert_chain_der_len(struct tlsv1_client *conn)
27 struct x509_certificate *cert;
29 if (conn->cred == NULL)
32 cert = conn->cred->cert;
34 len += 3 + cert->cert_len;
35 if (x509_certificate_self_signed(cert))
37 cert = x509_certificate_get_subject(conn->cred->trusted_certs,
45 u8 * tls_send_client_hello(struct tlsv1_client *conn, size_t *out_len)
47 u8 *hello, *end, *pos, *hs_length, *hs_start, *rhdr;
51 u16 tls_version = TLS_VERSION;
53 /* Pick the highest locally enabled TLS version */
55 if ((conn->flags & TLS_CONN_DISABLE_TLSv1_2) &&
56 tls_version == TLS_VERSION_1_2)
57 tls_version = TLS_VERSION_1_1;
58 #endif /* CONFIG_TLSV12 */
60 if ((conn->flags & TLS_CONN_DISABLE_TLSv1_1) &&
61 tls_version == TLS_VERSION_1_1)
62 tls_version = TLS_VERSION_1;
63 #endif /* CONFIG_TLSV11 */
64 if ((conn->flags & TLS_CONN_DISABLE_TLSv1_0) &&
65 tls_version == TLS_VERSION_1) {
66 wpa_printf(MSG_INFO, "TLSv1: No TLS version allowed");
70 wpa_printf(MSG_DEBUG, "TLSv1: Send ClientHello (ver %s)",
71 tls_version_str(tls_version));
75 WPA_PUT_BE32(conn->client_random, now.sec);
76 if (random_get_bytes(conn->client_random + 4, TLS_RANDOM_LEN - 4)) {
77 wpa_printf(MSG_ERROR, "TLSv1: Could not generate "
81 wpa_hexdump(MSG_MSGDUMP, "TLSv1: client_random",
82 conn->client_random, TLS_RANDOM_LEN);
84 len = 150 + conn->num_cipher_suites * 2 + conn->client_hello_ext_len;
85 hello = os_malloc(len);
91 pos = rhdr + TLS_RECORD_HEADER_LEN;
93 /* opaque fragment[TLSPlaintext.length] */
97 /* HandshakeType msg_type */
98 *pos++ = TLS_HANDSHAKE_TYPE_CLIENT_HELLO;
99 /* uint24 length (to be filled) */
102 /* body - ClientHello */
103 /* ProtocolVersion client_version */
104 WPA_PUT_BE16(pos, tls_version);
106 /* Random random: uint32 gmt_unix_time, opaque random_bytes */
107 os_memcpy(pos, conn->client_random, TLS_RANDOM_LEN);
108 pos += TLS_RANDOM_LEN;
109 /* SessionID session_id */
110 *pos++ = conn->session_id_len;
111 os_memcpy(pos, conn->session_id, conn->session_id_len);
112 pos += conn->session_id_len;
113 /* CipherSuite cipher_suites<2..2^16-1> */
114 WPA_PUT_BE16(pos, 2 * conn->num_cipher_suites);
116 for (i = 0; i < conn->num_cipher_suites; i++) {
117 WPA_PUT_BE16(pos, conn->cipher_suites[i]);
120 /* CompressionMethod compression_methods<1..2^8-1> */
122 *pos++ = TLS_COMPRESSION_NULL;
129 if (conn->rl.tls_version >= TLS_VERSION_1_2) {
131 * Add signature_algorithms extension since we support only
132 * SHA256 (and not the default SHA1) with TLSv1.2.
134 /* ExtensionsType extension_type = signature_algorithms(13) */
135 WPA_PUT_BE16(pos, TLS_EXT_SIGNATURE_ALGORITHMS);
137 /* opaque extension_data<0..2^16-1> length */
138 WPA_PUT_BE16(pos, 8);
140 /* supported_signature_algorithms<2..2^16-2> length */
141 WPA_PUT_BE16(pos, 6);
143 /* supported_signature_algorithms */
144 *pos++ = TLS_HASH_ALG_SHA512;
145 *pos++ = TLS_SIGN_ALG_RSA;
146 *pos++ = TLS_HASH_ALG_SHA384;
147 *pos++ = TLS_SIGN_ALG_RSA;
148 *pos++ = TLS_HASH_ALG_SHA256;
149 *pos++ = TLS_SIGN_ALG_RSA;
151 #endif /* CONFIG_TLSV12 */
153 if (conn->client_hello_ext) {
154 os_memcpy(pos, conn->client_hello_ext,
155 conn->client_hello_ext_len);
156 pos += conn->client_hello_ext_len;
159 if (conn->flags & TLS_CONN_REQUEST_OCSP) {
160 wpa_printf(MSG_DEBUG,
161 "TLSv1: Add status_request extension for OCSP stapling");
162 /* ExtensionsType extension_type = status_request(5) */
163 WPA_PUT_BE16(pos, TLS_EXT_STATUS_REQUEST);
165 /* opaque extension_data<0..2^16-1> length */
166 WPA_PUT_BE16(pos, 5);
172 * CertificateStatusType status_type;
173 * select (status_type) {
174 * case ocsp: OCSPStatusRequest;
176 * } CertificateStatusRequest;
178 * enum { ocsp(1), (255) } CertificateStatusType;
180 *pos++ = 1; /* status_type = ocsp(1) */
184 * ResponderID responder_id_list<0..2^16-1>;
185 * Extensions request_extensions;
186 * } OCSPStatusRequest;
188 * opaque ResponderID<1..2^16-1>;
189 * opaque Extensions<0..2^16-1>;
191 WPA_PUT_BE16(pos, 0); /* responder_id_list(empty) */
193 WPA_PUT_BE16(pos, 0); /* request_extensions(empty) */
197 if (pos == ext_start + 2)
198 pos -= 2; /* no extensions */
200 WPA_PUT_BE16(ext_start, pos - ext_start - 2);
202 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
203 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
205 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
206 rhdr, end - rhdr, hs_start, pos - hs_start,
208 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create TLS record");
209 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
210 TLS_ALERT_INTERNAL_ERROR);
215 conn->state = SERVER_HELLO;
221 static int tls_write_client_certificate(struct tlsv1_client *conn,
222 u8 **msgpos, u8 *end)
224 u8 *pos, *rhdr, *hs_start, *hs_length, *cert_start;
226 struct x509_certificate *cert;
229 if (TLS_RECORD_HEADER_LEN + 1 + 3 + 3 > end - pos) {
230 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
231 TLS_ALERT_INTERNAL_ERROR);
235 wpa_printf(MSG_DEBUG, "TLSv1: Send Certificate");
237 pos += TLS_RECORD_HEADER_LEN;
239 /* opaque fragment[TLSPlaintext.length] */
243 /* HandshakeType msg_type */
244 *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE;
245 /* uint24 length (to be filled) */
248 /* body - Certificate */
249 /* uint24 length (to be filled) */
252 cert = conn->cred ? conn->cred->cert : NULL;
254 if (3 + cert->cert_len > (size_t) (end - pos)) {
255 wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space "
256 "for Certificate (cert_len=%lu left=%lu)",
257 (unsigned long) cert->cert_len,
258 (unsigned long) (end - pos));
259 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
260 TLS_ALERT_INTERNAL_ERROR);
263 WPA_PUT_BE24(pos, cert->cert_len);
265 os_memcpy(pos, cert->cert_start, cert->cert_len);
266 pos += cert->cert_len;
268 if (x509_certificate_self_signed(cert))
270 cert = x509_certificate_get_subject(conn->cred->trusted_certs,
273 if (conn->cred == NULL || cert == conn->cred->cert || cert == NULL) {
275 * Client was not configured with all the needed certificates
276 * to form a full certificate chain. The server may fail to
277 * validate the chain unless it is configured with all the
278 * missing CA certificates.
280 wpa_printf(MSG_DEBUG, "TLSv1: Full client certificate chain "
281 "not configured - validation may fail");
283 WPA_PUT_BE24(cert_start, pos - cert_start - 3);
285 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
287 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
288 rhdr, end - rhdr, hs_start, pos - hs_start,
290 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
291 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
292 TLS_ALERT_INTERNAL_ERROR);
297 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
305 static int tlsv1_key_x_dh(struct tlsv1_client *conn, u8 **pos, u8 *end)
307 /* ClientDiffieHellmanPublic */
308 u8 *csecret, *csecret_start, *dh_yc, *shared;
309 size_t csecret_len, dh_yc_len, shared_len;
311 csecret_len = conn->dh_p_len;
312 csecret = os_malloc(csecret_len);
313 if (csecret == NULL) {
314 wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
315 "memory for Yc (Diffie-Hellman)");
316 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
317 TLS_ALERT_INTERNAL_ERROR);
320 if (random_get_bytes(csecret, csecret_len)) {
321 wpa_printf(MSG_DEBUG, "TLSv1: Failed to get random "
322 "data for Diffie-Hellman");
323 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
324 TLS_ALERT_INTERNAL_ERROR);
329 if (os_memcmp(csecret, conn->dh_p, csecret_len) > 0)
330 csecret[0] = 0; /* make sure Yc < p */
332 csecret_start = csecret;
333 while (csecret_len > 1 && *csecret_start == 0) {
337 wpa_hexdump_key(MSG_DEBUG, "TLSv1: DH client's secret value",
338 csecret_start, csecret_len);
340 /* Yc = g^csecret mod p */
341 dh_yc_len = conn->dh_p_len;
342 dh_yc = os_malloc(dh_yc_len);
344 wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
345 "memory for Diffie-Hellman");
346 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
347 TLS_ALERT_INTERNAL_ERROR);
351 if (crypto_mod_exp(conn->dh_g, conn->dh_g_len,
352 csecret_start, csecret_len,
353 conn->dh_p, conn->dh_p_len,
354 dh_yc, &dh_yc_len)) {
355 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
356 TLS_ALERT_INTERNAL_ERROR);
362 wpa_hexdump(MSG_DEBUG, "TLSv1: DH Yc (client's public value)",
365 if (end - *pos < 2) {
366 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
367 TLS_ALERT_INTERNAL_ERROR);
372 WPA_PUT_BE16(*pos, dh_yc_len);
374 if (dh_yc_len > (size_t) (end - *pos)) {
375 wpa_printf(MSG_DEBUG, "TLSv1: Not enough room in the "
376 "message buffer for Yc");
377 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
378 TLS_ALERT_INTERNAL_ERROR);
383 os_memcpy(*pos, dh_yc, dh_yc_len);
387 shared_len = conn->dh_p_len;
388 shared = os_malloc(shared_len);
389 if (shared == NULL) {
390 wpa_printf(MSG_DEBUG, "TLSv1: Could not allocate memory for "
392 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
393 TLS_ALERT_INTERNAL_ERROR);
398 /* shared = Ys^csecret mod p */
399 if (crypto_mod_exp(conn->dh_ys, conn->dh_ys_len,
400 csecret_start, csecret_len,
401 conn->dh_p, conn->dh_p_len,
402 shared, &shared_len)) {
403 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
404 TLS_ALERT_INTERNAL_ERROR);
409 wpa_hexdump_key(MSG_DEBUG, "TLSv1: Shared secret from DH key exchange",
412 os_memset(csecret_start, 0, csecret_len);
414 if (tls_derive_keys(conn, shared, shared_len)) {
415 wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
416 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
417 TLS_ALERT_INTERNAL_ERROR);
421 os_memset(shared, 0, shared_len);
423 tlsv1_client_free_dh(conn);
428 static int tlsv1_key_x_rsa(struct tlsv1_client *conn, u8 **pos, u8 *end)
430 u8 pre_master_secret[TLS_PRE_MASTER_SECRET_LEN];
434 if (tls_derive_pre_master_secret(pre_master_secret) < 0 ||
435 tls_derive_keys(conn, pre_master_secret,
436 TLS_PRE_MASTER_SECRET_LEN)) {
437 wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
438 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
439 TLS_ALERT_INTERNAL_ERROR);
443 /* EncryptedPreMasterSecret */
444 if (conn->server_rsa_key == NULL) {
445 wpa_printf(MSG_DEBUG, "TLSv1: No server RSA key to "
446 "use for encrypting pre-master secret");
447 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
448 TLS_ALERT_INTERNAL_ERROR);
452 /* RSA encrypted value is encoded with PKCS #1 v1.5 block type 2. */
455 res = crypto_public_key_encrypt_pkcs1_v15(
456 conn->server_rsa_key,
457 pre_master_secret, TLS_PRE_MASTER_SECRET_LEN,
459 os_memset(pre_master_secret, 0, TLS_PRE_MASTER_SECRET_LEN);
461 wpa_printf(MSG_DEBUG, "TLSv1: RSA encryption failed");
462 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
463 TLS_ALERT_INTERNAL_ERROR);
466 WPA_PUT_BE16(*pos - 2, clen);
467 wpa_hexdump(MSG_MSGDUMP, "TLSv1: Encrypted pre_master_secret",
475 static int tls_write_client_key_exchange(struct tlsv1_client *conn,
476 u8 **msgpos, u8 *end)
478 u8 *pos, *rhdr, *hs_start, *hs_length;
480 tls_key_exchange keyx;
481 const struct tls_cipher_suite *suite;
483 suite = tls_get_cipher_suite(conn->rl.cipher_suite);
485 keyx = TLS_KEY_X_NULL;
487 keyx = suite->key_exchange;
491 wpa_printf(MSG_DEBUG, "TLSv1: Send ClientKeyExchange");
494 pos += TLS_RECORD_HEADER_LEN;
496 /* opaque fragment[TLSPlaintext.length] */
500 /* HandshakeType msg_type */
501 *pos++ = TLS_HANDSHAKE_TYPE_CLIENT_KEY_EXCHANGE;
502 /* uint24 length (to be filled) */
505 /* body - ClientKeyExchange */
506 if (keyx == TLS_KEY_X_DH_anon || keyx == TLS_KEY_X_DHE_RSA) {
507 if (tlsv1_key_x_dh(conn, &pos, end) < 0)
510 if (tlsv1_key_x_rsa(conn, &pos, end) < 0)
514 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
516 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
517 rhdr, end - rhdr, hs_start, pos - hs_start,
519 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
520 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
521 TLS_ALERT_INTERNAL_ERROR);
525 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
533 static int tls_write_client_certificate_verify(struct tlsv1_client *conn,
534 u8 **msgpos, u8 *end)
536 u8 *pos, *rhdr, *hs_start, *hs_length, *signed_start;
537 size_t rlen, hlen, clen;
542 wpa_printf(MSG_DEBUG, "TLSv1: Send CertificateVerify");
544 pos += TLS_RECORD_HEADER_LEN;
548 /* HandshakeType msg_type */
549 *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE_VERIFY;
550 /* uint24 length (to be filled) */
555 * RFC 2246: 7.4.3 and 7.4.8:
556 * Signature signature
559 * digitally-signed struct {
560 * opaque md5_hash[16];
561 * opaque sha_hash[20];
565 * digitally-signed struct {
566 * opaque sha_hash[20];
569 * The hash values are calculated over all handshake messages sent or
570 * received starting at ClientHello up to, but not including, this
571 * CertificateVerify message, including the type and length fields of
572 * the handshake messages.
578 if (conn->rl.tls_version == TLS_VERSION_1_2) {
579 hlen = SHA256_MAC_LEN;
580 if (conn->verify.sha256_cert == NULL ||
581 crypto_hash_finish(conn->verify.sha256_cert, hpos, &hlen) <
583 conn->verify.sha256_cert = NULL;
584 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
585 TLS_ALERT_INTERNAL_ERROR);
588 conn->verify.sha256_cert = NULL;
591 * RFC 3447, A.2.4 RSASSA-PKCS1-v1_5
593 * DigestInfo ::= SEQUENCE {
594 * digestAlgorithm DigestAlgorithm,
595 * digest OCTET STRING
598 * SHA-256 OID: sha256WithRSAEncryption ::= {pkcs-1 11}
600 * DER encoded DigestInfo for SHA256 per RFC 3447:
601 * 30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05 00 04 20 ||
604 os_memmove(hash + 19, hash, hlen);
606 os_memcpy(hash, "\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65"
607 "\x03\x04\x02\x01\x05\x00\x04\x20", 19);
609 #endif /* CONFIG_TLSV12 */
612 if (conn->verify.md5_cert == NULL ||
613 crypto_hash_finish(conn->verify.md5_cert, hpos, &hlen) < 0) {
614 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
615 TLS_ALERT_INTERNAL_ERROR);
616 conn->verify.md5_cert = NULL;
617 crypto_hash_finish(conn->verify.sha1_cert, NULL, NULL);
618 conn->verify.sha1_cert = NULL;
623 conn->verify.md5_cert = NULL;
625 if (conn->verify.sha1_cert == NULL ||
626 crypto_hash_finish(conn->verify.sha1_cert, hpos, &hlen) < 0) {
627 conn->verify.sha1_cert = NULL;
628 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
629 TLS_ALERT_INTERNAL_ERROR);
632 conn->verify.sha1_cert = NULL;
638 #endif /* CONFIG_TLSV12 */
640 wpa_hexdump(MSG_MSGDUMP, "TLSv1: CertificateVerify hash", hash, hlen);
643 if (conn->rl.tls_version >= TLS_VERSION_1_2) {
646 * TLS v1.2 adds explicit indication of the used signature and
650 * HashAlgorithm hash;
651 * SignatureAlgorithm signature;
652 * } SignatureAndHashAlgorithm;
654 *pos++ = TLS_HASH_ALG_SHA256;
655 *pos++ = TLS_SIGN_ALG_RSA;
657 #endif /* CONFIG_TLSV12 */
661 * In digital signing, one-way hash functions are used as input for a
662 * signing algorithm. A digitally-signed element is encoded as an
663 * opaque vector <0..2^16-1>, where the length is specified by the
664 * signing algorithm and key.
666 * In RSA signing, a 36-byte structure of two hashes (one SHA and one
667 * MD5) is signed (encrypted with the private key). It is encoded with
668 * PKCS #1 block type 0 or type 1 as described in [PKCS1].
670 signed_start = pos; /* length to be filled */
673 if (conn->cred == NULL ||
674 crypto_private_key_sign_pkcs1(conn->cred->key, hash, hlen,
676 wpa_printf(MSG_DEBUG, "TLSv1: Failed to sign hash (PKCS #1)");
677 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
678 TLS_ALERT_INTERNAL_ERROR);
681 WPA_PUT_BE16(signed_start, clen);
685 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
687 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
688 rhdr, end - rhdr, hs_start, pos - hs_start,
690 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
691 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
692 TLS_ALERT_INTERNAL_ERROR);
697 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
705 static int tls_write_client_change_cipher_spec(struct tlsv1_client *conn,
706 u8 **msgpos, u8 *end)
711 wpa_printf(MSG_DEBUG, "TLSv1: Send ChangeCipherSpec");
713 payload[0] = TLS_CHANGE_CIPHER_SPEC;
715 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC,
716 *msgpos, end - *msgpos, payload, sizeof(payload),
718 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
719 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
720 TLS_ALERT_INTERNAL_ERROR);
724 if (tlsv1_record_change_write_cipher(&conn->rl) < 0) {
725 wpa_printf(MSG_DEBUG, "TLSv1: Failed to set write cipher for "
727 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
728 TLS_ALERT_INTERNAL_ERROR);
738 static int tls_write_client_finished(struct tlsv1_client *conn,
739 u8 **msgpos, u8 *end)
743 u8 verify_data[1 + 3 + TLS_VERIFY_DATA_LEN];
744 u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
746 wpa_printf(MSG_DEBUG, "TLSv1: Send Finished");
748 /* Encrypted Handshake Message: Finished */
751 if (conn->rl.tls_version >= TLS_VERSION_1_2) {
752 hlen = SHA256_MAC_LEN;
753 if (conn->verify.sha256_client == NULL ||
754 crypto_hash_finish(conn->verify.sha256_client, hash, &hlen)
756 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
757 TLS_ALERT_INTERNAL_ERROR);
758 conn->verify.sha256_client = NULL;
761 conn->verify.sha256_client = NULL;
763 #endif /* CONFIG_TLSV12 */
766 if (conn->verify.md5_client == NULL ||
767 crypto_hash_finish(conn->verify.md5_client, hash, &hlen) < 0) {
768 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
769 TLS_ALERT_INTERNAL_ERROR);
770 conn->verify.md5_client = NULL;
771 crypto_hash_finish(conn->verify.sha1_client, NULL, NULL);
772 conn->verify.sha1_client = NULL;
775 conn->verify.md5_client = NULL;
777 if (conn->verify.sha1_client == NULL ||
778 crypto_hash_finish(conn->verify.sha1_client, hash + MD5_MAC_LEN,
780 conn->verify.sha1_client = NULL;
781 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
782 TLS_ALERT_INTERNAL_ERROR);
785 conn->verify.sha1_client = NULL;
786 hlen = MD5_MAC_LEN + SHA1_MAC_LEN;
790 #endif /* CONFIG_TLSV12 */
792 if (tls_prf(conn->rl.tls_version,
793 conn->master_secret, TLS_MASTER_SECRET_LEN,
794 "client finished", hash, hlen,
795 verify_data + 1 + 3, TLS_VERIFY_DATA_LEN)) {
796 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate verify_data");
797 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
798 TLS_ALERT_INTERNAL_ERROR);
801 wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (client)",
802 verify_data + 1 + 3, TLS_VERIFY_DATA_LEN);
805 pos = hs_start = verify_data;
806 /* HandshakeType msg_type */
807 *pos++ = TLS_HANDSHAKE_TYPE_FINISHED;
809 WPA_PUT_BE24(pos, TLS_VERIFY_DATA_LEN);
811 pos += TLS_VERIFY_DATA_LEN; /* verify_data already in place */
812 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
814 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
815 *msgpos, end - *msgpos, hs_start, pos - hs_start,
817 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
818 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
819 TLS_ALERT_INTERNAL_ERROR);
829 static u8 * tls_send_client_key_exchange(struct tlsv1_client *conn,
838 if (conn->certificate_requested)
839 msglen += tls_client_cert_chain_der_len(conn);
841 msg = os_malloc(msglen);
848 if (conn->certificate_requested) {
849 if (tls_write_client_certificate(conn, &pos, end) < 0) {
855 if (tls_write_client_key_exchange(conn, &pos, end) < 0 ||
856 (conn->certificate_requested && conn->cred && conn->cred->key &&
857 tls_write_client_certificate_verify(conn, &pos, end) < 0) ||
858 tls_write_client_change_cipher_spec(conn, &pos, end) < 0 ||
859 tls_write_client_finished(conn, &pos, end) < 0) {
864 *out_len = pos - msg;
866 conn->state = SERVER_CHANGE_CIPHER_SPEC;
872 static u8 * tls_send_change_cipher_spec(struct tlsv1_client *conn,
879 msg = os_malloc(1000);
886 if (tls_write_client_change_cipher_spec(conn, &pos, end) < 0 ||
887 tls_write_client_finished(conn, &pos, end) < 0) {
892 *out_len = pos - msg;
894 wpa_printf(MSG_DEBUG, "TLSv1: Session resumption completed "
896 if (!conn->session_resumed && conn->use_session_ticket)
897 conn->session_resumed = 1;
898 conn->state = ESTABLISHED;
904 u8 * tlsv1_client_handshake_write(struct tlsv1_client *conn, size_t *out_len,
907 switch (conn->state) {
908 case CLIENT_KEY_EXCHANGE:
909 return tls_send_client_key_exchange(conn, out_len);
910 case CHANGE_CIPHER_SPEC:
911 return tls_send_change_cipher_spec(conn, out_len);
913 wpa_printf(MSG_DEBUG, "TLSv1: Handshake completed "
915 conn->state = ESTABLISHED;
918 /* Need to return something to get final TLS ACK. */
923 wpa_printf(MSG_DEBUG, "TLSv1: Unexpected state %d while "
924 "generating reply", conn->state);
930 u8 * tlsv1_client_send_alert(struct tlsv1_client *conn, u8 level,
931 u8 description, size_t *out_len)
933 u8 *alert, *pos, *length;
935 wpa_printf(MSG_DEBUG, "TLSv1: Send Alert(%d:%d)", level, description);
938 alert = os_malloc(10);
945 /* ContentType type */
946 *pos++ = TLS_CONTENT_TYPE_ALERT;
947 /* ProtocolVersion version */
948 WPA_PUT_BE16(pos, conn->rl.tls_version ? conn->rl.tls_version :
951 /* uint16 length (to be filled) */
954 /* opaque fragment[TLSPlaintext.length] */
957 /* AlertLevel level */
959 /* AlertDescription description */
960 *pos++ = description;
962 WPA_PUT_BE16(length, pos - length - 2);
963 *out_len = pos - alert;