Added WirelessManager, a port of wpa_supplicant.
[AROS.git] / workbench / network / WirelessManager / src / tls / tlsv1_client_write.c
blobb47425f232b7d6a279f8bd1f0ee6278bdae75b55
1 /*
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
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "crypto/md5.h"
19 #include "crypto/sha1.h"
20 #include "crypto/tls.h"
21 #include "x509v3.h"
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)
30 size_t len = 0;
31 struct x509_certificate *cert;
33 if (conn->cred == NULL)
34 return 0;
36 cert = conn->cred->cert;
37 while (cert) {
38 len += 3 + cert->cert_len;
39 if (x509_certificate_self_signed(cert))
40 break;
41 cert = x509_certificate_get_subject(conn->cred->trusted_certs,
42 &cert->issuer);
45 return len;
49 u8 * tls_send_client_hello(struct tlsv1_client *conn, size_t *out_len)
51 u8 *hello, *end, *pos, *hs_length, *hs_start, *rhdr;
52 struct os_time now;
53 size_t len, i;
55 wpa_printf(MSG_DEBUG, "TLSv1: Send ClientHello");
56 *out_len = 0;
58 os_get_time(&now);
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 "
62 "client_random");
63 return NULL;
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);
70 if (hello == NULL)
71 return NULL;
72 end = hello + len;
74 rhdr = hello;
75 pos = rhdr + TLS_RECORD_HEADER_LEN;
77 /* opaque fragment[TLSPlaintext.length] */
79 /* Handshake */
80 hs_start = pos;
81 /* HandshakeType msg_type */
82 *pos++ = TLS_HANDSHAKE_TYPE_CLIENT_HELLO;
83 /* uint24 length (to be filled) */
84 hs_length = pos;
85 pos += 3;
86 /* body - ClientHello */
87 /* ProtocolVersion client_version */
88 WPA_PUT_BE16(pos, TLS_VERSION);
89 pos += 2;
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);
99 pos += 2;
100 for (i = 0; i < conn->num_cipher_suites; i++) {
101 WPA_PUT_BE16(pos, conn->cipher_suites[i]);
102 pos += 2;
104 /* CompressionMethod compression_methods<1..2^8-1> */
105 *pos++ = 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);
122 os_free(hello);
123 return NULL;
126 conn->state = SERVER_HELLO;
128 return 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;
136 size_t rlen;
137 struct x509_certificate *cert;
139 pos = *msgpos;
141 wpa_printf(MSG_DEBUG, "TLSv1: Send Certificate");
142 rhdr = pos;
143 pos += TLS_RECORD_HEADER_LEN;
145 /* opaque fragment[TLSPlaintext.length] */
147 /* Handshake */
148 hs_start = pos;
149 /* HandshakeType msg_type */
150 *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE;
151 /* uint24 length (to be filled) */
152 hs_length = pos;
153 pos += 3;
154 /* body - Certificate */
155 /* uint24 length (to be filled) */
156 cert_start = pos;
157 pos += 3;
158 cert = conn->cred ? conn->cred->cert : NULL;
159 while (cert) {
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);
167 return -1;
169 WPA_PUT_BE24(pos, cert->cert_len);
170 pos += 3;
171 os_memcpy(pos, cert->cert_start, cert->cert_len);
172 pos += cert->cert_len;
174 if (x509_certificate_self_signed(cert))
175 break;
176 cert = x509_certificate_get_subject(conn->cred->trusted_certs,
177 &cert->issuer);
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);
198 return -1;
200 pos = rhdr + rlen;
202 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
204 *msgpos = pos;
206 return 0;
210 static int tlsv1_key_x_anon_dh(struct tlsv1_client *conn, u8 **pos, u8 *end)
212 /* ClientDiffieHellmanPublic */
213 u8 *csecret, *csecret_start, *dh_yc, *shared;
214 size_t csecret_len, dh_yc_len, shared_len;
216 csecret_len = conn->dh_p_len;
217 csecret = os_malloc(csecret_len);
218 if (csecret == NULL) {
219 wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
220 "memory for Yc (Diffie-Hellman)");
221 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
222 TLS_ALERT_INTERNAL_ERROR);
223 return -1;
225 if (os_get_random(csecret, csecret_len)) {
226 wpa_printf(MSG_DEBUG, "TLSv1: Failed to get random "
227 "data for Diffie-Hellman");
228 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
229 TLS_ALERT_INTERNAL_ERROR);
230 os_free(csecret);
231 return -1;
234 if (os_memcmp(csecret, conn->dh_p, csecret_len) > 0)
235 csecret[0] = 0; /* make sure Yc < p */
237 csecret_start = csecret;
238 while (csecret_len > 1 && *csecret_start == 0) {
239 csecret_start++;
240 csecret_len--;
242 wpa_hexdump_key(MSG_DEBUG, "TLSv1: DH client's secret value",
243 csecret_start, csecret_len);
245 /* Yc = g^csecret mod p */
246 dh_yc_len = conn->dh_p_len;
247 dh_yc = os_malloc(dh_yc_len);
248 if (dh_yc == NULL) {
249 wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
250 "memory for Diffie-Hellman");
251 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
252 TLS_ALERT_INTERNAL_ERROR);
253 os_free(csecret);
254 return -1;
256 if (crypto_mod_exp(conn->dh_g, conn->dh_g_len,
257 csecret_start, csecret_len,
258 conn->dh_p, conn->dh_p_len,
259 dh_yc, &dh_yc_len)) {
260 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
261 TLS_ALERT_INTERNAL_ERROR);
262 os_free(csecret);
263 os_free(dh_yc);
264 return -1;
267 wpa_hexdump(MSG_DEBUG, "TLSv1: DH Yc (client's public value)",
268 dh_yc, dh_yc_len);
270 WPA_PUT_BE16(*pos, dh_yc_len);
271 *pos += 2;
272 if (*pos + dh_yc_len > end) {
273 wpa_printf(MSG_DEBUG, "TLSv1: Not enough room in the "
274 "message buffer for Yc");
275 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
276 TLS_ALERT_INTERNAL_ERROR);
277 os_free(csecret);
278 os_free(dh_yc);
279 return -1;
281 os_memcpy(*pos, dh_yc, dh_yc_len);
282 *pos += dh_yc_len;
283 os_free(dh_yc);
285 shared_len = conn->dh_p_len;
286 shared = os_malloc(shared_len);
287 if (shared == NULL) {
288 wpa_printf(MSG_DEBUG, "TLSv1: Could not allocate memory for "
289 "DH");
290 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
291 TLS_ALERT_INTERNAL_ERROR);
292 os_free(csecret);
293 return -1;
296 /* shared = Ys^csecret mod p */
297 if (crypto_mod_exp(conn->dh_ys, conn->dh_ys_len,
298 csecret_start, csecret_len,
299 conn->dh_p, conn->dh_p_len,
300 shared, &shared_len)) {
301 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
302 TLS_ALERT_INTERNAL_ERROR);
303 os_free(csecret);
304 os_free(shared);
305 return -1;
307 wpa_hexdump_key(MSG_DEBUG, "TLSv1: Shared secret from DH key exchange",
308 shared, shared_len);
310 os_memset(csecret_start, 0, csecret_len);
311 os_free(csecret);
312 if (tls_derive_keys(conn, shared, shared_len)) {
313 wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
314 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
315 TLS_ALERT_INTERNAL_ERROR);
316 os_free(shared);
317 return -1;
319 os_memset(shared, 0, shared_len);
320 os_free(shared);
321 tlsv1_client_free_dh(conn);
322 return 0;
326 static int tlsv1_key_x_rsa(struct tlsv1_client *conn, u8 **pos, u8 *end)
328 u8 pre_master_secret[TLS_PRE_MASTER_SECRET_LEN];
329 size_t clen;
330 int res;
332 if (tls_derive_pre_master_secret(pre_master_secret) < 0 ||
333 tls_derive_keys(conn, pre_master_secret,
334 TLS_PRE_MASTER_SECRET_LEN)) {
335 wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
336 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
337 TLS_ALERT_INTERNAL_ERROR);
338 return -1;
341 /* EncryptedPreMasterSecret */
342 if (conn->server_rsa_key == NULL) {
343 wpa_printf(MSG_DEBUG, "TLSv1: No server RSA key to "
344 "use for encrypting pre-master secret");
345 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
346 TLS_ALERT_INTERNAL_ERROR);
347 return -1;
350 /* RSA encrypted value is encoded with PKCS #1 v1.5 block type 2. */
351 *pos += 2;
352 clen = end - *pos;
353 res = crypto_public_key_encrypt_pkcs1_v15(
354 conn->server_rsa_key,
355 pre_master_secret, TLS_PRE_MASTER_SECRET_LEN,
356 *pos, &clen);
357 os_memset(pre_master_secret, 0, TLS_PRE_MASTER_SECRET_LEN);
358 if (res < 0) {
359 wpa_printf(MSG_DEBUG, "TLSv1: RSA encryption failed");
360 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
361 TLS_ALERT_INTERNAL_ERROR);
362 return -1;
364 WPA_PUT_BE16(*pos - 2, clen);
365 wpa_hexdump(MSG_MSGDUMP, "TLSv1: Encrypted pre_master_secret",
366 *pos, clen);
367 *pos += clen;
369 return 0;
373 static int tls_write_client_key_exchange(struct tlsv1_client *conn,
374 u8 **msgpos, u8 *end)
376 u8 *pos, *rhdr, *hs_start, *hs_length;
377 size_t rlen;
378 tls_key_exchange keyx;
379 const struct tls_cipher_suite *suite;
381 suite = tls_get_cipher_suite(conn->rl.cipher_suite);
382 if (suite == NULL)
383 keyx = TLS_KEY_X_NULL;
384 else
385 keyx = suite->key_exchange;
387 pos = *msgpos;
389 wpa_printf(MSG_DEBUG, "TLSv1: Send ClientKeyExchange");
391 rhdr = pos;
392 pos += TLS_RECORD_HEADER_LEN;
394 /* opaque fragment[TLSPlaintext.length] */
396 /* Handshake */
397 hs_start = pos;
398 /* HandshakeType msg_type */
399 *pos++ = TLS_HANDSHAKE_TYPE_CLIENT_KEY_EXCHANGE;
400 /* uint24 length (to be filled) */
401 hs_length = pos;
402 pos += 3;
403 /* body - ClientKeyExchange */
404 if (keyx == TLS_KEY_X_DH_anon) {
405 if (tlsv1_key_x_anon_dh(conn, &pos, end) < 0)
406 return -1;
407 } else {
408 if (tlsv1_key_x_rsa(conn, &pos, end) < 0)
409 return -1;
412 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
414 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
415 rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
416 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
417 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
418 TLS_ALERT_INTERNAL_ERROR);
419 return -1;
421 pos = rhdr + rlen;
422 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
424 *msgpos = pos;
426 return 0;
430 static int tls_write_client_certificate_verify(struct tlsv1_client *conn,
431 u8 **msgpos, u8 *end)
433 u8 *pos, *rhdr, *hs_start, *hs_length, *signed_start;
434 size_t rlen, hlen, clen;
435 u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN], *hpos;
436 enum { SIGN_ALG_RSA, SIGN_ALG_DSA } alg = SIGN_ALG_RSA;
438 pos = *msgpos;
440 wpa_printf(MSG_DEBUG, "TLSv1: Send CertificateVerify");
441 rhdr = pos;
442 pos += TLS_RECORD_HEADER_LEN;
444 /* Handshake */
445 hs_start = pos;
446 /* HandshakeType msg_type */
447 *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE_VERIFY;
448 /* uint24 length (to be filled) */
449 hs_length = pos;
450 pos += 3;
453 * RFC 2246: 7.4.3 and 7.4.8:
454 * Signature signature
456 * RSA:
457 * digitally-signed struct {
458 * opaque md5_hash[16];
459 * opaque sha_hash[20];
460 * };
462 * DSA:
463 * digitally-signed struct {
464 * opaque sha_hash[20];
465 * };
467 * The hash values are calculated over all handshake messages sent or
468 * received starting at ClientHello up to, but not including, this
469 * CertificateVerify message, including the type and length fields of
470 * the handshake messages.
473 hpos = hash;
475 if (alg == SIGN_ALG_RSA) {
476 hlen = MD5_MAC_LEN;
477 if (conn->verify.md5_cert == NULL ||
478 crypto_hash_finish(conn->verify.md5_cert, hpos, &hlen) < 0)
480 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
481 TLS_ALERT_INTERNAL_ERROR);
482 conn->verify.md5_cert = NULL;
483 crypto_hash_finish(conn->verify.sha1_cert, NULL, NULL);
484 conn->verify.sha1_cert = NULL;
485 return -1;
487 hpos += MD5_MAC_LEN;
488 } else
489 crypto_hash_finish(conn->verify.md5_cert, NULL, NULL);
491 conn->verify.md5_cert = NULL;
492 hlen = SHA1_MAC_LEN;
493 if (conn->verify.sha1_cert == NULL ||
494 crypto_hash_finish(conn->verify.sha1_cert, hpos, &hlen) < 0) {
495 conn->verify.sha1_cert = NULL;
496 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
497 TLS_ALERT_INTERNAL_ERROR);
498 return -1;
500 conn->verify.sha1_cert = NULL;
502 if (alg == SIGN_ALG_RSA)
503 hlen += MD5_MAC_LEN;
505 wpa_hexdump(MSG_MSGDUMP, "TLSv1: CertificateVerify hash", hash, hlen);
508 * RFC 2246, 4.7:
509 * In digital signing, one-way hash functions are used as input for a
510 * signing algorithm. A digitally-signed element is encoded as an
511 * opaque vector <0..2^16-1>, where the length is specified by the
512 * signing algorithm and key.
514 * In RSA signing, a 36-byte structure of two hashes (one SHA and one
515 * MD5) is signed (encrypted with the private key). It is encoded with
516 * PKCS #1 block type 0 or type 1 as described in [PKCS1].
518 signed_start = pos; /* length to be filled */
519 pos += 2;
520 clen = end - pos;
521 if (conn->cred == NULL ||
522 crypto_private_key_sign_pkcs1(conn->cred->key, hash, hlen,
523 pos, &clen) < 0) {
524 wpa_printf(MSG_DEBUG, "TLSv1: Failed to sign hash (PKCS #1)");
525 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
526 TLS_ALERT_INTERNAL_ERROR);
527 return -1;
529 WPA_PUT_BE16(signed_start, clen);
531 pos += clen;
533 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
535 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
536 rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
537 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
538 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
539 TLS_ALERT_INTERNAL_ERROR);
540 return -1;
542 pos = rhdr + rlen;
544 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
546 *msgpos = pos;
548 return 0;
552 static int tls_write_client_change_cipher_spec(struct tlsv1_client *conn,
553 u8 **msgpos, u8 *end)
555 u8 *pos, *rhdr;
556 size_t rlen;
558 pos = *msgpos;
560 wpa_printf(MSG_DEBUG, "TLSv1: Send ChangeCipherSpec");
561 rhdr = pos;
562 pos += TLS_RECORD_HEADER_LEN;
563 *pos = TLS_CHANGE_CIPHER_SPEC;
564 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC,
565 rhdr, end - rhdr, 1, &rlen) < 0) {
566 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
567 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
568 TLS_ALERT_INTERNAL_ERROR);
569 return -1;
572 if (tlsv1_record_change_write_cipher(&conn->rl) < 0) {
573 wpa_printf(MSG_DEBUG, "TLSv1: Failed to set write cipher for "
574 "record layer");
575 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
576 TLS_ALERT_INTERNAL_ERROR);
577 return -1;
580 *msgpos = rhdr + rlen;
582 return 0;
586 static int tls_write_client_finished(struct tlsv1_client *conn,
587 u8 **msgpos, u8 *end)
589 u8 *pos, *rhdr, *hs_start, *hs_length;
590 size_t rlen, hlen;
591 u8 verify_data[TLS_VERIFY_DATA_LEN];
592 u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
594 pos = *msgpos;
596 wpa_printf(MSG_DEBUG, "TLSv1: Send Finished");
598 /* Encrypted Handshake Message: Finished */
600 hlen = MD5_MAC_LEN;
601 if (conn->verify.md5_client == NULL ||
602 crypto_hash_finish(conn->verify.md5_client, hash, &hlen) < 0) {
603 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
604 TLS_ALERT_INTERNAL_ERROR);
605 conn->verify.md5_client = NULL;
606 crypto_hash_finish(conn->verify.sha1_client, NULL, NULL);
607 conn->verify.sha1_client = NULL;
608 return -1;
610 conn->verify.md5_client = NULL;
611 hlen = SHA1_MAC_LEN;
612 if (conn->verify.sha1_client == NULL ||
613 crypto_hash_finish(conn->verify.sha1_client, hash + MD5_MAC_LEN,
614 &hlen) < 0) {
615 conn->verify.sha1_client = NULL;
616 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
617 TLS_ALERT_INTERNAL_ERROR);
618 return -1;
620 conn->verify.sha1_client = NULL;
622 if (tls_prf(conn->master_secret, TLS_MASTER_SECRET_LEN,
623 "client finished", hash, MD5_MAC_LEN + SHA1_MAC_LEN,
624 verify_data, TLS_VERIFY_DATA_LEN)) {
625 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate verify_data");
626 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
627 TLS_ALERT_INTERNAL_ERROR);
628 return -1;
630 wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (client)",
631 verify_data, TLS_VERIFY_DATA_LEN);
633 rhdr = pos;
634 pos += TLS_RECORD_HEADER_LEN;
635 /* Handshake */
636 hs_start = pos;
637 /* HandshakeType msg_type */
638 *pos++ = TLS_HANDSHAKE_TYPE_FINISHED;
639 /* uint24 length (to be filled) */
640 hs_length = pos;
641 pos += 3;
642 os_memcpy(pos, verify_data, TLS_VERIFY_DATA_LEN);
643 pos += TLS_VERIFY_DATA_LEN;
644 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
645 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
647 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
648 rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
649 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
650 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
651 TLS_ALERT_INTERNAL_ERROR);
652 return -1;
655 pos = rhdr + rlen;
657 *msgpos = pos;
659 return 0;
663 static u8 * tls_send_client_key_exchange(struct tlsv1_client *conn,
664 size_t *out_len)
666 u8 *msg, *end, *pos;
667 size_t msglen;
669 *out_len = 0;
671 msglen = 1000;
672 if (conn->certificate_requested)
673 msglen += tls_client_cert_chain_der_len(conn);
675 msg = os_malloc(msglen);
676 if (msg == NULL)
677 return NULL;
679 pos = msg;
680 end = msg + msglen;
682 if (conn->certificate_requested) {
683 if (tls_write_client_certificate(conn, &pos, end) < 0) {
684 os_free(msg);
685 return NULL;
689 if (tls_write_client_key_exchange(conn, &pos, end) < 0 ||
690 (conn->certificate_requested && conn->cred && conn->cred->key &&
691 tls_write_client_certificate_verify(conn, &pos, end) < 0) ||
692 tls_write_client_change_cipher_spec(conn, &pos, end) < 0 ||
693 tls_write_client_finished(conn, &pos, end) < 0) {
694 os_free(msg);
695 return NULL;
698 *out_len = pos - msg;
700 conn->state = SERVER_CHANGE_CIPHER_SPEC;
702 return msg;
706 static u8 * tls_send_change_cipher_spec(struct tlsv1_client *conn,
707 size_t *out_len)
709 u8 *msg, *end, *pos;
711 *out_len = 0;
713 msg = os_malloc(1000);
714 if (msg == NULL)
715 return NULL;
717 pos = msg;
718 end = msg + 1000;
720 if (tls_write_client_change_cipher_spec(conn, &pos, end) < 0 ||
721 tls_write_client_finished(conn, &pos, end) < 0) {
722 os_free(msg);
723 return NULL;
726 *out_len = pos - msg;
728 wpa_printf(MSG_DEBUG, "TLSv1: Session resumption completed "
729 "successfully");
730 conn->state = ESTABLISHED;
732 return msg;
736 u8 * tlsv1_client_handshake_write(struct tlsv1_client *conn, size_t *out_len,
737 int no_appl_data)
739 switch (conn->state) {
740 case CLIENT_KEY_EXCHANGE:
741 return tls_send_client_key_exchange(conn, out_len);
742 case CHANGE_CIPHER_SPEC:
743 return tls_send_change_cipher_spec(conn, out_len);
744 case ACK_FINISHED:
745 wpa_printf(MSG_DEBUG, "TLSv1: Handshake completed "
746 "successfully");
747 conn->state = ESTABLISHED;
748 *out_len = 0;
749 if (no_appl_data) {
750 /* Need to return something to get final TLS ACK. */
751 return os_malloc(1);
753 return NULL;
754 default:
755 wpa_printf(MSG_DEBUG, "TLSv1: Unexpected state %d while "
756 "generating reply", conn->state);
757 return NULL;
762 u8 * tlsv1_client_send_alert(struct tlsv1_client *conn, u8 level,
763 u8 description, size_t *out_len)
765 u8 *alert, *pos, *length;
767 wpa_printf(MSG_DEBUG, "TLSv1: Send Alert(%d:%d)", level, description);
768 *out_len = 0;
770 alert = os_malloc(10);
771 if (alert == NULL)
772 return NULL;
774 pos = alert;
776 /* TLSPlaintext */
777 /* ContentType type */
778 *pos++ = TLS_CONTENT_TYPE_ALERT;
779 /* ProtocolVersion version */
780 WPA_PUT_BE16(pos, TLS_VERSION);
781 pos += 2;
782 /* uint16 length (to be filled) */
783 length = pos;
784 pos += 2;
785 /* opaque fragment[TLSPlaintext.length] */
787 /* Alert */
788 /* AlertLevel level */
789 *pos++ = level;
790 /* AlertDescription description */
791 *pos++ = description;
793 WPA_PUT_BE16(length, pos - length - 2);
794 *out_len = pos - alert;
796 return alert;