2 * TLSv1 server - 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.
18 #include "crypto/md5.h"
19 #include "crypto/sha1.h"
20 #include "crypto/tls.h"
22 #include "tlsv1_common.h"
23 #include "tlsv1_record.h"
24 #include "tlsv1_server.h"
25 #include "tlsv1_server_i.h"
28 static size_t tls_server_cert_chain_der_len(struct tlsv1_server *conn)
31 struct x509_certificate *cert;
33 cert = conn->cred->cert;
35 len += 3 + cert->cert_len;
36 if (x509_certificate_self_signed(cert))
38 cert = x509_certificate_get_subject(conn->cred->trusted_certs,
46 static int tls_write_server_hello(struct tlsv1_server *conn,
49 u8 *pos, *rhdr, *hs_start, *hs_length;
55 wpa_printf(MSG_DEBUG, "TLSv1: Send ServerHello");
57 pos += TLS_RECORD_HEADER_LEN;
60 WPA_PUT_BE32(conn->server_random, now.sec);
61 if (os_get_random(conn->server_random + 4, TLS_RANDOM_LEN - 4)) {
62 wpa_printf(MSG_ERROR, "TLSv1: Could not generate "
66 wpa_hexdump(MSG_MSGDUMP, "TLSv1: server_random",
67 conn->server_random, TLS_RANDOM_LEN);
69 conn->session_id_len = TLS_SESSION_ID_MAX_LEN;
70 if (os_get_random(conn->session_id, conn->session_id_len)) {
71 wpa_printf(MSG_ERROR, "TLSv1: Could not generate "
75 wpa_hexdump(MSG_MSGDUMP, "TLSv1: session_id",
76 conn->session_id, conn->session_id_len);
78 /* opaque fragment[TLSPlaintext.length] */
82 /* HandshakeType msg_type */
83 *pos++ = TLS_HANDSHAKE_TYPE_SERVER_HELLO;
84 /* uint24 length (to be filled) */
87 /* body - ServerHello */
88 /* ProtocolVersion server_version */
89 WPA_PUT_BE16(pos, TLS_VERSION);
91 /* Random random: uint32 gmt_unix_time, opaque random_bytes */
92 os_memcpy(pos, conn->server_random, TLS_RANDOM_LEN);
93 pos += TLS_RANDOM_LEN;
94 /* SessionID session_id */
95 *pos++ = conn->session_id_len;
96 os_memcpy(pos, conn->session_id, conn->session_id_len);
97 pos += conn->session_id_len;
98 /* CipherSuite cipher_suite */
99 WPA_PUT_BE16(pos, conn->cipher_suite);
101 /* CompressionMethod compression_method */
102 *pos++ = TLS_COMPRESSION_NULL;
104 if (conn->session_ticket && conn->session_ticket_cb) {
105 int res = conn->session_ticket_cb(
106 conn->session_ticket_cb_ctx,
107 conn->session_ticket, conn->session_ticket_len,
108 conn->client_random, conn->server_random,
109 conn->master_secret);
111 wpa_printf(MSG_DEBUG, "TLSv1: SessionTicket callback "
112 "indicated failure");
113 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
114 TLS_ALERT_HANDSHAKE_FAILURE);
117 conn->use_session_ticket = res;
119 if (conn->use_session_ticket) {
120 if (tlsv1_server_derive_keys(conn, NULL, 0) < 0) {
121 wpa_printf(MSG_DEBUG, "TLSv1: Failed to "
123 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
124 TLS_ALERT_INTERNAL_ERROR);
130 * RFC 4507 specifies that server would include an empty
131 * SessionTicket extension in ServerHello and a
132 * NewSessionTicket message after the ServerHello. However,
133 * EAP-FAST (RFC 4851), i.e., the only user of SessionTicket
134 * extension at the moment, does not use such extensions.
136 * TODO: Add support for configuring RFC 4507 behavior and make
137 * EAP-FAST disable it.
141 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
142 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
144 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
145 rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
146 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create TLS record");
147 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
148 TLS_ALERT_INTERNAL_ERROR);
159 static int tls_write_server_certificate(struct tlsv1_server *conn,
160 u8 **msgpos, u8 *end)
162 u8 *pos, *rhdr, *hs_start, *hs_length, *cert_start;
164 struct x509_certificate *cert;
165 const struct tls_cipher_suite *suite;
167 suite = tls_get_cipher_suite(conn->rl.cipher_suite);
168 if (suite && suite->key_exchange == TLS_KEY_X_DH_anon) {
169 wpa_printf(MSG_DEBUG, "TLSv1: Do not send Certificate when "
170 "using anonymous DH");
176 wpa_printf(MSG_DEBUG, "TLSv1: Send Certificate");
178 pos += TLS_RECORD_HEADER_LEN;
180 /* opaque fragment[TLSPlaintext.length] */
184 /* HandshakeType msg_type */
185 *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE;
186 /* uint24 length (to be filled) */
189 /* body - Certificate */
190 /* uint24 length (to be filled) */
193 cert = conn->cred->cert;
195 if (pos + 3 + cert->cert_len > end) {
196 wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space "
197 "for Certificate (cert_len=%lu left=%lu)",
198 (unsigned long) cert->cert_len,
199 (unsigned long) (end - pos));
200 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
201 TLS_ALERT_INTERNAL_ERROR);
204 WPA_PUT_BE24(pos, cert->cert_len);
206 os_memcpy(pos, cert->cert_start, cert->cert_len);
207 pos += cert->cert_len;
209 if (x509_certificate_self_signed(cert))
211 cert = x509_certificate_get_subject(conn->cred->trusted_certs,
214 if (cert == conn->cred->cert || cert == NULL) {
216 * Server was not configured with all the needed certificates
217 * to form a full certificate chain. The client may fail to
218 * validate the chain unless it is configured with all the
219 * missing CA certificates.
221 wpa_printf(MSG_DEBUG, "TLSv1: Full server certificate chain "
222 "not configured - validation may fail");
224 WPA_PUT_BE24(cert_start, pos - cert_start - 3);
226 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
228 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
229 rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
230 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
231 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
232 TLS_ALERT_INTERNAL_ERROR);
237 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
245 static int tls_write_server_key_exchange(struct tlsv1_server *conn,
246 u8 **msgpos, u8 *end)
248 tls_key_exchange keyx;
249 const struct tls_cipher_suite *suite;
250 u8 *pos, *rhdr, *hs_start, *hs_length;
255 suite = tls_get_cipher_suite(conn->rl.cipher_suite);
257 keyx = TLS_KEY_X_NULL;
259 keyx = suite->key_exchange;
261 if (!tls_server_key_exchange_allowed(conn->rl.cipher_suite)) {
262 wpa_printf(MSG_DEBUG, "TLSv1: No ServerKeyExchange needed");
266 if (keyx != TLS_KEY_X_DH_anon) {
268 wpa_printf(MSG_DEBUG, "TLSv1: ServerKeyExchange not yet "
269 "supported with key exchange type %d", keyx);
273 if (conn->cred == NULL || conn->cred->dh_p == NULL ||
274 conn->cred->dh_g == NULL) {
275 wpa_printf(MSG_DEBUG, "TLSv1: No DH parameters available for "
276 "ServerKeyExhcange");
280 os_free(conn->dh_secret);
281 conn->dh_secret_len = conn->cred->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 (os_get_random(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, conn->cred->dh_p, conn->dh_secret_len) >
302 conn->dh_secret[0] = 0; /* make sure secret < p */
304 pos = conn->dh_secret;
305 while (pos + 1 < conn->dh_secret + conn->dh_secret_len && *pos == 0)
307 if (pos != conn->dh_secret) {
308 os_memmove(conn->dh_secret, pos,
309 conn->dh_secret_len - (pos - conn->dh_secret));
310 conn->dh_secret_len -= pos - conn->dh_secret;
312 wpa_hexdump_key(MSG_DEBUG, "TLSv1: DH server's secret value",
313 conn->dh_secret, conn->dh_secret_len);
315 /* Ys = g^secret mod p */
316 dh_ys_len = conn->cred->dh_p_len;
317 dh_ys = os_malloc(dh_ys_len);
319 wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate memory for "
321 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
322 TLS_ALERT_INTERNAL_ERROR);
325 if (crypto_mod_exp(conn->cred->dh_g, conn->cred->dh_g_len,
326 conn->dh_secret, conn->dh_secret_len,
327 conn->cred->dh_p, conn->cred->dh_p_len,
328 dh_ys, &dh_ys_len)) {
329 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
330 TLS_ALERT_INTERNAL_ERROR);
335 wpa_hexdump(MSG_DEBUG, "TLSv1: DH Ys (server's public value)",
340 * select (KeyExchangeAlgorithm) {
341 * case diffie_hellman:
342 * ServerDHParams params;
343 * Signature signed_params;
345 * ServerRSAParams params;
346 * Signature signed_params;
348 * } ServerKeyExchange;
351 * opaque dh_p<1..2^16-1>;
352 * opaque dh_g<1..2^16-1>;
353 * opaque dh_Ys<1..2^16-1>;
359 wpa_printf(MSG_DEBUG, "TLSv1: Send ServerKeyExchange");
361 pos += TLS_RECORD_HEADER_LEN;
363 /* opaque fragment[TLSPlaintext.length] */
367 /* HandshakeType msg_type */
368 *pos++ = TLS_HANDSHAKE_TYPE_SERVER_KEY_EXCHANGE;
369 /* uint24 length (to be filled) */
373 /* body - ServerDHParams */
375 if (pos + 2 + conn->cred->dh_p_len > end) {
376 wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space for "
378 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
379 TLS_ALERT_INTERNAL_ERROR);
383 WPA_PUT_BE16(pos, conn->cred->dh_p_len);
385 os_memcpy(pos, conn->cred->dh_p, conn->cred->dh_p_len);
386 pos += conn->cred->dh_p_len;
389 if (pos + 2 + conn->cred->dh_g_len > end) {
390 wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space for "
392 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
393 TLS_ALERT_INTERNAL_ERROR);
397 WPA_PUT_BE16(pos, conn->cred->dh_g_len);
399 os_memcpy(pos, conn->cred->dh_g, conn->cred->dh_g_len);
400 pos += conn->cred->dh_g_len;
403 if (pos + 2 + dh_ys_len > end) {
404 wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space for "
406 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
407 TLS_ALERT_INTERNAL_ERROR);
411 WPA_PUT_BE16(pos, dh_ys_len);
413 os_memcpy(pos, dh_ys, dh_ys_len);
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 generate a record");
422 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
423 TLS_ALERT_INTERNAL_ERROR);
428 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
436 static int tls_write_server_certificate_request(struct tlsv1_server *conn,
437 u8 **msgpos, u8 *end)
439 u8 *pos, *rhdr, *hs_start, *hs_length;
442 if (!conn->verify_peer) {
443 wpa_printf(MSG_DEBUG, "TLSv1: No CertificateRequest needed");
449 wpa_printf(MSG_DEBUG, "TLSv1: Send CertificateRequest");
451 pos += TLS_RECORD_HEADER_LEN;
453 /* opaque fragment[TLSPlaintext.length] */
457 /* HandshakeType msg_type */
458 *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST;
459 /* uint24 length (to be filled) */
462 /* body - CertificateRequest */
466 * rsa_sign(1), dss_sign(2), rsa_fixed_dh(3), dss_fixed_dh(4),
468 * } ClientCertificateType;
469 * ClientCertificateType certificate_types<1..2^8-1>
472 *pos++ = 1; /* rsa_sign */
475 * opaque DistinguishedName<1..2^16-1>
476 * DistinguishedName certificate_authorities<3..2^16-1>
478 /* TODO: add support for listing DNs for trusted CAs */
479 WPA_PUT_BE16(pos, 0);
482 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
484 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
485 rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
486 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
487 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
488 TLS_ALERT_INTERNAL_ERROR);
493 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
501 static int tls_write_server_hello_done(struct tlsv1_server *conn,
502 u8 **msgpos, u8 *end)
504 u8 *pos, *rhdr, *hs_start, *hs_length;
509 wpa_printf(MSG_DEBUG, "TLSv1: Send ServerHelloDone");
511 pos += TLS_RECORD_HEADER_LEN;
513 /* opaque fragment[TLSPlaintext.length] */
517 /* HandshakeType msg_type */
518 *pos++ = TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE;
519 /* uint24 length (to be filled) */
522 /* body - ServerHelloDone (empty) */
524 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
526 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
527 rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
528 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
529 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
530 TLS_ALERT_INTERNAL_ERROR);
535 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
543 static int tls_write_server_change_cipher_spec(struct tlsv1_server *conn,
544 u8 **msgpos, u8 *end)
551 wpa_printf(MSG_DEBUG, "TLSv1: Send ChangeCipherSpec");
553 pos += TLS_RECORD_HEADER_LEN;
554 *pos = TLS_CHANGE_CIPHER_SPEC;
555 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC,
556 rhdr, end - rhdr, 1, &rlen) < 0) {
557 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
558 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
559 TLS_ALERT_INTERNAL_ERROR);
563 if (tlsv1_record_change_write_cipher(&conn->rl) < 0) {
564 wpa_printf(MSG_DEBUG, "TLSv1: Failed to set write cipher for "
566 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
567 TLS_ALERT_INTERNAL_ERROR);
571 *msgpos = rhdr + rlen;
577 static int tls_write_server_finished(struct tlsv1_server *conn,
578 u8 **msgpos, u8 *end)
580 u8 *pos, *rhdr, *hs_start, *hs_length;
582 u8 verify_data[TLS_VERIFY_DATA_LEN];
583 u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
587 wpa_printf(MSG_DEBUG, "TLSv1: Send Finished");
589 /* Encrypted Handshake Message: Finished */
592 if (conn->verify.md5_server == NULL ||
593 crypto_hash_finish(conn->verify.md5_server, hash, &hlen) < 0) {
594 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
595 TLS_ALERT_INTERNAL_ERROR);
596 conn->verify.md5_server = NULL;
597 crypto_hash_finish(conn->verify.sha1_server, NULL, NULL);
598 conn->verify.sha1_server = NULL;
601 conn->verify.md5_server = NULL;
603 if (conn->verify.sha1_server == NULL ||
604 crypto_hash_finish(conn->verify.sha1_server, hash + MD5_MAC_LEN,
606 conn->verify.sha1_server = NULL;
607 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
608 TLS_ALERT_INTERNAL_ERROR);
611 conn->verify.sha1_server = NULL;
613 if (tls_prf(conn->master_secret, TLS_MASTER_SECRET_LEN,
614 "server finished", hash, MD5_MAC_LEN + SHA1_MAC_LEN,
615 verify_data, TLS_VERIFY_DATA_LEN)) {
616 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate verify_data");
617 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
618 TLS_ALERT_INTERNAL_ERROR);
621 wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (server)",
622 verify_data, TLS_VERIFY_DATA_LEN);
625 pos += TLS_RECORD_HEADER_LEN;
628 /* HandshakeType msg_type */
629 *pos++ = TLS_HANDSHAKE_TYPE_FINISHED;
630 /* uint24 length (to be filled) */
633 os_memcpy(pos, verify_data, TLS_VERIFY_DATA_LEN);
634 pos += TLS_VERIFY_DATA_LEN;
635 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
636 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
638 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
639 rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
640 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
641 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
642 TLS_ALERT_INTERNAL_ERROR);
654 static u8 * tls_send_server_hello(struct tlsv1_server *conn, size_t *out_len)
661 msglen = 1000 + tls_server_cert_chain_der_len(conn);
663 msg = os_malloc(msglen);
670 if (tls_write_server_hello(conn, &pos, end) < 0) {
675 if (conn->use_session_ticket) {
676 /* Abbreviated handshake using session ticket; RFC 4507 */
677 if (tls_write_server_change_cipher_spec(conn, &pos, end) < 0 ||
678 tls_write_server_finished(conn, &pos, end) < 0) {
683 *out_len = pos - msg;
685 conn->state = CHANGE_CIPHER_SPEC;
691 if (tls_write_server_certificate(conn, &pos, end) < 0 ||
692 tls_write_server_key_exchange(conn, &pos, end) < 0 ||
693 tls_write_server_certificate_request(conn, &pos, end) < 0 ||
694 tls_write_server_hello_done(conn, &pos, end) < 0) {
699 *out_len = pos - msg;
701 conn->state = CLIENT_CERTIFICATE;
707 static u8 * tls_send_change_cipher_spec(struct tlsv1_server *conn,
714 msg = os_malloc(1000);
721 if (tls_write_server_change_cipher_spec(conn, &pos, end) < 0 ||
722 tls_write_server_finished(conn, &pos, end) < 0) {
727 *out_len = pos - msg;
729 wpa_printf(MSG_DEBUG, "TLSv1: Handshake completed successfully");
730 conn->state = ESTABLISHED;
736 u8 * tlsv1_server_handshake_write(struct tlsv1_server *conn, size_t *out_len)
738 switch (conn->state) {
740 return tls_send_server_hello(conn, out_len);
741 case SERVER_CHANGE_CIPHER_SPEC:
742 return tls_send_change_cipher_spec(conn, out_len);
744 if (conn->state == ESTABLISHED && conn->use_session_ticket) {
745 /* Abbreviated handshake was already completed. */
748 wpa_printf(MSG_DEBUG, "TLSv1: Unexpected state %d while "
749 "generating reply", conn->state);
755 u8 * tlsv1_server_send_alert(struct tlsv1_server *conn, u8 level,
756 u8 description, size_t *out_len)
758 u8 *alert, *pos, *length;
760 wpa_printf(MSG_DEBUG, "TLSv1: Send Alert(%d:%d)", level, description);
763 alert = os_malloc(10);
770 /* ContentType type */
771 *pos++ = TLS_CONTENT_TYPE_ALERT;
772 /* ProtocolVersion version */
773 WPA_PUT_BE16(pos, TLS_VERSION);
775 /* uint16 length (to be filled) */
778 /* opaque fragment[TLSPlaintext.length] */
781 /* AlertLevel level */
783 /* AlertDescription description */
784 *pos++ = description;
786 WPA_PUT_BE16(length, pos - length - 2);
787 *out_len = pos - alert;