2 * TLSv1 client - write handshake message
3 * Copyright (c) 2006-2011, 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 wpa_printf(MSG_DEBUG, "TLSv1: Send ClientHello");
55 WPA_PUT_BE32(conn->client_random, now.sec);
56 if (random_get_bytes(conn->client_random + 4, TLS_RANDOM_LEN - 4)) {
57 wpa_printf(MSG_ERROR, "TLSv1: Could not generate "
61 wpa_hexdump(MSG_MSGDUMP, "TLSv1: client_random",
62 conn->client_random, TLS_RANDOM_LEN);
64 len = 100 + conn->num_cipher_suites * 2 + conn->client_hello_ext_len;
65 hello = os_malloc(len);
71 pos = rhdr + TLS_RECORD_HEADER_LEN;
73 /* opaque fragment[TLSPlaintext.length] */
77 /* HandshakeType msg_type */
78 *pos++ = TLS_HANDSHAKE_TYPE_CLIENT_HELLO;
79 /* uint24 length (to be filled) */
82 /* body - ClientHello */
83 /* ProtocolVersion client_version */
84 WPA_PUT_BE16(pos, TLS_VERSION);
86 /* Random random: uint32 gmt_unix_time, opaque random_bytes */
87 os_memcpy(pos, conn->client_random, TLS_RANDOM_LEN);
88 pos += TLS_RANDOM_LEN;
89 /* SessionID session_id */
90 *pos++ = conn->session_id_len;
91 os_memcpy(pos, conn->session_id, conn->session_id_len);
92 pos += conn->session_id_len;
93 /* CipherSuite cipher_suites<2..2^16-1> */
94 WPA_PUT_BE16(pos, 2 * conn->num_cipher_suites);
96 for (i = 0; i < conn->num_cipher_suites; i++) {
97 WPA_PUT_BE16(pos, conn->cipher_suites[i]);
100 /* CompressionMethod compression_methods<1..2^8-1> */
102 *pos++ = TLS_COMPRESSION_NULL;
104 if (conn->client_hello_ext) {
105 os_memcpy(pos, conn->client_hello_ext,
106 conn->client_hello_ext_len);
107 pos += conn->client_hello_ext_len;
110 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
111 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
113 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
114 rhdr, end - rhdr, hs_start, pos - hs_start,
116 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create TLS record");
117 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
118 TLS_ALERT_INTERNAL_ERROR);
123 conn->state = SERVER_HELLO;
129 static int tls_write_client_certificate(struct tlsv1_client *conn,
130 u8 **msgpos, u8 *end)
132 u8 *pos, *rhdr, *hs_start, *hs_length, *cert_start;
134 struct x509_certificate *cert;
138 wpa_printf(MSG_DEBUG, "TLSv1: Send Certificate");
140 pos += TLS_RECORD_HEADER_LEN;
142 /* opaque fragment[TLSPlaintext.length] */
146 /* HandshakeType msg_type */
147 *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE;
148 /* uint24 length (to be filled) */
151 /* body - Certificate */
152 /* uint24 length (to be filled) */
155 cert = conn->cred ? conn->cred->cert : NULL;
157 if (pos + 3 + cert->cert_len > end) {
158 wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space "
159 "for Certificate (cert_len=%lu left=%lu)",
160 (unsigned long) cert->cert_len,
161 (unsigned long) (end - pos));
162 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
163 TLS_ALERT_INTERNAL_ERROR);
166 WPA_PUT_BE24(pos, cert->cert_len);
168 os_memcpy(pos, cert->cert_start, cert->cert_len);
169 pos += cert->cert_len;
171 if (x509_certificate_self_signed(cert))
173 cert = x509_certificate_get_subject(conn->cred->trusted_certs,
176 if (conn->cred == NULL || cert == conn->cred->cert || cert == NULL) {
178 * Client was not configured with all the needed certificates
179 * to form a full certificate chain. The server may fail to
180 * validate the chain unless it is configured with all the
181 * missing CA certificates.
183 wpa_printf(MSG_DEBUG, "TLSv1: Full client certificate chain "
184 "not configured - validation may fail");
186 WPA_PUT_BE24(cert_start, pos - cert_start - 3);
188 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
190 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
191 rhdr, end - rhdr, hs_start, pos - hs_start,
193 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
194 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
195 TLS_ALERT_INTERNAL_ERROR);
200 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
208 static int tlsv1_key_x_anon_dh(struct tlsv1_client *conn, u8 **pos, u8 *end)
210 /* ClientDiffieHellmanPublic */
211 u8 *csecret, *csecret_start, *dh_yc, *shared;
212 size_t csecret_len, dh_yc_len, shared_len;
214 csecret_len = conn->dh_p_len;
215 csecret = os_malloc(csecret_len);
216 if (csecret == NULL) {
217 wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
218 "memory for Yc (Diffie-Hellman)");
219 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
220 TLS_ALERT_INTERNAL_ERROR);
223 if (random_get_bytes(csecret, csecret_len)) {
224 wpa_printf(MSG_DEBUG, "TLSv1: Failed to get random "
225 "data for Diffie-Hellman");
226 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
227 TLS_ALERT_INTERNAL_ERROR);
232 if (os_memcmp(csecret, conn->dh_p, csecret_len) > 0)
233 csecret[0] = 0; /* make sure Yc < p */
235 csecret_start = csecret;
236 while (csecret_len > 1 && *csecret_start == 0) {
240 wpa_hexdump_key(MSG_DEBUG, "TLSv1: DH client's secret value",
241 csecret_start, csecret_len);
243 /* Yc = g^csecret mod p */
244 dh_yc_len = conn->dh_p_len;
245 dh_yc = os_malloc(dh_yc_len);
247 wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
248 "memory for Diffie-Hellman");
249 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
250 TLS_ALERT_INTERNAL_ERROR);
254 if (crypto_mod_exp(conn->dh_g, conn->dh_g_len,
255 csecret_start, csecret_len,
256 conn->dh_p, conn->dh_p_len,
257 dh_yc, &dh_yc_len)) {
258 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
259 TLS_ALERT_INTERNAL_ERROR);
265 wpa_hexdump(MSG_DEBUG, "TLSv1: DH Yc (client's public value)",
268 WPA_PUT_BE16(*pos, dh_yc_len);
270 if (*pos + dh_yc_len > end) {
271 wpa_printf(MSG_DEBUG, "TLSv1: Not enough room in the "
272 "message buffer for Yc");
273 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
274 TLS_ALERT_INTERNAL_ERROR);
279 os_memcpy(*pos, dh_yc, dh_yc_len);
283 shared_len = conn->dh_p_len;
284 shared = os_malloc(shared_len);
285 if (shared == NULL) {
286 wpa_printf(MSG_DEBUG, "TLSv1: Could not allocate memory for "
288 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
289 TLS_ALERT_INTERNAL_ERROR);
294 /* shared = Ys^csecret mod p */
295 if (crypto_mod_exp(conn->dh_ys, conn->dh_ys_len,
296 csecret_start, csecret_len,
297 conn->dh_p, conn->dh_p_len,
298 shared, &shared_len)) {
299 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
300 TLS_ALERT_INTERNAL_ERROR);
305 wpa_hexdump_key(MSG_DEBUG, "TLSv1: Shared secret from DH key exchange",
308 os_memset(csecret_start, 0, csecret_len);
310 if (tls_derive_keys(conn, shared, shared_len)) {
311 wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
312 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
313 TLS_ALERT_INTERNAL_ERROR);
317 os_memset(shared, 0, shared_len);
319 tlsv1_client_free_dh(conn);
324 static int tlsv1_key_x_rsa(struct tlsv1_client *conn, u8 **pos, u8 *end)
326 u8 pre_master_secret[TLS_PRE_MASTER_SECRET_LEN];
330 if (tls_derive_pre_master_secret(pre_master_secret) < 0 ||
331 tls_derive_keys(conn, pre_master_secret,
332 TLS_PRE_MASTER_SECRET_LEN)) {
333 wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
334 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
335 TLS_ALERT_INTERNAL_ERROR);
339 /* EncryptedPreMasterSecret */
340 if (conn->server_rsa_key == NULL) {
341 wpa_printf(MSG_DEBUG, "TLSv1: No server RSA key to "
342 "use for encrypting pre-master secret");
343 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
344 TLS_ALERT_INTERNAL_ERROR);
348 /* RSA encrypted value is encoded with PKCS #1 v1.5 block type 2. */
351 res = crypto_public_key_encrypt_pkcs1_v15(
352 conn->server_rsa_key,
353 pre_master_secret, TLS_PRE_MASTER_SECRET_LEN,
355 os_memset(pre_master_secret, 0, TLS_PRE_MASTER_SECRET_LEN);
357 wpa_printf(MSG_DEBUG, "TLSv1: RSA encryption failed");
358 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
359 TLS_ALERT_INTERNAL_ERROR);
362 WPA_PUT_BE16(*pos - 2, clen);
363 wpa_hexdump(MSG_MSGDUMP, "TLSv1: Encrypted pre_master_secret",
371 static int tls_write_client_key_exchange(struct tlsv1_client *conn,
372 u8 **msgpos, u8 *end)
374 u8 *pos, *rhdr, *hs_start, *hs_length;
376 tls_key_exchange keyx;
377 const struct tls_cipher_suite *suite;
379 suite = tls_get_cipher_suite(conn->rl.cipher_suite);
381 keyx = TLS_KEY_X_NULL;
383 keyx = suite->key_exchange;
387 wpa_printf(MSG_DEBUG, "TLSv1: Send ClientKeyExchange");
390 pos += TLS_RECORD_HEADER_LEN;
392 /* opaque fragment[TLSPlaintext.length] */
396 /* HandshakeType msg_type */
397 *pos++ = TLS_HANDSHAKE_TYPE_CLIENT_KEY_EXCHANGE;
398 /* uint24 length (to be filled) */
401 /* body - ClientKeyExchange */
402 if (keyx == TLS_KEY_X_DH_anon) {
403 if (tlsv1_key_x_anon_dh(conn, &pos, end) < 0)
406 if (tlsv1_key_x_rsa(conn, &pos, end) < 0)
410 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
412 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
413 rhdr, end - rhdr, hs_start, pos - hs_start,
415 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
416 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
417 TLS_ALERT_INTERNAL_ERROR);
421 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
429 static int tls_write_client_certificate_verify(struct tlsv1_client *conn,
430 u8 **msgpos, u8 *end)
432 u8 *pos, *rhdr, *hs_start, *hs_length, *signed_start;
433 size_t rlen, hlen, clen;
435 enum { SIGN_ALG_RSA, SIGN_ALG_DSA } alg = SIGN_ALG_RSA;
439 wpa_printf(MSG_DEBUG, "TLSv1: Send CertificateVerify");
441 pos += TLS_RECORD_HEADER_LEN;
445 /* HandshakeType msg_type */
446 *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE_VERIFY;
447 /* uint24 length (to be filled) */
452 * RFC 2246: 7.4.3 and 7.4.8:
453 * Signature signature
456 * digitally-signed struct {
457 * opaque md5_hash[16];
458 * opaque sha_hash[20];
462 * digitally-signed struct {
463 * opaque sha_hash[20];
466 * The hash values are calculated over all handshake messages sent or
467 * received starting at ClientHello up to, but not including, this
468 * CertificateVerify message, including the type and length fields of
469 * the handshake messages.
475 if (conn->rl.tls_version == TLS_VERSION_1_2) {
476 hlen = SHA256_MAC_LEN;
477 if (conn->verify.sha256_cert == NULL ||
478 crypto_hash_finish(conn->verify.sha256_cert, hpos, &hlen) <
480 conn->verify.sha256_cert = NULL;
481 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
482 TLS_ALERT_INTERNAL_ERROR);
485 conn->verify.sha256_cert = NULL;
488 * RFC 3447, A.2.4 RSASSA-PKCS1-v1_5
490 * DigestInfo ::= SEQUENCE {
491 * digestAlgorithm DigestAlgorithm,
492 * digest OCTET STRING
495 * SHA-256 OID: sha256WithRSAEncryption ::= {pkcs-1 11}
497 * DER encoded DigestInfo for SHA256 per RFC 3447:
498 * 30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05 00 04 20 ||
501 os_memmove(hash + 19, hash, hlen);
503 os_memcpy(hash, "\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65"
504 "\x03\x04\x02\x01\x05\x00\x04\x20", 19);
506 #endif /* CONFIG_TLSV12 */
508 if (alg == SIGN_ALG_RSA) {
510 if (conn->verify.md5_cert == NULL ||
511 crypto_hash_finish(conn->verify.md5_cert, hpos, &hlen) < 0)
513 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
514 TLS_ALERT_INTERNAL_ERROR);
515 conn->verify.md5_cert = NULL;
516 crypto_hash_finish(conn->verify.sha1_cert, NULL, NULL);
517 conn->verify.sha1_cert = NULL;
522 crypto_hash_finish(conn->verify.md5_cert, NULL, NULL);
524 conn->verify.md5_cert = NULL;
526 if (conn->verify.sha1_cert == NULL ||
527 crypto_hash_finish(conn->verify.sha1_cert, hpos, &hlen) < 0) {
528 conn->verify.sha1_cert = NULL;
529 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
530 TLS_ALERT_INTERNAL_ERROR);
533 conn->verify.sha1_cert = NULL;
535 if (alg == SIGN_ALG_RSA)
540 #endif /* CONFIG_TLSV12 */
542 wpa_hexdump(MSG_MSGDUMP, "TLSv1: CertificateVerify hash", hash, hlen);
545 if (conn->rl.tls_version >= TLS_VERSION_1_2) {
548 * TLS v1.2 adds explicit indication of the used signature and
552 * HashAlgorithm hash;
553 * SignatureAlgorithm signature;
554 * } SignatureAndHashAlgorithm;
556 *pos++ = TLS_HASH_ALG_SHA256;
557 *pos++ = TLS_SIGN_ALG_RSA;
559 #endif /* CONFIG_TLSV12 */
563 * In digital signing, one-way hash functions are used as input for a
564 * signing algorithm. A digitally-signed element is encoded as an
565 * opaque vector <0..2^16-1>, where the length is specified by the
566 * signing algorithm and key.
568 * In RSA signing, a 36-byte structure of two hashes (one SHA and one
569 * MD5) is signed (encrypted with the private key). It is encoded with
570 * PKCS #1 block type 0 or type 1 as described in [PKCS1].
572 signed_start = pos; /* length to be filled */
575 if (conn->cred == NULL ||
576 crypto_private_key_sign_pkcs1(conn->cred->key, hash, hlen,
578 wpa_printf(MSG_DEBUG, "TLSv1: Failed to sign hash (PKCS #1)");
579 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
580 TLS_ALERT_INTERNAL_ERROR);
583 WPA_PUT_BE16(signed_start, clen);
587 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
589 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
590 rhdr, end - rhdr, hs_start, pos - hs_start,
592 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
593 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
594 TLS_ALERT_INTERNAL_ERROR);
599 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
607 static int tls_write_client_change_cipher_spec(struct tlsv1_client *conn,
608 u8 **msgpos, u8 *end)
613 wpa_printf(MSG_DEBUG, "TLSv1: Send ChangeCipherSpec");
615 payload[0] = TLS_CHANGE_CIPHER_SPEC;
617 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC,
618 *msgpos, end - *msgpos, payload, sizeof(payload),
620 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
621 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
622 TLS_ALERT_INTERNAL_ERROR);
626 if (tlsv1_record_change_write_cipher(&conn->rl) < 0) {
627 wpa_printf(MSG_DEBUG, "TLSv1: Failed to set write cipher for "
629 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
630 TLS_ALERT_INTERNAL_ERROR);
640 static int tls_write_client_finished(struct tlsv1_client *conn,
641 u8 **msgpos, u8 *end)
645 u8 verify_data[1 + 3 + TLS_VERIFY_DATA_LEN];
646 u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
648 wpa_printf(MSG_DEBUG, "TLSv1: Send Finished");
650 /* Encrypted Handshake Message: Finished */
653 if (conn->rl.tls_version >= TLS_VERSION_1_2) {
654 hlen = SHA256_MAC_LEN;
655 if (conn->verify.sha256_client == NULL ||
656 crypto_hash_finish(conn->verify.sha256_client, hash, &hlen)
658 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
659 TLS_ALERT_INTERNAL_ERROR);
660 conn->verify.sha256_client = NULL;
663 conn->verify.sha256_client = NULL;
665 #endif /* CONFIG_TLSV12 */
668 if (conn->verify.md5_client == NULL ||
669 crypto_hash_finish(conn->verify.md5_client, hash, &hlen) < 0) {
670 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
671 TLS_ALERT_INTERNAL_ERROR);
672 conn->verify.md5_client = NULL;
673 crypto_hash_finish(conn->verify.sha1_client, NULL, NULL);
674 conn->verify.sha1_client = NULL;
677 conn->verify.md5_client = NULL;
679 if (conn->verify.sha1_client == NULL ||
680 crypto_hash_finish(conn->verify.sha1_client, hash + MD5_MAC_LEN,
682 conn->verify.sha1_client = NULL;
683 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
684 TLS_ALERT_INTERNAL_ERROR);
687 conn->verify.sha1_client = NULL;
688 hlen = MD5_MAC_LEN + SHA1_MAC_LEN;
692 #endif /* CONFIG_TLSV12 */
694 if (tls_prf(conn->rl.tls_version,
695 conn->master_secret, TLS_MASTER_SECRET_LEN,
696 "client finished", hash, hlen,
697 verify_data + 1 + 3, TLS_VERIFY_DATA_LEN)) {
698 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate verify_data");
699 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
700 TLS_ALERT_INTERNAL_ERROR);
703 wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (client)",
704 verify_data + 1 + 3, TLS_VERIFY_DATA_LEN);
707 pos = hs_start = verify_data;
708 /* HandshakeType msg_type */
709 *pos++ = TLS_HANDSHAKE_TYPE_FINISHED;
711 WPA_PUT_BE24(pos, TLS_VERIFY_DATA_LEN);
713 pos += TLS_VERIFY_DATA_LEN; /* verify_data already in place */
714 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
716 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
717 *msgpos, end - *msgpos, hs_start, pos - hs_start,
719 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
720 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
721 TLS_ALERT_INTERNAL_ERROR);
731 static u8 * tls_send_client_key_exchange(struct tlsv1_client *conn,
740 if (conn->certificate_requested)
741 msglen += tls_client_cert_chain_der_len(conn);
743 msg = os_malloc(msglen);
750 if (conn->certificate_requested) {
751 if (tls_write_client_certificate(conn, &pos, end) < 0) {
757 if (tls_write_client_key_exchange(conn, &pos, end) < 0 ||
758 (conn->certificate_requested && conn->cred && conn->cred->key &&
759 tls_write_client_certificate_verify(conn, &pos, end) < 0) ||
760 tls_write_client_change_cipher_spec(conn, &pos, end) < 0 ||
761 tls_write_client_finished(conn, &pos, end) < 0) {
766 *out_len = pos - msg;
768 conn->state = SERVER_CHANGE_CIPHER_SPEC;
774 static u8 * tls_send_change_cipher_spec(struct tlsv1_client *conn,
781 msg = os_malloc(1000);
788 if (tls_write_client_change_cipher_spec(conn, &pos, end) < 0 ||
789 tls_write_client_finished(conn, &pos, end) < 0) {
794 *out_len = pos - msg;
796 wpa_printf(MSG_DEBUG, "TLSv1: Session resumption completed "
798 conn->state = ESTABLISHED;
804 u8 * tlsv1_client_handshake_write(struct tlsv1_client *conn, size_t *out_len,
807 switch (conn->state) {
808 case CLIENT_KEY_EXCHANGE:
809 return tls_send_client_key_exchange(conn, out_len);
810 case CHANGE_CIPHER_SPEC:
811 return tls_send_change_cipher_spec(conn, out_len);
813 wpa_printf(MSG_DEBUG, "TLSv1: Handshake completed "
815 conn->state = ESTABLISHED;
818 /* Need to return something to get final TLS ACK. */
823 wpa_printf(MSG_DEBUG, "TLSv1: Unexpected state %d while "
824 "generating reply", conn->state);
830 u8 * tlsv1_client_send_alert(struct tlsv1_client *conn, u8 level,
831 u8 description, size_t *out_len)
833 u8 *alert, *pos, *length;
835 wpa_printf(MSG_DEBUG, "TLSv1: Send Alert(%d:%d)", level, description);
838 alert = os_malloc(10);
845 /* ContentType type */
846 *pos++ = TLS_CONTENT_TYPE_ALERT;
847 /* ProtocolVersion version */
848 WPA_PUT_BE16(pos, conn->rl.tls_version ? conn->rl.tls_version :
851 /* uint16 length (to be filled) */
854 /* opaque fragment[TLSPlaintext.length] */
857 /* AlertLevel level */
859 /* AlertDescription description */
860 *pos++ = description;
862 WPA_PUT_BE16(length, pos - length - 2);
863 *out_len = pos - alert;