tr4mpass/tests/mocks/mock_irecv_ctl.c
tr4m0ryp 30807c8ef6 feat: real Path A implementation, cert extraction, mock test harness
Converts the Path A pipeline from stubs to a working implementation for
A5-A11 devices, adds real certificate extraction via lockdownd and ECDSA
P-256 signing, and introduces a comprehensive mock-based test suite.

Activation
- cert_extract: lockdownd-backed DeviceCertificate,
  AccountTokenCertificate, UniqueDeviceCertificate with base64 decode
  and CertificateChain array fallback.
- signer: OpenSSL EVP ECDSA P-256 sign/verify with DER-encoded
  signatures plus a test keypair generator.
- record.c: 3 of 6 cert fields now populate from live device data.
  AccountTokenSignature signs when TR4MPASS_SIGNING_KEY is set.
  FairPlayKeyData, RKCData, SepNonce remain documented stubs
  (SEP-blocked per 26.3-vulnerability.md).

Path A bypass
- path_a_ramdisk: iBSS/iBEC/devicetree/trustcache/ramdisk upload via
  libirecovery, reconnect handling between DFU and recovery stages,
  iBoot go/bootx commands, SSH readiness poll.
- path_a_ssh: libssh2 + SCP. Mounts rootfs rw (apfs then hfs), runs
  AMFI patcher, backs up and replaces /usr/libexec/mobileactivationd
  with a user-supplied patched binary, verifies mode.

Build, CLI, config
- Makefile: add libssh2 dependency, split test targets into
  hw-independent (make test) and full mock (make test-mocks).
- env_config + cli_flags: 12 new --flags for ramdisk, SSH, signing
  paths; flags map to TR4MPASS_* env vars. --verbose prints summary.
- start.sh slimmed; helpers extracted to start-helpers.sh.

Test suite (20 to 248 tests)
- Mock framework under tests/mocks/ intercepts libimobiledevice,
  libirecovery, libusb, libssh2, libcurl, plus mobileactivation and
  afc. Staging API in mock_control.h.
- Unit tests per module: cert_extract, signer, ramdisk, SSH jailbreak,
  env_config.
- Integration tests in tests/integration/: 6 Path A E2E scenarios
  (happy path, exploit fail, reconnect timeout, SSH auth fail, SCP
  fail, missing env) and 3 Path B scenarios (identity manipulation
  works, nonce barrier rejection is handled cleanly, signal
  detection). Path B tests verify the tool reports the SEP barrier
  failure rather than misclaiming success.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-18 17:43:30 +02:00

155 lines
5 KiB
C

/*
* mock_irecv_ctl.c -- Staging + reset for the libirecovery mock.
*/
#include "mock_irecv_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char g_irecv_cmd_expect[256];
int g_irecv_cmd_expect_active;
size_t *g_irecv_file_upload_bytes_out;
int g_irecv_file_upload_active;
mock_irecv_setenv_result_t g_irecv_setenv_results[MOCK_SETENV_SLOTS];
int g_irecv_setenv_default_error;
int g_irecv_open_error;
irecv_open_error_slot_t g_irecv_open_error_slots[IRECV_OPEN_ERROR_SLOTS];
irecv_send_file_stage_t g_irecv_send_file_stages[IRECV_SEND_FILE_STAGES];
uint32_t g_irecv_info_pid = 0x1281;
uint64_t g_irecv_info_ecid;
char g_irecv_info_serial[128] =
"CPID:8020 CPRV:11 ECID:0012345678ABCDEF";
void mock_irecv_reset(void)
{
int i;
g_irecv_cmd_expect[0] = '\0';
g_irecv_cmd_expect_active = 0;
g_irecv_file_upload_bytes_out = NULL;
g_irecv_file_upload_active = 0;
for (i = 0; i < MOCK_SETENV_SLOTS; i++)
memset(&g_irecv_setenv_results[i], 0,
sizeof(g_irecv_setenv_results[i]));
g_irecv_setenv_default_error = 0;
g_irecv_open_error = 0;
for (i = 0; i < IRECV_OPEN_ERROR_SLOTS; i++)
memset(&g_irecv_open_error_slots[i], 0,
sizeof(g_irecv_open_error_slots[i]));
for (i = 0; i < IRECV_SEND_FILE_STAGES; i++)
memset(&g_irecv_send_file_stages[i], 0,
sizeof(g_irecv_send_file_stages[i]));
g_irecv_info_pid = 0x1281;
g_irecv_info_ecid = 0;
snprintf(g_irecv_info_serial, sizeof(g_irecv_info_serial), "%s",
"CPID:8020 CPRV:11 ECID:0012345678ABCDEF");
}
void mock_irecv_expect_command(const char *cmd_substring)
{
if (!cmd_substring) {
g_irecv_cmd_expect_active = 0;
return;
}
snprintf(g_irecv_cmd_expect, sizeof(g_irecv_cmd_expect), "%s",
cmd_substring);
g_irecv_cmd_expect_active = 1;
}
void mock_irecv_expect_file_upload(size_t *bytes_sent_out)
{
g_irecv_file_upload_bytes_out = bytes_sent_out;
g_irecv_file_upload_active = 1;
}
void mock_irecv_set_setenv_result(const char *var, int error_code)
{
int i;
if (!var) {
g_irecv_setenv_default_error = error_code;
return;
}
for (i = 0; i < MOCK_SETENV_SLOTS; i++) {
if (!g_irecv_setenv_results[i].in_use) {
snprintf(g_irecv_setenv_results[i].var,
sizeof(g_irecv_setenv_results[i].var), "%s", var);
g_irecv_setenv_results[i].error = error_code;
g_irecv_setenv_results[i].in_use = 1;
return;
}
}
}
void mock_irecv_set_device_info(uint32_t pid, uint64_t ecid,
const char *serial_string)
{
g_irecv_info_pid = pid;
g_irecv_info_ecid = ecid;
snprintf(g_irecv_info_serial, sizeof(g_irecv_info_serial), "%s",
serial_string ? serial_string : "");
}
/*
* mock_irecv_set_open_error -- backwards-compatible sticky open error.
* Also queues a single entry in the FIFO so new-style callers still see
* an error on the very first call.
*/
void mock_irecv_set_open_error(int error_code)
{
g_irecv_open_error = error_code;
if (error_code != 0) {
int i;
for (i = 0; i < IRECV_OPEN_ERROR_SLOTS; i++) {
if (!g_irecv_open_error_slots[i].in_use) {
g_irecv_open_error_slots[i].error_code = error_code;
g_irecv_open_error_slots[i].in_use = 1;
g_irecv_open_error_slots[i].consumed = 0;
return;
}
}
}
}
/*
* mock_irecv_queue_open_error -- push one more staged open error onto
* the FIFO. Each irecv_open_with_ecid call consumes one slot; once the
* FIFO is empty the call succeeds normally.
*/
void mock_irecv_queue_open_error(int error_code)
{
int i;
if (error_code == 0)
return;
for (i = 0; i < IRECV_OPEN_ERROR_SLOTS; i++) {
if (!g_irecv_open_error_slots[i].in_use) {
g_irecv_open_error_slots[i].error_code = error_code;
g_irecv_open_error_slots[i].in_use = 1;
g_irecv_open_error_slots[i].consumed = 0;
return;
}
}
}
/*
* mock_irecv_set_send_file_error -- queue an error for the next unconsumed
* irecv_send_file() call whose path contains path_needle. Matches are
* FIFO by slot order; each slot fires at most once.
*/
void mock_irecv_set_send_file_error(const char *path_needle, int error_code)
{
int i;
if (!path_needle || error_code == 0)
return;
for (i = 0; i < IRECV_SEND_FILE_STAGES; i++) {
if (!g_irecv_send_file_stages[i].in_use) {
snprintf(g_irecv_send_file_stages[i].path_needle,
sizeof(g_irecv_send_file_stages[i].path_needle),
"%s", path_needle);
g_irecv_send_file_stages[i].error_code = error_code;
g_irecv_send_file_stages[i].in_use = 1;
g_irecv_send_file_stages[i].consumed = 0;
return;
}
}
}