Skip to content

Commit c342473

Browse files
authored
Merge pull request #212 from WitkacDevelopers/fix/sigfields_with_multiple_instances
Fix handling of signature fields with multiple instances
2 parents 5020581 + 66fcc08 commit c342473

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using iTextSharp.text;
5+
using iTextSharp.text.pdf;
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
8+
namespace iTextSharp.LGPLv2.Core.FunctionalTests;
9+
10+
[TestClass]
11+
public class PdfVerifySignatureTests
12+
{
13+
[TestMethod]
14+
public void Detect_Singature_Fields()
15+
{
16+
var filename = Path.Combine(TestUtils.GetBaseDir(), "iTextExamples", "resources", "pdfs", "issue_sig_fields.pdf");
17+
using var reader = new PdfReader(filename);
18+
var fields = reader.AcroFields;
19+
var names = fields.GetSignatureNames();
20+
Assert.AreEqual(1, names.Count);
21+
22+
23+
}
24+
25+
[TestMethod]
26+
public void Detect_PKCS7()
27+
{
28+
var filename = Path.Combine(TestUtils.GetBaseDir(), "iTextExamples", "resources", "pdfs", "issue_sig_fields.pdf");
29+
using var reader = new PdfReader(filename);
30+
var fields = reader.AcroFields;
31+
var names = fields.GetSignatureNames();
32+
Assert.AreEqual(1, names.Count);
33+
34+
var cover = fields.SignatureCoversWholeDocument(names[0]);
35+
Assert.IsTrue(cover);
36+
37+
38+
39+
var pkcs7 = fields.VerifySignature(names[0]);
40+
Assert.IsNotNull(pkcs7);
41+
Assert.IsTrue(pkcs7.Verify());
42+
}
43+
44+
45+
}
Binary file not shown.

src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/AcroFields.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,8 @@ public int GetRevision(string field)
12981298

12991299
return value[1];
13001300
}
1301+
1302+
13011303

13021304
/// <summary>
13031305
/// Gets the signature dictionary, the one keyed by /V.
@@ -1316,9 +1318,14 @@ public PdfDictionary GetSignatureDictionary(string name)
13161318
}
13171319

13181320
var item = fields[name];
1319-
var merged = item.GetMerged(idx: 0);
1321+
1322+
//get first merged dicionary with /V key
1323+
var merged = item.Merged.FirstOrDefault(x=>x.Contains(PdfName.V));
13201324

1325+
if (merged == null) return null;
13211326
return merged.GetAsDict(PdfName.V);
1327+
1328+
13221329
}
13231330

13241331
/// <summary>
@@ -2966,8 +2973,15 @@ private void findSignatureNames()
29662973
foreach (var entry in fields)
29672974
{
29682975
var item = entry.Value;
2969-
var merged = item.GetMerged(idx: 0);
2970-
2976+
2977+
// find first merged dictionary with /V
2978+
var merged = item.Merged.FirstOrDefault(x => x.Contains(PdfName.V));
2979+
2980+
if (merged== null)
2981+
{
2982+
continue;
2983+
}
2984+
29712985
if (!PdfName.Sig.Equals(merged.Get(PdfName.Ft)))
29722986
{
29732987
continue;
@@ -3242,6 +3256,7 @@ public class Item
32423256
/// <param name="idx">instance index</param>
32433257
/// <returns>the merged dictionary for the given instance</returns>
32443258
public PdfDictionary GetMerged(int idx) => Merged[idx];
3259+
32453260

32463261
/// <summary>
32473262
/// Retrieve the page number of the given instance

0 commit comments

Comments
 (0)