Metadata-Version: 2.4
Name: esp-idf-sbom
Version: 1.5.0
Summary: SPDX SBOM generator for ESP-IDF projects
Author: Espressif Systems
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/espressif/esp-idf-sbom
Keywords: espressif,embedded,spdx,sbom
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: Environment :: Console
Classifier: Topic :: Software Development :: Embedded Systems
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Operating System :: POSIX
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: MacOS :: MacOS X
Requires-Python: >=3.7
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: PyYAML
Requires-Dist: schema
Requires-Dist: license-expression
Requires-Dist: rich
Requires-Dist: pyparsing>=2.2.2
Requires-Dist: esp-pylib[cli]>=1.1.3
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: czespressif; extra == "dev"
Requires-Dist: spdx-tools>=0.8.0rc1; extra == "dev"
Requires-Dist: cyclonedx-python-lib; extra == "dev"
Requires-Dist: jsonschema; extra == "dev"
Dynamic: license-file
Dynamic: license-expression

# ESP-IDF SBOM Tool (**esp-idf-sbom**)

The esp-idf-sbom tool creates [Software bill of materials][1] (SBOM)
files in the [Software Package Data Exchange][2] (SPDX) and [CycloneDX][11]
formats for applications
generated by the [Espressif IoT Development Framework][3] (ESP-IDF).
It also allows to check generated SBOM files for know vulnerabilities against
the [National Vulnerability Database][4] (NVD) based on the
[Common Platform Enumeration][5] (CPE) provided in the SBOM.

### Table of Contents
- [Required ESP-IDF versions](#required-esp-idf-versions)
- [Installation](#installation)
- [Creating SBOM](#creating-sbom)
- [Checking vulnerabilities](#checking-vulnerabilities)
- [CVE statements](#cve-statements)
  - [Statements in manifests](#statements-in-manifests)
  - [Exclusion lists](#exclusion-lists)
  - [VEX documents](#vex-documents)
  - [The statements file](#the-statements-file)
  - [Updating a VEX file](#updating-a-vex-file)
  - [Statuses and justifications](#statuses-and-justifications)
- [Usage example](#usage-example)
- [SBOM layout](#sbom-layout)
- [Output formats](#output-formats)
- [Manifest file](#manifest-file)
  - [Validating manifest files](#validating-manifest-files)
  - [Checking manifest files for vulnerabilities](#checking-manifest-files-for-vulnerabilities)
- [Describing your own project](#describing-your-own-project)
  - [The project](#the-project)
  - [Your components](#your-components)
- [Licenses and Copyrights](#licenses-and-copyrights)
- [Return Values](#return-values)
- [Resources](#resources)

## Required ESP-IDF versions

All release branches of currently supported ESP-IDF versions allow to
generate the SBOM file.

| ESP-IDF version | branch with SBOM support  | commits
|-----------------|---------------------------|----------------------------------------------------------------------------------------------------------------------------------|
| 4.3             | release/v4.3              | [befb32b45bc9314b48c29624f9a2c2ef30e34260](https://github.com/espressif/esp-idf/commit/befb32b45bc9314b48c29624f9a2c2ef30e34260) |
|                 |                           | [f1eef50947ab5770ae4d904c07615e7acab06002](https://github.com/espressif/esp-idf/commit/f1eef50947ab5770ae4d904c07615e7acab06002) |
| 4.4             | release/v4.4              | [ee505a996045c3657711c3d70c58af8dd48b1426](https://github.com/espressif/esp-idf/commit/ee505a996045c3657711c3d70c58af8dd48b1426) |
|                 |                           | [53f271ce108d6fa99cf92d59fe9b9dcc4b8fb45b](https://github.com/espressif/esp-idf/commit/53f271ce108d6fa99cf92d59fe9b9dcc4b8fb45b) |
| 5.0             | release/v5.0              | [30735b33efabd6cf038bcb258b674cf828ad5ecf](https://github.com/espressif/esp-idf/commit/30735b33efabd6cf038bcb258b674cf828ad5ecf) |
|                 |                           | [9156bbb55c920d6704329975311c331b931ed6bc](https://github.com/espressif/esp-idf/commit/9156bbb55c920d6704329975311c331b931ed6bc) |
| 5.1             | release/v5.1              | [0f781c718c8548cd2b0e41a30e1814f1c6ed93a2](https://github.com/espressif/esp-idf/commit/0f781c718c8548cd2b0e41a30e1814f1c6ed93a2) |
|                 |                           | [03162bb276d4155760e8aa839020f0587f5ef599](https://github.com/espressif/esp-idf/commit/03162bb276d4155760e8aa839020f0587f5ef599) |
| latest          | master                    |                                                                                                                                  |

Older versions, e.g. `v5.0.2`, do not have the required code merged and the following error
message will be printed.

    E: Project description file "build/project_description.json" does not support SBOM generation. Please see the list of IDF versions required by esp-idf-sbom.

If you see this error message and want to try `esp-idf-sbom`, you can

1. switch to the release branch for version you are using. For example `release/v5.0` if you are using `v5.0.2`.
2. use future ESP-IDF versions to experiment with esp-idf-sbom.
3. use `git-cherry-pick` and apply commits for your release from the table above. For example for `v5.0.2` use

    $ git cherry-pick 30735b33efabd6cf038bcb258b674cf828ad5ecf 9156bbb55c920d6704329975311c331b931ed6bc


## Installation

Currently esp-idf-sbom is not integrated into ESP-IDF and needs to by installed
separately from Python Package Index (PyPI) with

    pip install esp-idf-sbom

After installation the `esp-idf-sbom` command should be available or the esp_idf_sbom
python module can be used with

    python -m esp_idf_sbom


## Creating SBOM

The application has to be built before the SBOM file can be created. This step should
not be required in the future once esp-idf-sbom is integrated into ESP-IDF.

The SBOM file can be created with

    esp-idf-sbom create <project description file>

The `project description file` is a path to the *project_description.json* file, which
is by default created by the ESP-IDF build system in the project's *build* directory.
The generated SBOM file is printed to the standard output stream by default or can be
saved in a file if the `--output-file` option is used.

The output format is selected with the `--format` option. A bare name selects the
latest spec version esp-idf-sbom supports for that format; a specific version can be
pinned with an `@version` suffix (e.g. `spdx-json@2.2`). The supported values are:

* `spdx-tag-value`, `spdx-tag-value@2.2` - SPDX 2.2 tag/value (default)
* `spdx-json`, `spdx-json@2.2` - SPDX 2.2 JSON
* `spdx-json-ld`, `spdx-json-ld@3.0.1` - SPDX 3.0 JSON-LD
* `cyclonedx-json`, `cyclonedx-json@1.6` - CycloneDX 1.6 JSON

A bare name always points to the latest supported version and may advance on a major
esp-idf-sbom release (a breaking change), so pin `@version` when you need reproducible
output.

For example `esp-idf-sbom create --format cyclonedx-json -o sbom.cdx.json build/project_description.json`.

By default the SBOM also contains the CVE statements of the manifests and of
the `excluded_cves.yaml` files. The `--vex-format` option can leave them out, or
write them to a separate VEX document, see [VEX documents](#vex-documents).


## Checking vulnerabilities

Vulnerabilities are checked based on the [Common Platform Enumeration][5] (CPE) information
included in the SBOM file for the SBOM packages. While checking for vulnerabilities, only packages
with direct or indirect relationship to the **project** package are examined. For example if mbedtls
component is compiled, due to component dependecies, but it's actually not linked into the
final binary, it will be by default presented in the SBOM file, but it will not be reachable
from the root **project** package and hence it will not be checked for vulnerabilities.
The reason for this is to avoid possible false positives, because such packages
have no direct impact on the resulting application. This can be changed with the `--check-all-packages`
option. If used, all packages in the SBOM file will be checked for possible vulnerabilities
regardless their relationships to the application binary.

SBOM file generated by the esp-idf-sbom tool can be checked for known vulnerabilities
with

    esp-idf-sbom check [SBOM file]

If *SBOM file* is not provided, the standard input stream is used. The SBOM file may be
in any format esp-idf-sbom can produce (SPDX tag/value, SPDX JSON, SPDX 3.0 JSON-LD or CycloneDX JSON);
the format is detected automatically.

If the SBOM was created with `--vex-format openvex` or
`--vex-format cyclonedx-json`, the CVE statements are in a separate VEX file.
Pass it with `--vex`, see
[Using a VEX file with check](#using-a-vex-file-with-check).

The default report format consists of multiple tables:

1. Report summary
2. Packages with Identified Vulnerabilities
3. Packages with Possible Vulnerabilities
4. Packages with Excluded Vulnerabilities
5. Packages with No Identified Vulnerabilities
6. Packages without CPE and Keyword Information

The output format may be changed with the `--format` option, which supports exporting
the report into **json**, **csv** or **markdown** format.

If NVD reports that a CVE is in CISA's [Known Exploited Vulnerabilities][12] (KEV)
catalog, this information is included in the report.

When a statement covers a CVE, the report shows the status, justification,
detail and action of the statement. The **json**, **csv** and **markdown**
formats have them in the `vex_status`, `vex_justification`, `vex_detail` and
`vex_action` fields. For example, a CVE that does not affect the package is
shown as excluded, with the reason. See [CVE statements](#cve-statements).

When the `--extended-scan` option is applied with the check command, the
checker uses the product part of the CPE from the SBOM file to search
through descriptions of unanalyzed CVEs in NVD. This can be beneficial for
getting early notifications about potential new CVEs that have not yet been
analyzed by the NVD and do not have a CPE assigned. In addition to the CPE
product name, all keywords specified in **cve-keywords** in the related
manifest file, which are also included in the SBOM, are checked.  For instance,
the CPE product part for mbedtls is **mbed_tls**, so this keyword will be
searched when `--extended-scan` is used. If the mbedtls library manifest file
contains **mbed tls** and **mbedtls** in **cve-keywords**, these will also be
searched. Be aware that using the `--extended-scan` option may result in false
positive reports that are unrelated to the scanned packages or their versions.
These reports must be thoroughly analyzed and are included in a separate table
titled `Packages with Possible Vulnerabilities` in the report.

The vulnerability check typically uses the NVD REST API by default, provided by
[https://nvd.nist.gov](https://nvd.nist.gov).

NVD rate-limits requests to its REST API. Without an API key the limit is 5
requests in a rolling 30 second window; with an API key it is 50 requests in
the same window. esp-idf-sbom reads the key from the `NVDAPIKEY` environment
variable and, when it is set, sends it in the `apikey` request header and
shortens the delay between requests accordingly, which speeds up the scan
considerably. A free API key can be requested [here][9]; see the NVD
[Rate Limits][10] documentation for details.

When the online REST API is used without a key, esp-idf-sbom prints a one-time
hint to standard error suggesting the `NVDAPIKEY` key or the `--local-db`
mirror. Set the `SBOM_NO_HINT` environment variable or pass `--no-hint` to
silence it.

    export NVDAPIKEY=YOUR_API_KEY
    esp-idf-sbom check [SBOM file]

An alternative source of information for the vulnerability check can be a local
NVD mirror, available from the [esp-nvd-mirror][6] repository. The NVD mirror
may provide a faster and more reliable way to perform vulnerability checks in
case there are availability issues with NVD API endpoints or when a large
number of packages need to be scanned quickly. You can enable the use of the
local NVD mirror with the `--local-db` option.

    esp-idf-sbom check --local-db [SBOM file]

By default, the `--local-db` option updates the NVD mirror before performing a
vulnerability check. To disable this, use the `--no-sync-db` option. Be aware
that the initial setup of the NVD mirror may take some time. You can also
update the NVD mirror manually with the `sync-db` command.

    esp-idf-sbom sync-db
    esp-idf-sbom check --local-db --no-sync-db [SBOM file]


## CVE statements

A CVE statement says whether a CVE affects a package. For example: the CVE does
not affect the package, because the package never runs the vulnerable code.
[VEX][13] is the standard format for such statements.

A statement has these fields. The places where statements are written name
them a little differently:

| Field         | Meaning                             | Manifest, `excluded_cves.yaml` | Statements file |
|---------------|-------------------------------------|--------------------------------|-----------------|
| status        | whether the CVE affects the package | always not affected            | `status`        |
| justification | why the CVE does not affect it      | `justification`                | `justification` |
| detail        | why the status applies, as text     | `reason`                       | `detail`        |
| action        | what users should do                | not available                  | `action`        |
| response      | what you do about the CVE           | always `will_not_fix`          | `response`      |

For `status` and `justification` values, see
[Statuses and justifications](#statuses-and-justifications).

Statements are written in these places:

| Place                                    | Written by                    | Statuses          | Read by                             |
|------------------------------------------|-------------------------------|-------------------|-------------------------------------|
| `cve-exclude-list` in a manifest         | the maintainer of a component | not affected only | `create`, `manifest check`          |
| `excluded_cves.yaml` in this repository  | the esp-idf-sbom maintainers  | not affected only | `create`, `check`, `manifest check` |
| `excluded_cves.yaml` in the ESP-IDF tree | the ESP-IDF maintainers       | not affected only | `create`, `manifest check`          |
| `excluded_cves.yaml` in your project     | you                           | not affected only | `create`                            |
| a statements file                        | you                           | all statuses      | `vex update`                        |

`create` writes the statements of the manifests and of the `excluded_cves.yaml`
files into the SBOM or into a separate VEX document, see
[VEX documents](#vex-documents). `check` reads them from there.

Use a statements file when you want to say more than "not affected", for
example that the CVE affects your product and users should update. `vex update`
writes your statements into a VEX document, see
[Updating a VEX file](#updating-a-vex-file).

### Statements in manifests

The `cve-exclude-list` key of a manifest lists the CVEs that do not affect the
package:

    cve-exclude-list:
      - cve: CVE-2023-1234
        reason: The package never calls the vulnerable function.
        justification: vulnerable_code_not_in_execute_path

`reason` is required. `justification` is optional. Without it, the
justification of the matching entry in `excluded_cves.yaml` is used, if there
is one, see [Exclusion lists](#exclusion-lists).

`create` writes each entry as a `not_affected` statement with the response
`will_not_fix`: the CVE does not affect the package, so no fix is planned.

In `.gitmodules`, an entry is `sbom-cve-exclude-list = <CVE> <reason>`. It
cannot have a justification.

[Manifest file](#manifest-file) describes the other keys of a manifest.

### Exclusion lists

An `excluded_cves.yaml` file lists CVEs by their id. esp-idf-sbom downloads the
file of this repository, [excluded_cves.yaml](excluded_cves.yaml), and keeps a
copy in `~/.esp-idf-sbom`. The value of a CVE is one of:

* a string, the reason. The CVE has nothing to do with Espressif products, for
  example a Linux kernel CVE that only mentions the name of a library. `check`
  and `manifest check` drop the CVE from the report.
* a mapping with `cpes` and `reason`. The CVE does not affect the listed CPEs
  and versions. The report shows the CVE as excluded, with the reason. The
  mapping can also have a `justification`, see
  [Statuses and justifications](#statuses-and-justifications).

You can add an `excluded_cves.yaml` file with the same format in two more
places. Its entries are added to the list of this repository for one run. For
the same CVE, the local entry is used.

#### In the ESP-IDF tree

An `excluded_cves.yaml` file at the root of the ESP-IDF tree is used by `create`
and `manifest check`. It is for the ESP-IDF maintainers. The file is part of the
ESP-IDF tree, so an entry and the fix can be on the same release branch.

This helps when the version alone cannot show that a CVE is fixed. A release
branch reports the version of the last release until the next release. For
example, `release/v6.0` reports 6.0.1 until 6.0.2 is released. When NVD lists
6.0.1 as affected and the branch already has the fix, an entry in the ESP-IDF
tree excludes the CVE on this branch only. An entry in the file of this
repository cannot do this, because it would also hide the CVE for the users of
the 6.0.1 release.

```yaml
# <ESP-IDF root>/excluded_cves.yaml
CVE-2026-45160:
  cpes:
    - cpe: cpe:2.3:a:espressif:esp-idf:6.0.1:*:*:*:*:*:*:*
  reason: >-
    Fixed on release/v6.0. The branch still reports 6.0.1 (the version NVD
    lists as affected) until 6.0.2 is released.
```

#### In a project

An `excluded_cves.yaml` file at the root of your project is used only by
`create`. The root of the project is the directory with `CMakeLists.txt`, whose
`build` directory has `project_description.json`. The file is for application
developers, and it applies only to the SBOM of this project.

Use it for CVEs that do not affect your firmware, for example because your
sdkconfig disables the vulnerable code, or because your hardware is not
affected.

```yaml
# <project root>/excluded_cves.yaml
CVE-2025-12345:
  cpes:
    - cpe: cpe:2.3:a:example:libfoo:2.3.1:*:*:*:*:*:*:*
  reason: >-
    The vulnerable feature is disabled in this project's configuration
    (CONFIG_LIBFOO_FEATURE_X is not set).
```

In both files, use the mapping with `cpes`, not a string. The report then shows
the CVE as excluded, with the reason. A string removes the CVE from the report.
When a new version has the fix, NVD no longer lists the CVE for it, and you can
remove the entry.

### VEX documents

`create` writes the statements of the manifests and of the `excluded_cves.yaml`
files. The `--vex-format` option says where:

| `--vex-format`              | SBOM                    | separate file             |
|-----------------------------|-------------------------|---------------------------|
| `embed`                     | contains the statements | none                      |
| `none`                      | no statements           | none                      |
| `openvex`, `cyclonedx-json` | no statements           | written to `--vex-output` |

`embed` is the default.

Use `none` or a VEX format when the SBOM must not contain vulnerability
information. Some regulations require this. Some tools also ignore the
statements in an SBOM.

    esp-idf-sbom create --vex-format openvex --vex-output app.openvex.json \
                        -o app.spdx build/project_description.json

With `--vex-format none`, the statements are lost. `check` and other scanners
then report CVEs that were already found not to affect the package. Use a VEX
format to keep the statements.

`create` stops with an error when a statement breaks a rule of the VEX format.
For example, OpenVEX requires a justification or a reason for a CVE that does
not affect the package, so a `cve-exclude-list` entry needs one of them.

When you run `create` again, for example after a new build, it writes a new VEX
document with the statements of the manifests and of the `excluded_cves.yaml`
files. To add your own statements again, run `vex update` with your statements
files, see [Updating a VEX file](#updating-a-vex-file).

#### VEX formats

* `openvex` - [OpenVEX][14] 0.2.0. The file is not tied to one SBOM, so it works
  with an SBOM in any format. This matters for SPDX 2.2, which `create` writes
  by default and which has no VEX format of its own.
* `cyclonedx-json` - CycloneDX 1.6. The file is linked to one CycloneDX SBOM and
  works only with that SBOM. It needs `--format cyclonedx-json`.

#### Using a VEX file with check

`check` reads the statements from the SBOM. An SBOM written with
`--vex-format none` or with a VEX format does not contain them, so `check`
reports the CVEs that the statements exclude. Pass the VEX file so that `check`
can use them:

    esp-idf-sbom check --vex app.openvex.json app.spdx

The option can be used more than once. A CycloneDX VEX is refused for any SBOM
other than the one it was created with. A statement that names a package that is
not in the SBOM is ignored with a warning. When the VEX files have more than one
statement about a CVE of a package, the newest statement is used, whatever the
order of the files on the command line. The time of an OpenVEX statement is its
`timestamp`, as in the OpenVEX specification. The time of a CycloneDX statement
is its `lastUpdated`, because `vex update` changes a CycloneDX statement in
place. With the same time, the statement of the file further right on the
command line is used.

A CVE is excluded when a statement says that it does not affect the package, or
that the package has the fix. When a statement says that the CVE affects the
package, `check` reports it, even if a manifest or `excluded_cves.yaml`
excludes it, because the VEX file is the newer document. A statement that the
CVE is under investigation does not change what the scan found.

The scan finds the CVEs that NVD lists for the CPEs of a package. `check` also
reports the CVE of a statement in the SBOM or in the VEX file when the scan did
not find it, for example because NVD has no CPE data for the CVE yet. The report
shows such a CVE without a score and a description. It is excluded or reported
as described above, and a CVE under investigation is reported as a possible
vulnerability.

### The statements file

A statements file is a YAML file with your statements. `vex update` reads it.
The file lists packages, and for each package the statements about its CVEs:

    format-version: 1
    packages:
      - package: cpe:2.3:o:amazon:freertos:10.5.1:*:*:*:*:*:*:*
        vulnerabilities:
          - cve: CVE-2099-1111
            status: not_affected
            justification: vulnerable_code_not_in_execute_path
          - cve: CVE-2099-2222
            status: affected
            detail: The product calls the affected function from the TCP task.
            action: Update to ESP-IDF v5.5.2 in firmware 1.3.
            response: [update]

Use the CPEs and the CVE ids from your own `check` report. The ones above are
only examples.

Use the status and justification values of the format of your VEX document:
the CISA values for OpenVEX, and the CycloneDX values for CycloneDX. Another
format gets the closest value that it has, see
[Statuses and justifications](#statuses-and-justifications).

The keys:

* `package` - the package in the SBOM. The easiest is the CPE that the `check`
  report shows. Part, vendor and product are compared without case, and the
  version only if the CPE has one. These ids work too:
  * a PURL.
  * a CycloneDX BOM-Link, `urn:cdx:<serial>/<version>#<bom-ref>`.
  * an SPDX 3.0.1 element id, `<namespace>#<id>`.
  * an SPDX 2.2 id, `SPDXRef-<id>`.
  * the ref of the package, for example `COMPONENT-freertos`, or its name.
* `vulnerabilities` - the statements about the CVEs of the package:
  * `cve` - the CVE id.
  * `status` - a CISA status or a CycloneDX state.
  * `justification` - why the CVE does not affect the package. Only for
    `not_affected` and `false_positive`.
  * `detail` - why the status applies. In an OpenVEX document, `not_affected`
    and `false_positive` need a `justification` or a `detail`.
  * `action` - what users should do. In an OpenVEX document, `affected` and
    `exploitable` need it.
  * `response` - what you do about the CVE, a list with one or more of
    `can_not_fix`, `will_not_fix`, `update`, `rollback` and
    `workaround_available`. Only a CycloneDX VEX document keeps it. CycloneDX
    strongly recommends it for `affected`.

`cve` is required. A statement changes only the keys that it has, see
[Updating a VEX file](#updating-a-vex-file). So it needs `status` only when the
VEX document has no statement about the CVE for the package. A key with an
empty value removes the field, for example `detail: ''` or `response: []`.

`format-version` at the top of the file is the version of the file format. It
is optional, and the default is 1. esp-idf-sbom stops with an error for a
version that it does not know, so that it does not misread a file in a newer
format.

Unknown keys are ignored, so that a newer version can add keys.

`vex update` writes the keys to these fields of the VEX document:

| Key             | OpenVEX                                | CycloneDX                                        |
|-----------------|----------------------------------------|--------------------------------------------------|
| `package`       | `products`: the PURL and the first CPE | `affects`: a BOM-Link to the package in the SBOM |
| `cve`           | `vulnerability.name`                   | `id`                                             |
| `status`        | `status`                               | `analysis.state`                                 |
| `justification` | `justification`                        | `analysis.justification`                         |
| `detail`        | `impact_statement`                     | `analysis.detail`                                |
| `action`        | `action_statement`                     | `recommendation`                                 |
| `response`      | not written                            | `analysis.response`                              |

OpenVEX gets the CISA values of `status` and `justification`, and CycloneDX gets
the CycloneDX values, see [Statuses and justifications](#statuses-and-justifications).

### Updating a VEX file

`vex update` writes the statements of your statements files into a VEX
document. The whole flow, with a CycloneDX SBOM and VEX document:

Create the SBOM and a separate VEX document:

    esp-idf-sbom create --format cyclonedx-json --vex-format cyclonedx-json \
                        --vex-output app.vex.cdx.json -o app.cdx.json \
                        build/project_description.json

Check the SBOM with the VEX document:

    esp-idf-sbom check --vex app.vex.cdx.json app.cdx.json

Write a statement about each new CVE to a statements file, for example
`statements.yaml`, see [The statements file](#the-statements-file). Then update
the VEX document. The result goes to a new file, so the VEX file you started
with stays as it is:

    esp-idf-sbom vex update --vex app.vex.cdx.json -o app.updated.vex.cdx.json \
                            app.cdx.json statements.yaml

Check the SBOM again, with the updated VEX document. The report shows the VEX
status of each CVE that a statement covers:

    esp-idf-sbom check --vex app.updated.vex.cdx.json app.cdx.json

A statement in a statements file changes the statement of the same CVE for the
same package in the VEX document. It changes only the keys that it has. The
other keys keep their values from the VEX document. For example, a statement
with a new `justification` keeps the `detail` of the VEX document. When you
change the status, also set or remove the keys that described the old status,
for example `detail`.

A statement without `status` keeps the status of the VEX document. For example,
this statement fixes only the `detail`:

    packages:
      - package: cpe:2.3:o:amazon:freertos:10.5.1:*:*:*:*:*:*:*
        vulnerabilities:
          - cve: CVE-2099-2222
            detail: The product calls the affected function from the TCP task.

`vex update` checks each statement that it writes:

* Every statement needs a status. A statements file must give it when the VEX
  document has no statement about the CVE for the package. Without `--vex`, this
  is true for every statement.
* OpenVEX requires a `justification` or a `detail` for `not_affected` and
  `false_positive`, and an `action` for `affected`. CycloneDX has no such rules.

A statement that breaks a rule stops `vex update` with an error. The rules apply
to the statement after all statements files are applied. So the keys that a
rule needs can come from different statements files, for example the `status`
from `product.yaml` and the `detail` from `shared.yaml`.

The statements that no statements file names stay as they are.

With `--report`, `vex update` also prints the statements of the updated VEX
document to stderr, the modified ones first:

    esp-idf-sbom vex update --report --vex app.vex.cdx.json -o app.updated.vex.cdx.json \
                            app.cdx.json statements.yaml

The report starts with a summary: the number of statements, of modified
statements, of changed statuses and of statements that no statements file names.
For each statement, the report gives the CVE, the package with its version and
ids, whether the statement is modified, and whether a statements file has an
entry for it. Each id can be used as `package` in a statements file. A table
below gives for each field the value in the VEX file (Old), the value that the
statements files set (Statement) and the new value (New), with the names of the
format of the VEX document, see
[Statuses and justifications](#statuses-and-justifications). For a changed
field, the old value is red and the new value is green. A new statement is
modified too, and has no old values.

The statements in the SBOM are not written to the VEX document. They stay in
the SBOM, and `check` uses both, see
[Using a VEX file with check](#using-a-vex-file-with-check).

More than one statements file can be given. They are applied in their order on
the command line, from left to right. So for the same key of the same CVE and
package, the value of the file further right is used. In this example,
`product.yaml` wins over `shared.yaml`:

    esp-idf-sbom vex update --vex app.vex.cdx.json -o app.updated.vex.cdx.json \
                            app.cdx.json shared.yaml product.yaml

A statement in a statements file that names a package that is not in the SBOM
is skipped with a warning, for example when its package id has a typo. A
statement in the VEX file for such a package stays as it is, because a VEX file
can cover a whole product line. A statement that names more than one package,
for example two copies of one library with the same CPE, applies to all of them,
also with a warning.

The updated VEX document has the same format and id as the VEX file. If a
statement is new or changed, the version is increased by one and the update time
of the document is set to now. Otherwise the output is the VEX file as it is.

`vex update` changes only what it knows in the VEX file: the version and the
update time of the document, and the status, justification, detail, action,
response and times of the statements. Everything else stays as it is, also the
fields that you added by hand, for example `ratings` in a CycloneDX VEX.
Packages with the same new statement share one statement.

In a CycloneDX VEX, a changed statement is changed in place. It keeps the time
when it was first issued and gets the current time as the time when it was last
updated. A new statement gets the current time as both times. When a statement
names several packages and only some of them change, these packages move to a
copy of the statement.

An OpenVEX document is a history of statements. When it has more than one
statement about a CVE of a package, the newest statement is used. So
`vex update` does not change the statements in an OpenVEX document. For the new
and changed statements, it adds statements with the current time at the end of
the document. Such a statement names only the packages whose statement is new or
changed.

Without `--vex`, `vex update` writes a new VEX document with the statements
from the statements files. `--format` selects its format: `openvex` (the
default) or `cyclonedx-json`, which needs a CycloneDX SBOM.

### Statuses and justifications

OpenVEX and SPDX 3.0.1 use the values that CISA, the US Cybersecurity and
Infrastructure Security Agency, defined for VEX. CycloneDX uses its own values.
The statements file takes the values of both. The manifests and
`excluded_cves.yaml` take the justifications of both.

Use the values of the format that you deliver. A format that does not have a
value writes the closest value that it has. When esp-idf-sbom reads such a file
back, for example in `check`, it gets the value that the format wrote, not the
original one.

#### Statuses

| CISA                  | CycloneDX                | Meaning                                                  |
|-----------------------|--------------------------|----------------------------------------------------------|
| `not_affected`        | `not_affected`           | The CVE does not affect the package.                     |
| `affected`            | `exploitable`            | The CVE affects the package. `action` says what to do.   |
| `fixed`               | `resolved`               | The package has the fix.                                 |
| `under_investigation` | `in_triage`              | It is not known yet whether the CVE affects the package. |
| `not_affected`        | `false_positive`         | The CVE was matched to the package by mistake.           |
| `fixed`               | `resolved_with_pedigree` | The package has the fix, and the SBOM shows the change.  |

In the first four rows, both names mean the same. The last two rows are
CycloneDX states that CISA does not have, so OpenVEX and SPDX 3.0.1 write them
as `not_affected` and `fixed`.

`resolved_with_pedigree` says that the pedigree of the package in the SBOM shows
the change, for example with its commits. esp-idf-sbom does not write a
pedigree, so use this state only when the package in your SBOM has a pedigree
that shows the fix.

The `check` report shows the CISA name of a status, for example `affected` for
`exploitable`. It shows `false_positive` and `resolved_with_pedigree` as they
are.

#### Justifications

A justification says why the CVE does not affect the package. Only
`not_affected` and `false_positive` can have one.

The CISA justifications, and the value that CycloneDX writes for them:

| CISA                                                | Meaning                                                               | CycloneDX writes                  |
|-----------------------------------------------------|-----------------------------------------------------------------------|-----------------------------------|
| `component_not_present`                             | The vulnerable component is not in the product.                       | `code_not_present`                |
| `vulnerable_code_not_present`                       | The component is in the product, but the vulnerable code is not.      | `code_not_present`                |
| `vulnerable_code_not_in_execute_path`               | The vulnerable code is in the product, but the product never runs it. | `code_not_reachable`              |
| `vulnerable_code_cannot_be_controlled_by_adversary` | An attacker cannot control the vulnerable code to exploit the CVE.    | `protected_at_runtime`            |
| `inline_mitigations_already_exist`                  | Built-in protections that users cannot turn off prevent the exploit.  | `protected_by_mitigating_control` |

The CycloneDX justifications, and the value that OpenVEX and SPDX 3.0.1 write
for them:

| CycloneDX                         | Meaning                                                            | OpenVEX and SPDX 3.0.1 write                        |
|-----------------------------------|--------------------------------------------------------------------|-----------------------------------------------------|
| `code_not_present`                | The vulnerable code was removed, for example by the build.         | `vulnerable_code_not_present`                       |
| `code_not_reachable`              | The product never runs the vulnerable code.                        | `vulnerable_code_not_in_execute_path`               |
| `requires_configuration`          | The exploit needs a configuration that the product does not have.  | `vulnerable_code_cannot_be_controlled_by_adversary` |
| `requires_dependency`             | The exploit needs a dependency that is not present.                | `vulnerable_code_cannot_be_controlled_by_adversary` |
| `requires_environment`            | The exploit needs an environment that is not present.              | `vulnerable_code_cannot_be_controlled_by_adversary` |
| `protected_by_compiler`           | The compiler flags of the build prevent the exploit.               | `inline_mitigations_already_exist`                  |
| `protected_at_runtime`            | Something prevents the exploit while the product runs.             | `vulnerable_code_cannot_be_controlled_by_adversary` |
| `protected_at_perimeter`          | Attacks are blocked at the physical, logical or network perimeter. | `vulnerable_code_cannot_be_controlled_by_adversary` |
| `protected_by_mitigating_control` | Other measures reduce the chance or the impact of an exploit.      | `inline_mitigations_already_exist`                  |

For example, when a statements file has `component_not_present` and the VEX
document is in the CycloneDX format, `vex update` writes `code_not_present`
into the CycloneDX VEX document.


## Usage example

This is an example of basic usage for the blink project, which is part of the ESP-IDF.
It's expected that ESP-IDF is installed and set.

    $ cd examples/get-started/blink/ # In esp-idf directory go to the blink example
    $ idf.py build                   # Project has to be built first
    $ esp-idf-sbom create -o blink.spdx build/project_description.json
    $ esp-idf-sbom check blink.spdx

    $ esp-idf-sbom create build/project_description.json | esp-idf-sbom check

The resulting `blink.spdx` sbom file can be found in the `blink` project directory.


## SBOM layout

The SBOM file is created based on application sources, build artefacts, information
provided by the ESP-IDF build system and SBOM manifest files. The resulting SBOM
describes a package for the final **project** application, the **framework**
(ESP-IDF itself), the used **toolchain**, the **components** used during build, git
**submodules** and **subpackages**. The **framework** package carries the ESP-IDF
version and the CPEs Espressif uses in NVD advisories (the application CPE plus the
generic `esp32` hardware and `esp32_firmware` OS CPEs); the **project** package
depends on it instead of carrying the ESP-IDF CPE directly. The **subpackages**
are created based on `sbom.yml` manifest files found in **submodules** and **subpackages**
sub-directories or referenced manifest files or manifest files for virtual packages.
Please see [Manifest file](#manifest-file).

Each package carries, as available, its name, version, supplier, repository, CPE(s),
PURL, license, copyright and the list of excluded CVEs. Packages are linked together
with dependency relationships rooted at the **project** package. By default packages for
configuration only components and components not linked into the application are present
in the SBOM, but are not linked as dependencies of the project. In other words
dependencies on such packages are removed. This behaviour can be altered with
`--add-config-deps` and `--add-unused-deps` command line options.


## Output formats

The same SBOM model is serialized into the format selected with the `create --format`
option (see [Creating SBOM](#creating-sbom)). The `check` command reads any of them and
detects the format automatically. The model concepts map to each format as follows.

| Concept       | SPDX 2.x                               | SPDX 3.0 JSON-LD                             | CycloneDX                            |
|---------------|----------------------------------------|----------------------------------------------|--------------------------------------|
| package       | SPDX package                           | `software_Package`                           | component                            |
| dependency    | `DEPENDS_ON` relationship              | `dependsOn` relationship                     | `dependencies` entry                 |
| name          | `PackageName`                          | `name`                                       | `name`                               |
| version       | `PackageVersion`                       | `software_packageVersion`                    | `version`                            |
| supplier      | `PackageSupplier`                      | `suppliedBy` (Agent)                         | `supplier.name`                      |
| originator    | `PackageOriginator`                    | `originatedBy` (Agent)                       | `publisher`                          |
| description   | `PackageSummary`                       | `description`                                | `description`                        |
| repository    | `ExternalRef OTHER repository`         | `externalRef` (vcs)                          | `externalReferences` (vcs)           |
| download URL  | `PackageDownloadLocation`              | `software_downloadLocation`                  | `externalReferences` (distribution)  |
| CPE           | `ExternalRef SECURITY cpe23Type`       | `cpe23` externalIdentifier                   | `cpe` (+ `evidence.identity`)        |
| PURL          | `ExternalRef PACKAGE-MANAGER purl`     | `software_packageUrl`                        | `purl`                               |
| license       | `PackageLicenseDeclared`               | `hasDeclaredLicense`                         | `licenses`                           |
| scanned license | `PackageLicenseConcluded`            | `hasConcludedLicense`                        | `evidence.licenses`                  |
| copyright     | `PackageCopyrightText`                 | `software_copyrightText`                     | `copyright`                          |
| scanned copyright | `PackageAttributionText`           | `software_attributionText`                   | `evidence.copyright`                 |
| custom-licenses | `LicenseID` / `ExtractedText`        | `expandedlicensing_CustomLicense`            | not available                        |
| checksum      | `PackageChecksum`                      | `verifiedUsing` (Hash)                       | `hashes`                             |
| excluded CVEs | `PackageComment`                       | `security_Vulnerability` + VEX               | `not_affected` VEX `vulnerabilities` |
| cve-keywords  | `PackageComment`                       | `comment` (YAML)                             | `properties`                         |

The concepts above belong to individual packages. The document as a whole carries two
more, set from the `document` key of the project manifest (see
[Manifest file](#manifest-file)).

| Concept               | SPDX 2.x           | SPDX 3.0 JSON-LD           | CycloneDX               | VEX                                                 |
|-----------------------|--------------------|----------------------------|-------------------------|-----------------------------------------------------|
| document supplier     | not available      | not available              | `metadata.supplier`     | not written                                         |
| document manufacturer | `Creator:`         | `CreationInfo.createdBy`   | `metadata.manufacturer` | CycloneDX `metadata.manufacturer`, OpenVEX `author` |

The two are not symmetric across formats. SPDX has no document level slot for the supplier
of the described product, its nearest equivalent being the root package's own
`PackageSupplier`, so only the manufacturer is recorded there. In SPDX 3.0 the manufacturer
becomes a `Person` or an `Organization` agent, by the prefix of its name. Its `contact` and
`url`, if set, become `email` and `urlScheme` external identifiers. In SPDX 2.x the contact
is folded into the `Creator` value as `name (email)`, and the `url` is not written.

Without `document manufacturer`, only the tool is named as the creator. OpenVEX requires an
author, so it writes `Unknown Author`. Espressif made the tool, so it is never named as the
creator or the author.

## Manifest file

During SBOM generation the esp-idf-sbom tool is looking for `sbom.yml` manifest files.
They are used as a source of information for the corresponding package in the SBOM file
as described above.

The manifest file may be present at root of **project**, **component**, **submodule** or
in any of their sub-directories. If `sbom.yml` is found in a sub-directory a new **subpackage**
package is created and linked with the parent package. This can be used in cases
where e.g. one **component** contains multiple libraries and they should be represented
by separate packages.

Example of multiple `sbom.yml` files usage for the `console` component.

    console
    ├── argtable3
    │   └── sbom.yml
    ├── linenoise
    │   └── sbom.yml
    └── sbom.yml

The `esp-idf-sbom` tool will create main console **component** package, which will
contain two **subpackages** for `argtable3` and `linenoise` libraries. Please note that
the manifest file in the `console` component root directory is not necessary to create
a package, because `esp-idf-sbom` automatically creates a package for each
**component**. The `sbom.yml` files may be placed at any sub-directory depth and
`esp-idf-sbom` will create proper package hierarchy for them.

The `sbom.yml` is a simple yaml file, which may contain the following entries.

* **name**:
    Package name that will be used in the package.
* **version**:
    Package version.
* **description**:
    Short package description.
* **repository**:
    Link to git repository.
* **url**:
    Link to package download location.
* **cpe**:
    CPE used for vulnerabilities check against NVD. This can be single CPE value or a
    list of CPEs.
* **purl**:
    [Package URL][8] (PURL) identifying the source of the package (see
    [Output formats](#output-formats)). Like `cpe`, the value supports the
    `{}` placeholder which is substituted with the package `version`, so the PURL
    template is written once and a version bump only touches the `version` field:

    ```
    name: zlib
    version: 1.3.2
    cpe: cpe:2.3:a:zlib:zlib:{}:*:*:*:*:*:*:*
    purl: pkg:github/madler/zlib@{}
    ```

    Only a single PURL per package is supported. PURL identifies the *source* of
    the artifact (one URL per package), while the multi-value `cpe` list already
    covers the case where the same software is tracked in NVD under multiple
    vendor names.

    When `purl` is not set explicitly, a PURL is auto-derived from the commit the
    package was built from, with the package path inside the repository as the
    PURL subpath:

    ```
    pkg:github/espressif/esp-idf@<sha>#components/heap
    ```

    The commit comes from the checkout, or for a managed component from the
    `repository_info` the IDF Component Registry writes into `idf_component.yml`.
    The `github` and `gitlab` PURL types define the version as a commit or tag,
    and a package version is usually neither, so it is not used when a commit is
    known. If no commit is known, the package version is used with `url`
    preferred over `repository`. This applies to the toolchain, whose version is
    a release tag.

    Only `github.com` and `gitlab.com` repository root URLs are recognised. Other
    hosts (e.g. `https://www.lua.org/`) need an explicit `purl`. Virtual packages
    never auto-derive from a commit, because they own no directory and would get
    the PURL of the component that declares them.

    The explicit `purl` value also acts as an override for forks where the
    auto-derived "where we built from" PURL is not what downstream OSV/GHSA-style
    scanners would query (e.g. for `mbedtls`, the resolved submodule URL is
    `github.com/espressif/mbedtls` but advisories are indexed against the
    upstream `github.com/Mbed-TLS/mbedtls`):

    ```
    sbom-purl = pkg:github/Mbed-TLS/mbedtls@{}
    ```
* **supplier**:
    Package supplier. Person or organization distributing the package. The value has to
    start with *Person:* or *Organization:*, as the SPDX specification requires, and SBOM
    creation fails without it. An email address can be added in parentheses after the
    name, for example `Organization: Acme Corp (psirt@acme.example)`. SPDX keeps the
    value as it is and CycloneDX reports the email as the contact of the entity.
* **originator**:
    Package originator. If the package comes from another person or organization
    that has been identified as a supplier. For example if a component is based
    on 3rd party code with some modifications, the originator is the 3rd party code
    author, but the supplier is the person or organization distributing the final
    component. For more detailed information please see the SPDX specification.
    The value has the same form as `supplier`, so it has to start with *Person:* or
    *Organization:* and can carry an email address in parentheses.
* **hash**:
    SHA of the directory(`git-tree` object) the manifest file describes or HEAD SHA of a submodule. This value
    is used during the manifest file validation to check if the hash in the manifest file matches the
    SHA recorded in the `git-tree`. Its purpose is to make sure that the information in the manifest
    file is up-to-date. For example if a submodule or 3rd party library is updated, the
    version in the manifest file should be probably updated too. The SHA value can be obtained
    e.g. with `git ls-tree HEAD <path>`, where `<path>` is a package directory, which is described by
    the manifest file. Please note that a hash value for a directory, not a submodule, cannot
    be placed in manifest file, which is stored within the same directory, because the directory SHA
    in `git-tree` will change every time the manifest file changes(chicken egg problem). To make
    the hash variable work, it needs to be placed in a referenced manifest, which is not stored
    within a directory it describes. For example freertos component can have main `sbom.yml`
    manifest file, which refers to `sbom_FreeRTOS-Kernel.yml` manifest describing the `FreeRTOS-Kernel`
    package in the `FreeRTOS-Kernel` directory.

components/freertos/sbom.yml
```
manifests:
  - path: sbom_FreeRTOS-Kernel.yml
    dest: FreeRTOS-Kernel
```
components/freertos/sbom_FreeRTOS-Kernel.yml
```
name: 'freertos'
version: '10.4.3'
cpe: cpe:2.3:o:amazon:freertos:{}:*:*:*:*:*:*:*
supplier: 'Organization: Espressif Systems (Shanghai) CO LTD'
originator: 'Organization: Amazon Web Services'
description: An open-source, real-time operating system (RTOS) with additional features and patches from Espressif.
hash: 4e8101b6f57a0640ae54c6da605c1b532c0f8f89
cve-exclude-list:
  - cve: CVE-2021-43997
    reason: This vulnerability only affects ARMv7-M and ARMv8-M ports of FreeRTOS and hence does not affect Espressif SoCs which are not based on these architectures.
```

* **license**:
    License expression explicitly declared by the author. It has to be a valid SPDX license
    expression, meaning license identifiers from the [SPDX license list][15] combined with
    `AND`, `OR` and `WITH`. The expression is validated and SBOM creation fails if it is
    not valid. A license that is not on the SPDX license list is used as `LicenseRef-<id>`
    and described with the **custom-licenses** key below.
* **copyright**:
    Copyright explicitly declared by the author. This can be single string or a
    list of copyrights.
* **custom-licenses**:
    Description of licenses that are not on the [SPDX license list][15]. Such a license is
    referred to as `LicenseRef-<id>` in a license expression, and the SBOM has to say what
    the identifier means. Each entry is a dictionary with the following keys.

    * id: the full identifier, for example `LicenseRef-Acme-Proprietary`
    * name: license name
    * text: license text
    * file: file with the license text, relative to the manifest directory
    * url: where the license is published
    * comment: any additional note

    Use either `text` or `file`, not both. A license used in a license expression but not
    described here is still added to the SBOM, with `NOASSERTION` instead of the text.
    Describing a license that no expression uses is allowed.

```
license: LicenseRef-Acme-Proprietary AND MIT
custom-licenses:
  - id: LicenseRef-Acme-Proprietary
    name: Acme Proprietary License
    file: LICENSE
    url: https://acme.example/license
```

* **cve-exclude-list**:
    List of CVEs that do not affect this package. `create` writes each entry as a
    statement that the CVE does not affect the package, see
    [Statements in manifests](#statements-in-manifests).

    * cve: CVE-ID
    * reason: why this package is not affected
    * justification (optional): a CISA or a CycloneDX value, see
      [Statuses and justifications](#statuses-and-justifications).
```
      version: 0.1.0
      description: Blink application example
      cve-exclude-list:
        - cve: CVE-2023-1234
          reason: Description why this package is not vulnerable
          justification: vulnerable_code_not_present
```

* **cve-keywords**:
    When using the `--extended-scan` option to run the checker, look for these keywords
    in the CVE descriptions. These keywords are also included in the generated
    SBOM file.
```
      cve-keywords:
        - mbedtls
        - mbed tls
```

* **manifests**:
    List of manifests which cannot be added directly into the **component** or **submodule**
    sub-directories to create **subpackage**. For example the following will create a new
    package for the `subpackage` directory with information from the `subpackage.yml` manifest file.
    This manifest file is treated as it would be actually stored in the `subpackage` directoery.

    * path: path of manifest file relatitve to the sbom.yml
    * dest: destination directory for path, again relative to sbom.yml
```
      version: 0.1.0
      description: Blink application example
      manifests:
        - path: subpackage.yml
          dest: subpackage
```

    Alternatively, the content of `subpackage.yml` can be directly included as a **manifest** dictionary.

    * manifest: a dictionary that holds information about the subpackage manifest
    * dest: destination directory for manifest, relative to sbom.yml
```
      version: 0.1.0
      description: Blink application example
      manifests:
        - manifest:
            name: 'sub-package'
            version: '1.2.3'
            supplier: 'Organization: Espressif Systems (Shanghai) CO LTD'
            description: 'Some useful description'
          dest: subpackage
```

* **virtpackages**:
    In some situations, it can be beneficial to define a relationship or dependency
    on a package that isn't physically available during compilation. For example,
    certain functionality required by an application might be implemented in ROM
    and used by the application. A case in point is mbedtls, which could reside in
    the ROM and be utilized by the application depending on configuration settings.
    In these instances, a virtual package can be useful to represent this
    dependency.
```
      virtpackages:
        - virt_pkg1.yml
        - virt_pkg2.yml
```

* **if**:
    An expression utilizing the configuration variables can conditionally
    include a package. This condition is disregarded for **project** and
    **component** packages, which are always included. The operators listed
    below, in order of decreasing precedence, can be used. The order of
    operations can be adjusted using brackets. If the expression evaluates to
    **true**, the package is included. The **--disable-conditions** option can
    disable conditions usage.

    - **!**: negation
    - **<**, **>**, **>=**, **<=**: comparison
    - **=**, **!=**: equal, not equal
    - **&&**: logical and
    - **||**: logical or
```
      if: 'IDF_TARGET = "esp32"'
      if: '(IDF_TARGET_ESP32 || IDF_TARGET = "esp32") && !IDF_TOOLCHAIN_GCC'
      if: '1536 <= FREERTOS_ISR_STACKSIZE < 0xffff'
```

* **document**:
    Information about the generated SBOM document itself, rather than about the
    package whose directory holds the manifest. This is the only key that does not
    describe a package. It is read from the **project** manifest only, and has no
    effect in any other manifest. It lives in `sbom.yml` for convenience, so that
    no separate document metadata file is needed.

    Two organizations can be described, both optional.

    * **supplier**: the organization supplying the product this SBOM describes,
      stating at document level what the project package's `supplier` states about
      the described product. As there, this is whoever delivers the product to its
      recipient, which need not be whoever wrote it. When an application is
      redistributed, for example built by one organization and then shipped by
      another under its own name, the redistributing organization is the supplier
      and the original author belongs in the project package's `originator`. This
      is also the organization a recipient is expected to contact about a
      vulnerability in the delivered product.
    * **manufacturer**: the organization that produced the SBOM document. This is
      not the vendor of the `esp-idf-sbom` tool, which is recorded separately among
      the document's tools; it is the organization running the tool.

    For an organization building and shipping its own firmware the two are the same.
    They differ when the document is produced by someone other than the party
    delivering the product, for example when the original builder generates the SBOM
    for an application that another organization then redistributes. Each takes the
    following entries.

    * **name**: has to start with *Person:* or *Organization:*, as for `supplier`. A
      plain name is rejected.
    * **url**: link to the organization, for example its home or security page.
    * **contact**: email address for reporting vulnerabilities.

    `name` is required, together with at least one of `url` or `contact`. A name on
    its own identifies the organization but gives no way to reach it, which is what
    consumers checking an SBOM for a reachable vulnerability contact are looking for.

```
      document:
        supplier:
          name: 'Organization: Acme Corp'
          url: 'https://acme.example'
          contact: 'psirt@acme.example'
        manufacturer:
          name: 'Organization: Acme Corp'
          url: 'https://acme.example'
          contact: 'psirt@acme.example'
```

Example of the `sbom.yml` manifest file for the ESP-IDF blink project.

    version: 0.1.0
    description: Blink application example
    url: https://blink.org/blink-0.1.0.tar.gz # non-existing package download URL example
    cpe: cpe:2.3:a:hrbata:blink:{}:*:*:*:*:*:*:* # non-existing CPE example
    supplier: 'Person: Frantisek Hrbata (frantisek.hrbata@espressif.com)'
    originator: 'Organization: Espressif Systems (Shanghai) CO LTD'
    cve-exclude-list:
       - cve: CVE-2023-1234
         reason: Description why this package is not vulnerable
       - cve: CVE-2023-1235
         reason: Description why this package is not vulnerable


Information from the `sbom.yml` manifest file is mapped to the following SPDX tags and
CycloneDX fields.

| manifest         | SPDX                         | CycloneDX                         |
|------------------|------------------------------|-----------------------------------|
| name             | PackageName                  | name                              |
| version          | PackageVersion               | version                           |
| description      | PackageSummary               | description                       |
| repository       | ExternalRef OTHER repository | externalReferences (vcs)          |
| url              | PackageDownloadLocation      | externalReferences (distribution) |
| cpe              | ExternalRef cpe23Type        | cpe (+ evidence.identity)         |
| supplier         | PackageSupplier              | supplier.name                     |
| originator       | PackageOriginator            | publisher                         |
| license          | PackageLicenseDeclared       | licenses                          |
| copyright        | PackageCopyrightText         | copyright                         |
| scanned license  | PackageLicenseConcluded      | evidence.licenses                 |
| scanned copyright | PackageAttributionText      | evidence.copyright                |
| custom-licenses  | LicenseID / ExtractedText    | licenses (name, text, url)        |
| cve-exclude-list | PackageComment               | not_affected VEX                  |
| cve-keywords     | PackageComment               | properties                        |

The `document` key is the exception, as it describes the document rather than a package
and is read from the project manifest only.

| manifest              | SPDX          | CycloneDX               |
|-----------------------|---------------|-------------------------|
| document.supplier     | not available | metadata.supplier       |
| document.manufacturer | Creator       | metadata.manufacturer   |

Even though the `sbom.yml` file is the primary source of information, the esp-idf-sbom tool
is also looking at other places if it's not present. The `idf_component.yml` manifest file,
used for components managed by the component manager, may contain `sbom` dictionary/namespace,
which will be used by esp-idf-sbom if presented. This dictionary may contain the same information
as `sbom.yml`.

Example of the `idf_component.yml` manifest file for led_strip managed component.

    dependencies:
      idf:
        version: '>=5.0'
    description: Driver for Addressable LED Strip (WS2812, etc)
    url: https://github.com/espressif/idf-extra-components/tree/master/led_strip
    version: 2.4.1
    sbom:
      cpe: cpe:2.3:a:hrbata:led_strip:{}:*:*:*:*:*:*:* # non-existing CPE example
      supplier: 'Organization: Espressif Systems (Shanghai) CO LTD'
      cve-exclude-list:
         - cve: CVE-2023-1234
           reason: Description why this package is not vulnerable
         - cve: CVE-2023-1235
           reason: Description why this package is not vulnerable

If the `sbom` dictionary is not presented in `idf_component.yml` or it's missing some information,
the version, description, maintainers and url information from `idf_component.yml` manifest is used.

Information from the `idf_component.yml` manifest file is mapped to the following SPDX tags
and CycloneDX fields.

| manifest    | SPDX                    | CycloneDX                         |
|-------------|-------------------------|-----------------------------------|
| name        | PackageName             | name                              |
| version     | PackageVersion          | version                           |
| description | PackageSummary          | description                       |
| maintainers | PackageSupplier         | supplier.name                     |
| url         | PackageDownloadLocation | externalReferences (distribution) |

Component version may be guessed based on git-describe and Espressif
as a default supplier may be guessed based on git repository or package URL. The guessing
may be disabled by using the '--no-guess' option.

For git submodules, the `.gitmodules` configuration file is also checked for additional submodule
information. Submodule configuration may contain variables with the `sbom-` prefix, which are considered as
SBOM manifest information. All keys used in the `sbom.yml` manifest file can also be specified in
`.gitmodules` with the `git-config` format instead of yaml.

Example of manifest information added for the `micro-ecc` submodule in `.gitmodules`.

    [submodule "components/bootloader/subproject/components/micro-ecc/micro-ecc"]
            path = components/bootloader/subproject/components/micro-ecc/micro-ecc
            url = ../../kmackay/micro-ecc.git
            sbom-version = 1.0
            sbom-cpe = cpe:2.3:a:micro-ecc_project:micro-ecc:{}:*:*:*:*:*:*:*
            sbom-supplier = Person: Ken MacKay
            sbom-url = https://github.com/kmackay/micro-ecc
            sbom-description = A small and fast ECDH and ECDSA implementation for 8-bit, 32-bit, and 64-bit processors
            sbom-hash = d037ec89546fad14b5c4d5456c2e23a71e554966
            sbom-cve-exclude-list = CVE-2023-1234 Description why this package is not vulnerable
            sbom-cve-exclude-list = CVE-2023-1235 Description why this package is not vulnerable


Manifest information is gathered in the following order and only missing
values are added. If e.g. `version` is found in `sbom.yml` any other
`version` value found e.g. in `.gitmodules` is ignored.

1. referenced manifest from parent package
2. `sbom.yml`
3. `sbom` dictionary/namespace in `idf_component.yml`
4. sbom information contained in submodule configuration in `.gitmodules`
5. `idf_component.yml` information provided for component manager


### Validating manifest files

Manifest files are validated while the SBOM is generated. They can be also validated explicitly
with the `esp-idf-sbom manifest validate` command.

    esp-idf-sbom manifest validate [PATH_TO_VALIDATE...]

`PATH_TO_VALIDATE` is an optional path to a manifest file(sbom.yml, idf_component.yml or .gitmodules) or
directory, which will be searched for manifest files. If `PATH_TO_VALIDATE` is not provided, the current
working directory is used.

Usage example:

    $ esp-idf-sbom manifest validate ~/work/esp-idf ~/work/idf-extra-components/
    $ esp-idf-sbom manifest validate ~/work/esp-idf/.gitmodules ~/work/esp-idf/components/freertos/FreeRTOS-Kernel/sbom.yml


### Checking manifest files for vulnerabilities

The `esp-idf-sbom` tool uses the generated SBOM file to check for possible vulnerabilities.
It also allows to scan for vulnerabilities based on the information presented in manifest
files. This can be used e.g. to scan a whole repository without a need to generate the SBOM file.

    esp-idf-sbom manifest check [PATH_TO_CHECK...]

`PATH_TO_CHECK` is an optional path to a manifest file(sbom.yml, idf_component.yml or .gitmodules) or
directory, which will be searched for manifest files. If `PATH_TO_CHECK` is not provided, the current
working directory is used.

Usage example:

    $ esp-idf-sbom manifest check ~/work/esp-idf ~/work/idf-extra-components/
    $ esp-idf-sbom manifest check ~/work/esp-idf/.gitmodules ~/work/esp-idf/components/freertos/FreeRTOS-Kernel/sbom.yml
    # Use a local NVD mirror for vulnerability checks.
    $ esp-idf-sbom manifest check --local-db ~/work/esp-idf ~/work/idf-extra-components/


## Describing your own project

ESP-IDF and managed components should already carry SBOM data. Your own code does not.
Add a small `sbom.yml` so the project and each of your components have an identifier and
a license. SBOM validators, for example the EU Cyber Resilience Act profile, expect every
component to have both.

### The project

Put a `sbom.yml` in the project root, next to the top `CMakeLists.txt`. Describe the
application, and describe the SBOM document itself with the `document` key.

    # <project>/sbom.yml
    version: 1.0.0
    description: ACME thermostat firmware
    supplier: 'Organization: ACME Corp (psirt@acme.example)'
    license: LicenseRef-Proprietary
    purl: pkg:generic/acme/thermostat@{}
    custom-licenses:
      - id: LicenseRef-Proprietary
        name: ACME Proprietary License
        file: LICENSE
    document:
      manufacturer:
        name: 'Organization: ACME Corp'
        contact: 'psirt@acme.example'

### Your components

Put a `sbom.yml` in the component directory, for `main` and for every component you
wrote. Give it an identifier and a license.

    # main/sbom.yml
    license: LicenseRef-Proprietary
    purl: pkg:generic/acme/thermostat-main@{}

`purl` is an identifier some validators require, for example the Cyber Resilience Act
profile. It is not the only one. The esp-idf-sbom `check` command matches vulnerabilities
by CPE, not by `purl`. So when a component has a CPE in the NVD, define `cpe` as well.
`{}` in `purl` is replaced with the component version. `license` is the license. Use an
SPDX identifier for a standard license, or `LicenseRef-<id>` for your own, defined once
with `custom-licenses` in the project manifest and then known to the whole document. For
the other keys a manifest can hold, see [Manifest file](#manifest-file).


## Licenses and Copyrights

Adding licenses and copyrights information into the SBOM file has to be explicitly
requested by using the `--file-tags` option. This requires to scan all files and
may take some time. It also may result in a quite big SBOM file.

All **component** and **submodule** files are scanned for the `SPDX-License-Identifier`,
`SPDX-FileCopyrightText` and `SPDX-FileContributor` SPDX file tags. Information from
these tags is used in the generated SBOM file to specify licenses and copyrights for
the packages which represent **project**, **component** or **submodule**. The project's
final license expression is a logical AND of all licenses concluded from **components**
and **submodules** used in the final project binary.

The license can be also explicitly declared by the author in the `sbom.yml` file with the `license`
variable. This information is used as the declared license of the
given **project**, **component** or **submodule** (see [Output formats](#output-formats)).
The same holds for the `copyright` variable.

What the author declares and what the file scan finds are kept apart. The declared license
and copyright describe the package itself. The license and the copyright notices collected
from the files describe the code the package carries, which usually belongs to somebody
else, so they are reported separately.

* SPDX keeps the declared license in `PackageLicenseDeclared` and the scanned one in
  `PackageLicenseConcluded`. The declared copyright is the `PackageCopyrightText` and the
  scanned notices are written as `PackageAttributionText`.
* CycloneDX 1.6 holds one license expression per component, so the declared license and
  copyright stay in `licenses` and `copyright`, and the scanned ones are reported under
  `evidence`. Both carry the `acknowledgement` field saying which kind they are.

With nothing declared, the scanned license and copyright are all there is, so they are
reported as the license and the copyright of the package, and nothing is reported
separately.

A license that is not on the [SPDX license list][15] is referred to as `LicenseRef-<id>`,
both in the `license` variable and in the `SPDX-License-Identifier` file tag. The SPDX
formats require the document to say what such an identifier means, so every one of them is
described in the SBOM. Use the `custom-licenses` manifest key to provide the license text
and name. Without it the license is reported with `NOASSERTION`.

CycloneDX reports the name, the text and the url as a license object when a license field
is a single license or a plain `AND` of licenses. This is done for the declared license and
for the scan results in `evidence`. An `OR` or a `WITH` expression has no license object, so
there the license keeps its identifier only.


## Return Values

* **0**:
    No error, no vulnerability found, manifest file(s) valid.
* **1**:
    Vulnerability found or manifest file not valid.
* **128**:
    Fatal error.


## Resources

* DevCon24 presentation - [Enhancing IoT Security Generating SBOMs with esp-idf-sbom][7]


[1]: https://en.wikipedia.org/wiki/Software_supply_chain
[2]: https://spdx.dev
[3]: https://docs.espressif.com/projects/esp-idf/en/latest
[4]: https://nvd.nist.gov
[5]: https://en.wikipedia.org/wiki/Common_Platform_Enumeration
[6]: https://github.com/espressif/esp-nvd-mirror
[7]: https://www.youtube.com/watch?v=ODZwxKawVXo
[8]: https://github.com/package-url/purl-spec
[9]: https://nvd.nist.gov/developers/request-an-api-key
[10]: https://nvd.nist.gov/developers/start-here
[11]: https://cyclonedx.org
[12]: https://www.cisa.gov/known-exploited-vulnerabilities-catalog
[13]: https://www.cisa.gov/sites/default/files/2023-04/minimum-requirements-for-vex-508c.pdf
[14]: https://openvex.dev
[15]: https://spdx.org/licenses/
