Skip to content

Integrazione HSM

Pro — Commercial License Required
L'integrazione HSM richiede il pacchetto Pro e un dispositivo hardware compatibile PKCS#11.

TCPDF-Next Pro supporta la firma con Hardware Security Module (HSM), smart card e token USB tramite PKCS#11. Le chiavi private non lasciano mai il dispositivo hardware.

Classi HSM

ClasseScopo
HsmSignerImplementa SignerInterface per firma basata su HSM
Pkcs11BridgeComunicazione libreria PKCS#11 basso livello

Pkcs11Bridge

Gestisce la connessione a una libreria PKCS#11.

php
use Yeeefang\TcpdfNext\Pro\Security\Hsm\Pkcs11Bridge;

$bridge = new Pkcs11Bridge(
    libraryPath: '/usr/lib/softhsm/libsofthsm2.so',
    slotId:      0,
    pin:         $_ENV['PKCS11_PIN'], // non codificare mai i PIN
);

// Lista chiavi disponibili
$keys = $bridge->listPrivateKeys();
foreach ($keys as $key) {
    echo $key->label(); // "signing-key-2026"
    echo $key->type();  // 'RSA', 'EC'
}

HsmSigner

Sostituzione diretta per firma basata su software nel workflow DigitalSigner.

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('Documento firmato HSM.');

$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('Firma archiviale per produzione');

$signer->sign($pdf);
$pdf->save('/output/hsm-signed.pdf');

HSM Supportati

Qualsiasi dispositivo compatibile PKCS#11 funziona. Esempi comuni:

DispositivoPercorso Libreria (tipico)
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 eTokenC:\Windows\System32\eTPKCS11.dll

Algoritmi Firma

php
$hsm->algorithm('sha256WithRSAEncryption');  // predefinito
$hsm->algorithm('sha256WithRSAPSS');         // RSASSA-PSS
$hsm->algorithm('ecdsaWithSHA256');          // ECDSA P-256
AlgoritmoNote
RSA PKCS#1 v1.5 (SHA-256/384/512)Più ampiamente compatibile
RSASSA-PSS (SHA-256)Raccomandato per nuove implementazioni
ECDSA (P-256 / P-384)Firme più piccole e veloci

Testing con 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 01

Gestione Errori

php
use Yeeefang\TcpdfNext\Pro\Security\Hsm\HsmException;

try {
    $bridge->openSession();
} catch (HsmException $e) {
    echo $e->getMessage(); // "PKCS#11 error: CKR_PIN_INCORRECT"
}
Codice PKCS#11Significato
CKR_PIN_INCORRECTPIN errato
CKR_PIN_LOCKEDPIN bloccato dopo troppi tentativi
CKR_TOKEN_NOT_PRESENTDispositivo HSM non connesso
CKR_KEY_HANDLE_INVALIDChiave non trovata sul token

Prossimi Passi

Rilasciato sotto licenza LGPL-3.0-or-later.