2 * TLSv1 client - write handshake message
3 * Copyright (c) 2006-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
22 #include "tlsv1_common.h"
23 #include "tlsv1_record.h"
24 #include "tlsv1_client.h"
25 #include "tlsv1_client_i.h"
28 static size_t tls_client_cert_chain_der_len(struct tlsv1_client *conn)
31 struct x509_certificate *cert;
33 if (conn->cred == NULL)
36 cert = conn->cred->cert;
38 len += 3 + cert->cert_len;
39 if (x509_certificate_self_signed(cert))
41 cert = x509_certificate_get_subject(conn->cred->trusted_certs,
49 u8 * tls_send_client_hello(struct tlsv1_client *conn, size_t *out_len)
51 u8 *hello, *end, *pos, *hs_length, *hs_start, *rhdr;
55 wpa_printf(MSG_DEBUG, "TLSv1: Send ClientHello");
59 WPA_PUT_BE32(conn->client_random, now.sec);
60 if (os_get_random(conn->client_random + 4, TLS_RANDOM_LEN - 4)) {
61 wpa_printf(MSG_ERROR, "TLSv1: Could not generate "
65 wpa_hexdump(MSG_MSGDUMP, "TLSv1: client_random",
66 conn->client_random, TLS_RANDOM_LEN);
68 len = 100 + conn->num_cipher_suites * 2 + conn->client_hello_ext_len;
69 hello = os_malloc(len);
75 pos = rhdr + TLS_RECORD_HEADER_LEN;
77 /* opaque fragment[TLSPlaintext.length] */
81 /* HandshakeType msg_type */
82 *pos++ = TLS_HANDSHAKE_TYPE_CLIENT_HELLO;
83 /* uint24 length (to be filled) */
86 /* body - ClientHello */
87 /* ProtocolVersion client_version */
88 WPA_PUT_BE16(pos, TLS_VERSION);
90 /* Random random: uint32 gmt_unix_time, opaque random_bytes */
91 os_memcpy(pos, conn->client_random, TLS_RANDOM_LEN);
92 pos += TLS_RANDOM_LEN;
93 /* SessionID session_id */
94 *pos++ = conn->session_id_len;
95 os_memcpy(pos, conn->session_id, conn->session_id_len);
96 pos += conn->session_id_len;
97 /* CipherSuite cipher_suites<2..2^16-1> */
98 WPA_PUT_BE16(pos, 2 * conn->num_cipher_suites);
100 for (i = 0; i < conn->num_cipher_suites; i++) {
101 WPA_PUT_BE16(pos, conn->cipher_suites[i]);
104 /* CompressionMethod compression_methods<1..2^8-1> */
106 *pos++ = TLS_COMPRESSION_NULL;
108 if (conn->client_hello_ext) {
109 os_memcpy(pos, conn->client_hello_ext,
110 conn->client_hello_ext_len);
111 pos += conn->client_hello_ext_len;
114 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
115 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
117 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
118 rhdr, end - rhdr, pos - hs_start, out_len) < 0) {
119 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create TLS record");
120 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
121 TLS_ALERT_INTERNAL_ERROR);
126 conn->state = SERVER_HELLO;
132 static int tls_write_client_certificate(struct tlsv1_client *conn,
133 u8 **msgpos, u8 *end)
135 u8 *pos, *rhdr, *hs_start, *hs_length, *cert_start;
137 struct x509_certificate *cert;
141 wpa_printf(MSG_DEBUG, "TLSv1: Send Certificate");
143 pos += TLS_RECORD_HEADER_LEN;
145 /* opaque fragment[TLSPlaintext.length] */
149 /* HandshakeType msg_type */
150 *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE;
151 /* uint24 length (to be filled) */
154 /* body - Certificate */
155 /* uint24 length (to be filled) */
158 cert = conn->cred ? conn->cred->cert : NULL;
160 if (pos + 3 + cert->cert_len > end) {
161 wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space "
162 "for Certificate (cert_len=%lu left=%lu)",
163 (unsigned long) cert->cert_len,
164 (unsigned long) (end - pos));
165 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
166 TLS_ALERT_INTERNAL_ERROR);
169 WPA_PUT_BE24(pos, cert->cert_len);
171 os_memcpy(pos, cert->cert_start, cert->cert_len);
172 pos += cert->cert_len;
174 if (x509_certificate_self_signed(cert))
176 cert = x509_certificate_get_subject(conn->cred->trusted_certs,
179 if (conn->cred == NULL || cert == conn->cred->cert || cert == NULL) {
181 * Client was not configured with all the needed certificates
182 * to form a full certificate chain. The server may fail to
183 * validate the chain unless it is configured with all the
184 * missing CA certificates.
186 wpa_printf(MSG_DEBUG, "TLSv1: Full client certificate chain "
187 "not configured - validation may fail");
189 WPA_PUT_BE24(cert_start, pos - cert_start - 3);
191 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
193 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
194 rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
195 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
196 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
197 TLS_ALERT_INTERNAL_ERROR);
202 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
210 static int tlsv1_key_x_anon_dh(struct tlsv1_client *conn, u8 **pos, u8 *end)
213 /* ClientDiffieHellmanPublic */
214 u8 *csecret, *csecret_start, *dh_yc, *shared;
215 size_t csecret_len, dh_yc_len, shared_len;
217 csecret_len = conn->dh_p_len;
218 csecret = os_malloc(csecret_len);
219 if (csecret == NULL) {
220 wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
221 "memory for Yc (Diffie-Hellman)");
222 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
223 TLS_ALERT_INTERNAL_ERROR);
226 if (os_get_random(csecret, csecret_len)) {
227 wpa_printf(MSG_DEBUG, "TLSv1: Failed to get random "
228 "data for Diffie-Hellman");
229 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
230 TLS_ALERT_INTERNAL_ERROR);
235 if (os_memcmp(csecret, conn->dh_p, csecret_len) > 0)
236 csecret[0] = 0; /* make sure Yc < p */
238 csecret_start = csecret;
239 while (csecret_len > 1 && *csecret_start == 0) {
243 wpa_hexdump_key(MSG_DEBUG, "TLSv1: DH client's secret value",
244 csecret_start, csecret_len);
246 /* Yc = g^csecret mod p */
247 dh_yc_len = conn->dh_p_len;
248 dh_yc = os_malloc(dh_yc_len);
250 wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
251 "memory for Diffie-Hellman");
252 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
253 TLS_ALERT_INTERNAL_ERROR);
257 if (crypto_mod_exp(conn->dh_g, conn->dh_g_len,
258 csecret_start, csecret_len,
259 conn->dh_p, conn->dh_p_len,
260 dh_yc, &dh_yc_len)) {
261 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
262 TLS_ALERT_INTERNAL_ERROR);
268 wpa_hexdump(MSG_DEBUG, "TLSv1: DH Yc (client's public value)",
271 WPA_PUT_BE16(*pos, dh_yc_len);
273 if (*pos + dh_yc_len > end) {
274 wpa_printf(MSG_DEBUG, "TLSv1: Not enough room in the "
275 "message buffer for Yc");
276 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
277 TLS_ALERT_INTERNAL_ERROR);
282 os_memcpy(*pos, dh_yc, dh_yc_len);
286 shared_len = conn->dh_p_len;
287 shared = os_malloc(shared_len);
288 if (shared == NULL) {
289 wpa_printf(MSG_DEBUG, "TLSv1: Could not allocate memory for "
291 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
292 TLS_ALERT_INTERNAL_ERROR);
297 /* shared = Ys^csecret mod p */
298 if (crypto_mod_exp(conn->dh_ys, conn->dh_ys_len,
299 csecret_start, csecret_len,
300 conn->dh_p, conn->dh_p_len,
301 shared, &shared_len)) {
302 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
303 TLS_ALERT_INTERNAL_ERROR);
308 wpa_hexdump_key(MSG_DEBUG, "TLSv1: Shared secret from DH key exchange",
311 os_memset(csecret_start, 0, csecret_len);
313 if (tls_derive_keys(conn, shared, shared_len)) {
314 wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
315 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
316 TLS_ALERT_INTERNAL_ERROR);
320 os_memset(shared, 0, shared_len);
322 tlsv1_client_free_dh(conn);
325 tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_INTERNAL_ERROR);
327 #endif /* EAP_FAST */
331 static int tlsv1_key_x_rsa(struct tlsv1_client *conn, u8 **pos, u8 *end)
333 u8 pre_master_secret[TLS_PRE_MASTER_SECRET_LEN];
337 if (tls_derive_pre_master_secret(pre_master_secret) < 0 ||
338 tls_derive_keys(conn, pre_master_secret,
339 TLS_PRE_MASTER_SECRET_LEN)) {
340 wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
341 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
342 TLS_ALERT_INTERNAL_ERROR);
346 /* EncryptedPreMasterSecret */
347 if (conn->server_rsa_key == NULL) {
348 wpa_printf(MSG_DEBUG, "TLSv1: No server RSA key to "
349 "use for encrypting pre-master secret");
350 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
351 TLS_ALERT_INTERNAL_ERROR);
355 /* RSA encrypted value is encoded with PKCS #1 v1.5 block type 2. */
358 res = crypto_public_key_encrypt_pkcs1_v15(
359 conn->server_rsa_key,
360 pre_master_secret, TLS_PRE_MASTER_SECRET_LEN,
362 os_memset(pre_master_secret, 0, TLS_PRE_MASTER_SECRET_LEN);
364 wpa_printf(MSG_DEBUG, "TLSv1: RSA encryption failed");
365 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
366 TLS_ALERT_INTERNAL_ERROR);
369 WPA_PUT_BE16(*pos - 2, clen);
370 wpa_hexdump(MSG_MSGDUMP, "TLSv1: Encrypted pre_master_secret",
378 static int tls_write_client_key_exchange(struct tlsv1_client *conn,
379 u8 **msgpos, u8 *end)
381 u8 *pos, *rhdr, *hs_start, *hs_length;
383 tls_key_exchange keyx;
384 const struct tls_cipher_suite *suite;
386 suite = tls_get_cipher_suite(conn->rl.cipher_suite);
388 keyx = TLS_KEY_X_NULL;
390 keyx = suite->key_exchange;
394 wpa_printf(MSG_DEBUG, "TLSv1: Send ClientKeyExchange");
397 pos += TLS_RECORD_HEADER_LEN;
399 /* opaque fragment[TLSPlaintext.length] */
403 /* HandshakeType msg_type */
404 *pos++ = TLS_HANDSHAKE_TYPE_CLIENT_KEY_EXCHANGE;
405 /* uint24 length (to be filled) */
408 /* body - ClientKeyExchange */
409 if (keyx == TLS_KEY_X_DH_anon) {
410 if (tlsv1_key_x_anon_dh(conn, &pos, end) < 0)
413 if (tlsv1_key_x_rsa(conn, &pos, end) < 0)
417 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
419 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
420 rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
421 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
422 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
423 TLS_ALERT_INTERNAL_ERROR);
427 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
435 static int tls_write_client_certificate_verify(struct tlsv1_client *conn,
436 u8 **msgpos, u8 *end)
438 u8 *pos, *rhdr, *hs_start, *hs_length, *signed_start;
439 size_t rlen, hlen, clen;
440 u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN], *hpos;
441 enum { SIGN_ALG_RSA, SIGN_ALG_DSA } alg = SIGN_ALG_RSA;
445 wpa_printf(MSG_DEBUG, "TLSv1: Send CertificateVerify");
447 pos += TLS_RECORD_HEADER_LEN;
451 /* HandshakeType msg_type */
452 *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE_VERIFY;
453 /* uint24 length (to be filled) */
458 * RFC 2246: 7.4.3 and 7.4.8:
459 * Signature signature
462 * digitally-signed struct {
463 * opaque md5_hash[16];
464 * opaque sha_hash[20];
468 * digitally-signed struct {
469 * opaque sha_hash[20];
472 * The hash values are calculated over all handshake messages sent or
473 * received starting at ClientHello up to, but not including, this
474 * CertificateVerify message, including the type and length fields of
475 * the handshake messages.
480 if (alg == SIGN_ALG_RSA) {
482 if (conn->verify.md5_cert == NULL ||
483 crypto_hash_finish(conn->verify.md5_cert, hpos, &hlen) < 0)
485 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
486 TLS_ALERT_INTERNAL_ERROR);
487 conn->verify.md5_cert = NULL;
488 crypto_hash_finish(conn->verify.sha1_cert, NULL, NULL);
489 conn->verify.sha1_cert = NULL;
494 crypto_hash_finish(conn->verify.md5_cert, NULL, NULL);
496 conn->verify.md5_cert = NULL;
498 if (conn->verify.sha1_cert == NULL ||
499 crypto_hash_finish(conn->verify.sha1_cert, hpos, &hlen) < 0) {
500 conn->verify.sha1_cert = NULL;
501 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
502 TLS_ALERT_INTERNAL_ERROR);
505 conn->verify.sha1_cert = NULL;
507 if (alg == SIGN_ALG_RSA)
510 wpa_hexdump(MSG_MSGDUMP, "TLSv1: CertificateVerify hash", hash, hlen);
514 * In digital signing, one-way hash functions are used as input for a
515 * signing algorithm. A digitally-signed element is encoded as an
516 * opaque vector <0..2^16-1>, where the length is specified by the
517 * signing algorithm and key.
519 * In RSA signing, a 36-byte structure of two hashes (one SHA and one
520 * MD5) is signed (encrypted with the private key). It is encoded with
521 * PKCS #1 block type 0 or type 1 as described in [PKCS1].
523 signed_start = pos; /* length to be filled */
526 if (conn->cred == NULL ||
527 crypto_private_key_sign_pkcs1(conn->cred->key, hash, hlen,
529 wpa_printf(MSG_DEBUG, "TLSv1: Failed to sign hash (PKCS #1)");
530 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
531 TLS_ALERT_INTERNAL_ERROR);
534 WPA_PUT_BE16(signed_start, clen);
538 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
540 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
541 rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
542 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
543 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
544 TLS_ALERT_INTERNAL_ERROR);
549 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
557 static int tls_write_client_change_cipher_spec(struct tlsv1_client *conn,
558 u8 **msgpos, u8 *end)
565 wpa_printf(MSG_DEBUG, "TLSv1: Send ChangeCipherSpec");
567 pos += TLS_RECORD_HEADER_LEN;
568 *pos = TLS_CHANGE_CIPHER_SPEC;
569 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC,
570 rhdr, end - rhdr, 1, &rlen) < 0) {
571 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
572 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
573 TLS_ALERT_INTERNAL_ERROR);
577 if (tlsv1_record_change_write_cipher(&conn->rl) < 0) {
578 wpa_printf(MSG_DEBUG, "TLSv1: Failed to set write cipher for "
580 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
581 TLS_ALERT_INTERNAL_ERROR);
585 *msgpos = rhdr + rlen;
591 static int tls_write_client_finished(struct tlsv1_client *conn,
592 u8 **msgpos, u8 *end)
594 u8 *pos, *rhdr, *hs_start, *hs_length;
596 u8 verify_data[TLS_VERIFY_DATA_LEN];
597 u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
601 wpa_printf(MSG_DEBUG, "TLSv1: Send Finished");
603 /* Encrypted Handshake Message: Finished */
606 if (conn->verify.md5_client == NULL ||
607 crypto_hash_finish(conn->verify.md5_client, hash, &hlen) < 0) {
608 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
609 TLS_ALERT_INTERNAL_ERROR);
610 conn->verify.md5_client = NULL;
611 crypto_hash_finish(conn->verify.sha1_client, NULL, NULL);
612 conn->verify.sha1_client = NULL;
615 conn->verify.md5_client = NULL;
617 if (conn->verify.sha1_client == NULL ||
618 crypto_hash_finish(conn->verify.sha1_client, hash + MD5_MAC_LEN,
620 conn->verify.sha1_client = NULL;
621 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
622 TLS_ALERT_INTERNAL_ERROR);
625 conn->verify.sha1_client = NULL;
627 if (tls_prf(conn->master_secret, TLS_MASTER_SECRET_LEN,
628 "client finished", hash, MD5_MAC_LEN + SHA1_MAC_LEN,
629 verify_data, TLS_VERIFY_DATA_LEN)) {
630 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate verify_data");
631 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
632 TLS_ALERT_INTERNAL_ERROR);
635 wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (client)",
636 verify_data, TLS_VERIFY_DATA_LEN);
639 pos += TLS_RECORD_HEADER_LEN;
642 /* HandshakeType msg_type */
643 *pos++ = TLS_HANDSHAKE_TYPE_FINISHED;
644 /* uint24 length (to be filled) */
647 os_memcpy(pos, verify_data, TLS_VERIFY_DATA_LEN);
648 pos += TLS_VERIFY_DATA_LEN;
649 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
650 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
652 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
653 rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
654 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
655 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
656 TLS_ALERT_INTERNAL_ERROR);
668 static u8 * tls_send_client_key_exchange(struct tlsv1_client *conn,
677 if (conn->certificate_requested)
678 msglen += tls_client_cert_chain_der_len(conn);
680 msg = os_malloc(msglen);
687 if (conn->certificate_requested) {
688 if (tls_write_client_certificate(conn, &pos, end) < 0) {
694 if (tls_write_client_key_exchange(conn, &pos, end) < 0 ||
695 (conn->certificate_requested && conn->cred && conn->cred->key &&
696 tls_write_client_certificate_verify(conn, &pos, end) < 0) ||
697 tls_write_client_change_cipher_spec(conn, &pos, end) < 0 ||
698 tls_write_client_finished(conn, &pos, end) < 0) {
703 *out_len = pos - msg;
705 conn->state = SERVER_CHANGE_CIPHER_SPEC;
711 static u8 * tls_send_change_cipher_spec(struct tlsv1_client *conn,
718 msg = os_malloc(1000);
725 if (tls_write_client_change_cipher_spec(conn, &pos, end) < 0 ||
726 tls_write_client_finished(conn, &pos, end) < 0) {
731 *out_len = pos - msg;
733 wpa_printf(MSG_DEBUG, "TLSv1: Session resumption completed "
735 conn->state = ESTABLISHED;
741 u8 * tlsv1_client_handshake_write(struct tlsv1_client *conn, size_t *out_len,
744 switch (conn->state) {
745 case CLIENT_KEY_EXCHANGE:
746 return tls_send_client_key_exchange(conn, out_len);
747 case CHANGE_CIPHER_SPEC:
748 return tls_send_change_cipher_spec(conn, out_len);
750 wpa_printf(MSG_DEBUG, "TLSv1: Handshake completed "
752 conn->state = ESTABLISHED;
755 /* Need to return something to get final TLS ACK. */
760 wpa_printf(MSG_DEBUG, "TLSv1: Unexpected state %d while "
761 "generating reply", conn->state);
767 u8 * tlsv1_client_send_alert(struct tlsv1_client *conn, u8 level,
768 u8 description, size_t *out_len)
770 u8 *alert, *pos, *length;
772 wpa_printf(MSG_DEBUG, "TLSv1: Send Alert(%d:%d)", level, description);
775 alert = os_malloc(10);
782 /* ContentType type */
783 *pos++ = TLS_CONTENT_TYPE_ALERT;
784 /* ProtocolVersion version */
785 WPA_PUT_BE16(pos, TLS_VERSION);
787 /* uint16 length (to be filled) */
790 /* opaque fragment[TLSPlaintext.length] */
793 /* AlertLevel level */
795 /* AlertDescription description */
796 *pos++ = description;
798 WPA_PUT_BE16(length, pos - length - 2);
799 *out_len = pos - alert;