public void Sign1(String src, String name, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, String reason, String location) { PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties()); // Create the signature appearance PdfSignatureAppearance appearance = signer.GetSignatureAppearance(); appearance .SetReason(reason) .SetLocation(location); // This name corresponds to the name of the field that already exists in the document. signer.SetFieldName(name); // Set the custom text and a custom font appearance.SetLayer2Text("This document was signed by Bruno Specimen"); appearance.SetLayer2Font(PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN)); IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm); // Sign the document using the detached mode, CMS or CAdES equivalent. signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }
public void Sign(String src, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, String reason, String location, ICollection <ICrlClient> crlList, IOcspClient ocspClient, ITSAClient tsaClient, int estimatedSize) { PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties()); // Create the signature appearance Rectangle rect = new Rectangle(36, 648, 200, 100); PdfSignatureAppearance appearance = signer.GetSignatureAppearance(); appearance .SetReason(reason) .SetLocation(location) // Specify if the appearance before field is signed will be used // as a background for the signed field. The "false" value is the default value. .SetReuseAppearance(false) .SetPageRect(rect) .SetPageNumber(1); signer.SetFieldName("sig"); IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm); // Sign the document using the detached mode, CMS or CAdES equivalent. // Pass the created TSAClient to the signing method. signer.SignDetached(pks, chain, crlList, ocspClient, tsaClient, estimatedSize, subfilter); }
public void Sign(String src, String tmp, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, String reason, String location) { PdfReader reader = new PdfReader(src); // Pass the temporary file's path to the PdfSigner constructor PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), tmp, new StampingProperties()); // Create the signature appearance Rectangle rect = new Rectangle(36, 648, 200, 100); PdfSignatureAppearance appearance = signer.GetSignatureAppearance(); appearance .SetReason(reason) .SetLocation(location) // Specify if the appearance before field is signed will be used // as a background for the signed field. The "false" value is the default value. .SetReuseAppearance(false) .SetPageRect(rect) .SetPageNumber(1); signer.SetFieldName("sig"); IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm); // Sign the document using the detached mode, CMS or CAdES equivalent. signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }
public void Sign2(String src, String name, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, String reason, String location) { PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties()); PdfSignatureAppearance appearance = signer.GetSignatureAppearance(); appearance.SetReason(reason); appearance.SetLocation(location); signer.SetFieldName(name); // Creating the appearance for layer 2 PdfFormXObject n2 = appearance.GetLayer2(); // Custom text, custom font, and right-to-left writing // Characters: لورانس العرب Text text = new Text("\u0644\u0648\u0631\u0627\u0646\u0633 \u0627\u0644\u0639\u0631\u0628"); text.SetFont(PdfFontFactory.CreateFont("../../../resources/font/NotoNaskhArabic-Regular.ttf", PdfEncodings.IDENTITY_H, true)); text.SetBaseDirection(BaseDirection.RIGHT_TO_LEFT); new Canvas(n2, signer.GetDocument()).Add(new Paragraph(text).SetTextAlignment(TextAlignment.RIGHT)); IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm); signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }
/// <exception cref="Org.BouncyCastle.Security.GeneralSecurityException"/> /// <exception cref="System.IO.IOException"/> protected internal virtual void Sign(String src, String name, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, String reason, String location, Rectangle rectangleForNewField, bool setReuseAppearance, bool isAppendMode, int certificationLevel, float?fontSize ) { PdfReader reader = new PdfReader(src); StampingProperties properties = new StampingProperties(); if (isAppendMode) { properties.UseAppendMode(); } PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), properties); signer.SetCertificationLevel(certificationLevel); PdfFont font = PdfFontFactory.CreateFont(FONT, "WinAnsi", true); // Creating the appearance PdfSignatureAppearance appearance = signer.GetSignatureAppearance().SetReason(reason).SetLocation(location ).SetLayer2Font(font).SetReuseAppearance(setReuseAppearance); if (rectangleForNewField != null) { appearance.SetPageRect(rectangleForNewField); } if (fontSize != null) { appearance.SetLayer2FontSize((float)fontSize); } signer.SetFieldName(name); // Creating the signature IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm); signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }
public static void Sign(string fileName, string signedFileName, string reason, string location, string privateKeyFileName, string certFileName, string password) { PdfReader reader = new PdfReader(fileName); PdfWriter write = new PdfWriter(signedFileName); PdfSigner signer = new PdfSigner(reader, write, false); PdfSignatureAppearance appearance = signer.GetSignatureAppearance(); appearance.SetReason(reason); appearance.SetLocation(location); appearance.SetReuseAppearance(false); Rectangle rect = new Rectangle(36, 648, 200, 100); appearance.SetPageRect(rect); appearance.SetPageNumber(1); signer.SetFieldName("sig"); IExternalSignature pks = new PrivateKeySignature(ReadPrivateKey(privateKeyFileName, password), GetEncryptionAlgorithm()); X509CertificateParser parser = new X509CertificateParser(); X509Certificate cert = LoadCertificate(certFileName); X509Certificate[] chain = new X509Certificate[1]; chain[0] = cert; PdfSigner.CryptoStandard subfilter = GetSubFilter(); signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }
public void Sign(String src, String dest, X509Certificate[] chain, PdfSigner.CryptoStandard subfilter, String reason, String location) { PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties()); // Create the signature appearance //Rectangle rect = new Rectangle(36, 648, 200, 100); //iText.Signatures.PdfSignatureAppearance appearance = signer.GetSignatureAppearance(); //appearance // .SetReason(reason) // .SetLocation(location) // .SetPageRect(rect) // .SetPageNumber(1); signer.SetFieldName("QAMgr"); //IExternalDigest digest = new BouncyCastleDigest(); IExternalSignature signature = new ServerSignature(); //IExternalSignature sing = new // Sign the document using the detached mode, CMS or CAdES equivalent. //signer.SignDetached(digest, signature, chain, null, null, null, // 0, subfilter); // Sign the document using the detached mode, CMS or CAdES equivalent. signer.SignDetached(signature, chain, null, null, null, 0, subfilter); }
/// <exception cref="Org.BouncyCastle.Security.GeneralSecurityException"/> /// <exception cref="System.IO.IOException"/> protected internal virtual void Sign(String src, String name, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, String reason, String location, Rectangle rectangleForNewField, bool setReuseAppearance, bool isAppendMode) { Sign(src, name, dest, chain, pk, digestAlgorithm, subfilter, reason, location, rectangleForNewField, setReuseAppearance , isAppendMode, PdfSigner.NOT_CERTIFIED, null); }
public void Sign(String src, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, int certificationLevel, String reason, String location) { PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties()); // Create the signature appearance PdfSignatureAppearance appearance = signer.GetSignatureAppearance(); appearance.SetReason(reason); appearance.SetLocation(location); Rectangle rect = new Rectangle(36, 648, 200, 100); appearance.SetPageRect(rect).SetPageNumber(1); signer.SetFieldName("sig"); /* Set the document's certification level. This parameter defines if changes are allowed * after the applying of the signature. */ signer.SetCertificationLevel(certificationLevel); PrivateKeySignature pks = new PrivateKeySignature(pk, digestAlgorithm); // Sign the document using the detached mode, CMS or CAdES equivalent. signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }
public void Sign(String src, String name, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, String reason, String location, PdfSignatureAppearance.RenderingMode renderingMode, ImageData image) { PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties()); // Create the signature appearance PdfSignatureAppearance appearance = signer.GetSignatureAppearance(); appearance.SetReason(reason); appearance.SetLocation(location); // This name corresponds to the name of the field that already exists in the document. signer.SetFieldName(name); appearance.SetLayer2Text("Signed on " + DateTime.Now); // Set the rendering mode for this signature. appearance.SetRenderingMode(renderingMode); // Set the Image object to render when the rendering mode is set to RenderingMode.GRAPHIC // or RenderingMode.GRAPHIC_AND_DESCRIPTION. appearance.SetSignatureGraphic(image); PrivateKeySignature pks = new PrivateKeySignature(pk, digestAlgorithm); // Sign the document using the detached mode, CMS or CAdES equivalent. signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }
protected internal virtual void Sign(String src, String name, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, int certificationLevel) { PdfReader reader = new PdfReader(src); StampingProperties properties = new StampingProperties(); properties.UseAppendMode(); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), properties); signer.SetCertificationLevel(certificationLevel); signer.SetFieldName(name); // Creating the signature IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm); signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }
public void Sign(String src, String name, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, String reason, String location) { PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties()); // Create the signature appearance signer.GetSignatureAppearance() .SetReason(reason) .SetLocation(location); signer.SetFieldName(name); IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm); // Sign the document using the detached mode, CMS or CAdES equivalent. signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }
public void SignAgain(String src, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, String reason, String location) { PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties().UseAppendMode()); PdfSignatureAppearance appearance = signer.GetSignatureAppearance(); appearance.SetReason(reason); appearance.SetLocation(location); appearance.SetReuseAppearance(false); Rectangle rect = new Rectangle(36, 700, 200, 100); appearance.SetPageRect(rect).SetPageNumber(1); signer.SetFieldName("Signature2"); PrivateKeySignature pks = new PrivateKeySignature(pk, digestAlgorithm); signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }
public void Sign(String src, String name, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, String reason, String location) { PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties()); // Create the signature appearance PdfSignatureAppearance appearance = signer.GetSignatureAppearance(); appearance .SetReason(reason) .SetLocation(location); // This name corresponds to the name of the field that already exists in the document. signer.SetFieldName(name); // Get the background layer and draw a gray rectangle as a background. PdfFormXObject n0 = appearance.GetLayer0(); float x = n0.GetBBox().ToRectangle().GetLeft(); float y = n0.GetBBox().ToRectangle().GetBottom(); float width = n0.GetBBox().ToRectangle().GetWidth(); float height = n0.GetBBox().ToRectangle().GetHeight(); PdfCanvas canvas = new PdfCanvas(n0, signer.GetDocument()); canvas.SetFillColor(ColorConstants.LIGHT_GRAY); canvas.Rectangle(x, y, width, height); canvas.Fill(); // Set the signature information on layer 2 PdfFormXObject n2 = appearance.GetLayer2(); Paragraph p = new Paragraph("This document was signed by Bruno Specimen."); new Canvas(n2, signer.GetDocument()).Add(p); IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm); // Sign the document using the detached mode, CMS or CAdES equivalent. signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }
public void Sign3(String src, String name, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, String reason, String location) { PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties()); PdfSignatureAppearance appearance = signer.GetSignatureAppearance(); appearance.SetReason(reason); appearance.SetLocation(location); signer.SetFieldName(name); // Set a custom text and background image appearance.SetLayer2Text("This document was signed by Bruno Specimen"); appearance.SetImage(ImageDataFactory.Create(IMG)); appearance.SetImageScale(1); PrivateKeySignature pks = new PrivateKeySignature(pk, digestAlgorithm); signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }
public void Sign(String src, String dest, X509Certificate[] chain, PdfSigner.CryptoStandard subfilter, String reason, String location) { PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties()); // Create the signature appearance Rectangle rect = new Rectangle(36, 648, 200, 100); PdfSignatureAppearance appearance = signer.GetSignatureAppearance(); appearance .SetReason(reason) .SetLocation(location) .SetPageRect(rect) .SetPageNumber(1); signer.SetFieldName("sig"); IExternalSignature pks = new ServerSignature(); // Sign the document using the detached mode, CMS or CAdES equivalent. signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }
public void Sign(String src, String name, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, String reason, String location, String contact, String fullName) { PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties()); // Create the signature appearance PdfSignatureAppearance appearance = signer.GetSignatureAppearance(); appearance.SetReason(reason); appearance.SetLocation(location); appearance.SetContact(contact); signer.SetFieldName(name); // Set the signature event to allow modification of the signature dictionary. signer.SetSignatureEvent(new CustomISignatureEvent(fullName)); PrivateKeySignature pks = new PrivateKeySignature(pk, digestAlgorithm); // Sign the document using the detached mode, CMS or CAdES equivalent. signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }
public void Sign(String src, String name, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, String reason, String location) { PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties()); // Create the signature appearance signer.GetSignatureAppearance() .SetReason(reason) .SetLocation(location) // Specify if the appearance before field is signed will be used // as a background for the signed field. The "false" value is the default value. .SetReuseAppearance(false); // This name corresponds to the name of the field that already exists in the document. signer.SetFieldName(name); IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm); // Sign the document using the detached mode, CMS or CAdES equivalent. signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }
/// <summary>Signs the document using the detached mode, CMS or CAdES equivalent.</summary> /// <remarks> /// Signs the document using the detached mode, CMS or CAdES equivalent. /// <br /><br /> /// NOTE: This method closes the underlying pdf document. This means, that current instance /// of PdfSigner cannot be used after this method call. /// </remarks> /// <param name="externalSignature">the interface providing the actual signing</param> /// <param name="chain">the certificate chain</param> /// <param name="crlList">the CRL list</param> /// <param name="ocspClient">the OCSP client</param> /// <param name="tsaClient">the Timestamp client</param> /// <param name="externalDigest">an implementation that provides the digest</param> /// <param name="estimatedSize">the reserved size for the signature. It will be estimated if 0</param> /// <param name="sigtype">Either Signature.CMS or Signature.CADES</param> /// <exception cref="System.IO.IOException"/> /// <exception cref="Org.BouncyCastle.Security.GeneralSecurityException"/> public virtual void SignDetached(IExternalSignature externalSignature, X509Certificate[] chain, ICollection <ICrlClient> crlList, IOcspClient ocspClient, ITSAClient tsaClient, int estimatedSize, PdfSigner.CryptoStandard sigtype) { if (closed) { throw new PdfException(PdfException.ThisInstanceOfPdfSignerAlreadyClosed); } ICollection <byte[]> crlBytes = null; int i = 0; while (crlBytes == null && i < chain.Length) { crlBytes = ProcessCrl(chain[i++], crlList); } if (estimatedSize == 0) { estimatedSize = 8192; if (crlBytes != null) { foreach (byte[] element in crlBytes) { estimatedSize += element.Length + 10; } } if (ocspClient != null) { estimatedSize += 4192; } if (tsaClient != null) { estimatedSize += 4192; } } PdfSignatureAppearance appearance = GetSignatureAppearance(); appearance.SetCertificate(chain[0]); if (sigtype == PdfSigner.CryptoStandard.CADES) { AddDeveloperExtension(PdfDeveloperExtension.ESIC_1_7_EXTENSIONLEVEL2); } PdfSignature dic = new PdfSignature(PdfName.Adobe_PPKLite, sigtype == PdfSigner.CryptoStandard.CADES ? PdfName .ETSI_CAdES_DETACHED : PdfName.Adbe_pkcs7_detached); dic.SetReason(appearance.GetReason()); dic.SetLocation(appearance.GetLocation()); dic.SetSignatureCreator(appearance.GetSignatureCreator()); dic.SetContact(appearance.GetContact()); dic.SetDate(new PdfDate(GetSignDate())); // time-stamp will over-rule this cryptoDictionary = dic; IDictionary <PdfName, int?> exc = new Dictionary <PdfName, int?>(); exc[PdfName.Contents] = estimatedSize * 2 + 2; PreClose(exc); String hashAlgorithm = externalSignature.GetHashAlgorithm(); PdfPKCS7 sgn = new PdfPKCS7((ICipherParameters)null, chain, hashAlgorithm, false); Stream data = GetRangeStream(); byte[] hash = DigestAlgorithms.Digest(data, SignUtils.GetMessageDigest(hashAlgorithm)); byte[] ocsp = null; if (chain.Length >= 2 && ocspClient != null) { ocsp = ocspClient.GetEncoded((X509Certificate)chain[0], (X509Certificate)chain[1], null); } byte[] sh = sgn.GetAuthenticatedAttributeBytes(hash, ocsp, crlBytes, sigtype); byte[] extSignature = externalSignature.Sign(sh); sgn.SetExternalDigest(extSignature, null, externalSignature.GetEncryptionAlgorithm()); byte[] encodedSig = sgn.GetEncodedPKCS7(hash, tsaClient, ocsp, crlBytes, sigtype); if (estimatedSize < encodedSig.Length) { throw new System.IO.IOException("Not enough space"); } byte[] paddedSig = new byte[estimatedSize]; System.Array.Copy(encodedSig, 0, paddedSig, 0, encodedSig.Length); PdfDictionary dic2 = new PdfDictionary(); dic2.Put(PdfName.Contents, new PdfString(paddedSig).SetHexWriting(true)); Close(dic2); closed = true; }
/// <exception cref="Org.BouncyCastle.Security.GeneralSecurityException"/> /// <exception cref="System.IO.IOException"/> protected internal virtual void Sign(String src, String name, String dest, X509Certificate [] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, String reason, String location, Rectangle rectangleForNewField, bool setReuseAppearance) { PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), false ); // Creating the appearance PdfSignatureAppearance appearance = signer.GetSignatureAppearance().SetReason(reason ).SetLocation(location).SetReuseAppearance(setReuseAppearance); if (rectangleForNewField != null) { appearance.SetPageRect(rectangleForNewField); } signer.SetFieldName(name); // Creating the signature IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm); signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }