Metadata-Version: 2.4
Name: jpegtrust
Version: 0.3.0
Summary: Embed, extract, and verify JPEG Trust provenance metadata
Project-URL: Homepage, https://github.com/JPEG-Trust-Community/reference-software
Project-URL: Documentation, https://github.com/JPEG-Trust-Community/reference-software/tree/main/pip_package#readme
Project-URL: Repository, https://github.com/JPEG-Trust-Community/reference-software
Project-URL: Issues, https://github.com/JPEG-Trust-Community/reference-software/issues
Author: JPEG Trust NU Research Team
License-Expression: MIT
License-File: LICENSE
Keywords: c2pa,jpeg,jumbf,metadata,provenance,trust
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Multimedia :: Graphics
Classifier: Topic :: Security
Requires-Python: >=3.10
Requires-Dist: click>=8.0
Requires-Dist: pyyaml>=6.0
Provides-Extra: c2pa
Requires-Dist: c2pa-python<0.39,>=0.38.0; extra == 'c2pa'
Provides-Extra: crypto
Requires-Dist: cryptography>=41.0; extra == 'crypto'
Provides-Extra: dev
Requires-Dist: pytest-cov; extra == 'dev'
Requires-Dist: pytest>=7.0; extra == 'dev'
Description-Content-Type: text/markdown

# jpegtrust

Embed, extract, and verify JPEG Trust provenance metadata.

`jpegtrust` is a Python package for working with JPEG Trust provenance metadata based on the C2PA and JUMBF standards. It lets you sign JPEG and PNG images with a C2PA manifest, embed unsigned metadata, extract it back out, and verify images against configurable trust profiles.

**`embed_image` does not sign.** It writes the metadata as unsigned JSON. Its output has no Claim or Claim Signature, so it is not a Trust Record under ISO/DIS 21617-1. Use `sign_image` for signed output.

## Installation

```bash
pip install jpegtrust
```

Optional extras:

| Extra | Needed for |
|---|---|
| `c2pa` | `sign_image`, `jpegtrust sign`, and reading signed files with the C2PA SDK |
| `crypto` | Certificate chain validation (`jpegtrust.crypto`) |

```bash
pip install 'jpegtrust[c2pa]'
```

## Quick start

```python
from jpegtrust import embed_image, extract_from_file, sign_image, verify_image, create_profile, save_profile

# Create a trust profile
profile = create_profile(template="newsroom", name="Wire Photo Policy")
save_profile(profile, "profile.yml")

# Sign an image (needs the c2pa extra)
assertions = {
    "c2pa.actions.v2": {
        "actions": [{
            "action": "c2pa.created",
            "digitalSourceType": "http://cv.iptc.org/newscodes/digitalsourcetype/digitalCapture",
        }]
    }
}
sign_image("photo.jpg", assertions, "signed.jpg", cert_chain="chain.pem", private_key="key.pem")

# Or embed unsigned metadata
embed_image("photo.jpg", "metadata.json", "labelled.jpg")

# Extract metadata
metadata = extract_from_file("signed.jpg")

# Verify against the profile
report = verify_image("signed.jpg", "profile.yml")
print(f"{report.status} | {report.score} | {report.passed_checks}/{report.total_checks}")
```

## CLI

```bash
# Profile operations
jpegtrust profile list-templates
jpegtrust profile list-checks
jpegtrust profile create --template newsroom -o profile.yml

# Sign (needs the c2pa extra; ASSERTIONS is a JSON file with the assertion map)
jpegtrust sign photo.jpg assertions.json --cert chain.pem --key key.pem -o signed.jpg
jpegtrust sign photo.jpg assertions.json --cert chain.pem --key key.pem --tsa-url http://tsa.example/

# Embed unsigned metadata
jpegtrust embed photo.jpg metadata.json -o labelled.jpg

# Extract metadata
jpegtrust extract signed.jpg
jpegtrust extract signed.jpg -o metadata.json

# Verify
jpegtrust verify signed.jpg profile.yml
jpegtrust verify signed.jpg profile.yml --format json

# Batch operations
jpegtrust batch embed ./photos metadata.json -o ./signed
jpegtrust batch extract ./signed -o ./extracted
jpegtrust batch verify ./signed profile.yml
```

## API reference

### Signing

| Function | Description |
|---|---|
| `sign_image(image, assertions, output, cert_chain, private_key, alg="ES256", tsa_url=None, thumbnail=False)` | Sign a JPEG or PNG with one C2PA manifest that holds the assertion map. Needs the `c2pa` extra |
| `jpegtrust.sign.SigningError` | Raised for any input or signing problem. A subclass of `ValueError` |

### Embedding (unsigned)

| Function | Description |
|---|---|
| `embed_image(image, metadata, output)` | Embed unsigned metadata from a file path or dict into a JPEG or PNG |
| `embed_metadata(image_bytes, metadata_dict)` | Embed unsigned metadata into raw JPEG or PNG bytes |

### Extraction

| Function | Description |
|---|---|
| `extract_from_file(image)` | Extract and parse metadata from a JPEG or PNG file |
| `extract_metadata(image_bytes)` | Extract the raw JSON string from an unsigned JSON box |

### Verification

| Function | Description |
|---|---|
| `verify(indicator, profile_yaml)` | Verify an indicator dict against a profile YAML string |
| `verify_image(image_path, profile_path)` | Extract + verify in one call |

## Signing

`sign_image` signs an image with the C2PA SDK (`c2pa-python`). It takes:

- the input JPEG or PNG, which it does not change
- an assertion map `{label: data}`, or the path of a JSON file that holds one. This is the same shape as `declaration.assertions` in the metadata that `embed_image` takes
- the output path
- a PEM certificate chain, leaf first, and the PEM private key of the leaf certificate
- optional: `alg` (`ES256`, `ES384`, `ES512`, `PS256`, `PS384`, `PS512` or `ED25519`; it must match the key), `tsa_url` (an RFC 3161 time-stamp authority; an empty value means no time-stamp) and `thumbnail` (add the SDK's claim thumbnail; off by default)

The output holds one signed manifest: the assertions, a `c2pa.hash.data` hard binding that the SDK adds, a Claim and a Claim Signature. It holds no Trust Declaration.

`sign_image` raises `SigningError`, and leaves no file at the output path, when:

- the image already has a manifest. It does not add the old manifest as an ingredient
- the assertion map holds status fields (`signature_status`, `content_status`, `assertion_status`, `status`)
- the assertion map holds a label that the SDK writes itself (`c2pa.hash.*`, `c2pa.thumbnail.*`)
- the SDK reads the signed output back as invalid. For example, a `c2pa.created` action needs a `digitalSourceType`
- the SDK stores a label under another name. For example, the SDK stores `c2pa.actions` as `c2pa.actions.v2`, so use `c2pa.actions.v2`
- the output path is the input path, the output folder does not exist, or the key, certificate or assertion file cannot be read or used

Error messages name the key file's path but never hold any part of the key.

The validator in this repository gives signed output a scoped conformance pass when the root of the signing chain is a trust anchor. For tests and examples, `validator/tests/fixtures/` holds public test keys (`test-signing-cert.txt`, `test-signing-key.txt`, root `test-root-cert.txt`). Anyone can sign with them, so do not use them for real work.

## Where status fields come from

`extract_from_file` reads a signed file with the C2PA SDK when the `c2pa` extra is installed. The SDK computes the status fields (for example `manifests[0].status.signature`). For an unsigned JSON box, `extract_from_file` drops any status fields that the file supplies about itself, so an unsigned file fails the `signature_chain` and `pixel_hash` checks.

`verify(indicator, profile_yaml)` takes a dict and trusts it, including any status fields in it. Pass it only dicts from `extract_from_file` or from a source that you trust.

`verify_image` does not check the signer's certificate against trust anchors. Use the validator for signer trust and conformance.

### Profiles

| Function | Description |
|---|---|
| `create_profile(template, name, issuer)` | Create a profile from a predefined template |
| `save_profile(profile, path)` | Save profile as YAML |
| `load_profile(path)` | Load profile YAML from file |
| `list_templates()` | List available templates and their checks |
| `list_checks()` | List all available check definitions |

### Batch

| Function | Description |
|---|---|
| `batch_embed(input_dir, metadata, output_dir)` | Embed metadata into all images in a directory |
| `batch_extract(input_dir, output_dir)` | Extract metadata from all images |
| `batch_verify(input_dir, profile)` | Verify all images against a profile |

## Templates

| Template | Checks | Use case |
|---|---|---|
| `newsroom` | All 9 checks | Wire photos, editorial verification |
| `insurance` | signature, timestamp, author, edit history, GPS, pixel hash | Claims photo validation |
| `creator` | signature, author, copyright, AI declaration, pixel hash | Artist authenticity |
| `ai-labelling` | signature, AI declaration, digital source type, AI labelling, edit history, pixel hash, data mining | AI labelling |
| `custom` | None (you pick) | Build your own |

## Trust profiles

Trust profiles are multi-document YAML files that define verification rules. Each rule contains an expression evaluated against the image's Trust Indicator Set (ISO/DIS 21617-1 A.4). In that set, `declaration` is the Trust Declaration written when the asset was created, and `manifests` lists the other Trust Manifests with the active one first, so `manifests[0]` is the active manifest:

```yaml
---
metadata:
  name: "My Profile"
  issuer: "My Organization"
  date: "2025-01-01T00:00:00Z"
  version: "1.0.0"
  language: en

---
- id: signature_check
  expression: >
    manifests[0].status.signature == "claimSignature.validated"
  report_text:
    'true':
      en: "Valid signature"
    'false':
      en: "No valid signature"
```

## Verification report

```python
report = verify_image("signed.jpg", "profile.yml")

report.status        # "pass" | "warning" | "fail"
report.score         # 0.0 - 1.0
report.verdict       # "trusted" | "mostly_trusted" | "profile_mismatch"
report.passed        # ["signature_chain", "pixel_hash", ...]
report.failed        # ["gps_location", ...]
report.total_checks  # 9
report.passed_checks # 7
report.c2pa          # C2PA detection info
report.to_dict()     # Full report as a dict
```

## License

MIT
