Skip to content

Commit

Permalink
Updated to use libplist 2.5.0 API
Browse files Browse the repository at this point in the history
  • Loading branch information
nikias committed May 5, 2024
1 parent 0548d9f commit e6d8c0b
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 432 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fi
LIBIRECOVERY_VERSION=1.2.0
LIBIMOBILEDEVICE_VERSION=1.3.0
LIBUSBMUXD_VERSION=2.0.2
LIBPLIST_VERSION=2.3.0
LIBPLIST_VERSION=2.5.0
LIMD_GLUE_VERSION=1.2.0
LIBZIP_VERSION=1.0
LIBCURL_VERSION=7.0
Expand Down
2 changes: 1 addition & 1 deletion src/ace3.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ int ace3_create_binary(const unsigned char* uarp_fw, size_t uarp_size, uint64_t

plist_t p_im4m = plist_dict_get_item(tss, "USBPortController1,Ticket");
uint64_t im4m_size = 0;
const char* im4m = plist_get_data_ptr(p_im4m, &im4m_size);
const uint8_t* im4m = plist_get_data_ptr(p_im4m, &im4m_size);

struct uarp_header* uarp_hdr = (struct uarp_header*)uarp_fw;
uint32_t uarp_hdr_size = be32toh(uarp_hdr->header_size);
Expand Down
139 changes: 0 additions & 139 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,145 +558,6 @@ void get_user_input(char *buf, int maxlen, int secure)
buf[len] = 0;
}

uint64_t _plist_dict_get_uint(plist_t dict, const char *key)
{
uint64_t uintval = 0;
char *strval = NULL;
uint64_t strsz = 0;
plist_t node = plist_dict_get_item(dict, key);
if (!node) {
return uintval;
}
switch (plist_get_node_type(node)) {
case PLIST_UINT:
plist_get_uint_val(node, &uintval);
break;
case PLIST_STRING:
plist_get_string_val(node, &strval);
if (strval) {
uintval = strtoull(strval, NULL, 0);
free(strval);
}
break;
case PLIST_DATA:
plist_get_data_val(node, &strval, &strsz);
if (strval) {
if (strsz == 8) {
uintval = le64toh(*(uint64_t*)strval);
} else if (strsz == 4) {
uintval = le32toh(*(uint32_t*)strval);
} else if (strsz == 2) {
uintval = le16toh(*(uint16_t*)strval);
} else if (strsz == 1) {
uintval = strval[0];
} else {
error("%s: ERROR: invalid size %" PRIu64 " for data to integer conversion\n", __func__, strsz);
}
free(strval);
}
break;
default:
break;
}
return uintval;
}

uint8_t _plist_dict_get_bool(plist_t dict, const char *key)
{
uint8_t bval = 0;
uint64_t uintval = 0;
char *strval = NULL;
uint64_t strsz = 0;
plist_t node = plist_dict_get_item(dict, key);
if (!node) {
return 0;
}
switch (plist_get_node_type(node)) {
case PLIST_BOOLEAN:
plist_get_bool_val(node, &bval);
break;
case PLIST_UINT:
plist_get_uint_val(node, &uintval);
bval = (uint8_t)uintval;
break;
case PLIST_STRING:
plist_get_string_val(node, &strval);
if (strval) {
if (strcmp(strval, "true")) {
bval = 1;
} else if (strcmp(strval, "false")) {
bval = 0;
}
free(strval);
}
break;
case PLIST_DATA:
plist_get_data_val(node, &strval, &strsz);
if (strval) {
if (strsz == 1) {
bval = strval[0];
} else {
error("%s: ERROR: invalid size %" PRIu64 " for data to boolean conversion\n", __func__, strsz);
}
free(strval);
}
break;
default:
break;
}
return bval;
}

int _plist_dict_copy_uint(plist_t target_dict, plist_t source_dict, const char *key, const char *alt_source_key)
{
if (plist_dict_get_item(source_dict, (alt_source_key) ? alt_source_key : key) == NULL) {
return -1;
}
uint64_t u64val = _plist_dict_get_uint(source_dict, (alt_source_key) ? alt_source_key : key);
plist_dict_set_item(target_dict, key, plist_new_uint(u64val));
return 0;
}

int _plist_dict_copy_bool(plist_t target_dict, plist_t source_dict, const char *key, const char *alt_source_key)
{
if (plist_dict_get_item(source_dict, (alt_source_key) ? alt_source_key : key) == NULL) {
return -1;
}
uint64_t bval = _plist_dict_get_bool(source_dict, (alt_source_key) ? alt_source_key : key);
plist_dict_set_item(target_dict, key, plist_new_bool(bval));
return 0;
}

int _plist_dict_copy_data(plist_t target_dict, plist_t source_dict, const char *key, const char *alt_source_key)
{
plist_t node = plist_dict_get_item(source_dict, (alt_source_key) ? alt_source_key : key);
if (!PLIST_IS_DATA(node)) {
return -1;
}
plist_dict_set_item(target_dict, key, plist_copy(node));
return 0;
}

int _plist_dict_copy_string(plist_t target_dict, plist_t source_dict, const char *key, const char *alt_source_key)
{
plist_t node = plist_dict_get_item(source_dict, (alt_source_key) ? alt_source_key : key);
if (!PLIST_IS_STRING(node)) {
return -1;
}
plist_dict_set_item(target_dict, key, plist_copy(node));
return 0;
}

int _plist_dict_copy_item(plist_t target_dict, plist_t source_dict, const char *key, const char *alt_source_key)
{
plist_t node = plist_dict_get_item(source_dict, (alt_source_key) ? alt_source_key : key);
if (!node) {
return -1;
}
plist_dict_set_item(target_dict, key, plist_copy(node));
return 0;
}

const char* path_get_basename(const char* path)
{
#ifdef WIN32
Expand Down
8 changes: 0 additions & 8 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,6 @@ char* realpath(const char *filename, char *resolved_name);

void get_user_input(char *buf, int maxlen, int secure);

uint8_t _plist_dict_get_bool(plist_t dict, const char *key);
uint64_t _plist_dict_get_uint(plist_t dict, const char *key);
int _plist_dict_copy_uint(plist_t target_dict, plist_t source_dict, const char *key, const char *alt_source_key);
int _plist_dict_copy_bool(plist_t target_dict, plist_t source_dict, const char *key, const char *alt_source_key);
int _plist_dict_copy_data(plist_t target_dict, plist_t source_dict, const char *key, const char *alt_source_key);
int _plist_dict_copy_string(plist_t target_dict, plist_t source_dict, const char *key, const char *alt_source_key);
int _plist_dict_copy_item(plist_t target_dict, plist_t source_dict, const char *key, const char *alt_source_key);

const char* path_get_basename(const char* path);

#ifdef __cplusplus
Expand Down
56 changes: 28 additions & 28 deletions src/idevicerestore.c
Original file line number Diff line number Diff line change
Expand Up @@ -724,12 +724,12 @@ int idevicerestore_start(struct idevicerestore_client_t* client)
return -1;
}

unsigned int b_pdfu_cpid = (unsigned int)_plist_dict_get_uint(build_identity, "USBPortController1,ChipID");
unsigned int b_pdfu_cpid = (unsigned int)plist_dict_get_uint(build_identity, "USBPortController1,ChipID");
if (b_pdfu_cpid != pdfu_cpid) {
error("ERROR: cpid 0x%02x doesn't match USBPortController1,ChipID in build identity (0x%02x)\n", pdfu_cpid, b_pdfu_cpid);
return -1;
}
unsigned int b_pdfu_bdid = (unsigned int)_plist_dict_get_uint(build_identity, "USBPortController1,BoardID");
unsigned int b_pdfu_bdid = (unsigned int)plist_dict_get_uint(build_identity, "USBPortController1,BoardID");
if (b_pdfu_bdid != pdfu_bdid) {
error("ERROR: bdid 0x%x doesn't match USBPortController1,BoardID in build identity (0x%x)\n", pdfu_bdid, b_pdfu_bdid);
return -1;
Expand All @@ -738,9 +738,9 @@ int idevicerestore_start(struct idevicerestore_client_t* client)
plist_t parameters = plist_new_dict();
plist_dict_set_item(parameters, "@USBPortController1,Ticket", plist_new_bool(1));
plist_dict_set_item(parameters, "USBPortController1,ECID", plist_new_int(client->ecid));
_plist_dict_copy_item(parameters, build_identity, "USBPortController1,BoardID", NULL);
_plist_dict_copy_item(parameters, build_identity, "USBPortController1,ChipID", NULL);
_plist_dict_copy_item(parameters, build_identity, "USBPortController1,SecurityDomain", NULL);
plist_dict_copy_item(parameters, build_identity, "USBPortController1,BoardID", NULL);
plist_dict_copy_item(parameters, build_identity, "USBPortController1,ChipID", NULL);
plist_dict_copy_item(parameters, build_identity, "USBPortController1,SecurityDomain", NULL);
plist_dict_set_item(parameters, "USBPortController1,SecurityMode", plist_new_bool(1));
plist_dict_set_item(parameters, "USBPortController1,ProductionMode", plist_new_bool(1));
plist_t usbf = plist_access_path(build_identity, 2, "Manifest", "USBPortController1,USBFirmware");
Expand Down Expand Up @@ -771,7 +771,7 @@ int idevicerestore_start(struct idevicerestore_client_t* client)
usbf = plist_copy(usbf);
plist_dict_remove_item(usbf, "Info");
plist_dict_set_item(parameters, "USBPortController1,USBFirmware", usbf);
plist_dict_set_item(parameters, "USBPortController1,Nonce", plist_new_data((const char*)pdfu_nonce, pdfu_nsize));
plist_dict_set_item(parameters, "USBPortController1,Nonce", plist_new_data(pdfu_nonce, pdfu_nsize));

plist_t request = tss_request_new(NULL);
if (request == NULL) {
Expand Down Expand Up @@ -2263,14 +2263,14 @@ int get_tss_response(struct idevicerestore_client_t* client, plist_t build_ident
plist_t parameters = plist_new_dict();
plist_dict_set_item(parameters, "ApECID", plist_new_uint(client->ecid));
if (client->nonce) {
plist_dict_set_item(parameters, "ApNonce", plist_new_data((const char*)client->nonce, client->nonce_size));
plist_dict_set_item(parameters, "ApNonce", plist_new_data(client->nonce, client->nonce_size));
}
unsigned char* sep_nonce = NULL;
unsigned int sep_nonce_size = 0;
get_sep_nonce(client, &sep_nonce, &sep_nonce_size);

if (sep_nonce) {
plist_dict_set_item(parameters, "ApSepNonce", plist_new_data((const char*)sep_nonce, sep_nonce_size));
plist_dict_set_item(parameters, "ApSepNonce", plist_new_data(sep_nonce, sep_nonce_size));
free(sep_nonce);
}

Expand Down Expand Up @@ -2331,20 +2331,20 @@ int get_tss_response(struct idevicerestore_client_t* client, plist_t build_ident
plist_t pinfo = NULL;
normal_get_preflight_info(client, &pinfo);
if (pinfo) {
_plist_dict_copy_data(parameters, pinfo, "BbNonce", "Nonce");
_plist_dict_copy_uint(parameters, pinfo, "BbChipID", "ChipID");
_plist_dict_copy_uint(parameters, pinfo, "BbGoldCertId", "CertID");
_plist_dict_copy_data(parameters, pinfo, "BbSNUM", "ChipSerialNo");
plist_dict_copy_data(parameters, pinfo, "BbNonce", "Nonce");
plist_dict_copy_uint(parameters, pinfo, "BbChipID", "ChipID");
plist_dict_copy_uint(parameters, pinfo, "BbGoldCertId", "CertID");
plist_dict_copy_data(parameters, pinfo, "BbSNUM", "ChipSerialNo");

/* add baseband parameters */
tss_request_add_baseband_tags(request, parameters, NULL);

_plist_dict_copy_uint(parameters, pinfo, "eUICC,ChipID", "EUICCChipID");
if (_plist_dict_get_uint(parameters, "eUICC,ChipID") >= 5) {
_plist_dict_copy_data(parameters, pinfo, "eUICC,EID", "EUICCCSN");
_plist_dict_copy_data(parameters, pinfo, "eUICC,RootKeyIdentifier", "EUICCCertIdentifier");
_plist_dict_copy_data(parameters, pinfo, "EUICCGoldNonce", NULL);
_plist_dict_copy_data(parameters, pinfo, "EUICCMainNonce", NULL);
plist_dict_copy_uint(parameters, pinfo, "eUICC,ChipID", "EUICCChipID");
if (plist_dict_get_uint(parameters, "eUICC,ChipID") >= 5) {
plist_dict_copy_data(parameters, pinfo, "eUICC,EID", "EUICCCSN");
plist_dict_copy_data(parameters, pinfo, "eUICC,RootKeyIdentifier", "EUICCCertIdentifier");
plist_dict_copy_data(parameters, pinfo, "EUICCGoldNonce", NULL);
plist_dict_copy_data(parameters, pinfo, "EUICCMainNonce", NULL);

/* add vinyl parameters */
tss_request_add_vinyl_tags(request, parameters, NULL);
Expand Down Expand Up @@ -2387,15 +2387,15 @@ int get_recoveryos_root_ticket_tss_response(struct idevicerestore_client_t* clie

/* ApNonce */
if (client->nonce) {
plist_dict_set_item(parameters, "ApNonce", plist_new_data((const char*)client->nonce, client->nonce_size));
plist_dict_set_item(parameters, "ApNonce", plist_new_data(client->nonce, client->nonce_size));
}
unsigned char* sep_nonce = NULL;
unsigned int sep_nonce_size = 0;
get_sep_nonce(client, &sep_nonce, &sep_nonce_size);

/* ApSepNonce */
if (sep_nonce) {
plist_dict_set_item(parameters, "ApSepNonce", plist_new_data((const char*)sep_nonce, sep_nonce_size));
plist_dict_set_item(parameters, "ApSepNonce", plist_new_data(sep_nonce, sep_nonce_size));
free(sep_nonce);
}

Expand Down Expand Up @@ -2496,12 +2496,12 @@ int get_recovery_os_local_policy_tss_response(
uint8_t digest[SHA384_DIGEST_LENGTH];
SHA384(lpol_file, lpol_file_length, digest);
plist_t lpol = plist_new_dict();
plist_dict_set_item(lpol, "Digest", plist_new_data((char*)digest, SHA384_DIGEST_LENGTH));
plist_dict_set_item(lpol, "Digest", plist_new_data(digest, SHA384_DIGEST_LENGTH));
plist_dict_set_item(lpol, "Trusted", plist_new_bool(1));
plist_dict_set_item(parameters, "Ap,LocalPolicy", lpol);

_plist_dict_copy_data(parameters, args, "Ap,NextStageIM4MHash", NULL);
_plist_dict_copy_data(parameters, args, "Ap,RecoveryOSPolicyNonceHash", NULL);
plist_dict_copy_data(parameters, args, "Ap,NextStageIM4MHash", NULL);
plist_dict_copy_data(parameters, args, "Ap,RecoveryOSPolicyNonceHash", NULL);

plist_t vol_uuid_node = plist_dict_get_item(args, "Ap,VolumeUUID");
char* vol_uuid_str = NULL;
Expand All @@ -2518,7 +2518,7 @@ int get_recovery_os_local_policy_tss_response(
for (i = 0; i < 16; i++) {
vol_uuid[i] = (unsigned char)vuuid[i];
}
plist_dict_set_item(parameters, "Ap,VolumeUUID", plist_new_data((char*)vol_uuid, 16));
plist_dict_set_item(parameters, "Ap,VolumeUUID", plist_new_data(vol_uuid, 16));

/* create basic request */
request = tss_request_new(NULL);
Expand Down Expand Up @@ -2566,14 +2566,14 @@ int get_local_policy_tss_response(struct idevicerestore_client_t* client, plist_
plist_dict_set_item(parameters, "ApECID", plist_new_uint(client->ecid));
plist_dict_set_item(parameters, "Ap,LocalBoot", plist_new_bool(0));
if (client->nonce) {
plist_dict_set_item(parameters, "ApNonce", plist_new_data((const char*)client->nonce, client->nonce_size));
plist_dict_set_item(parameters, "ApNonce", plist_new_data(client->nonce, client->nonce_size));
}
unsigned char* sep_nonce = NULL;
unsigned int sep_nonce_size = 0;
get_sep_nonce(client, &sep_nonce, &sep_nonce_size);

if (sep_nonce) {
plist_dict_set_item(parameters, "ApSepNonce", plist_new_data((const char*)sep_nonce, sep_nonce_size));
plist_dict_set_item(parameters, "ApSepNonce", plist_new_data(sep_nonce, sep_nonce_size));
free(sep_nonce);
}

Expand All @@ -2591,7 +2591,7 @@ int get_local_policy_tss_response(struct idevicerestore_client_t* client, plist_
uint8_t digest[SHA384_DIGEST_LENGTH];
SHA384(lpol_file, lpol_file_length, digest);
plist_t lpol = plist_new_dict();
plist_dict_set_item(lpol, "Digest", plist_new_data((char*)digest, SHA384_DIGEST_LENGTH));
plist_dict_set_item(lpol, "Digest", plist_new_data(digest, SHA384_DIGEST_LENGTH));
plist_dict_set_item(lpol, "Trusted", plist_new_bool(1));
plist_dict_set_item(parameters, "Ap,LocalPolicy", lpol);

Expand All @@ -2603,7 +2603,7 @@ int get_local_policy_tss_response(struct idevicerestore_client_t* client, plist_
// Hash it and add it as Ap,NextStageIM4MHash
uint8_t hash[SHA384_DIGEST_LENGTH];
SHA384(ticket, ticket_length, hash);
plist_dict_set_item(parameters, "Ap,NextStageIM4MHash", plist_new_data((char*)hash, SHA384_DIGEST_LENGTH));
plist_dict_set_item(parameters, "Ap,NextStageIM4MHash", plist_new_data(hash, SHA384_DIGEST_LENGTH));

/* create basic request */
request = tss_request_new(NULL);
Expand Down
Loading

0 comments on commit e6d8c0b

Please sign in to comment.