tr4mpass/tests/test_cert_extract.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

190 lines
5.9 KiB
C

/*
* test_cert_extract.c -- Certificate extraction tests (Phase 2A).
*
* Exercises cert_extract_* via the mock lockdownd API. Because the
* extractor is only linked when building with the mock framework
* (make test-mocks), the entire suite is compiled out under plain
* `make test` -- there we just print a skip banner.
*/
#include "test_framework.h"
#ifdef TEST_MODE
#include "activation/cert_extract.h"
#include "mocks/mock_control.h"
#include <stdlib.h>
#include <string.h>
#include <plist/plist.h>
/* Non-NULL sentinel; the mock ignores the pointer value. */
static lockdownd_client_t fake_client(void)
{
static int dummy;
return (lockdownd_client_t)&dummy;
}
static const unsigned char CERT_A[] = {
0x30, 0x82, 0x01, 0x0a, 0xDE, 0xAD, 0xBE, 0xEF,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
};
static const unsigned char CERT_B[] = {
0x30, 0x82, 0x02, 0x20, 0xCA, 0xFE, 0xBA, 0xBE,
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
0x99, 0xAA, 0xBB, 0xCC
};
/* ------------------------------------------------------------------ */
static void test_extract_device_cert_success(void)
{
cert_blob_t blob = {NULL, 0};
mock_reset_all();
mock_lockdownd_set_value("com.apple.mobile.lockdown",
"DeviceCertificate",
"data", CERT_A, sizeof(CERT_A));
ASSERT_EQ(cert_extract_device_certificate(fake_client(), &blob), 0);
ASSERT_NOTNULL(blob.data);
ASSERT_EQ(blob.len, sizeof(CERT_A));
ASSERT_MEM_EQ(blob.data, CERT_A, sizeof(CERT_A));
cert_blob_free(&blob);
ASSERT_NULL(blob.data);
ASSERT_EQ(blob.len, (size_t)0);
}
static void test_extract_device_cert_missing(void)
{
cert_blob_t blob = {NULL, 0};
mock_reset_all();
/* No staged value: the getter must surface -1. */
ASSERT_EQ(cert_extract_device_certificate(fake_client(), &blob), -1);
ASSERT_NULL(blob.data);
ASSERT_EQ(blob.len, (size_t)0);
}
static void test_extract_account_token_cert_success(void)
{
cert_blob_t blob = {NULL, 0};
mock_reset_all();
mock_lockdownd_set_value(NULL, "AccountTokenCertificate",
"data", CERT_B, sizeof(CERT_B));
ASSERT_EQ(cert_extract_account_token_certificate(fake_client(), &blob), 0);
ASSERT_EQ(blob.len, sizeof(CERT_B));
ASSERT_MEM_EQ(blob.data, CERT_B, sizeof(CERT_B));
cert_blob_free(&blob);
}
static void test_extract_account_token_cert_fallback_to_chain(void)
{
/*
* Verify the fallback order: with only the SECOND-choice key
* staged (ActivationStateDeadline), the extractor must find it
* after AccountTokenCertificate misses. The mock cannot wrap a
* real PLIST_ARRAY, so the third-choice CertificateChain path is
* covered via a DATA node staged under that key below.
*/
cert_blob_t blob = {NULL, 0};
mock_reset_all();
mock_lockdownd_set_value(NULL, "ActivationStateDeadline",
"data", CERT_A, sizeof(CERT_A));
ASSERT_EQ(cert_extract_account_token_certificate(fake_client(), &blob), 0);
ASSERT_EQ(blob.len, sizeof(CERT_A));
ASSERT_MEM_EQ(blob.data, CERT_A, sizeof(CERT_A));
cert_blob_free(&blob);
/* Third fallback: CertificateChain as raw DATA. */
mock_reset_all();
mock_lockdownd_set_value(NULL, "CertificateChain",
"data", CERT_B, sizeof(CERT_B));
blob.data = NULL; blob.len = 0;
ASSERT_EQ(cert_extract_account_token_certificate(fake_client(), &blob), 0);
ASSERT_EQ(blob.len, sizeof(CERT_B));
cert_blob_free(&blob);
}
static void test_extract_account_token_cert_all_missing(void)
{
cert_blob_t blob = {NULL, 0};
mock_reset_all();
ASSERT_EQ(cert_extract_account_token_certificate(fake_client(), &blob), -1);
ASSERT_NULL(blob.data);
}
static void test_extract_unique_cert_success(void)
{
cert_blob_t blob = {NULL, 0};
mock_reset_all();
mock_lockdownd_set_value("com.apple.mobile.activation",
"UniqueDeviceCertificate",
"data", CERT_B, sizeof(CERT_B));
ASSERT_EQ(cert_extract_unique_device_certificate(fake_client(), &blob), 0);
ASSERT_EQ(blob.len, sizeof(CERT_B));
ASSERT_MEM_EQ(blob.data, CERT_B, sizeof(CERT_B));
cert_blob_free(&blob);
}
static void test_extract_unique_cert_wrong_domain(void)
{
cert_blob_t blob = {NULL, 0};
mock_reset_all();
/* Value staged on wrong domain -- lookup must miss. */
mock_lockdownd_set_value("com.apple.mobile.lockdown",
"UniqueDeviceCertificate",
"data", CERT_A, sizeof(CERT_A));
ASSERT_EQ(cert_extract_unique_device_certificate(fake_client(), &blob), -1);
ASSERT_NULL(blob.data);
}
static void test_cert_blob_free_null_safe(void)
{
cert_blob_t zeroed = {NULL, 0};
/* No-op on NULL. */
cert_blob_free(NULL);
/* No-op on already-zeroed blob. */
cert_blob_free(&zeroed);
ASSERT_NULL(zeroed.data);
ASSERT_EQ(zeroed.len, (size_t)0);
}
static void test_cert_blob_free_normal(void)
{
cert_blob_t blob;
blob.data = (unsigned char *)malloc(32);
ASSERT_NOTNULL(blob.data);
blob.len = 32;
memset(blob.data, 0x5A, 32);
cert_blob_free(&blob);
ASSERT_NULL(blob.data);
ASSERT_EQ(blob.len, (size_t)0);
}
#endif /* TEST_MODE */
/* ------------------------------------------------------------------ */
void run_cert_extract_tests(void)
{
printf("--- Section 5: Certificate extraction ---\n");
#ifdef TEST_MODE
test_extract_device_cert_success();
test_extract_device_cert_missing();
test_extract_account_token_cert_success();
test_extract_account_token_cert_fallback_to_chain();
test_extract_account_token_cert_all_missing();
test_extract_unique_cert_success();
test_extract_unique_cert_wrong_domain();
test_cert_blob_free_null_safe();
test_cert_blob_free_normal();
#else
printf(" (skipped: requires mock build, run `make test-mocks`)\n");
#endif
}