CycloneDX and SPDX Software Bill of Materials (SBOM) for OCaml
README.md

sbom #

Software bills of materials in CycloneDX 1.6 and SPDX 2.3 for OCaml.

A software bill of materials (SBOM) lists the components a piece of software is built from, with their versions, licences and digests, so that a customer or an auditor can check what shipped. The two formats in use are SPDX, an ISO standard from the Linux Foundation, and CycloneDX, from OWASP. sbom gives typed OCaml values for both, JSON codecs through jsont, a converter from SPDX to CycloneDX that reports every fact it cannot carry across, and an SPDX licence-expression checker for deny-list policies.

Installation #

Install with opam:

$ opam install sbom

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install sbom

Usage #

An SPDX 2.3 document with one package, serialised to JSON:

let doc =
  let info =
    Spdx.creation_info ~created:"2025-01-01T00:00:00Z"
      ~creators:["Tool: ocaml-sbom"] ()
  in
  let pkg =
    Spdx.package ~spdx_id:"SPDXRef-pkg"
      ~name:"my-library" ~version:"1.0.0"
      ~download_location:"https://example.com/my-library-1.0.0.tar.gz"
      ~license_declared:"ISC" ()
  in
  Spdx.document ~name:"my-sbom"
    ~namespace:"https://example.com/sbom/my-sbom"
    ~creation_info:info ~packages:[pkg] ()

let json = Spdx.to_string doc

The same library as a CycloneDX 1.6 component, identified by its Package URL:

let bom =
  let comp =
    Cyclonedx.component ~typ:Library ~name:"my-library" ~version:"1.0.0"
      ~purl:(Sbom.purl ~typ:"opam" ~name:"my-library" ~version:"1.0.0" ())
      ~licenses:[Sbom.license_id "ISC"] ()
  in
  Cyclonedx.bom [comp]

let json = Cyclonedx.to_string bom

API #

The package has six libraries and one command.

  • sbom (module Sbom) holds the types both formats share: hash algorithms and digests, licence references, Package URLs and the parse error.
  • sbom.spdx (module Spdx) models SPDX 2.3 documents: creation information, packages, files, checksums, external references and relationships.
  • sbom.cyclonedx (module Cyclonedx) models CycloneDX 1.6 BOMs: components, including files, data and cryptographic assets, metadata and the dependency graph.
  • sbom.convert (module Sbom_convert) converts the SPDX packages a caller selects, and the dependencies between them, to CycloneDX. It returns a diagnostic for every relationship or checksum that CycloneDX cannot carry exactly, and refuses duplicate or dangling identifiers.
  • sbom.license (module Sbom_license) parses SPDX licence expressions and checks them, or every package of a document, against a deny list.
  • sbom.validate (module Sbom_validate) checks a CycloneDX 1.6 JSON document against the 1.6.1 schema bundled from spec/cyclonedx-1.6, then checks that its bom-refs are unique and that every reference from its dependencies and compositions names one. The sbom-validate FILE command runs both checks and prints each error with its JSON Pointer.

Sbom_license.check_expression treats an expression it cannot parse as compliant. Parse with Sbom_license.parse first when a malformed expression must be refused.

References #

License #

ISC. See LICENSE.md.