DigitalSignatureTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. using ComPDFKit.DigitalSign;
  2. using ComPDFKit.Import;
  3. using ComPDFKit.PDFAnnotation;
  4. using ComPDFKit.PDFAnnotation.Form;
  5. using ComPDFKit.PDFDocument;
  6. using ComPDFKit.PDFPage;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Xml.Linq;
  12. namespace DigitalSignatureTest
  13. {
  14. internal class DigitalSignatureTest
  15. {
  16. static private string parentPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())));
  17. static private string outputPath = Path.Combine(parentPath, "Output", "CS");
  18. static void Main()
  19. {
  20. #region Preparation work
  21. Console.WriteLine("Running digital signature sample...\n");
  22. SDKLicenseHelper.LicenseVerify();
  23. string certificatePath = "Certificate.pfx";
  24. string password = "ComPDFKit";
  25. if (!Directory.Exists(outputPath))
  26. {
  27. Directory.CreateDirectory(outputPath);
  28. }
  29. #endregion
  30. #region Sample 0: Create certificate
  31. GenerateCertificate();
  32. #endregion
  33. #region Sample 1: Create digital signature
  34. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  35. CreateDigitalSignature(document, certificatePath, password);
  36. document.Release();
  37. #endregion
  38. #region Sample 2: Verify signature
  39. CPDFDocument signedDoc = CPDFDocument.InitWithFilePath("Signed.pdf");
  40. VerifyDigitalSignature(signedDoc);
  41. #endregion
  42. #region Sample 3: Verify certificate
  43. VerifyCertificate(certificatePath, password);
  44. #endregion
  45. #region Sample 4: Print digital signature info
  46. PrintDigitalSignatureInfo(signedDoc);
  47. #endregion
  48. #region Sample 5: Trust Certificate
  49. TrustCertificate(signedDoc);
  50. #endregion
  51. #region Sample 6: Remove digital signature
  52. RemoveDigitalSignature(signedDoc);
  53. signedDoc.Release();
  54. #endregion
  55. Console.WriteLine("Done!");
  56. Console.ReadLine();
  57. }
  58. /// <summary>
  59. /// in the core function "CPDFPKCS12CertHelper.GeneratePKCS12Cert":
  60. ///
  61. /// Generate certificate
  62. ///
  63. /// Password: ComPDFKit
  64. ///
  65. /// info: /C=SG/O=ComPDFKit/D=R&D Department/CN=Alan/emailAddress=xxxx@example.com
  66. ///
  67. /// C=SG: This represents the country code "SG," which typically stands for Singapore.
  68. /// O=ComPDFKit: This is the Organization (O) field, indicating the name of the organization or entity, in this case, "ComPDFKit."
  69. /// D=R&D Department: This is the Department (D) field, indicating the specific department within the organization, in this case, "R&D Department."
  70. /// CN=Alan: This is the Common Name (CN) field, which usually represents the name of the individual or entity. In this case, it is "Alan."
  71. /// emailAddress=xxxx@example.com: Email is xxxx@example.com
  72. ///
  73. /// CPDFCertUsage.CPDFCertUsageAll: Used for both digital signing and data validation simultaneously.
  74. ///
  75. /// is_2048 = true: Enhanced security encryption.
  76. /// </summary>
  77. private static void GenerateCertificate()
  78. {
  79. Console.WriteLine("--------------------");
  80. Console.WriteLine("Generate certificate signature.");
  81. string info = "/C=SG/O=ComPDFKit/D=R&D Department/CN=Alan/emailAddress=xxxx@example.com";
  82. string password = "ComPDFKit";
  83. string filePath = outputPath + "\\Certificate.pfx";
  84. if (CPDFPKCS12CertHelper.GeneratePKCS12Cert(info, password, filePath, CPDFCertUsage.CPDFCertUsageAll, true))
  85. {
  86. Console.WriteLine("File saved in " + filePath);
  87. Console.WriteLine("Generate PKCS12 certificate done.");
  88. }
  89. else
  90. {
  91. Console.WriteLine("Generate PKCS12 certificate failed.");
  92. }
  93. Console.WriteLine("--------------------");
  94. }
  95. /// <summary>
  96. ///
  97. /// Adding a signature is divided into two steps:
  98. /// creating a signature field and filling in the signature.
  99. ///
  100. /// Page Index: 0
  101. /// Rect: CRect(28, 420, 150, 370)
  102. /// Border RGB:{ 0, 0, 0 }
  103. /// Widget Background RGB: { 150, 180, 210 }
  104. ///
  105. /// Text: Grantor Name
  106. /// Content:
  107. /// Name: get grantor name from certificate
  108. /// Date: now(yyyy.mm.dd)
  109. /// Reason: I am the owner of the document.
  110. /// DN: Subject
  111. /// Location: Singapor
  112. /// IsContentAlignLeft: false
  113. /// IsDrawLogo: True
  114. /// LogoBitmap: logo.png
  115. /// text color RGB: { 0, 0, 0 }
  116. /// content color RGB: { 0, 0, 0 }
  117. /// Output file name: document.FileName + "_Signed.pdf"
  118. /// </summary>
  119. private static void CreateDigitalSignature(CPDFDocument document, string certificatePath, string password)
  120. {
  121. Console.WriteLine("--------------------");
  122. Console.WriteLine("Create digital signature.");
  123. CPDFSignatureCertificate certificate = CPDFPKCS12CertHelper.GetCertificateWithPKCS12Path("Certificate.pfx", "ComPDFKit");
  124. CPDFPage page = document.PageAtIndex(0);
  125. CPDFSignatureWidget signatureField = page.CreateWidget(C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS) as CPDFSignatureWidget;
  126. signatureField.SetRect(new CRect(28, 420, 150, 370));
  127. signatureField.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
  128. signatureField.SetWidgetBgRGBColor(new byte[] { 150, 180, 210 });
  129. signatureField.UpdateAp();
  130. string name = GetGrantorFromDictionary(certificate.SubjectDict) + "\n";
  131. string date = DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss");
  132. string reason = "I am the owner of the document.";
  133. string location = certificate.SubjectDict["C"];
  134. string DN = certificate.Subject;
  135. CPDFSignatureConfig signatureConfig = new CPDFSignatureConfig
  136. {
  137. Text = GetGrantorFromDictionary(certificate.SubjectDict),
  138. Content =
  139. "Name: " + name + "\n" +
  140. "Date: " + date + "\n" +
  141. "Reason: " + reason + " \n" +
  142. "Location: " + location + "\n" +
  143. "DN: " + DN + "\n",
  144. IsContentAlignLeft = false,
  145. IsDrawLogo = true,
  146. LogoBitmap = new Bitmap("Logo.png"),
  147. TextColor = new float[] { 0, 0, 0 },
  148. ContentColor = new float[] { 0, 0, 0 }
  149. };
  150. string filePath = outputPath + "\\" + document.FileName + "_Signed.pdf";
  151. signatureField.UpdataApWithSignature(signatureConfig);
  152. if (document.WriteSignatureToFilePath(signatureField,
  153. filePath,
  154. certificatePath, password,
  155. location,
  156. reason, CPDFSignaturePermissions.CPDFSignaturePermissionsNone))
  157. {
  158. Console.WriteLine("File saved in " + filePath);
  159. Console.WriteLine("Create digital signature done.");
  160. }
  161. else
  162. {
  163. Console.WriteLine("Create digital signature failed.");
  164. }
  165. Console.WriteLine("--------------------");
  166. }
  167. /// <summary>
  168. /// Remove digital signature
  169. /// You can choose if you want to remove the appearance
  170. /// </summary>
  171. /// <param name="document"></param>
  172. private static void RemoveDigitalSignature(CPDFDocument document)
  173. {
  174. Console.WriteLine("--------------------");
  175. Console.WriteLine("Remove digital signature.");
  176. CPDFSignature signature = document.GetSignatureList()[0];
  177. document.RemoveSignature(signature, true);
  178. string filePath = outputPath + "\\" + document.FileName + "_RemovedSign.pdf";
  179. document.WriteToFilePath(filePath);
  180. Console.WriteLine("File saved in " + filePath);
  181. Console.WriteLine("Remove digital signature done.");
  182. Console.WriteLine("--------------------");
  183. }
  184. /// <summary>
  185. /// There are two steps can help you to trust a certificate.
  186. /// Set your trust path as a folder path,
  187. /// then add your certificate to the trust path.
  188. /// </summary>
  189. private static void TrustCertificate(CPDFDocument document)
  190. {
  191. Console.WriteLine("--------------------");
  192. Console.WriteLine("Trust certificate.");
  193. CPDFSignature signature = document.GetSignatureList()[0];
  194. CPDFSignatureCertificate signatureCertificate = signature.SignerList[0].CertificateList[0];
  195. Console.WriteLine("Certificate trusted status: " + signatureCertificate.IsTrusted.ToString());
  196. Console.WriteLine("---Begin trusted---");
  197. string trustedFolder = AppDomain.CurrentDomain.BaseDirectory + @"\TrustedFolder\";
  198. if (!Directory.Exists(trustedFolder))
  199. {
  200. Directory.CreateDirectory(trustedFolder);
  201. }
  202. CPDFSignature.SignCertTrustedFolder = trustedFolder;
  203. if (signatureCertificate.AddToTrustedCertificates())
  204. {
  205. Console.WriteLine("Certificate trusted status: " + signatureCertificate.IsTrusted.ToString());
  206. Console.WriteLine("Trust certificate done.");
  207. }
  208. else
  209. {
  210. Console.WriteLine("Trust certificate failed.");
  211. }
  212. Console.WriteLine("--------------------");
  213. }
  214. /// <summary>
  215. /// Verify certificate
  216. ///
  217. /// To verify the trustworthiness of a certificate,
  218. /// you need to verify that all certificates in the certificate chain are trustworthy.
  219. ///
  220. /// In ComPDFKit,this progess is automatic.
  221. /// You should call the "CPDFSignatureCertificate.CheckCertificateIsTrusted" first.
  222. /// then you can view the "CPDFSignatureCertificate.IsTrusted" property.
  223. /// </summary>
  224. /// <param name="document">A signed document</param>
  225. private static void VerifyCertificate(string certificatePath, string password)
  226. {
  227. Console.WriteLine("--------------------");
  228. Console.WriteLine("Verify certificate.");
  229. CPDFSignatureCertificate certificate = CPDFPKCS12CertHelper.GetCertificateWithPKCS12Path(certificatePath, password);
  230. certificate.CheckCertificateIsTrusted();
  231. if (certificate.IsTrusted)
  232. {
  233. Console.WriteLine("Certificate is trusted");
  234. }
  235. else
  236. {
  237. Console.WriteLine("Certificate is not trusted");
  238. }
  239. Console.WriteLine("Verify certificate done.");
  240. Console.WriteLine("--------------------");
  241. }
  242. /// <summary>
  243. /// Verify digital signature
  244. ///
  245. /// Refresh the validation status before reading the attributes, or else you may obtain inaccurate results.
  246. /// Is the signature verified: indicating whether the document has been tampered with.
  247. /// Is the certificate trusted: referring to the trust status of the certificate.
  248. /// </summary>
  249. private static void VerifyDigitalSignature(CPDFDocument document)
  250. {
  251. Console.WriteLine("--------------------");
  252. Console.WriteLine("Verify digital signature.");
  253. foreach (var signature in document.GetSignatureList())
  254. {
  255. signature.VerifySignatureWithDocument(document);
  256. foreach (var signer in signature.SignerList)
  257. {
  258. Console.WriteLine("Is the certificate trusted: " + signer.IsCertTrusted.ToString());
  259. Console.WriteLine("Is the signature verified: " + signer.IsSignVerified.ToString());
  260. if (signer.IsCertTrusted && signer.IsSignVerified)
  261. {
  262. // Signature is valid and the certificate is trusted
  263. // Perform corresponding actions
  264. }
  265. else if (!signer.IsCertTrusted && signer.IsSignVerified)
  266. {
  267. // Signature is valid but the certificate is not trusted
  268. // Perform corresponding actions
  269. }
  270. else
  271. {
  272. // Signature is invalid
  273. // Perform corresponding actions
  274. }
  275. }
  276. }
  277. Console.WriteLine("Verify digital signature done.");
  278. Console.WriteLine("--------------------");
  279. }
  280. public static string GetGrantorFromDictionary(Dictionary<string, string> dictionary)
  281. {
  282. string grantor = string.Empty;
  283. dictionary.TryGetValue("CN", out grantor);
  284. if (string.IsNullOrEmpty(grantor))
  285. {
  286. dictionary.TryGetValue("OU", out grantor);
  287. }
  288. if (string.IsNullOrEmpty(grantor))
  289. {
  290. dictionary.TryGetValue("O", out grantor);
  291. }
  292. if (string.IsNullOrEmpty(grantor))
  293. {
  294. grantor = "Unknown Signer";
  295. }
  296. return grantor;
  297. }
  298. /// <summary>
  299. /// this samples shows how to get main properties in digital signature.
  300. /// read API reference to see all of the properties can get
  301. /// </summary>
  302. /// <param name="document"></param>
  303. private static void PrintDigitalSignatureInfo(CPDFDocument document)
  304. {
  305. Console.WriteLine("--------------------");
  306. Console.WriteLine("Print digital signature info.");
  307. foreach (var signature in document.GetSignatureList())
  308. {
  309. signature.VerifySignatureWithDocument(document);
  310. Console.WriteLine("Name: " + signature.Name);
  311. Console.WriteLine("Location: " + signature.Location);
  312. Console.WriteLine("Reason: " + signature.Reason);
  313. foreach (var signer in signature.SignerList)
  314. {
  315. Console.WriteLine("Date: " + signer.AuthenDate);
  316. foreach (var certificate in signer.CertificateList)
  317. {
  318. Console.WriteLine("Subject: " + certificate.Subject);
  319. }
  320. }
  321. }
  322. Console.WriteLine("Print digital signature info done.");
  323. Console.WriteLine("--------------------");
  324. }
  325. }
  326. }