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

156
src/cli/cli_flags.c Normal file
View file

@ -0,0 +1,156 @@
/*
* cli_flags.c -- Convert bypass-pipeline CLI flags into env vars.
*
* We do our own argv walk rather than getopt_long because main.c's
* getopt_long pass has already permuted argv by the time we run, and
* the bypass flags (--ramdisk, --ssh-host, ...) would otherwise be
* separated from their values. A hand-rolled walk is also the only
* way to ignore every flag we do not know without coupling to the
* short-option spec owned by main.c.
*/
#include "cli/cli_flags.h"
#include "util/env_config.h"
#include "util/log.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* -------------------------------------------------------------- */
/* Flag table */
/* -------------------------------------------------------------- */
typedef struct {
const char *long_name; /* without leading "--" */
const char *env_name; /* TR4MPASS_* */
const char *metavar; /* for --help: <path>, <host>, ... */
const char *description; /* human-readable, <= 58 chars */
} flag_map_t;
static const flag_map_t g_flag_map[] = {
{ "ramdisk", TR4MPASS_ENV_RAMDISK_PATH,
"<path>", "Staged ramdisk .dmg" },
{ "ibss", TR4MPASS_ENV_IBSS_PATH,
"<path>", "Signed iBSS image" },
{ "ibec", TR4MPASS_ENV_IBEC_PATH,
"<path>", "Signed iBEC image" },
{ "devicetree", TR4MPASS_ENV_DEVICETREE_PATH,
"<path>", "DeviceTree image" },
{ "trustcache", TR4MPASS_ENV_TRUSTCACHE_PATH,
"<path>", "Static trust cache blob" },
{ "patched-mad", TR4MPASS_ENV_PATCHED_MAD,
"<path>", "Patched mobileactivationd binary" },
{ "ssh-host", TR4MPASS_ENV_SSH_HOST,
"<host>", "Ramdisk SSH host (default 127.0.0.1)"},
{ "ssh-port", TR4MPASS_ENV_SSH_PORT,
"<port>", "Ramdisk SSH port" },
{ "ssh-user", TR4MPASS_ENV_SSH_USER,
"<user>", "Ramdisk SSH user (default root)" },
{ "ssh-password", TR4MPASS_ENV_SSH_PASSWORD,
"<pass>", "Ramdisk SSH password" },
{ "signing-key", TR4MPASS_ENV_SIGNING_KEY,
"<path>", "Signing key path for bootchain" },
{ "ssh-timeout", TR4MPASS_ENV_RAMDISK_TIMEOUT,
"<sec>", "Ramdisk SSH readiness timeout" },
};
static const size_t g_flag_map_count =
sizeof(g_flag_map) / sizeof(g_flag_map[0]);
/* -------------------------------------------------------------- */
/* Lookup helpers */
/* -------------------------------------------------------------- */
static const flag_map_t *find_exact(const char *name, size_t name_len)
{
for (size_t i = 0; i < g_flag_map_count; ++i) {
const char *ln = g_flag_map[i].long_name;
if (strlen(ln) == name_len && strncmp(ln, name, name_len) == 0)
return &g_flag_map[i];
}
return NULL;
}
static int is_secret(const char *env_name)
{
return env_name != NULL &&
(strstr(env_name, "PASSWORD") != NULL ||
strstr(env_name, "SIGNING_KEY") != NULL);
}
static int apply_env(const char *env_name, const char *value)
{
if (!env_name || !value)
return -1;
if (setenv(env_name, value, 1) != 0) {
log_error("setenv(%s) failed", env_name);
return -1;
}
log_debug("cli_flags: %s <- %s", env_name,
is_secret(env_name) ? "<secret>" : value);
return 0;
}
/* -------------------------------------------------------------- */
/* Parsing */
/* -------------------------------------------------------------- */
int cli_parse_bypass_flags(int argc, char *argv[])
{
if (argc <= 1 || !argv)
return 0;
int rc = 0;
for (int i = 1; i < argc; ++i) {
const char *a = argv[i];
if (!a || a[0] != '-' || a[1] != '-' || a[2] == '\0')
continue; /* not a long option */
const char *name = a + 2;
const char *eq = strchr(name, '=');
size_t nlen = eq ? (size_t)(eq - name) : strlen(name);
const flag_map_t *m = find_exact(name, nlen);
if (!m)
continue; /* owned by main.c or unknown -- leave alone */
const char *value = NULL;
if (eq) {
value = eq + 1;
} else if (i + 1 < argc) {
const char *next = argv[i + 1];
if (!next) {
log_error("--%s requires an argument", m->long_name);
rc = -1;
continue;
}
value = next;
++i; /* consume value */
} else {
log_error("--%s requires an argument", m->long_name);
rc = -1;
continue;
}
if (apply_env(m->env_name, value) != 0)
rc = -1;
}
return rc;
}
void cli_flags_print_help(void)
{
printf(" Bypass pipeline overrides (also read from environment):\n");
for (size_t i = 0; i < g_flag_map_count; ++i) {
char head[64];
snprintf(head, sizeof(head), "--%s %s",
g_flag_map[i].long_name,
g_flag_map[i].metavar);
printf(" %-28s %s (%s)\n", head,
g_flag_map[i].description,
g_flag_map[i].env_name);
}
}