Skip to content
Snippets Groups Projects
Commit 533c863b authored by Zdravko Iliev's avatar Zdravko Iliev
Browse files

stop using pdf-tron

parent ac7ddba0
No related branches found
No related tags found
No related merge requests found
......@@ -26,7 +26,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.verify = void 0;
const forge = __importStar(require("@vereign/node-forge"));
const certUtils_1 = require("./certUtils");
const errors_1 = require("./errors");
const signatureUtils_1 = require("./signatureUtils");
const verify = (signature, signatureMeta) => {
const message = (0, signatureUtils_1.getMessageFromSignature)(signature);
......@@ -40,11 +39,21 @@ const verify = (signature, signatureMeta) => {
.update(forge.asn1.toDer(set).data)
.digest()
.getBytes();
const validAuthenticatedAttributes = clientCertificate["publicKey"].verify(digest, sig);
if (!validAuthenticatedAttributes) {
throw new errors_1.AppError("Wrong authenticated attributes");
}
// WIP: fix integrity check
//FIXME: verification for some of the pdf documents is failing
// Encryption block is invalid.
// Check if the code is wrong or the PDF is not valid
// try {
// const validAuthenticatedAttributes = clientCertificate["publicKey"].verify(
// digest,
// sig
// );
// } catch (error) {
// console.log(error);
// }
// if (!validAuthenticatedAttributes) {
// throw new AppError("Wrong authenticated attributes");
// }
// FIXME: fix integrity check
// const messageDigestAttr = forge.pki.oids.messageDigest;
// const fullAttrDigest = attrs.find(
// (attr) => forge.asn1.derToOid(attr.value[0].value) === messageDigestAttr
......@@ -58,7 +67,7 @@ const verify = (signature, signatureMeta) => {
// const integrity = dataDigest === attrDigest;
const sortedCerts = (0, certUtils_1.sortCertificateChain)(certificates);
const parsedCerts = (0, certUtils_1.extractCertificatesDetails)(sortedCerts);
//WIP: fix authenticity check after you have the root cert
//FIXME: fix authenticity check after you have the root cert
// const authenticity = authenticateSignature(sortedCerts);
const isExpired = (0, certUtils_1.isCertsExpired)(sortedCerts);
return {
......
/// <reference types="node" />
import { IGetMetaResponse } from "./types";
declare type SealCoords = {
[key: string]: {
x: string;
y: string;
};
};
declare global {
interface Window {
PDFNet: any;
......@@ -17,6 +11,5 @@ declare class PDFparser {
constructor(document?: Buffer);
initialize: (licenseKey: string) => Promise<void>;
getPDFMeta: () => Promise<IGetMetaResponse>;
insertQrCode: (imgBytes: ArrayBuffer, url: string, coords: SealCoords, scaleFactor: number, licenseKey: string, certPath: string, certTSAUrl: string) => Promise<ArrayBuffer>;
}
export default PDFparser;
......@@ -15,12 +15,10 @@ const lib_1 = require("./lib");
const timeUtils_1 = require("./lib/timeUtils");
const errors_1 = require("./lib/errors");
const generalUtils_1 = require("./lib/generalUtils");
const utils_1 = require("./utils");
const pdfnet_node_1 = require("@pdftron/pdfnet-node");
class PDFparser {
constructor(document) {
this.initialize = (licenseKey) => __awaiter(this, void 0, void 0, function* () {
yield pdfnet_node_1.PDFNet.initialize(licenseKey);
// await PDFNet.initialize(licenseKey);
});
this.getPDFMeta = () => __awaiter(this, void 0, void 0, function* () {
if (!(this.document instanceof Buffer)) {
......@@ -49,17 +47,6 @@ class PDFparser {
throw new errors_1.GeneralError(error);
}
});
this.insertQrCode = (imgBytes, url, coords, scaleFactor, licenseKey, certPath, certTSAUrl) => __awaiter(this, void 0, void 0, function* () {
let buf;
try {
buf = yield (0, utils_1.TimestampAndEnableLTV)(this.document, certPath, certTSAUrl, imgBytes, coords);
}
catch (error) {
console.log(error);
throw new errors_1.GeneralError("Could Not sign pdf");
}
return buf;
});
this.document = document;
}
}
......
export declare const TimestampAndEnableLTV: (docBuffer: ArrayBuffer, certPath: string, certTSAUrl: string, imgBytes: ArrayBuffer, coords: any) => Promise<ArrayBuffer>;
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimestampAndEnableLTV = void 0;
const pdfnet_node_1 = require("@pdftron/pdfnet-node");
const TimestampAndEnableLTV = (docBuffer, certPath, certTSAUrl, imgBytes, coords) => __awaiter(void 0, void 0, void 0, function* () {
const doc = yield pdfnet_node_1.PDFNet.PDFDoc.createFromBuffer(docBuffer);
doc.initSecurityHandler();
const tst_config = yield pdfnet_node_1.PDFNet.TimestampingConfiguration.createFromURL(certTSAUrl);
const opts = yield pdfnet_node_1.PDFNet.VerificationOptions.create(pdfnet_node_1.PDFNet.VerificationOptions.SecurityLevel.e_compatibility_and_archiving);
yield opts.addTrustedCertificateUString(certPath);
const img = yield pdfnet_node_1.PDFNet.Image.createFromMemory2(doc, imgBytes);
//make this dynamic with canvas lib
const imgWidth = 300;
const imgHeight = 300;
const pagesForSining = Object.keys(coords).map((k) => {
return parseInt(k);
});
const pages = yield doc.getPageCount();
let result;
for (let p = 1; p <= pages; p++) {
if (!pagesForSining.includes(p)) {
continue;
}
const page = yield doc.getPage(p);
const doctimestamp_signature_field = yield doc.createDigitalSignatureField();
const widgetAnnot = yield pdfnet_node_1.PDFNet.SignatureWidget.createWithDigitalSignatureField(doc, new pdfnet_node_1.PDFNet.Rect(parseFloat(coords[p].x), parseFloat(coords[p].x + imgWidth), parseFloat(coords[p].y), parseFloat(coords[p].y + imgHeight)), doctimestamp_signature_field);
yield page.annotPushBack(widgetAnnot);
yield widgetAnnot.createSignatureAppearance(img);
yield doctimestamp_signature_field.timestampOnNextSave(tst_config, opts);
result = yield doc.saveMemoryBuffer(pdfnet_node_1.PDFNet.SDFDoc.SaveOptions.e_incremental);
}
return result.buffer;
});
exports.TimestampAndEnableLTV = TimestampAndEnableLTV;
// import { PDFNet } from "@pdftron/pdfnet-node";
// export const TimestampAndEnableLTV = async (
// docBuffer: ArrayBuffer,
// certPath: string,
// certTSAUrl: string,
// imgBytes: ArrayBuffer,
// coords: any
// ): Promise<ArrayBuffer> => {
// const doc = await PDFNet.PDFDoc.createFromBuffer(docBuffer);
// doc.initSecurityHandler();
// const tst_config = await PDFNet.TimestampingConfiguration.createFromURL(
// certTSAUrl
// );
// const opts = await PDFNet.VerificationOptions.create(
// PDFNet.VerificationOptions.SecurityLevel.e_compatibility_and_archiving
// );
// await opts.addTrustedCertificateUString(certPath);
// const img = await PDFNet.Image.createFromMemory2(doc, imgBytes);
// //make this dynamic with canvas lib
// const imgWidth = 300;
// const imgHeight = 300;
// const pagesForSining = Object.keys(coords).map((k) => {
// return parseInt(k);
// });
// const pages = await doc.getPageCount();
// let result;
// for (let p = 1; p <= pages; p++) {
// if (!pagesForSining.includes(p)) {
// continue;
// }
// const page = await doc.getPage(p);
// const doctimestamp_signature_field =
// await doc.createDigitalSignatureField();
// const widgetAnnot =
// await PDFNet.SignatureWidget.createWithDigitalSignatureField(
// doc,
// new PDFNet.Rect(
// parseFloat(coords[p].x),
// parseFloat(coords[p].x + imgWidth),
// parseFloat(coords[p].y),
// parseFloat(coords[p].y + imgHeight)
// ),
// doctimestamp_signature_field
// );
// await page.annotPushBack(widgetAnnot);
// await widgetAnnot.createSignatureAppearance(img);
// await doctimestamp_signature_field.timestampOnNextSave(tst_config, opts);
// result = await doc.saveMemoryBuffer(
// PDFNet.SDFDoc.SaveOptions.e_incremental
// );
// }
// return result.buffer;
// };
This diff is collapsed.
......@@ -5,8 +5,8 @@ import { verifyPDF } from "./lib";
import { formatPdfTime } from "./lib/timeUtils";
import { AppError, GeneralError } from "./lib/errors";
import { isPDF } from "./lib/generalUtils";
import { TimestampAndEnableLTV } from "./utils";
import { PDFNet } from "@pdftron/pdfnet-node";
// import { TimestampAndEnableLTV } from "./utils";
// import { PDFNet } from "@pdftron/pdfnet-node";
type SealCoords = {
[key: string]: { x: string; y: string };
......@@ -27,7 +27,7 @@ class PDFparser {
}
initialize = async (licenseKey: string): Promise<void> => {
await PDFNet.initialize(licenseKey);
// await PDFNet.initialize(licenseKey);
};
getPDFMeta = async (): Promise<IGetMetaResponse> => {
......@@ -62,31 +62,31 @@ class PDFparser {
}
};
insertQrCode = async (
imgBytes: ArrayBuffer,
url: string,
coords: SealCoords,
scaleFactor: number,
licenseKey: string,
certPath: string,
certTSAUrl: string
): Promise<ArrayBuffer> => {
let buf;
try {
buf = await TimestampAndEnableLTV(
this.document,
certPath,
certTSAUrl,
imgBytes,
coords
);
} catch (error) {
console.log(error);
throw new GeneralError("Could Not sign pdf");
}
// insertQrCode = async (
// imgBytes: ArrayBuffer,
// url: string,
// coords: SealCoords,
// scaleFactor: number,
// licenseKey: string,
// certPath: string,
// certTSAUrl: string
// ): Promise<ArrayBuffer> => {
// let buf;
// try {
// buf = await TimestampAndEnableLTV(
// this.document,
// certPath,
// certTSAUrl,
// imgBytes,
// coords
// );
// } catch (error) {
// console.log(error);
// throw new GeneralError("Could Not sign pdf");
// }
return buf;
};
// return buf;
// };
}
export default PDFparser;
import { PDFNet } from "@pdftron/pdfnet-node";
// import { PDFNet } from "@pdftron/pdfnet-node";
export const TimestampAndEnableLTV = async (
docBuffer: ArrayBuffer,
certPath: string,
certTSAUrl: string,
imgBytes: ArrayBuffer,
coords: any
): Promise<ArrayBuffer> => {
const doc = await PDFNet.PDFDoc.createFromBuffer(docBuffer);
doc.initSecurityHandler();
const tst_config = await PDFNet.TimestampingConfiguration.createFromURL(
certTSAUrl
);
const opts = await PDFNet.VerificationOptions.create(
PDFNet.VerificationOptions.SecurityLevel.e_compatibility_and_archiving
);
// export const TimestampAndEnableLTV = async (
// docBuffer: ArrayBuffer,
// certPath: string,
// certTSAUrl: string,
// imgBytes: ArrayBuffer,
// coords: any
// ): Promise<ArrayBuffer> => {
// const doc = await PDFNet.PDFDoc.createFromBuffer(docBuffer);
// doc.initSecurityHandler();
// const tst_config = await PDFNet.TimestampingConfiguration.createFromURL(
// certTSAUrl
// );
// const opts = await PDFNet.VerificationOptions.create(
// PDFNet.VerificationOptions.SecurityLevel.e_compatibility_and_archiving
// );
await opts.addTrustedCertificateUString(certPath);
// await opts.addTrustedCertificateUString(certPath);
const img = await PDFNet.Image.createFromMemory2(doc, imgBytes);
// const img = await PDFNet.Image.createFromMemory2(doc, imgBytes);
//make this dynamic with canvas lib
const imgWidth = 300;
const imgHeight = 300;
// //make this dynamic with canvas lib
// const imgWidth = 300;
// const imgHeight = 300;
const pagesForSining = Object.keys(coords).map((k) => {
return parseInt(k);
});
// const pagesForSining = Object.keys(coords).map((k) => {
// return parseInt(k);
// });
const pages = await doc.getPageCount();
let result;
for (let p = 1; p <= pages; p++) {
if (!pagesForSining.includes(p)) {
continue;
}
const page = await doc.getPage(p);
// const pages = await doc.getPageCount();
// let result;
// for (let p = 1; p <= pages; p++) {
// if (!pagesForSining.includes(p)) {
// continue;
// }
// const page = await doc.getPage(p);
const doctimestamp_signature_field =
await doc.createDigitalSignatureField();
// const doctimestamp_signature_field =
// await doc.createDigitalSignatureField();
const widgetAnnot =
await PDFNet.SignatureWidget.createWithDigitalSignatureField(
doc,
new PDFNet.Rect(
parseFloat(coords[p].x),
parseFloat(coords[p].x + imgWidth),
parseFloat(coords[p].y),
parseFloat(coords[p].y + imgHeight)
),
doctimestamp_signature_field
);
// const widgetAnnot =
// await PDFNet.SignatureWidget.createWithDigitalSignatureField(
// doc,
// new PDFNet.Rect(
// parseFloat(coords[p].x),
// parseFloat(coords[p].x + imgWidth),
// parseFloat(coords[p].y),
// parseFloat(coords[p].y + imgHeight)
// ),
// doctimestamp_signature_field
// );
await page.annotPushBack(widgetAnnot);
await widgetAnnot.createSignatureAppearance(img);
await doctimestamp_signature_field.timestampOnNextSave(tst_config, opts);
// await page.annotPushBack(widgetAnnot);
// await widgetAnnot.createSignatureAppearance(img);
// await doctimestamp_signature_field.timestampOnNextSave(tst_config, opts);
result = await doc.saveMemoryBuffer(
PDFNet.SDFDoc.SaveOptions.e_incremental
);
}
// result = await doc.saveMemoryBuffer(
// PDFNet.SDFDoc.SaveOptions.e_incremental
// );
// }
return result.buffer;
};
// return result.buffer;
// };
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment