Skip to content

Commit

Permalink
Added support to add a document timestamp from a TSA.
Browse files Browse the repository at this point in the history
  • Loading branch information
zboris12 committed Oct 24, 2022
1 parent 528eaad commit 8b008cc
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 234 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ZgaPdfSigner
A javascript tool to sign a pdf or set protection of a pdf in web browser.
And it also can be used in Google Apps Script.
And it also can be used in Google Apps Script and nodejs.

PS: __ZGA__ is the abbreviation of my father's name.
And I use this name to hope the merits from this application will be dedicated to my parents.
Expand All @@ -11,7 +11,8 @@ And I use this name to hope the merits from this application will be dedicated t
* Sign a pdf with a visible pkcs#7 signature by drawing an image.
* Sign a pdf and set DocMDP(document modification detection and prevention).
* Add a new signature to a pdf if it has been signed already. (An incremental update)
* Sign a pdf with a timestamp from TSA(Time Stamp Authority). (Only in Google Apps Script)
* Add a document timestamp from TSA(Time Stamp Authority). (Only in Google Apps Script and nodejs)
* Sign a pdf with a timestamp from TSA. (Only in Google Apps Script and nodejs)
* Set password protection to a pdf. Supported algorithms:
* 40bit RC4 Encryption
* 128bit RC4 Encryption
Expand Down Expand Up @@ -138,8 +139,8 @@ fld.createFile(Utilities.newBlob(u8arr, "application/pdf").setName("signed_test.

## Detail of SignOption

* __p12cert__: Array<number>|Uint8Array|ArrayBuffer|string :point_right: Certificate's data
* __pwd__: string :point_right: The passphrase of the certificate
* __p12cert__: Array<number>|Uint8Array|ArrayBuffer|string :point_right: (Optional) Certificate's data. In the case of adding a document timestamp, it must be omitted.
* __pwd__: string :point_right: (Optional) The passphrase of the certificate. In the case of adding a document timestamp, it must be omitted.
* __permission__: number :point_right: (Optional) The modification permissions granted for this document.
This is a setting of DocMDP(document modification detection and prevention). Valid values are:
* 1: No changes to the document are permitted; any change to the document invalidates the signature.
Expand All @@ -148,7 +149,7 @@ fld.createFile(Utilities.newBlob(u8arr, "application/pdf").setName("signed_test.
* __reason__: string :point_right: (Optional) The reason for signing
* __location__: string :point_right: (Optional) Your location
* __contact__: string :point_right: (Optional) Your contact information
* __signdate__: Date|string|_TsaServiceInfo_ :point_right: (Optional)
* __signdate__: Date|string|_TsaServiceInfo_ :point_right: (Optional) In the case of adding a document timestamp, it can't be omitted and can't be a Date.
* When it is a Date, it means the date and time of signing.
* When it is a string, it can be an url of TSA or an index of the preset TSAs as below:
* "1": http://ts.ssl.com
Expand All @@ -161,6 +162,7 @@ fld.createFile(Utilities.newBlob(u8arr, "application/pdf").setName("signed_test.
* When it is a _TsaServiceInfo_, it means a full customized information of a TSA.
* __url__: string :point_right: The url of TSA
* __len__: number :point_right: (Optional) The length of signature's placeholder
* __headers__: Object<string, *> :point_right: (Optional) The customized headers for sending to tsa server
* When it is omitted, the system timestamp will be used.
* __signame__: string :point_right: (Optional) The name of the signature
* __drawinf__: _SignDrawInfo_ :point_right: (Optional) Visible signature's information
Expand Down
4 changes: 3 additions & 1 deletion closure/forge-ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ forge.asn1.integerToDer = function(num){};
forge.asn1.oidToDer = function(oid){};
forge.asn1.Type = {};
/** @type {number} */
forge.asn1.Type.NONE;
/** @type {number} */
forge.asn1.Type.UTF8;
/** @type {number} */
forge.asn1.Type.SET;
Expand All @@ -126,7 +128,7 @@ forge.asn1.Class.CONTEXT_SPECIFIC;
* @param {number} tagClass
* @param {number} type
* @param {boolean} constructed
* @param {Array<string>|string} value
* @param {Array<forge.asn1>|forge.asn1|number|string} value
* @param {Object=} options
* @return {forge.asn1}
*/
Expand Down
7 changes: 5 additions & 2 deletions closure/zb-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* {{
* url: string,
* len: (number|undefined),
* headers: (Object<string, *>|undefined),
* }}
*/
var TsaServiceInfo;
Expand Down Expand Up @@ -32,15 +33,17 @@ var SignAreaInfo;
*/
var SignDrawInfo;
/**
* In the case of adding a document timestamp, the p12cert and pwd must be omitted. But meanwhile the tsa must be provided.
*
* permission: (DocMDP) The modification permissions granted for this document. Valid values are:
* 1 : No changes to the document are permitted; any change to the document invalidates the signature.
* 2 : Permitted changes are filling in forms, instantiating page templates, and signing; other changes invalidate the signature.
* 3 : Permitted changes are the same as for 2, as well as annotation creation, deletion, and modification; other changes invalidate the signature.
*
* @typedef
* {{
* p12cert: (Array<number>|Uint8Array|ArrayBuffer|string),
* pwd: string,
* p12cert: (Array<number>|Uint8Array|ArrayBuffer|string|undefined),
* pwd: (string|undefined),
* permission: (number|undefined),
* reason: (string|undefined),
* location: (string|undefined),
Expand Down
15 changes: 9 additions & 6 deletions test4node.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ globalThis.forge = require("node-forge");
require("./zgapdfcryptor.js");
require("./zgapdfsigner.js");

Zga.UrlFetchApp = {};
/**
* @param {string} url
* @param {UrlFetchParams} params
* @return {Promise<Uint8Array>}
*/
Zga.UrlFetchApp.fetch = function(url, params){
Zga.urlFetch = function(url, params){
return new Promise(function(resolve, reject){
/** @type {URL} */
var opts = m_urlparser.parse(url);
Expand Down Expand Up @@ -91,13 +90,17 @@ async function main(){
}

if(!ps){
throw new Error("The passphrase is not specified.");
// throw new Error("The passphrase is not specified.");
pfxPath = "";
}

/** @type {Buffer} */
var pdf = m_fs.readFileSync(pdfPath);
/** @type {Buffer} */
var pfx = m_fs.readFileSync(pfxPath);
var pfx = null;
if(pfxPath){
pfx = m_fs.readFileSync(pfxPath);
}
/** @type {Buffer} */
var img = null;
/** @type {string} */
Expand All @@ -109,11 +112,11 @@ async function main(){

/** @type {SignOption} */
var sopt = null;
if(pfx){
if(pdf){
sopt = {
p12cert: pfx,
pwd: ps,
permission: 1,
permission: pfx ? 1 : 0,
signdate: "1",
reason: "I have a test reason.",
location: "I am on the earth.",
Expand Down
Loading

0 comments on commit 8b008cc

Please sign in to comment.