Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# macOS Finder metadata
.DS_Store
.idea/
*.iml
.DS_Store
.agent/
.agent/
42 changes: 42 additions & 0 deletions niap-cc/niap-android-cert-ext/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Project exclude paths

# Packaging output
dist/

# Gradle
.gradle/
.kotlin/
.claude/
build/

# IntelliJ IDEA
.idea/
*.iml

# Android Studio
.idea/
local.properties

# macOS
.DS_Store

# Log files
*.log

# Java class files
*.class

# Generated files
**/build/

# Keystore files
*.jks

# Environment variables
.env
adb_key.*
*.hprof
bin/

# TLS lab reproduction harness generated artifacts
tools/tls-lab-repro/out/
114 changes: 114 additions & 0 deletions niap-cc/niap-android-cert-ext/AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Project Knowledge for Agent Execution

This document contains architectural context, workflows, and guidelines for LLM agents to develop, run, and verify security test cases in this repository.

---

## 1. Starting and Stopping TestBed Core (MCP Server)
This project integrates with a desktop-based automation runner and MCP server hosted by the `testbed-core` application on port `11452` (`localhost:11452`). The server must be active for the agent to interface with physical devices or emulators.

### How to Start the Server
Open a separate terminal window, navigate to the `testbed-core` repository directory, and start the desktop program:

```bash
cd ../testbed-core
./gradlew :composeApp:run
```

### How to Stop (Kill) the Server
To prevent port collisions, dual-instance conflicts, or general socket errors (`Address already in use`), identify and force-kill any running processes holding port `11452`. It is recommended to perform this cleanup before launching automated runs:

```bash
# Force-kill processes listening on port 11452
lsof -t -i :11452 | xargs kill -9
```

---

## 2. Referencing Security Documents (MDFPP-CC)
To ensure test accuracy and compliance with security requirements, review the target criteria documented under the `docs/` folder (such as [FIA_XCU_EXT.1.md](docs/FIA_XCU_EXT.1.md) or related Common Criteria files).

If the document references are empty or need to be synchronized with the remote requirements repository, run:

```bash
git submodule update --init --recursive
```

---

## 3. Document-Driven Test Development (Supreme Directive)
The core operating principle of this project is **strict adherence of test implementations to the security requirements documents**:
* Before generating or modifying any JUnit test suites, the LLM agent **must** read the relevant functional requirements and test case evaluation criteria from the target files in the `docs/` folder.
* The written JUnit test assertions must align perfectly with the Pass/Fail decision boundaries defined in the criteria.

---

## 4. Test Development and Execution Workflow
When developing and running security test suites, follow this structured workflow (refer to the main tool references for deeper API mechanics):

1. **Requirements Reference**: Retrieve target specifications from the target documents in `docs/`.
2. **Test Generation**: Author the Kotlin/Java test suites inside the `agent-test` module (e.g., under `org.example.plugin.fiax509`). Ensure all log entries adhere to the logging design patterns (using helpers like `logi` or `logp`).
3. **Compilation and Loading**:
* Compile the JUnit plugin JAR:
```bash
./gradlew :agent-test:jar
```
* Invoke the `junit_test_reload` MCP tool to notify the server and load the newly compiled test definitions.
4. **Execution and Monitoring**:
* Trigger execution using the `junit_test_execute` MCP tool (passing target class and method arguments).
* Poll `junit_test_receive` to stream log messages in real-time and capture structural exit states (Pass/Fail reports and stack traces).

---

## 5. Automation MCP Tools & Specifications
The following MCP tools are exposed by the `testbed-core` interface for automated workflow execution:

* **`junit_test_reload`**: Reloads and updates the JUnit plugin JAR context on the server.
* **`junit_test_list`**: Returns a list of all current tests loaded in the environment (as a JSON array).
* **`junit_test_execute`**: Accepts a class name and optional method identifier to begin execution.
* **`junit_test_receive`**: Retrieves current logs (`status: "Running"`) or finalized structural details (`status: "Finished"`, Pass/Fail indicator, exception details).

> [!NOTE]
> `junit_test_receive` supports active streaming. For long-running operations (e.g., processes taking over 2 minutes), safely poll `junit_test_receive` to track task execution progress.

---

## 6. Utilizing Timers for Passive Wait States
When the agent is required to enter a wait state (e.g. waiting for a compilation step, a background script, or multi-stage device deployments to complete), **always schedule a 60-second timer using the `schedule` tool** rather than generating repeated active prompt-and-response loops. This saves token budgets and resources.

---

## 7. Configuring the Local EST Test Server (libest)
For testing security certificate acquisitions (EST protocol) and client-side mutual TLS actions, run a local mock container based on **Cisco libest** and NGINX (configured under `est-server/`).
* See [est-server/README.md](est-server/README.md) for full docker deployment guidelines.
* Ensure the environment signature algorithm is configured to **SHA-384** (via `EST_CERT_PROFILE=niap`) to properly satisfy Common Criteria / NIAP verification criteria during automated runs.

---

## 8. Lesson Learned: Never Depend on Non-SDK (Hidden) Platform APIs in App-Level Code

**Incident (2026-12, lab resubmission work)**: To read stapled OCSP responses, the app-level library reflected on the *concrete* platform session class (`session.javaClass.getMethod("getStatusResponses")`, which resolves to the hidden `com.android.org.conscrypt.Java7ExtendedSSLSession`). All JVM unit tests passed, but on a stock-policy Android 16 device the call was denied by hidden-API enforcement (`hiddenapi: ... api=blocked ... using reflection: denied`), the exception was swallowed, and every TLS connection failed with a misleading "server did not staple an OCSP response" error. Earlier development devices most likely had hidden-API enforcement relaxed (`hidden_api_policy`), which hides this class of bug (for example, the older `NiapKeyManager` reflection into `android.security.net.config.RemotePrivateKey` can only work in that state). Evaluation labs test on stock-policy devices.

**Rules for code that must run on stock devices (App-Level track: `cert-lib`, `cert-manager`, `validator-test-app`, `cert-test-app`)**:
1. **Public SDK APIs only.** Do not use `Class.forName("com.android.…")`, `com.android.org.conscrypt.*`, `android.security.net.config.*`, `getDeclaredMethod` / `getDeclaredField` / `setAccessible` against platform classes, or any other non-SDK member.
2. **If reflection is unavoidable** (e.g. to stay compatible with older API levels), look the member up on the **public declaring type** (e.g. `ExtendedSSLSession::class.java.getMethod("getStatusResponses")`), never on `obj.javaClass` of a platform-provided object.
3. **Never swallow API-access failures** (`NoSuchMethodException`/`NoSuchMethodError`, `LinkageError`, `SecurityException`, hidden-API denials). Fail closed with a distinct, explicit error message so the real cause is visible.
4. **Verification devices must use default hidden-API enforcement.** Before any device verification, check:
```bash
adb shell settings get global hidden_api_policy # must print: null
adb shell settings get global hidden_api_policy_pre_p_apps # must print: null
adb shell settings get global hidden_api_policy_p_apps # must print: null
```
If any of them was relaxed for debugging, restore it with `adb shell settings delete global <key>` before testing. Never relax it on a device used for verification or for producing evidence.
5. **Scan for hidden-API usage in every device run.** Collect the app's `hiddenapi` log lines (e.g. `adb logcat -d | grep -i hiddenapi | grep -E "com.example.niap|com.android.niap"`). Any `denied` line is a test failure; `Accessing hidden ...` warnings must be removed before submission.
6. **JVM unit tests are not enough.** They do not enforce hidden-API restrictions or use the platform TLS stack. Any change touching platform TLS/crypto objects (`SSLSession`, `SSLSocket`, `SSLEngine`, JCA providers, KeyStore) is only "done" after it has been verified on a stock-policy device (e.g. with `tools/tls-lab-repro`).

> Platform-private APIs are only acceptable in code that ships inside the platform image itself (platform builds / platform-signed privileged components), never in this app-level library or its apps.

**Hidden-API guard**: Two automated checks enforce these rules across the four app-level modules (`cert-lib`, `cert-manager`, `validator-test-app`, `cert-test-app`). Run the static source scanner with `tools/check-no-hidden-api.sh` (per-line exemptions require `// hidden-api-check: allow <reason>` and are printed in the report), and run Android Lint with `PrivateApi`/`DiscouragedPrivateApi`/`SoonBlockedPrivateApi`/`BlockedPrivateApi` set to errors via:
```bash
./gradlew --console=plain -PskipTestbedCopy=true \
:cert-lib:lintDebug :validator-test-app:lintStrictDebug \
:cert-manager:lintDebug :cert-test-app:lintDebug
```
`tools/package-submission.sh` runs both guards automatically and aborts before building artifacts if either fails.
Loading