HSM Integration
★ Pro — Commercial License Required
HSM integration requires the Pro package and a PKCS#11-compatible hardware device.
TCPDF-Next Pro supports signing with Hardware Security Modules (HSMs), smart cards, and USB tokens via PKCS#11. Private keys never leave the hardware device.
HSM Classes
| Class | Purpose |
|---|---|
HsmSigner | Implements SignerInterface for HSM-based signing |
Pkcs11Bridge | Low-level PKCS#11 library communication |
Pkcs11Bridge
Manages the connection to a PKCS#11 library.
php
use Yeeefang\TcpdfNext\Pro\Security\Hsm\Pkcs11Bridge;
$bridge = new Pkcs11Bridge(
libraryPath: '/usr/lib/softhsm/libsofthsm2.so',
slotId: 0,
pin: $_ENV['PKCS11_PIN'], // never hardcode PINs
);
// List available keys
$keys = $bridge->listPrivateKeys();
foreach ($keys as $key) {
echo $key->label(); // "signing-key-2026"
echo $key->type(); // 'RSA', 'EC'
}HsmSigner
Drop-in replacement for software-based signing in the DigitalSigner workflow.
php
use Yeeefang\TcpdfNext\Core\Document;
use Yeeefang\TcpdfNext\Pro\Security\Hsm\{HsmSigner, Pkcs11Bridge};
use Yeeefang\TcpdfNext\Pro\Security\Signature\DigitalSigner;
use Yeeefang\TcpdfNext\Pro\Security\Timestamp\TsaClient;
use Yeeefang\TcpdfNext\Contracts\Enums\SignatureLevel;
$pdf = Document::create()->addPage()->text('HSM-signed document.');
$bridge = new Pkcs11Bridge(
libraryPath: $_ENV['PKCS11_LIBRARY'],
slotId: (int) $_ENV['PKCS11_SLOT'],
pin: $_ENV['PKCS11_PIN'],
);
$hsm = new HsmSigner($bridge);
$hsm->selectKey(label: 'signing-key-2026');
$hsm->certificate('/certs/hsm-signing.pem');
$hsm->chain(['/certs/intermediate.pem', '/certs/root.pem']);
$signer = new DigitalSigner($hsm);
$signer->level(SignatureLevel::PAdES_B_LTA);
$signer->timestampAuthority(new TsaClient('https://tsa.example.com/timestamp'));
$signer->reason('Enterprise archival signature');
$signer->sign($pdf);
$pdf->save('/output/hsm-signed.pdf');Supported HSMs
Any PKCS#11-compatible device works. Common examples:
| Device | Library Path (typical) |
|---|---|
| SoftHSM 2 (testing) | /usr/lib/softhsm/libsofthsm2.so |
| Thales Luna | /usr/lib/libCryptoki2_64.so |
| AWS CloudHSM | /opt/cloudhsm/lib/libcloudhsm_pkcs11.so |
| YubiKey (PIV) | /usr/lib/libykcs11.so |
| SafeNet eToken | C:\Windows\System32\eTPKCS11.dll |
Signature Algorithms
php
$hsm->algorithm('sha256WithRSAEncryption'); // default
$hsm->algorithm('sha256WithRSAPSS'); // RSASSA-PSS
$hsm->algorithm('ecdsaWithSHA256'); // ECDSA P-256| Algorithm | Notes |
|---|---|
| RSA PKCS#1 v1.5 (SHA-256/384/512) | Most widely compatible |
| RSASSA-PSS (SHA-256) | Recommended for new deployments |
| ECDSA (P-256 / P-384) | Smaller, faster signatures |
Testing with SoftHSM 2
bash
apt-get install softhsm2
softhsm2-util --init-token --slot 0 --label "test-token" \
--so-pin 87654321 --pin 12345678
pkcs11-tool --module /usr/lib/softhsm/libsofthsm2.so \
--login --pin 12345678 \
--keypairgen --key-type rsa:2048 --label "signing-key-2026" --id 01Error Handling
php
use Yeeefang\TcpdfNext\Pro\Security\Hsm\HsmException;
try {
$bridge->openSession();
} catch (HsmException $e) {
echo $e->getMessage(); // "PKCS#11 error: CKR_PIN_INCORRECT"
}| PKCS#11 Code | Meaning |
|---|---|
CKR_PIN_INCORRECT | Wrong PIN |
CKR_PIN_LOCKED | PIN locked after too many attempts |
CKR_TOKEN_NOT_PRESENT | HSM device not connected |
CKR_KEY_HANDLE_INVALID | Key not found on the token |
Next Steps
- PAdES Digital Signatures -- Full signature pipeline from B-B to B-LTA.
- Long-Term Validation -- DSS, OCSP, CRL, and archival timestamps.
- Pro Package Overview -- Full module listing and license information.