Skip to content

Implement TimeStamping feature #617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 44 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
28c747b
Update tcpdf.php
hidasw May 14, 2023
ff9e5a8
Create asn1_function_tcpdf.php
hidasw May 14, 2023
ff20f9f
Create asn1_parser_tcpdf.php
hidasw May 14, 2023
e998624
Create functionLog_tcpdf.php
hidasw May 14, 2023
ffce574
Create Local Root CA.crt
hidasw May 14, 2023
15bf870
Create tcpdf test.pem
hidasw May 14, 2023
a295943
Create index.php
hidasw May 14, 2023
abfaeda
Update tcpdf.php
hidasw May 15, 2023
01fb957
Update index.php
hidasw May 15, 2023
1656d3c
reformatting and cleanup
hidasw May 16, 2023
80f70d3
Update tcpdf_asn1.min.php
hidasw May 17, 2023
3ab497a
Update tcpdf.php
hidasw May 17, 2023
c8e4c7b
major update
hidasw May 25, 2023
c16b58c
Update tcpdf.php
hidasw Jun 4, 2023
f1de348
Update include/tcpdf_cmssignature.php
hidasw Dec 22, 2023
1499b2b
Remove type on properties.
evamtinez Apr 16, 2024
df74f95
Curl close was moved after curl_getinfo.
evamtinez Apr 16, 2024
4a384cb
Fix indentation
evamtinez Apr 16, 2024
fe6c482
Merge pull request #2 from evamtinez/main
hidasw Apr 18, 2024
3a741b6
Add TSA parameter
hidasw Apr 19, 2024
fe247a1
update to support LTV and TSA at once
hidasw Apr 22, 2024
050eec1
Merge branch 'main' into main
hidasw Apr 22, 2024
92f3539
significant improvement on signing process
hidasw Apr 24, 2024
3ecc5ab
Merge branch 'main' of https://github.com/hidasw/TCPDF
hidasw Apr 24, 2024
959052d
combines several functions
hidasw Apr 24, 2024
9950510
Update example_052.php
hidasw Apr 24, 2024
e1297c2
Update tcpdf.php
hidasw May 14, 2024
a7b5ebc
Update tcpdf_cmssignature.php
hidasw May 14, 2024
46ecd34
Update example_052.php
hidasw May 14, 2024
6ab1ed7
Delete RootCATest.pem.crl
hidasw May 14, 2024
f166179
Delete RootCATest.der.crl
hidasw May 14, 2024
f7dc852
Delete Root CA Test.crt
hidasw May 14, 2024
7a766b6
Delete Root CA Test OCSP Signer.pem
hidasw May 14, 2024
2543e57
Delete PDF User.pem
hidasw May 14, 2024
0913f4f
Create PDF Signing CA.crt
hidasw May 14, 2024
58228d7
Create ocspTest.bat
hidasw May 14, 2024
5ea1de8
Create longChain.pfx
hidasw May 14, 2024
d2f6c24
Create longChain.pem
hidasw May 14, 2024
ca7a5e3
Create long.pfx
hidasw May 14, 2024
67ce271
Create b6ce4782.0
hidasw May 14, 2024
42beeea
Create 7262ea48.0
hidasw May 14, 2024
0884ea0
Create 71ab782e.0
hidasw May 14, 2024
5d1c8ec
Create 5a0ce691.0
hidasw May 14, 2024
cbb1aac
Create 26a91b3f.0
hidasw May 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 90 additions & 3 deletions tcpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,8 @@ class TCPDF {
* @protected
* @since 4.6.005 (2009-04-24)
*/
protected $signature_max_length = 11742;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i''m increasing this size to provide enough space to place tsa response data. and sometimes need more, depend on tsa response data size.

// protected $signature_max_length = 11742;
protected $signature_max_length = 20742;

/**
* Data for digital signature appearance.
Expand Down Expand Up @@ -7692,12 +7693,14 @@ public function Output($name='doc.pdf', $dest='I') {
$signature = $tmparr[1];
// decode signature
$signature = base64_decode(trim($signature));
// add TSA timestamp to signature
$signature = $this->applyTSA($signature);
// convert signature to hex
$signature = current(unpack('H*', $signature));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should before $this->applyTSA($signature)

// add TSA timestamp to signature
$signature = $this->applyTSA($signature);

$signature = str_pad($signature, $this->signature_max_length, '0');
// Add signature to the document

$this->buffer = substr($pdfdoc, 0, $byte_range[1]).'<'.$signature.'>'.substr($pdfdoc, $byte_range[1]);
$this->bufferlen = strlen($this->buffer);
}
Expand Down Expand Up @@ -13675,6 +13678,90 @@ protected function applyTSA($signature) {
return $signature;
}
//@TODO: implement this feature
// start timestamping
// by Hida since 5.9.128 (2011-10-06)
if($this->tsa_timestamp) {
//Include asn1 fuction script
require_once(dirname(__FILE__).'/include/asn1_parser_tcpdf.php');
require_once(dirname(__FILE__).'/include/asn1_function_tcpdf.php');
require_once(dirname(__FILE__).'/include/functionLog_tcpdf.php');

$tsaLog = __FILE__." line:(".__LINE__."). Perform timestamping...\n";
//Parse TCPDF Signature structure to get signed hash sequence
$p = asn1parse($signature);
$p1 = asn1parse($p[0][1]);
$p2 = asn1parse($p1[1][1]);
$p3 = asn1parse($p2[0][1]);
$p2 = asn1parse($p3[4][1]);
$pa1 = asn1parse($p2[0][1]);
$pa2 = asn1parse($pa1[3][1]);

//Create timestamp request

//Create hash of encrypted contents TCPDF signature
$hash = hash('sha1', hex2bin($pa1[5][1]));
//Build timestamp request data
$tsReqData = seq(
int(1).
seq(
seq(
"06052B0E03021A". // Obj_sha1
"0500" // Null
).
oct($hash)
).
int(hash('crc32', rand())).
'0101ff'
);
$raw_data = hex2bin($tsReqData);

//Send request to TSA Server with Curl
if(extension_loaded('curl')) {
$tsaLog .= __FILE__." line:(".__LINE__."). Curl was already Loaded\n".__FILE__." line:(".__LINE__."). Curl is sending tsRequest to \"".$this->tsa_data['tsa_host']."\" ...\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->tsa_data['tsa_host']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERAGENT, '1');
curl_setopt($ch, CURLOPT_POSTFIELDS, $raw_data);

$tsResponse = curl_exec($ch);
if($tsResponse != false) {
$tsaLog .= __FILE__." line:(".__LINE__."). tsRequest is sent.\n";
} else {
tsaLog("$tsaLog".__FILE__." line:(".__LINE__."). can't send tsRequest, Timestamp failed!\n",'w');
}
//parse ts response
$hexTs = bin2hex($tsResponse);
$tsparse = asn1parse($hexTs);

$tsparse0 = asn1parse($tsparse[0][1]);
if(count($tsparse0) > 1) { //Remove response status data, only take timeStampToken
$timeStamp = seq($tsparse0[1][1]);
} else {
$timeStamp = seq($tsparse0[0][1]);
}

//Add timestamp to TCPDF Signature
$timeStamp = seq("060B2A864886F70D010910020E".set($timeStamp));
$pkcs7 = int($pa1[0][1]).seq($pa1[1][1]).seq($pa1[2][1]).explicit(0, $pa1[3][1]).seq($pa1[4][1]).oct($pa1[5][1]);
$time = seq($pkcs7.explicit(1,$timeStamp));
$aa=seq(int(1). set($p3[1][1]).seq($p3[2][1]).explicit(0, $p3[3][1]).set($time));
$hdaSignature = seq("06092A864886F70D010702".explicit(0,($aa)))."0000";

$signature = $hdaSignature;
// $tsaLog .= $signature;
tsaLog("$tsaLog".__FILE__." line:(".__LINE__."). Timestamp Success.\n");
} else {
$tsaLog .= __FILE__." line:(".__LINE__."). Curl was not loaded, trying to load it...\n";
if(@dl('php_curl.dll')) {
$tsaLog .= __FILE__." line:(".__LINE__."). Curl successfully Loaded.\n";
} else {
tsaLog("$tsaLog\n".__FILE__." line:(".__LINE__."). Curl failed to load. Timestamping failed!", 'w');
}
}
}
// end timestamping
return $signature;
}

Expand Down