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

263
tests/mocks/mock_control.h Normal file
View file

@ -0,0 +1,263 @@
/*
* mock_control.h -- Public staging API for the tr4mpass mock library.
*
* Tests call these functions in their setup to:
* - stage canned responses for the next library call,
* - register expectations that the next call must match,
* - inspect the recorded call log after the SUT runs,
* - and reset all state between tests.
*
* The mocks themselves live in mock_*.c files in this directory. They
* replace the symbols normally provided by libimobiledevice, libirecovery,
* libusb-1.0, libssh2, and libcurl when the test binary is linked without
* those real libraries.
*
* NONE of this API is thread-safe. Tests must run sequentially and call
* mock_reset_all() in their setup hook.
*/
#ifndef TR4MPASS_MOCK_CONTROL_H
#define TR4MPASS_MOCK_CONTROL_H
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/* ------------------------------------------------------------------ */
/* Global reset */
/* ------------------------------------------------------------------ */
/* Clear every staged value, expectation, captured call, and scratch
* buffer in every mock. MUST be called at the start of every test. */
void mock_reset_all(void);
/* ------------------------------------------------------------------ */
/* Call log (shared across all mocks) */
/* ------------------------------------------------------------------ */
#define MOCK_LOG_MAX 512
/* Append an entry. Used internally by mocks; exposed for ad-hoc test
* helpers that want to record synthetic events. */
void mock_log_append(const char *fmt, ...);
/* Number of entries currently in the log. */
size_t mock_get_call_count(void);
/* Read-only pointer to entry at index; NULL if out of range. */
const char *mock_get_call_log(size_t index);
/* Return 1 if any entry contains the given substring, else 0. */
int mock_call_log_contains(const char *needle);
/* ------------------------------------------------------------------ */
/* lockdownd / idevice staging */
/* ------------------------------------------------------------------ */
/*
* Stage a value to be returned by the next matching lockdownd_get_value
* call where (domain matches OR domain==NULL) AND key matches.
*
* data is duplicated; caller retains ownership of its original buffer.
* If data_type is "string", data is interpreted as a C string.
* If data_type is "uint", data is interpreted as a pointer to uint64_t.
* If data_type is "data", data is a raw byte blob of len bytes.
*
* Multiple stagings with the same (domain,key) are allowed; mocks
* return them in FIFO order.
*/
void mock_lockdownd_set_value(const char *domain, const char *key,
const char *data_type,
const void *data, size_t len);
/* Configure whether idevice_get_device_list returns a list with
* `count` fake UDID entries, or zero if count==0. The UDIDs returned
* are synthetic ("MOCKUDID0", "MOCKUDID1", ...). */
void mock_idevice_set_device_list(int count);
/* Force the next lockdownd_client_new_with_handshake call to fail
* with the given error code (default 0 = LOCKDOWN_E_SUCCESS). */
void mock_lockdownd_set_handshake_error(int error_code);
/* ------------------------------------------------------------------ */
/* libirecovery staging */
/* ------------------------------------------------------------------ */
/*
* Assert that the NEXT irecv_send_command call receives a string
* matching cmd_substring (substring match). If it does not, the
* call is recorded as a failure and the mock returns an error code.
* Only one expectation may be active at a time.
*/
void mock_irecv_expect_command(const char *cmd_substring);
/*
* Assert that the NEXT irecv_send_file call uploads a file. On the
* call, *bytes_sent_out is set to the number of bytes the SUT attempted
* to transfer. bytes_sent_out may be NULL if the test does not care.
*/
void mock_irecv_expect_file_upload(size_t *bytes_sent_out);
/*
* Configure the return value of irecv_setenv for a specific variable
* name (exact match). Call with var=NULL to set the default.
*/
void mock_irecv_set_setenv_result(const char *var, int error_code);
/*
* Configure the synthetic irecv_device_info returned by
* irecv_get_device_info. pid=0x1281 simulates recovery mode,
* pid=0x1227 simulates DFU mode, etc.
*/
void mock_irecv_set_device_info(uint32_t pid, uint64_t ecid,
const char *serial_string);
/*
* Force every subsequent irecv_open_* call to fail with the given error
* code. This is a sticky flag kept for backwards compatibility; new code
* should prefer mock_irecv_queue_open_error() for FIFO semantics.
*/
void mock_irecv_set_open_error(int error_code);
/*
* Push one more staged open error onto an 8-slot FIFO. Each call to
* irecv_open_with_ecid consumes one slot (or returns success if the FIFO
* is empty and no sticky flag is set).
*/
void mock_irecv_queue_open_error(int error_code);
/*
* Force the next irecv_send_file() call whose path contains
* path_needle (strstr match) to return error_code. Up to 8 independent
* stagings may be queued; each fires at most once.
*/
void mock_irecv_set_send_file_error(const char *path_needle, int error_code);
/* ------------------------------------------------------------------ */
/* libusb staging */
/* ------------------------------------------------------------------ */
/*
* Stage a response for the next libusb_control_transfer call.
* If data_len>0 and data!=NULL, the data is copied into the caller's
* buffer (for IN transfers). Return code is the value libusb returns.
*/
void mock_libusb_stage_control_transfer(int return_code,
const void *data, size_t data_len);
/*
* Configure libusb_get_device_list to return `count` synthetic devices
* with the given (vid,pid) pair.
*/
void mock_libusb_set_device_list(int count, uint16_t vid, uint16_t pid);
/* ------------------------------------------------------------------ */
/* libssh2 staging */
/* ------------------------------------------------------------------ */
/*
* Stage a canned response for the next libssh2_channel_exec call that
* contains command_substring. exit_code is what the channel reports,
* stdout_data is fed to libssh2_channel_read when the SUT reads it.
*
* stdout_data is duplicated; caller retains ownership.
*/
void mock_ssh_set_exec_response(const char *command_substring,
int exit_code,
const void *stdout_data, size_t stdout_len);
/*
* Capture the destination path and payload of the next SCP upload
* (libssh2_scp_send*). On the call:
* *path_out is set to a newly allocated string that the TEST must free
* *bytes_out is set to the number of bytes the SUT wrote
* Either argument may be NULL if the test does not need it.
*/
void mock_ssh_expect_scp(char **path_out, size_t *bytes_out);
/*
* Force the next libssh2_userauth_password call to fail (0=success,
* non-zero=error).
*/
void mock_ssh_set_auth_result(int error_code);
/*
* Force the next libssh2_scp_send*() call to return a NULL channel. The
* SUT's ssh_scp_upload helper treats that as a transport failure and
* returns -1. The flag is self-clearing after the next SCP attempt so
* tests that want to fail only one SCP out of several stay predictable.
*/
void mock_ssh_force_scp_error(int err);
/* ------------------------------------------------------------------ */
/* hardware stub staging (mock_hw_stubs.c) */
/* ------------------------------------------------------------------ */
/*
* Force the checkm8 stub to return the given value. Default is 0 (success).
* Pass -1 to simulate an exploit-stage failure.
*/
void mock_checkm8_set_exploit_result(int result);
/*
* Force the checkm8_verify_pwned stub to return the given value. Default
* is 1 (pwned). Pass 0 to simulate a non-pwned device after exploit; -1
* for a query error.
*/
void mock_checkm8_set_verify_pwned_result(int result);
/* ------------------------------------------------------------------ */
/* mobileactivation staging (mock_afc_mobileactivation.c) */
/* ------------------------------------------------------------------ */
/*
* Stage an error code to be returned by mobileactivation_activate() /
* mobileactivation_activate_with_session() on the next call. Used by
* Path B tests to simulate the SEP nonce barrier documented in
* 26.3-vulnerability.md. Pass 0 to clear.
*/
void mock_mobileactivation_set_activate_error(int error_code);
/*
* Stage an error code for mobileactivation_create_activation_info() /
* mobileactivation_create_activation_info_with_session(). Used to
* simulate server-side rejection of session handshake.
*/
void mock_mobileactivation_set_create_info_error(int error_code);
/*
* Stage the next activation state returned by
* mobileactivation_get_activation_state(). Default is "Activated".
* Pass NULL to reset to default.
*/
void mock_mobileactivation_set_state(const char *state);
/* ------------------------------------------------------------------ */
/* libcurl staging */
/* ------------------------------------------------------------------ */
/*
* Stage a response for any curl_easy_perform whose URL (set via
* CURLOPT_URL) contains url_substring. The SUT's write callback will
* be invoked with the staged body; CURLINFO_RESPONSE_CODE reports
* http_code. Body is duplicated.
*
* Multiple stagings may coexist; the first matching substring wins.
*/
void mock_curl_set_response(const char *url_substring, long http_code,
const void *body, size_t body_len);
/* Return number of times curl_easy_perform has been called. */
int mock_curl_get_perform_count(void);
/* Return the last URL passed to curl_easy_setopt(CURLOPT_URL). */
const char *mock_curl_get_last_url(void);
#ifdef __cplusplus
}
#endif
#endif /* TR4MPASS_MOCK_CONTROL_H */