Coverage Report

Created: 2025-04-24 06:18

/src/hostap/src/tls/tlsv1_server_read.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * TLSv1 server - read handshake message
3
 * Copyright (c) 2006-2014, Jouni Malinen <j@w1.fi>
4
 *
5
 * This software may be distributed under the terms of the BSD license.
6
 * See README for more details.
7
 */
8
9
#include "includes.h"
10
11
#include "common.h"
12
#include "crypto/md5.h"
13
#include "crypto/sha1.h"
14
#include "crypto/sha256.h"
15
#include "crypto/tls.h"
16
#include "x509v3.h"
17
#include "tlsv1_common.h"
18
#include "tlsv1_record.h"
19
#include "tlsv1_server.h"
20
#include "tlsv1_server_i.h"
21
22
23
static int tls_process_client_key_exchange(struct tlsv1_server *conn, u8 ct,
24
             const u8 *in_data, size_t *in_len);
25
static int tls_process_change_cipher_spec(struct tlsv1_server *conn,
26
            u8 ct, const u8 *in_data,
27
            size_t *in_len);
28
29
30
static int testing_cipher_suite_filter(struct tlsv1_server *conn, u16 suite)
31
0
{
32
#ifdef CONFIG_TESTING_OPTIONS
33
  if ((conn->test_flags &
34
       (TLS_BREAK_SRV_KEY_X_HASH | TLS_BREAK_SRV_KEY_X_SIGNATURE |
35
        TLS_DHE_PRIME_511B | TLS_DHE_PRIME_767B | TLS_DHE_PRIME_15 |
36
        TLS_DHE_PRIME_58B | TLS_DHE_NON_PRIME)) &&
37
      suite != TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 &&
38
      suite != TLS_DHE_RSA_WITH_AES_256_CBC_SHA &&
39
      suite != TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 &&
40
      suite != TLS_DHE_RSA_WITH_AES_128_CBC_SHA &&
41
      suite != TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA)
42
    return 1;
43
#endif /* CONFIG_TESTING_OPTIONS */
44
45
0
  return 0;
46
0
}
47
48
49
static void tls_process_status_request_item(struct tlsv1_server *conn,
50
              const u8 *req, size_t req_len)
51
0
{
52
0
  const u8 *pos, *end;
53
0
  u8 status_type;
54
55
0
  pos = req;
56
0
  end = req + req_len;
57
58
  /*
59
   * RFC 6961, 2.2:
60
   * struct {
61
   *   CertificateStatusType status_type;
62
   *   uint16 request_length;
63
   *   select (status_type) {
64
   *     case ocsp: OCSPStatusRequest;
65
   *     case ocsp_multi: OCSPStatusRequest;
66
   *   } request;
67
   * } CertificateStatusRequestItemV2;
68
   *
69
   * enum { ocsp(1), ocsp_multi(2), (255) } CertificateStatusType;
70
   */
71
72
0
  if (end - pos < 1)
73
0
    return; /* Truncated data */
74
75
0
  status_type = *pos++;
76
0
  wpa_printf(MSG_DEBUG, "TLSv1: CertificateStatusType %u", status_type);
77
0
  if (status_type != 1 && status_type != 2)
78
0
    return; /* Unsupported status type */
79
  /*
80
   * For now, only OCSP stapling is supported, so ignore the specific
81
   * request, if any.
82
   */
83
0
  wpa_hexdump(MSG_DEBUG, "TLSv1: OCSPStatusRequest", pos, end - pos);
84
85
0
  if (status_type == 2)
86
0
    conn->status_request_multi = 1;
87
0
}
88
89
90
static void tls_process_status_request_v2(struct tlsv1_server *conn,
91
            const u8 *ext, size_t ext_len)
92
0
{
93
0
  const u8 *pos, *end;
94
95
0
  conn->status_request_v2 = 1;
96
97
0
  pos = ext;
98
0
  end = ext + ext_len;
99
100
  /*
101
   * RFC 6961, 2.2:
102
   * struct {
103
   *   CertificateStatusRequestItemV2
104
   *                    certificate_status_req_list<1..2^16-1>;
105
   * } CertificateStatusRequestListV2;
106
   */
107
108
0
  while (end - pos >= 2) {
109
0
    u16 len;
110
111
0
    len = WPA_GET_BE16(pos);
112
0
    pos += 2;
113
0
    if (len > end - pos)
114
0
      break; /* Truncated data */
115
0
    tls_process_status_request_item(conn, pos, len);
116
0
    pos += len;
117
0
  }
118
0
}
119
120
121
static int tls_process_client_hello(struct tlsv1_server *conn, u8 ct,
122
            const u8 *in_data, size_t *in_len)
123
0
{
124
0
  const u8 *pos, *end, *c;
125
0
  size_t left, len, i, j;
126
0
  u16 cipher_suite;
127
0
  u16 num_suites;
128
0
  int compr_null_found;
129
0
  u16 ext_type, ext_len;
130
131
0
  if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
132
0
    tlsv1_server_log(conn, "Expected Handshake; received content type 0x%x",
133
0
         ct);
134
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
135
0
           TLS_ALERT_UNEXPECTED_MESSAGE);
136
0
    return -1;
137
0
  }
138
139
0
  pos = in_data;
140
0
  left = *in_len;
141
142
0
  if (left < 4) {
143
0
    tlsv1_server_log(conn,
144
0
         "Truncated handshake message (expected ClientHello)");
145
0
    goto decode_error;
146
0
  }
147
148
  /* HandshakeType msg_type */
149
0
  if (*pos != TLS_HANDSHAKE_TYPE_CLIENT_HELLO) {
150
0
    tlsv1_server_log(conn, "Received unexpected handshake message %d (expected ClientHello)",
151
0
         *pos);
152
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
153
0
           TLS_ALERT_UNEXPECTED_MESSAGE);
154
0
    return -1;
155
0
  }
156
0
  tlsv1_server_log(conn, "Received ClientHello");
157
0
  pos++;
158
  /* uint24 length */
159
0
  len = WPA_GET_BE24(pos);
160
0
  pos += 3;
161
0
  left -= 4;
162
163
0
  if (len > left) {
164
0
    tlsv1_server_log(conn,
165
0
         "Truncated ClientHello (len=%d left=%d)",
166
0
         (int) len, (int) left);
167
0
    goto decode_error;
168
0
  }
169
170
  /* body - ClientHello */
171
172
0
  wpa_hexdump(MSG_MSGDUMP, "TLSv1: ClientHello", pos, len);
173
0
  end = pos + len;
174
175
  /* ProtocolVersion client_version */
176
0
  if (end - pos < 2) {
177
0
    tlsv1_server_log(conn, "Truncated ClientHello/client_version");
178
0
    goto decode_error;
179
0
  }
180
0
  conn->client_version = WPA_GET_BE16(pos);
181
0
  tlsv1_server_log(conn, "Client version %d.%d",
182
0
       conn->client_version >> 8,
183
0
       conn->client_version & 0xff);
184
0
  if (conn->client_version < TLS_VERSION_1) {
185
0
    tlsv1_server_log(conn, "Unexpected protocol version in ClientHello %u.%u",
186
0
         conn->client_version >> 8,
187
0
         conn->client_version & 0xff);
188
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
189
0
           TLS_ALERT_PROTOCOL_VERSION);
190
0
    return -1;
191
0
  }
192
0
  pos += 2;
193
194
0
  if (TLS_VERSION == TLS_VERSION_1)
195
0
    conn->rl.tls_version = TLS_VERSION_1;
196
0
#ifdef CONFIG_TLSV12
197
0
  else if (conn->client_version >= TLS_VERSION_1_2)
198
0
    conn->rl.tls_version = TLS_VERSION_1_2;
199
0
#endif /* CONFIG_TLSV12 */
200
0
  else if (conn->client_version > TLS_VERSION_1_1)
201
0
    conn->rl.tls_version = TLS_VERSION_1_1;
202
0
  else
203
0
    conn->rl.tls_version = conn->client_version;
204
0
  tlsv1_server_log(conn, "Using TLS v%s",
205
0
       tls_version_str(conn->rl.tls_version));
206
207
  /* Random random */
208
0
  if (end - pos < TLS_RANDOM_LEN) {
209
0
    tlsv1_server_log(conn, "Truncated ClientHello/client_random");
210
0
    goto decode_error;
211
0
  }
212
213
0
  os_memcpy(conn->client_random, pos, TLS_RANDOM_LEN);
214
0
  pos += TLS_RANDOM_LEN;
215
0
  wpa_hexdump(MSG_MSGDUMP, "TLSv1: client_random",
216
0
        conn->client_random, TLS_RANDOM_LEN);
217
218
  /* SessionID session_id */
219
0
  if (end - pos < 1) {
220
0
    tlsv1_server_log(conn, "Truncated ClientHello/session_id len");
221
0
    goto decode_error;
222
0
  }
223
0
  if (end - pos < 1 + *pos || *pos > TLS_SESSION_ID_MAX_LEN) {
224
0
    tlsv1_server_log(conn, "Truncated ClientHello/session_id");
225
0
    goto decode_error;
226
0
  }
227
0
  wpa_hexdump(MSG_MSGDUMP, "TLSv1: client session_id", pos + 1, *pos);
228
0
  pos += 1 + *pos;
229
  /* TODO: add support for session resumption */
230
231
  /* CipherSuite cipher_suites<2..2^16-1> */
232
0
  if (end - pos < 2) {
233
0
    tlsv1_server_log(conn,
234
0
         "Truncated ClientHello/cipher_suites len");
235
0
    goto decode_error;
236
0
  }
237
0
  num_suites = WPA_GET_BE16(pos);
238
0
  pos += 2;
239
0
  if (end - pos < num_suites) {
240
0
    tlsv1_server_log(conn, "Truncated ClientHello/cipher_suites");
241
0
    goto decode_error;
242
0
  }
243
0
  wpa_hexdump(MSG_MSGDUMP, "TLSv1: client cipher suites",
244
0
        pos, num_suites);
245
0
  if (num_suites & 1) {
246
0
    tlsv1_server_log(conn, "Odd len ClientHello/cipher_suites");
247
0
    goto decode_error;
248
0
  }
249
0
  num_suites /= 2;
250
251
0
  cipher_suite = 0;
252
0
  for (i = 0; !cipher_suite && i < conn->num_cipher_suites; i++) {
253
0
    if (testing_cipher_suite_filter(conn, conn->cipher_suites[i]))
254
0
      continue;
255
0
    c = pos;
256
0
    for (j = 0; j < num_suites; j++) {
257
0
      u16 tmp = WPA_GET_BE16(c);
258
0
      c += 2;
259
0
      if (!cipher_suite && tmp == conn->cipher_suites[i]) {
260
0
        cipher_suite = tmp;
261
0
        break;
262
0
      }
263
0
    }
264
0
  }
265
0
  pos += num_suites * 2;
266
0
  if (!cipher_suite) {
267
0
    tlsv1_server_log(conn, "No supported cipher suite available");
268
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
269
0
           TLS_ALERT_ILLEGAL_PARAMETER);
270
0
    return -1;
271
0
  }
272
273
0
  if (tlsv1_record_set_cipher_suite(&conn->rl, cipher_suite) < 0) {
274
0
    wpa_printf(MSG_DEBUG, "TLSv1: Failed to set CipherSuite for "
275
0
         "record layer");
276
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
277
0
           TLS_ALERT_INTERNAL_ERROR);
278
0
    return -1;
279
0
  }
280
281
0
  conn->cipher_suite = cipher_suite;
282
283
  /* CompressionMethod compression_methods<1..2^8-1> */
284
0
  if (end - pos < 1) {
285
0
    tlsv1_server_log(conn,
286
0
         "Truncated ClientHello/compression_methods len");
287
0
    goto decode_error;
288
0
  }
289
0
  num_suites = *pos++;
290
0
  if (end - pos < num_suites) {
291
0
    tlsv1_server_log(conn,
292
0
         "Truncated ClientHello/compression_methods");
293
0
    goto decode_error;
294
0
  }
295
0
  wpa_hexdump(MSG_MSGDUMP, "TLSv1: client compression_methods",
296
0
        pos, num_suites);
297
0
  compr_null_found = 0;
298
0
  for (i = 0; i < num_suites; i++) {
299
0
    if (*pos++ == TLS_COMPRESSION_NULL)
300
0
      compr_null_found = 1;
301
0
  }
302
0
  if (!compr_null_found) {
303
0
    tlsv1_server_log(conn, "Client does not accept NULL compression");
304
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
305
0
           TLS_ALERT_ILLEGAL_PARAMETER);
306
0
    return -1;
307
0
  }
308
309
0
  if (end - pos == 1) {
310
0
    tlsv1_server_log(conn, "Unexpected extra octet in the end of ClientHello: 0x%02x",
311
0
         *pos);
312
0
    goto decode_error;
313
0
  }
314
315
0
  if (end - pos >= 2) {
316
    /* Extension client_hello_extension_list<0..2^16-1> */
317
0
    ext_len = WPA_GET_BE16(pos);
318
0
    pos += 2;
319
320
0
    tlsv1_server_log(conn, "%u bytes of ClientHello extensions",
321
0
         ext_len);
322
0
    if (end - pos != ext_len) {
323
0
      tlsv1_server_log(conn, "Invalid ClientHello extension list length %u (expected %u)",
324
0
           ext_len, (unsigned int) (end - pos));
325
0
      goto decode_error;
326
0
    }
327
328
    /*
329
     * struct {
330
     *   ExtensionType extension_type (0..65535)
331
     *   opaque extension_data<0..2^16-1>
332
     * } Extension;
333
     */
334
335
0
    while (pos < end) {
336
0
      if (end - pos < 2) {
337
0
        tlsv1_server_log(conn, "Invalid extension_type field");
338
0
        goto decode_error;
339
0
      }
340
341
0
      ext_type = WPA_GET_BE16(pos);
342
0
      pos += 2;
343
344
0
      if (end - pos < 2) {
345
0
        tlsv1_server_log(conn, "Invalid extension_data length field");
346
0
        goto decode_error;
347
0
      }
348
349
0
      ext_len = WPA_GET_BE16(pos);
350
0
      pos += 2;
351
352
0
      if (end - pos < ext_len) {
353
0
        tlsv1_server_log(conn, "Invalid extension_data field");
354
0
        goto decode_error;
355
0
      }
356
357
0
      tlsv1_server_log(conn, "ClientHello Extension type %u",
358
0
           ext_type);
359
0
      wpa_hexdump(MSG_MSGDUMP, "TLSv1: ClientHello "
360
0
            "Extension data", pos, ext_len);
361
362
0
      if (ext_type == TLS_EXT_SESSION_TICKET) {
363
0
        os_free(conn->session_ticket);
364
0
        conn->session_ticket = os_malloc(ext_len);
365
0
        if (conn->session_ticket) {
366
0
          os_memcpy(conn->session_ticket, pos,
367
0
              ext_len);
368
0
          conn->session_ticket_len = ext_len;
369
0
        }
370
0
      } else if (ext_type == TLS_EXT_STATUS_REQUEST) {
371
0
        conn->status_request = 1;
372
0
      } else if (ext_type == TLS_EXT_STATUS_REQUEST_V2) {
373
0
        tls_process_status_request_v2(conn, pos,
374
0
                    ext_len);
375
0
      }
376
377
0
      pos += ext_len;
378
0
    }
379
0
  }
380
381
0
  *in_len = end - in_data;
382
383
0
  tlsv1_server_log(conn, "ClientHello OK - proceed to ServerHello");
384
0
  conn->state = SERVER_HELLO;
385
386
0
  return 0;
387
388
0
decode_error:
389
0
  tlsv1_server_log(conn, "Failed to decode ClientHello");
390
0
  tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
391
0
         TLS_ALERT_DECODE_ERROR);
392
0
  return -1;
393
0
}
394
395
396
static int tls_process_certificate(struct tlsv1_server *conn, u8 ct,
397
           const u8 *in_data, size_t *in_len)
398
0
{
399
0
  const u8 *pos, *end;
400
0
  size_t left, len, list_len, cert_len, idx;
401
0
  u8 type;
402
0
  struct x509_certificate *chain = NULL, *last = NULL, *cert;
403
0
  int reason;
404
405
0
  if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
406
0
    tlsv1_server_log(conn, "Expected Handshake; received content type 0x%x",
407
0
         ct);
408
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
409
0
           TLS_ALERT_UNEXPECTED_MESSAGE);
410
0
    return -1;
411
0
  }
412
413
0
  pos = in_data;
414
0
  left = *in_len;
415
416
0
  if (left < 4) {
417
0
    tlsv1_server_log(conn, "Too short Certificate message (len=%lu)",
418
0
         (unsigned long) left);
419
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
420
0
           TLS_ALERT_DECODE_ERROR);
421
0
    return -1;
422
0
  }
423
424
0
  type = *pos++;
425
0
  len = WPA_GET_BE24(pos);
426
0
  pos += 3;
427
0
  left -= 4;
428
429
0
  if (len > left) {
430
0
    tlsv1_server_log(conn, "Unexpected Certificate message length (len=%lu != left=%lu)",
431
0
         (unsigned long) len, (unsigned long) left);
432
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
433
0
           TLS_ALERT_DECODE_ERROR);
434
0
    return -1;
435
0
  }
436
437
0
  if (type == TLS_HANDSHAKE_TYPE_CLIENT_KEY_EXCHANGE) {
438
0
    if (conn->verify_peer) {
439
0
      tlsv1_server_log(conn, "Client did not include Certificate");
440
0
      tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
441
0
             TLS_ALERT_UNEXPECTED_MESSAGE);
442
0
      return -1;
443
0
    }
444
445
0
    return tls_process_client_key_exchange(conn, ct, in_data,
446
0
                   in_len);
447
0
  }
448
0
  if (type != TLS_HANDSHAKE_TYPE_CERTIFICATE) {
449
0
    tlsv1_server_log(conn, "Received unexpected handshake message %d (expected Certificate/ClientKeyExchange)",
450
0
         type);
451
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
452
0
           TLS_ALERT_UNEXPECTED_MESSAGE);
453
0
    return -1;
454
0
  }
455
456
0
  tlsv1_server_log(conn, "Received Certificate (certificate_list len %lu)",
457
0
       (unsigned long) len);
458
459
  /*
460
   * opaque ASN.1Cert<2^24-1>;
461
   *
462
   * struct {
463
   *     ASN.1Cert certificate_list<1..2^24-1>;
464
   * } Certificate;
465
   */
466
467
0
  end = pos + len;
468
469
0
  if (end - pos < 3) {
470
0
    tlsv1_server_log(conn, "Too short Certificate (left=%lu)",
471
0
         (unsigned long) left);
472
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
473
0
           TLS_ALERT_DECODE_ERROR);
474
0
    return -1;
475
0
  }
476
477
0
  list_len = WPA_GET_BE24(pos);
478
0
  pos += 3;
479
480
0
  if ((size_t) (end - pos) != list_len) {
481
0
    tlsv1_server_log(conn, "Unexpected certificate_list length (len=%lu left=%lu)",
482
0
         (unsigned long) list_len,
483
0
         (unsigned long) (end - pos));
484
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
485
0
           TLS_ALERT_DECODE_ERROR);
486
0
    return -1;
487
0
  }
488
489
0
  idx = 0;
490
0
  while (pos < end) {
491
0
    if (end - pos < 3) {
492
0
      tlsv1_server_log(conn, "Failed to parse certificate_list");
493
0
      tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
494
0
             TLS_ALERT_DECODE_ERROR);
495
0
      x509_certificate_chain_free(chain);
496
0
      return -1;
497
0
    }
498
499
0
    cert_len = WPA_GET_BE24(pos);
500
0
    pos += 3;
501
502
0
    if ((size_t) (end - pos) < cert_len) {
503
0
      tlsv1_server_log(conn, "Unexpected certificate length (len=%lu left=%lu)",
504
0
           (unsigned long) cert_len,
505
0
           (unsigned long) (end - pos));
506
0
      tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
507
0
             TLS_ALERT_DECODE_ERROR);
508
0
      x509_certificate_chain_free(chain);
509
0
      return -1;
510
0
    }
511
512
0
    tlsv1_server_log(conn, "Certificate %lu (len %lu)",
513
0
         (unsigned long) idx, (unsigned long) cert_len);
514
515
0
    if (idx == 0) {
516
0
      crypto_public_key_free(conn->client_rsa_key);
517
0
      if (tls_parse_cert(pos, cert_len,
518
0
             &conn->client_rsa_key)) {
519
0
        tlsv1_server_log(conn, "Failed to parse the certificate");
520
0
        tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
521
0
               TLS_ALERT_BAD_CERTIFICATE);
522
0
        x509_certificate_chain_free(chain);
523
0
        return -1;
524
0
      }
525
0
    }
526
527
0
    cert = x509_certificate_parse(pos, cert_len);
528
0
    if (cert == NULL) {
529
0
      tlsv1_server_log(conn, "Failed to parse the certificate");
530
0
      tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
531
0
             TLS_ALERT_BAD_CERTIFICATE);
532
0
      x509_certificate_chain_free(chain);
533
0
      return -1;
534
0
    }
535
536
0
    if (last == NULL)
537
0
      chain = cert;
538
0
    else
539
0
      last->next = cert;
540
0
    last = cert;
541
542
0
    idx++;
543
0
    pos += cert_len;
544
0
  }
545
546
0
  if (x509_certificate_chain_validate(conn->cred->trusted_certs, chain,
547
0
              &reason, 0) < 0) {
548
0
    int tls_reason;
549
0
    tlsv1_server_log(conn, "Server certificate chain validation failed (reason=%d)",
550
0
         reason);
551
0
    switch (reason) {
552
0
    case X509_VALIDATE_BAD_CERTIFICATE:
553
0
      tls_reason = TLS_ALERT_BAD_CERTIFICATE;
554
0
      break;
555
0
    case X509_VALIDATE_UNSUPPORTED_CERTIFICATE:
556
0
      tls_reason = TLS_ALERT_UNSUPPORTED_CERTIFICATE;
557
0
      break;
558
0
    case X509_VALIDATE_CERTIFICATE_REVOKED:
559
0
      tls_reason = TLS_ALERT_CERTIFICATE_REVOKED;
560
0
      break;
561
0
    case X509_VALIDATE_CERTIFICATE_EXPIRED:
562
0
      tls_reason = TLS_ALERT_CERTIFICATE_EXPIRED;
563
0
      break;
564
0
    case X509_VALIDATE_CERTIFICATE_UNKNOWN:
565
0
      tls_reason = TLS_ALERT_CERTIFICATE_UNKNOWN;
566
0
      break;
567
0
    case X509_VALIDATE_UNKNOWN_CA:
568
0
      tls_reason = TLS_ALERT_UNKNOWN_CA;
569
0
      break;
570
0
    default:
571
0
      tls_reason = TLS_ALERT_BAD_CERTIFICATE;
572
0
      break;
573
0
    }
574
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL, tls_reason);
575
0
    x509_certificate_chain_free(chain);
576
0
    return -1;
577
0
  }
578
579
0
  if (chain && (chain->extensions_present & X509_EXT_EXT_KEY_USAGE) &&
580
0
      !(chain->ext_key_usage &
581
0
        (X509_EXT_KEY_USAGE_ANY | X509_EXT_KEY_USAGE_CLIENT_AUTH))) {
582
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
583
0
           TLS_ALERT_BAD_CERTIFICATE);
584
0
    x509_certificate_chain_free(chain);
585
0
    return -1;
586
0
  }
587
588
0
  x509_certificate_chain_free(chain);
589
590
0
  *in_len = end - in_data;
591
592
0
  conn->state = CLIENT_KEY_EXCHANGE;
593
594
0
  return 0;
595
0
}
596
597
598
static int tls_process_client_key_exchange_rsa(
599
  struct tlsv1_server *conn, const u8 *pos, const u8 *end)
600
0
{
601
0
  u8 *out;
602
0
  size_t outlen, outbuflen;
603
0
  u16 encr_len;
604
0
  int res;
605
0
  int use_random = 0;
606
607
0
  if (end - pos < 2) {
608
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
609
0
           TLS_ALERT_DECODE_ERROR);
610
0
    return -1;
611
0
  }
612
613
0
  encr_len = WPA_GET_BE16(pos);
614
0
  pos += 2;
615
0
  if (pos + encr_len > end) {
616
0
    tlsv1_server_log(conn, "Invalid ClientKeyExchange format: encr_len=%u left=%u",
617
0
         encr_len, (unsigned int) (end - pos));
618
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
619
0
           TLS_ALERT_DECODE_ERROR);
620
0
    return -1;
621
0
  }
622
623
0
  outbuflen = outlen = end - pos;
624
0
  out = os_malloc(outlen >= TLS_PRE_MASTER_SECRET_LEN ?
625
0
      outlen : TLS_PRE_MASTER_SECRET_LEN);
626
0
  if (out == NULL) {
627
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
628
0
           TLS_ALERT_INTERNAL_ERROR);
629
0
    return -1;
630
0
  }
631
632
  /*
633
   * struct {
634
   *   ProtocolVersion client_version;
635
   *   opaque random[46];
636
   * } PreMasterSecret;
637
   *
638
   * struct {
639
   *   public-key-encrypted PreMasterSecret pre_master_secret;
640
   * } EncryptedPreMasterSecret;
641
   */
642
643
  /*
644
   * Note: To avoid Bleichenbacher attack, we do not report decryption or
645
   * parsing errors from EncryptedPreMasterSecret processing to the
646
   * client. Instead, a random pre-master secret is used to force the
647
   * handshake to fail.
648
   */
649
650
0
  if (crypto_private_key_decrypt_pkcs1_v15(conn->cred->key,
651
0
             pos, encr_len,
652
0
             out, &outlen) < 0) {
653
0
    wpa_printf(MSG_DEBUG, "TLSv1: Failed to decrypt "
654
0
         "PreMasterSecret (encr_len=%u outlen=%lu)",
655
0
         encr_len, (unsigned long) outlen);
656
0
    use_random = 1;
657
0
  }
658
659
0
  if (!use_random && outlen != TLS_PRE_MASTER_SECRET_LEN) {
660
0
    tlsv1_server_log(conn, "Unexpected PreMasterSecret length %lu",
661
0
         (unsigned long) outlen);
662
0
    use_random = 1;
663
0
  }
664
665
0
  if (!use_random && WPA_GET_BE16(out) != conn->client_version) {
666
0
    tlsv1_server_log(conn, "Client version in ClientKeyExchange does not match with version in ClientHello");
667
0
    use_random = 1;
668
0
  }
669
670
0
  if (use_random) {
671
0
    wpa_printf(MSG_DEBUG, "TLSv1: Using random premaster secret "
672
0
         "to avoid revealing information about private key");
673
0
    outlen = TLS_PRE_MASTER_SECRET_LEN;
674
0
    if (os_get_random(out, outlen)) {
675
0
      wpa_printf(MSG_DEBUG, "TLSv1: Failed to get random "
676
0
           "data");
677
0
      tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
678
0
             TLS_ALERT_INTERNAL_ERROR);
679
0
      os_free(out);
680
0
      return -1;
681
0
    }
682
0
  }
683
684
0
  res = tlsv1_server_derive_keys(conn, out, outlen);
685
686
  /* Clear the pre-master secret since it is not needed anymore */
687
0
  os_memset(out, 0, outbuflen);
688
0
  os_free(out);
689
690
0
  if (res) {
691
0
    wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
692
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
693
0
           TLS_ALERT_INTERNAL_ERROR);
694
0
    return -1;
695
0
  }
696
697
0
  return 0;
698
0
}
699
700
701
static int tls_process_client_key_exchange_dh(
702
  struct tlsv1_server *conn, const u8 *pos, const u8 *end)
703
0
{
704
0
  const u8 *dh_yc;
705
0
  u16 dh_yc_len;
706
0
  u8 *shared;
707
0
  size_t shared_len;
708
0
  int res;
709
0
  const u8 *dh_p;
710
0
  size_t dh_p_len;
711
712
  /*
713
   * struct {
714
   *   select (PublicValueEncoding) {
715
   *     case implicit: struct { };
716
   *     case explicit: opaque dh_Yc<1..2^16-1>;
717
   *   } dh_public;
718
   * } ClientDiffieHellmanPublic;
719
   */
720
721
0
  tlsv1_server_log(conn, "ClientDiffieHellmanPublic received");
722
0
  wpa_hexdump(MSG_MSGDUMP, "TLSv1: ClientDiffieHellmanPublic",
723
0
        pos, end - pos);
724
725
0
  if (end == pos) {
726
0
    wpa_printf(MSG_DEBUG, "TLSv1: Implicit public value encoding "
727
0
         "not supported");
728
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
729
0
           TLS_ALERT_INTERNAL_ERROR);
730
0
    return -1;
731
0
  }
732
733
0
  if (end - pos < 3) {
734
0
    tlsv1_server_log(conn, "Invalid client public value length");
735
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
736
0
           TLS_ALERT_DECODE_ERROR);
737
0
    return -1;
738
0
  }
739
740
0
  dh_yc_len = WPA_GET_BE16(pos);
741
0
  dh_yc = pos + 2;
742
743
0
  if (dh_yc_len > end - dh_yc) {
744
0
    tlsv1_server_log(conn, "Client public value overflow (length %d)",
745
0
         dh_yc_len);
746
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
747
0
           TLS_ALERT_DECODE_ERROR);
748
0
    return -1;
749
0
  }
750
751
0
  wpa_hexdump(MSG_DEBUG, "TLSv1: DH Yc (client's public value)",
752
0
        dh_yc, dh_yc_len);
753
754
0
  if (conn->cred == NULL || conn->cred->dh_p == NULL ||
755
0
      conn->dh_secret == NULL) {
756
0
    wpa_printf(MSG_DEBUG, "TLSv1: No DH parameters available");
757
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
758
0
           TLS_ALERT_INTERNAL_ERROR);
759
0
    return -1;
760
0
  }
761
762
0
  tlsv1_server_get_dh_p(conn, &dh_p, &dh_p_len);
763
764
0
  shared_len = dh_p_len;
765
0
  shared = os_malloc(shared_len);
766
0
  if (shared == NULL) {
767
0
    wpa_printf(MSG_DEBUG, "TLSv1: Could not allocate memory for "
768
0
         "DH");
769
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
770
0
           TLS_ALERT_INTERNAL_ERROR);
771
0
    return -1;
772
0
  }
773
774
  /* shared = Yc^secret mod p */
775
0
  if (crypto_mod_exp(dh_yc, dh_yc_len, conn->dh_secret,
776
0
         conn->dh_secret_len, dh_p, dh_p_len,
777
0
         shared, &shared_len)) {
778
0
    os_free(shared);
779
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
780
0
           TLS_ALERT_INTERNAL_ERROR);
781
0
    return -1;
782
0
  }
783
0
  wpa_hexdump_key(MSG_DEBUG, "TLSv1: Shared secret from DH key exchange",
784
0
      shared, shared_len);
785
786
0
  os_memset(conn->dh_secret, 0, conn->dh_secret_len);
787
0
  os_free(conn->dh_secret);
788
0
  conn->dh_secret = NULL;
789
790
0
  res = tlsv1_server_derive_keys(conn, shared, shared_len);
791
792
  /* Clear the pre-master secret since it is not needed anymore */
793
0
  os_memset(shared, 0, shared_len);
794
0
  os_free(shared);
795
796
0
  if (res) {
797
0
    wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
798
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
799
0
           TLS_ALERT_INTERNAL_ERROR);
800
0
    return -1;
801
0
  }
802
803
0
  return 0;
804
0
}
805
806
807
static int tls_process_client_key_exchange(struct tlsv1_server *conn, u8 ct,
808
             const u8 *in_data, size_t *in_len)
809
0
{
810
0
  const u8 *pos, *end;
811
0
  size_t left, len;
812
0
  u8 type;
813
0
  tls_key_exchange keyx;
814
0
  const struct tls_cipher_suite *suite;
815
816
0
  if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
817
0
    tlsv1_server_log(conn, "Expected Handshake; received content type 0x%x",
818
0
         ct);
819
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
820
0
           TLS_ALERT_UNEXPECTED_MESSAGE);
821
0
    return -1;
822
0
  }
823
824
0
  pos = in_data;
825
0
  left = *in_len;
826
827
0
  if (left < 4) {
828
0
    tlsv1_server_log(conn, "Too short ClientKeyExchange (Left=%lu)",
829
0
         (unsigned long) left);
830
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
831
0
           TLS_ALERT_DECODE_ERROR);
832
0
    return -1;
833
0
  }
834
835
0
  type = *pos++;
836
0
  len = WPA_GET_BE24(pos);
837
0
  pos += 3;
838
0
  left -= 4;
839
840
0
  if (len > left) {
841
0
    tlsv1_server_log(conn, "Mismatch in ClientKeyExchange length (len=%lu != left=%lu)",
842
0
         (unsigned long) len, (unsigned long) left);
843
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
844
0
           TLS_ALERT_DECODE_ERROR);
845
0
    return -1;
846
0
  }
847
848
0
  end = pos + len;
849
850
0
  if (type != TLS_HANDSHAKE_TYPE_CLIENT_KEY_EXCHANGE) {
851
0
    tlsv1_server_log(conn, "Received unexpected handshake message %d (expected ClientKeyExchange)",
852
0
         type);
853
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
854
0
           TLS_ALERT_UNEXPECTED_MESSAGE);
855
0
    return -1;
856
0
  }
857
858
0
  tlsv1_server_log(conn, "Received ClientKeyExchange");
859
860
0
  wpa_hexdump(MSG_DEBUG, "TLSv1: ClientKeyExchange", pos, len);
861
862
0
  suite = tls_get_cipher_suite(conn->rl.cipher_suite);
863
0
  if (suite == NULL)
864
0
    keyx = TLS_KEY_X_NULL;
865
0
  else
866
0
    keyx = suite->key_exchange;
867
868
0
  if ((keyx == TLS_KEY_X_DH_anon || keyx == TLS_KEY_X_DHE_RSA) &&
869
0
      tls_process_client_key_exchange_dh(conn, pos, end) < 0)
870
0
    return -1;
871
872
0
  if (keyx != TLS_KEY_X_DH_anon && keyx != TLS_KEY_X_DHE_RSA &&
873
0
      tls_process_client_key_exchange_rsa(conn, pos, end) < 0)
874
0
    return -1;
875
876
0
  *in_len = end - in_data;
877
878
0
  conn->state = CERTIFICATE_VERIFY;
879
880
0
  return 0;
881
0
}
882
883
884
static int tls_process_certificate_verify(struct tlsv1_server *conn, u8 ct,
885
            const u8 *in_data, size_t *in_len)
886
0
{
887
0
  const u8 *pos, *end;
888
0
  size_t left, len;
889
0
  u8 type;
890
0
  size_t hlen;
891
0
  u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN], *hpos;
892
0
  u8 alert;
893
894
0
  if (ct == TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC) {
895
0
    if (conn->verify_peer) {
896
0
      tlsv1_server_log(conn, "Client did not include CertificateVerify");
897
0
      tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
898
0
             TLS_ALERT_UNEXPECTED_MESSAGE);
899
0
      return -1;
900
0
    }
901
902
0
    return tls_process_change_cipher_spec(conn, ct, in_data,
903
0
                  in_len);
904
0
  }
905
906
0
  if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
907
0
    tlsv1_server_log(conn, "Expected Handshake; received content type 0x%x",
908
0
         ct);
909
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
910
0
           TLS_ALERT_UNEXPECTED_MESSAGE);
911
0
    return -1;
912
0
  }
913
914
0
  pos = in_data;
915
0
  left = *in_len;
916
917
0
  if (left < 4) {
918
0
    tlsv1_server_log(conn, "Too short CertificateVerify message (len=%lu)",
919
0
         (unsigned long) left);
920
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
921
0
           TLS_ALERT_DECODE_ERROR);
922
0
    return -1;
923
0
  }
924
925
0
  type = *pos++;
926
0
  len = WPA_GET_BE24(pos);
927
0
  pos += 3;
928
0
  left -= 4;
929
930
0
  if (len > left) {
931
0
    tlsv1_server_log(conn, "Unexpected CertificateVerify message length (len=%lu != left=%lu)",
932
0
         (unsigned long) len, (unsigned long) left);
933
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
934
0
           TLS_ALERT_DECODE_ERROR);
935
0
    return -1;
936
0
  }
937
938
0
  end = pos + len;
939
940
0
  if (type != TLS_HANDSHAKE_TYPE_CERTIFICATE_VERIFY) {
941
0
    tlsv1_server_log(conn, "Received unexpected handshake message %d (expected CertificateVerify)",
942
0
         type);
943
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
944
0
           TLS_ALERT_UNEXPECTED_MESSAGE);
945
0
    return -1;
946
0
  }
947
948
0
  tlsv1_server_log(conn, "Received CertificateVerify");
949
950
  /*
951
   * struct {
952
   *   Signature signature;
953
   * } CertificateVerify;
954
   */
955
956
0
  hpos = hash;
957
958
0
#ifdef CONFIG_TLSV12
959
0
  if (conn->rl.tls_version == TLS_VERSION_1_2) {
960
    /*
961
     * RFC 5246, 4.7:
962
     * TLS v1.2 adds explicit indication of the used signature and
963
     * hash algorithms.
964
     *
965
     * struct {
966
     *   HashAlgorithm hash;
967
     *   SignatureAlgorithm signature;
968
     * } SignatureAndHashAlgorithm;
969
     */
970
0
    if (end - pos < 2) {
971
0
      tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
972
0
             TLS_ALERT_DECODE_ERROR);
973
0
      return -1;
974
0
    }
975
0
    if (pos[0] != TLS_HASH_ALG_SHA256 ||
976
0
        pos[1] != TLS_SIGN_ALG_RSA) {
977
0
      wpa_printf(MSG_DEBUG, "TLSv1.2: Unsupported hash(%u)/"
978
0
           "signature(%u) algorithm",
979
0
           pos[0], pos[1]);
980
0
      tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
981
0
             TLS_ALERT_INTERNAL_ERROR);
982
0
      return -1;
983
0
    }
984
0
    pos += 2;
985
986
0
    hlen = SHA256_MAC_LEN;
987
0
    if (conn->verify.sha256_cert == NULL ||
988
0
        crypto_hash_finish(conn->verify.sha256_cert, hpos, &hlen) <
989
0
        0) {
990
0
      conn->verify.sha256_cert = NULL;
991
0
      tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
992
0
             TLS_ALERT_INTERNAL_ERROR);
993
0
      return -1;
994
0
    }
995
0
    conn->verify.sha256_cert = NULL;
996
0
  } else {
997
0
#endif /* CONFIG_TLSV12 */
998
999
0
  hlen = MD5_MAC_LEN;
1000
0
  if (conn->verify.md5_cert == NULL ||
1001
0
      crypto_hash_finish(conn->verify.md5_cert, hpos, &hlen) < 0) {
1002
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1003
0
           TLS_ALERT_INTERNAL_ERROR);
1004
0
    conn->verify.md5_cert = NULL;
1005
0
    crypto_hash_finish(conn->verify.sha1_cert, NULL, NULL);
1006
0
    conn->verify.sha1_cert = NULL;
1007
0
    return -1;
1008
0
  }
1009
0
  hpos += MD5_MAC_LEN;
1010
1011
0
  conn->verify.md5_cert = NULL;
1012
0
  hlen = SHA1_MAC_LEN;
1013
0
  if (conn->verify.sha1_cert == NULL ||
1014
0
      crypto_hash_finish(conn->verify.sha1_cert, hpos, &hlen) < 0) {
1015
0
    conn->verify.sha1_cert = NULL;
1016
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1017
0
           TLS_ALERT_INTERNAL_ERROR);
1018
0
    return -1;
1019
0
  }
1020
0
  conn->verify.sha1_cert = NULL;
1021
1022
0
  hlen += MD5_MAC_LEN;
1023
1024
0
#ifdef CONFIG_TLSV12
1025
0
  }
1026
0
#endif /* CONFIG_TLSV12 */
1027
1028
0
  wpa_hexdump(MSG_MSGDUMP, "TLSv1: CertificateVerify hash", hash, hlen);
1029
1030
0
  if (tls_verify_signature(conn->rl.tls_version, conn->client_rsa_key,
1031
0
         hash, hlen, pos, end - pos, &alert) < 0) {
1032
0
    tlsv1_server_log(conn, "Invalid Signature in CertificateVerify");
1033
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL, alert);
1034
0
    return -1;
1035
0
  }
1036
1037
0
  *in_len = end - in_data;
1038
1039
0
  conn->state = CHANGE_CIPHER_SPEC;
1040
1041
0
  return 0;
1042
0
}
1043
1044
1045
static int tls_process_change_cipher_spec(struct tlsv1_server *conn,
1046
            u8 ct, const u8 *in_data,
1047
            size_t *in_len)
1048
0
{
1049
0
  const u8 *pos;
1050
0
  size_t left;
1051
1052
0
  if (ct != TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC) {
1053
0
    tlsv1_server_log(conn, "Expected ChangeCipherSpec; received content type 0x%x",
1054
0
         ct);
1055
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1056
0
           TLS_ALERT_UNEXPECTED_MESSAGE);
1057
0
    return -1;
1058
0
  }
1059
1060
0
  pos = in_data;
1061
0
  left = *in_len;
1062
1063
0
  if (left < 1) {
1064
0
    tlsv1_server_log(conn, "Too short ChangeCipherSpec");
1065
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1066
0
           TLS_ALERT_DECODE_ERROR);
1067
0
    return -1;
1068
0
  }
1069
1070
0
  if (*pos != TLS_CHANGE_CIPHER_SPEC) {
1071
0
    tlsv1_server_log(conn, "Expected ChangeCipherSpec; received data 0x%x",
1072
0
         *pos);
1073
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1074
0
           TLS_ALERT_UNEXPECTED_MESSAGE);
1075
0
    return -1;
1076
0
  }
1077
1078
0
  tlsv1_server_log(conn, "Received ChangeCipherSpec");
1079
0
  if (tlsv1_record_change_read_cipher(&conn->rl) < 0) {
1080
0
    wpa_printf(MSG_DEBUG, "TLSv1: Failed to change read cipher "
1081
0
         "for record layer");
1082
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1083
0
           TLS_ALERT_INTERNAL_ERROR);
1084
0
    return -1;
1085
0
  }
1086
1087
0
  *in_len = pos + 1 - in_data;
1088
1089
0
  conn->state = CLIENT_FINISHED;
1090
1091
0
  return 0;
1092
0
}
1093
1094
1095
static int tls_process_client_finished(struct tlsv1_server *conn, u8 ct,
1096
               const u8 *in_data, size_t *in_len)
1097
0
{
1098
0
  const u8 *pos, *end;
1099
0
  size_t left, len, hlen;
1100
0
  u8 verify_data[TLS_VERIFY_DATA_LEN];
1101
0
  u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
1102
1103
#ifdef CONFIG_TESTING_OPTIONS
1104
  if ((conn->test_flags &
1105
       (TLS_BREAK_SRV_KEY_X_HASH | TLS_BREAK_SRV_KEY_X_SIGNATURE)) &&
1106
      !conn->test_failure_reported) {
1107
    tlsv1_server_log(conn, "TEST-FAILURE: Client Finished received after invalid ServerKeyExchange");
1108
    conn->test_failure_reported = 1;
1109
  }
1110
1111
  if ((conn->test_flags & TLS_DHE_PRIME_15) &&
1112
      !conn->test_failure_reported) {
1113
    tlsv1_server_log(conn, "TEST-FAILURE: Client Finished received after bogus DHE \"prime\" 15");
1114
    conn->test_failure_reported = 1;
1115
  }
1116
1117
  if ((conn->test_flags & TLS_DHE_PRIME_58B) &&
1118
      !conn->test_failure_reported) {
1119
    tlsv1_server_log(conn, "TEST-FAILURE: Client Finished received after short 58-bit DHE prime in long container");
1120
    conn->test_failure_reported = 1;
1121
  }
1122
1123
  if ((conn->test_flags & TLS_DHE_PRIME_511B) &&
1124
      !conn->test_failure_reported) {
1125
    tlsv1_server_log(conn, "TEST-WARNING: Client Finished received after short 511-bit DHE prime (insecure)");
1126
    conn->test_failure_reported = 1;
1127
  }
1128
1129
  if ((conn->test_flags & TLS_DHE_PRIME_767B) &&
1130
      !conn->test_failure_reported) {
1131
    tlsv1_server_log(conn, "TEST-NOTE: Client Finished received after 767-bit DHE prime (relatively insecure)");
1132
    conn->test_failure_reported = 1;
1133
  }
1134
1135
  if ((conn->test_flags & TLS_DHE_NON_PRIME) &&
1136
      !conn->test_failure_reported) {
1137
    tlsv1_server_log(conn, "TEST-NOTE: Client Finished received after non-prime claimed as DHE prime");
1138
    conn->test_failure_reported = 1;
1139
  }
1140
#endif /* CONFIG_TESTING_OPTIONS */
1141
1142
0
  if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
1143
0
    tlsv1_server_log(conn, "Expected Finished; received content type 0x%x",
1144
0
         ct);
1145
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1146
0
           TLS_ALERT_UNEXPECTED_MESSAGE);
1147
0
    return -1;
1148
0
  }
1149
1150
0
  pos = in_data;
1151
0
  left = *in_len;
1152
1153
0
  if (left < 4) {
1154
0
    tlsv1_server_log(conn, "Too short record (left=%lu) forFinished",
1155
0
         (unsigned long) left);
1156
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1157
0
           TLS_ALERT_DECODE_ERROR);
1158
0
    return -1;
1159
0
  }
1160
1161
0
  if (pos[0] != TLS_HANDSHAKE_TYPE_FINISHED) {
1162
0
    wpa_printf(MSG_DEBUG, "TLSv1: Expected Finished; received "
1163
0
         "type 0x%x", pos[0]);
1164
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1165
0
           TLS_ALERT_UNEXPECTED_MESSAGE);
1166
0
    return -1;
1167
0
  }
1168
1169
0
  len = WPA_GET_BE24(pos + 1);
1170
1171
0
  pos += 4;
1172
0
  left -= 4;
1173
1174
0
  if (len > left) {
1175
0
    tlsv1_server_log(conn, "Too short buffer for Finished (len=%lu > left=%lu)",
1176
0
         (unsigned long) len, (unsigned long) left);
1177
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1178
0
           TLS_ALERT_DECODE_ERROR);
1179
0
    return -1;
1180
0
  }
1181
0
  end = pos + len;
1182
0
  if (len != TLS_VERIFY_DATA_LEN) {
1183
0
    tlsv1_server_log(conn, "Unexpected verify_data length in Finished: %lu (expected %d)",
1184
0
         (unsigned long) len, TLS_VERIFY_DATA_LEN);
1185
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1186
0
           TLS_ALERT_DECODE_ERROR);
1187
0
    return -1;
1188
0
  }
1189
0
  wpa_hexdump(MSG_MSGDUMP, "TLSv1: verify_data in Finished",
1190
0
        pos, TLS_VERIFY_DATA_LEN);
1191
1192
0
#ifdef CONFIG_TLSV12
1193
0
  if (conn->rl.tls_version >= TLS_VERSION_1_2) {
1194
0
    hlen = SHA256_MAC_LEN;
1195
0
    if (conn->verify.sha256_client == NULL ||
1196
0
        crypto_hash_finish(conn->verify.sha256_client, hash, &hlen)
1197
0
        < 0) {
1198
0
      tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1199
0
             TLS_ALERT_INTERNAL_ERROR);
1200
0
      conn->verify.sha256_client = NULL;
1201
0
      return -1;
1202
0
    }
1203
0
    conn->verify.sha256_client = NULL;
1204
0
  } else {
1205
0
#endif /* CONFIG_TLSV12 */
1206
1207
0
  hlen = MD5_MAC_LEN;
1208
0
  if (conn->verify.md5_client == NULL ||
1209
0
      crypto_hash_finish(conn->verify.md5_client, hash, &hlen) < 0) {
1210
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1211
0
           TLS_ALERT_INTERNAL_ERROR);
1212
0
    conn->verify.md5_client = NULL;
1213
0
    crypto_hash_finish(conn->verify.sha1_client, NULL, NULL);
1214
0
    conn->verify.sha1_client = NULL;
1215
0
    return -1;
1216
0
  }
1217
0
  conn->verify.md5_client = NULL;
1218
0
  hlen = SHA1_MAC_LEN;
1219
0
  if (conn->verify.sha1_client == NULL ||
1220
0
      crypto_hash_finish(conn->verify.sha1_client, hash + MD5_MAC_LEN,
1221
0
             &hlen) < 0) {
1222
0
    conn->verify.sha1_client = NULL;
1223
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1224
0
           TLS_ALERT_INTERNAL_ERROR);
1225
0
    return -1;
1226
0
  }
1227
0
  conn->verify.sha1_client = NULL;
1228
0
  hlen = MD5_MAC_LEN + SHA1_MAC_LEN;
1229
1230
0
#ifdef CONFIG_TLSV12
1231
0
  }
1232
0
#endif /* CONFIG_TLSV12 */
1233
1234
0
  if (tls_prf(conn->rl.tls_version,
1235
0
        conn->master_secret, TLS_MASTER_SECRET_LEN,
1236
0
        "client finished", hash, hlen,
1237
0
        verify_data, TLS_VERIFY_DATA_LEN)) {
1238
0
    wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive verify_data");
1239
0
    tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1240
0
           TLS_ALERT_DECRYPT_ERROR);
1241
0
    return -1;
1242
0
  }
1243
0
  wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (client)",
1244
0
      verify_data, TLS_VERIFY_DATA_LEN);
1245
1246
0
  if (os_memcmp_const(pos, verify_data, TLS_VERIFY_DATA_LEN) != 0) {
1247
0
    tlsv1_server_log(conn, "Mismatch in verify_data");
1248
0
    conn->state = FAILED;
1249
0
    return -1;
1250
0
  }
1251
1252
0
  tlsv1_server_log(conn, "Received Finished");
1253
1254
0
  *in_len = end - in_data;
1255
1256
0
  if (conn->use_session_ticket) {
1257
    /* Abbreviated handshake using session ticket; RFC 4507 */
1258
0
    tlsv1_server_log(conn, "Abbreviated handshake completed successfully");
1259
0
    conn->state = ESTABLISHED;
1260
0
  } else {
1261
    /* Full handshake */
1262
0
    conn->state = SERVER_CHANGE_CIPHER_SPEC;
1263
0
  }
1264
1265
0
  return 0;
1266
0
}
1267
1268
1269
int tlsv1_server_process_handshake(struct tlsv1_server *conn, u8 ct,
1270
           const u8 *buf, size_t *len)
1271
0
{
1272
0
  if (ct == TLS_CONTENT_TYPE_ALERT) {
1273
0
    if (*len < 2) {
1274
0
      tlsv1_server_log(conn, "Alert underflow");
1275
0
      tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
1276
0
             TLS_ALERT_DECODE_ERROR);
1277
0
      return -1;
1278
0
    }
1279
0
    tlsv1_server_log(conn, "Received alert %d:%d", buf[0], buf[1]);
1280
0
    *len = 2;
1281
0
    conn->state = FAILED;
1282
0
    return -1;
1283
0
  }
1284
1285
0
  switch (conn->state) {
1286
0
  case CLIENT_HELLO:
1287
0
    if (tls_process_client_hello(conn, ct, buf, len))
1288
0
      return -1;
1289
0
    break;
1290
0
  case CLIENT_CERTIFICATE:
1291
0
    if (tls_process_certificate(conn, ct, buf, len))
1292
0
      return -1;
1293
0
    break;
1294
0
  case CLIENT_KEY_EXCHANGE:
1295
0
    if (tls_process_client_key_exchange(conn, ct, buf, len))
1296
0
      return -1;
1297
0
    break;
1298
0
  case CERTIFICATE_VERIFY:
1299
0
    if (tls_process_certificate_verify(conn, ct, buf, len))
1300
0
      return -1;
1301
0
    break;
1302
0
  case CHANGE_CIPHER_SPEC:
1303
0
    if (tls_process_change_cipher_spec(conn, ct, buf, len))
1304
0
      return -1;
1305
0
    break;
1306
0
  case CLIENT_FINISHED:
1307
0
    if (tls_process_client_finished(conn, ct, buf, len))
1308
0
      return -1;
1309
0
    break;
1310
0
  default:
1311
0
    tlsv1_server_log(conn, "Unexpected state %d while processing received message",
1312
0
         conn->state);
1313
0
    return -1;
1314
0
  }
1315
1316
0
  if (ct == TLS_CONTENT_TYPE_HANDSHAKE)
1317
0
    tls_verify_hash_add(&conn->verify, buf, *len);
1318
1319
0
  return 0;
1320
0
}