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.
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;
251
u8 *pos, *rhdr, *hs_start, *hs_length;
255
#endif /* EAP_FAST */
257
suite = tls_get_cipher_suite(conn->rl.cipher_suite);
259
keyx = TLS_KEY_X_NULL;
261
keyx = suite->key_exchange;
263
if (!tls_server_key_exchange_allowed(conn->rl.cipher_suite)) {
264
wpa_printf(MSG_DEBUG, "TLSv1: No ServerKeyExchange needed");
268
if (keyx != TLS_KEY_X_DH_anon) {
270
wpa_printf(MSG_DEBUG, "TLSv1: ServerKeyExchange not yet "
271
"supported with key exchange type %d", keyx);
276
if (conn->cred == NULL || conn->cred->dh_p == NULL ||
277
conn->cred->dh_g == NULL) {
278
wpa_printf(MSG_DEBUG, "TLSv1: No DH parameters available for "
279
"ServerKeyExhcange");
283
os_free(conn->dh_secret);
284
conn->dh_secret_len = conn->cred->dh_p_len;
285
conn->dh_secret = os_malloc(conn->dh_secret_len);
286
if (conn->dh_secret == NULL) {
287
wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
288
"memory for secret (Diffie-Hellman)");
289
tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
290
TLS_ALERT_INTERNAL_ERROR);
293
if (os_get_random(conn->dh_secret, conn->dh_secret_len)) {
294
wpa_printf(MSG_DEBUG, "TLSv1: Failed to get random "
295
"data for Diffie-Hellman");
296
tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
297
TLS_ALERT_INTERNAL_ERROR);
298
os_free(conn->dh_secret);
299
conn->dh_secret = NULL;
303
if (os_memcmp(conn->dh_secret, conn->cred->dh_p, conn->dh_secret_len) >
305
conn->dh_secret[0] = 0; /* make sure secret < p */
307
pos = conn->dh_secret;
308
while (pos + 1 < conn->dh_secret + conn->dh_secret_len && *pos == 0)
310
if (pos != conn->dh_secret) {
311
os_memmove(conn->dh_secret, pos,
312
conn->dh_secret_len - (pos - conn->dh_secret));
313
conn->dh_secret_len -= pos - conn->dh_secret;
315
wpa_hexdump_key(MSG_DEBUG, "TLSv1: DH server's secret value",
316
conn->dh_secret, conn->dh_secret_len);
318
/* Ys = g^secret mod p */
319
dh_ys_len = conn->cred->dh_p_len;
320
dh_ys = os_malloc(dh_ys_len);
322
wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate memory for "
324
tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
325
TLS_ALERT_INTERNAL_ERROR);
328
if (crypto_mod_exp(conn->cred->dh_g, conn->cred->dh_g_len,
329
conn->dh_secret, conn->dh_secret_len,
330
conn->cred->dh_p, conn->cred->dh_p_len,
331
dh_ys, &dh_ys_len)) {
332
tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
333
TLS_ALERT_INTERNAL_ERROR);
338
wpa_hexdump(MSG_DEBUG, "TLSv1: DH Ys (server's public value)",
343
* select (KeyExchangeAlgorithm) {
344
* case diffie_hellman:
345
* ServerDHParams params;
346
* Signature signed_params;
348
* ServerRSAParams params;
349
* Signature signed_params;
351
* } ServerKeyExchange;
354
* opaque dh_p<1..2^16-1>;
355
* opaque dh_g<1..2^16-1>;
356
* opaque dh_Ys<1..2^16-1>;
362
wpa_printf(MSG_DEBUG, "TLSv1: Send ServerKeyExchange");
364
pos += TLS_RECORD_HEADER_LEN;
366
/* opaque fragment[TLSPlaintext.length] */
370
/* HandshakeType msg_type */
371
*pos++ = TLS_HANDSHAKE_TYPE_SERVER_KEY_EXCHANGE;
372
/* uint24 length (to be filled) */
376
/* body - ServerDHParams */
378
if (pos + 2 + conn->cred->dh_p_len > end) {
379
wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space for "
381
tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
382
TLS_ALERT_INTERNAL_ERROR);
386
WPA_PUT_BE16(pos, conn->cred->dh_p_len);
388
os_memcpy(pos, conn->cred->dh_p, conn->cred->dh_p_len);
389
pos += conn->cred->dh_p_len;
392
if (pos + 2 + conn->cred->dh_g_len > end) {
393
wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space for "
395
tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
396
TLS_ALERT_INTERNAL_ERROR);
400
WPA_PUT_BE16(pos, conn->cred->dh_g_len);
402
os_memcpy(pos, conn->cred->dh_g, conn->cred->dh_g_len);
403
pos += conn->cred->dh_g_len;
406
if (pos + 2 + dh_ys_len > end) {
407
wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space for "
409
tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
410
TLS_ALERT_INTERNAL_ERROR);
414
WPA_PUT_BE16(pos, dh_ys_len);
416
os_memcpy(pos, dh_ys, dh_ys_len);
420
WPA_PUT_BE24(hs_length, pos - hs_length - 3);
422
if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
423
rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
424
wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
425
tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
426
TLS_ALERT_INTERNAL_ERROR);
431
tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
438
#endif /* EAP_FAST */
442
static int tls_write_server_certificate_request(struct tlsv1_server *conn,
443
u8 **msgpos, u8 *end)
445
u8 *pos, *rhdr, *hs_start, *hs_length;
448
if (!conn->verify_peer) {
449
wpa_printf(MSG_DEBUG, "TLSv1: No CertificateRequest needed");
455
wpa_printf(MSG_DEBUG, "TLSv1: Send CertificateRequest");
457
pos += TLS_RECORD_HEADER_LEN;
459
/* opaque fragment[TLSPlaintext.length] */
463
/* HandshakeType msg_type */
464
*pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST;
465
/* uint24 length (to be filled) */
468
/* body - CertificateRequest */
472
* rsa_sign(1), dss_sign(2), rsa_fixed_dh(3), dss_fixed_dh(4),
474
* } ClientCertificateType;
475
* ClientCertificateType certificate_types<1..2^8-1>
478
*pos++ = 1; /* rsa_sign */
481
* opaque DistinguishedName<1..2^16-1>
482
* DistinguishedName certificate_authorities<3..2^16-1>
484
/* TODO: add support for listing DNs for trusted CAs */
485
WPA_PUT_BE16(pos, 0);
488
WPA_PUT_BE24(hs_length, pos - hs_length - 3);
490
if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
491
rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
492
wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
493
tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
494
TLS_ALERT_INTERNAL_ERROR);
499
tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
507
static int tls_write_server_hello_done(struct tlsv1_server *conn,
508
u8 **msgpos, u8 *end)
510
u8 *pos, *rhdr, *hs_start, *hs_length;
515
wpa_printf(MSG_DEBUG, "TLSv1: Send ServerHelloDone");
517
pos += TLS_RECORD_HEADER_LEN;
519
/* opaque fragment[TLSPlaintext.length] */
523
/* HandshakeType msg_type */
524
*pos++ = TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE;
525
/* uint24 length (to be filled) */
528
/* body - ServerHelloDone (empty) */
530
WPA_PUT_BE24(hs_length, pos - hs_length - 3);
532
if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
533
rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
534
wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
535
tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
536
TLS_ALERT_INTERNAL_ERROR);
541
tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
549
static int tls_write_server_change_cipher_spec(struct tlsv1_server *conn,
550
u8 **msgpos, u8 *end)
557
wpa_printf(MSG_DEBUG, "TLSv1: Send ChangeCipherSpec");
559
pos += TLS_RECORD_HEADER_LEN;
560
*pos = TLS_CHANGE_CIPHER_SPEC;
561
if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC,
562
rhdr, end - rhdr, 1, &rlen) < 0) {
563
wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
564
tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
565
TLS_ALERT_INTERNAL_ERROR);
569
if (tlsv1_record_change_write_cipher(&conn->rl) < 0) {
570
wpa_printf(MSG_DEBUG, "TLSv1: Failed to set write cipher for "
572
tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
573
TLS_ALERT_INTERNAL_ERROR);
577
*msgpos = rhdr + rlen;
583
static int tls_write_server_finished(struct tlsv1_server *conn,
584
u8 **msgpos, u8 *end)
586
u8 *pos, *rhdr, *hs_start, *hs_length;
588
u8 verify_data[TLS_VERIFY_DATA_LEN];
589
u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
593
wpa_printf(MSG_DEBUG, "TLSv1: Send Finished");
595
/* Encrypted Handshake Message: Finished */
598
if (conn->verify.md5_server == NULL ||
599
crypto_hash_finish(conn->verify.md5_server, hash, &hlen) < 0) {
600
tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
601
TLS_ALERT_INTERNAL_ERROR);
602
conn->verify.md5_server = NULL;
603
crypto_hash_finish(conn->verify.sha1_server, NULL, NULL);
604
conn->verify.sha1_server = NULL;
607
conn->verify.md5_server = NULL;
609
if (conn->verify.sha1_server == NULL ||
610
crypto_hash_finish(conn->verify.sha1_server, hash + MD5_MAC_LEN,
612
conn->verify.sha1_server = NULL;
613
tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
614
TLS_ALERT_INTERNAL_ERROR);
617
conn->verify.sha1_server = NULL;
619
if (tls_prf(conn->master_secret, TLS_MASTER_SECRET_LEN,
620
"server finished", hash, MD5_MAC_LEN + SHA1_MAC_LEN,
621
verify_data, TLS_VERIFY_DATA_LEN)) {
622
wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate verify_data");
623
tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
624
TLS_ALERT_INTERNAL_ERROR);
627
wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (server)",
628
verify_data, TLS_VERIFY_DATA_LEN);
631
pos += TLS_RECORD_HEADER_LEN;
634
/* HandshakeType msg_type */
635
*pos++ = TLS_HANDSHAKE_TYPE_FINISHED;
636
/* uint24 length (to be filled) */
639
os_memcpy(pos, verify_data, TLS_VERIFY_DATA_LEN);
640
pos += TLS_VERIFY_DATA_LEN;
641
WPA_PUT_BE24(hs_length, pos - hs_length - 3);
642
tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
644
if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
645
rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
646
wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
647
tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
648
TLS_ALERT_INTERNAL_ERROR);
660
static u8 * tls_send_server_hello(struct tlsv1_server *conn, size_t *out_len)
667
msglen = 1000 + tls_server_cert_chain_der_len(conn);
669
msg = os_malloc(msglen);
676
if (tls_write_server_hello(conn, &pos, end) < 0) {
681
if (conn->use_session_ticket) {
682
/* Abbreviated handshake using session ticket; RFC 4507 */
683
if (tls_write_server_change_cipher_spec(conn, &pos, end) < 0 ||
684
tls_write_server_finished(conn, &pos, end) < 0) {
689
*out_len = pos - msg;
691
conn->state = CHANGE_CIPHER_SPEC;
697
if (tls_write_server_certificate(conn, &pos, end) < 0 ||
698
tls_write_server_key_exchange(conn, &pos, end) < 0 ||
699
tls_write_server_certificate_request(conn, &pos, end) < 0 ||
700
tls_write_server_hello_done(conn, &pos, end) < 0) {
705
*out_len = pos - msg;
707
conn->state = CLIENT_CERTIFICATE;
713
static u8 * tls_send_change_cipher_spec(struct tlsv1_server *conn,
720
msg = os_malloc(1000);
727
if (tls_write_server_change_cipher_spec(conn, &pos, end) < 0 ||
728
tls_write_server_finished(conn, &pos, end) < 0) {
733
*out_len = pos - msg;
735
wpa_printf(MSG_DEBUG, "TLSv1: Handshake completed successfully");
736
conn->state = ESTABLISHED;
742
u8 * tlsv1_server_handshake_write(struct tlsv1_server *conn, size_t *out_len)
744
switch (conn->state) {
746
return tls_send_server_hello(conn, out_len);
747
case SERVER_CHANGE_CIPHER_SPEC:
748
return tls_send_change_cipher_spec(conn, out_len);
750
if (conn->state == ESTABLISHED && conn->use_session_ticket) {
751
/* Abbreviated handshake was already completed. */
754
wpa_printf(MSG_DEBUG, "TLSv1: Unexpected state %d while "
755
"generating reply", conn->state);
761
u8 * tlsv1_server_send_alert(struct tlsv1_server *conn, u8 level,
762
u8 description, size_t *out_len)
764
u8 *alert, *pos, *length;
766
wpa_printf(MSG_DEBUG, "TLSv1: Send Alert(%d:%d)", level, description);
769
alert = os_malloc(10);
776
/* ContentType type */
777
*pos++ = TLS_CONTENT_TYPE_ALERT;
778
/* ProtocolVersion version */
779
WPA_PUT_BE16(pos, TLS_VERSION);
781
/* uint16 length (to be filled) */
784
/* opaque fragment[TLSPlaintext.length] */
787
/* AlertLevel level */
789
/* AlertDescription description */
790
*pos++ = description;
792
WPA_PUT_BE16(length, pos - length - 2);
793
*out_len = pos - alert;