-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcie-middleware-fix-pades.patch
1217 lines (1097 loc) · 37.2 KB
/
cie-middleware-fix-pades.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From f4d19af4c7cdf59d924006d2fc7d885fee0b9140 Mon Sep 17 00:00:00 2001
From: Luca Magrone <luca@magrone.cc>
Date: Wed, 23 Oct 2024 15:50:44 +0200
Subject: [PATCH] cie_sign_sdk: PdfSignatureGenerator: Rewrite implementation
of PADES signature
As of now the code relies on a closed-source version of PoDoFo.
Write a new implementation of the PdfSignatureGenerator class based on
the code from podofosign and the old code of the class. Try to match
behaviour to the behaviour of versions for other platforms.
Use open-source PoDoFo.
Also fix loading document for verification with the correct method.
Signed-off-by: Luca Magrone <luca@magrone.cc>
---
cie_sign_sdk/include/PdfSignatureGenerator.h | 54 +-
cie_sign_sdk/include/PdfVerifier.h | 12 +-
cie_sign_sdk/src/PdfSignatureGenerator.cpp | 506 ++++++++++---------
cie_sign_sdk/src/PdfVerifier.cpp | 105 +++-
cie_sign_sdk/src/disigonsdk.cpp | 81 ++-
5 files changed, 470 insertions(+), 288 deletions(-)
diff --git a/cie_sign_sdk/include/PdfSignatureGenerator.h b/cie_sign_sdk/include/PdfSignatureGenerator.h
index 5a19d6f..90fe2c3 100644
--- a/cie_sign_sdk/include/PdfSignatureGenerator.h
+++ b/cie_sign_sdk/include/PdfSignatureGenerator.h
@@ -10,11 +10,18 @@
#ifndef _PDFSIGNATUREGENERATOR_H_
#define _PDFSIGNATUREGENERATOR_H_
-#include "podofo/podofo.h"
-#include "podofo/doc/PdfSignOutputDevice.h"
-#include "podofo/doc/PdfSignatureField.h"
+#include <podofo/podofo.h>
+#if PODOFO_VERSION_MAJOR < 1
+#if PODOFO_VERSION_MINOR < 10
+#include <podofo/doc/PdfSignOutputDevice.h>
+#include <podofo/doc/PdfSignatureField.h>
+#endif
+#else
+#error PoDoFo version not supported (yet)
+#endif
#include "ASN1/UUCByteArray.h"
+#define SIGNATURE_SIZE 10000
using namespace PoDoFo;
using namespace std;
@@ -34,37 +41,46 @@ public:
void 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);
+#if PODOFO_VERSION_MINOR < 10
void GetBufferForSignature(UUCByteArray& toSign);
void SetSignature(const char* signature, int len);
-
+#endif
+
void GetSignedPdf(UUCByteArray& signature);
- void AddFont(const char* szFontName, const char* szFontPath);
-
const double getWidth(int pageIndex);
const double getHeight(int pageIndex);
-
+
+#if PODOFO_VERSION_MINOR < 10
private:
+#endif
+
PdfMemDocument* m_pPdfDocument;
-
+
+#if PODOFO_VERSION_MINOR < 10
+private:
+
PdfSignatureField* m_pSignatureField;
-
+
PdfSignOutputDevice* m_pSignOutputDevice;
-
+
PdfOutputDevice* m_pFinalOutDevice;
-
- char* m_pMainDocbuffer;
-
+
char* m_pSignDocbuffer;
-
- const double lastSignatureY(int left, int bottom);
-
+
int m_actualLen;
-
- static bool IsSignatureField(const PdfMemDocument* pDoc, const PdfObject *const pObj);
-
+
+#else
+ PdfSignature* m_pSignatureField;
+
+ BufferStreamDevice* m_pSignOutputDevice;
+
+private:
+
+ charbuff m_pOutputBuffer;
+#endif
};
#endif // _PDFSIGNATUREGENERATOR_H_
diff --git a/cie_sign_sdk/include/PdfVerifier.h b/cie_sign_sdk/include/PdfVerifier.h
index 66f971d..837b708 100644
--- a/cie_sign_sdk/include/PdfVerifier.h
+++ b/cie_sign_sdk/include/PdfVerifier.h
@@ -11,9 +11,15 @@
#define _PDFVERIFIER_H_
-#include "podofo/podofo.h"
-#include "podofo/doc/PdfSignOutputDevice.h"
-#include "podofo/doc/PdfSignatureField.h"
+#include <podofo/podofo.h>
+#if PODOFO_VERSION_MAJOR < 1
+#if PODOFO_VERSION_MINOR < 10
+#include <podofo/doc/PdfSignOutputDevice.h>
+#include <podofo/doc/PdfSignatureField.h>
+#endif
+#else
+#error PoDoFo version not supported (yet)
+#endif
#include "ASN1/UUCByteArray.h"
#include "disigonsdk.h"
diff --git a/cie_sign_sdk/src/PdfSignatureGenerator.cpp b/cie_sign_sdk/src/PdfSignatureGenerator.cpp
index be4dc0c..9fbd54e 100644
--- a/cie_sign_sdk/src/PdfSignatureGenerator.cpp
+++ b/cie_sign_sdk/src/PdfSignatureGenerator.cpp
@@ -6,12 +6,17 @@
* Copyright 2012 __MyCompanyName__. All rights reserved.
*
*/
+#include <iostream>
+#include <fstream>
#include "PdfSignatureGenerator.h"
#include "PdfVerifier.h"
#include "UUCLogger.h"
-#define SINGNATURE_SIZE 10000
+#define MAX_TMP 1000
+#define FONT_NAME "DejaVu Sans"
+#define FONT_SIZE 5.0
+#define TXT_PAD 5
#ifdef CreateFont
#undef CreateFont
@@ -25,33 +30,35 @@ int GetNumberOfSignatures(PdfMemDocument* pPdfDocument);
USE_LOG;
-PdfSignatureGenerator::PdfSignatureGenerator()
-: m_pPdfDocument(NULL), m_pSignatureField(NULL), m_pSignOutputDevice(NULL), m_pFinalOutDevice(NULL),
-m_pMainDocbuffer(NULL), m_pSignDocbuffer(NULL)
-{
- PoDoFo::PdfError::EnableLogging(false);
-}
+PdfSignatureGenerator::PdfSignatureGenerator() :
+ m_pSignatureField(NULL),
+ m_pSignOutputDevice(NULL),
+#if PODOFO_VERSION_MINOR < 10
+ m_pFinalOutDevice(NULL),
+ m_pSignDocbuffer(NULL),
+#endif
+ m_pPdfDocument(NULL) {}
PdfSignatureGenerator::~PdfSignatureGenerator()
{
if(m_pPdfDocument)
delete m_pPdfDocument;
-
+
+#if PODOFO_VERSION_MINOR < 10
if(m_pSignatureField)
delete m_pSignatureField;
+#endif
if(m_pSignOutputDevice)
delete m_pSignOutputDevice;
+#if PODOFO_VERSION_MINOR < 10
if(m_pFinalOutDevice)
delete m_pFinalOutDevice;
- if(m_pMainDocbuffer)
- delete m_pMainDocbuffer;
-
if(m_pSignDocbuffer)
delete m_pSignDocbuffer;
-
+#endif
}
int PdfSignatureGenerator::Load(const char* pdf, int len)
@@ -61,25 +68,21 @@ int PdfSignatureGenerator::Load(const char* pdf, int len)
try
{
- printf("PDF");
- //printf("%s", (char *)pdf);
- printf("LENGTH");
- printf("%i", len);
- printf("STOP");
-
m_pPdfDocument = new PdfMemDocument();
- m_pPdfDocument->Load(pdf, len);
- printf("OK m_pPdfDocument");
- int nSigns = PDFVerifier::GetNumberOfSignatures(m_pPdfDocument);
- printf("OK nSigns: %d", nSigns);
+#if PODOFO_VERSION_MINOR < 10
+ m_pPdfDocument->LoadFromBuffer(pdf, len, true);
- if(nSigns > 0)
- {
- m_pPdfDocument->SetIncrementalUpdates(true);
- }
m_actualLen = len;
-
- return nSigns;
+
+#else
+ // Copy pdf buffer for later use
+ auto input = std::make_shared<SpanStreamDevice>(bufferview(pdf, len));
+ m_pSignOutputDevice = new BufferStreamDevice(m_pOutputBuffer);
+ input->CopyTo(*m_pSignOutputDevice);
+
+ m_pPdfDocument->LoadFromBuffer(bufferview(pdf, len));
+#endif
+ return PDFVerifier::GetNumberOfSignatures(m_pPdfDocument);
}
catch(::PoDoFo::PdfError& err)
{
@@ -91,16 +94,6 @@ int PdfSignatureGenerator::Load(const char* pdf, int len)
}
}
-void PdfSignatureGenerator::AddFont(const char* szFontName, const char* szFontPath)
-{
- //printf(szFontName);
- //printf(szFontPath);
-
-
- PdfFont* font = m_pPdfDocument->CreateFont(szFontName, false, false, PdfEncodingFactory::GlobalWinAnsiEncodingInstance(), PdfFontCache::eFontCreationFlags_AutoSelectBase14, true, szFontPath);
- PdfFont* font1 = m_pPdfDocument->CreateFont(szFontName, true, false, PdfEncodingFactory::GlobalWinAnsiEncodingInstance(), PdfFontCache::eFontCreationFlags_AutoSelectBase14, true, szFontPath);
-}
-
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)
{
LOG_DBG((0, "quella con tutti 0\n", ""));
@@ -115,187 +108,298 @@ void PdfSignatureGenerator::InitSignature(int pageIndex, float left, float botto
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)
{
- LOG_DBG((0, "--> InitSignature", "%d, %f, %f, %f, %f, %s, %s, %s, %s, %s, %s, %s, %s", pageIndex, left, bottom, width, height, szReason, szName, szLocation, szFieldName, szSubFilter, szImagePath, szGraphometricData, szVersion));
+ //printf("--> InitSignature %d, %f, %f, %f, %f, %s, %s, %s, %s, %s, %s, %s, %s\n", pageIndex, left, bottom, width, height, szReason, szName, szLocation, szFieldName, szSubFilter, szImagePath, szGraphometricData, szVersion);
//LOG_DBG((0, "--> InitSignature", ""));
-
+#if PODOFO_VERSION_MINOR < 10
+ int fulllen = m_actualLen * 2 + SIGNATURE_SIZE * 2;
+
if(m_pSignatureField)
delete m_pSignatureField;
PdfPage* pPage = m_pPdfDocument->GetPage(pageIndex);
- PdfRect cropBox = pPage->GetCropBox();
-
- float left0 = left * cropBox.GetWidth();
- float bottom0 = cropBox.GetHeight() - (bottom * cropBox.GetHeight());
-
- float width0 = width * cropBox.GetWidth();
- float height0 = height * cropBox.GetHeight();
+ PdfRect cropBox = pPage->GetCropBox();
+
+ float cropBoxWidth = cropBox.GetWidth();
+ float cropBoxHeight = cropBox.GetHeight();
+#else
+ PdfPage* pPage = &m_pPdfDocument->GetPages().GetPageAt(pageIndex);
+ Rect cropBox = pPage->GetCropBox();
+
+ float cropBoxWidth = cropBox.Width;
+ float cropBoxHeight = cropBox.Height;
+#endif
- printf("pdf rect: %f, %f, %f, %f\n", left0, bottom0, width0, height0);
+ float left0 = left * cropBoxWidth;
+ float bottom0 = cropBoxHeight - (bottom * cropBoxHeight);
+ float width0 = width * cropBoxWidth;
+ float height0 = height * cropBoxHeight;
+
+#if PODOFO_VERSION_MINOR < 10
PdfRect rect(left0, bottom0, width0, height0);
-
- LOG_DBG((0, "InitSignature", "PdfSignatureField"));
- m_pSignatureField = new PdfSignatureField(pPage, rect, m_pPdfDocument, PdfString(szFieldName), szSubFilter);
+ PdfAcroForm* acroForm = m_pPdfDocument->GetAcroForm();
+
+ // Add /SigFlags to acroform
+ pdf_int64 flags = 3;
+ acroForm->GetObject()->GetDictionary().AddKey(PdfName("SigFlags"), PdfObject(flags));
+
+ // Create annotation
+ PdfAnnotation* pAnnot = pPage->CreateAnnotation(ePdfAnnotation_Widget, rect);
+ pAnnot->SetFlags(static_cast<EPdfAnnotationFlags>(0x84));
+
+ m_pSignatureField = new PdfSignatureField(pAnnot, acroForm, m_pPdfDocument);
+#else
+ Rect rect(left0, bottom0, width0, height0);
+
+ m_pSignatureField = &pPage->CreateField<PdfSignature>(PdfString(szFieldName), rect);
+ m_pSignatureField->EnsureValueObject();
+
+ m_pSignatureField->MustGetWidget().SetFlags(static_cast<PdfAnnotationFlags>(0x84));
+#endif
LOG_DBG((0, "InitSignature", "PdfSignatureField OK"));
- //if(width * height == 0)
- // m_pSignatureField->SetHighlightingMode(ePdfHighlightingMode_None);
-
+ // This is the card holder's name
+ // Shouldn't this go in /Name? Goes in /Reason
if(szReason && szReason[0])
{
- PdfString reason(szReason);
- PdfString reasonLabel(szReasonLabel);
- m_pSignatureField->SetSignatureReason(reasonLabel, reason);
+ PdfString name(szReason);
+ m_pSignatureField->SetSignatureReason(name);
}
-
+
LOG_DBG((0, "InitSignature", "szReason OK"));
+#if PODOFO_VERSION_MINOR < 10
+ // /T: SignatureN
+ if(szFieldName && szFieldName[0])
+ {
+ // This corresponds to /T
+ PdfString fieldName = PdfString(szFieldName);
+ m_pSignatureField->SetFieldName(fieldName);
+ }
+
+ LOG_DBG((0, "InitSignature", "szFieldName OK"));
+#endif
+
if(szLocation && szLocation[0])
{
PdfString location(szLocation);
- PdfString locationLabel(szLocationLabel);
- m_pSignatureField->SetSignatureLocation(locationLabel, location);
+ m_pSignatureField->SetSignatureLocation(location);
}
LOG_DBG((0, "InitSignature", "szLocation OK"));
PdfDate now;
+#if PODOFO_VERSION_MINOR >= 10
+ now = PdfDate::LocalNow();
+#endif
m_pSignatureField->SetSignatureDate(now);
-
+
LOG_DBG((0, "InitSignature", "Date OK"));
+ // This is the signature date
+ // Shouldn't this go in /M? Goes in /Name
if(szName && szName[0])
{
- PdfString name(szName);
- PdfString nameLabel(szNameLabel);
- m_pSignatureField->SetSignatureName(nameLabel, name);
+#if PODOFO_VERSION_MINOR < 10
+ m_pSignatureField->GetSignatureObject()->GetDictionary()
+ .AddKey(PdfName("Name"), PdfObject(PdfString(szName)));
+#else
+ m_pSignatureField->SetSignerName(PdfString(szName));
+#endif
}
-
- LOG_DBG((0, "InitSignature", "szName OK"));
- m_pSignatureField->SetSignatureSize(SINGNATURE_SIZE);
-
- LOG_DBG((0, "InitSignature", "SINGNATURE_SIZE OK"));
+ LOG_DBG((0, "InitSignature", "szName OK"));
- //if((szImagePath && szImagePath[0]) || (szDescription && szDescription[0]))
+ // Create graphical signature with stamp if we have a picture
if(width * height > 0)
{
+#if PODOFO_VERSION_MINOR < 10
+ PdfXObject sigXObject(rect, m_pPdfDocument);
+#else
+ auto sigXObject = m_pPdfDocument->CreateXObjectForm(rect);
+#endif
+ PdfPainter painter;
+
try
{
- //m_pSignatureField->SetFontSize(5);
- m_pSignatureField->SetAppearance(szImagePath, szDescription);
- LOG_DBG((0, "InitSignature", "SetAppearance OK"));
- }
- catch( PdfError & error )
- {
- LOG_ERR((0, "InitSignature", "SetAppearance error: %s, %s", PdfError::ErrorMessage(error.GetError()), error.what()));
- }
- catch( PdfError * perror )
- {
- LOG_ERR((0, "InitSignature", "SetAppearance error2: %s, %s", PdfError::ErrorMessage(perror->GetError()), perror->what()));
- }
- catch(std::exception& ex)
- {
- LOG_ERR((0, "InitSignature", "SetAppearance std exception, %s", ex.what()));
- }
- catch(std::exception* pex)
- {
- LOG_ERR((0, "InitSignature", "SetAppearance std exception2, %s", pex->what()));
- }
- catch(...)
- {
- LOG_ERR((0, "InitSignature", "SetAppearance unknown error"));
- }
- }
-
+ char* imgBuffer = NULL;
+ double scale;
+ streampos imgBufferSize = 0;
+ ifstream img(szImagePath, ios::in|ios::binary|ios::ate);
+ std::string signatureStamp;
+#if PODOFO_VERSION_MINOR < 10
+ PdfImage image(m_pPdfDocument);
+#else
+ auto image = m_pPdfDocument->CreateImage();
+#endif
- if(szGraphometricData && szGraphometricData[0])
- m_pSignatureField->SetGraphometricData(PdfString("Aruba_Sign_Biometric_Data"), PdfString(szGraphometricData), PdfString(szVersion));
+ // Copy the image in a buffer
+ if(img.is_open())
+ {
+ imgBufferSize = img.tellg();
- LOG_DBG((0, "InitSignature", "szGraphometricData OK"));
+ // Increase space we have to allocate
+#if PODOFO_VERSION_MINOR < 10
+ fulllen += imgBufferSize * 2;
+#endif
+ imgBuffer = new char[imgBufferSize];
+ img.seekg(0, ios::beg);
+ img.read(imgBuffer, imgBufferSize);
+ img.close();
+ }
- // // crea il nuovo doc con il campo di firma
- // int fulllen = m_actualLen * 3 + SINGNATURE_SIZE * 2;
- // m_pMainDocbuffer = new char[fulllen];
- // PdfOutputDevice pdfOutDevice(m_pMainDocbuffer, fulllen);
- // m_pPdfDocument->Write(&pdfOutDevice);
- // int mainDoclen = pdfOutDevice.GetLength();
-
- LOG_DBG((0, "InitSignature", "m_actualLen %d", m_actualLen));
- // crea il nuovo doc con il campo di firma
- int fulllen = m_actualLen * 2 + SINGNATURE_SIZE * 2 + (szGraphometricData ? (strlen(szGraphometricData) + strlen(szVersion) + 100) : 0);
+ // Generate signature string
+ // Append date
+ if(szName && szName[0])
+ signatureStamp.append(szName);
-
+ // Append name
+ if(szReason && szReason[0])
+ {
+ signatureStamp.append("\n");
+ signatureStamp.append(szReason);
+ }
+
+#if PODOFO_VERSION_MINOR < 10
+ image.LoadFromPngData((const unsigned char*)imgBuffer, imgBufferSize);
+ // Scale using width to try to avoid squeezing image
+ scale = (width0 / image.GetWidth());
+#else
+ image->LoadFromBuffer(bufferview(imgBuffer, imgBufferSize));
+ scale = (width0 / image->GetWidth());
+#endif
- int mainDoclen = 0;
- m_pMainDocbuffer = NULL;
- while (!m_pMainDocbuffer) {
- try{
- LOG_DBG((0, "InitSignature", "fulllen %d", fulllen));
- m_pMainDocbuffer = new char[fulllen];
- PdfOutputDevice pdfOutDevice(m_pMainDocbuffer, fulllen);
- m_pPdfDocument->Write(&pdfOutDevice);
- mainDoclen = pdfOutDevice.GetLength();
+ // Draw signature
+#if PODOFO_VERSION_MINOR < 10
+ painter.SetPage(&sigXObject);
+#else
+ painter.SetCanvas(*sigXObject);
+#endif
+ painter.Save();
+ painter.Restore();
+#if PODOFO_VERSION_MINOR < 10
+ painter.DrawImage(left0, bottom0, &image, scale, scale);
+#else
+ painter.DrawImage(*image, left0, bottom0, scale, scale);
+#endif
+
+ // Release buffer memory
+ if(imgBufferSize != 0)
+ delete[] imgBuffer;
+
+ // Create signature stamp
+#if PODOFO_VERSION_MINOR < 10
+ PdfFont* font = m_pPdfDocument->CreateFont(FONT_NAME, false,
+ PdfEncodingFactory::GlobalWinAnsiEncodingInstance(),
+ // We set no embedding but it doesn't work
+ PdfFontCache::eFontCreationFlags_AutoSelectBase14, false);
+ PdfRect sigRect = PdfRect(left0 + TXT_PAD, bottom0 - (TXT_PAD * 2), width0, height0);
+ painter.SetFont(font);
+ font->SetFontSize(FONT_SIZE);
+ painter.DrawMultiLineText(sigRect, PdfString(signatureStamp));
+
+ m_pSignatureField->SetAppearanceStream(&sigXObject);
+
+ LOG_DBG((0, "InitSignature", "SetAppearanceStream OK"));
+
+ // Remove the font we embedded
+ acroForm->GetObject()->GetDictionary().RemoveKey(PdfName("DR"));
+ acroForm->GetObject()->GetDictionary().RemoveKey(PdfName("DA"));
}
- catch (::PoDoFo::PdfError err) {
- if(m_pMainDocbuffer) {
- delete m_pMainDocbuffer;
- m_pMainDocbuffer = NULL;
+ catch( PdfError & error )
+ {
+ LOG_ERR((0, "InitSignature", "SetAppearanceStream error: %s, %s", PdfError::ErrorMessage(error.GetError()), error.what()));
+ if(painter.GetPage())
+ {
+ try
+ {
+ painter.FinishPage();
+ }
+ catch(...)
+ {
+ }
}
-
- LOG_DBG((0, "PdfError", "what %s", err.what()));
- fulllen *= 2;
}
+
+ painter.FinishPage();
}
-
- LOG_DBG((0, "InitSignature", "m_pMainDocbuffer %d", fulllen));
-
- // alloca un SignOutputDevice
+ // Set SubFilter
+ if(szSubFilter && szSubFilter[0])
+ {
+ m_pSignatureField->GetSignatureObject()->GetDictionary().AddKey("SubFilter",
+ PdfName(szSubFilter));
+ }
+
+ LOG_DBG((0, "InitSignature", "fulllen %d", fulllen));
+
m_pSignDocbuffer = new char[fulllen];
LOG_DBG((0, "InitSignature", "m_pSignDocbuffer %d", fulllen));
- m_pFinalOutDevice = new PdfOutputDevice(m_pSignDocbuffer, fulllen);
+ m_pFinalOutDevice = new PdfOutputDevice(m_pSignDocbuffer, fulllen);
m_pSignOutputDevice = new PdfSignOutputDevice(m_pFinalOutDevice);
LOG_DBG((0, "InitSignature", "buffers OK %d", fulllen));
- // imposta la firma
- m_pSignOutputDevice->SetSignatureSize(SINGNATURE_SIZE);
-
- LOG_DBG((0, "InitSignature", "SetSignatureSize OK %d", SINGNATURE_SIZE));
+ m_pSignOutputDevice->SetSignatureSize(SIGNATURE_SIZE);
- // Scrive il documento reale
- m_pSignOutputDevice->Write(m_pMainDocbuffer, mainDoclen);
+ LOG_DBG((0, "InitSignature", "SetSignatureSize OK %d", SIGNATURE_SIZE));
- LOG_DBG((0, "InitSignature", "Write OK %d", mainDoclen));
+ m_pSignatureField->SetSignature(*m_pSignOutputDevice->GetSignatureBeacon());
+ try
+ {
+ m_pPdfDocument->WriteUpdate(m_pSignOutputDevice);
+ }
+ catch(::PoDoFo::PdfError err)
+ {
+ printf("PdfError: %s\n", err.what());
+ }
+#else
+ PdfFont* font = m_pPdfDocument->GetFonts().SearchFont(FONT_NAME);
+ Rect sigRect = Rect(left0 + TXT_PAD, bottom0 - (TXT_PAD * 2), width0, height0);
+ painter.TextState.SetFont(*font, FONT_SIZE);
+ painter.DrawTextMultiLine(signatureStamp, sigRect);
- m_pSignOutputDevice->AdjustByteRange();
+ m_pSignatureField->SetAppearanceStream(*sigXObject);
+
+ LOG_DBG((0, "InitSignature", "SetAppearanceStream OK"));
- LOG_DBG((0, "InitSignature", "AdjustByteRange OK"));
+ // Remove the font we embedded
+ m_pPdfDocument->GetAcroForm()->GetObject().GetDictionary().RemoveKey(PdfName("DR"));
+ m_pPdfDocument->GetAcroForm()->GetObject().GetDictionary().RemoveKey(PdfName("DA"));
+ }
+ catch(...)
+ {
+ }
+ painter.FinishDrawing();
+ }
+#endif
}
+#if PODOFO_VERSION_MINOR < 10
void PdfSignatureGenerator::GetBufferForSignature(UUCByteArray& toSign)
{
- //int fulllen = m_actualLen * 2 + SINGNATURE_SIZE * 2;
int len = m_pSignOutputDevice->GetLength() * 2;
-
char* buffer = new char[len];
-
+ int nRead;
+
+ m_pSignOutputDevice->AdjustByteRange();
+ LOG_DBG((0, "SetSignature", "AdjustByteRange OK"));
+
m_pSignOutputDevice->Seek(0);
-
- int nRead = m_pSignOutputDevice->ReadForSignature(buffer, len);
+
+ nRead = m_pSignOutputDevice->ReadForSignature(buffer, len);
if(nRead == -1)
throw nRead;
-
+
toSign.append((BYTE*)buffer, nRead);
-
+
delete buffer;
}
@@ -303,7 +407,9 @@ void PdfSignatureGenerator::SetSignature(const char* signature, int len)
{
PdfData signatureData(signature, len);
m_pSignOutputDevice->SetSignature(signatureData);
+ m_pSignOutputDevice->Flush();
}
+#endif
void PdfSignatureGenerator::GetSignedPdf(UUCByteArray& signedPdf)
{
@@ -311,114 +417,38 @@ void PdfSignatureGenerator::GetSignedPdf(UUCByteArray& signedPdf)
char* szSignedPdf = new char[finalLength];
m_pSignOutputDevice->Seek(0);
+#if PODOFO_VERSION_MINOR < 10
int nRead = m_pSignOutputDevice->Read(szSignedPdf, finalLength);
-
+
signedPdf.append((BYTE*)szSignedPdf, nRead);
-
+#else
+ m_pSignOutputDevice->Read(szSignedPdf, finalLength);
+ signedPdf.append((BYTE*)szSignedPdf, finalLength);
+#endif
+
delete szSignedPdf;
}
const double PdfSignatureGenerator::getWidth(int pageIndex) {
if (m_pPdfDocument) {
+#if PODOFO_VERSION_MINOR < 10
PdfPage* pPage = m_pPdfDocument->GetPage(pageIndex);
return pPage->GetPageSize().GetWidth();
+#else
+ return m_pPdfDocument->GetPages().GetPageAt(pageIndex).GetRect().Width;
+#endif
}
return 0;
}
const double PdfSignatureGenerator::getHeight(int pageIndex) {
if (m_pPdfDocument) {
+#if PODOFO_VERSION_MINOR < 10
PdfPage* pPage = m_pPdfDocument->GetPage(pageIndex);
return pPage->GetPageSize().GetHeight();
+#else
+ return m_pPdfDocument->GetPages().GetPageAt(pageIndex).GetRect().Height;
+#endif
}
return 0;
}
-
-const double PdfSignatureGenerator::lastSignatureY(int left, int bottom) {
- if(!m_pPdfDocument)
- return -1;
- /// Find the document catalog dictionary
- const PdfObject *const trailer = m_pPdfDocument->GetTrailer();
- if (! trailer->IsDictionary())
- return -1;
- const PdfObject *const catalogRef = trailer->GetDictionary().GetKey(PdfName("Root"));
- if (catalogRef==0 || ! catalogRef->IsReference())
- return -2;//throw std::invalid_argument("Invalid /Root entry");
- const PdfObject *const catalog =
- m_pPdfDocument->GetObjects().GetObject(catalogRef->GetReference());
- if (catalog==0 || !catalog->IsDictionary())
- return -3;//throw std::invalid_argument("Invalid or non-dictionary
- //referenced by /Root entry");
-
- /// Find the Fields array in catalog dictionary
- const PdfObject *acroFormValue = catalog->GetDictionary().GetKey(PdfName("AcroForm"));
- if (acroFormValue == 0)
- return bottom;
- if (acroFormValue->IsReference())
- acroFormValue = m_pPdfDocument->GetObjects().GetObject(acroFormValue->GetReference());
-
- if (!acroFormValue->IsDictionary())
- return bottom;
-
- const PdfObject *fieldsValue = acroFormValue->GetDictionary().GetKey(PdfName("Fields"));
- if (fieldsValue == 0)
- return bottom;
-
- if (fieldsValue->IsReference())
- fieldsValue = m_pPdfDocument->GetObjects().GetObject(acroFormValue->GetReference());
-
- if (!fieldsValue->IsArray())
- return bottom;
-
- vector<const PdfObject*> signatureVector;
-
- /// Verify if each object of the array is a signature field
- const PdfArray &array = fieldsValue->GetArray();
-
- int minY = bottom;
-
- for (unsigned int i=0; i<array.size(); i++) {
- const PdfObject * pObj = m_pPdfDocument->GetObjects().GetObject(array[i].GetReference());
- if (IsSignatureField(m_pPdfDocument, pObj)) {
- const PdfObject *const keyRect = pObj->GetDictionary().GetKey(PdfName("Rect"));
- if (keyRect == 0) {
- return bottom;
- }
- PdfArray rectArray = keyRect->GetArray();
- PdfRect rect;
- rect.FromArray(rectArray);
-
- if (rect.GetLeft() == left) {
- minY = (rect.GetBottom() <= minY && rect.GetBottom()!=0) ? rect.GetBottom()-85 : minY;
- }
- }
- }
- return minY;
-}
-
-bool PdfSignatureGenerator::IsSignatureField(const PdfMemDocument* pDoc, const PdfObject *const pObj)
-{
- if (pObj == 0) return false;
-
- if (!pObj->IsDictionary())
- return false;
-
- const PdfObject *const keyFTValue = pObj->GetDictionary().GetKey(PdfName("FT"));
- if (keyFTValue == 0)
- return false;
-
- string value;
- keyFTValue->ToString(value);
- if (value != "/Sig")
- return false;
-
- const PdfObject *const keyVValue = pObj->GetDictionary().GetKey(PdfName("V"));
- if (keyVValue == 0)
- return false;
-
- const PdfObject *const signature = pDoc->GetObjects().GetObject(keyVValue->GetReference());
- if (signature->IsDictionary())
- return true;
- else
- return false;
-}
diff --git a/cie_sign_sdk/src/PdfVerifier.cpp b/cie_sign_sdk/src/PdfVerifier.cpp
index 2285603..5fb0436 100644
--- a/cie_sign_sdk/src/PdfVerifier.cpp
+++ b/cie_sign_sdk/src/PdfVerifier.cpp
@@ -42,7 +42,11 @@ int PDFVerifier::Load(const char* pdf, int len)
try
{
m_pPdfDocument = new PdfMemDocument();
- m_pPdfDocument->Load(pdf, len);
+#if PODOFO_VERSION_MINOR < 10
+ m_pPdfDocument->LoadFromBuffer(pdf, len, true);
+#else
+ m_pPdfDocument->LoadFromBuffer(bufferview(pdf, len));
+#endif
m_actualLen = len;
m_szDocBuffer = (char*)pdf;
@@ -66,7 +70,11 @@ int PDFVerifier::Load(const char* szFilePath)
try
{
m_pPdfDocument = new PdfMemDocument();
+#if PODOFO_VERSION_MINOR < 10
+ m_pPdfDocument->Load(szFilePath, false);
+#else
m_pPdfDocument->Load(szFilePath);
+#endif
BYTE buffer[BUFFERSIZE];
int nRead = 0;
@@ -109,7 +117,11 @@ int PDFVerifier::GetNumberOfSignatures(const char* szFilePath)
try {
+#if PODOFO_VERSION_MINOR < 10
+ doc.Load(szFilePath, false);
+#else
doc.Load(szFilePath);
+#endif
pfnCrashliticsLog("file loaded");
@@ -128,52 +140,37 @@ int PDFVerifier::GetNumberOfSignatures(const char* szFilePath)
int PDFVerifier::GetNumberOfSignatures(PdfMemDocument* pPdfDocument)
{
- printf("GetNumberOfSignatures");
-
/// Find the document catalog dictionary
+#if PODOFO_VERSION_MINOR < 10
const PdfObject *const trailer = pPdfDocument->GetTrailer();
if (!trailer->IsDictionary())
return -1;
- printf("trailer ok");
-
const PdfObject *const catalogRef = trailer->GetDictionary().GetKey(PdfName("Root"));
if (catalogRef==0 || !catalogRef->IsReference())
return -2;//throw std::invalid_argument("Invalid /Root entry");
- printf("Catalogref ok");
-
const PdfObject *const catalog =
pPdfDocument->GetObjects().GetObject(catalogRef->GetReference());
if (catalog==0 || !catalog->IsDictionary())
return -3;//throw std::invalid_argument("Invalid or non-dictionary
//referenced by /Root entry");
- printf("catalog ok");
-
/// Find the Fields array in catalog dictionary
const PdfObject *acroFormValue = catalog->GetDictionary().GetKey(PdfName("AcroForm"));
if (acroFormValue == 0)
return 0;
- printf("acroform ok 1");
-
if (acroFormValue->IsReference())
acroFormValue = pPdfDocument->GetObjects().GetObject(acroFormValue->GetReference());
- printf("acroform ok 2");
-
if (!acroFormValue->IsDictionary())
return 0;
- printf("acroform ok 3");
-
const PdfObject *fieldsValue = acroFormValue->GetDictionary().GetKey(PdfName("Fields"));
if (fieldsValue == 0)
return 0;
- printf("fieldsValue ok");
-
try
{
if (fieldsValue->IsReference())
@@ -191,12 +188,18 @@ int PDFVerifier::GetNumberOfSignatures(PdfMemDocument* pPdfDocument)
printf("First chance Exception\n");
}
- printf("fieldsValue ok 2");
-
if (!fieldsValue->IsArray())
return 0;
- printf("fieldsValue is array");
+#else
+ auto& acroForm = pPdfDocument->GetOrCreateAcroForm();
+ const PdfObject *fieldsValue = acroForm.GetObject().GetDictionary().GetKey("Fields");
+ if(fieldsValue->GetDataType() == PdfDataType::Reference)
+ fieldsValue = pPdfDocument->GetObjects().GetObject(fieldsValue->GetReference());
+
+ if(!fieldsValue || fieldsValue->GetDataType() != PdfDataType::Array)
+ return 0;
+#endif
/// Verify if each object of the array is a signature field
int n = 0;
@@ -228,6 +231,7 @@ int PDFVerifier::VerifySignature(int index, const char* szDate, char* signatureT
return -1;
/// Find the document catalog dictionary
+#if PODOFO_VERSION_MINOR < 10
const PdfObject *const trailer = m_pPdfDocument->GetTrailer();
if (!trailer->IsDictionary())
return -1;
@@ -266,8 +270,17 @@ int PDFVerifier::VerifySignature(int index, const char* szDate, char* signatureT
if (!fieldsValue->IsArray())
return 0;
- vector<const PdfObject*> signatureVector;
+#else
+ auto& acroForm = m_pPdfDocument->GetOrCreateAcroForm();
+ const PdfObject *fieldsValue = acroForm.GetObject().GetDictionary().GetKey("Fields");
+ if(fieldsValue->GetDataType() == PdfDataType::Reference)
+ fieldsValue = m_pPdfDocument->GetObjects().GetObject(fieldsValue->GetReference());
+ if(!fieldsValue || fieldsValue->GetDataType() != PdfDataType::Array)
+ return 0;
+#endif
+ vector<const PdfObject*> signatureVector;
+
/// Verify if each object of the array is a signature field
const PdfArray &array = fieldsValue->GetArray();
for (unsigned int i=0; i<array.size(); i++)
@@ -298,10 +311,16 @@ int PDFVerifier::VerifySignature(const PdfMemDocument* pDoc, const PdfObject *co
if (keyFTValue == 0)
return -2;
+#if PODOFO_VERSION_MINOR < 10
string value;
keyFTValue->ToString(value);
if (value != "/Sig")
return -3;
+#else
+ const PdfName value = keyFTValue->GetName();
+ if (value != "Sig")
+ return -3;
+#endif
const PdfObject *const keyVValue = pObj->GetDictionary().GetKey(PdfName("V"));
if (keyVValue == 0)
@@ -322,6 +341,12 @@ int PDFVerifier::VerifySignature(const PdfMemDocument* pDoc, const PdfObject *co
const PdfObject *const keySubFilter = signature->GetDictionary().GetKey(PdfName("SubFilter"));
keySubFilter->ToString(subfilter);
+
+#if PODOFO_VERSION_MINOR >= 10
+ // Podofo 0.10.x adds an invisible trailing character that makes comparison fail
+ if(!subfilter.empty())
+ subfilter.pop_back();
+#endif
const char* szEntry = strtok((char*)byteRange.c_str(), " []");
@@ -343,7 +368,7 @@ int PDFVerifier::VerifySignature(const PdfMemDocument* pDoc, const PdfObject *co
CSignedData signedData(signedDocument.getSignedData());
strcpy(signatureType, subfilter.c_str());
-
+
if(subfilter == "/adbe.pkcs7.detached" || subfilter == "/ETSI.CAdES.detached")
{
//NSLog(@"detached %s", subfilter.c_str());
@@ -385,7 +410,7 @@ int PDFVerifier::VerifySignature(const PdfMemDocument* pDoc, const PdfObject *co
else if(subfilter == "/adbe.pkcs7.sha1")
{
//NSLog(@"sha1 %s", subfilter.c_str());
-
+
return signedData.verify(0, szDate, pRevocationInfo);
}
@@ -427,10 +452,16 @@ bool PDFVerifier::IsSignatureField(const PdfMemDocument* pDoc, const PdfObject *