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

300
tests/mocks/mock_libcurl.c Normal file
View file

@ -0,0 +1,300 @@
/*
* mock_libcurl.c -- Link-time interposition of libcurl easy interface.
*
* The SUT uses curl for Albert activation server requests. This mock
* captures the URL + body passed via curl_easy_setopt, then on
* curl_easy_perform it invokes the write callback with any staged
* response body for matching URLs. CURLINFO_RESPONSE_CODE reports the
* staged HTTP status.
*/
#include "mock_internal.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <curl/curl.h>
/* Type alias for the write callback signature used by curl. */
typedef size_t (*curl_write_cb_t)(void *ptr, size_t size, size_t nmemb,
void *userdata);
/* ------------------------------------------------------------------ */
/* Curl handle */
/* ------------------------------------------------------------------ */
struct mock_curl_easy {
uint32_t magic;
char url[1024];
curl_write_cb_t write_cb;
void *write_data;
long last_http_code;
};
#define CURL_MAGIC 0x43524C45u /* "CRLE" */
/* Note: the public type is `CURL` which on the real libcurl is a void
* alias. We redeclare via typedef so our internal struct is legal
* without conflicting. The caller never dereferences it. */
/* ------------------------------------------------------------------ */
/* Staging */
/* ------------------------------------------------------------------ */
#define CURL_STAGE_SLOTS 16
typedef struct {
int in_use;
char substr[512];
long http_code;
void *body;
size_t body_len;
} curl_stage_t;
static curl_stage_t g_curl_stages[CURL_STAGE_SLOTS];
static int g_perform_count;
static char g_last_url[1024];
void mock_curl_reset(void)
{
int i;
for (i = 0; i < CURL_STAGE_SLOTS; i++) {
free(g_curl_stages[i].body);
memset(&g_curl_stages[i], 0, sizeof(g_curl_stages[i]));
}
g_perform_count = 0;
g_last_url[0] = '\0';
}
void mock_curl_set_response(const char *url_substring, long http_code,
const void *body, size_t body_len)
{
int i;
if (!url_substring)
return;
for (i = 0; i < CURL_STAGE_SLOTS; i++) {
if (!g_curl_stages[i].in_use) {
curl_stage_t *s = &g_curl_stages[i];
snprintf(s->substr, sizeof(s->substr), "%s", url_substring);
s->http_code = http_code;
s->body = mock_memdup(body, body_len);
s->body_len = body_len;
s->in_use = 1;
return;
}
}
}
int mock_curl_get_perform_count(void)
{
return g_perform_count;
}
const char *mock_curl_get_last_url(void)
{
return g_last_url;
}
/* ------------------------------------------------------------------ */
/* curl_easy_* */
/* ------------------------------------------------------------------ */
CURLcode curl_global_init(long flags)
{
(void)flags;
return CURLE_OK;
}
CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
curl_free_callback f,
curl_realloc_callback r,
curl_strdup_callback s,
curl_calloc_callback c)
{
(void)flags; (void)m; (void)f; (void)r; (void)s; (void)c;
return CURLE_OK;
}
void curl_global_cleanup(void) {}
CURL *curl_easy_init(void)
{
struct mock_curl_easy *e;
mock_log_append("curl_easy_init");
e = calloc(1, sizeof(*e));
if (!e) return NULL;
e->magic = CURL_MAGIC;
return (CURL *)e;
}
void curl_easy_cleanup(CURL *handle)
{
struct mock_curl_easy *e = (struct mock_curl_easy *)handle;
mock_log_append("curl_easy_cleanup");
if (!e) return;
e->magic = 0;
free(e);
}
CURLcode curl_easy_setopt(CURL *handle, CURLoption option, ...)
{
struct mock_curl_easy *e = (struct mock_curl_easy *)handle;
va_list ap;
if (!e)
return CURLE_FAILED_INIT;
va_start(ap, option);
switch (option) {
case CURLOPT_URL: {
const char *url = va_arg(ap, const char *);
if (url) {
snprintf(e->url, sizeof(e->url), "%s", url);
snprintf(g_last_url, sizeof(g_last_url), "%s", url);
mock_log_append("curl_easy_setopt(URL=%s)", url);
}
break;
}
case CURLOPT_WRITEFUNCTION: {
e->write_cb = va_arg(ap, curl_write_cb_t);
break;
}
case CURLOPT_WRITEDATA: {
e->write_data = va_arg(ap, void *);
break;
}
default:
/* All other options are discarded; we don't need them. */
break;
}
va_end(ap);
return CURLE_OK;
}
static curl_stage_t *find_curl_stage(const char *url)
{
int i;
if (!url)
return NULL;
for (i = 0; i < CURL_STAGE_SLOTS; i++) {
if (g_curl_stages[i].in_use &&
strstr(url, g_curl_stages[i].substr))
return &g_curl_stages[i];
}
return NULL;
}
CURLcode curl_easy_perform(CURL *handle)
{
struct mock_curl_easy *e = (struct mock_curl_easy *)handle;
curl_stage_t *stage;
if (!e)
return CURLE_FAILED_INIT;
g_perform_count++;
mock_log_append("curl_easy_perform(url=%s)", e->url);
stage = find_curl_stage(e->url);
if (!stage) {
/* No staging: respond with empty 200. */
e->last_http_code = 200;
return CURLE_OK;
}
e->last_http_code = stage->http_code;
if (e->write_cb && stage->body && stage->body_len > 0) {
e->write_cb(stage->body, 1, stage->body_len, e->write_data);
}
return CURLE_OK;
}
CURLcode curl_easy_getinfo(CURL *handle, CURLINFO info, ...)
{
struct mock_curl_easy *e = (struct mock_curl_easy *)handle;
va_list ap;
va_start(ap, info);
if (e && info == CURLINFO_RESPONSE_CODE) {
long *p = va_arg(ap, long *);
if (p) *p = e->last_http_code;
}
va_end(ap);
return CURLE_OK;
}
const char *curl_easy_strerror(CURLcode code)
{
switch (code) {
case CURLE_OK: return "No error";
case CURLE_FAILED_INIT: return "Failed initialization";
default: return "Mock curl error";
}
}
char *curl_easy_escape(CURL *handle, const char *string, int length)
{
size_t n;
char *buf;
(void)handle;
if (!string)
return NULL;
n = length > 0 ? (size_t)length : strlen(string);
buf = malloc(n * 3 + 1); /* worst case: %HH */
if (!buf) return NULL;
/* Trivial percent-encoding of non-alphanumerics. Adequate for tests. */
{
size_t i, o = 0;
static const char hex[] = "0123456789ABCDEF";
for (i = 0; i < n; i++) {
unsigned char c = (unsigned char)string[i];
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') || c == '-' || c == '_' ||
c == '.' || c == '~') {
buf[o++] = (char)c;
} else {
buf[o++] = '%';
buf[o++] = hex[c >> 4];
buf[o++] = hex[c & 0xF];
}
}
buf[o] = '\0';
}
return buf;
}
void curl_free(void *p) { free(p); }
/* ------------------------------------------------------------------ */
/* curl_slist_* */
/* ------------------------------------------------------------------ */
struct curl_slist *curl_slist_append(struct curl_slist *list,
const char *value)
{
struct curl_slist *node = calloc(1, sizeof(*node));
if (!node) return list;
node->data = mock_strdup_safe(value);
node->next = NULL;
if (!list) return node;
{
struct curl_slist *p = list;
while (p->next) p = p->next;
p->next = node;
}
return list;
}
void curl_slist_free_all(struct curl_slist *list)
{
while (list) {
struct curl_slist *next = list->next;
free(list->data);
free(list);
list = next;
}
}