2 * TLSv1 server - 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_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 wpa_printf(MSG_DEBUG, "TLSv1: 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 wpa_printf(MSG_DEBUG, "TLSv1: SessionTicket callback "
108 "indicated failure");
109 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
110 TLS_ALERT_HANDSHAKE_FAILURE);
113 conn->use_session_ticket = res;
115 if (conn->use_session_ticket) {
116 if (tlsv1_server_derive_keys(conn, NULL, 0) < 0) {
117 wpa_printf(MSG_DEBUG, "TLSv1: Failed to "
119 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
120 TLS_ALERT_INTERNAL_ERROR);
126 * RFC 4507 specifies that server would include an empty
127 * SessionTicket extension in ServerHello and a
128 * NewSessionTicket message after the ServerHello. However,
129 * EAP-FAST (RFC 4851), i.e., the only user of SessionTicket
130 * extension at the moment, does not use such extensions.
132 * TODO: Add support for configuring RFC 4507 behavior and make
133 * EAP-FAST disable it.
137 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
138 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
140 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
141 rhdr, end - rhdr, hs_start, pos - hs_start,
143 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create TLS record");
144 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
145 TLS_ALERT_INTERNAL_ERROR);
156 static int tls_write_server_certificate(struct tlsv1_server *conn,
157 u8 **msgpos, u8 *end)
159 u8 *pos, *rhdr, *hs_start, *hs_length, *cert_start;
161 struct x509_certificate *cert;
162 const struct tls_cipher_suite *suite;
164 suite = tls_get_cipher_suite(conn->rl.cipher_suite);
165 if (suite && suite->key_exchange == TLS_KEY_X_DH_anon) {
166 wpa_printf(MSG_DEBUG, "TLSv1: Do not send Certificate when "
167 "using anonymous DH");
173 wpa_printf(MSG_DEBUG, "TLSv1: Send Certificate");
175 pos += TLS_RECORD_HEADER_LEN;
177 /* opaque fragment[TLSPlaintext.length] */
181 /* HandshakeType msg_type */
182 *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE;
183 /* uint24 length (to be filled) */
186 /* body - Certificate */
187 /* uint24 length (to be filled) */
190 cert = conn->cred->cert;
192 if (pos + 3 + cert->cert_len > end) {
193 wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space "
194 "for Certificate (cert_len=%lu left=%lu)",
195 (unsigned long) cert->cert_len,
196 (unsigned long) (end - pos));
197 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
198 TLS_ALERT_INTERNAL_ERROR);
201 WPA_PUT_BE24(pos, cert->cert_len);
203 os_memcpy(pos, cert->cert_start, cert->cert_len);
204 pos += cert->cert_len;
206 if (x509_certificate_self_signed(cert))
208 cert = x509_certificate_get_subject(conn->cred->trusted_certs,
211 if (cert == conn->cred->cert || cert == NULL) {
213 * Server was not configured with all the needed certificates
214 * to form a full certificate chain. The client may fail to
215 * validate the chain unless it is configured with all the
216 * missing CA certificates.
218 wpa_printf(MSG_DEBUG, "TLSv1: Full server certificate chain "
219 "not configured - validation may fail");
221 WPA_PUT_BE24(cert_start, pos - cert_start - 3);
223 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
225 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
226 rhdr, end - rhdr, hs_start, pos - hs_start,
228 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
229 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
230 TLS_ALERT_INTERNAL_ERROR);
235 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
243 static int tls_write_server_key_exchange(struct tlsv1_server *conn,
244 u8 **msgpos, u8 *end)
246 tls_key_exchange keyx;
247 const struct tls_cipher_suite *suite;
248 u8 *pos, *rhdr, *hs_start, *hs_length;
253 suite = tls_get_cipher_suite(conn->rl.cipher_suite);
255 keyx = TLS_KEY_X_NULL;
257 keyx = suite->key_exchange;
259 if (!tls_server_key_exchange_allowed(conn->rl.cipher_suite)) {
260 wpa_printf(MSG_DEBUG, "TLSv1: No ServerKeyExchange needed");
264 if (keyx != TLS_KEY_X_DH_anon) {
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 os_free(conn->dh_secret);
279 conn->dh_secret_len = conn->cred->dh_p_len;
280 conn->dh_secret = os_malloc(conn->dh_secret_len);
281 if (conn->dh_secret == NULL) {
282 wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
283 "memory for secret (Diffie-Hellman)");
284 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
285 TLS_ALERT_INTERNAL_ERROR);
288 if (random_get_bytes(conn->dh_secret, conn->dh_secret_len)) {
289 wpa_printf(MSG_DEBUG, "TLSv1: Failed to get random "
290 "data for Diffie-Hellman");
291 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
292 TLS_ALERT_INTERNAL_ERROR);
293 os_free(conn->dh_secret);
294 conn->dh_secret = NULL;
298 if (os_memcmp(conn->dh_secret, conn->cred->dh_p, conn->dh_secret_len) >
300 conn->dh_secret[0] = 0; /* make sure secret < p */
302 pos = conn->dh_secret;
303 while (pos + 1 < conn->dh_secret + conn->dh_secret_len && *pos == 0)
305 if (pos != conn->dh_secret) {
306 os_memmove(conn->dh_secret, pos,
307 conn->dh_secret_len - (pos - conn->dh_secret));
308 conn->dh_secret_len -= pos - conn->dh_secret;
310 wpa_hexdump_key(MSG_DEBUG, "TLSv1: DH server's secret value",
311 conn->dh_secret, conn->dh_secret_len);
313 /* Ys = g^secret mod p */
314 dh_ys_len = conn->cred->dh_p_len;
315 dh_ys = os_malloc(dh_ys_len);
317 wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate memory for "
319 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
320 TLS_ALERT_INTERNAL_ERROR);
323 if (crypto_mod_exp(conn->cred->dh_g, conn->cred->dh_g_len,
324 conn->dh_secret, conn->dh_secret_len,
325 conn->cred->dh_p, conn->cred->dh_p_len,
326 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 wpa_printf(MSG_DEBUG, "TLSv1: 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 */
373 if (pos + 2 + conn->cred->dh_p_len > end) {
374 wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space for "
376 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
377 TLS_ALERT_INTERNAL_ERROR);
381 WPA_PUT_BE16(pos, conn->cred->dh_p_len);
383 os_memcpy(pos, conn->cred->dh_p, conn->cred->dh_p_len);
384 pos += conn->cred->dh_p_len;
387 if (pos + 2 + conn->cred->dh_g_len > end) {
388 wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space for "
390 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
391 TLS_ALERT_INTERNAL_ERROR);
395 WPA_PUT_BE16(pos, conn->cred->dh_g_len);
397 os_memcpy(pos, conn->cred->dh_g, conn->cred->dh_g_len);
398 pos += conn->cred->dh_g_len;
401 if (pos + 2 + dh_ys_len > end) {
402 wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space for "
404 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
405 TLS_ALERT_INTERNAL_ERROR);
409 WPA_PUT_BE16(pos, dh_ys_len);
411 os_memcpy(pos, dh_ys, dh_ys_len);
415 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
417 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
418 rhdr, end - rhdr, hs_start, pos - hs_start,
420 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
421 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
422 TLS_ALERT_INTERNAL_ERROR);
427 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
435 static int tls_write_server_certificate_request(struct tlsv1_server *conn,
436 u8 **msgpos, u8 *end)
438 u8 *pos, *rhdr, *hs_start, *hs_length;
441 if (!conn->verify_peer) {
442 wpa_printf(MSG_DEBUG, "TLSv1: No CertificateRequest needed");
448 wpa_printf(MSG_DEBUG, "TLSv1: Send CertificateRequest");
450 pos += TLS_RECORD_HEADER_LEN;
452 /* opaque fragment[TLSPlaintext.length] */
456 /* HandshakeType msg_type */
457 *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST;
458 /* uint24 length (to be filled) */
461 /* body - CertificateRequest */
465 * rsa_sign(1), dss_sign(2), rsa_fixed_dh(3), dss_fixed_dh(4),
467 * } ClientCertificateType;
468 * ClientCertificateType certificate_types<1..2^8-1>
471 *pos++ = 1; /* rsa_sign */
474 * opaque DistinguishedName<1..2^16-1>
475 * DistinguishedName certificate_authorities<3..2^16-1>
477 /* TODO: add support for listing DNs for trusted CAs */
478 WPA_PUT_BE16(pos, 0);
481 WPA_PUT_BE24(hs_length, pos - hs_length - 3);
483 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
484 rhdr, end - rhdr, hs_start, pos - hs_start,
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)
508 wpa_printf(MSG_DEBUG, "TLSv1: Send ServerHelloDone");
510 /* opaque fragment[TLSPlaintext.length] */
514 /* HandshakeType msg_type */
515 *pos++ = TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE;
517 WPA_PUT_BE24(pos, 0);
519 /* body - ServerHelloDone (empty) */
521 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
522 *msgpos, end - *msgpos, payload, pos - payload,
524 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
525 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
526 TLS_ALERT_INTERNAL_ERROR);
530 tls_verify_hash_add(&conn->verify, payload, pos - payload);
538 static int tls_write_server_change_cipher_spec(struct tlsv1_server *conn,
539 u8 **msgpos, u8 *end)
544 wpa_printf(MSG_DEBUG, "TLSv1: Send ChangeCipherSpec");
546 payload[0] = TLS_CHANGE_CIPHER_SPEC;
548 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC,
549 *msgpos, end - *msgpos, payload, sizeof(payload),
551 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
552 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
553 TLS_ALERT_INTERNAL_ERROR);
557 if (tlsv1_record_change_write_cipher(&conn->rl) < 0) {
558 wpa_printf(MSG_DEBUG, "TLSv1: Failed to set write cipher for "
560 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
561 TLS_ALERT_INTERNAL_ERROR);
571 static int tls_write_server_finished(struct tlsv1_server *conn,
572 u8 **msgpos, u8 *end)
576 u8 verify_data[1 + 3 + TLS_VERIFY_DATA_LEN];
577 u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
581 wpa_printf(MSG_DEBUG, "TLSv1: Send Finished");
583 /* Encrypted Handshake Message: Finished */
586 if (conn->rl.tls_version >= TLS_VERSION_1_2) {
587 hlen = SHA256_MAC_LEN;
588 if (conn->verify.sha256_server == NULL ||
589 crypto_hash_finish(conn->verify.sha256_server, hash, &hlen)
591 conn->verify.sha256_server = NULL;
592 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
593 TLS_ALERT_INTERNAL_ERROR);
596 conn->verify.sha256_server = NULL;
598 #endif /* CONFIG_TLSV12 */
601 if (conn->verify.md5_server == NULL ||
602 crypto_hash_finish(conn->verify.md5_server, hash, &hlen) < 0) {
603 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
604 TLS_ALERT_INTERNAL_ERROR);
605 conn->verify.md5_server = NULL;
606 crypto_hash_finish(conn->verify.sha1_server, NULL, NULL);
607 conn->verify.sha1_server = NULL;
610 conn->verify.md5_server = NULL;
612 if (conn->verify.sha1_server == NULL ||
613 crypto_hash_finish(conn->verify.sha1_server, hash + MD5_MAC_LEN,
615 conn->verify.sha1_server = NULL;
616 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
617 TLS_ALERT_INTERNAL_ERROR);
620 conn->verify.sha1_server = NULL;
621 hlen = MD5_MAC_LEN + SHA1_MAC_LEN;
625 #endif /* CONFIG_TLSV12 */
627 if (tls_prf(conn->rl.tls_version,
628 conn->master_secret, TLS_MASTER_SECRET_LEN,
629 "server finished", hash, hlen,
630 verify_data + 1 + 3, TLS_VERIFY_DATA_LEN)) {
631 wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate verify_data");
632 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
633 TLS_ALERT_INTERNAL_ERROR);
636 wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (server)",
637 verify_data + 1 + 3, TLS_VERIFY_DATA_LEN);
640 pos = hs_start = verify_data;
641 /* HandshakeType msg_type */
642 *pos++ = TLS_HANDSHAKE_TYPE_FINISHED;
644 WPA_PUT_BE24(pos, TLS_VERIFY_DATA_LEN);
646 pos += TLS_VERIFY_DATA_LEN;
647 tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
649 if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
650 *msgpos, end - *msgpos, hs_start, pos - hs_start,
652 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
653 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
654 TLS_ALERT_INTERNAL_ERROR);
664 static u8 * tls_send_server_hello(struct tlsv1_server *conn, size_t *out_len)
671 msglen = 1000 + tls_server_cert_chain_der_len(conn);
673 msg = os_malloc(msglen);
680 if (tls_write_server_hello(conn, &pos, end) < 0) {
685 if (conn->use_session_ticket) {
686 /* Abbreviated handshake using session ticket; RFC 4507 */
687 if (tls_write_server_change_cipher_spec(conn, &pos, end) < 0 ||
688 tls_write_server_finished(conn, &pos, end) < 0) {
693 *out_len = pos - msg;
695 conn->state = CHANGE_CIPHER_SPEC;
701 if (tls_write_server_certificate(conn, &pos, end) < 0 ||
702 tls_write_server_key_exchange(conn, &pos, end) < 0 ||
703 tls_write_server_certificate_request(conn, &pos, end) < 0 ||
704 tls_write_server_hello_done(conn, &pos, end) < 0) {
709 *out_len = pos - msg;
711 conn->state = CLIENT_CERTIFICATE;
717 static u8 * tls_send_change_cipher_spec(struct tlsv1_server *conn,
724 msg = os_malloc(1000);
731 if (tls_write_server_change_cipher_spec(conn, &pos, end) < 0 ||
732 tls_write_server_finished(conn, &pos, end) < 0) {
737 *out_len = pos - msg;
739 wpa_printf(MSG_DEBUG, "TLSv1: Handshake completed successfully");
740 conn->state = ESTABLISHED;
746 u8 * tlsv1_server_handshake_write(struct tlsv1_server *conn, size_t *out_len)
748 switch (conn->state) {
750 return tls_send_server_hello(conn, out_len);
751 case SERVER_CHANGE_CIPHER_SPEC:
752 return tls_send_change_cipher_spec(conn, out_len);
754 if (conn->state == ESTABLISHED && conn->use_session_ticket) {
755 /* Abbreviated handshake was already completed. */
758 wpa_printf(MSG_DEBUG, "TLSv1: Unexpected state %d while "
759 "generating reply", conn->state);
765 u8 * tlsv1_server_send_alert(struct tlsv1_server *conn, u8 level,
766 u8 description, size_t *out_len)
768 u8 *alert, *pos, *length;
770 wpa_printf(MSG_DEBUG, "TLSv1: Send Alert(%d:%d)", level, description);
773 alert = os_malloc(10);
780 /* ContentType type */
781 *pos++ = TLS_CONTENT_TYPE_ALERT;
782 /* ProtocolVersion version */
783 WPA_PUT_BE16(pos, conn->rl.tls_version ? conn->rl.tls_version :
786 /* uint16 length (to be filled) */
789 /* opaque fragment[TLSPlaintext.length] */
792 /* AlertLevel level */
794 /* AlertDescription description */
795 *pos++ = description;
797 WPA_PUT_BE16(length, pos - length - 2);
798 *out_len = pos - alert;