2 * TLSv1 server - write handshake message
3 * Copyright (c) 2006-2014, 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_server.h"
21 #include "tlsv1_server_i.h"
24 static size_t tls_server_cert_chain_der_len(struct tlsv1_server *conn)
27 struct x509_certificate *cert;
29 cert = conn->cred->cert;
31 len += 3 + cert->cert_len;
32 if (x509_certificate_self_signed(cert))
34 cert = x509_certificate_get_subject(conn->cred->trusted_certs,
42 static int tls_write_server_hello(struct tlsv1_server *conn,
45 u8 *pos, *rhdr, *hs_start, *hs_length;
51 tlsv1_server_log(conn, "Send ServerHello");
53 pos += TLS_RECORD_HEADER_LEN;
56 WPA_PUT_BE32(conn->server_random, now.sec);
57 if (random_get_bytes(conn->server_random + 4, TLS_RANDOM_LEN - 4)) {
58 wpa_printf(MSG_ERROR, "TLSv1: Could not generate "
62 wpa_hexdump(MSG_MSGDUMP, "TLSv1: server_random",
63 conn->server_random, TLS_RANDOM_LEN);
65 conn->session_id_len = TLS_SESSION_ID_MAX_LEN;
66 if (random_get_bytes(conn->session_id, conn->session_id_len)) {
67 wpa_printf(MSG_ERROR, "TLSv1: Could not generate "
71 wpa_hexdump(MSG_MSGDUMP, "TLSv1: session_id",
72 conn->session_id, conn->session_id_len);
74 /* opaque fragment[TLSPlaintext.length] */
78 /* HandshakeType msg_type */
79 *pos++ = TLS_HANDSHAKE_TYPE_SERVER_HELLO;
80 /* uint24 length (to be filled) */
83 /* body - ServerHello */
84 /* ProtocolVersion server_version */
85 WPA_PUT_BE16(pos, conn->rl.tls_version);
87 /* Random random: uint32 gmt_unix_time, opaque random_bytes */
88 os_memcpy(pos, conn->server_random, TLS_RANDOM_LEN);
89 pos += TLS_RANDOM_LEN;
90 /* SessionID session_id */
91 *pos++ = conn->session_id_len;
92 os_memcpy(pos, conn->session_id, conn->session_id_len);
93 pos += conn->session_id_len;
94 /* CipherSuite cipher_suite */
95 WPA_PUT_BE16(pos, conn->cipher_suite);
97 /* CompressionMethod compression_method */
98 *pos++ = TLS_COMPRESSION_NULL;
100 if (conn->session_ticket && conn->session_ticket_cb) {
101 int res = conn->session_ticket_cb(
102 conn->session_ticket_cb_ctx,
103 conn->session_ticket, conn->session_ticket_len,
104 conn->client_random, conn->server_random,
105 conn->master_secret);
107 tlsv1_server_log(conn, "SessionTicket callback indicated failure");
108 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
109 TLS_ALERT_HANDSHAKE_FAILURE);
112 conn->use_session_ticket = res;
114 if (conn->use_session_ticket) {
115 if (tlsv1_server_derive_keys(conn, NULL, 0) < 0) {
116 wpa_printf(MSG_DEBUG, "TLSv1: Failed to "
118 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
119 TLS_ALERT_INTERNAL_ERROR);
125 * RFC 4507 specifies that server would include an empty
126 * SessionTicket extension in ServerHello and a
127 * NewSessionTicket message after the ServerHello. However,
128 * EAP-FAST (RFC 4851), i.e., the only user of SessionTicket
129 * extension at the moment, does not use such extensions.
131 * TODO: Add support for configuring RFC 4507 behavior and make
132 * EAP-FAST disable it.
136 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
137 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
139 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
140 rhdr, end - rhdr, hs_start, pos - hs_start,
142 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create TLS record");
143 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
144 TLS_ALERT_INTERNAL_ERROR);
155 static int tls_write_server_certificate(struct tlsv1_server *conn,
156 u8 **msgpos, u8 *end)
158 u8 *pos, *rhdr, *hs_start, *hs_length, *cert_start;
160 struct x509_certificate *cert;
161 const struct tls_cipher_suite *suite;
163 suite = tls_get_cipher_suite(conn->rl.cipher_suite);
164 if (suite && suite->key_exchange == TLS_KEY_X_DH_anon) {
165 wpa_printf(MSG_DEBUG, "TLSv1: Do not send Certificate when "
166 "using anonymous DH");
172 tlsv1_server_log(conn, "Send Certificate");
174 pos += TLS_RECORD_HEADER_LEN;
176 /* opaque fragment[TLSPlaintext.length] */
180 /* HandshakeType msg_type */
181 *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE;
182 /* uint24 length (to be filled) */
185 /* body - Certificate */
186 /* uint24 length (to be filled) */
189 cert = conn->cred->cert;
191 if (pos + 3 + cert->cert_len > end) {
192 wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space "
193 "for Certificate (cert_len=%lu left=%lu)",
194 (unsigned long) cert->cert_len,
195 (unsigned long) (end - pos));
196 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
197 TLS_ALERT_INTERNAL_ERROR);
200 WPA_PUT_BE24(pos, cert->cert_len);
202 os_memcpy(pos, cert->cert_start, cert->cert_len);
203 pos += cert->cert_len;
205 if (x509_certificate_self_signed(cert))
207 cert = x509_certificate_get_subject(conn->cred->trusted_certs,
210 if (cert == conn->cred->cert || cert == NULL) {
212 * Server was not configured with all the needed certificates
213 * to form a full certificate chain. The client may fail to
214 * validate the chain unless it is configured with all the
215 * missing CA certificates.
217 wpa_printf(MSG_DEBUG, "TLSv1: Full server certificate chain "
218 "not configured - validation may fail");
220 WPA_PUT_BE24(cert_start, pos - cert_start - 3);
222 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
224 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
225 rhdr, end - rhdr, hs_start, pos - hs_start,
227 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
228 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
229 TLS_ALERT_INTERNAL_ERROR);
234 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
242 static int tls_write_server_key_exchange(struct tlsv1_server *conn,
243 u8 **msgpos, u8 *end)
245 tls_key_exchange keyx;
246 const struct tls_cipher_suite *suite;
247 u8 *pos, *rhdr, *hs_start, *hs_length, *server_params;
254 suite = tls_get_cipher_suite(conn->rl.cipher_suite);
256 keyx = TLS_KEY_X_NULL;
258 keyx = suite->key_exchange;
260 if (!tls_server_key_exchange_allowed(conn->rl.cipher_suite)) {
261 wpa_printf(MSG_DEBUG, "TLSv1: No ServerKeyExchange needed");
265 if (keyx != TLS_KEY_X_DH_anon && keyx != TLS_KEY_X_DHE_RSA) {
266 wpa_printf(MSG_DEBUG, "TLSv1: ServerKeyExchange not yet "
267 "supported with key exchange type %d", keyx);
271 if (conn->cred == NULL || conn->cred->dh_p == NULL ||
272 conn->cred->dh_g == NULL) {
273 wpa_printf(MSG_DEBUG, "TLSv1: No DH parameters available for "
274 "ServerKeyExhcange");
278 tlsv1_server_get_dh_p(conn, &dh_p, &dh_p_len);
280 os_free(conn->dh_secret);
281 conn->dh_secret_len = dh_p_len;
282 conn->dh_secret = os_malloc(conn->dh_secret_len);
283 if (conn->dh_secret == NULL) {
284 wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
285 "memory for secret (Diffie-Hellman)");
286 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
287 TLS_ALERT_INTERNAL_ERROR);
290 if (random_get_bytes(conn->dh_secret, conn->dh_secret_len)) {
291 wpa_printf(MSG_DEBUG, "TLSv1: Failed to get random "
292 "data for Diffie-Hellman");
293 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
294 TLS_ALERT_INTERNAL_ERROR);
295 os_free(conn->dh_secret);
296 conn->dh_secret = NULL;
300 if (os_memcmp(conn->dh_secret, dh_p, conn->dh_secret_len) > 0)
301 conn->dh_secret[0] = 0; /* make sure secret < p */
303 pos = conn->dh_secret;
304 while (pos + 1 < conn->dh_secret + conn->dh_secret_len && *pos == 0)
306 if (pos != conn->dh_secret) {
307 os_memmove(conn->dh_secret, pos,
308 conn->dh_secret_len - (pos - conn->dh_secret));
309 conn->dh_secret_len -= pos - conn->dh_secret;
311 wpa_hexdump_key(MSG_DEBUG, "TLSv1: DH server's secret value",
312 conn->dh_secret, conn->dh_secret_len);
314 /* Ys = g^secret mod p */
315 dh_ys_len = dh_p_len;
316 dh_ys = os_malloc(dh_ys_len);
318 wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate memory for "
320 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
321 TLS_ALERT_INTERNAL_ERROR);
324 if (crypto_mod_exp(conn->cred->dh_g, conn->cred->dh_g_len,
325 conn->dh_secret, conn->dh_secret_len,
326 dh_p, dh_p_len, dh_ys, &dh_ys_len)) {
327 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
328 TLS_ALERT_INTERNAL_ERROR);
333 wpa_hexdump(MSG_DEBUG, "TLSv1: DH Ys (server's public value)",
338 * select (KeyExchangeAlgorithm) {
339 * case diffie_hellman:
340 * ServerDHParams params;
341 * Signature signed_params;
343 * ServerRSAParams params;
344 * Signature signed_params;
346 * } ServerKeyExchange;
349 * opaque dh_p<1..2^16-1>;
350 * opaque dh_g<1..2^16-1>;
351 * opaque dh_Ys<1..2^16-1>;
357 tlsv1_server_log(conn, "Send ServerKeyExchange");
359 pos += TLS_RECORD_HEADER_LEN;
361 /* opaque fragment[TLSPlaintext.length] */
365 /* HandshakeType msg_type */
366 *pos++ = TLS_HANDSHAKE_TYPE_SERVER_KEY_EXCHANGE;
367 /* uint24 length (to be filled) */
371 /* body - ServerDHParams */
374 if (pos + 2 + dh_p_len > end) {
375 wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space for "
377 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
378 TLS_ALERT_INTERNAL_ERROR);
382 WPA_PUT_BE16(pos, dh_p_len);
384 os_memcpy(pos, dh_p, dh_p_len);
388 if (pos + 2 + conn->cred->dh_g_len > end) {
389 wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space for "
391 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
392 TLS_ALERT_INTERNAL_ERROR);
396 WPA_PUT_BE16(pos, conn->cred->dh_g_len);
398 os_memcpy(pos, conn->cred->dh_g, conn->cred->dh_g_len);
399 pos += conn->cred->dh_g_len;
402 if (pos + 2 + dh_ys_len > end) {
403 wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space for "
405 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
406 TLS_ALERT_INTERNAL_ERROR);
410 WPA_PUT_BE16(pos, dh_ys_len);
412 os_memcpy(pos, dh_ys, dh_ys_len);
417 * select (SignatureAlgorithm)
418 * { case anonymous: struct { };
420 * digitally-signed struct {
421 * opaque md5_hash[16];
422 * opaque sha_hash[20];
425 * digitally-signed struct {
426 * opaque sha_hash[20];
431 * MD5(ClientHello.random + ServerHello.random + ServerParams);
434 * SHA(ClientHello.random + ServerHello.random + ServerParams);
437 if (keyx == TLS_KEY_X_DHE_RSA) {
443 if (conn->rl.tls_version >= TLS_VERSION_1_2) {
445 hlen = tlsv12_key_x_server_params_hash(
446 conn->rl.tls_version, conn->client_random,
447 conn->server_random, server_params,
448 pos - server_params, hash + 19);
452 * TLS v1.2 adds explicit indication of the used
453 * signature and hash algorithms.
456 * HashAlgorithm hash;
457 * SignatureAlgorithm signature;
458 * } SignatureAndHashAlgorithm;
460 if (hlen < 0 || pos + 2 > end) {
461 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
462 TLS_ALERT_INTERNAL_ERROR);
465 *pos++ = TLS_HASH_ALG_SHA256;
466 *pos++ = TLS_SIGN_ALG_RSA;
469 * RFC 3447, A.2.4 RSASSA-PKCS1-v1_5
471 * DigestInfo ::= SEQUENCE {
472 * digestAlgorithm DigestAlgorithm,
473 * digest OCTET STRING
476 * SHA-256 OID: sha256WithRSAEncryption ::= {pkcs-1 11}
478 * DER encoded DigestInfo for SHA256 per RFC 3447:
479 * 30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05 00
484 "\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65"
485 "\x03\x04\x02\x01\x05\x00\x04\x20", 19);
487 #else /* CONFIG_TLSV12 */
488 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
489 TLS_ALERT_INTERNAL_ERROR);
491 #endif /* CONFIG_TLSV12 */
493 hlen = tls_key_x_server_params_hash(
494 conn->rl.tls_version, conn->client_random,
495 conn->server_random, server_params,
496 pos - server_params, hash);
500 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
501 TLS_ALERT_INTERNAL_ERROR);
505 wpa_hexdump(MSG_MSGDUMP, "TLS: ServerKeyExchange signed_params hash",
507 #ifdef CONFIG_TESTING_OPTIONS
508 if (conn->test_flags & TLS_BREAK_SRV_KEY_X_HASH) {
509 tlsv1_server_log(conn, "TESTING: Break ServerKeyExchange signed params hash");
510 hash[hlen - 1] ^= 0x80;
512 #endif /* CONFIG_TESTING_OPTIONS */
516 * In digital signing, one-way hash functions are used as input
517 * for a signing algorithm. A digitally-signed element is
518 * encoded as an opaque vector <0..2^16-1>, where the length is
519 * specified by the signing algorithm and key.
521 * In RSA signing, a 36-byte structure of two hashes (one SHA
522 * and one MD5) is signed (encrypted with the private key). It
523 * is encoded with PKCS #1 block type 0 or type 1 as described
526 signed_start = pos; /* length to be filled */
529 if (conn->cred == NULL ||
530 crypto_private_key_sign_pkcs1(conn->cred->key, hash, hlen,
532 wpa_printf(MSG_DEBUG, "TLSv1: Failed to sign hash (PKCS #1)");
533 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
534 TLS_ALERT_INTERNAL_ERROR);
537 WPA_PUT_BE16(signed_start, clen);
538 #ifdef CONFIG_TESTING_OPTIONS
539 if (conn->test_flags & TLS_BREAK_SRV_KEY_X_SIGNATURE) {
540 tlsv1_server_log(conn, "TESTING: Break ServerKeyExchange signed params signature");
541 pos[clen - 1] ^= 0x80;
543 #endif /* CONFIG_TESTING_OPTIONS */
548 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
550 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
551 rhdr, end - rhdr, hs_start, pos - hs_start,
553 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
554 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
555 TLS_ALERT_INTERNAL_ERROR);
560 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
568 static int tls_write_server_certificate_request(struct tlsv1_server *conn,
569 u8 **msgpos, u8 *end)
571 u8 *pos, *rhdr, *hs_start, *hs_length;
574 if (!conn->verify_peer) {
575 wpa_printf(MSG_DEBUG, "TLSv1: No CertificateRequest needed");
581 tlsv1_server_log(conn, "Send CertificateRequest");
583 pos += TLS_RECORD_HEADER_LEN;
585 /* opaque fragment[TLSPlaintext.length] */
589 /* HandshakeType msg_type */
590 *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST;
591 /* uint24 length (to be filled) */
594 /* body - CertificateRequest */
598 * rsa_sign(1), dss_sign(2), rsa_fixed_dh(3), dss_fixed_dh(4),
600 * } ClientCertificateType;
601 * ClientCertificateType certificate_types<1..2^8-1>
604 *pos++ = 1; /* rsa_sign */
607 * opaque DistinguishedName<1..2^16-1>
608 * DistinguishedName certificate_authorities<3..2^16-1>
610 /* TODO: add support for listing DNs for trusted CAs */
611 WPA_PUT_BE16(pos, 0);
614 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
616 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
617 rhdr, end - rhdr, hs_start, pos - hs_start,
619 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
620 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
621 TLS_ALERT_INTERNAL_ERROR);
626 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
634 static int tls_write_server_hello_done(struct tlsv1_server *conn,
635 u8 **msgpos, u8 *end)
641 tlsv1_server_log(conn, "Send ServerHelloDone");
643 /* opaque fragment[TLSPlaintext.length] */
647 /* HandshakeType msg_type */
648 *pos++ = TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE;
650 WPA_PUT_BE24(pos, 0);
652 /* body - ServerHelloDone (empty) */
654 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
655 *msgpos, end - *msgpos, payload, pos - payload,
657 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
658 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
659 TLS_ALERT_INTERNAL_ERROR);
663 tls_verify_hash_add(&conn->verify, payload, pos - payload);
671 static int tls_write_server_change_cipher_spec(struct tlsv1_server *conn,
672 u8 **msgpos, u8 *end)
677 tlsv1_server_log(conn, "Send ChangeCipherSpec");
679 payload[0] = TLS_CHANGE_CIPHER_SPEC;
681 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC,
682 *msgpos, end - *msgpos, payload, sizeof(payload),
684 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
685 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
686 TLS_ALERT_INTERNAL_ERROR);
690 if (tlsv1_record_change_write_cipher(&conn->rl) < 0) {
691 wpa_printf(MSG_DEBUG, "TLSv1: Failed to set write cipher for "
693 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
694 TLS_ALERT_INTERNAL_ERROR);
704 static int tls_write_server_finished(struct tlsv1_server *conn,
705 u8 **msgpos, u8 *end)
709 u8 verify_data[1 + 3 + TLS_VERIFY_DATA_LEN];
710 u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
714 tlsv1_server_log(conn, "Send Finished");
716 /* Encrypted Handshake Message: Finished */
719 if (conn->rl.tls_version >= TLS_VERSION_1_2) {
720 hlen = SHA256_MAC_LEN;
721 if (conn->verify.sha256_server == NULL ||
722 crypto_hash_finish(conn->verify.sha256_server, hash, &hlen)
724 conn->verify.sha256_server = NULL;
725 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
726 TLS_ALERT_INTERNAL_ERROR);
729 conn->verify.sha256_server = NULL;
731 #endif /* CONFIG_TLSV12 */
734 if (conn->verify.md5_server == NULL ||
735 crypto_hash_finish(conn->verify.md5_server, hash, &hlen) < 0) {
736 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
737 TLS_ALERT_INTERNAL_ERROR);
738 conn->verify.md5_server = NULL;
739 crypto_hash_finish(conn->verify.sha1_server, NULL, NULL);
740 conn->verify.sha1_server = NULL;
743 conn->verify.md5_server = NULL;
745 if (conn->verify.sha1_server == NULL ||
746 crypto_hash_finish(conn->verify.sha1_server, hash + MD5_MAC_LEN,
748 conn->verify.sha1_server = NULL;
749 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
750 TLS_ALERT_INTERNAL_ERROR);
753 conn->verify.sha1_server = NULL;
754 hlen = MD5_MAC_LEN + SHA1_MAC_LEN;
758 #endif /* CONFIG_TLSV12 */
760 if (tls_prf(conn->rl.tls_version,
761 conn->master_secret, TLS_MASTER_SECRET_LEN,
762 "server finished", hash, hlen,
763 verify_data + 1 + 3, TLS_VERIFY_DATA_LEN)) {
764 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate verify_data");
765 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
766 TLS_ALERT_INTERNAL_ERROR);
769 wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (server)",
770 verify_data + 1 + 3, TLS_VERIFY_DATA_LEN);
771 #ifdef CONFIG_TESTING_OPTIONS
772 if (conn->test_flags & TLS_BREAK_VERIFY_DATA) {
773 tlsv1_server_log(conn, "TESTING: Break verify_data (server)");
774 verify_data[1 + 3 + 1] ^= 0x80;
776 #endif /* CONFIG_TESTING_OPTIONS */
779 pos = hs_start = verify_data;
780 /* HandshakeType msg_type */
781 *pos++ = TLS_HANDSHAKE_TYPE_FINISHED;
783 WPA_PUT_BE24(pos, TLS_VERIFY_DATA_LEN);
785 pos += TLS_VERIFY_DATA_LEN;
786 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
788 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
789 *msgpos, end - *msgpos, hs_start, pos - hs_start,
791 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
792 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
793 TLS_ALERT_INTERNAL_ERROR);
803 static u8 * tls_send_server_hello(struct tlsv1_server *conn, size_t *out_len)
810 msglen = 1000 + tls_server_cert_chain_der_len(conn);
812 msg = os_malloc(msglen);
819 if (tls_write_server_hello(conn, &pos, end) < 0) {
824 if (conn->use_session_ticket) {
825 /* Abbreviated handshake using session ticket; RFC 4507 */
826 if (tls_write_server_change_cipher_spec(conn, &pos, end) < 0 ||
827 tls_write_server_finished(conn, &pos, end) < 0) {
832 *out_len = pos - msg;
834 conn->state = CHANGE_CIPHER_SPEC;
840 if (tls_write_server_certificate(conn, &pos, end) < 0 ||
841 tls_write_server_key_exchange(conn, &pos, end) < 0 ||
842 tls_write_server_certificate_request(conn, &pos, end) < 0 ||
843 tls_write_server_hello_done(conn, &pos, end) < 0) {
848 *out_len = pos - msg;
850 conn->state = CLIENT_CERTIFICATE;
856 static u8 * tls_send_change_cipher_spec(struct tlsv1_server *conn,
863 msg = os_malloc(1000);
870 if (tls_write_server_change_cipher_spec(conn, &pos, end) < 0 ||
871 tls_write_server_finished(conn, &pos, end) < 0) {
876 *out_len = pos - msg;
878 tlsv1_server_log(conn, "Handshake completed successfully");
879 conn->state = ESTABLISHED;
885 u8 * tlsv1_server_handshake_write(struct tlsv1_server *conn, size_t *out_len)
887 switch (conn->state) {
889 return tls_send_server_hello(conn, out_len);
890 case SERVER_CHANGE_CIPHER_SPEC:
891 return tls_send_change_cipher_spec(conn, out_len);
893 if (conn->state == ESTABLISHED && conn->use_session_ticket) {
894 /* Abbreviated handshake was already completed. */
897 tlsv1_server_log(conn, "Unexpected state %d while generating reply",
904 u8 * tlsv1_server_send_alert(struct tlsv1_server *conn, u8 level,
905 u8 description, size_t *out_len)
907 u8 *alert, *pos, *length;
909 tlsv1_server_log(conn, "Send Alert(%d:%d)", level, description);
912 alert = os_malloc(10);
919 /* ContentType type */
920 *pos++ = TLS_CONTENT_TYPE_ALERT;
921 /* ProtocolVersion version */
922 WPA_PUT_BE16(pos, conn->rl.tls_version ? conn->rl.tls_version :
925 /* uint16 length (to be filled) */
928 /* opaque fragment[TLSPlaintext.length] */
931 /* AlertLevel level */
933 /* AlertDescription description */
934 *pos++ = description;
936 WPA_PUT_BE16(length, pos - length - 2);
937 *out_len = pos - alert;