Coverage Report

Created: 2025-04-24 06:18

/src/hostap/src/tls/tlsv1_client.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * TLS v1.0/v1.1/v1.2 client (RFC 2246, RFC 4346, RFC 5246)
3
 * Copyright (c) 2006-2019, 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/sha1.h"
13
#include "crypto/tls.h"
14
#include "x509v3.h"
15
#include "tlsv1_common.h"
16
#include "tlsv1_record.h"
17
#include "tlsv1_client.h"
18
#include "tlsv1_client_i.h"
19
20
/* TODO:
21
 * Support for a message fragmented across several records (RFC 2246, 6.2.1)
22
 */
23
24
25
void tls_alert(struct tlsv1_client *conn, u8 level, u8 description)
26
0
{
27
0
  conn->alert_level = level;
28
0
  conn->alert_description = description;
29
0
}
30
31
32
void tlsv1_client_free_dh(struct tlsv1_client *conn)
33
0
{
34
0
  os_free(conn->dh_p);
35
0
  os_free(conn->dh_g);
36
0
  os_free(conn->dh_ys);
37
0
  conn->dh_p = conn->dh_g = conn->dh_ys = NULL;
38
0
}
39
40
41
u16 tls_client_highest_ver(struct tlsv1_client *conn)
42
0
{
43
0
  u16 tls_version = TLS_VERSION;
44
45
  /* Pick the highest locally enabled TLS version */
46
0
#ifdef CONFIG_TLSV12
47
0
  if ((conn->flags & TLS_CONN_DISABLE_TLSv1_2) &&
48
0
      tls_version == TLS_VERSION_1_2)
49
0
    tls_version = TLS_VERSION_1_1;
50
0
#endif /* CONFIG_TLSV12 */
51
0
#ifdef CONFIG_TLSV11
52
0
  if ((conn->flags & TLS_CONN_DISABLE_TLSv1_1) &&
53
0
      tls_version == TLS_VERSION_1_1)
54
0
    tls_version = TLS_VERSION_1;
55
0
#endif /* CONFIG_TLSV11 */
56
0
  if ((conn->flags & TLS_CONN_DISABLE_TLSv1_0) &&
57
0
      tls_version == TLS_VERSION_1)
58
0
    return 0;
59
60
0
  return tls_version;
61
0
}
62
63
64
int tls_derive_pre_master_secret(struct tlsv1_client *conn,
65
         u8 *pre_master_secret)
66
0
{
67
0
  WPA_PUT_BE16(pre_master_secret, tls_client_highest_ver(conn));
68
0
  if (os_get_random(pre_master_secret + 2,
69
0
        TLS_PRE_MASTER_SECRET_LEN - 2))
70
0
    return -1;
71
0
  return 0;
72
0
}
73
74
75
int tls_derive_keys(struct tlsv1_client *conn,
76
        const u8 *pre_master_secret, size_t pre_master_secret_len)
77
0
{
78
0
  u8 seed[2 * TLS_RANDOM_LEN];
79
0
  u8 key_block[TLS_MAX_KEY_BLOCK_LEN];
80
0
  u8 *pos;
81
0
  size_t key_block_len;
82
83
0
  if (pre_master_secret) {
84
0
    wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: pre_master_secret",
85
0
        pre_master_secret, pre_master_secret_len);
86
0
    os_memcpy(seed, conn->client_random, TLS_RANDOM_LEN);
87
0
    os_memcpy(seed + TLS_RANDOM_LEN, conn->server_random,
88
0
        TLS_RANDOM_LEN);
89
0
    if (tls_prf(conn->rl.tls_version,
90
0
          pre_master_secret, pre_master_secret_len,
91
0
          "master secret", seed, 2 * TLS_RANDOM_LEN,
92
0
          conn->master_secret, TLS_MASTER_SECRET_LEN)) {
93
0
      wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive "
94
0
           "master_secret");
95
0
      return -1;
96
0
    }
97
0
    wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: master_secret",
98
0
        conn->master_secret, TLS_MASTER_SECRET_LEN);
99
0
  }
100
101
0
  os_memcpy(seed, conn->server_random, TLS_RANDOM_LEN);
102
0
  os_memcpy(seed + TLS_RANDOM_LEN, conn->client_random, TLS_RANDOM_LEN);
103
0
  key_block_len = 2 * (conn->rl.hash_size + conn->rl.key_material_len);
104
0
  if (conn->rl.tls_version == TLS_VERSION_1)
105
0
    key_block_len += 2 * conn->rl.iv_size;
106
0
  if (tls_prf(conn->rl.tls_version,
107
0
        conn->master_secret, TLS_MASTER_SECRET_LEN,
108
0
        "key expansion", seed, 2 * TLS_RANDOM_LEN,
109
0
        key_block, key_block_len)) {
110
0
    wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive key_block");
111
0
    return -1;
112
0
  }
113
0
  wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: key_block",
114
0
      key_block, key_block_len);
115
116
0
  pos = key_block;
117
118
  /* client_write_MAC_secret */
119
0
  os_memcpy(conn->rl.write_mac_secret, pos, conn->rl.hash_size);
120
0
  pos += conn->rl.hash_size;
121
  /* server_write_MAC_secret */
122
0
  os_memcpy(conn->rl.read_mac_secret, pos, conn->rl.hash_size);
123
0
  pos += conn->rl.hash_size;
124
125
  /* client_write_key */
126
0
  os_memcpy(conn->rl.write_key, pos, conn->rl.key_material_len);
127
0
  pos += conn->rl.key_material_len;
128
  /* server_write_key */
129
0
  os_memcpy(conn->rl.read_key, pos, conn->rl.key_material_len);
130
0
  pos += conn->rl.key_material_len;
131
132
0
  if (conn->rl.tls_version == TLS_VERSION_1) {
133
    /* client_write_IV */
134
0
    os_memcpy(conn->rl.write_iv, pos, conn->rl.iv_size);
135
0
    pos += conn->rl.iv_size;
136
    /* server_write_IV */
137
0
    os_memcpy(conn->rl.read_iv, pos, conn->rl.iv_size);
138
0
  } else {
139
    /*
140
     * Use IV field to set the mask value for TLS v1.1. A fixed
141
     * mask of zero is used per the RFC 4346, 6.2.3.2 CBC Block
142
     * Cipher option 2a.
143
     */
144
0
    os_memset(conn->rl.write_iv, 0, conn->rl.iv_size);
145
0
  }
146
147
0
  return 0;
148
0
}
149
150
151
/**
152
 * tlsv1_client_handshake - Process TLS handshake
153
 * @conn: TLSv1 client connection data from tlsv1_client_init()
154
 * @in_data: Input data from TLS peer
155
 * @in_len: Input data length
156
 * @out_len: Length of the output buffer.
157
 * @appl_data: Pointer to application data pointer, or %NULL if dropped
158
 * @appl_data_len: Pointer to variable that is set to appl_data length
159
 * @need_more_data: Set to 1 if more data would be needed to complete
160
 *  processing
161
 * Returns: Pointer to output data, %NULL on failure
162
 */
163
u8 * tlsv1_client_handshake(struct tlsv1_client *conn,
164
          const u8 *in_data, size_t in_len,
165
          size_t *out_len, u8 **appl_data,
166
          size_t *appl_data_len, int *need_more_data)
167
0
{
168
0
  const u8 *pos, *end;
169
0
  u8 *msg = NULL, *in_msg = NULL, *in_pos, *in_end, alert, ct;
170
0
  size_t in_msg_len;
171
0
  int no_appl_data;
172
0
  int used;
173
174
0
  if (need_more_data)
175
0
    *need_more_data = 0;
176
177
0
  if (conn->state == CLIENT_HELLO) {
178
0
    if (in_len)
179
0
      return NULL;
180
0
    return tls_send_client_hello(conn, out_len);
181
0
  }
182
183
0
  if (conn->partial_input) {
184
0
    if (wpabuf_resize(&conn->partial_input, in_len) < 0) {
185
0
      wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
186
0
           "memory for pending record");
187
0
      tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
188
0
          TLS_ALERT_INTERNAL_ERROR);
189
0
      goto failed;
190
0
    }
191
0
    wpabuf_put_data(conn->partial_input, in_data, in_len);
192
0
    in_data = wpabuf_head(conn->partial_input);
193
0
    in_len = wpabuf_len(conn->partial_input);
194
0
  }
195
196
0
  if (in_data == NULL || in_len == 0)
197
0
    return NULL;
198
199
0
  pos = in_data;
200
0
  end = in_data + in_len;
201
0
  in_msg = os_malloc(in_len);
202
0
  if (in_msg == NULL)
203
0
    return NULL;
204
205
  /* Each received packet may include multiple records */
206
0
  while (pos < end) {
207
0
    in_msg_len = in_len;
208
0
    used = tlsv1_record_receive(&conn->rl, pos, end - pos,
209
0
              in_msg, &in_msg_len, &alert);
210
0
    if (used < 0) {
211
0
      wpa_printf(MSG_DEBUG, "TLSv1: Processing received "
212
0
           "record failed");
213
0
      tls_alert(conn, TLS_ALERT_LEVEL_FATAL, alert);
214
0
      goto failed;
215
0
    }
216
0
    if (used == 0) {
217
0
      struct wpabuf *partial;
218
0
      wpa_printf(MSG_DEBUG, "TLSv1: Need more data");
219
0
      partial = wpabuf_alloc_copy(pos, end - pos);
220
0
      wpabuf_free(conn->partial_input);
221
0
      conn->partial_input = partial;
222
0
      if (conn->partial_input == NULL) {
223
0
        wpa_printf(MSG_DEBUG, "TLSv1: Failed to "
224
0
             "allocate memory for pending "
225
0
             "record");
226
0
        tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
227
0
            TLS_ALERT_INTERNAL_ERROR);
228
0
        goto failed;
229
0
      }
230
0
      os_free(in_msg);
231
0
      if (need_more_data)
232
0
        *need_more_data = 1;
233
0
      return NULL;
234
0
    }
235
0
    ct = pos[0];
236
237
0
    in_pos = in_msg;
238
0
    in_end = in_msg + in_msg_len;
239
240
    /* Each received record may include multiple messages of the
241
     * same ContentType. */
242
0
    while (in_pos < in_end) {
243
0
      in_msg_len = in_end - in_pos;
244
0
      if (tlsv1_client_process_handshake(conn, ct, in_pos,
245
0
                 &in_msg_len,
246
0
                 appl_data,
247
0
                 appl_data_len) < 0)
248
0
        goto failed;
249
0
      in_pos += in_msg_len;
250
0
    }
251
252
0
    pos += used;
253
0
  }
254
255
0
  os_free(in_msg);
256
0
  in_msg = NULL;
257
258
0
  no_appl_data = appl_data == NULL || *appl_data == NULL;
259
0
  msg = tlsv1_client_handshake_write(conn, out_len, no_appl_data);
260
261
0
failed:
262
0
  os_free(in_msg);
263
0
  if (conn->alert_level) {
264
0
    wpabuf_free(conn->partial_input);
265
0
    conn->partial_input = NULL;
266
0
    conn->state = FAILED;
267
0
    os_free(msg);
268
0
    msg = tlsv1_client_send_alert(conn, conn->alert_level,
269
0
                conn->alert_description,
270
0
                out_len);
271
0
  } else if (msg == NULL) {
272
0
    msg = os_zalloc(1);
273
0
    *out_len = 0;
274
0
  }
275
276
0
  if (need_more_data == NULL || !(*need_more_data)) {
277
0
    wpabuf_free(conn->partial_input);
278
0
    conn->partial_input = NULL;
279
0
  }
280
281
0
  return msg;
282
0
}
283
284
285
/**
286
 * tlsv1_client_encrypt - Encrypt data into TLS tunnel
287
 * @conn: TLSv1 client connection data from tlsv1_client_init()
288
 * @in_data: Pointer to plaintext data to be encrypted
289
 * @in_len: Input buffer length
290
 * @out_data: Pointer to output buffer (encrypted TLS data)
291
 * @out_len: Maximum out_data length
292
 * Returns: Number of bytes written to out_data, -1 on failure
293
 *
294
 * This function is used after TLS handshake has been completed successfully to
295
 * send data in the encrypted tunnel.
296
 */
297
int tlsv1_client_encrypt(struct tlsv1_client *conn,
298
       const u8 *in_data, size_t in_len,
299
       u8 *out_data, size_t out_len)
300
0
{
301
0
  size_t rlen;
302
303
0
  wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: Plaintext AppData",
304
0
      in_data, in_len);
305
306
0
  if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_APPLICATION_DATA,
307
0
            out_data, out_len, in_data, in_len, &rlen) < 0) {
308
0
    wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
309
0
    tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
310
0
        TLS_ALERT_INTERNAL_ERROR);
311
0
    return -1;
312
0
  }
313
314
0
  return rlen;
315
0
}
316
317
318
/**
319
 * tlsv1_client_decrypt - Decrypt data from TLS tunnel
320
 * @conn: TLSv1 client connection data from tlsv1_client_init()
321
 * @in_data: Pointer to input buffer (encrypted TLS data)
322
 * @in_len: Input buffer length
323
 * @need_more_data: Set to 1 if more data would be needed to complete
324
 *  processing
325
 * Returns: Decrypted data or %NULL on failure
326
 *
327
 * This function is used after TLS handshake has been completed successfully to
328
 * receive data from the encrypted tunnel.
329
 */
330
struct wpabuf * tlsv1_client_decrypt(struct tlsv1_client *conn,
331
             const u8 *in_data, size_t in_len,
332
             int *need_more_data)
333
0
{
334
0
  const u8 *in_end, *pos;
335
0
  int used;
336
0
  u8 alert, *out_pos, ct;
337
0
  size_t olen;
338
0
  struct wpabuf *buf = NULL;
339
340
0
  if (need_more_data)
341
0
    *need_more_data = 0;
342
343
0
  if (conn->partial_input) {
344
0
    if (wpabuf_resize(&conn->partial_input, in_len) < 0) {
345
0
      wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
346
0
           "memory for pending record");
347
0
      alert = TLS_ALERT_INTERNAL_ERROR;
348
0
      goto fail;
349
0
    }
350
0
    wpabuf_put_data(conn->partial_input, in_data, in_len);
351
0
    in_data = wpabuf_head(conn->partial_input);
352
0
    in_len = wpabuf_len(conn->partial_input);
353
0
  }
354
355
0
  pos = in_data;
356
0
  in_end = in_data + in_len;
357
358
0
  while (pos < in_end) {
359
0
    ct = pos[0];
360
0
    if (wpabuf_resize(&buf, in_end - pos) < 0) {
361
0
      alert = TLS_ALERT_INTERNAL_ERROR;
362
0
      goto fail;
363
0
    }
364
0
    out_pos = wpabuf_put(buf, 0);
365
0
    olen = wpabuf_tailroom(buf);
366
0
    used = tlsv1_record_receive(&conn->rl, pos, in_end - pos,
367
0
              out_pos, &olen, &alert);
368
0
    if (used < 0) {
369
0
      wpa_printf(MSG_DEBUG, "TLSv1: Record layer processing "
370
0
           "failed");
371
0
      goto fail;
372
0
    }
373
0
    if (used == 0) {
374
0
      struct wpabuf *partial;
375
0
      wpa_printf(MSG_DEBUG, "TLSv1: Need more data");
376
0
      partial = wpabuf_alloc_copy(pos, in_end - pos);
377
0
      wpabuf_free(conn->partial_input);
378
0
      conn->partial_input = partial;
379
0
      if (conn->partial_input == NULL) {
380
0
        wpa_printf(MSG_DEBUG, "TLSv1: Failed to "
381
0
             "allocate memory for pending "
382
0
             "record");
383
0
        alert = TLS_ALERT_INTERNAL_ERROR;
384
0
        goto fail;
385
0
      }
386
0
      if (need_more_data)
387
0
        *need_more_data = 1;
388
0
      return buf;
389
0
    }
390
391
0
    if (ct == TLS_CONTENT_TYPE_ALERT) {
392
0
      if (olen < 2) {
393
0
        wpa_printf(MSG_DEBUG, "TLSv1: Alert "
394
0
             "underflow");
395
0
        alert = TLS_ALERT_DECODE_ERROR;
396
0
        goto fail;
397
0
      }
398
0
      wpa_printf(MSG_DEBUG, "TLSv1: Received alert %d:%d",
399
0
           out_pos[0], out_pos[1]);
400
0
      if (out_pos[0] == TLS_ALERT_LEVEL_WARNING) {
401
        /* Continue processing */
402
0
        pos += used;
403
0
        continue;
404
0
      }
405
406
0
      alert = out_pos[1];
407
0
      goto fail;
408
0
    }
409
410
0
    if (ct != TLS_CONTENT_TYPE_APPLICATION_DATA) {
411
0
      wpa_printf(MSG_DEBUG, "TLSv1: Unexpected content type "
412
0
           "0x%x when decrypting application data",
413
0
           pos[0]);
414
0
      alert = TLS_ALERT_UNEXPECTED_MESSAGE;
415
0
      goto fail;
416
0
    }
417
418
0
    wpabuf_put(buf, olen);
419
420
0
    pos += used;
421
0
  }
422
423
0
  wpabuf_free(conn->partial_input);
424
0
  conn->partial_input = NULL;
425
0
  return buf;
426
427
0
fail:
428
0
  wpabuf_free(buf);
429
0
  wpabuf_free(conn->partial_input);
430
0
  conn->partial_input = NULL;
431
0
  tls_alert(conn, TLS_ALERT_LEVEL_FATAL, alert);
432
0
  return NULL;
433
0
}
434
435
436
/**
437
 * tlsv1_client_global_init - Initialize TLSv1 client
438
 * Returns: 0 on success, -1 on failure
439
 *
440
 * This function must be called before using any other TLSv1 client functions.
441
 */
442
int tlsv1_client_global_init(void)
443
2
{
444
2
  return crypto_global_init();
445
2
}
446
447
448
/**
449
 * tlsv1_client_global_deinit - Deinitialize TLSv1 client
450
 *
451
 * This function can be used to deinitialize the TLSv1 client that was
452
 * initialized by calling tlsv1_client_global_init(). No TLSv1 client functions
453
 * can be called after this before calling tlsv1_client_global_init() again.
454
 */
455
void tlsv1_client_global_deinit(void)
456
2
{
457
2
  crypto_global_deinit();
458
2
}
459
460
461
/**
462
 * tlsv1_client_init - Initialize TLSv1 client connection
463
 * Returns: Pointer to TLSv1 client connection data or %NULL on failure
464
 */
465
struct tlsv1_client * tlsv1_client_init(void)
466
0
{
467
0
  struct tlsv1_client *conn;
468
0
  size_t count;
469
0
  u16 *suites;
470
471
0
  conn = os_zalloc(sizeof(*conn));
472
0
  if (conn == NULL)
473
0
    return NULL;
474
475
0
  conn->state = CLIENT_HELLO;
476
477
0
  if (tls_verify_hash_init(&conn->verify) < 0) {
478
0
    wpa_printf(MSG_DEBUG, "TLSv1: Failed to initialize verify "
479
0
         "hash");
480
0
    os_free(conn);
481
0
    return NULL;
482
0
  }
483
484
0
  count = 0;
485
0
  suites = conn->cipher_suites;
486
0
  suites[count++] = TLS_DHE_RSA_WITH_AES_256_CBC_SHA256;
487
0
  suites[count++] = TLS_RSA_WITH_AES_256_CBC_SHA256;
488
0
  suites[count++] = TLS_DHE_RSA_WITH_AES_256_CBC_SHA;
489
0
  suites[count++] = TLS_RSA_WITH_AES_256_CBC_SHA;
490
0
  suites[count++] = TLS_DHE_RSA_WITH_AES_128_CBC_SHA256;
491
0
  suites[count++] = TLS_RSA_WITH_AES_128_CBC_SHA256;
492
0
  suites[count++] = TLS_DHE_RSA_WITH_AES_128_CBC_SHA;
493
0
  suites[count++] = TLS_RSA_WITH_AES_128_CBC_SHA;
494
0
  suites[count++] = TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA;
495
0
  suites[count++] = TLS_RSA_WITH_3DES_EDE_CBC_SHA;
496
0
  suites[count++] = TLS_RSA_WITH_RC4_128_SHA;
497
0
  suites[count++] = TLS_RSA_WITH_RC4_128_MD5;
498
0
  conn->num_cipher_suites = count;
499
500
0
  conn->rl.tls_version = TLS_VERSION;
501
502
0
  return conn;
503
0
}
504
505
506
/**
507
 * tlsv1_client_deinit - Deinitialize TLSv1 client connection
508
 * @conn: TLSv1 client connection data from tlsv1_client_init()
509
 */
510
void tlsv1_client_deinit(struct tlsv1_client *conn)
511
0
{
512
0
  crypto_public_key_free(conn->server_rsa_key);
513
0
  tlsv1_record_set_cipher_suite(&conn->rl, TLS_NULL_WITH_NULL_NULL);
514
0
  tlsv1_record_change_write_cipher(&conn->rl);
515
0
  tlsv1_record_change_read_cipher(&conn->rl);
516
0
  tls_verify_hash_free(&conn->verify);
517
0
  os_free(conn->client_hello_ext);
518
0
  tlsv1_client_free_dh(conn);
519
0
  tlsv1_cred_free(conn->cred);
520
0
  wpabuf_free(conn->partial_input);
521
0
  x509_certificate_chain_free(conn->server_cert);
522
0
  os_free(conn);
523
0
}
524
525
526
/**
527
 * tlsv1_client_established - Check whether connection has been established
528
 * @conn: TLSv1 client connection data from tlsv1_client_init()
529
 * Returns: 1 if connection is established, 0 if not
530
 */
531
int tlsv1_client_established(struct tlsv1_client *conn)
532
0
{
533
0
  return conn->state == ESTABLISHED;
534
0
}
535
536
537
/**
538
 * tlsv1_client_prf - Use TLS-PRF to derive keying material
539
 * @conn: TLSv1 client connection data from tlsv1_client_init()
540
 * @label: Label (e.g., description of the key) for PRF
541
 * @context: Optional extra upper-layer context (max len 2^16)
542
 * @context_len: The length of the context value
543
 * @server_random_first: seed is 0 = client_random|server_random,
544
 * 1 = server_random|client_random
545
 * @out: Buffer for output data from TLS-PRF
546
 * @out_len: Length of the output buffer
547
 * Returns: 0 on success, -1 on failure
548
 */
549
int tlsv1_client_prf(struct tlsv1_client *conn, const char *label,
550
         const u8 *context, size_t context_len,
551
         int server_random_first, u8 *out, size_t out_len)
552
0
{
553
0
  u8 *seed, *pos;
554
0
  size_t seed_len = 2 * TLS_RANDOM_LEN;
555
0
  int res;
556
557
0
  if (conn->state != ESTABLISHED)
558
0
    return -1;
559
560
0
  if (context_len > 65535)
561
0
    return -1;
562
563
0
  if (context)
564
0
    seed_len += 2 + context_len;
565
566
0
  seed = os_malloc(seed_len);
567
0
  if (!seed)
568
0
    return -1;
569
570
0
  if (server_random_first) {
571
0
    os_memcpy(seed, conn->server_random, TLS_RANDOM_LEN);
572
0
    os_memcpy(seed + TLS_RANDOM_LEN, conn->client_random,
573
0
        TLS_RANDOM_LEN);
574
0
  } else {
575
0
    os_memcpy(seed, conn->client_random, TLS_RANDOM_LEN);
576
0
    os_memcpy(seed + TLS_RANDOM_LEN, conn->server_random,
577
0
        TLS_RANDOM_LEN);
578
0
  }
579
580
0
  if (context) {
581
0
    pos = seed + 2 * TLS_RANDOM_LEN;
582
0
    WPA_PUT_BE16(pos, context_len);
583
0
    pos += 2;
584
0
    os_memcpy(pos, context, context_len);
585
0
  }
586
587
0
  res = tls_prf(conn->rl.tls_version,
588
0
          conn->master_secret, TLS_MASTER_SECRET_LEN,
589
0
          label, seed, seed_len, out, out_len);
590
0
  os_free(seed);
591
0
  return res;
592
0
}
593
594
595
/**
596
 * tlsv1_client_get_cipher - Get current cipher name
597
 * @conn: TLSv1 client connection data from tlsv1_client_init()
598
 * @buf: Buffer for the cipher name
599
 * @buflen: buf size
600
 * Returns: 0 on success, -1 on failure
601
 *
602
 * Get the name of the currently used cipher.
603
 */
604
int tlsv1_client_get_cipher(struct tlsv1_client *conn, char *buf,
605
          size_t buflen)
606
0
{
607
0
  char *cipher;
608
609
0
  switch (conn->rl.cipher_suite) {
610
0
  case TLS_RSA_WITH_RC4_128_MD5:
611
0
    cipher = "RC4-MD5";
612
0
    break;
613
0
  case TLS_RSA_WITH_RC4_128_SHA:
614
0
    cipher = "RC4-SHA";
615
0
    break;
616
0
  case TLS_RSA_WITH_DES_CBC_SHA:
617
0
    cipher = "DES-CBC-SHA";
618
0
    break;
619
0
  case TLS_RSA_WITH_3DES_EDE_CBC_SHA:
620
0
    cipher = "DES-CBC3-SHA";
621
0
    break;
622
0
  case TLS_DHE_RSA_WITH_DES_CBC_SHA:
623
0
    cipher = "DHE-RSA-DES-CBC-SHA";
624
0
    break;
625
0
  case TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA:
626
0
    cipher = "DHE-RSA-DES-CBC3-SHA";
627
0
    break;
628
0
  case TLS_DH_anon_WITH_RC4_128_MD5:
629
0
    cipher = "ADH-RC4-MD5";
630
0
    break;
631
0
  case TLS_DH_anon_WITH_DES_CBC_SHA:
632
0
    cipher = "ADH-DES-SHA";
633
0
    break;
634
0
  case TLS_DH_anon_WITH_3DES_EDE_CBC_SHA:
635
0
    cipher = "ADH-DES-CBC3-SHA";
636
0
    break;
637
0
  case TLS_RSA_WITH_AES_128_CBC_SHA:
638
0
    cipher = "AES-128-SHA";
639
0
    break;
640
0
  case TLS_DHE_RSA_WITH_AES_128_CBC_SHA:
641
0
    cipher = "DHE-RSA-AES-128-SHA";
642
0
    break;
643
0
  case TLS_DH_anon_WITH_AES_128_CBC_SHA:
644
0
    cipher = "ADH-AES-128-SHA";
645
0
    break;
646
0
  case TLS_RSA_WITH_AES_256_CBC_SHA:
647
0
    cipher = "AES-256-SHA";
648
0
    break;
649
0
  case TLS_DHE_RSA_WITH_AES_256_CBC_SHA:
650
0
    cipher = "DHE-RSA-AES-256-SHA";
651
0
    break;
652
0
  case TLS_DH_anon_WITH_AES_256_CBC_SHA:
653
0
    cipher = "ADH-AES-256-SHA";
654
0
    break;
655
0
  case TLS_RSA_WITH_AES_128_CBC_SHA256:
656
0
    cipher = "AES-128-SHA256";
657
0
    break;
658
0
  case TLS_RSA_WITH_AES_256_CBC_SHA256:
659
0
    cipher = "AES-256-SHA256";
660
0
    break;
661
0
  case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256:
662
0
    cipher = "DHE-RSA-AES-128-SHA256";
663
0
    break;
664
0
  case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256:
665
0
    cipher = "DHE-RSA-AES-256-SHA256";
666
0
    break;
667
0
  case TLS_DH_anon_WITH_AES_128_CBC_SHA256:
668
0
    cipher = "ADH-AES-128-SHA256";
669
0
    break;
670
0
  case TLS_DH_anon_WITH_AES_256_CBC_SHA256:
671
0
    cipher = "ADH-AES-256-SHA256";
672
0
    break;
673
0
  default:
674
0
    return -1;
675
0
  }
676
677
0
  if (os_strlcpy(buf, cipher, buflen) >= buflen)
678
0
    return -1;
679
0
  return 0;
680
0
}
681
682
683
/**
684
 * tlsv1_client_shutdown - Shutdown TLS connection
685
 * @conn: TLSv1 client connection data from tlsv1_client_init()
686
 * Returns: 0 on success, -1 on failure
687
 */
688
int tlsv1_client_shutdown(struct tlsv1_client *conn)
689
0
{
690
0
  conn->state = CLIENT_HELLO;
691
692
0
  if (tls_verify_hash_init(&conn->verify) < 0) {
693
0
    wpa_printf(MSG_DEBUG, "TLSv1: Failed to re-initialize verify "
694
0
         "hash");
695
0
    return -1;
696
0
  }
697
698
0
  tlsv1_record_set_cipher_suite(&conn->rl, TLS_NULL_WITH_NULL_NULL);
699
0
  tlsv1_record_change_write_cipher(&conn->rl);
700
0
  tlsv1_record_change_read_cipher(&conn->rl);
701
702
0
  conn->certificate_requested = 0;
703
0
  crypto_public_key_free(conn->server_rsa_key);
704
0
  conn->server_rsa_key = NULL;
705
0
  conn->session_resumed = 0;
706
707
0
  return 0;
708
0
}
709
710
711
/**
712
 * tlsv1_client_resumed - Was session resumption used
713
 * @conn: TLSv1 client connection data from tlsv1_client_init()
714
 * Returns: 1 if current session used session resumption, 0 if not
715
 */
716
int tlsv1_client_resumed(struct tlsv1_client *conn)
717
0
{
718
0
  return !!conn->session_resumed;
719
0
}
720
721
722
/**
723
 * tlsv1_client_hello_ext - Set TLS extension for ClientHello
724
 * @conn: TLSv1 client connection data from tlsv1_client_init()
725
 * @ext_type: Extension type
726
 * @data: Extension payload (%NULL to remove extension)
727
 * @data_len: Extension payload length
728
 * Returns: 0 on success, -1 on failure
729
 */
730
int tlsv1_client_hello_ext(struct tlsv1_client *conn, int ext_type,
731
         const u8 *data, size_t data_len)
732
0
{
733
0
  u8 *pos;
734
735
0
  conn->session_ticket_included = 0;
736
0
  os_free(conn->client_hello_ext);
737
0
  conn->client_hello_ext = NULL;
738
0
  conn->client_hello_ext_len = 0;
739
740
0
  if (data == NULL || data_len == 0)
741
0
    return 0;
742
743
0
  pos = conn->client_hello_ext = os_malloc(4 + data_len);
744
0
  if (pos == NULL)
745
0
    return -1;
746
747
0
  WPA_PUT_BE16(pos, ext_type);
748
0
  pos += 2;
749
0
  WPA_PUT_BE16(pos, data_len);
750
0
  pos += 2;
751
0
  os_memcpy(pos, data, data_len);
752
0
  conn->client_hello_ext_len = 4 + data_len;
753
754
0
  if (ext_type == TLS_EXT_PAC_OPAQUE) {
755
0
    conn->session_ticket_included = 1;
756
0
    wpa_printf(MSG_DEBUG, "TLSv1: Using session ticket");
757
0
  }
758
759
0
  return 0;
760
0
}
761
762
763
/**
764
 * tlsv1_client_get_random - Get random data from TLS connection
765
 * @conn: TLSv1 client connection data from tlsv1_client_init()
766
 * @keys: Structure of random data (filled on success)
767
 * Returns: 0 on success, -1 on failure
768
 */
769
int tlsv1_client_get_random(struct tlsv1_client *conn, struct tls_random *keys)
770
0
{
771
0
  os_memset(keys, 0, sizeof(*keys));
772
0
  if (conn->state == CLIENT_HELLO)
773
0
    return -1;
774
775
0
  keys->client_random = conn->client_random;
776
0
  keys->client_random_len = TLS_RANDOM_LEN;
777
778
0
  if (conn->state != SERVER_HELLO) {
779
0
    keys->server_random = conn->server_random;
780
0
    keys->server_random_len = TLS_RANDOM_LEN;
781
0
  }
782
783
0
  return 0;
784
0
}
785
786
787
/**
788
 * tlsv1_client_get_keyblock_size - Get TLS key_block size
789
 * @conn: TLSv1 client connection data from tlsv1_client_init()
790
 * Returns: Size of the key_block for the negotiated cipher suite or -1 on
791
 * failure
792
 */
793
int tlsv1_client_get_keyblock_size(struct tlsv1_client *conn)
794
0
{
795
0
  if (conn->state == CLIENT_HELLO || conn->state == SERVER_HELLO)
796
0
    return -1;
797
798
0
  return 2 * (conn->rl.hash_size + conn->rl.key_material_len +
799
0
        conn->rl.iv_size);
800
0
}
801
802
803
/**
804
 * tlsv1_client_set_cipher_list - Configure acceptable cipher suites
805
 * @conn: TLSv1 client connection data from tlsv1_client_init()
806
 * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
807
 * (TLS_CIPHER_*).
808
 * Returns: 0 on success, -1 on failure
809
 */
810
int tlsv1_client_set_cipher_list(struct tlsv1_client *conn, u8 *ciphers)
811
0
{
812
0
  size_t count;
813
0
  u16 *suites;
814
815
  /* TODO: implement proper configuration of cipher suites */
816
0
  if (ciphers[0] == TLS_CIPHER_ANON_DH_AES128_SHA) {
817
0
    count = 0;
818
0
    suites = conn->cipher_suites;
819
0
    suites[count++] = TLS_DH_anon_WITH_AES_256_CBC_SHA256;
820
0
    suites[count++] = TLS_DH_anon_WITH_AES_256_CBC_SHA;
821
0
    suites[count++] = TLS_DH_anon_WITH_AES_128_CBC_SHA256;
822
0
    suites[count++] = TLS_DH_anon_WITH_AES_128_CBC_SHA;
823
0
    suites[count++] = TLS_DH_anon_WITH_3DES_EDE_CBC_SHA;
824
0
    suites[count++] = TLS_DH_anon_WITH_RC4_128_MD5;
825
0
    suites[count++] = TLS_DH_anon_WITH_DES_CBC_SHA;
826
827
    /*
828
     * Cisco AP (at least 350 and 1200 series) local authentication
829
     * server does not know how to search cipher suites from the
830
     * list and seem to require that the last entry in the list is
831
     * the one that it wants to use. However, TLS specification
832
     * requires the list to be in the client preference order. As a
833
     * workaround, add anon-DH AES-128-SHA1 again at the end of the
834
     * list to allow the Cisco code to find it.
835
     */
836
0
    suites[count++] = TLS_DH_anon_WITH_AES_128_CBC_SHA;
837
0
    conn->num_cipher_suites = count;
838
0
  }
839
840
0
  return 0;
841
0
}
842
843
844
/**
845
 * tlsv1_client_set_cred - Set client credentials
846
 * @conn: TLSv1 client connection data from tlsv1_client_init()
847
 * @cred: Credentials from tlsv1_cred_alloc()
848
 * Returns: 0 on success, -1 on failure
849
 *
850
 * On success, the client takes ownership of the credentials block and caller
851
 * must not free it. On failure, caller is responsible for freeing the
852
 * credential block.
853
 */
854
int tlsv1_client_set_cred(struct tlsv1_client *conn,
855
        struct tlsv1_credentials *cred)
856
0
{
857
0
  tlsv1_cred_free(conn->cred);
858
0
  conn->cred = cred;
859
0
  return 0;
860
0
}
861
862
863
/**
864
 * tlsv1_client_set_flags - Set connection flags
865
 * @conn: TLSv1 client connection data from tlsv1_client_init()
866
 * @flags: TLS_CONN_* bitfield
867
 */
868
void tlsv1_client_set_flags(struct tlsv1_client *conn, unsigned int flags)
869
0
{
870
0
  conn->flags = flags;
871
0
  conn->rl.tls_version = tls_client_highest_ver(conn);
872
0
}
873
874
875
void tlsv1_client_set_session_ticket_cb(struct tlsv1_client *conn,
876
          tlsv1_client_session_ticket_cb cb,
877
          void *ctx)
878
0
{
879
0
  wpa_printf(MSG_DEBUG, "TLSv1: SessionTicket callback set %p (ctx %p)",
880
0
       cb, ctx);
881
0
  conn->session_ticket_cb = cb;
882
0
  conn->session_ticket_cb_ctx = ctx;
883
0
}
884
885
886
void tlsv1_client_set_cb(struct tlsv1_client *conn,
887
       void (*event_cb)(void *ctx, enum tls_event ev,
888
            union tls_event_data *data),
889
       void *cb_ctx,
890
       int cert_in_cb)
891
0
{
892
0
  conn->event_cb = event_cb;
893
0
  conn->cb_ctx = cb_ctx;
894
0
  conn->cert_in_cb = !!cert_in_cb;
895
0
}
896
897
898
int tlsv1_client_get_version(struct tlsv1_client *conn, char *buf,
899
           size_t buflen)
900
0
{
901
0
  if (!conn)
902
0
    return -1;
903
0
  switch (conn->rl.tls_version) {
904
0
  case TLS_VERSION_1:
905
0
    os_strlcpy(buf, "TLSv1", buflen);
906
0
    break;
907
0
  case TLS_VERSION_1_1:
908
0
    os_strlcpy(buf, "TLSv1.1", buflen);
909
0
    break;
910
0
  case TLS_VERSION_1_2:
911
0
    os_strlcpy(buf, "TLSv1.2", buflen);
912
0
    break;
913
0
  default:
914
0
    return -1;
915
0
  }
916
917
0
  return 0;
918
0
}