ViewCertificateDialog.xaml.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Shapes;
  15. using ComPDFKit.DigitalSign;
  16. namespace Compdfkit_Tools.PDFControl
  17. {
  18. /// <summary>
  19. /// ViewCertificationControl.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class ViewCertificateDialog : Window
  22. {
  23. private List<CPDFSignatureCertificate> certificateList;
  24. public ViewCertificateDialog()
  25. {
  26. InitializeComponent();
  27. }
  28. public void InitCertificateList(CPDFSignature signature)
  29. {
  30. CertificateListView.ItemsSource = null;
  31. certificateList = signature.SignerList.Last().CertificateList;
  32. CertificateListView.ItemsSource = certificateList;
  33. }
  34. private void CertificateListView_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
  35. {
  36. int index = CertificateListView.SelectedIndex;
  37. if(index >= 0)
  38. {
  39. CertificateInfoControl.LoadCertificateInfo(certificateList[index]);
  40. }
  41. }
  42. }
  43. public class DictionaryValueConverter : IValueConverter
  44. {
  45. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  46. {
  47. if (value is Dictionary<string, string> dictionary)
  48. {
  49. return GetGrantorFormDictionary(dictionary);
  50. }
  51. return "Unknown Signer";
  52. }
  53. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  54. {
  55. return null;
  56. }
  57. public static string GetGrantorFormDictionary(Dictionary<string, string> dictionary)
  58. {
  59. string grantor = string.Empty;
  60. dictionary.TryGetValue("CN", out grantor);
  61. if (string.IsNullOrEmpty(grantor))
  62. {
  63. dictionary.TryGetValue("OU", out grantor);
  64. }
  65. if (string.IsNullOrEmpty(grantor))
  66. {
  67. grantor = "Unknown Signer";
  68. }
  69. return grantor;
  70. }
  71. public static string GetDNFromDictionary(Dictionary<string, string> dictionary)
  72. {
  73. List<string> dnParts = new List<string>();
  74. if (dictionary.TryGetValue("CN", out string cn))
  75. {
  76. if (!string.IsNullOrEmpty(cn))
  77. {
  78. dnParts.Add("CN=" + cn);
  79. }
  80. }
  81. if (dictionary.TryGetValue("O", out string o))
  82. {
  83. if (!string.IsNullOrEmpty(o))
  84. {
  85. dnParts.Add("O=" + o);
  86. }
  87. }
  88. if (dictionary.TryGetValue("OU", out string ou))
  89. {
  90. dnParts.Add("OU=" + ou);
  91. }
  92. if (dictionary.TryGetValue("C", out string c))
  93. {
  94. dnParts.Add("C=" + c);
  95. }
  96. if (dictionary.TryGetValue("ST", out string st))
  97. {
  98. dnParts.Add("ST=" + st);
  99. }
  100. string DN = string.Join(", ", dnParts);
  101. return DN;
  102. }
  103. public static string GetEmailFormDictionary(Dictionary<string, string> dictionary)
  104. {
  105. string email = string.Empty;
  106. dictionary.TryGetValue("emailAddress", out email);
  107. return email;
  108. }
  109. public static List<string> GetUsage(CPDFSignatureCertificate certificate)
  110. {
  111. int usage = certificate.KeyUsage;
  112. List<string> usageList = new List<string>();
  113. if ((usage & 1 << 0) != 0)
  114. {
  115. usageList.Add("Encipher Only");
  116. }
  117. if ((usage & 1 << 1) != 0)
  118. {
  119. usageList.Add("CRL Signature");
  120. }
  121. if ((usage & 1 << 2) != 0)
  122. {
  123. usageList.Add("Certificate Signature");
  124. }
  125. if ((usage & 1 << 3) != 0)
  126. {
  127. usageList.Add("Key Agreement");
  128. }
  129. if ((usage & 1 << 4) != 0)
  130. {
  131. usageList.Add("Data Encipherment");
  132. }
  133. if ((usage & 1 << 5) != 0)
  134. {
  135. usageList.Add("Key Encipherment");
  136. }
  137. if ((usage & 1 << 6) != 0)
  138. {
  139. usageList.Add("Non Repudiation");
  140. }
  141. if ((usage & 1 << 7) != 0)
  142. {
  143. usageList.Add("Digital Signature");
  144. }
  145. if ((usage & 1 << 15) != 0)
  146. {
  147. usageList.Add("Decipher Only");
  148. }
  149. return usageList;
  150. }
  151. }
  152. }