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

stop using pdf-tron

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