diff options
| author | 2021-07-27 03:36:45 +0200 | |
|---|---|---|
| committer | 2021-07-27 03:36:45 +0200 | |
| commit | ab1c815340afc2883c9ef08795eebd9bed1858c5 (patch) | |
| tree | a188e92dfdff14e501bf42b5d880155468cffbdd /src | |
| parent | 19eca85f90d9950596e1c6021d7b0f89407f5d7f (diff) | |
| download | libimobiledevice-ab1c815340afc2883c9ef08795eebd9bed1858c5.tar.gz libimobiledevice-ab1c815340afc2883c9ef08795eebd9bed1858c5.tar.bz2 | |
Add support for MbedTLS
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile.am | 11 | ||||
| -rw-r--r-- | src/idevice.c | 195 | ||||
| -rw-r--r-- | src/idevice.h | 19 |
3 files changed, 188 insertions, 37 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 1ef47fc..96fe963 100644 --- a/src/Makefile.am +++ b/src/Makefile.am | |||
| @@ -5,20 +5,15 @@ AM_CPPFLAGS = \ | |||
| 5 | AM_CFLAGS = \ | 5 | AM_CFLAGS = \ |
| 6 | $(GLOBAL_CFLAGS) \ | 6 | $(GLOBAL_CFLAGS) \ |
| 7 | $(libusbmuxd_CFLAGS) \ | 7 | $(libusbmuxd_CFLAGS) \ |
| 8 | $(libgnutls_CFLAGS) \ | ||
| 9 | $(libtasn1_CFLAGS) \ | ||
| 10 | $(libplist_CFLAGS) \ | 8 | $(libplist_CFLAGS) \ |
| 9 | $(ssl_lib_CFLAGS) \ | ||
| 11 | $(LFS_CFLAGS) \ | 10 | $(LFS_CFLAGS) \ |
| 12 | $(openssl_CFLAGS) \ | ||
| 13 | $(PTHREAD_CFLAGS) | 11 | $(PTHREAD_CFLAGS) |
| 14 | 12 | ||
| 15 | AM_LDFLAGS = \ | 13 | AM_LDFLAGS = \ |
| 16 | $(libgnutls_LIBS) \ | ||
| 17 | $(libtasn1_LIBS) \ | ||
| 18 | $(libplist_LIBS) \ | ||
| 19 | $(libusbmuxd_LIBS) \ | 14 | $(libusbmuxd_LIBS) \ |
| 20 | $(libgcrypt_LIBS) \ | 15 | $(libplist_LIBS) \ |
| 21 | $(openssl_LIBS) \ | 16 | $(ssl_lib_LIBS) \ |
| 22 | $(PTHREAD_LIBS) | 17 | $(PTHREAD_LIBS) |
| 23 | 18 | ||
| 24 | lib_LTLIBRARIES = libimobiledevice-1.0.la | 19 | lib_LTLIBRARIES = libimobiledevice-1.0.la |
diff --git a/src/idevice.c b/src/idevice.c index e67a649..5b9c1ac 100644 --- a/src/idevice.c +++ b/src/idevice.c | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | * idevice.c | 2 | * idevice.c |
| 3 | * Device discovery and communication interface. | 3 | * Device discovery and communication interface. |
| 4 | * | 4 | * |
| 5 | * Copyright (c) 2009-2019 Nikias Bassen. All Rights Reserved. | 5 | * Copyright (c) 2009-2021 Nikias Bassen. All Rights Reserved. |
| 6 | * Copyright (c) 2014 Martin Szulecki All Rights Reserved. | 6 | * Copyright (c) 2014 Martin Szulecki All Rights Reserved. |
| 7 | * Copyright (c) 2008 Zach C. All Rights Reserved. | 7 | * Copyright (c) 2008 Zach C. All Rights Reserved. |
| 8 | * | 8 | * |
| @@ -31,12 +31,21 @@ | |||
| 31 | #include <time.h> | 31 | #include <time.h> |
| 32 | 32 | ||
| 33 | #include <usbmuxd.h> | 33 | #include <usbmuxd.h> |
| 34 | #ifdef HAVE_OPENSSL | 34 | |
| 35 | #if defined(HAVE_OPENSSL) | ||
| 35 | #include <openssl/err.h> | 36 | #include <openssl/err.h> |
| 36 | #include <openssl/rsa.h> | 37 | #include <openssl/rsa.h> |
| 37 | #include <openssl/ssl.h> | 38 | #include <openssl/ssl.h> |
| 38 | #else | 39 | #elif defined(HAVE_GNUTLS) |
| 39 | #include <gnutls/gnutls.h> | 40 | #include <gnutls/gnutls.h> |
| 41 | #elif defined(HAVE_MBEDTLS) | ||
| 42 | #include <mbedtls/rsa.h> | ||
| 43 | #include <mbedtls/ssl.h> | ||
| 44 | #include <mbedtls/entropy.h> | ||
| 45 | #include <mbedtls/ctr_drbg.h> | ||
| 46 | #include <mbedtls/debug.h> | ||
| 47 | #else | ||
| 48 | #error No supported TLS/SSL library enabled | ||
| 40 | #endif | 49 | #endif |
| 41 | 50 | ||
| 42 | #include "idevice.h" | 51 | #include "idevice.h" |
| @@ -106,7 +115,7 @@ static void id_function(CRYPTO_THREADID *thread) | |||
| 106 | 115 | ||
| 107 | static void internal_idevice_init(void) | 116 | static void internal_idevice_init(void) |
| 108 | { | 117 | { |
| 109 | #ifdef HAVE_OPENSSL | 118 | #if defined(HAVE_OPENSSL) |
| 110 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) | 119 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
| 111 | int i; | 120 | int i; |
| 112 | SSL_library_init(); | 121 | SSL_library_init(); |
| @@ -124,14 +133,16 @@ static void internal_idevice_init(void) | |||
| 124 | #endif | 133 | #endif |
| 125 | CRYPTO_set_locking_callback(locking_function); | 134 | CRYPTO_set_locking_callback(locking_function); |
| 126 | #endif | 135 | #endif |
| 127 | #else | 136 | #elif defined(HAVE_GNUTLS) |
| 128 | gnutls_global_init(); | 137 | gnutls_global_init(); |
| 138 | #elif defined(HAVE_MBEDTLS) | ||
| 139 | // NO-OP | ||
| 129 | #endif | 140 | #endif |
| 130 | } | 141 | } |
| 131 | 142 | ||
| 132 | static void internal_idevice_deinit(void) | 143 | static void internal_idevice_deinit(void) |
| 133 | { | 144 | { |
| 134 | #ifdef HAVE_OPENSSL | 145 | #if defined(HAVE_OPENSSL) |
| 135 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) | 146 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
| 136 | int i; | 147 | int i; |
| 137 | if (mutex_buf) { | 148 | if (mutex_buf) { |
| @@ -152,8 +163,10 @@ static void internal_idevice_deinit(void) | |||
| 152 | SSL_COMP_free_compression_methods(); | 163 | SSL_COMP_free_compression_methods(); |
| 153 | openssl_remove_thread_state(); | 164 | openssl_remove_thread_state(); |
| 154 | #endif | 165 | #endif |
| 155 | #else | 166 | #elif defined(HAVE_GNUTLS) |
| 156 | gnutls_global_deinit(); | 167 | gnutls_global_deinit(); |
| 168 | #elif defined(HAVE_MBEDTLS) | ||
| 169 | // NO-OP | ||
| 157 | #endif | 170 | #endif |
| 158 | } | 171 | } |
| 159 | 172 | ||
| @@ -556,7 +569,11 @@ static idevice_error_t internal_connection_send(idevice_connection_t connection, | |||
| 556 | 569 | ||
| 557 | LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_send(idevice_connection_t connection, const char *data, uint32_t len, uint32_t *sent_bytes) | 570 | LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_send(idevice_connection_t connection, const char *data, uint32_t len, uint32_t *sent_bytes) |
| 558 | { | 571 | { |
| 559 | if (!connection || !data || (connection->ssl_data && !connection->ssl_data->session)) { | 572 | if (!connection || !data |
| 573 | #if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS) | ||
| 574 | || (connection->ssl_data && !connection->ssl_data->session) | ||
| 575 | #endif | ||
| 576 | ) { | ||
| 560 | return IDEVICE_E_INVALID_ARG; | 577 | return IDEVICE_E_INVALID_ARG; |
| 561 | } | 578 | } |
| 562 | 579 | ||
| @@ -564,7 +581,7 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_send(idevice_connection_ | |||
| 564 | connection->status = IDEVICE_E_SUCCESS; | 581 | connection->status = IDEVICE_E_SUCCESS; |
| 565 | uint32_t sent = 0; | 582 | uint32_t sent = 0; |
| 566 | while (sent < len) { | 583 | while (sent < len) { |
| 567 | #ifdef HAVE_OPENSSL | 584 | #if defined(HAVE_OPENSSL) |
| 568 | int s = SSL_write(connection->ssl_data->session, (const void*)(data+sent), (int)(len-sent)); | 585 | int s = SSL_write(connection->ssl_data->session, (const void*)(data+sent), (int)(len-sent)); |
| 569 | if (s <= 0) { | 586 | if (s <= 0) { |
| 570 | int sslerr = SSL_get_error(connection->ssl_data->session, s); | 587 | int sslerr = SSL_get_error(connection->ssl_data->session, s); |
| @@ -573,8 +590,10 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_send(idevice_connection_ | |||
| 573 | } | 590 | } |
| 574 | break; | 591 | break; |
| 575 | } | 592 | } |
| 576 | #else | 593 | #elif defined(HAVE_GNUTLS) |
| 577 | ssize_t s = gnutls_record_send(connection->ssl_data->session, (void*)(data+sent), (size_t)(len-sent)); | 594 | ssize_t s = gnutls_record_send(connection->ssl_data->session, (void*)(data+sent), (size_t)(len-sent)); |
| 595 | #elif defined(HAVE_MBEDTLS) | ||
| 596 | int s = mbedtls_ssl_write(&connection->ssl_data->ctx, (const unsigned char*)(data+sent), (size_t)(len-sent)); | ||
| 578 | #endif | 597 | #endif |
| 579 | if (s < 0) { | 598 | if (s < 0) { |
| 580 | break; | 599 | break; |
| @@ -662,7 +681,12 @@ static idevice_error_t internal_connection_receive_timeout(idevice_connection_t | |||
| 662 | 681 | ||
| 663 | LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_receive_timeout(idevice_connection_t connection, char *data, uint32_t len, uint32_t *recv_bytes, unsigned int timeout) | 682 | LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_receive_timeout(idevice_connection_t connection, char *data, uint32_t len, uint32_t *recv_bytes, unsigned int timeout) |
| 664 | { | 683 | { |
| 665 | if (!connection || (connection->ssl_data && !connection->ssl_data->session) || len == 0) { | 684 | if (!connection |
| 685 | #if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS) | ||
| 686 | || (connection->ssl_data && !connection->ssl_data->session) | ||
| 687 | #endif | ||
| 688 | || len == 0 | ||
| 689 | ) { | ||
| 666 | return IDEVICE_E_INVALID_ARG; | 690 | return IDEVICE_E_INVALID_ARG; |
| 667 | } | 691 | } |
| 668 | 692 | ||
| @@ -678,7 +702,7 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_receive_timeout(idevice_ | |||
| 678 | connection->ssl_recv_timeout = timeout; | 702 | connection->ssl_recv_timeout = timeout; |
| 679 | connection->status = IDEVICE_E_SUCCESS; | 703 | connection->status = IDEVICE_E_SUCCESS; |
| 680 | while (received < len) { | 704 | while (received < len) { |
| 681 | #ifdef HAVE_OPENSSL | 705 | #if defined(HAVE_OPENSSL) |
| 682 | int r = SSL_read(connection->ssl_data->session, (void*)((char*)(data+received)), (int)len-received); | 706 | int r = SSL_read(connection->ssl_data->session, (void*)((char*)(data+received)), (int)len-received); |
| 683 | if (r > 0) { | 707 | if (r > 0) { |
| 684 | received += r; | 708 | received += r; |
| @@ -689,13 +713,20 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_receive_timeout(idevice_ | |||
| 689 | } | 713 | } |
| 690 | break; | 714 | break; |
| 691 | } | 715 | } |
| 692 | #else | 716 | #elif defined(HAVE_GNUTLS) |
| 693 | ssize_t r = gnutls_record_recv(connection->ssl_data->session, (void*)(data+received), (size_t)len-received); | 717 | ssize_t r = gnutls_record_recv(connection->ssl_data->session, (void*)(data+received), (size_t)len-received); |
| 694 | if (r > 0) { | 718 | if (r > 0) { |
| 695 | received += r; | 719 | received += r; |
| 696 | } else { | 720 | } else { |
| 697 | break; | 721 | break; |
| 698 | } | 722 | } |
| 723 | #elif defined(HAVE_MBEDTLS) | ||
| 724 | int r = mbedtls_ssl_read(&connection->ssl_data->ctx, (void*)(data+received), (size_t)len-received); | ||
| 725 | if (r > 0) { | ||
| 726 | received += r; | ||
| 727 | } else { | ||
| 728 | break; | ||
| 729 | } | ||
| 699 | #endif | 730 | #endif |
| 700 | } | 731 | } |
| 701 | connection->ssl_recv_timeout = (unsigned int)-1; | 732 | connection->ssl_recv_timeout = (unsigned int)-1; |
| @@ -744,7 +775,11 @@ static idevice_error_t internal_connection_receive(idevice_connection_t connecti | |||
| 744 | 775 | ||
| 745 | LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_receive(idevice_connection_t connection, char *data, uint32_t len, uint32_t *recv_bytes) | 776 | LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_receive(idevice_connection_t connection, char *data, uint32_t len, uint32_t *recv_bytes) |
| 746 | { | 777 | { |
| 747 | if (!connection || (connection->ssl_data && !connection->ssl_data->session)) { | 778 | if (!connection |
| 779 | #if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS) | ||
| 780 | || (connection->ssl_data && !connection->ssl_data->session) | ||
| 781 | #endif | ||
| 782 | ) { | ||
| 748 | return IDEVICE_E_INVALID_ARG; | 783 | return IDEVICE_E_INVALID_ARG; |
| 749 | } | 784 | } |
| 750 | 785 | ||
| @@ -753,11 +788,13 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_receive(idevice_connecti | |||
| 753 | debug_info("WARNING: ssl_recv_timeout was not properly reset in idevice_connection_receive_timeout"); | 788 | debug_info("WARNING: ssl_recv_timeout was not properly reset in idevice_connection_receive_timeout"); |
| 754 | connection->ssl_recv_timeout = (unsigned int)-1; | 789 | connection->ssl_recv_timeout = (unsigned int)-1; |
| 755 | } | 790 | } |
| 756 | #ifdef HAVE_OPENSSL | 791 | #if defined(HAVE_OPENSSL) |
| 757 | int received = SSL_read(connection->ssl_data->session, (void*)data, (int)len); | 792 | int received = SSL_read(connection->ssl_data->session, (void*)data, (int)len); |
| 758 | debug_info("SSL_read %d, received %d", len, received); | 793 | debug_info("SSL_read %d, received %d", len, received); |
| 759 | #else | 794 | #elif defined(HAVE_GNUTLS) |
| 760 | ssize_t received = gnutls_record_recv(connection->ssl_data->session, (void*)data, (size_t)len); | 795 | ssize_t received = gnutls_record_recv(connection->ssl_data->session, (void*)data, (size_t)len); |
| 796 | #elif defined(HAVE_MBEDTLS) | ||
| 797 | int received = mbedtls_ssl_read(&connection->ssl_data->ctx, (unsigned char*)data, (size_t)len); | ||
| 761 | #endif | 798 | #endif |
| 762 | if (received > 0) { | 799 | if (received > 0) { |
| 763 | *recv_bytes = received; | 800 | *recv_bytes = received; |
| @@ -806,10 +843,16 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_get_udid(idevice_t device, char **u | |||
| 806 | return IDEVICE_E_SUCCESS; | 843 | return IDEVICE_E_SUCCESS; |
| 807 | } | 844 | } |
| 808 | 845 | ||
| 846 | #if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS) | ||
| 847 | typedef ssize_t ssl_cb_ret_type_t; | ||
| 848 | #elif defined(HAVE_MBEDTLS) | ||
| 849 | typedef int ssl_cb_ret_type_t; | ||
| 850 | #endif | ||
| 851 | |||
| 809 | /** | 852 | /** |
| 810 | * Internally used SSL callback function for receiving encrypted data. | 853 | * Internally used SSL callback function for receiving encrypted data. |
| 811 | */ | 854 | */ |
| 812 | static ssize_t internal_ssl_read(idevice_connection_t connection, char *buffer, size_t length) | 855 | static ssl_cb_ret_type_t internal_ssl_read(idevice_connection_t connection, char *buffer, size_t length) |
| 813 | { | 856 | { |
| 814 | uint32_t bytes = 0; | 857 | uint32_t bytes = 0; |
| 815 | uint32_t pos = 0; | 858 | uint32_t pos = 0; |
| @@ -849,7 +892,7 @@ static ssize_t internal_ssl_read(idevice_connection_t connection, char *buffer, | |||
| 849 | /** | 892 | /** |
| 850 | * Internally used SSL callback function for sending encrypted data. | 893 | * Internally used SSL callback function for sending encrypted data. |
| 851 | */ | 894 | */ |
| 852 | static ssize_t internal_ssl_write(idevice_connection_t connection, const char *buffer, size_t length) | 895 | static ssl_cb_ret_type_t internal_ssl_write(idevice_connection_t connection, const char *buffer, size_t length) |
| 853 | { | 896 | { |
| 854 | uint32_t bytes = 0; | 897 | uint32_t bytes = 0; |
| 855 | idevice_error_t res; | 898 | idevice_error_t res; |
| @@ -871,14 +914,14 @@ static void internal_ssl_cleanup(ssl_data_t ssl_data) | |||
| 871 | if (!ssl_data) | 914 | if (!ssl_data) |
| 872 | return; | 915 | return; |
| 873 | 916 | ||
| 874 | #ifdef HAVE_OPENSSL | 917 | #if defined(HAVE_OPENSSL) |
| 875 | if (ssl_data->session) { | 918 | if (ssl_data->session) { |
| 876 | SSL_free(ssl_data->session); | 919 | SSL_free(ssl_data->session); |
| 877 | } | 920 | } |
| 878 | if (ssl_data->ctx) { | 921 | if (ssl_data->ctx) { |
| 879 | SSL_CTX_free(ssl_data->ctx); | 922 | SSL_CTX_free(ssl_data->ctx); |
| 880 | } | 923 | } |
| 881 | #else | 924 | #elif defined(HAVE_GNUTLS) |
| 882 | if (ssl_data->session) { | 925 | if (ssl_data->session) { |
| 883 | gnutls_deinit(ssl_data->session); | 926 | gnutls_deinit(ssl_data->session); |
| 884 | } | 927 | } |
| @@ -897,6 +940,13 @@ static void internal_ssl_cleanup(ssl_data_t ssl_data) | |||
| 897 | if (ssl_data->host_privkey) { | 940 | if (ssl_data->host_privkey) { |
| 898 | gnutls_x509_privkey_deinit(ssl_data->host_privkey); | 941 | gnutls_x509_privkey_deinit(ssl_data->host_privkey); |
| 899 | } | 942 | } |
| 943 | #elif defined(HAVE_MBEDTLS) | ||
| 944 | mbedtls_pk_free(&ssl_data->root_privkey); | ||
| 945 | mbedtls_x509_crt_free(&ssl_data->certificate); | ||
| 946 | mbedtls_entropy_free(&ssl_data->entropy); | ||
| 947 | mbedtls_ctr_drbg_free(&ssl_data->ctr_drbg); | ||
| 948 | mbedtls_ssl_config_free(&ssl_data->config); | ||
| 949 | mbedtls_ssl_free(&ssl_data->ctx); | ||
| 900 | #endif | 950 | #endif |
| 901 | } | 951 | } |
| 902 | 952 | ||
| @@ -961,7 +1011,7 @@ static const char *ssl_error_to_string(int e) | |||
| 961 | #endif | 1011 | #endif |
| 962 | #endif | 1012 | #endif |
| 963 | 1013 | ||
| 964 | #ifndef HAVE_OPENSSL | 1014 | #if defined(HAVE_GNUTLS) |
| 965 | /** | 1015 | /** |
| 966 | * Internally used gnutls callback function that gets called during handshake. | 1016 | * Internally used gnutls callback function that gets called during handshake. |
| 967 | */ | 1017 | */ |
| @@ -992,6 +1042,23 @@ static int internal_cert_callback(gnutls_session_t session, const gnutls_datum_t | |||
| 992 | } | 1042 | } |
| 993 | return res; | 1043 | return res; |
| 994 | } | 1044 | } |
| 1045 | #elif defined(HAVE_MBEDTLS) | ||
| 1046 | static void _mbedtls_log_cb(void* ctx, int level, const char* filename, int line, const char* message) | ||
| 1047 | { | ||
| 1048 | fprintf(stderr, "[mbedtls][%d] %s:%d => %s", level, filename, line, message); | ||
| 1049 | } | ||
| 1050 | |||
| 1051 | static int cert_verify_cb(void* ctx, mbedtls_x509_crt* cert, int depth, uint32_t *flags) | ||
| 1052 | { | ||
| 1053 | *flags = 0; | ||
| 1054 | return 0; | ||
| 1055 | } | ||
| 1056 | |||
| 1057 | static int _mbedtls_f_rng(void* p_rng, unsigned char* buf, size_t len) | ||
| 1058 | { | ||
| 1059 | memset(buf, 4, len); | ||
| 1060 | return 0; | ||
| 1061 | } | ||
| 995 | #endif | 1062 | #endif |
| 996 | 1063 | ||
| 997 | LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_enable_ssl(idevice_connection_t connection) | 1064 | LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_enable_ssl(idevice_connection_t connection) |
| @@ -1008,7 +1075,7 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_enable_ssl(idevice_conne | |||
| 1008 | return ret; | 1075 | return ret; |
| 1009 | } | 1076 | } |
| 1010 | 1077 | ||
| 1011 | #ifdef HAVE_OPENSSL | 1078 | #if defined(HAVE_OPENSSL) |
| 1012 | key_data_t root_cert = { NULL, 0 }; | 1079 | key_data_t root_cert = { NULL, 0 }; |
| 1013 | key_data_t root_privkey = { NULL, 0 }; | 1080 | key_data_t root_privkey = { NULL, 0 }; |
| 1014 | 1081 | ||
| @@ -1118,7 +1185,7 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_enable_ssl(idevice_conne | |||
| 1118 | } | 1185 | } |
| 1119 | /* required for proper multi-thread clean up to prevent leaks */ | 1186 | /* required for proper multi-thread clean up to prevent leaks */ |
| 1120 | openssl_remove_thread_state(); | 1187 | openssl_remove_thread_state(); |
| 1121 | #else | 1188 | #elif defined(HAVE_GNUTLS) |
| 1122 | ssl_data_t ssl_data_loc = (ssl_data_t)malloc(sizeof(struct ssl_data_private)); | 1189 | ssl_data_t ssl_data_loc = (ssl_data_t)malloc(sizeof(struct ssl_data_private)); |
| 1123 | 1190 | ||
| 1124 | /* Set up GnuTLS... */ | 1191 | /* Set up GnuTLS... */ |
| @@ -1176,6 +1243,82 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_enable_ssl(idevice_conne | |||
| 1176 | ret = IDEVICE_E_SUCCESS; | 1243 | ret = IDEVICE_E_SUCCESS; |
| 1177 | debug_info("SSL mode enabled"); | 1244 | debug_info("SSL mode enabled"); |
| 1178 | } | 1245 | } |
| 1246 | #elif defined(HAVE_MBEDTLS) | ||
| 1247 | key_data_t root_cert = { NULL, 0 }; | ||
| 1248 | key_data_t root_privkey = { NULL, 0 }; | ||
| 1249 | |||
| 1250 | pair_record_import_crt_with_name(pair_record, USERPREF_ROOT_CERTIFICATE_KEY, &root_cert); | ||
| 1251 | pair_record_import_key_with_name(pair_record, USERPREF_ROOT_PRIVATE_KEY_KEY, &root_privkey); | ||
| 1252 | |||
| 1253 | plist_free(pair_record); | ||
| 1254 | |||
| 1255 | ssl_data_t ssl_data_loc = (ssl_data_t)malloc(sizeof(struct ssl_data_private)); | ||
| 1256 | |||
| 1257 | mbedtls_ssl_init(&ssl_data_loc->ctx); | ||
| 1258 | mbedtls_ssl_config_init(&ssl_data_loc->config); | ||
| 1259 | mbedtls_entropy_init(&ssl_data_loc->entropy); | ||
| 1260 | mbedtls_ctr_drbg_init(&ssl_data_loc->ctr_drbg); | ||
| 1261 | |||
| 1262 | int r = mbedtls_ctr_drbg_seed(&ssl_data_loc->ctr_drbg, mbedtls_entropy_func, &ssl_data_loc->entropy, NULL, 0); | ||
| 1263 | if (r != 0) { | ||
| 1264 | debug_info("ERROR: [mbedtls] mbedtls_ctr_drbg_seed failed: %d", r); | ||
| 1265 | return ret; | ||
| 1266 | } | ||
| 1267 | |||
| 1268 | if (mbedtls_ssl_config_defaults(&ssl_data_loc->config, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT) != 0) { | ||
| 1269 | debug_info("ERROR: [mbedtls] Failed to set config defaults"); | ||
| 1270 | return ret; | ||
| 1271 | } | ||
| 1272 | |||
| 1273 | mbedtls_ssl_conf_rng(&ssl_data_loc->config, mbedtls_ctr_drbg_random, &ssl_data_loc->ctr_drbg); | ||
| 1274 | |||
| 1275 | mbedtls_ssl_conf_dbg(&ssl_data_loc->config, _mbedtls_log_cb, NULL); | ||
| 1276 | |||
| 1277 | mbedtls_ssl_conf_verify(&ssl_data_loc->config, cert_verify_cb, NULL); | ||
| 1278 | |||
| 1279 | mbedtls_ssl_setup(&ssl_data_loc->ctx, &ssl_data_loc->config); | ||
| 1280 | |||
| 1281 | mbedtls_ssl_set_bio(&ssl_data_loc->ctx, connection, (mbedtls_ssl_send_t*)&internal_ssl_write, (mbedtls_ssl_recv_t*)&internal_ssl_read, NULL); | ||
| 1282 | |||
| 1283 | mbedtls_x509_crt_init(&ssl_data_loc->certificate); | ||
| 1284 | |||
| 1285 | int crterr = mbedtls_x509_crt_parse(&ssl_data_loc->certificate, root_cert.data, root_cert.size); | ||
| 1286 | if (crterr < 0) { | ||
| 1287 | debug_info("ERROR: [mbedtls] parsing root cert failed: %d", crterr); | ||
| 1288 | return ret; | ||
| 1289 | } | ||
| 1290 | |||
| 1291 | mbedtls_ssl_conf_ca_chain(&ssl_data_loc->config, &ssl_data_loc->certificate, NULL); | ||
| 1292 | |||
| 1293 | mbedtls_pk_init(&ssl_data_loc->root_privkey); | ||
| 1294 | |||
| 1295 | #if MBEDTLS_VERSION_NUMBER >= 0x03000000 | ||
| 1296 | int pkerr = mbedtls_pk_parse_key(&ssl_data_loc->root_privkey, root_privkey.data, root_privkey.size, NULL, 0, &_mbedtls_f_rng, NULL); | ||
| 1297 | #else | ||
| 1298 | int pkerr = mbedtls_pk_parse_key(&ssl_data_loc->root_privkey, root_privkey.data, root_privkey.size, NULL, 0); | ||
| 1299 | #endif | ||
| 1300 | if (pkerr < 0) { | ||
| 1301 | debug_info("ERROR: [mbedtls] parsing private key failed: %d (size=%d)", pkerr, root_privkey.size); | ||
| 1302 | return ret; | ||
| 1303 | } | ||
| 1304 | |||
| 1305 | mbedtls_ssl_conf_own_cert(&ssl_data_loc->config, &ssl_data_loc->certificate, &ssl_data_loc->root_privkey); | ||
| 1306 | |||
| 1307 | int return_me = 0; | ||
| 1308 | do { | ||
| 1309 | return_me = mbedtls_ssl_handshake(&ssl_data_loc->ctx); | ||
| 1310 | } while (return_me == MBEDTLS_ERR_SSL_WANT_READ || return_me == MBEDTLS_ERR_SSL_WANT_WRITE); | ||
| 1311 | |||
| 1312 | if (return_me != 0) { | ||
| 1313 | debug_info("ERROR during SSL handshake: %d", return_me); | ||
| 1314 | internal_ssl_cleanup(ssl_data_loc); | ||
| 1315 | free(ssl_data_loc); | ||
| 1316 | } else { | ||
| 1317 | connection->ssl_data = ssl_data_loc; | ||
| 1318 | ret = IDEVICE_E_SUCCESS; | ||
| 1319 | debug_info("SSL mode enabled, %s, cipher: %s", mbedtls_ssl_get_version(&ssl_data_loc->ctx), mbedtls_ssl_get_ciphersuite(&ssl_data_loc->ctx)); | ||
| 1320 | debug_info("SSL mode enabled"); | ||
| 1321 | } | ||
| 1179 | #endif | 1322 | #endif |
| 1180 | return ret; | 1323 | return ret; |
| 1181 | } | 1324 | } |
| @@ -1197,7 +1340,7 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_disable_bypass_ssl(idevi | |||
| 1197 | // some services require plain text communication after SSL handshake | 1340 | // some services require plain text communication after SSL handshake |
| 1198 | // sending out SSL_shutdown will cause bytes | 1341 | // sending out SSL_shutdown will cause bytes |
| 1199 | if (!sslBypass) { | 1342 | if (!sslBypass) { |
| 1200 | #ifdef HAVE_OPENSSL | 1343 | #if defined(HAVE_OPENSSL) |
| 1201 | if (connection->ssl_data->session) { | 1344 | if (connection->ssl_data->session) { |
| 1202 | /* see: https://www.openssl.org/docs/ssl/SSL_shutdown.html#RETURN_VALUES */ | 1345 | /* see: https://www.openssl.org/docs/ssl/SSL_shutdown.html#RETURN_VALUES */ |
| 1203 | if (SSL_shutdown(connection->ssl_data->session) == 0) { | 1346 | if (SSL_shutdown(connection->ssl_data->session) == 0) { |
| @@ -1210,10 +1353,12 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_disable_bypass_ssl(idevi | |||
| 1210 | } | 1353 | } |
| 1211 | } | 1354 | } |
| 1212 | } | 1355 | } |
| 1213 | #else | 1356 | #elif defined(HAVE_GNUTLS) |
| 1214 | if (connection->ssl_data->session) { | 1357 | if (connection->ssl_data->session) { |
| 1215 | gnutls_bye(connection->ssl_data->session, GNUTLS_SHUT_RDWR); | 1358 | gnutls_bye(connection->ssl_data->session, GNUTLS_SHUT_RDWR); |
| 1216 | } | 1359 | } |
| 1360 | #elif defined(HAVE_MBEDTLS) | ||
| 1361 | mbedtls_ssl_close_notify(&connection->ssl_data->ctx); | ||
| 1217 | #endif | 1362 | #endif |
| 1218 | } | 1363 | } |
| 1219 | 1364 | ||
diff --git a/src/idevice.h b/src/idevice.h index 4e53a7f..7a8f4ce 100644 --- a/src/idevice.h +++ b/src/idevice.h | |||
| @@ -26,11 +26,15 @@ | |||
| 26 | #include <config.h> | 26 | #include <config.h> |
| 27 | #endif | 27 | #endif |
| 28 | 28 | ||
| 29 | #ifdef HAVE_OPENSSL | 29 | #if defined(HAVE_OPENSSL) |
| 30 | #include <openssl/ssl.h> | 30 | #include <openssl/ssl.h> |
| 31 | #else | 31 | #elif defined(HAVE_GNUTLS) |
| 32 | #include <gnutls/gnutls.h> | 32 | #include <gnutls/gnutls.h> |
| 33 | #include <gnutls/x509.h> | 33 | #include <gnutls/x509.h> |
| 34 | #elif defined(HAVE_MBEDTLS) | ||
| 35 | #include <mbedtls/ssl.h> | ||
| 36 | #include <mbedtls/entropy.h> | ||
| 37 | #include <mbedtls/ctr_drbg.h> | ||
| 34 | #endif | 38 | #endif |
| 35 | 39 | ||
| 36 | #ifdef WIN32 | 40 | #ifdef WIN32 |
| @@ -49,16 +53,23 @@ | |||
| 49 | #define DEVICE_VERSION(maj, min, patch) (((maj & 0xFF) << 16) | ((min & 0xFF) << 8) | (patch & 0xFF)) | 53 | #define DEVICE_VERSION(maj, min, patch) (((maj & 0xFF) << 16) | ((min & 0xFF) << 8) | (patch & 0xFF)) |
| 50 | 54 | ||
| 51 | struct ssl_data_private { | 55 | struct ssl_data_private { |
| 52 | #ifdef HAVE_OPENSSL | 56 | #if defined(HAVE_OPENSSL) |
| 53 | SSL *session; | 57 | SSL *session; |
| 54 | SSL_CTX *ctx; | 58 | SSL_CTX *ctx; |
| 55 | #else | 59 | #elif defined(HAVE_GNUTLS) |
| 56 | gnutls_certificate_credentials_t certificate; | 60 | gnutls_certificate_credentials_t certificate; |
| 57 | gnutls_session_t session; | 61 | gnutls_session_t session; |
| 58 | gnutls_x509_privkey_t root_privkey; | 62 | gnutls_x509_privkey_t root_privkey; |
| 59 | gnutls_x509_crt_t root_cert; | 63 | gnutls_x509_crt_t root_cert; |
| 60 | gnutls_x509_privkey_t host_privkey; | 64 | gnutls_x509_privkey_t host_privkey; |
| 61 | gnutls_x509_crt_t host_cert; | 65 | gnutls_x509_crt_t host_cert; |
| 66 | #elif defined(HAVE_MBEDTLS) | ||
| 67 | mbedtls_ssl_context ctx; | ||
| 68 | mbedtls_ssl_config config; | ||
| 69 | mbedtls_entropy_context entropy; | ||
| 70 | mbedtls_ctr_drbg_context ctr_drbg; | ||
| 71 | mbedtls_x509_crt certificate; | ||
| 72 | mbedtls_pk_context root_privkey; | ||
| 62 | #endif | 73 | #endif |
| 63 | }; | 74 | }; |
| 64 | typedef struct ssl_data_private *ssl_data_t; | 75 | typedef struct ssl_data_private *ssl_data_t; |
