From 9f43793b89b600d519e9e76581c1fe427c08baac Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Tue, 20 Aug 2019 14:23:59 +0200 Subject: Reduce code duplication in normal.c with helper function --- src/normal.c | 145 +++++++++++++---------------------------------------------- src/normal.h | 1 + 2 files changed, 33 insertions(+), 113 deletions(-) diff --git a/src/normal.c b/src/normal.c index 921a157..e09dd28 100644 --- a/src/normal.c +++ b/src/normal.c @@ -297,37 +297,49 @@ int normal_enter_recovery(struct idevicerestore_client_t* client) { return 0; } -static int normal_get_nonce_by_key(struct idevicerestore_client_t* client, const char* key, unsigned char** nonce, int* nonce_size) { +plist_t normal_get_lockdown_value(struct idevicerestore_client_t* client, const char* domain, const char* key) +{ idevice_t device = NULL; - plist_t nonce_node = NULL; + plist_t node = NULL; lockdownd_client_t lockdown = NULL; idevice_error_t device_error = IDEVICE_E_SUCCESS; lockdownd_error_t lockdown_error = LOCKDOWN_E_SUCCESS; device_error = idevice_new(&device, client->udid); if (device_error != IDEVICE_E_SUCCESS) { - return -1; + error("ERROR: Unable to connect to device?!\n"); + return NULL; } lockdown_error = lockdownd_client_new(device, &lockdown, "idevicerestore"); if (lockdown_error != LOCKDOWN_E_SUCCESS) { error("ERROR: Unable to connect to lockdownd\n"); idevice_free(device); - return -1; + return NULL; } - lockdown_error = lockdownd_get_value(lockdown, NULL, key, &nonce_node); + lockdown_error = lockdownd_get_value(lockdown, domain, key, &node); if (lockdown_error != LOCKDOWN_E_SUCCESS) { - error("Unable to get %s from lockdownd\n", key); + debug("ERROR: Unable to get %s-%s from lockdownd\n", domain, key); lockdownd_client_free(lockdown); idevice_free(device); - return -1; + return NULL; } + lockdownd_client_free(lockdown); + idevice_free(device); + lockdown = NULL; + device = NULL; + + return node; +} + +static int normal_get_nonce_by_key(struct idevicerestore_client_t* client, const char* key, unsigned char** nonce, int* nonce_size) +{ + plist_t nonce_node = normal_get_lockdown_value(client, NULL, key); + if (!nonce_node || plist_get_node_type(nonce_node) != PLIST_DATA) { error("Unable to get %s\n", key); - lockdownd_client_free(lockdown); - idevice_free(device); return -1; } @@ -336,52 +348,24 @@ static int normal_get_nonce_by_key(struct idevicerestore_client_t* client, const *nonce_size = (int)n_size; plist_free(nonce_node); - lockdownd_client_free(lockdown); - idevice_free(device); - lockdown = NULL; - device = NULL; - return 0; } -int normal_get_sep_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size) { +int normal_get_sep_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size) +{ return normal_get_nonce_by_key(client, "SEPNonce", nonce, nonce_size); } -int normal_get_ap_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size) { +int normal_get_ap_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size) +{ return normal_get_nonce_by_key(client, "ApNonce", nonce, nonce_size); } int normal_is_image4_supported(struct idevicerestore_client_t* client) { - idevice_t device = NULL; - plist_t node = NULL; - lockdownd_client_t lockdown = NULL; - idevice_error_t device_error = IDEVICE_E_SUCCESS; - lockdownd_error_t lockdown_error = LOCKDOWN_E_SUCCESS; - - device_error = idevice_new(&device, client->udid); - if (device_error != IDEVICE_E_SUCCESS) { - return 0; - } - - lockdown_error = lockdownd_client_new(device, &lockdown, "idevicerestore"); - if (lockdown_error != LOCKDOWN_E_SUCCESS) { - error("ERROR: Unable to connect to lockdownd\n"); - idevice_free(device); - return 0; - } - - lockdown_error = lockdownd_get_value(lockdown, NULL, "Image4Supported", &node); - if (lockdown_error != LOCKDOWN_E_SUCCESS) { - lockdownd_client_free(lockdown); - idevice_free(device); - return 0; - } + plist_t node = normal_get_lockdown_value(client, NULL, "Image4Supported"); if (!node || plist_get_node_type(node) != PLIST_BOOLEAN) { - lockdownd_client_free(lockdown); - idevice_free(device); return 0; } @@ -389,95 +373,30 @@ int normal_is_image4_supported(struct idevicerestore_client_t* client) plist_get_bool_val(node, &bval); plist_free(node); - lockdownd_client_free(lockdown); - idevice_free(device); - lockdown = NULL; - device = NULL; - return bval; } -int normal_get_ecid(struct idevicerestore_client_t* client, uint64_t* ecid) { - idevice_t device = NULL; - plist_t unique_chip_node = NULL; - lockdownd_client_t lockdown = NULL; - idevice_error_t device_error = IDEVICE_E_SUCCESS; - lockdownd_error_t lockdown_error = LOCKDOWN_E_SUCCESS; - - device_error = idevice_new(&device, client->udid); - if (device_error != IDEVICE_E_SUCCESS) { - return -1; - } - - lockdown_error = lockdownd_client_new(device, &lockdown, "idevicerestore"); - if (lockdown_error != LOCKDOWN_E_SUCCESS) { - error("ERROR: Unable to connect to lockdownd\n"); - idevice_free(device); - return -1; - } - - lockdown_error = lockdownd_get_value(lockdown, NULL, "UniqueChipID", &unique_chip_node); - if (lockdown_error != LOCKDOWN_E_SUCCESS) { - error("ERROR: Unable to get UniqueChipID from lockdownd\n"); - lockdownd_client_free(lockdown); - idevice_free(device); - return -1; - } - +int normal_get_ecid(struct idevicerestore_client_t* client, uint64_t* ecid) +{ + plist_t unique_chip_node = normal_get_lockdown_value(client, NULL, "UniqueChipID"); if (!unique_chip_node || plist_get_node_type(unique_chip_node) != PLIST_UINT) { error("ERROR: Unable to get ECID\n"); - lockdownd_client_free(lockdown); - idevice_free(device); return -1; } plist_get_uint_val(unique_chip_node, ecid); plist_free(unique_chip_node); - lockdownd_client_free(lockdown); - idevice_free(device); - lockdown = NULL; - device = NULL; return 0; } -int normal_get_preflight_info(struct idevicerestore_client_t* client, plist_t *preflight_info) { - idevice_t device = NULL; - plist_t node = NULL; - lockdownd_client_t lockdown = NULL; - idevice_error_t device_error = IDEVICE_E_SUCCESS; - lockdownd_error_t lockdown_error = LOCKDOWN_E_SUCCESS; - - device_error = idevice_new(&device, client->udid); - if (device_error != IDEVICE_E_SUCCESS) { - return -1; - } - - lockdown_error = lockdownd_client_new(device, &lockdown, "idevicerestore"); - if (lockdown_error != LOCKDOWN_E_SUCCESS) { - error("ERROR: Unable to connect to lockdownd\n"); - idevice_free(device); - return -1; - } - - lockdown_error = lockdownd_get_value(lockdown, NULL, "FirmwarePreflightInfo", &node); - if (lockdown_error != LOCKDOWN_E_SUCCESS) { - debug("ERROR: Unable to get FirmwarePreflightInfo from lockdownd\n"); - lockdownd_client_free(lockdown); - idevice_free(device); - return -1; - } - +int normal_get_preflight_info(struct idevicerestore_client_t* client, plist_t *preflight_info) +{ + plist_t node = normal_get_lockdown_value(client, NULL, "FirmwarePreflightInfo"); if (!node || plist_get_node_type(node) != PLIST_DICT) { error("ERROR: Unable to get FirmwarePreflightInfo\n"); - lockdownd_client_free(lockdown); - idevice_free(device); return -1; } *preflight_info = node; - lockdownd_client_free(lockdown); - idevice_free(device); - lockdown = NULL; - device = NULL; return 0; } diff --git a/src/normal.h b/src/normal.h index ddd8a58..5de6127 100644 --- a/src/normal.h +++ b/src/normal.h @@ -51,6 +51,7 @@ int normal_is_image4_supported(struct idevicerestore_client_t* client); int normal_get_ap_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size); int normal_get_sep_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size); int normal_get_preflight_info(struct idevicerestore_client_t* client, plist_t *preflight_info); +plist_t normal_get_lockdown_value(struct idevicerestore_client_t* client, const char* domain, const char* key); #ifdef __cplusplus } -- cgit v1.1-32-gdbae