Guest User

Untitled

a guest
Apr 30th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. public void SignPDF(byte[] pdf, SignData signData) //signData: my custom class, it's about sign image,location,size
  2. {
  3. string timeStampServer = "http://xxxxxxxx";
  4. string timeStampUser = "xxxx";
  5. string timeStampPass = "xxxx";
  6.  
  7. string cryptokiDLL = @"C:Program FilesSafeNetLunaClientcryptoki.dll";
  8. string tokenLabel = "xxx";
  9. string pin = "xxxx";
  10. string primaryKeyLabel = "xxxxx";
  11. string certsDir = @"C:CERTs";
  12.  
  13. iTextSharp.text.Image signImg = null;
  14. iTextSharp.text.Rectangle signRec = null;
  15. byte[] signedPDF = null;
  16. string hash = null;
  17.  
  18. try
  19. {
  20.  
  21. HashAlgorithm hashAlgorithm = HashAlgorithm.SHA256;
  22. Pkcs11RsaSignature pkcs11RsaSignature = new Pkcs11RsaSignature(cryptokiDLL, null, tokenLabel, pin, primaryKeyLabel, null, hashAlgorithm);
  23. byte[] signingCertificate = pkcs11RsaSignature.GetSigningCertificate();//1460
  24. List<byte[]> otherCertificates = new List<byte[]>();
  25. foreach (string file in Directory.GetFiles(certsDir))
  26. otherCertificates.Add(File.ReadAllBytes(file));
  27. var chain = CertUtils.BuildCertPath(signingCertificate, otherCertificates).ToList();
  28.  
  29. ITSAClient tsaClient = new TSAClientBouncyCastle(timeStampServer, timeStampUser, timeStampPass);
  30.  
  31. IOcspClient ocspClient = new OcspClientBouncyCastle();
  32.  
  33. List<ICrlClient> crlList = new List<ICrlClient>();
  34. crlList.Add(new CrlClientOnline(chain));
  35.  
  36. using (PdfReader pdfReader = new PdfReader(pdf))
  37. {
  38. using (MemoryStream msPDFfinal = new MemoryStream())
  39. {
  40. using (PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, msPDFfinal, ''))
  41. {
  42. PdfWriter writer = pdfStamper.Writer;
  43. PdfSignatureAppearance appearance = pdfStamper.SignatureAppearance;
  44.  
  45. byte[] signImgBinary = signData.ImzaBinary;
  46. int x = signData.X;
  47. int y = signData.Y;
  48. int scale = signData.Scale;
  49. int pageNum = signData.PageNum;
  50.  
  51. signImg = iTextSharp.text.Image.GetInstance(signImgBinary);
  52. signImg.ScalePercent(scale);
  53.  
  54. float imgX = (float)x;
  55. float imgY = (float)y;
  56. float scaledW = signImg.ScaledWidth;
  57. float scaledH = signImg.ScaledHeight;
  58.  
  59. signRec = new iTextSharp.text.Rectangle(imgX, imgY, imgX + scaledW, imgY + scaledH);
  60.  
  61. appearance.Layer2Text = " ";
  62. appearance.Image = signImg;
  63. appearance.SetVisibleSignature(signRec, pageNum, "SIG");
  64.  
  65. //without timestamp, it is signing the pdf. Everything is ok.
  66. //MakeSignature.SignDetached(appearance, pkcs11RsaSignature, chain, null, null, null, 0, CryptoStandard.CADES);
  67.  
  68. //it returns 504 error, no server response
  69. MakeSignature.SignDetached(appearance, pkcs11RsaSignature, chain, crlList, ocspClient, tsaClient, 0, CryptoStandard.CADES);
  70. }
  71.  
  72. signedPDF = msPDFfinal.ToArray();
  73. }
  74. }
  75. }
  76. catch (Exception ex)
  77. {
  78. MessageBox.Show(ex.Message);
  79. }
  80. }
  81.  
  82. protected internal virtual byte[] GetTSAResponse(byte[] requestBytes) {
  83. HttpWebRequest con = (HttpWebRequest)WebRequest.Create(tsaURL);
  84. con.UserAgent = "itextsharp";
  85. con.ProtocolVersion = HttpVersion.Version10;
  86. con.ContentLength = requestBytes.Length;
  87. con.ContentType = "application/timestamp-query";
  88. con.Method = "POST";
  89. con.KeepAlive = true;
  90. if ((tsaUsername != null) && !tsaUsername.Equals("") ) {
  91. string authInfo = tsaUsername + ":" + tsaPassword;
  92. authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo), Base64FormattingOptions.None);
  93. con.Headers["Authorization"] = "Basic " + authInfo;
  94. }
  95. Stream outp = con.GetRequestStream();
  96. outp.Write(requestBytes, 0, requestBytes.Length);
  97. outp.Close();
Add Comment
Please, Sign In to add comment