setasign/setapdf-signer-addon-pkcs11 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

setasign/setapdf-signer-addon-pkcs11

最新稳定版本:v1.1.1

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.

统计信息

  • 总下载量: 827
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固