Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added method that locks whole document #26

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions itext.tests/itext.sign.tests/itext/signatures/PdfSignerUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,34 @@ public virtual void SignWithFieldLockNotNullTest() {
NUnit.Framework.Assert.IsTrue(signer.closed);
}

[NUnit.Framework.Test]
public virtual void SignWithFieldLockNotNullAndLocksWholeDocumentTest()
{
PdfSigner signer = new PdfSigner(new PdfReader(new MemoryStream(CreateSimpleDocument(PdfVersion.PDF_2_0)))
, new ByteArrayOutputStream(), new StampingProperties());
signer.cryptoDictionary = new PdfSignature();
signer.appearance.SetPageRect(new Rectangle(100, 100, 10, 10));
var documentLocker = new PdfSigFieldLock().SetDocumentPermissions(PdfSigFieldLock.LockPermissions.NO_CHANGES_ALLOWED).SetFieldLock(PdfSigFieldLock.LockAction.ALL);
signer.SetFieldLockDict(documentLocker);
IExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256);
signer.SignDetached(pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CADES);
NUnit.Framework.Assert.IsTrue(signer.closed);
}

[NUnit.Framework.Test]
public virtual void SignWithFieldLockNotNullAndLocksFieldTest()
{
PdfSigner signer = new PdfSigner(new PdfReader(new MemoryStream(CreateSimpleDocument(PdfVersion.PDF_2_0)))
, new ByteArrayOutputStream(), new StampingProperties());
signer.cryptoDictionary = new PdfSignature();
signer.appearance.SetPageRect(new Rectangle(100, 100, 10, 10));
var documentLocker = new PdfSigFieldLock().SetFieldLock(PdfSigFieldLock.LockAction.ALL);
signer.SetFieldLockDict(documentLocker);
IExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256);
signer.SignDetached(pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CADES);
NUnit.Framework.Assert.IsTrue(signer.closed);
}

[NUnit.Framework.Test]
public virtual void SignDetachedWhenAlreadySignedIsNotPossibleTest() {
PdfSigner signer = new PdfSigner(new PdfReader(new MemoryStream(CreateSimpleDocument())), new ByteArrayOutputStream
Expand Down
38 changes: 37 additions & 1 deletion itext/itext.sign/itext/signatures/PdfSigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -742,8 +742,10 @@ protected internal virtual void PreClose(IDictionary<PdfName, int?> exclusionSiz
if (certificationLevel > 0) {
AddDocMDP(cryptoDictionary);
}
if (fieldLock != null) {
if (fieldLock != null)
{
AddFieldMDP(cryptoDictionary, fieldLock);
LockWholeDocument(fieldLock);
}
if (signatureEvent != null) {
signatureEvent.GetSignatureDictionary(cryptoDictionary);
Expand Down Expand Up @@ -814,6 +816,40 @@ protected internal virtual void PreClose(IDictionary<PdfName, int?> exclusionSiz
}
}

/// <summary>Adds keys to the signature dictionary that define the document permissions.</summary>
/// <remarks>
/// Adds keys to the signature dictionary that define the field permissions.
/// This method is only used for signatures that lock the entire document.
/// </remarks>
/// <param name="fieldLock"></param>
/// the
/// <see cref="iText.Forms.PdfSigFieldLock"/>
/// instance specified the field lock to be set
/// </param>
protected internal virtual void LockWholeDocument(PdfSigFieldLock fieldLock)
{
PdfDictionary pdfObject = fieldLock.GetPdfObject();

if (pdfObject.ContainsKey(PdfName.P))
{
if (pdfObject.ContainsKey(PdfName.Fields))
{
pdfObject.Remove(PdfName.Fields);
}
if (pdfObject.ContainsKey(PdfName.Action))
{
pdfObject.Remove(PdfName.Action);
}
if (pdfObject.ContainsKey(PdfName.All))
{
pdfObject.Remove(PdfName.All);
}

document.GetCatalog().Put(PdfName.Action, pdfObject);
document.GetCatalog().SetModified();
}
}

/// <summary>Populates already existing signature form field in the acroForm object.</summary>
/// <remarks>
/// Populates already existing signature form field in the acroForm object.
Expand Down