PKI Management for node-opcua

6.13.0 · active · verified Tue Apr 21

node-opcua-pki is a comprehensive library for managing Public Key Infrastructures (PKI), Certificate Authorities (CA), and OPC UA certificates, offering both a powerful command-line interface (CLI) and a programmatic API. Designed specifically for the node-opcua ecosystem, it provides full lifecycle management for certificates, including support for intermediate CAs, OpenSSL 3.5.x compatibility, and a robust PFX (PKCS#12) toolbox. The current stable version is 6.13.0, with frequent minor and patch releases demonstrating active maintenance and continuous feature development. Key differentiators include its deep integration with OPC UA standards, a rich set of CLI commands for common PKI operations, and specialized features like an in-memory DER/PEM buffer API for `CertificateAuthority` operations and a certificate database for querying issued certificates. It serves as a critical component for securing OPC UA applications.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `node-opcua-pki` CLI to initialize a Public Key Infrastructure and generate a self-signed OPC UA application certificate for development or testing. It includes verification steps.

#!/bin/bash

# This script demonstrates how to set up a basic PKI structure and generate a self-signed certificate.
# Prerequisites: Node.js (with npx) and OpenSSL (or LibreSSL) installed on your system.
# For Debian/Ubuntu, install OpenSSL: `sudo apt install openssl`

# Define a root directory for our PKI (relative path).
PKI_ROOT="./my_opcua_pki_example"

echo "\n--- 1. Creating a new OPC UA PKI directory structure in ${PKI_ROOT} ---"
npx node-opcua-pki createPKI \
  --root "${PKI_ROOT}" \
  --keySize 2048 \
  --silent

# Check if the PKI root directory was created
if [ ! -d "${PKI_ROOT}" ]; then
  echo "Error: PKI root directory '${PKI_ROOT}' was not created. Exiting." >&2
  exit 1
fi

echo "\n--- 2. Creating a self-signed OPC UA Application Certificate ---"
npx node-opcua-pki certificate \
  --root "${PKI_ROOT}" \
  --selfSigned \
  --applicationUri "urn:my-opcua-server:application" \
  --subject "/C=US/ST=CA/L=SF/O=MyCompany/CN=MyOPCUAServer" \
  -o "${PKI_ROOT}/own/certs/server_certificate.pem" \
  --dns "localhost" \
  --ip "127.0.0.1" \
  --validity 365 # Valid for 1 year

# Check if the certificate was created
if [ ! -f "${PKI_ROOT}/own/certs/server_certificate.pem" ]; then
  echo "Error: Server certificate was not created. Exiting." >&2
  exit 1
fi

echo "\n--- 3. Dumping the created certificate information for verification ---"
npx node-opcua-pki dump "${PKI_ROOT}/own/certs/server_certificate.pem"

echo "\nPKI setup and self-signed certificate generation complete in '${PKI_ROOT}'."

view raw JSON →