Certenov0.19.0

Verify PDF signatures in Node.js with Certeno

In this tutorial you write a small Node.js script that reads a signed PDF, verifies every digital signature in it, and prints one line per signature. There is no viewer and no browser: the same engine that runs in the viewer runs inside your Node process.

You will:

  • create a Node project and install Certeno,
  • write a script that verifies a PDF and prints the result,
  • add your licence, which a server or script needs before it starts.

Prerequisites

  • Node.js 20.19 or later and npm.
  • Access to the Certeno registry. Certeno's packages are served from a private registry. Installation shows how to point the @trexolab scope at it.
  • A PDF with a digital signature, to see something to verify.
  • A Certeno Server licence, for step 4. You need its licence key (it starts with CERT-). If you do not have one yet, request an evaluation or a production licence on the contact page. You can do steps 1 to 3 without it.

Tip: You can try the verification itself, with no licence, in the live demo before you set anything up.


1. Create a Node project

  1. Create a folder and a package.json in it:

    bash
    mkdir verify-pdf && cd verify-pdf
    npm init -y
  2. Turn on ES modules, so the script can use import and top-level await:

    bash
    npm pkg set type=module

2. Install Certeno

  1. Point the @trexolab scope at the Certeno registry, in a .npmrc file at the project root:

    bash
    echo '@trexolab:registry=https://certeno.trexolab.com/api/registry' > .npmrc
  2. Install the core package. It contains the verification engine:

    bash
    npm install @trexolab/certeno-core

There is nothing else to set up. The engine is a WebAssembly module inside the package, so you do not compile anything or copy a .wasm file.


3. Verify a PDF

  1. Create verify.mjs:

    js
    import { createVerifier } from '@trexolab/certeno-core'
    import { readFile } from 'node:fs/promises'
     
    const path = process.argv[2]
    if (!path) {
      console.error('Usage: node verify.mjs <file.pdf>')
      process.exit(1)
    }
     
    // createVerifier() is async: it starts the engine.
    const verifier = await createVerifier()
    try {
      const result = await verifier.verify(await readFile(path), path)
     
      if (result.signatures.length === 0) {
        console.log(`${path} has no signatures.`)
      } else {
        console.log(`${result.signatures.length} signature(s) in ${path}`)
        for (const sig of result.signatures) {
          console.log(`- ${sig.name}: ${sig.overallStatus} (${sig.overallMessage})`)
        }
      }
    } finally {
      // Release the engine, even when verify() throws.
      verifier.dispose()
    }
  2. Run it on your signed PDF:

    bash
    node verify.mjs signed.pdf

    You should see a line for the file and one line for each signature, such as:

    text
    1 signature(s) in signed.pdf
    - Jane Example: warning (Signature is VALID with warnings — signer's identity could not be verified against a trusted root.)

    The name and the message depend on your PDF. Each signature gets one overall status: valid, warning, invalid or unknown. The line above is what a signature that is intact, but whose certificate does not chain to a root Certeno trusts, looks like. That is expected for a test certificate; a signature from a trusted authority reads valid.

verify() returns much more than this: eight separate checks per signature (integrity, signature, certificate chain, expiry, timestamp, revocation, algorithm and key usage), the signer's certificate and the PAdES level. Examples prints all of it.

Note: Without the licence option, createVerifier() does no licence check. A server or script that you run for your own users needs a Server licence, which the next step adds.


4. Add your licence

A Server licence belongs to your machines. Certeno activates the machine the first time the script runs, and checks the licence again every 6 hours while a long-running process works. The verifier does not start unless the licence is valid and includes the node_runtime and signature_verification features.

  1. Install the licence server's SDK. Certeno loads it only when you pass licence. Pin the version, because the latest tag on npm points at an older beta:

    bash
    npm install @trexolab-solution/[email protected]
  2. Keep the licence key out of your code. Put it in an environment variable:

    bash
    export CERTENO_LICENCE_KEY=CERT-XXXX-XXXX-XXXX-XXXX
  3. Create licensed.mjs:

    js
    import { createVerifier, CertenoLicenceError } from '@trexolab/certeno-core'
     
    try {
      const verifier = await createVerifier({
        licence: { licenceKey: process.env.CERTENO_LICENCE_KEY },
      })
      console.log('Licensed. Features:', verifier.licence?.features.join(', '))
      // ... verifier.verify(pdf, 'file.pdf')
      verifier.dispose()
    } catch (error) {
      if (error instanceof CertenoLicenceError) {
        console.error(`Licence problem: ${error.code}`)
        process.exit(1)
      }
      throw error
    }
  4. Run it:

    bash
    node licensed.mjs

    The first run needs a network, because it activates the machine. You should see Licensed. followed by the features your plan includes. Later runs only check the licence, and start from the cached activation if the network is down.


5. Verify your output

  1. Run node verify.mjs signed.pdf again. The output is the same as in step 3: the licence changes what the engine may do, not what a signature looks like.

  2. To see a licence that is refused, unset the key and run the licensed script:

    bash
    unset CERTENO_LICENCE_KEY
    node licensed.mjs

    You should see Licence problem: NO_LICENCE_KEY, and the script exits with status 1. A key the licence server does not know gives UNAUTHORIZED instead.

  3. In a container, mount the folder in the home directory where the activation is kept. Otherwise every start looks like a new machine and uses one of your plan's activations. Licensing lists the options.

If something is not as described, see Troubleshooting. Every code a licence error can have is listed in Licensing.


Next steps

  • Licensing: what each feature unlocks, and how a licence behaves when the network is down.
  • Examples: print every check, the signer's certificate and the timestamp.
  • Revocation Checking: check online whether a certificate has been revoked, from a server.
  • Core API: createVerifier(), verify() and the VerificationResult types.
  • Deployment: the production checklist.
  • Error Reference: the errors you can meet and how to handle them.