1- From 536b26734b6068c3477402e3b8c38e08caaaf30e Mon Sep 17 00:00:00 2001
1+ From 6298d0b8f11bb756a0529a783a85e555e5049c83 Mon Sep 17 00:00:00 2001
22From: Luca Magrone <luca@magrone.cc>
3- Date: Fri, 18 Oct 2024 22:22:49 +0200
3+ Date: Sun, 20 Oct 2024 03:29:27 +0200
44Subject: [PATCH] cie_sign_sdk: PdfSignatureGenerator: Rewrite implementation
55 of PADES signature
66
77As of now the code relies on a closed-source version of PoDoFo.
88Write a new implementation of the PdfSignatureGenerator class based on
99the code from podofosign and the old code of the class. Try to match
10- behaviour of the program to the behaviour of versions of this project
11- for other platforms.
12- Use PoDoFo open-source version 0.9.8 to write the implementation.
10+ behaviour to the behaviour of versions for other platforms.
11+ Use open-source PoDoFo version 0.9.8 API.
1312Also fix loading document for verification with the correct method.
1413
1514Signed-off-by: Luca Magrone <luca@magrone.cc>
1615---
1716 CIEID/src/it/ipzs/cieid/MainFrame.java | 6 +-
1817 cie_sign_sdk/include/PdfSignatureGenerator.h | 2 -
19- cie_sign_sdk/src/PdfSignatureGenerator.cpp | 303 ++++++++++---------
18+ cie_sign_sdk/src/PdfSignatureGenerator.cpp | 301 ++++++++++---------
2019 cie_sign_sdk/src/PdfVerifier.cpp | 6 +-
21- 4 files changed, 169 insertions(+), 148 deletions(-)
20+ 4 files changed, 169 insertions(+), 146 deletions(-)
2221
2322diff --git a/CIEID/src/it/ipzs/cieid/MainFrame.java b/CIEID/src/it/ipzs/cieid/MainFrame.java
2423index ac56eca..cfe9270 100644
@@ -60,10 +59,18 @@ index 5a19d6f..024d107 100644
6059
6160 const double getHeight(int pageIndex);
6261diff --git a/cie_sign_sdk/src/PdfSignatureGenerator.cpp b/cie_sign_sdk/src/PdfSignatureGenerator.cpp
63- index be4dc0c..3346e0e 100644
62+ index be4dc0c..a0c4726 100644
6463--- a/cie_sign_sdk/src/PdfSignatureGenerator.cpp
6564+++ b/cie_sign_sdk/src/PdfSignatureGenerator.cpp
66- @@ -12,6 +12,10 @@
65+ @@ -6,12 +6,18 @@
66+ * Copyright 2012 __MyCompanyName__. All rights reserved.
67+ *
68+ */
69+ + #include <iostream>
70+ + #include <fstream>
71+
72+ #include "PdfSignatureGenerator.h"
73+ #include "PdfVerifier.h"
6774 #include "UUCLogger.h"
6875
6976 #define SINGNATURE_SIZE 10000
@@ -74,7 +81,7 @@ index be4dc0c..3346e0e 100644
7481
7582 #ifdef CreateFont
7683 #undef CreateFont
77- @@ -27,10 +31 ,7 @@ USE_LOG;
84+ @@ -27,10 +33 ,7 @@ USE_LOG;
7885
7986 PdfSignatureGenerator::PdfSignatureGenerator()
8087 : m_pPdfDocument(NULL), m_pSignatureField(NULL), m_pSignOutputDevice(NULL), m_pFinalOutDevice(NULL),
@@ -86,7 +93,7 @@ index be4dc0c..3346e0e 100644
8693
8794 PdfSignatureGenerator::~PdfSignatureGenerator()
8895 {
89- @@ -61,25 +62 ,16 @@ int PdfSignatureGenerator::Load(const char* pdf, int len)
96+ @@ -61,25 +64 ,16 @@ int PdfSignatureGenerator::Load(const char* pdf, int len)
9097
9198 try
9299 {
@@ -116,7 +123,7 @@ index be4dc0c..3346e0e 100644
116123 }
117124 catch(::PoDoFo::PdfError& err)
118125 {
119- @@ -91,16 +83 ,6 @@ int PdfSignatureGenerator::Load(const char* pdf, int len)
126+ @@ -91,16 +85 ,6 @@ int PdfSignatureGenerator::Load(const char* pdf, int len)
120127 }
121128 }
122129
@@ -133,7 +140,7 @@ index be4dc0c..3346e0e 100644
133140 void PdfSignatureGenerator::InitSignature(int pageIndex, const char* szReason, const char* szReasonLabel, const char* szName, const char* szNameLabel, const char* szLocation, const char* szLocationLabel, const char* szFieldName, const char* szSubFilter)
134141 {
135142 LOG_DBG((0, "quella con tutti 0\n", ""));
136- @@ -115,187 +97 ,227 @@ void PdfSignatureGenerator::InitSignature(int pageIndex, float left, float botto
143+ @@ -115,187 +99 ,227 @@ void PdfSignatureGenerator::InitSignature(int pageIndex, float left, float botto
137144
138145 void PdfSignatureGenerator::InitSignature(int pageIndex, float left, float bottom, float width, float height, const char* szReason, const char* szReasonLabel, const char* szName, const char* szNameLabel, const char* szLocation, const char* szLocationLabel, const char* szFieldName, const char* szSubFilter, const char* szImagePath, const char* szDescription, const char* szGraphometricData, const char* szVersion)
139146 {
@@ -165,7 +172,7 @@ index be4dc0c..3346e0e 100644
165172+ float height0 = height * cropBoxHeight;
166173
167174+ LOG_DBG((0, "InitSignature", "m_actualLen %d", m_actualLen));
168- + int fulllen = m_actualLen * 2 + SINGNATURE_SIZE * 2 + (szGraphometricData ? (strlen(szGraphometricData) + strlen(szVersion) + 100) : 0) ;
175+ + int fulllen = m_actualLen * 2 + SINGNATURE_SIZE * 2;
169176+
170177+ printf("pdf rect: %f, %f, %f, %f\n", left0, bottom0, width0, height0);
171178+
@@ -275,26 +282,31 @@ index be4dc0c..3346e0e 100644
275282- }
276283- }
277284-
278- + PdfImage image(m_pPdfDocument);
279- + double scale;
280-
285+ -
281286- if(szGraphometricData && szGraphometricData[0])
282287- m_pSignatureField->SetGraphometricData(PdfString("Aruba_Sign_Biometric_Data"), PdfString(szGraphometricData), PdfString(szVersion));
283- + // Generate signature string
284- + std::string signatureStamp;
285-
288+ -
286289- LOG_DBG((0, "InitSignature", "szGraphometricData OK"));
287- + // Append date
288- + if(szName && szName[0])
289- + signatureStamp.append(szName);
290-
291- + // Append name
292- + if(szReason && szReason[0])
290+ + char* imgBuffer;
291+ + double scale;
292+ + streampos imgBufferSize;
293+ + ifstream img(szImagePath, ios::in|ios::binary|ios::ate);
294+ + std::string signatureStamp;
295+ + PdfImage image(m_pPdfDocument);
296+ +
297+ + // Copy the image in a buffer
298+ + if(img.is_open())
293299+ {
294- + signatureStamp.append("\n");
295- + signatureStamp.append(szReason);
300+ + imgBufferSize = img.tellg();
301+ + imgBuffer = new char[imgBufferSize];
302+ + img.seekg(0, ios::beg);
303+ + img.read(imgBuffer, imgBufferSize);
304+ + img.close();
296305+ }
297306
307+ + // Increase space we have to allocate
308+ + fulllen += imgBufferSize * 2;
309+
298310- // // crea il nuovo doc con il campo di firma
299311- // int fulllen = m_actualLen * 3 + SINGNATURE_SIZE * 2;
300312- // m_pMainDocbuffer = new char[fulllen];
@@ -305,9 +317,19 @@ index be4dc0c..3346e0e 100644
305317- LOG_DBG((0, "InitSignature", "m_actualLen %d", m_actualLen));
306318- // crea il nuovo doc con il campo di firma
307319- int fulllen = m_actualLen * 2 + SINGNATURE_SIZE * 2 + (szGraphometricData ? (strlen(szGraphometricData) + strlen(szVersion) + 100) : 0);
308- -
320+ + // Generate signature string
321+ + // Append date
322+ + if(szName && szName[0])
323+ + signatureStamp.append(szName);
324+
309325-
310- -
326+ + // Append name
327+ + if(szReason && szReason[0])
328+ + {
329+ + signatureStamp.append("\n");
330+ + signatureStamp.append(szReason);
331+ + }
332+
311333- int mainDoclen = 0;
312334- m_pMainDocbuffer = NULL;
313335- while (!m_pMainDocbuffer) {
@@ -317,7 +339,7 @@ index be4dc0c..3346e0e 100644
317339- PdfOutputDevice pdfOutDevice(m_pMainDocbuffer, fulllen);
318340- m_pPdfDocument->Write(&pdfOutDevice);
319341- mainDoclen = pdfOutDevice.GetLength();
320- + image.LoadFromPng(szImagePath );
342+ + image.LoadFromPngData((const unsigned char*)imgBuffer, imgBufferSize );
321343+ // Scale using width to try to avoid squeezing image
322344+ scale = (width0 / image.GetWidth());
323345+
@@ -327,6 +349,9 @@ index be4dc0c..3346e0e 100644
327349+ painter.Restore();
328350+ painter.DrawImage(left0, bottom0, &image, scale, scale);
329351+
352+ + // Release buffer memory
353+ + delete[] imgBuffer;
354+ +
330355+ // Create signature stamp
331356+ PdfFont* font = m_pPdfDocument->CreateFont(FONT_NAME, false,
332357+ PdfEncodingFactory::GlobalWinAnsiEncodingInstance(),
@@ -377,73 +402,53 @@ index be4dc0c..3346e0e 100644
377402
378403-
379404- // alloca un SignOutputDevice
380- - m_pSignDocbuffer = new char[fulllen];
381405+ // Set SubFilter
382406+ if(szSubFilter && szSubFilter[0])
383407+ {
384408+ m_pSignatureField->GetSignatureObject()->GetDictionary().AddKey("SubFilter",
385409+ PdfName(szSubFilter));
386410+ }
387-
388- - LOG_DBG((0, "InitSignature", "m_pSignDocbuffer %d", fulllen));
411+ +
389412+ // Add /SigFlags
390413+ pdf_int64 flags = 3;
391414+ m_pPdfDocument->GetAcroForm()->GetObject()->GetDictionary().AddKey(PdfName("SigFlags"),
392415+ PdfObject(flags));
416+ +
417+ + LOG_DBG((0, "InitSignature", "fulllen %d", fulllen));
418+ +
419+ m_pSignDocbuffer = new char[fulllen];
420+
421+ LOG_DBG((0, "InitSignature", "m_pSignDocbuffer %d", fulllen));
393422
394423- m_pFinalOutDevice = new PdfOutputDevice(m_pSignDocbuffer, fulllen);
395- - m_pSignOutputDevice = new PdfSignOutputDevice(m_pFinalOutDevice);
396- + // Keep trying to write the final document until we don't catch an error.
397- + // Double buffer lenght on error.
398- + m_pSignDocbuffer = NULL;
399- + while (!m_pSignDocbuffer) {
400- + try
401- + {
402- + LOG_DBG((0, "InitSignature", "fulllen %d", fulllen));
424+ + m_pFinalOutDevice = new PdfOutputDevice(m_pSignDocbuffer, fulllen);
425+ m_pSignOutputDevice = new PdfSignOutputDevice(m_pFinalOutDevice);
403426
404- - LOG_DBG((0, "InitSignature", "buffers OK %d", fulllen));
405- + m_pSignDocbuffer = new char[fulllen];
427+ LOG_DBG((0, "InitSignature", "buffers OK %d", fulllen));
406428
407429- // imposta la firma
408- - m_pSignOutputDevice->SetSignatureSize(SINGNATURE_SIZE);
430+ m_pSignOutputDevice->SetSignatureSize(SINGNATURE_SIZE);
409431-
410432- LOG_DBG((0, "InitSignature", "SetSignatureSize OK %d", SINGNATURE_SIZE));
411- + LOG_DBG((0, "InitSignature", "m_pSignDocbuffer %d", fulllen));
412-
433+ -
413434- // Scrive il documento reale
414435- m_pSignOutputDevice->Write(m_pMainDocbuffer, mainDoclen);
415- + m_pFinalOutDevice = new PdfOutputDevice(m_pSignDocbuffer, fulllen);
416- + m_pSignOutputDevice = new PdfSignOutputDevice(m_pFinalOutDevice);
417436
418437- LOG_DBG((0, "InitSignature", "Write OK %d", mainDoclen));
419- + LOG_DBG((0, "InitSignature", "buffers OK %d", fulllen));
420-
438+ -
421439- m_pSignOutputDevice->AdjustByteRange();
422- + m_pSignOutputDevice->SetSignatureSize(SINGNATURE_SIZE);
423-
440+ -
424441- LOG_DBG((0, "InitSignature", "AdjustByteRange OK"));
425- + LOG_DBG((0, "InitSignature", "SetSignatureSize OK %d", SINGNATURE_SIZE));
442+ + LOG_DBG((0, "InitSignature", "SetSignatureSize OK %d", SINGNATURE_SIZE));
426443
427- + m_pSignatureField->SetSignature(*m_pSignOutputDevice->GetSignatureBeacon());
428- +
429- + m_pPdfDocument->WriteUpdate(m_pSignOutputDevice);
430- + }
431- + catch(::PoDoFo::PdfError err)
432- + {
433- + if(m_pSignDocbuffer) {
434- + delete m_pSignDocbuffer;
435- + m_pSignDocbuffer = NULL;
436- + }
437- + if(m_pFinalOutDevice)
438- + delete m_pFinalOutDevice;
439- + if(m_pSignOutputDevice)
440- + delete m_pSignOutputDevice;
441- +
442- + LOG_DBG((0, "PdfError", "what %s", err.what()));
443- + printf("Allocating bigger buffer...\n");
444- +
445- + fulllen *= 2;
446- + }
444+ + m_pSignatureField->SetSignature(*m_pSignOutputDevice->GetSignatureBeacon());
445+ + try
446+ + {
447+ + m_pPdfDocument->WriteUpdate(m_pSignOutputDevice);
448+ + }
449+ + catch(::PoDoFo::PdfError err)
450+ + {
451+ + printf("PdfError: %s\n", err.what());
447452+ }
448453 }
449454
@@ -474,7 +479,7 @@ index be4dc0c..3346e0e 100644
474479 delete buffer;
475480 }
476481
477- @@ -303,6 +325 ,7 @@ void PdfSignatureGenerator::SetSignature(const char* signature, int len)
482+ @@ -303,6 +327 ,7 @@ void PdfSignatureGenerator::SetSignature(const char* signature, int len)
478483 {
479484 PdfData signatureData(signature, len);
480485 m_pSignOutputDevice->SetSignature(signatureData);
0 commit comments