# contentauth/c2pa-rs architecture

> Architecture diagram of the GitHub repository contentauth/c2pa-rs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/contentauth/c2pa-rs
- Repository: https://github.com/contentauth/c2pa-rs

## Overview

##### Project type and purpose
- This is a Rust-based, multi-crate workspace whose primary deliverable is the `c2pa` library (implementing the C2PA and CAWG identity assertions spec).
- It also ships a high-level Rust SDK (`sdk` crate), a C FFI layer (`c_api` crate), a CLI tool (`cli` crate), plus auxiliary utilities (`export_schema`, `make_test_images`) and WASM support.

##### Top-level components (crates)
- Core Rust library (root `Cargo.toml`, workspace index).
- `sdk` crate: the heart of the implementation—modules for claims, manifests, asset handlers, cryptography, identity, JUMBF, I/O, validation, utilities.
- `c_api` crate: C-compatible bindings wrapping core library functions for use from C/C++.
- `cli` crate: command-line interface that invokes core library operations (read/write manifests, sign, validate).
- `export_schema` crate: generates JSON schemas for manifests.
- `make_test_images` crate: utility to derive test assets (thumbnails, manifests embedded in images).
- WASM/WebAssembly validator (`sdk/src/wasm/webcrypto_validator.rs`).
- Tests and examples: integration tests, fixtures, sample code under each crate.

##### Relationships and data flows
- CLI and C API both depend on the core library (`c2pa` / `sdk` crates).
- `export_schema` and `make_test_images` are standalone binaries that also depend on the core library modules.
- Core library exposes layered modules:
- Asset handlers (PNG, JPEG, PDF, MP3, etc.) → I/O → JUMBF processing → claim/manifest generation → signing (via cryptography module) → storage.
- Identity assertion builder/validator modules feed into manifest.
- Crypto module has two implementations (OpenSSL FFI + pure Rust).
- Validation engine (OCSP, time-stamp, COSE, raw signatures).
- WASM validator sits alongside the crypto module when targeting web.

##### Architectural patterns and design principles
- Modular, crate-per-concern design: each crate encapsulates a specific delivery or binding (e.g., CLI, C API, WASM).
- Layered domain model in `sdk`:
1. Domain types (claims, manifests, assertions)
2. Builders/generators
3. Crypto + signing/validation
4. Asset I/O
5. Utilities (CBOR, Merkle, XMP, thumbnailing)
- FFI abstraction: `c_api` surfaces a C interface over the Rust internals.
- Test harness with fixtures under each crate, ensuring coverage of formats and cryptographic workflows.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Binaries
    subgraph "Binaries"
        direction TB
        CLI["CLI Binary"]:::binary
        Export["JSON Schema Exporter"]:::binary
        TestImg["Test-Image Utility"]:::binary
    end

    %% Libraries
    subgraph "Libraries"
        direction TB
        CoreSDK["Core SDK Library (sdk)"]:::lib
        CAPI["C API Crate"]:::lib
    end

    %% Core SDK Layers
    subgraph "Core SDK Layers" 
        direction TB
        AssetIO["Asset I/O & Handlers"]:::lib
        ManifestEngine["Manifest Engine"]:::lib
        CryptoLayer["Crypto Module"]:::lib
        Identity["Identity & Assertion"]:::lib
        Utils["Utilities"]:::lib
        WASM["WASM Validator"]:::lib
    end

    %% Crypto Engines
    subgraph "Crypto Engines"
        direction TB
        OpenSSL["OpenSSL FFI"]:::external
        RustNative["Rust-native Crypto"]:::external
    end

    %% External Services
    subgraph "External Services"
        direction TB
        OCSP["OCSP / TSA Endpoints"]:::external
        WebCrypto["WebCrypto API"]:::external
    end

    %% Storage
    FS["File System"]:::storage

    %% Connections
    CLI -->|"depends on"| CoreSDK
    CAPI -->|"wraps"| CoreSDK
    Export -->|"uses"| CoreSDK
    TestImg -->|"uses"| CoreSDK

    CoreSDK --> AssetIO
    CoreSDK --> ManifestEngine
    CoreSDK --> CryptoLayer
    CoreSDK --> Identity
    CoreSDK --> Utils
    CoreSDK --> WASM

    AssetIO --> FS
    ManifestEngine --> CryptoLayer
    CryptoLayer --> OpenSSL
    CryptoLayer --> RustNative
    ManifestEngine --> FS

    WASM -->|"calls"| WebCrypto
    CryptoLayer -->|"network request"| OCSP

    %% Click Events
    click CoreSDK "https://github.com/contentauth/c2pa-rs/tree/main/sdk/"
    click WASM "https://github.com/contentauth/c2pa-rs/blob/main/sdk/src/wasm/webcrypto_validator.rs"
    click CAPI "https://github.com/contentauth/c2pa-rs/tree/main/c_api/"
    click CLI "https://github.com/contentauth/c2pa-rs/tree/main/cli/"
    click Export "https://github.com/contentauth/c2pa-rs/tree/main/export_schema/"
    click TestImg "https://github.com/contentauth/c2pa-rs/tree/main/make_test_images/"

    %% Styles
    classDef lib fill:#D0E8FF,stroke:#036,stroke-width:1px
    classDef binary fill:#D0FFD0,stroke:#060,stroke-width:1px
    classDef external fill:#FFE0B2,stroke:#E65,stroke-width:1px
    classDef storage fill:#F0F0F0,stroke:#999,stroke-width:1px
```
