setasign/setapdf-signer-addon-pkcs11
Composer 安装命令:
composer require setasign/setapdf-signer-addon-pkcs11
包简介
A SetaPDF-Signer component signature add-on for accessing keys through the PKCS11 interface.
README 文档
README
This package offers a module for the SetaPDF-Signer component that allows you to use keys stored on a PKCS11 compatible device (e.g. HSM, USB Token) to digital sign PDF documents in pure PHP.
Requirements
This module requires the PKCS11 PHP extension to be installed.
You also need to provide the path to the PKCS11 module of your device.
The package is developed and tested on PHP >= 7.4 up to PHP 8.5 (PKCS11 PHP extension compiled from master; 2026-01-27). Requirements of the SetaPDF-Signer component can be found here.
Installation
Add following to your composer.json:
{
"require": {
"setasign/setapdf-signer-addon-pkcs11": "^1.1"
},
"repositories": [
{
"type": "composer",
"url": "https://www.setasign.com/downloads/"
}
]
}
and execute composer update. You need to define the repository to evaluate the dependency to the
SetaPDF-Signer component
(see here for more details).
Usage
All classes in this package are located in the namespace setasign\SetaPDF\Signer\Module\Pkcs11.
The Module class
This is the main signature module which can be used with the SetaPDF-Signer component. Internally it holds an own instance of the PAdES signature module and offers all relevant proxy methods.
The only arguments you need to pass to the Module instance is a \Pkcs11\Key instance of the private
key and the related X509 certificate.
The default padding schema for signatures using RSA keys is RSASSA-PKCS1-v1_5. To use
RSASSA-PSS just call $module->setPssPadding();.
A simple complete signature process would look like this:
<?php
use setasign\SetaPDF\Signer\Module\Pkcs11\Module;
use setasign\SetaPDF2\Core\Document;
use setasign\SetaPDF2\Core\Writer\HttpWriter;
use setasign\SetaPDF2\Signer\Digest;
use setasign\SetaPDF2\Signer\Signer;
$pin = '123456';
$modulePath = '/usr/lib/softhsm/libsofthsm2.so';
$pkcs11 = new Pkcs11\Module($modulePath);
$slotList = $pkcs11->getSlotList();
$slotId = $slotList[0];
$session = $pkcs11->openSession($slotId, Pkcs11\CKF_RW_SESSION);
$session->login(Pkcs11\CKU_USER, $pin);
$skey = $session->findObjects([
Pkcs11\CKA_PRIVATE => true,
Pkcs11\CKA_LABEL => "SetaPDF-Demo"
])[0];
$module = new setasign\SetaPDF\Signer\Module\Pkcs11\Module($skey);
$module->setCertificate('path/to/setapdf.pem');
$module->setPssPadding();
$module->setDigest(SetaPDF_Signer_Digest::SHA_512);
$fileToSign = 'path/to/Laboratory-Report.pdf';
// create a writer instance
$writer = new HttpWriter('signed.pdf');
// create the document instance
$document = Document::loadByFilename($fileToSign, $writer);
// create the signer instance
$signer = new Signer($document);
$signer->sign($module);
$session->logout();
Testing
For testing purpose we used SoftHSM2 and imported existing test certificates and keys into it using following command:
softhsm2-util --import <PATH-TO-CERTIFICATE> --token "<THE-TOKEN-NAME>" --label "<THE-LABEL>" --id <UNIQUE-ID-IN-HEX-NOTATION>
CSR generation and key attestation is not part of this add-on.
setasign/setapdf-signer-addon-pkcs11 适用场景与选型建议
setasign/setapdf-signer-addon-pkcs11 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 894 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 02 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 setasign/setapdf-signer-addon-pkcs11 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 setasign/setapdf-signer-addon-pkcs11 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 894
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-02-17