/// <summary>Adds keys to the signature dictionary that define the field permissions.</summary> /// <remarks> /// Adds keys to the signature dictionary that define the field permissions. /// This method is only used for signatures that lock fields. /// </remarks> /// <param name="crypto">the signature dictionary</param> protected internal virtual void AddFieldMDP(PdfSignature crypto, PdfSigFieldLockDictionary fieldLock) { PdfDictionary reference = new PdfDictionary(); PdfDictionary transformParams = new PdfDictionary(); transformParams.PutAll(fieldLock.GetPdfObject()); transformParams.Put(PdfName.Type, PdfName.TransformParams); transformParams.Put(PdfName.V, new PdfName("1.2")); reference.Put(PdfName.TransformMethod, PdfName.FieldMDP); reference.Put(PdfName.Type, PdfName.SigRef); reference.Put(PdfName.TransformParams, transformParams); reference.Put(new PdfName("DigestValue"), new PdfString("aa")); PdfArray loc = new PdfArray(); loc.Add(new PdfNumber(0)); loc.Add(new PdfNumber(0)); reference.Put(new PdfName("DigestLocation"), loc); reference.Put(new PdfName("DigestMethod"), new PdfName("MD5")); reference.Put(PdfName.Data, document.GetTrailer().Get(PdfName.Root)); PdfArray types = crypto.GetPdfObject().GetAsArray(PdfName.Reference); if (types == null) { types = new PdfArray(); } types.Add(reference); crypto.Put(PdfName.Reference, types); }
/// <summary>Adds keys to the signature dictionary that define the certification level and the permissions.</summary> /// <remarks> /// Adds keys to the signature dictionary that define the certification level and the permissions. /// This method is only used for Certifying signatures. /// </remarks> /// <param name="crypto">the signature dictionary</param> protected internal virtual void AddDocMDP(PdfSignature crypto) { PdfDictionary reference = new PdfDictionary(); PdfDictionary transformParams = new PdfDictionary(); transformParams.Put(PdfName.P, new PdfNumber(certificationLevel)); transformParams.Put(PdfName.V, new PdfName("1.2")); transformParams.Put(PdfName.Type, PdfName.TransformParams); reference.Put(PdfName.TransformMethod, PdfName.DocMDP); reference.Put(PdfName.Type, PdfName.SigRef); reference.Put(PdfName.TransformParams, transformParams); if (document.GetPdfVersion().CompareTo(PdfVersion.PDF_1_6) < 0) { reference.Put(new PdfName("DigestValue"), new PdfString("aa")); PdfArray loc = new PdfArray(); loc.Add(new PdfNumber(0)); loc.Add(new PdfNumber(0)); reference.Put(new PdfName("DigestLocation"), loc); reference.Put(new PdfName("DigestMethod"), new PdfName("MD5")); } reference.Put(PdfName.Data, document.GetTrailer().Get(PdfName.Root)); PdfArray types = new PdfArray(); types.Add(reference); crypto.Put(PdfName.Reference, types); }
/// <summary>Signs a document with a PAdES-LTV Timestamp.</summary> /// <remarks> /// Signs a document with a PAdES-LTV Timestamp. The document is closed at the end. /// <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="tsa">the timestamp generator</param> /// <param name="signatureName"> /// the signature name or null to have a name generated /// automatically /// </param> /// <exception cref="System.IO.IOException"/> /// <exception cref="Org.BouncyCastle.Security.GeneralSecurityException"/> public virtual void Timestamp(ITSAClient tsa, String signatureName) { if (closed) { throw new PdfException(PdfException.ThisInstanceOfPdfSignerAlreadyClosed); } int contentEstimated = tsa.GetTokenSizeEstimate(); AddDeveloperExtension(PdfDeveloperExtension.ESIC_1_7_EXTENSIONLEVEL5); SetFieldName(signatureName); PdfSignature dic = new PdfSignature(PdfName.Adobe_PPKLite, PdfName.ETSI_RFC3161); dic.Put(PdfName.Type, PdfName.DocTimeStamp); cryptoDictionary = dic; IDictionary <PdfName, int?> exc = new Dictionary <PdfName, int?>(); exc[PdfName.Contents] = contentEstimated * 2 + 2; PreClose(exc); Stream data = GetRangeStream(); IDigest messageDigest = tsa.GetMessageDigest(); byte[] buf = new byte[4096]; int n; while ((n = data.Read(buf)) > 0) { messageDigest.Update(buf, 0, n); } byte[] tsImprint = messageDigest.Digest(); byte[] tsToken; try { tsToken = tsa.GetTimeStampToken(tsImprint); } catch (Exception e) { throw new GeneralSecurityException(e.Message, e); } if (contentEstimated + 2 < tsToken.Length) { throw new System.IO.IOException("Not enough space"); } byte[] paddedSig = new byte[contentEstimated]; System.Array.Copy(tsToken, 0, paddedSig, 0, tsToken.Length); PdfDictionary dic2 = new PdfDictionary(); dic2.Put(PdfName.Contents, new PdfString(paddedSig).SetHexWriting(true)); Close(dic2); closed = true; }