~ubuntu-branches/ubuntu/saucy/wpasupplicant/saucy

« back to all changes in this revision

Viewing changes to src/crypto/tls_internal.c

  • Committer: Bazaar Package Importer
  • Author(s): Mathieu Trudel-Lapierre
  • Date: 2010-11-22 09:43:43 UTC
  • mfrom: (1.1.16 upstream)
  • Revision ID: james.westby@ubuntu.com-20101122094343-qgsxaojvmswfri77
Tags: 0.7.3-0ubuntu1
* Get wpasupplicant 0.7.3 from Debian's SVN. Leaving 0.7.3-1 as unreleased
  for now.
* Build-Depend on debhelper 8, since the packaging from Debian uses compat 8.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * WPA Supplicant / TLS interface functions and an internal TLS implementation
3
 
 * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
 
2
 * TLS interface functions and an internal TLS implementation
 
3
 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
4
4
 *
5
5
 * This program is free software; you can redistribute it and/or modify
6
6
 * it under the terms of the GNU General Public License version 2 as
331
331
}
332
332
 
333
333
 
334
 
u8 * tls_connection_handshake(void *tls_ctx, struct tls_connection *conn,
335
 
                              const u8 *in_data, size_t in_len,
336
 
                              size_t *out_len, u8 **appl_data,
337
 
                              size_t *appl_data_len)
 
334
struct wpabuf * tls_connection_handshake(void *tls_ctx,
 
335
                                         struct tls_connection *conn,
 
336
                                         const struct wpabuf *in_data,
 
337
                                         struct wpabuf **appl_data)
338
338
{
339
339
#ifdef CONFIG_TLS_INTERNAL_CLIENT
 
340
        u8 *res, *ad;
 
341
        size_t res_len, ad_len;
 
342
        struct wpabuf *out;
 
343
 
340
344
        if (conn->client == NULL)
341
345
                return NULL;
342
346
 
 
347
        ad = NULL;
 
348
        res = tlsv1_client_handshake(conn->client,
 
349
                                     in_data ? wpabuf_head(in_data) : NULL,
 
350
                                     in_data ? wpabuf_len(in_data) : 0,
 
351
                                     &res_len, &ad, &ad_len);
 
352
        if (res == NULL)
 
353
                return NULL;
 
354
        out = wpabuf_alloc_ext_data(res, res_len);
 
355
        if (out == NULL) {
 
356
                os_free(res);
 
357
                os_free(ad);
 
358
                return NULL;
 
359
        }
 
360
        if (appl_data) {
 
361
                if (ad) {
 
362
                        *appl_data = wpabuf_alloc_ext_data(ad, ad_len);
 
363
                        if (*appl_data == NULL)
 
364
                                os_free(ad);
 
365
                } else
 
366
                        *appl_data = NULL;
 
367
        } else
 
368
                os_free(ad);
 
369
 
 
370
        return out;
 
371
#else /* CONFIG_TLS_INTERNAL_CLIENT */
 
372
        return NULL;
 
373
#endif /* CONFIG_TLS_INTERNAL_CLIENT */
 
374
}
 
375
 
 
376
 
 
377
struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
 
378
                                                struct tls_connection *conn,
 
379
                                                const struct wpabuf *in_data,
 
380
                                                struct wpabuf **appl_data)
 
381
{
 
382
#ifdef CONFIG_TLS_INTERNAL_SERVER
 
383
        u8 *res;
 
384
        size_t res_len;
 
385
        struct wpabuf *out;
 
386
 
 
387
        if (conn->server == NULL)
 
388
                return NULL;
 
389
 
343
390
        if (appl_data)
344
391
                *appl_data = NULL;
345
392
 
346
 
        wpa_printf(MSG_DEBUG, "TLS: %s(in_data=%p in_len=%lu)",
347
 
                   __func__, in_data, (unsigned long) in_len);
348
 
        return tlsv1_client_handshake(conn->client, in_data, in_len, out_len,
349
 
                                      appl_data, appl_data_len);
350
 
#else /* CONFIG_TLS_INTERNAL_CLIENT */
351
 
        return NULL;
352
 
#endif /* CONFIG_TLS_INTERNAL_CLIENT */
353
 
}
354
 
 
355
 
 
356
 
u8 * tls_connection_server_handshake(void *tls_ctx,
357
 
                                     struct tls_connection *conn,
358
 
                                     const u8 *in_data, size_t in_len,
359
 
                                     size_t *out_len)
360
 
{
361
 
#ifdef CONFIG_TLS_INTERNAL_SERVER
362
 
        u8 *out;
363
 
        if (conn->server == NULL)
364
 
                return NULL;
365
 
 
366
 
        wpa_printf(MSG_DEBUG, "TLS: %s(in_data=%p in_len=%lu)",
367
 
                   __func__, in_data, (unsigned long) in_len);
368
 
        out = tlsv1_server_handshake(conn->server, in_data, in_len, out_len);
369
 
        if (out == NULL && tlsv1_server_established(conn->server)) {
370
 
                out = os_malloc(1);
371
 
                *out_len = 0;
 
393
        res = tlsv1_server_handshake(conn->server, wpabuf_head(in_data),
 
394
                                     wpabuf_len(in_data), &res_len);
 
395
        if (res == NULL && tlsv1_server_established(conn->server))
 
396
                return wpabuf_alloc(0);
 
397
        if (res == NULL)
 
398
                return NULL;
 
399
        out = wpabuf_alloc_ext_data(res, res_len);
 
400
        if (out == NULL) {
 
401
                os_free(res);
 
402
                return NULL;
372
403
        }
 
404
 
373
405
        return out;
374
406
#else /* CONFIG_TLS_INTERNAL_SERVER */
375
407
        return NULL;
377
409
}
378
410
 
379
411
 
380
 
int tls_connection_encrypt(void *tls_ctx, struct tls_connection *conn,
381
 
                           const u8 *in_data, size_t in_len,
382
 
                           u8 *out_data, size_t out_len)
 
412
struct wpabuf * tls_connection_encrypt(void *tls_ctx,
 
413
                                       struct tls_connection *conn,
 
414
                                       const struct wpabuf *in_data)
383
415
{
384
416
#ifdef CONFIG_TLS_INTERNAL_CLIENT
385
417
        if (conn->client) {
386
 
                return tlsv1_client_encrypt(conn->client, in_data, in_len,
387
 
                                            out_data, out_len);
 
418
                struct wpabuf *buf;
 
419
                int res;
 
420
                buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
 
421
                if (buf == NULL)
 
422
                        return NULL;
 
423
                res = tlsv1_client_encrypt(conn->client, wpabuf_head(in_data),
 
424
                                           wpabuf_len(in_data),
 
425
                                           wpabuf_mhead(buf),
 
426
                                           wpabuf_size(buf));
 
427
                if (res < 0) {
 
428
                        wpabuf_free(buf);
 
429
                        return NULL;
 
430
                }
 
431
                wpabuf_put(buf, res);
 
432
                return buf;
388
433
        }
389
434
#endif /* CONFIG_TLS_INTERNAL_CLIENT */
390
435
#ifdef CONFIG_TLS_INTERNAL_SERVER
391
436
        if (conn->server) {
392
 
                return tlsv1_server_encrypt(conn->server, in_data, in_len,
393
 
                                            out_data, out_len);
 
437
                struct wpabuf *buf;
 
438
                int res;
 
439
                buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
 
440
                if (buf == NULL)
 
441
                        return NULL;
 
442
                res = tlsv1_server_encrypt(conn->server, wpabuf_head(in_data),
 
443
                                           wpabuf_len(in_data),
 
444
                                           wpabuf_mhead(buf),
 
445
                                           wpabuf_size(buf));
 
446
                if (res < 0) {
 
447
                        wpabuf_free(buf);
 
448
                        return NULL;
 
449
                }
 
450
                wpabuf_put(buf, res);
 
451
                return buf;
394
452
        }
395
453
#endif /* CONFIG_TLS_INTERNAL_SERVER */
396
 
        return -1;
 
454
        return NULL;
397
455
}
398
456
 
399
457
 
400
 
int tls_connection_decrypt(void *tls_ctx, struct tls_connection *conn,
401
 
                           const u8 *in_data, size_t in_len,
402
 
                           u8 *out_data, size_t out_len)
 
458
struct wpabuf * tls_connection_decrypt(void *tls_ctx,
 
459
                                       struct tls_connection *conn,
 
460
                                       const struct wpabuf *in_data)
403
461
{
404
462
#ifdef CONFIG_TLS_INTERNAL_CLIENT
405
463
        if (conn->client) {
406
 
                return tlsv1_client_decrypt(conn->client, in_data, in_len,
407
 
                                            out_data, out_len);
 
464
                struct wpabuf *buf;
 
465
                int res;
 
466
                buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
 
467
                if (buf == NULL)
 
468
                        return NULL;
 
469
                res = tlsv1_client_decrypt(conn->client, wpabuf_head(in_data),
 
470
                                           wpabuf_len(in_data),
 
471
                                           wpabuf_mhead(buf),
 
472
                                           wpabuf_size(buf));
 
473
                if (res < 0) {
 
474
                        wpabuf_free(buf);
 
475
                        return NULL;
 
476
                }
 
477
                wpabuf_put(buf, res);
 
478
                return buf;
408
479
        }
409
480
#endif /* CONFIG_TLS_INTERNAL_CLIENT */
410
481
#ifdef CONFIG_TLS_INTERNAL_SERVER
411
482
        if (conn->server) {
412
 
                return tlsv1_server_decrypt(conn->server, in_data, in_len,
413
 
                                            out_data, out_len);
 
483
                struct wpabuf *buf;
 
484
                int res;
 
485
                buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
 
486
                if (buf == NULL)
 
487
                        return NULL;
 
488
                res = tlsv1_server_decrypt(conn->server, wpabuf_head(in_data),
 
489
                                           wpabuf_len(in_data),
 
490
                                           wpabuf_mhead(buf),
 
491
                                           wpabuf_size(buf));
 
492
                if (res < 0) {
 
493
                        wpabuf_free(buf);
 
494
                        return NULL;
 
495
                }
 
496
                wpabuf_put(buf, res);
 
497
                return buf;
414
498
        }
415
499
#endif /* CONFIG_TLS_INTERNAL_SERVER */
416
 
        return -1;
 
500
        return NULL;
417
501
}
418
502
 
419
503
 
524
608
}
525
609
 
526
610
 
527
 
int tls_connection_ia_send_phase_finished(void *tls_ctx,
528
 
                                          struct tls_connection *conn,
529
 
                                          int final,
530
 
                                          u8 *out_data, size_t out_len)
 
611
struct wpabuf * tls_connection_ia_send_phase_finished(
 
612
        void *tls_ctx, struct tls_connection *conn, int final)
531
613
{
532
 
        return -1;
 
614
        return NULL;
533
615
}
534
616
 
535
617