driver_test: Remove forgotten, unused prototypes
[hostap-gosc2009.git] / src / tls / tlsv1_server_read.c
blob49e811ffcff514252ac11d04054c6aeeb1b5755a
1 /*
2 * TLSv1 server - read 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_server.h"
25 #include "tlsv1_server_i.h"
28 static int tls_process_client_key_exchange(struct tlsv1_server *conn, u8 ct,
29 const u8 *in_data, size_t *in_len);
30 static int tls_process_change_cipher_spec(struct tlsv1_server *conn,
31 u8 ct, const u8 *in_data,
32 size_t *in_len);
35 static int tls_process_client_hello(struct tlsv1_server *conn, u8 ct,
36 const u8 *in_data, size_t *in_len)
38 const u8 *pos, *end, *c;
39 size_t left, len, i, j;
40 u16 cipher_suite;
41 u16 num_suites;
42 int compr_null_found;
43 u16 ext_type, ext_len;
45 if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
46 wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
47 "received content type 0x%x", ct);
48 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
49 TLS_ALERT_UNEXPECTED_MESSAGE);
50 return -1;
53 pos = in_data;
54 left = *in_len;
56 if (left < 4)
57 goto decode_error;
59 /* HandshakeType msg_type */
60 if (*pos != TLS_HANDSHAKE_TYPE_CLIENT_HELLO) {
61 wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
62 "message %d (expected ClientHello)", *pos);
63 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
64 TLS_ALERT_UNEXPECTED_MESSAGE);
65 return -1;
67 wpa_printf(MSG_DEBUG, "TLSv1: Received ClientHello");
68 pos++;
69 /* uint24 length */
70 len = WPA_GET_BE24(pos);
71 pos += 3;
72 left -= 4;
74 if (len > left)
75 goto decode_error;
77 /* body - ClientHello */
79 wpa_hexdump(MSG_MSGDUMP, "TLSv1: ClientHello", pos, len);
80 end = pos + len;
82 /* ProtocolVersion client_version */
83 if (end - pos < 2)
84 goto decode_error;
85 conn->client_version = WPA_GET_BE16(pos);
86 wpa_printf(MSG_DEBUG, "TLSv1: Client version %d.%d",
87 conn->client_version >> 8, conn->client_version & 0xff);
88 if (conn->client_version < TLS_VERSION) {
89 wpa_printf(MSG_DEBUG, "TLSv1: Unexpected protocol version in "
90 "ClientHello");
91 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
92 TLS_ALERT_PROTOCOL_VERSION);
93 return -1;
95 pos += 2;
97 /* Random random */
98 if (end - pos < TLS_RANDOM_LEN)
99 goto decode_error;
101 os_memcpy(conn->client_random, pos, TLS_RANDOM_LEN);
102 pos += TLS_RANDOM_LEN;
103 wpa_hexdump(MSG_MSGDUMP, "TLSv1: client_random",
104 conn->client_random, TLS_RANDOM_LEN);
106 /* SessionID session_id */
107 if (end - pos < 1)
108 goto decode_error;
109 if (end - pos < 1 + *pos || *pos > TLS_SESSION_ID_MAX_LEN)
110 goto decode_error;
111 wpa_hexdump(MSG_MSGDUMP, "TLSv1: client session_id", pos + 1, *pos);
112 pos += 1 + *pos;
113 /* TODO: add support for session resumption */
115 /* CipherSuite cipher_suites<2..2^16-1> */
116 if (end - pos < 2)
117 goto decode_error;
118 num_suites = WPA_GET_BE16(pos);
119 pos += 2;
120 if (end - pos < num_suites)
121 goto decode_error;
122 wpa_hexdump(MSG_MSGDUMP, "TLSv1: client cipher suites",
123 pos, num_suites);
124 if (num_suites & 1)
125 goto decode_error;
126 num_suites /= 2;
128 cipher_suite = 0;
129 for (i = 0; !cipher_suite && i < conn->num_cipher_suites; i++) {
130 c = pos;
131 for (j = 0; j < num_suites; j++) {
132 u16 tmp = WPA_GET_BE16(c);
133 c += 2;
134 if (!cipher_suite && tmp == conn->cipher_suites[i]) {
135 cipher_suite = tmp;
136 break;
140 pos += num_suites * 2;
141 if (!cipher_suite) {
142 wpa_printf(MSG_INFO, "TLSv1: No supported cipher suite "
143 "available");
144 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
145 TLS_ALERT_ILLEGAL_PARAMETER);
146 return -1;
149 if (tlsv1_record_set_cipher_suite(&conn->rl, cipher_suite) < 0) {
150 wpa_printf(MSG_DEBUG, "TLSv1: Failed to set CipherSuite for "
151 "record layer");
152 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
153 TLS_ALERT_INTERNAL_ERROR);
154 return -1;
157 conn->cipher_suite = cipher_suite;
159 /* CompressionMethod compression_methods<1..2^8-1> */
160 if (end - pos < 1)
161 goto decode_error;
162 num_suites = *pos++;
163 if (end - pos < num_suites)
164 goto decode_error;
165 wpa_hexdump(MSG_MSGDUMP, "TLSv1: client compression_methods",
166 pos, num_suites);
167 compr_null_found = 0;
168 for (i = 0; i < num_suites; i++) {
169 if (*pos++ == TLS_COMPRESSION_NULL)
170 compr_null_found = 1;
172 if (!compr_null_found) {
173 wpa_printf(MSG_INFO, "TLSv1: Client does not accept NULL "
174 "compression");
175 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
176 TLS_ALERT_ILLEGAL_PARAMETER);
177 return -1;
180 if (end - pos == 1) {
181 wpa_printf(MSG_DEBUG, "TLSv1: Unexpected extra octet in the "
182 "end of ClientHello: 0x%02x", *pos);
183 goto decode_error;
186 if (end - pos >= 2) {
187 /* Extension client_hello_extension_list<0..2^16-1> */
188 ext_len = WPA_GET_BE16(pos);
189 pos += 2;
191 wpa_printf(MSG_DEBUG, "TLSv1: %u bytes of ClientHello "
192 "extensions", ext_len);
193 if (end - pos != ext_len) {
194 wpa_printf(MSG_DEBUG, "TLSv1: Invalid ClientHello "
195 "extension list length %u (expected %u)",
196 ext_len, (unsigned int) (end - pos));
197 goto decode_error;
201 * struct {
202 * ExtensionType extension_type (0..65535)
203 * opaque extension_data<0..2^16-1>
204 * } Extension;
207 while (pos < end) {
208 if (end - pos < 2) {
209 wpa_printf(MSG_DEBUG, "TLSv1: Invalid "
210 "extension_type field");
211 goto decode_error;
214 ext_type = WPA_GET_BE16(pos);
215 pos += 2;
217 if (end - pos < 2) {
218 wpa_printf(MSG_DEBUG, "TLSv1: Invalid "
219 "extension_data length field");
220 goto decode_error;
223 ext_len = WPA_GET_BE16(pos);
224 pos += 2;
226 if (end - pos < ext_len) {
227 wpa_printf(MSG_DEBUG, "TLSv1: Invalid "
228 "extension_data field");
229 goto decode_error;
232 wpa_printf(MSG_DEBUG, "TLSv1: ClientHello Extension "
233 "type %u", ext_type);
234 wpa_hexdump(MSG_MSGDUMP, "TLSv1: ClientHello "
235 "Extension data", pos, ext_len);
237 if (ext_type == TLS_EXT_SESSION_TICKET) {
238 os_free(conn->session_ticket);
239 conn->session_ticket = os_malloc(ext_len);
240 if (conn->session_ticket) {
241 os_memcpy(conn->session_ticket, pos,
242 ext_len);
243 conn->session_ticket_len = ext_len;
247 pos += ext_len;
251 *in_len = end - in_data;
253 wpa_printf(MSG_DEBUG, "TLSv1: ClientHello OK - proceed to "
254 "ServerHello");
255 conn->state = SERVER_HELLO;
257 return 0;
259 decode_error:
260 wpa_printf(MSG_DEBUG, "TLSv1: Failed to decode ClientHello");
261 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
262 TLS_ALERT_DECODE_ERROR);
263 return -1;
267 static int tls_process_certificate(struct tlsv1_server *conn, u8 ct,
268 const u8 *in_data, size_t *in_len)
270 const u8 *pos, *end;
271 size_t left, len, list_len, cert_len, idx;
272 u8 type;
273 struct x509_certificate *chain = NULL, *last = NULL, *cert;
274 int reason;
276 if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
277 wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
278 "received content type 0x%x", ct);
279 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
280 TLS_ALERT_UNEXPECTED_MESSAGE);
281 return -1;
284 pos = in_data;
285 left = *in_len;
287 if (left < 4) {
288 wpa_printf(MSG_DEBUG, "TLSv1: Too short Certificate message "
289 "(len=%lu)", (unsigned long) left);
290 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
291 TLS_ALERT_DECODE_ERROR);
292 return -1;
295 type = *pos++;
296 len = WPA_GET_BE24(pos);
297 pos += 3;
298 left -= 4;
300 if (len > left) {
301 wpa_printf(MSG_DEBUG, "TLSv1: Unexpected Certificate message "
302 "length (len=%lu != left=%lu)",
303 (unsigned long) len, (unsigned long) left);
304 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
305 TLS_ALERT_DECODE_ERROR);
306 return -1;
309 if (type == TLS_HANDSHAKE_TYPE_CLIENT_KEY_EXCHANGE) {
310 if (conn->verify_peer) {
311 wpa_printf(MSG_DEBUG, "TLSv1: Client did not include "
312 "Certificate");
313 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
314 TLS_ALERT_UNEXPECTED_MESSAGE);
315 return -1;
318 return tls_process_client_key_exchange(conn, ct, in_data,
319 in_len);
321 if (type != TLS_HANDSHAKE_TYPE_CERTIFICATE) {
322 wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
323 "message %d (expected Certificate/"
324 "ClientKeyExchange)", type);
325 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
326 TLS_ALERT_UNEXPECTED_MESSAGE);
327 return -1;
330 wpa_printf(MSG_DEBUG,
331 "TLSv1: Received Certificate (certificate_list len %lu)",
332 (unsigned long) len);
335 * opaque ASN.1Cert<2^24-1>;
337 * struct {
338 * ASN.1Cert certificate_list<1..2^24-1>;
339 * } Certificate;
342 end = pos + len;
344 if (end - pos < 3) {
345 wpa_printf(MSG_DEBUG, "TLSv1: Too short Certificate "
346 "(left=%lu)", (unsigned long) left);
347 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
348 TLS_ALERT_DECODE_ERROR);
349 return -1;
352 list_len = WPA_GET_BE24(pos);
353 pos += 3;
355 if ((size_t) (end - pos) != list_len) {
356 wpa_printf(MSG_DEBUG, "TLSv1: Unexpected certificate_list "
357 "length (len=%lu left=%lu)",
358 (unsigned long) list_len,
359 (unsigned long) (end - pos));
360 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
361 TLS_ALERT_DECODE_ERROR);
362 return -1;
365 idx = 0;
366 while (pos < end) {
367 if (end - pos < 3) {
368 wpa_printf(MSG_DEBUG, "TLSv1: Failed to parse "
369 "certificate_list");
370 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
371 TLS_ALERT_DECODE_ERROR);
372 x509_certificate_chain_free(chain);
373 return -1;
376 cert_len = WPA_GET_BE24(pos);
377 pos += 3;
379 if ((size_t) (end - pos) < cert_len) {
380 wpa_printf(MSG_DEBUG, "TLSv1: Unexpected certificate "
381 "length (len=%lu left=%lu)",
382 (unsigned long) cert_len,
383 (unsigned long) (end - pos));
384 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
385 TLS_ALERT_DECODE_ERROR);
386 x509_certificate_chain_free(chain);
387 return -1;
390 wpa_printf(MSG_DEBUG, "TLSv1: Certificate %lu (len %lu)",
391 (unsigned long) idx, (unsigned long) cert_len);
393 if (idx == 0) {
394 crypto_public_key_free(conn->client_rsa_key);
395 if (tls_parse_cert(pos, cert_len,
396 &conn->client_rsa_key)) {
397 wpa_printf(MSG_DEBUG, "TLSv1: Failed to parse "
398 "the certificate");
399 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
400 TLS_ALERT_BAD_CERTIFICATE);
401 x509_certificate_chain_free(chain);
402 return -1;
406 cert = x509_certificate_parse(pos, cert_len);
407 if (cert == NULL) {
408 wpa_printf(MSG_DEBUG, "TLSv1: Failed to parse "
409 "the certificate");
410 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
411 TLS_ALERT_BAD_CERTIFICATE);
412 x509_certificate_chain_free(chain);
413 return -1;
416 if (last == NULL)
417 chain = cert;
418 else
419 last->next = cert;
420 last = cert;
422 idx++;
423 pos += cert_len;
426 if (x509_certificate_chain_validate(conn->cred->trusted_certs, chain,
427 &reason) < 0) {
428 int tls_reason;
429 wpa_printf(MSG_DEBUG, "TLSv1: Server certificate chain "
430 "validation failed (reason=%d)", reason);
431 switch (reason) {
432 case X509_VALIDATE_BAD_CERTIFICATE:
433 tls_reason = TLS_ALERT_BAD_CERTIFICATE;
434 break;
435 case X509_VALIDATE_UNSUPPORTED_CERTIFICATE:
436 tls_reason = TLS_ALERT_UNSUPPORTED_CERTIFICATE;
437 break;
438 case X509_VALIDATE_CERTIFICATE_REVOKED:
439 tls_reason = TLS_ALERT_CERTIFICATE_REVOKED;
440 break;
441 case X509_VALIDATE_CERTIFICATE_EXPIRED:
442 tls_reason = TLS_ALERT_CERTIFICATE_EXPIRED;
443 break;
444 case X509_VALIDATE_CERTIFICATE_UNKNOWN:
445 tls_reason = TLS_ALERT_CERTIFICATE_UNKNOWN;
446 break;
447 case X509_VALIDATE_UNKNOWN_CA:
448 tls_reason = TLS_ALERT_UNKNOWN_CA;
449 break;
450 default:
451 tls_reason = TLS_ALERT_BAD_CERTIFICATE;
452 break;
454 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL, tls_reason);
455 x509_certificate_chain_free(chain);
456 return -1;
459 x509_certificate_chain_free(chain);
461 *in_len = end - in_data;
463 conn->state = CLIENT_KEY_EXCHANGE;
465 return 0;
469 static int tls_process_client_key_exchange_rsa(
470 struct tlsv1_server *conn, const u8 *pos, const u8 *end)
472 u8 *out;
473 size_t outlen, outbuflen;
474 u16 encr_len;
475 int res;
476 int use_random = 0;
478 if (end - pos < 2) {
479 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
480 TLS_ALERT_DECODE_ERROR);
481 return -1;
484 encr_len = WPA_GET_BE16(pos);
485 pos += 2;
487 outbuflen = outlen = end - pos;
488 out = os_malloc(outlen >= TLS_PRE_MASTER_SECRET_LEN ?
489 outlen : TLS_PRE_MASTER_SECRET_LEN);
490 if (out == NULL) {
491 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
492 TLS_ALERT_INTERNAL_ERROR);
493 return -1;
497 * struct {
498 * ProtocolVersion client_version;
499 * opaque random[46];
500 * } PreMasterSecret;
502 * struct {
503 * public-key-encrypted PreMasterSecret pre_master_secret;
504 * } EncryptedPreMasterSecret;
508 * Note: To avoid Bleichenbacher attack, we do not report decryption or
509 * parsing errors from EncryptedPreMasterSecret processing to the
510 * client. Instead, a random pre-master secret is used to force the
511 * handshake to fail.
514 if (crypto_private_key_decrypt_pkcs1_v15(conn->cred->key,
515 pos, end - pos,
516 out, &outlen) < 0) {
517 wpa_printf(MSG_DEBUG, "TLSv1: Failed to decrypt "
518 "PreMasterSecret (encr_len=%d outlen=%lu)",
519 (int) (end - pos), (unsigned long) outlen);
520 use_random = 1;
523 if (outlen != TLS_PRE_MASTER_SECRET_LEN) {
524 wpa_printf(MSG_DEBUG, "TLSv1: Unexpected PreMasterSecret "
525 "length %lu", (unsigned long) outlen);
526 use_random = 1;
529 if (WPA_GET_BE16(out) != conn->client_version) {
530 wpa_printf(MSG_DEBUG, "TLSv1: Client version in "
531 "ClientKeyExchange does not match with version in "
532 "ClientHello");
533 use_random = 1;
536 if (use_random) {
537 wpa_printf(MSG_DEBUG, "TLSv1: Using random premaster secret "
538 "to avoid revealing information about private key");
539 outlen = TLS_PRE_MASTER_SECRET_LEN;
540 if (os_get_random(out, outlen)) {
541 wpa_printf(MSG_DEBUG, "TLSv1: Failed to get random "
542 "data");
543 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
544 TLS_ALERT_INTERNAL_ERROR);
545 os_free(out);
546 return -1;
550 res = tlsv1_server_derive_keys(conn, out, outlen);
552 /* Clear the pre-master secret since it is not needed anymore */
553 os_memset(out, 0, outbuflen);
554 os_free(out);
556 if (res) {
557 wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
558 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
559 TLS_ALERT_INTERNAL_ERROR);
560 return -1;
563 return 0;
567 static int tls_process_client_key_exchange_dh_anon(
568 struct tlsv1_server *conn, const u8 *pos, const u8 *end)
570 const u8 *dh_yc;
571 u16 dh_yc_len;
572 u8 *shared;
573 size_t shared_len;
574 int res;
577 * struct {
578 * select (PublicValueEncoding) {
579 * case implicit: struct { };
580 * case explicit: opaque dh_Yc<1..2^16-1>;
581 * } dh_public;
582 * } ClientDiffieHellmanPublic;
585 wpa_hexdump(MSG_MSGDUMP, "TLSv1: ClientDiffieHellmanPublic",
586 pos, end - pos);
588 if (end == pos) {
589 wpa_printf(MSG_DEBUG, "TLSv1: Implicit public value encoding "
590 "not supported");
591 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
592 TLS_ALERT_INTERNAL_ERROR);
593 return -1;
596 if (end - pos < 3) {
597 wpa_printf(MSG_DEBUG, "TLSv1: Invalid client public value "
598 "length");
599 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
600 TLS_ALERT_DECODE_ERROR);
601 return -1;
604 dh_yc_len = WPA_GET_BE16(pos);
605 dh_yc = pos + 2;
607 if (dh_yc + dh_yc_len > end) {
608 wpa_printf(MSG_DEBUG, "TLSv1: Client public value overflow "
609 "(length %d)", dh_yc_len);
610 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
611 TLS_ALERT_DECODE_ERROR);
612 return -1;
615 wpa_hexdump(MSG_DEBUG, "TLSv1: DH Yc (client's public value)",
616 dh_yc, dh_yc_len);
618 if (conn->cred == NULL || conn->cred->dh_p == NULL ||
619 conn->dh_secret == NULL) {
620 wpa_printf(MSG_DEBUG, "TLSv1: No DH parameters available");
621 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
622 TLS_ALERT_INTERNAL_ERROR);
623 return -1;
626 shared_len = conn->cred->dh_p_len;
627 shared = os_malloc(shared_len);
628 if (shared == NULL) {
629 wpa_printf(MSG_DEBUG, "TLSv1: Could not allocate memory for "
630 "DH");
631 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
632 TLS_ALERT_INTERNAL_ERROR);
633 return -1;
636 /* shared = Yc^secret mod p */
637 if (crypto_mod_exp(dh_yc, dh_yc_len, conn->dh_secret,
638 conn->dh_secret_len,
639 conn->cred->dh_p, conn->cred->dh_p_len,
640 shared, &shared_len)) {
641 os_free(shared);
642 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
643 TLS_ALERT_INTERNAL_ERROR);
644 return -1;
646 wpa_hexdump_key(MSG_DEBUG, "TLSv1: Shared secret from DH key exchange",
647 shared, shared_len);
649 os_memset(conn->dh_secret, 0, conn->dh_secret_len);
650 os_free(conn->dh_secret);
651 conn->dh_secret = NULL;
653 res = tlsv1_server_derive_keys(conn, shared, shared_len);
655 /* Clear the pre-master secret since it is not needed anymore */
656 os_memset(shared, 0, shared_len);
657 os_free(shared);
659 if (res) {
660 wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
661 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
662 TLS_ALERT_INTERNAL_ERROR);
663 return -1;
666 return 0;
670 static int tls_process_client_key_exchange(struct tlsv1_server *conn, u8 ct,
671 const u8 *in_data, size_t *in_len)
673 const u8 *pos, *end;
674 size_t left, len;
675 u8 type;
676 tls_key_exchange keyx;
677 const struct tls_cipher_suite *suite;
679 if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
680 wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
681 "received content type 0x%x", ct);
682 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
683 TLS_ALERT_UNEXPECTED_MESSAGE);
684 return -1;
687 pos = in_data;
688 left = *in_len;
690 if (left < 4) {
691 wpa_printf(MSG_DEBUG, "TLSv1: Too short ClientKeyExchange "
692 "(Left=%lu)", (unsigned long) left);
693 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
694 TLS_ALERT_DECODE_ERROR);
695 return -1;
698 type = *pos++;
699 len = WPA_GET_BE24(pos);
700 pos += 3;
701 left -= 4;
703 if (len > left) {
704 wpa_printf(MSG_DEBUG, "TLSv1: Mismatch in ClientKeyExchange "
705 "length (len=%lu != left=%lu)",
706 (unsigned long) len, (unsigned long) left);
707 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
708 TLS_ALERT_DECODE_ERROR);
709 return -1;
712 end = pos + len;
714 if (type != TLS_HANDSHAKE_TYPE_CLIENT_KEY_EXCHANGE) {
715 wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
716 "message %d (expected ClientKeyExchange)", type);
717 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
718 TLS_ALERT_UNEXPECTED_MESSAGE);
719 return -1;
722 wpa_printf(MSG_DEBUG, "TLSv1: Received ClientKeyExchange");
724 wpa_hexdump(MSG_DEBUG, "TLSv1: ClientKeyExchange", pos, len);
726 suite = tls_get_cipher_suite(conn->rl.cipher_suite);
727 if (suite == NULL)
728 keyx = TLS_KEY_X_NULL;
729 else
730 keyx = suite->key_exchange;
732 if (keyx == TLS_KEY_X_DH_anon &&
733 tls_process_client_key_exchange_dh_anon(conn, pos, end) < 0)
734 return -1;
736 if (keyx != TLS_KEY_X_DH_anon &&
737 tls_process_client_key_exchange_rsa(conn, pos, end) < 0)
738 return -1;
740 *in_len = end - in_data;
742 conn->state = CERTIFICATE_VERIFY;
744 return 0;
748 static int tls_process_certificate_verify(struct tlsv1_server *conn, u8 ct,
749 const u8 *in_data, size_t *in_len)
751 const u8 *pos, *end;
752 size_t left, len;
753 u8 type;
754 size_t hlen, buflen;
755 u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN], *hpos, *buf;
756 enum { SIGN_ALG_RSA, SIGN_ALG_DSA } alg = SIGN_ALG_RSA;
757 u16 slen;
759 if (ct == TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC) {
760 if (conn->verify_peer) {
761 wpa_printf(MSG_DEBUG, "TLSv1: Client did not include "
762 "CertificateVerify");
763 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
764 TLS_ALERT_UNEXPECTED_MESSAGE);
765 return -1;
768 return tls_process_change_cipher_spec(conn, ct, in_data,
769 in_len);
772 if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
773 wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
774 "received content type 0x%x", ct);
775 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
776 TLS_ALERT_UNEXPECTED_MESSAGE);
777 return -1;
780 pos = in_data;
781 left = *in_len;
783 if (left < 4) {
784 wpa_printf(MSG_DEBUG, "TLSv1: Too short CertificateVerify "
785 "message (len=%lu)", (unsigned long) left);
786 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
787 TLS_ALERT_DECODE_ERROR);
788 return -1;
791 type = *pos++;
792 len = WPA_GET_BE24(pos);
793 pos += 3;
794 left -= 4;
796 if (len > left) {
797 wpa_printf(MSG_DEBUG, "TLSv1: Unexpected CertificateVerify "
798 "message length (len=%lu != left=%lu)",
799 (unsigned long) len, (unsigned long) left);
800 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
801 TLS_ALERT_DECODE_ERROR);
802 return -1;
805 end = pos + len;
807 if (type != TLS_HANDSHAKE_TYPE_CERTIFICATE_VERIFY) {
808 wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
809 "message %d (expected CertificateVerify)", type);
810 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
811 TLS_ALERT_UNEXPECTED_MESSAGE);
812 return -1;
815 wpa_printf(MSG_DEBUG, "TLSv1: Received CertificateVerify");
818 * struct {
819 * Signature signature;
820 * } CertificateVerify;
823 hpos = hash;
825 if (alg == SIGN_ALG_RSA) {
826 hlen = MD5_MAC_LEN;
827 if (conn->verify.md5_cert == NULL ||
828 crypto_hash_finish(conn->verify.md5_cert, hpos, &hlen) < 0)
830 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
831 TLS_ALERT_INTERNAL_ERROR);
832 conn->verify.md5_cert = NULL;
833 crypto_hash_finish(conn->verify.sha1_cert, NULL, NULL);
834 conn->verify.sha1_cert = NULL;
835 return -1;
837 hpos += MD5_MAC_LEN;
838 } else
839 crypto_hash_finish(conn->verify.md5_cert, NULL, NULL);
841 conn->verify.md5_cert = NULL;
842 hlen = SHA1_MAC_LEN;
843 if (conn->verify.sha1_cert == NULL ||
844 crypto_hash_finish(conn->verify.sha1_cert, hpos, &hlen) < 0) {
845 conn->verify.sha1_cert = NULL;
846 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
847 TLS_ALERT_INTERNAL_ERROR);
848 return -1;
850 conn->verify.sha1_cert = NULL;
852 if (alg == SIGN_ALG_RSA)
853 hlen += MD5_MAC_LEN;
855 wpa_hexdump(MSG_MSGDUMP, "TLSv1: CertificateVerify hash", hash, hlen);
857 if (end - pos < 2) {
858 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
859 TLS_ALERT_DECODE_ERROR);
860 return -1;
862 slen = WPA_GET_BE16(pos);
863 pos += 2;
864 if (end - pos < slen) {
865 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
866 TLS_ALERT_DECODE_ERROR);
867 return -1;
870 wpa_hexdump(MSG_MSGDUMP, "TLSv1: Signature", pos, end - pos);
871 if (conn->client_rsa_key == NULL) {
872 wpa_printf(MSG_DEBUG, "TLSv1: No client public key to verify "
873 "signature");
874 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
875 TLS_ALERT_INTERNAL_ERROR);
876 return -1;
879 buflen = end - pos;
880 buf = os_malloc(end - pos);
881 if (crypto_public_key_decrypt_pkcs1(conn->client_rsa_key,
882 pos, end - pos, buf, &buflen) < 0)
884 wpa_printf(MSG_DEBUG, "TLSv1: Failed to decrypt signature");
885 os_free(buf);
886 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
887 TLS_ALERT_DECRYPT_ERROR);
888 return -1;
891 wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: Decrypted Signature",
892 buf, buflen);
894 if (buflen != hlen || os_memcmp(buf, hash, buflen) != 0) {
895 wpa_printf(MSG_DEBUG, "TLSv1: Invalid Signature in "
896 "CertificateVerify - did not match with calculated "
897 "hash");
898 os_free(buf);
899 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
900 TLS_ALERT_DECRYPT_ERROR);
901 return -1;
904 os_free(buf);
906 *in_len = end - in_data;
908 conn->state = CHANGE_CIPHER_SPEC;
910 return 0;
914 static int tls_process_change_cipher_spec(struct tlsv1_server *conn,
915 u8 ct, const u8 *in_data,
916 size_t *in_len)
918 const u8 *pos;
919 size_t left;
921 if (ct != TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC) {
922 wpa_printf(MSG_DEBUG, "TLSv1: Expected ChangeCipherSpec; "
923 "received content type 0x%x", ct);
924 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
925 TLS_ALERT_UNEXPECTED_MESSAGE);
926 return -1;
929 pos = in_data;
930 left = *in_len;
932 if (left < 1) {
933 wpa_printf(MSG_DEBUG, "TLSv1: Too short ChangeCipherSpec");
934 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
935 TLS_ALERT_DECODE_ERROR);
936 return -1;
939 if (*pos != TLS_CHANGE_CIPHER_SPEC) {
940 wpa_printf(MSG_DEBUG, "TLSv1: Expected ChangeCipherSpec; "
941 "received data 0x%x", *pos);
942 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
943 TLS_ALERT_UNEXPECTED_MESSAGE);
944 return -1;
947 wpa_printf(MSG_DEBUG, "TLSv1: Received ChangeCipherSpec");
948 if (tlsv1_record_change_read_cipher(&conn->rl) < 0) {
949 wpa_printf(MSG_DEBUG, "TLSv1: Failed to change read cipher "
950 "for record layer");
951 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
952 TLS_ALERT_INTERNAL_ERROR);
953 return -1;
956 *in_len = pos + 1 - in_data;
958 conn->state = CLIENT_FINISHED;
960 return 0;
964 static int tls_process_client_finished(struct tlsv1_server *conn, u8 ct,
965 const u8 *in_data, size_t *in_len)
967 const u8 *pos, *end;
968 size_t left, len, hlen;
969 u8 verify_data[TLS_VERIFY_DATA_LEN];
970 u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
972 if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
973 wpa_printf(MSG_DEBUG, "TLSv1: Expected Finished; "
974 "received content type 0x%x", ct);
975 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
976 TLS_ALERT_UNEXPECTED_MESSAGE);
977 return -1;
980 pos = in_data;
981 left = *in_len;
983 if (left < 4) {
984 wpa_printf(MSG_DEBUG, "TLSv1: Too short record (left=%lu) for "
985 "Finished",
986 (unsigned long) left);
987 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
988 TLS_ALERT_DECODE_ERROR);
989 return -1;
992 if (pos[0] != TLS_HANDSHAKE_TYPE_FINISHED) {
993 wpa_printf(MSG_DEBUG, "TLSv1: Expected Finished; received "
994 "type 0x%x", pos[0]);
995 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
996 TLS_ALERT_UNEXPECTED_MESSAGE);
997 return -1;
1000 len = WPA_GET_BE24(pos + 1);
1002 pos += 4;
1003 left -= 4;
1005 if (len > left) {
1006 wpa_printf(MSG_DEBUG, "TLSv1: Too short buffer for Finished "
1007 "(len=%lu > left=%lu)",
1008 (unsigned long) len, (unsigned long) left);
1009 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1010 TLS_ALERT_DECODE_ERROR);
1011 return -1;
1013 end = pos + len;
1014 if (len != TLS_VERIFY_DATA_LEN) {
1015 wpa_printf(MSG_DEBUG, "TLSv1: Unexpected verify_data length "
1016 "in Finished: %lu (expected %d)",
1017 (unsigned long) len, TLS_VERIFY_DATA_LEN);
1018 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1019 TLS_ALERT_DECODE_ERROR);
1020 return -1;
1022 wpa_hexdump(MSG_MSGDUMP, "TLSv1: verify_data in Finished",
1023 pos, TLS_VERIFY_DATA_LEN);
1025 hlen = MD5_MAC_LEN;
1026 if (conn->verify.md5_client == NULL ||
1027 crypto_hash_finish(conn->verify.md5_client, hash, &hlen) < 0) {
1028 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1029 TLS_ALERT_INTERNAL_ERROR);
1030 conn->verify.md5_client = NULL;
1031 crypto_hash_finish(conn->verify.sha1_client, NULL, NULL);
1032 conn->verify.sha1_client = NULL;
1033 return -1;
1035 conn->verify.md5_client = NULL;
1036 hlen = SHA1_MAC_LEN;
1037 if (conn->verify.sha1_client == NULL ||
1038 crypto_hash_finish(conn->verify.sha1_client, hash + MD5_MAC_LEN,
1039 &hlen) < 0) {
1040 conn->verify.sha1_client = NULL;
1041 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1042 TLS_ALERT_INTERNAL_ERROR);
1043 return -1;
1045 conn->verify.sha1_client = NULL;
1047 if (tls_prf(conn->master_secret, TLS_MASTER_SECRET_LEN,
1048 "client finished", hash, MD5_MAC_LEN + SHA1_MAC_LEN,
1049 verify_data, TLS_VERIFY_DATA_LEN)) {
1050 wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive verify_data");
1051 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1052 TLS_ALERT_DECRYPT_ERROR);
1053 return -1;
1055 wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (client)",
1056 verify_data, TLS_VERIFY_DATA_LEN);
1058 if (os_memcmp(pos, verify_data, TLS_VERIFY_DATA_LEN) != 0) {
1059 wpa_printf(MSG_INFO, "TLSv1: Mismatch in verify_data");
1060 return -1;
1063 wpa_printf(MSG_DEBUG, "TLSv1: Received Finished");
1065 *in_len = end - in_data;
1067 if (conn->use_session_ticket) {
1068 /* Abbreviated handshake using session ticket; RFC 4507 */
1069 wpa_printf(MSG_DEBUG, "TLSv1: Abbreviated handshake completed "
1070 "successfully");
1071 conn->state = ESTABLISHED;
1072 } else {
1073 /* Full handshake */
1074 conn->state = SERVER_CHANGE_CIPHER_SPEC;
1077 return 0;
1081 int tlsv1_server_process_handshake(struct tlsv1_server *conn, u8 ct,
1082 const u8 *buf, size_t *len)
1084 if (ct == TLS_CONTENT_TYPE_ALERT) {
1085 if (*len < 2) {
1086 wpa_printf(MSG_DEBUG, "TLSv1: Alert underflow");
1087 tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1088 TLS_ALERT_DECODE_ERROR);
1089 return -1;
1091 wpa_printf(MSG_DEBUG, "TLSv1: Received alert %d:%d",
1092 buf[0], buf[1]);
1093 *len = 2;
1094 conn->state = FAILED;
1095 return -1;
1098 switch (conn->state) {
1099 case CLIENT_HELLO:
1100 if (tls_process_client_hello(conn, ct, buf, len))
1101 return -1;
1102 break;
1103 case CLIENT_CERTIFICATE:
1104 if (tls_process_certificate(conn, ct, buf, len))
1105 return -1;
1106 break;
1107 case CLIENT_KEY_EXCHANGE:
1108 if (tls_process_client_key_exchange(conn, ct, buf, len))
1109 return -1;
1110 break;
1111 case CERTIFICATE_VERIFY:
1112 if (tls_process_certificate_verify(conn, ct, buf, len))
1113 return -1;
1114 break;
1115 case CHANGE_CIPHER_SPEC:
1116 if (tls_process_change_cipher_spec(conn, ct, buf, len))
1117 return -1;
1118 break;
1119 case CLIENT_FINISHED:
1120 if (tls_process_client_finished(conn, ct, buf, len))
1121 return -1;
1122 break;
1123 default:
1124 wpa_printf(MSG_DEBUG, "TLSv1: Unexpected state %d "
1125 "while processing received message",
1126 conn->state);
1127 return -1;
1130 if (ct == TLS_CONTENT_TYPE_HANDSHAKE)
1131 tls_verify_hash_add(&conn->verify, buf, *len);
1133 return 0;