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>
This commit is contained in:
tr4m0ryp 2026-04-18 17:43:30 +02:00
parent 5b758931ac
commit 30807c8ef6
58 changed files with 7967 additions and 1210 deletions

View file

@ -0,0 +1,244 @@
/*
* integration_helpers.c -- Fixture setup for Path A / Path B E2E tests.
*
* Responsibilities:
* - create temp directory + minimal blob files for each pipeline stage
* - set the TR4MPASS_* env vars that path_a_* / path_b_* read
* - build synthetic device_info_t records for A5-A11 / A12+ / WiFi-only
* - provide small call-log inspection helpers for ordering assertions
*
* Everything lives under /tmp/tr4mpass_e2e_<pid>/ so parallel test runs
* never collide. mock_reset_all() is intentionally NOT called here --
* tests choose when to reset mock state relative to fixture setup.
*/
#include "integration_helpers.h"
#include "mocks/mock_control.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
/* 32 bytes is enough to satisfy file_readable() + SCP-upload byte counts. */
static const unsigned char E2E_BLOB[32] = {
0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0xba, 0xbe,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
};
static int
write_blob(const char *path)
{
FILE *f;
size_t n;
if (!path || !*path) {
errno = EINVAL;
return -1;
}
f = fopen(path, "wb");
if (!f)
return -1;
n = fwrite(E2E_BLOB, 1, sizeof(E2E_BLOB), f);
fclose(f);
if (n != sizeof(E2E_BLOB)) {
unlink(path);
return -1;
}
return 0;
}
void
e2e_unset_all_env(void)
{
unsetenv("TR4MPASS_IBSS_PATH");
unsetenv("TR4MPASS_IBEC_PATH");
unsetenv("TR4MPASS_DEVICETREE_PATH");
unsetenv("TR4MPASS_RAMDISK_PATH");
unsetenv("TR4MPASS_TRUSTCACHE_PATH");
unsetenv("TR4MPASS_SSH_PORT");
unsetenv("TR4MPASS_SSH_HOST");
unsetenv("TR4MPASS_SSH_USER");
unsetenv("TR4MPASS_SSH_PASSWORD");
unsetenv("TR4MPASS_RAMDISK_TIMEOUT");
unsetenv("TR4MPASS_TEST_SSH_READY");
unsetenv("TR4MPASS_PATCHED_MAD");
unsetenv("TR4MPASS_JBINIT_PATCHER");
unsetenv("TR4MPASS_SIGNING_KEY");
}
int
e2e_fixture_init(e2e_fixture_t *fx)
{
int pid = (int)getpid();
if (!fx)
return -1;
memset(fx, 0, sizeof(*fx));
snprintf(fx->base_dir, sizeof(fx->base_dir),
"/tmp/tr4mpass_e2e_%d", pid);
if (mkdir(fx->base_dir, 0755) != 0 && errno != EEXIST) {
fprintf(stderr, "e2e: mkdir %s failed: %s\n",
fx->base_dir, strerror(errno));
return -1;
}
snprintf(fx->ibss, sizeof(fx->ibss),
"%s/iBSS.img4", fx->base_dir);
snprintf(fx->ibec, sizeof(fx->ibec),
"%s/iBEC.img4", fx->base_dir);
snprintf(fx->devtree, sizeof(fx->devtree),
"%s/DeviceTree.img4", fx->base_dir);
snprintf(fx->trustcache, sizeof(fx->trustcache),
"%s/trustcache", fx->base_dir);
snprintf(fx->ramdisk, sizeof(fx->ramdisk),
"%s/ramdisk.dmg", fx->base_dir);
snprintf(fx->patched_mad, sizeof(fx->patched_mad),
"%s/mobileactivationd", fx->base_dir);
if (write_blob(fx->ibss) != 0) return -1;
if (write_blob(fx->ibec) != 0) return -1;
if (write_blob(fx->devtree) != 0) return -1;
if (write_blob(fx->trustcache) != 0) return -1;
if (write_blob(fx->ramdisk) != 0) return -1;
if (write_blob(fx->patched_mad) != 0) return -1;
setenv("TR4MPASS_IBSS_PATH", fx->ibss, 1);
setenv("TR4MPASS_IBEC_PATH", fx->ibec, 1);
setenv("TR4MPASS_DEVICETREE_PATH", fx->devtree, 1);
setenv("TR4MPASS_TRUSTCACHE_PATH", fx->trustcache, 1);
setenv("TR4MPASS_RAMDISK_PATH", fx->ramdisk, 1);
setenv("TR4MPASS_PATCHED_MAD", fx->patched_mad, 1);
setenv("TR4MPASS_SSH_HOST", "127.0.0.1", 1);
setenv("TR4MPASS_SSH_PORT", "2222", 1);
setenv("TR4MPASS_SSH_USER", "root", 1);
setenv("TR4MPASS_SSH_PASSWORD", "alpine", 1);
setenv("TR4MPASS_RAMDISK_TIMEOUT", "2", 1);
/* Skip the real 127.0.0.1:2222 poll -- the mock does not run sshd. */
setenv("TR4MPASS_TEST_SSH_READY", "1", 1);
return 0;
}
void
e2e_fixture_teardown(e2e_fixture_t *fx)
{
if (!fx)
return;
if (fx->ibss[0]) unlink(fx->ibss);
if (fx->ibec[0]) unlink(fx->ibec);
if (fx->devtree[0]) unlink(fx->devtree);
if (fx->trustcache[0]) unlink(fx->trustcache);
if (fx->ramdisk[0]) unlink(fx->ramdisk);
if (fx->patched_mad[0]) unlink(fx->patched_mad);
if (fx->base_dir[0]) rmdir(fx->base_dir);
e2e_unset_all_env();
}
device_info_t
e2e_make_device_path_a(void)
{
device_info_t dev;
memset(&dev, 0, sizeof(dev));
dev.checkm8_vulnerable = 1;
dev.is_dfu_mode = 1;
dev.cpid = 0x8015;
dev.ecid = 0x001A2B3C4D5E6F70ULL;
snprintf(dev.udid, sizeof(dev.udid),
"abcdef0123456789abcdef0123456789abcdef01");
snprintf(dev.product_type, sizeof(dev.product_type), "iPhone10,3");
snprintf(dev.chip_id, sizeof(dev.chip_id), "0x8015");
snprintf(dev.ios_version, sizeof(dev.ios_version), "14.8.1");
snprintf(dev.activation_state, sizeof(dev.activation_state),
"Unactivated");
snprintf(dev.serial, sizeof(dev.serial), "F2LXXXXXABCD");
snprintf(dev.imei, sizeof(dev.imei), "352000123456789");
snprintf(dev.hardware_model, sizeof(dev.hardware_model), "D22AP");
snprintf(dev.device_name, sizeof(dev.device_name), "e2e-iphone-a11");
snprintf(dev.chip_name, sizeof(dev.chip_name), "A11");
return dev;
}
device_info_t
e2e_make_device_path_b_cellular(void)
{
device_info_t dev;
memset(&dev, 0, sizeof(dev));
dev.checkm8_vulnerable = 0;
dev.is_dfu_mode = 1;
dev.cpid = 0x8020;
dev.ecid = 0x0022334455667788ULL;
snprintf(dev.udid, sizeof(dev.udid),
"11112222333344445555666677778888aaaabbbb");
snprintf(dev.product_type, sizeof(dev.product_type), "iPhone11,8");
snprintf(dev.chip_id, sizeof(dev.chip_id), "0x8020");
snprintf(dev.ios_version, sizeof(dev.ios_version), "17.4");
snprintf(dev.activation_state, sizeof(dev.activation_state),
"Unactivated");
snprintf(dev.serial, sizeof(dev.serial), "F2LYYYYABCD");
snprintf(dev.imei, sizeof(dev.imei), "358000987654321");
snprintf(dev.hardware_model, sizeof(dev.hardware_model), "N841AP");
snprintf(dev.device_name, sizeof(dev.device_name), "e2e-iphone-a12-cell");
snprintf(dev.chip_name, sizeof(dev.chip_name), "A12");
dev.is_cellular = 1;
return dev;
}
device_info_t
e2e_make_device_path_b_wifi(void)
{
device_info_t dev;
memset(&dev, 0, sizeof(dev));
dev.checkm8_vulnerable = 0;
dev.is_dfu_mode = 1;
dev.cpid = 0x8101;
dev.ecid = 0x0099AABBCCDDEEFFULL;
snprintf(dev.udid, sizeof(dev.udid),
"aaaabbbbccccddddeeeeffff0000111122223333");
snprintf(dev.product_type, sizeof(dev.product_type), "iPad11,6");
snprintf(dev.chip_id, sizeof(dev.chip_id), "0x8101");
snprintf(dev.ios_version, sizeof(dev.ios_version), "17.4");
snprintf(dev.activation_state, sizeof(dev.activation_state),
"Unactivated");
snprintf(dev.serial, sizeof(dev.serial), "F2LWIFIABCD");
dev.imei[0] = '\0';
dev.meid[0] = '\0';
snprintf(dev.hardware_model, sizeof(dev.hardware_model), "J217AP");
snprintf(dev.device_name, sizeof(dev.device_name), "e2e-ipad-a13-wifi");
snprintf(dev.chip_name, sizeof(dev.chip_name), "A13");
dev.is_cellular = 0;
return dev;
}
int
e2e_first_index_of(const char *needle)
{
size_t n;
size_t i;
if (!needle)
return -1;
n = mock_get_call_count();
for (i = 0; i < n; i++) {
const char *line = mock_get_call_log(i);
if (line && strstr(line, needle) != NULL)
return (int)i;
}
return -1;
}
int
e2e_ordering_ok(const char *needle_a, const char *needle_b)
{
int ia = e2e_first_index_of(needle_a);
int ib = e2e_first_index_of(needle_b);
if (ia < 0 || ib < 0)
return 0;
return ia < ib ? 1 : 0;
}

View file

@ -0,0 +1,90 @@
/*
* integration_helpers.h -- Shared fixtures for Path A / Path B E2E tests.
*
* Each E2E test needs a synthetic device_info_t, a set of readable temp
* files for the iBSS/iBEC/devicetree/ramdisk/patched-mad stages, and a
* consistent set of TR4MPASS_* environment variables so path_a_*.c and
* path_b_*.c accept the inputs without reaching real hardware.
*
* All paths live under /tmp/tr4mpass_e2e_<pid>/ so concurrent test runs
* do not collide. Tests call e2e_fixture_init() in setUp and
* e2e_fixture_teardown() in tearDown; mock_reset_all() must still be
* invoked explicitly (the helpers never touch mock state).
*/
#ifndef TR4MPASS_E2E_INTEGRATION_HELPERS_H
#define TR4MPASS_E2E_INTEGRATION_HELPERS_H
#include <stddef.h>
#include <stdint.h>
#include "device/device.h"
#define E2E_PATH_MAX 160
typedef struct {
char base_dir[E2E_PATH_MAX];
char ibss[E2E_PATH_MAX];
char ibec[E2E_PATH_MAX];
char devtree[E2E_PATH_MAX];
char trustcache[E2E_PATH_MAX];
char ramdisk[E2E_PATH_MAX];
char patched_mad[E2E_PATH_MAX];
} e2e_fixture_t;
/*
* e2e_fixture_init -- Create a fresh /tmp/tr4mpass_e2e_<pid>/ directory
* populated with minimal placeholder files for each Path A stage. The
* TR4MPASS_* environment variables are set to point at those files.
*
* Returns 0 on success, -1 on any I/O failure. On success the caller
* must call e2e_fixture_teardown() to remove the temp files and unset
* the environment variables.
*/
int e2e_fixture_init(e2e_fixture_t *fx);
/*
* e2e_fixture_teardown -- Delete temp files and unset env vars. Safe to
* call even when e2e_fixture_init() failed partway through.
*/
void e2e_fixture_teardown(e2e_fixture_t *fx);
/*
* e2e_make_device_path_a -- Populate a device_info_t for a Path A
* (checkm8-vulnerable) device with all identity fields filled in. The
* device is returned by value so the caller owns the storage.
*/
device_info_t e2e_make_device_path_a(void);
/*
* e2e_make_device_path_b_cellular -- A12+ cellular device fixture
* (checkm8_vulnerable=0, IMEI populated).
*/
device_info_t e2e_make_device_path_b_cellular(void);
/*
* e2e_make_device_path_b_wifi -- A12+ WiFi-only device fixture
* (checkm8_vulnerable=0, IMEI and MEID empty).
*/
device_info_t e2e_make_device_path_b_wifi(void);
/*
* e2e_unset_all_env -- Clear every TR4MPASS_* env var this fixture
* touches. Used by the "missing env" test case that wants a completely
* unconfigured environment.
*/
void e2e_unset_all_env(void);
/*
* e2e_first_index_of -- Return the index of the first call-log entry
* containing needle, or -1 if none. Used by ordering assertions.
*/
int e2e_first_index_of(const char *needle);
/*
* e2e_ordering_ok -- Shorthand for "a appears before b in the call log".
* Returns 1 if both are present and a comes first, 0 otherwise.
*/
int e2e_ordering_ok(const char *needle_a, const char *needle_b);
#endif /* TR4MPASS_E2E_INTEGRATION_HELPERS_H */

View file

@ -0,0 +1,252 @@
/*
* test_path_a_e2e.c -- End-to-end Path A bypass pipeline tests.
*
* Drives path_a_module.execute() against the full mock stack with Phase 2
* modules linked in. checkm8 is stubbed by mock_hw_stubs.c so each stage
* is individually inject-failable. Six scenarios: happy, exploit-fail,
* ramdisk-timeout, SSH-auth-fail, SCP-fail, missing-env. Call ordering
* is verified via the shared mock log (checkm8 -> iBSS -> iBEC -> "go"
* -> "bootx" -> ssh -> mount -> jbinit -> backup-mad -> SCP).
*/
#include "test_framework.h"
#include "integration_helpers.h"
#include "bypass/path_a.h"
#include "device/device.h"
#include "mocks/mock_control.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* IRECV_E_NO_DEVICE numeric equivalent. */
#define E2E_IRECV_E_NO_DEVICE (-1)
static void
stage_happy_ssh(void)
{
/* path_a_jailbreak: rw mount, nvram (advisory), amfi-patch, verify. */
mock_ssh_set_exec_response("mount -u -w -t apfs", 0, NULL, 0);
mock_ssh_set_exec_response("nvram auto-boot=false", 0, NULL, 0);
mock_ssh_set_exec_response("jbinit", 0, NULL, 0);
mock_ssh_set_exec_response("ls -la /mnt1/usr/libexec/mobileactivationd",
0, NULL, 0);
/* path_a_replace_mobileactivationd: backup, chmod, chown, stat. */
mock_ssh_set_exec_response("cp /mnt1/usr/libexec/mobileactivationd "
"/mnt1/usr/libexec/mobileactivationd.bak",
0, NULL, 0);
mock_ssh_set_exec_response("chmod 755", 0, NULL, 0);
mock_ssh_set_exec_response("chown 0:0", 0, NULL, 0);
mock_ssh_set_exec_response("stat -c", 0,
"755 root:wheel\n",
strlen("755 root:wheel\n"));
}
/*
* test_path_a_e2e_happy_path -- Full pipeline succeeds end-to-end.
* Asserts execute()==0, every stage's log marker is present, and the
* inter-stage ordering constraints hold.
*/
static void
test_path_a_e2e_happy_path(void)
{
device_info_t dev;
e2e_fixture_t fx;
int rc;
printf(" [e2e] path_a happy path\n");
mock_reset_all();
ASSERT_EQ(e2e_fixture_init(&fx), 0);
stage_happy_ssh();
dev = e2e_make_device_path_a();
/* deletescript + activation need non-null handle + lockdown; mocks
* accept any non-null opaque pointer. */
dev.handle = (idevice_t)(uintptr_t)0x1;
dev.lockdown = (lockdownd_client_t)(uintptr_t)0x2;
rc = path_a_module.execute(&dev);
ASSERT_EQ(rc, 0);
ASSERT_EQ(mock_call_log_contains("checkm8_exploit"), 1);
ASSERT_EQ(mock_call_log_contains("checkm8_verify_pwned"), 1);
ASSERT_EQ(mock_call_log_contains("irecv_open_with_ecid"), 1);
ASSERT_EQ(mock_call_log_contains(fx.ibss), 1);
ASSERT_EQ(mock_call_log_contains(fx.ibec), 1);
ASSERT_EQ(mock_call_log_contains(fx.devtree), 1);
ASSERT_EQ(mock_call_log_contains(fx.ramdisk), 1);
ASSERT_EQ(mock_call_log_contains("irecv_send_command(go)"), 1);
ASSERT_EQ(mock_call_log_contains("irecv_send_command(bootx)"), 1);
ASSERT_EQ(mock_call_log_contains("libssh2_userauth_password"), 1);
ASSERT_EQ(mock_call_log_contains("mount -u -w -t apfs"), 1);
ASSERT_EQ(mock_call_log_contains("jbinit"), 1);
ASSERT_EQ(mock_call_log_contains("mobileactivationd.bak"), 1);
ASSERT_EQ(mock_call_log_contains("libssh2_scp_send"), 1);
ASSERT_EQ(mock_call_log_contains("mobileactivation_activate"), 1);
ASSERT_EQ(mock_call_log_contains(
"mobileactivation_get_activation_state"), 1);
ASSERT_EQ(e2e_ordering_ok("checkm8_exploit",
"irecv_open_with_ecid"), 1);
ASSERT_EQ(e2e_ordering_ok(fx.ibss, fx.ibec), 1);
ASSERT_EQ(e2e_ordering_ok("irecv_send_command(go)",
"irecv_send_command(bootx)"), 1);
ASSERT_EQ(e2e_ordering_ok("irecv_send_command(bootx)",
"libssh2_userauth_password"), 1);
ASSERT_EQ(e2e_ordering_ok("mount -u -w -t apfs",
"jbinit"), 1);
ASSERT_EQ(e2e_ordering_ok("mobileactivationd.bak",
"libssh2_scp_send"), 1);
e2e_fixture_teardown(&fx);
}
/* Exploit failure stops the pipeline at step 1 -- no further mock calls. */
static void
test_path_a_e2e_exploit_fails(void)
{
device_info_t dev;
e2e_fixture_t fx;
int rc;
printf(" [e2e] path_a exploit failure\n");
mock_reset_all();
ASSERT_EQ(e2e_fixture_init(&fx), 0);
mock_checkm8_set_exploit_result(-1);
dev = e2e_make_device_path_a();
rc = path_a_module.execute(&dev);
ASSERT_EQ(rc, -1);
ASSERT_EQ(mock_call_log_contains("checkm8_exploit"), 1);
ASSERT_EQ(mock_call_log_contains("checkm8_verify_pwned"), 0);
ASSERT_EQ(mock_call_log_contains("irecv_open_with_ecid"), 0);
ASSERT_EQ(mock_call_log_contains("libssh2_userauth_password"), 0);
ASSERT_EQ(mock_call_log_contains("libssh2_scp_send"), 0);
ASSERT_EQ(mock_call_log_contains("mobileactivation_activate"), 0);
e2e_fixture_teardown(&fx);
}
/*
* iBSS uploads, reconnect never succeeds -- pipeline fails at ramdisk.
* The FIFO returns success once (for the initial pwned DFU open), then
* the sticky flag forces every subsequent open to fail.
*/
static void
test_path_a_e2e_ramdisk_timeout(void)
{
device_info_t dev;
e2e_fixture_t fx;
int rc;
printf(" [e2e] path_a ramdisk reconnect timeout\n");
mock_reset_all();
ASSERT_EQ(e2e_fixture_init(&fx), 0);
setenv("TR4MPASS_RAMDISK_TIMEOUT", "1", 1);
mock_irecv_set_open_error(E2E_IRECV_E_NO_DEVICE);
dev = e2e_make_device_path_a();
rc = path_a_module.execute(&dev);
ASSERT_EQ(rc, -1);
ASSERT_EQ(mock_call_log_contains("irecv_send_command(go)"), 0);
ASSERT_EQ(mock_call_log_contains("irecv_send_command(bootx)"), 0);
ASSERT_EQ(mock_call_log_contains("libssh2_userauth_password"), 0);
ASSERT_EQ(mock_call_log_contains("mobileactivation_activate"), 0);
e2e_fixture_teardown(&fx);
}
/* Ramdisk boots, SSH auth refused -- no SCP attempted. */
static void
test_path_a_e2e_ssh_auth_fails(void)
{
device_info_t dev;
e2e_fixture_t fx;
int rc;
printf(" [e2e] path_a SSH auth failure\n");
mock_reset_all();
ASSERT_EQ(e2e_fixture_init(&fx), 0);
stage_happy_ssh();
mock_ssh_set_auth_result(-1);
dev = e2e_make_device_path_a();
rc = path_a_module.execute(&dev);
ASSERT_EQ(rc, -1);
ASSERT_EQ(mock_call_log_contains("irecv_send_command(bootx)"), 1);
ASSERT_EQ(mock_call_log_contains("libssh2_userauth_password"), 1);
ASSERT_EQ(mock_call_log_contains("libssh2_scp_send"), 0);
ASSERT_EQ(mock_call_log_contains("mobileactivation_activate"), 0);
e2e_fixture_teardown(&fx);
}
/* Jailbreak OK, SCP upload fails -- activation never attempted. */
static void
test_path_a_e2e_scp_fails(void)
{
device_info_t dev;
e2e_fixture_t fx;
int rc;
printf(" [e2e] path_a SCP upload failure\n");
mock_reset_all();
ASSERT_EQ(e2e_fixture_init(&fx), 0);
stage_happy_ssh();
mock_ssh_force_scp_error(1);
dev = e2e_make_device_path_a();
rc = path_a_module.execute(&dev);
ASSERT_EQ(rc, -1);
ASSERT_EQ(mock_call_log_contains("irecv_send_command(bootx)"), 1);
ASSERT_EQ(mock_call_log_contains("jbinit"), 1);
ASSERT_EQ(mock_call_log_contains("mobileactivationd.bak"), 1);
ASSERT_EQ(mock_call_log_contains("libssh2_scp_send"), 1);
ASSERT_EQ(mock_call_log_contains("mobileactivation_activate"), 0);
e2e_fixture_teardown(&fx);
}
/* Env unset -- pipeline fails early, no iRecovery/SSH traffic. */
static void
test_path_a_e2e_missing_env(void)
{
device_info_t dev;
int rc;
printf(" [e2e] path_a missing env vars\n");
mock_reset_all();
e2e_unset_all_env();
dev = e2e_make_device_path_a();
rc = path_a_module.execute(&dev);
ASSERT_EQ(rc, -1);
ASSERT_EQ(mock_call_log_contains("irecv_open_with_ecid"), 0);
ASSERT_EQ(mock_call_log_contains("libssh2_userauth_password"), 0);
ASSERT_EQ(mock_call_log_contains("libssh2_scp_send"), 0);
ASSERT_EQ(mock_call_log_contains("mobileactivation_activate"), 0);
}
void
run_path_a_integration_tests(void)
{
printf("--- Section 10: Path A end-to-end integration ---\n");
test_path_a_e2e_happy_path();
test_path_a_e2e_exploit_fails();
test_path_a_e2e_ramdisk_timeout();
test_path_a_e2e_ssh_auth_fails();
test_path_a_e2e_scp_fails();
test_path_a_e2e_missing_env();
}

View file

@ -0,0 +1,225 @@
/*
* test_path_b_e2e.c -- End-to-end Path B (A12+) pipeline tests.
*
* Two boundaries matter for Path B. First, identity manipulation (DFU
* read + recovery setenv) is expected to work on real hardware -- iBoot
* accepts the PWND marker. Second, activation itself is blocked by the
* SEP nonce barrier documented in 26.3-vulnerability.md; FairPlayKeyData,
* RKCData, and SepNonce are sealed by the Secure Enclave. The tool's
* honest behaviour on real A12+ hardware is a clean failure at
* mobileactivation_activate_with_session().
*
* These tests therefore assert:
* - path_b_manipulate_identity() writes the PWND marker successfully,
* - when the SEP barrier rejects activation (simulated via
* mock_mobileactivation_set_activate_error), the session layer
* surfaces -1 cleanly and get_activation_state still says Unactivated,
* - signal_detect_type correctly distinguishes cellular from WiFi-only.
*
* We do NOT drive path_b_module.execute() end-to-end: step_reboot_to_recovery
* uses 2-second libusb polls and a 60s timeout that make synchronous
* tests impractical. The per-step helpers are exercised directly.
*/
#include "test_framework.h"
#include "integration_helpers.h"
#include "activation/activation.h"
#include "activation/record.h"
#include "activation/session.h"
#include "bypass/path_b.h"
#include "bypass/signal.h"
#include "device/device.h"
#include "mocks/mock_control.h"
#include <libusb-1.0/libusb.h>
#include <plist/plist.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Error code used to simulate the SEP nonce barrier. The numeric value
* is arbitrary; the SUT only checks != MOBILEACTIVATION_E_SUCCESS. */
#define E2E_MOBILEACTIVATION_E_NONCE_INVALID (-7)
/* Build a USB string descriptor: [len, 0x03, UTF-16LE payload...]. */
static size_t
build_string_descriptor(const char *ascii, unsigned char *desc, size_t cap)
{
size_t slen = strlen(ascii);
size_t i, pos;
desc[0] = (unsigned char)(2 + slen * 2);
desc[1] = 0x03;
pos = 2;
for (i = 0; i < slen && pos + 1 < cap; i++) {
desc[pos++] = (unsigned char)ascii[i];
desc[pos++] = 0x00;
}
return pos;
}
/*
* Stage two descriptor reads for path_b_manipulate_identity():
* 1. initial read returns the original serial,
* 2. verify read returns original + " PWND:[checkm8]" (simulates the
* successful setenv having taken effect).
*/
static void
stage_usb_serial_descriptor(const char *ascii_serial)
{
unsigned char desc[256];
unsigned char desc_after[256];
char modified[256];
size_t n1, n2;
snprintf(modified, sizeof(modified), "%s PWND:[checkm8]", ascii_serial);
n1 = build_string_descriptor(ascii_serial, desc, sizeof(desc));
n2 = build_string_descriptor(modified, desc_after, sizeof(desc_after));
mock_libusb_stage_control_transfer((int)n1, desc, n1);
mock_libusb_stage_control_transfer((int)n2, desc_after, n2);
}
/* Static sentinel used as a non-null libusb_device_handle *. */
static libusb_device_handle *
fake_usb_handle(void)
{
static int sentinel;
return (libusb_device_handle *)&sentinel;
}
/*
* Identity manipulation happy path. Asserts:
* - path_b_manipulate_identity returns 0,
* - irecv_setenv("serial-number", ...) contained "PWND:[checkm8]",
* - irecv_saveenv persisted the change.
*/
static void
test_path_b_e2e_identity_manipulation_succeeds(void)
{
device_info_t dev;
const char *current_serial =
"CPID:8020 CPRV:11 ECID:0022334455667788";
int rc;
size_t i;
int found_pwnd = 0;
int found_saveenv = 0;
printf(" [e2e] path_b identity manipulation (happy)\n");
mock_reset_all();
dev = e2e_make_device_path_b_cellular();
dev.usb = fake_usb_handle();
mock_irecv_set_device_info(0x1281, dev.ecid, current_serial);
stage_usb_serial_descriptor(current_serial);
rc = path_b_manipulate_identity(&dev);
ASSERT_EQ(rc, 0);
for (i = 0; i < mock_get_call_count(); i++) {
const char *line = mock_get_call_log(i);
if (!line) continue;
if (strstr(line, "irecv_setenv(serial-number") &&
strstr(line, "PWND:[checkm8]"))
found_pwnd = 1;
if (strstr(line, "irecv_saveenv"))
found_saveenv = 1;
}
ASSERT_EQ(found_pwnd, 1);
ASSERT_EQ(found_saveenv, 1);
}
/*
* SEP nonce barrier: simulate mobileactivation rejecting the activation
* record, then drive the activation half of Path B directly and check
* that session_activate returns -1, the call actually reached the
* mobileactivation layer, and get_activation_state still says
* "Unactivated" -- the tool must not mask the barrier.
*/
static void
test_path_b_e2e_activation_rejected_by_nonce_barrier(void)
{
device_info_t dev;
plist_t record = NULL;
plist_t session_info = NULL;
plist_t handshake = NULL;
char state[64];
int rc;
printf(" [e2e] path_b activation nonce barrier\n");
mock_reset_all();
dev = e2e_make_device_path_b_cellular();
dev.handle = (idevice_t)(uintptr_t)0x1;
dev.lockdown = (lockdownd_client_t)(uintptr_t)0x2;
/* See 26.3-vulnerability.md: SepNonce cannot be produced without
* SEP-level access, so real hardware rejects the record at this
* layer. The test simulates that rejection. */
mock_mobileactivation_set_activate_error(
E2E_MOBILEACTIVATION_E_NONCE_INVALID);
mock_mobileactivation_set_state("Unactivated");
record = record_build_a12(&dev);
ASSERT_NOTNULL(record);
rc = session_get_info(&dev, &session_info);
ASSERT_EQ(rc, 0);
ASSERT_NOTNULL(session_info);
rc = session_drm_handshake(&dev, session_info, &handshake);
ASSERT_EQ(rc, 0);
ASSERT_NOTNULL(handshake);
/* Decisive assertion: with the staged nonce error, activation MUST
* fail and the failure must NOT be swallowed. */
rc = session_activate(&dev, record, handshake);
ASSERT_EQ(rc, -1);
ASSERT_EQ(mock_call_log_contains(
"mobileactivation_activate_with_session"), 1);
rc = activation_get_state(&dev, state, sizeof(state));
ASSERT_EQ(rc, 0);
ASSERT_STREQ(state, "Unactivated");
plist_free(record);
plist_free(session_info);
plist_free(handshake);
}
/*
* Signal detection: cellular device (IMEI populated) -> SIGNAL_GSM,
* WiFi-only (no IMEI, no MEID) -> SIGNAL_NONE.
*/
static void
test_path_b_e2e_signal_detection_cellular_vs_wifi(void)
{
device_info_t dev_cell = e2e_make_device_path_b_cellular();
device_info_t dev_wifi = e2e_make_device_path_b_wifi();
char buf[128];
int rc;
printf(" [e2e] path_b signal detection (cell vs wifi)\n");
mock_reset_all();
ASSERT_EQ(signal_detect_type(&dev_cell), SIGNAL_GSM);
ASSERT_EQ(signal_can_preserve(&dev_cell), 1);
rc = signal_get_carrier_id(&dev_cell, buf, sizeof(buf));
ASSERT_EQ(rc, 0);
ASSERT_STREQ(buf, dev_cell.imei);
ASSERT_EQ(signal_detect_type(&dev_wifi), SIGNAL_NONE);
ASSERT_EQ(signal_can_preserve(&dev_wifi), 0);
rc = signal_get_carrier_id(&dev_wifi, buf, sizeof(buf));
ASSERT_EQ(rc, -1);
}
void
run_path_b_integration_tests(void)
{
printf("--- Section 11: Path B end-to-end integration ---\n");
test_path_b_e2e_identity_manipulation_succeeds();
test_path_b_e2e_activation_rejected_by_nonce_barrier();
test_path_b_e2e_signal_detection_cellular_vs_wifi();
}