title: Signed pdf using timestamp itextSharp 5.x
body: <p>I need to add the ocsp, it looks like version 5.x and different from version 4.x</p>
  
  <p>In version 4 asks to send a date in this new asks to send a byte type what data these would send</p>
  
  <pre><code>byte[] ocsp = null;
  byte[] sh = signature.getAuthenticatedAttributeBytes(hashByte, ocsp, null, CryptoStandard.CMS);
  </code></pre>
  
  <p>I also need to add the TSAClientBouncyCastle, however I am using the option of preparing the pdf to subscribe later.</p>
  
  <hr>
  
  <p>My signature class is next, I have a method where I prepare the file to be signed externally, are two services restm a client and a server.</p>
  
  <p>I request to the server the hash of the file to be signed, I sign and return it to the server to add to the pdf.</p>
  
  <p>I just do not know how to fill in with ocsp and tsclient</p>
  
  <pre><code>class PDFSigner {
      private List&lt;Org.BouncyCastle.X509.X509Certificate&gt; chain = new List&lt;Org.BouncyCastle.X509.X509Certificate&gt;();
  
      public void certificate(List&lt;string&gt; certificate)
      {
          Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser();
          foreach (string cert in certificate)
          {
              this.chain.Add(cp.ReadCertificate(Convert.FromBase64String(cert)));
          }
  
      }
  
      public string prepared(string file)
      {
          try
          {
  
              FileStream signedPdf = new FileStream(file.Substring(0, file.Length - 4) + "_signed.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  
              PdfReader reader = new PdfReader(file);
              PdfStamper stamper = PdfStamper.CreateSignature(reader, signedPdf, '\0');
  
  
              // Criar a assinatura aparente
              PdfSignatureAppearance appearance = stamper.SignatureAppearance;
              appearance.Reason = "Because I can";
              appearance.Location = "My location";
              appearance.SetVisibleSignature(new Rectangle(100, 100, 350, 150), reader.NumberOfPages, "Signature");
              appearance.SignatureGraphic = Image.GetInstance("C:\\signature.png");
              appearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION;
              appearance.Certificate = this.chain[0];            
  
              PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED);
              dic.Reason = appearance.Reason;
              dic.Location = appearance.Location;
              dic.Contact = appearance.Contact;
              dic.Date = new PdfDate(appearance.SignDate);
              appearance.CryptoDictionary = dic;
              Dictionary&lt;PdfName, int&gt; exc = new Dictionary&lt;PdfName, int&gt;();
              exc.Add(PdfName.CONTENTS, (int)(8192 * 2 + 2));
              appearance.PreClose(exc);
              PdfPKCS7 signature = new PdfPKCS7(null, this.chain, "SHA1", false);
  
              Stream data = appearance.GetRangeStream();
              byte[] hashByte = DigestAlgorithms.Digest(data, "SHA1");
  
              DateTime cal = DateTime.Now;
              byte[] ocsp = null;
  
              byte[] sh = signature.getAuthenticatedAttributeBytes(hashByte, ocsp, null, CryptoStandard.CMS);
  
              string hash = Convert.ToBase64String(hashByte);
              string key = hash;
  
              if (Variable.appearance.ContainsKey(key))
                  Variable.appearance.Remove(key);                
  
              if (Variable.signature.ContainsKey(key))
                  Variable.signature.Remove(key);
  
  
  
              Variable.appearance.Add(key, appearance);
              Variable.signature.Add(key, signature);
  
              return hash;
          }
          catch (Exception e)
          {
              throw new System.Exception(e.Message);
          }
  
      }
  
      public Contracts.File savePdf(Contracts.File file)
      {
          try
          {
              byte[] signByte = Convert.FromBase64String(file.signature);
  
              PdfSignatureAppearance appearance = Variable.appearance[file.hash];
              PdfPKCS7 signature = Variable.signature[file.hash];
  
              signature.SetExternalDigest(signByte, null, "RSA");
              byte[] encodedSig = signature.GetEncodedPKCS7();
  
              byte[] paddedSig = new byte[8192];
              Array.Copy(encodedSig, 0, paddedSig, 0, encodedSig.Length);
              PdfDictionary dic = new PdfDictionary();
              dic.Put(PdfName.CONTENTS, new PdfString(paddedSig).SetHexWriting(true));
  
              appearance.Close(dic);
  
              Variable.appearance.Remove(file.hash);
              Variable.signature.Remove(file.hash);
              //verifySignature(file.name, "");
  
              return file;
          }
          catch (Exception e)
          {
  
              throw new System.Exception(e.Message);
          }
      } 
  }
  </code></pre>