From 113eca3e425add6aa5a3d7b0fcad71d005e44a45 Mon Sep 17 00:00:00 2001 From: Michael Luchko Date: Mon, 15 Jul 2019 16:48:36 +0300 Subject: [PATCH 1/2] ios 13 support changes SSL --- src/char_buffer.c | 4 ++ src/ios_webkit_debug_proxy_main.c | 1 + src/socket_manager.c | 91 +++++++++++++++++++++++++++-- src/webinspector.c | 95 +++++++++++++++++++++++++------ src/websocket.c | 1 + 5 files changed, 169 insertions(+), 23 deletions(-) mode change 100644 => 100755 src/char_buffer.c mode change 100644 => 100755 src/ios_webkit_debug_proxy_main.c mode change 100644 => 100755 src/socket_manager.c mode change 100644 => 100755 src/webinspector.c mode change 100644 => 100755 src/websocket.c diff --git a/src/char_buffer.c b/src/char_buffer.c old mode 100644 new mode 100755 index 70ea6eb2..21838c6d --- a/src/char_buffer.c +++ b/src/char_buffer.c @@ -14,6 +14,10 @@ #define MIN_LENGTH 1024 +// place holder .... +void* connectionSSL = NULL; + + cb_t cb_new() { cb_t self = (cb_t)malloc(sizeof(struct cb_struct)); if (self) { diff --git a/src/ios_webkit_debug_proxy_main.c b/src/ios_webkit_debug_proxy_main.c old mode 100644 new mode 100755 index b185d081..858066d4 --- a/src/ios_webkit_debug_proxy_main.c +++ b/src/ios_webkit_debug_proxy_main.c @@ -66,6 +66,7 @@ int main(int argc, char** argv) { signal(SIGINT, on_signal); signal(SIGTERM, on_signal); + #ifdef WIN32 WSADATA wsa_data; int res = WSAStartup(MAKEWORD(2,2), &wsa_data); diff --git a/src/socket_manager.c b/src/socket_manager.c old mode 100644 new mode 100755 index cbf0031d..1fa6c4fb --- a/src/socket_manager.c +++ b/src/socket_manager.c @@ -26,12 +26,20 @@ #include #include #include +#include #endif + +#include +#include +#include + + #include "char_buffer.h" #include "socket_manager.h" #include "hash_table.h" + #if defined(__MACH__) || defined(WIN32) #define SIZEOF_FD_SET sizeof(struct fd_set) #define RECV_FLAGS 0 @@ -40,6 +48,33 @@ #define RECV_FLAGS MSG_DONTWAIT #endif +// SSL based communication types import +#define SSL_ERROR_NONE 0 +#define SSL_ERROR_SSL 1 +#define SSL_ERROR_WANT_READ 2 +#define SSL_ERROR_WANT_WRITE 3 +#define SSL_ERROR_WANT_X509_LOOKUP 4 +#define SSL_ERROR_SYSCALL 5 /* look at error stack/return value/errno */ +#define SSL_ERROR_ZERO_RETURN 6 +#define SSL_ERROR_WANT_CONNECT 7 +#define SSL_ERROR_WANT_ACCEPT 8 + +enum connection_type { + CONNECTION_USBMUXD = 1 +}; + +struct idevice_connection_private { + char *udid; // added in v1.1.6 + enum connection_type type; + void *data; + void *ssl_data; +}; + +extern idevice_connection_t connectionSSL; +#define IS_SSL_FD(fd) if(connectionSSL!=NULL && (fd == (int)(long)connectionSSL->data)) + +// SSL based communication types import + struct sm_private { struct timeval timeout; // fds: @@ -373,8 +408,22 @@ sm_status sm_remove_fd(sm_t self, int fd) { return ret; } -sm_status sm_send(sm_t self, int fd, const char *data, size_t length, - void* value) { +sm_status sm_send(sm_t self, int fd, const char *data, size_t length, void* value) +{ + IS_SSL_FD(fd) + { + uint32_t sent_bytes = 0; + if( idevice_connection_send(connectionSSL, data, length, &sent_bytes) != IDEVICE_E_SUCCESS) + { + // wait 200 msec... + usleep(200 *1000); // SSL_ERROR_WANT_WRITE? - to do expose the SSL error by libimobiledevice hedaer... + if( idevice_connection_send(connectionSSL, data, length, &sent_bytes) != IDEVICE_E_SUCCESS) + return SM_ERROR; // SSL_ERROR_SSL??? + + } + return SM_SUCCESS; + } + sm_private_t my = self->private_state; sm_sendq_t sendq = (sm_sendq_t)ht_get_value(my->fd_to_sendq, HT_KEY(fd)); const char *head = data; @@ -532,12 +581,43 @@ void sm_resend(sm_t self, int fd) { } } -void sm_recv(sm_t self, int fd) { + +void sm_recv_SSL(sm_t self,int fd) +{ + sm_private_t my = self->private_state; + while (1) + { + ssize_t read_bytes = 0; + idevice_error_t error = idevice_connection_receive(connectionSSL, my->tmp_buf, my->tmp_buf_length, (uint32_t*)&read_bytes); + if (error != IDEVICE_E_SUCCESS) + break; + + void *value = ht_get_value(my->fd_to_value, HT_KEY(fd)); + if (read_bytes == 0 || self->on_recv(self, fd, value, my->tmp_buf, read_bytes)) + break; + } + + my->curr_recv_fd = 0; +} + + +void sm_recv(sm_t self, int fd) +{ + + IS_SSL_FD(fd) + { + return sm_recv_SSL(self,fd); + } + sm_private_t my = self->private_state; my->curr_recv_fd = fd; - while (1) { + + + while (1) + { ssize_t read_bytes = recv(fd, my->tmp_buf, my->tmp_buf_length, RECV_FLAGS); - if (read_bytes < 0) { + if (read_bytes < 0) + { #ifdef WIN32 if (WSAGetLastError() != WSAEWOULDBLOCK) { fprintf(stderr, "recv failed with error %d\n", WSAGetLastError()); @@ -558,6 +638,7 @@ void sm_recv(sm_t self, int fd) { } } my->curr_recv_fd = 0; + } int sm_select(sm_t self, int timeout_secs) { diff --git a/src/webinspector.c b/src/webinspector.c old mode 100644 new mode 100755 index 9192010d..beb38cc5 --- a/src/webinspector.c +++ b/src/webinspector.c @@ -26,10 +26,11 @@ #include #include #include +#include #include "char_buffer.h" #include "webinspector.h" - +#include "socket_manager.h" #define WI_DEBUG 1 @@ -47,6 +48,18 @@ struct wi_private { size_t body_length; }; +// iOS 13 -------------------------------------- +int g_vers[3] = {0, 0, 0}; + + struct service_client_private + { + idevice_connection_t connection; + }; + +extern idevice_connection_t connectionSSL; +// iOS 13 -------------------------------------- + + // // CONNECT // @@ -63,20 +76,34 @@ struct idevice_connection_private { void *ssl_data; }; -wi_status idevice_connection_get_fd(idevice_connection_t connection, - int *to_fd) { - if (!connection || !to_fd) { +wi_status idevice_connection_get_fd(idevice_connection_t connection, int *to_fd) +{ + if (!connection || !to_fd) return WI_ERROR; + + idevice_connection_private *c = ((sizeof(*connection) == sizeof(idevice_connection_private)) ? (idevice_connection_private *) connection : NULL); + + bool bInvalidStructure = false; + + if(g_vers[0] >= 13) + { + if (!c || c->type != CONNECTION_USBMUXD || c->data <=0) + bInvalidStructure = true; } - idevice_connection_private *c = ( - (sizeof(*connection) == sizeof(idevice_connection_private)) ? - (idevice_connection_private *) connection : NULL); - if (!c || c->type != CONNECTION_USBMUXD || c->data <= 0 || c->ssl_data) { - perror("Invalid idevice_connection struct. Please verify that " - __FILE__ "'s idevice_connection_private matches your version of" - " libimbiledevice/src/idevice.h"); - return WI_ERROR; + else if (!c || c->type != CONNECTION_USBMUXD || c->data <= 0 || c->ssl_data) + { + bInvalidStructure = true; + } + + if(bInvalidStructure) + { + perror("Invalid idevice_connection struct. Please verify that " + __FILE__ "'s idevice_connection_private matches your version of" + " libimbiledevice/src/idevice.h"); + return WI_ERROR; } + + int fd = (int)(long)c->data; struct stat fd_stat; if (fstat(fd, &fd_stat) < 0 || !S_ISSOCK(fd_stat.st_mode)) { @@ -88,6 +115,20 @@ wi_status idevice_connection_get_fd(idevice_connection_t connection, } #endif +// ------- MICKEL +enum connection_type { + CONNECTION_USBMUXD = 1 +}; +struct idevice_connection_private { + char *udid; // added in v1.1.6 + enum connection_type type; + void *data; + void *ssl_data; +}; +// ------- MICKEL + + + int wi_connect(const char *device_id, char **to_device_id, char **to_device_name, int *to_device_os_version, int recv_timeout) { int ret = -1; @@ -128,14 +169,14 @@ int wi_connect(const char *device_id, char **to_device_id, } if (to_device_os_version && !lockdownd_get_value(client, NULL, "ProductVersion", &node)) { - int vers[3] = {0, 0, 0}; + char *s_version = NULL; plist_get_string_val(node, &s_version); if (s_version && sscanf(s_version, "%d.%d.%d", - &vers[0], &vers[1], &vers[2]) >= 2) { - *to_device_os_version = ((vers[0] & 0xFF) << 16) | - ((vers[1] & 0xFF) << 8) | - (vers[2] & 0xFF); + &g_vers[0], &g_vers[1], &g_vers[2]) >= 2) { + *to_device_os_version = ((g_vers[0] & 0xFF) << 16) | + ((g_vers[1] & 0xFF) << 8) | + (g_vers[2] & 0xFF); } else { *to_device_os_version = 0; } @@ -156,6 +197,24 @@ int wi_connect(const char *device_id, char **to_device_id, goto leave_cleanup; } + + // iOS 13.x --------------------------------------------------------------------- + if(g_vers[0]>=13) // the wi connection is ssl based started iOS 13.0 and higher ... + { + service_client_t client_srv = (service_client_t)malloc(sizeof(struct service_client_private)); + + client_srv->connection = connection; + + /* enable SSL if requested */ + if (service->ssl_enabled == 1) + { + service_enable_ssl(client_srv); + connectionSSL = client_srv->connection; + } + } + + // iOS 13.x --------------------------------------------------------------------- + if (client) { // not needed anymore lockdownd_client_free(client); @@ -208,7 +267,7 @@ int wi_connect(const char *device_id, char **to_device_id, #endif // don't call usbmuxd_disconnect(fd)! //idevice_disconnect(connection); - free(connection); + //free(connection); // connectionSSL reuses - keep it... lockdownd_client_free(client); idevice_free(phone); return ret; diff --git a/src/websocket.c b/src/websocket.c old mode 100644 new mode 100755 index 3437deff..8740a4bd --- a/src/websocket.c +++ b/src/websocket.c @@ -32,6 +32,7 @@ typedef int8_t ws_state; #define STATE_CLOSED 7 + struct ws_private { ws_state state; From 56624f667cece73abeb9bdc0e651f57178a548e7 Mon Sep 17 00:00:00 2001 From: Michael Luchko Date: Mon, 22 Jul 2019 15:00:53 +0300 Subject: [PATCH 2/2] clean up code cleaning --- .../ios-webkit-debug-proxy/socket_manager.h | 33 +++++++- src/char_buffer.c | 4 +- src/socket_manager.c | 79 ++++--------------- src/webinspector.c | 79 +++---------------- 4 files changed, 62 insertions(+), 133 deletions(-) mode change 100644 => 100755 include/ios-webkit-debug-proxy/socket_manager.h diff --git a/include/ios-webkit-debug-proxy/socket_manager.h b/include/ios-webkit-debug-proxy/socket_manager.h old mode 100644 new mode 100755 index 014c5268..02625554 --- a/include/ios-webkit-debug-proxy/socket_manager.h +++ b/include/ios-webkit-debug-proxy/socket_manager.h @@ -15,17 +15,28 @@ extern "C" { #include #include +#include +#include + // Bind a server port, return the file descriptor (or -1 for error). int sm_listen(int port); // Connect to a server, return the file descriptor (or -1 for error). int sm_connect(const char *socket_addr); - - + typedef uint8_t sm_status; #define SM_ERROR 1 #define SM_SUCCESS 0 +#define SSL_ERROR_NONE 0 +#define SSL_ERROR_SSL 1 +#define SSL_ERROR_WANT_READ 2 +#define SSL_ERROR_WANT_WRITE 3 +#define SSL_ERROR_WANT_X509_LOOKUP 4 +#define SSL_ERROR_SYSCALL 5 /* look at error stack/return value/errno */ +#define SSL_ERROR_ZERO_RETURN 6 +#define SSL_ERROR_WANT_CONNECT 7 +#define SSL_ERROR_WANT_ACCEPT 8 struct sm_private; typedef struct sm_private *sm_private_t; @@ -76,6 +87,24 @@ struct sm_struct { sm_private_t private_state; }; + +// based on libimobiledevice/src/idevice.h +struct service_client_private +{ + idevice_connection_t connection; +}; +enum connection_type { + CONNECTION_USBMUXD = 1 +}; +struct idevice_connection_private { + char *udid; // added in v1.1.6 + enum connection_type type; + void *data; + void *ssl_data; +}; + + + #ifdef __cplusplus } #endif diff --git a/src/char_buffer.c b/src/char_buffer.c index 21838c6d..f313e8f8 100755 --- a/src/char_buffer.c +++ b/src/char_buffer.c @@ -12,11 +12,9 @@ #include "char_buffer.h" -#define MIN_LENGTH 1024 - -// place holder .... void* connectionSSL = NULL; +#define MIN_LENGTH 1024 cb_t cb_new() { cb_t self = (cb_t)malloc(sizeof(struct cb_struct)); diff --git a/src/socket_manager.c b/src/socket_manager.c index 1fa6c4fb..b8a21cba 100755 --- a/src/socket_manager.c +++ b/src/socket_manager.c @@ -27,13 +27,8 @@ #include #include #include -#endif - - -#include -#include #include - +#endif #include "char_buffer.h" #include "socket_manager.h" @@ -48,33 +43,9 @@ #define RECV_FLAGS MSG_DONTWAIT #endif -// SSL based communication types import -#define SSL_ERROR_NONE 0 -#define SSL_ERROR_SSL 1 -#define SSL_ERROR_WANT_READ 2 -#define SSL_ERROR_WANT_WRITE 3 -#define SSL_ERROR_WANT_X509_LOOKUP 4 -#define SSL_ERROR_SYSCALL 5 /* look at error stack/return value/errno */ -#define SSL_ERROR_ZERO_RETURN 6 -#define SSL_ERROR_WANT_CONNECT 7 -#define SSL_ERROR_WANT_ACCEPT 8 - -enum connection_type { - CONNECTION_USBMUXD = 1 -}; - -struct idevice_connection_private { - char *udid; // added in v1.1.6 - enum connection_type type; - void *data; - void *ssl_data; -}; - extern idevice_connection_t connectionSSL; #define IS_SSL_FD(fd) if(connectionSSL!=NULL && (fd == (int)(long)connectionSSL->data)) -// SSL based communication types import - struct sm_private { struct timeval timeout; // fds: @@ -580,42 +551,26 @@ void sm_resend(sm_t self, int fd) { sendq = nextq; } } - - -void sm_recv_SSL(sm_t self,int fd) -{ - sm_private_t my = self->private_state; - while (1) - { - ssize_t read_bytes = 0; - idevice_error_t error = idevice_connection_receive(connectionSSL, my->tmp_buf, my->tmp_buf_length, (uint32_t*)&read_bytes); - if (error != IDEVICE_E_SUCCESS) - break; - - void *value = ht_get_value(my->fd_to_value, HT_KEY(fd)); - if (read_bytes == 0 || self->on_recv(self, fd, value, my->tmp_buf, read_bytes)) - break; - } - - my->curr_recv_fd = 0; -} - void sm_recv(sm_t self, int fd) { - - IS_SSL_FD(fd) - { - return sm_recv_SSL(self,fd); + sm_private_t my = self->private_state; + my->curr_recv_fd = fd; + + while (1) { + ssize_t read_bytes = 0; + IS_SSL_FD(fd) + { + idevice_error_t error = idevice_connection_receive(connectionSSL, + my->tmp_buf, + my->tmp_buf_length, + (uint32_t*)&read_bytes); + if (error != IDEVICE_E_SUCCESS) + break; // SSL_ERROR_WANT_READ ? + } + else { + read_bytes = recv(fd, my->tmp_buf, my->tmp_buf_length, RECV_FLAGS); } - - sm_private_t my = self->private_state; - my->curr_recv_fd = fd; - - - while (1) - { - ssize_t read_bytes = recv(fd, my->tmp_buf, my->tmp_buf_length, RECV_FLAGS); if (read_bytes < 0) { #ifdef WIN32 diff --git a/src/webinspector.c b/src/webinspector.c index beb38cc5..d27898d2 100755 --- a/src/webinspector.c +++ b/src/webinspector.c @@ -40,6 +40,8 @@ // some arbitrarly limit, to catch bad packets #define MAX_BODY_LENGTH 1<<26 +extern idevice_connection_t connectionSSL; + struct wi_private { bool partials_supported; cb_t in; @@ -48,33 +50,11 @@ struct wi_private { size_t body_length; }; -// iOS 13 -------------------------------------- -int g_vers[3] = {0, 0, 0}; - - struct service_client_private - { - idevice_connection_t connection; - }; - -extern idevice_connection_t connectionSSL; -// iOS 13 -------------------------------------- - - // // CONNECT // #ifndef HAVE_IDEVICE_CONNECTION_GET_FD -// based on libimobiledevice/src/idevice.h -enum connection_type { - CONNECTION_USBMUXD = 1 -}; -struct idevice_connection_private { - char *udid; // added in v1.1.6 - enum connection_type type; - void *data; - void *ssl_data; -}; wi_status idevice_connection_get_fd(idevice_connection_t connection, int *to_fd) { @@ -83,19 +63,7 @@ wi_status idevice_connection_get_fd(idevice_connection_t connection, int *to_fd) idevice_connection_private *c = ((sizeof(*connection) == sizeof(idevice_connection_private)) ? (idevice_connection_private *) connection : NULL); - bool bInvalidStructure = false; - - if(g_vers[0] >= 13) - { - if (!c || c->type != CONNECTION_USBMUXD || c->data <=0) - bInvalidStructure = true; - } - else if (!c || c->type != CONNECTION_USBMUXD || c->data <= 0 || c->ssl_data) - { - bInvalidStructure = true; - } - - if(bInvalidStructure) + if (!c || c->type != CONNECTION_USBMUXD || c->data <= 0 || c->ssl_data) { perror("Invalid idevice_connection struct. Please verify that " __FILE__ "'s idevice_connection_private matches your version of" @@ -103,7 +71,6 @@ wi_status idevice_connection_get_fd(idevice_connection_t connection, int *to_fd) return WI_ERROR; } - int fd = (int)(long)c->data; struct stat fd_stat; if (fstat(fd, &fd_stat) < 0 || !S_ISSOCK(fd_stat.st_mode)) { @@ -115,20 +82,6 @@ wi_status idevice_connection_get_fd(idevice_connection_t connection, int *to_fd) } #endif -// ------- MICKEL -enum connection_type { - CONNECTION_USBMUXD = 1 -}; -struct idevice_connection_private { - char *udid; // added in v1.1.6 - enum connection_type type; - void *data; - void *ssl_data; -}; -// ------- MICKEL - - - int wi_connect(const char *device_id, char **to_device_id, char **to_device_name, int *to_device_os_version, int recv_timeout) { int ret = -1; @@ -139,6 +92,7 @@ int wi_connect(const char *device_id, char **to_device_id, lockdownd_client_t client = NULL; idevice_connection_t connection = NULL; int fd = -1; + int vers[3] = {0, 0, 0}; // get phone if (idevice_new(&phone, device_id)) { @@ -173,10 +127,10 @@ int wi_connect(const char *device_id, char **to_device_id, char *s_version = NULL; plist_get_string_val(node, &s_version); if (s_version && sscanf(s_version, "%d.%d.%d", - &g_vers[0], &g_vers[1], &g_vers[2]) >= 2) { - *to_device_os_version = ((g_vers[0] & 0xFF) << 16) | - ((g_vers[1] & 0xFF) << 8) | - (g_vers[2] & 0xFF); + &vers[0], &vers[1], &vers[2]) >= 2) { + *to_device_os_version = ((vers[0] & 0xFF) << 16) | + ((vers[1] & 0xFF) << 8) | + (vers[2] & 0xFF); } else { *to_device_os_version = 0; } @@ -197,24 +151,17 @@ int wi_connect(const char *device_id, char **to_device_id, goto leave_cleanup; } + if(vers[0]>=13) { + service_client_t client_srv = (service_client_t)malloc(sizeof(struct service_client_private)); + client_srv->connection = connection; - // iOS 13.x --------------------------------------------------------------------- - if(g_vers[0]>=13) // the wi connection is ssl based started iOS 13.0 and higher ... - { - service_client_t client_srv = (service_client_t)malloc(sizeof(struct service_client_private)); - - client_srv->connection = connection; - - /* enable SSL if requested */ - if (service->ssl_enabled == 1) - { + /* enable SSL if requested */ + if (service->ssl_enabled == 1){ service_enable_ssl(client_srv); connectionSSL = client_srv->connection; } } - // iOS 13.x --------------------------------------------------------------------- - if (client) { // not needed anymore lockdownd_client_free(client);