跳轉到

時間戳記

可信時間戳記(Trusted Timestamp)證明一份文件在特定時間點存在且未被修改。NextPDF 的 TsaClient 實作 RFC 3161(Internet X.509 PKI Time-Stamp Protocol,TSP),與任何符合規範的時間戳記授權機構(TSA)整合。

設定 TSA 連線

use NextPDF\Security\TsaClient;
use NextPDF\Security\TsaConfig;

$tsa = TsaClient::create(
    config: TsaConfig::create(
        url: 'https://tsa.example.com/tsa',
        hashAlgorithm: 'SHA-256',
        timeout: 30,  // 秒
    ),
);

取得時間戳記

// 手動請求時間戳記(測試用)
$hash = hash('sha256', $documentContent, binary: true);
$timestampToken = $tsa->requestTimestamp(
    hashedMessage: $hash,
    nonce: random_bytes(8),  // 防重放攻擊
    requestCertificate: true, // 在回應中包含 TSA 憑證
);

// timestampToken 為 DER 編碼的 TimeStampToken(TSTInfo)

在 PAdES B-T 中使用

use NextPDF\Security\PadesOrchestrator;
use NextPDF\Security\SigningOptions;
use NextPDF\Security\SignatureLevel;

$orchestrator = PadesOrchestrator::create(tsa: $tsa);

$signedPdf = $orchestrator->sign(
    pdf: $pdfBytes,
    certificate: $cert,
    options: SigningOptions::create(
        level: SignatureLevel::PadesB_T,  // 自動請求時間戳記
    ),
);

PAdES B-T 在 CMS SignedData 中的 unsignedAttrs 加入 id-aa-signatureTimeStampToken 屬性,儲存 TSA 發行的時間戳記憑證(CounterSignature 格式)。

文件時間戳記(Document Timestamp)

use NextPDF\Security\DocumentTimestampOptions;

// 對整份 PDF 加蓋文件時間戳記(不含憑證簽章)
$timestampedPdf = $orchestrator->addDocumentTimestamp(
    pdf: $pdfBytes,
    options: DocumentTimestampOptions::create(
        fieldName: 'DocumentTimestamp',
        pageNumber: 1,
        position: Rectangle::fromXY(x: 100.0, y: 10.0, width: 70.0, height: 15.0),
    ),
);

時間戳記驗證

use NextPDF\Security\TimestampValidator;

$validator = TimestampValidator::create();
$result = $validator->validateToken(
    token: $timestampToken,
    trustedRoots: TrustStore::fromFile('/path/to/trusted-roots.pem'),
);

if ($result->isValid()) {
    $genTime = $result->getGenerationTime();
    echo "Timestamp issued at: " . $genTime->format(DateTimeInterface::RFC3339);
}

免費 TSA 服務

TSA URL 政策 OID
FreeTSA https://freetsa.org/tsr 1.3.6.1.4.1.44947.1.1.1
DigiCert https://timestamp.digicert.com 商業授權
GlobalSign https://timestamp.globalsign.com/tsa/r6advanced1 商業授權

參見