Skip to content

Intégration HSM

Pro — Commercial License Required
L'intégration HSM nécessite le package Pro et un appareil matériel compatible PKCS#11.

TCPDF-Next Pro supporte la signature avec des modules de sécurité matériel (HSM), des cartes à puce et des tokens USB via PKCS#11. Les clés privées ne quittent jamais l'appareil matériel.

Classes HSM

ClasseObjectif
HsmSignerImplémente SignerInterface pour signature basée sur HSM
Pkcs11BridgeCommunication de bas niveau avec bibliothèque PKCS#11

Pkcs11Bridge

Gère la connexion à une bibliothèque 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'], // ne jamais coder en dur les PIN
);

// Lister les clés disponibles
$keys = $bridge->listPrivateKeys();
foreach ($keys as $key) {
    echo $key->label(); // "signing-key-2026"
    echo $key->type();  // 'RSA', 'EC'
}

HsmSigner

Remplacement direct pour la signature logicielle dans le 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('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('Production archival signature');

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

HSM supportés

Tout appareil compatible PKCS#11 fonctionne. Exemples courants :

AppareilChemin de bibliothèque (typique)
SoftHSM 2 (test)/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

Algorithmes de signature

php
$hsm->algorithm('sha256WithRSAEncryption');  // défaut
$hsm->algorithm('sha256WithRSAPSS');         // RSASSA-PSS
$hsm->algorithm('ecdsaWithSHA256');          // ECDSA P-256
AlgorithmeNotes
RSA PKCS#1 v1.5 (SHA-256/384/512)Le plus largement compatible
RSASSA-PSS (SHA-256)Recommandé pour nouveaux déploiements
ECDSA (P-256 / P-384)Signatures plus petites et rapides

Test avec 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

Gestion des erreurs

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

try {
    $bridge->openSession();
} catch (HsmException $e) {
    echo $e->getMessage(); // "PKCS#11 error: CKR_PIN_INCORRECT"
}
Code PKCS#11Signification
CKR_PIN_INCORRECTPIN incorrect
CKR_PIN_LOCKEDPIN verrouillé après trop de tentatives
CKR_TOKEN_NOT_PRESENTAppareil HSM non connecté
CKR_KEY_HANDLE_INVALIDClé introuvable sur le token

Prochaines étapes

Distribué sous licence LGPL-3.0-or-later.