ViewCertificateDialog.xaml.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /// Interaction logic for 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.First().CertificateList;
  32. CertificateListView.ItemsSource = certificateList;
  33. CertificateListView.SelectedIndex = certificateList.Count - 1;
  34. }
  35. private void CertificateListView_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
  36. {
  37. int index = CertificateListView.SelectedIndex;
  38. if(index >= 0)
  39. {
  40. CertificateInfoControl.LoadCertificateInfo(certificateList[index]);
  41. }
  42. }
  43. private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
  44. {
  45. this.Close();
  46. }
  47. }
  48. public class DictionaryValueConverter : IValueConverter
  49. {
  50. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  51. {
  52. if (value is Dictionary<string, string> dictionary)
  53. {
  54. return GetGrantorFromDictionary(dictionary);
  55. }
  56. return "Unknown Signer";
  57. }
  58. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  59. {
  60. return null;
  61. }
  62. public static string GetGrantorFromDictionary(Dictionary<string, string> dictionary)
  63. {
  64. string grantor = string.Empty;
  65. dictionary.TryGetValue("CN", out grantor);
  66. if (string.IsNullOrEmpty(grantor))
  67. {
  68. dictionary.TryGetValue("OU", out grantor);
  69. }
  70. if (string.IsNullOrEmpty(grantor))
  71. {
  72. grantor = "Unknown Signer";
  73. }
  74. return grantor;
  75. }
  76. public static string GetDNFromDictionary(Dictionary<string, string> dictionary)
  77. {
  78. List<string> dnParts = new List<string>();
  79. if (dictionary.TryGetValue("CN", out string cn))
  80. {
  81. if (!string.IsNullOrEmpty(cn))
  82. {
  83. dnParts.Add("CN=" + cn);
  84. }
  85. }
  86. if (dictionary.TryGetValue("O", out string o))
  87. {
  88. if (!string.IsNullOrEmpty(o))
  89. {
  90. dnParts.Add("O=" + o);
  91. }
  92. }
  93. if (dictionary.TryGetValue("OU", out string ou))
  94. {
  95. dnParts.Add("OU=" + ou);
  96. }
  97. if (dictionary.TryGetValue("C", out string c))
  98. {
  99. dnParts.Add("C=" + c);
  100. }
  101. if (dictionary.TryGetValue("ST", out string st))
  102. {
  103. dnParts.Add("ST=" + st);
  104. }
  105. string DN = string.Join(", ", dnParts);
  106. return DN;
  107. }
  108. public static string GetEmailFormDictionary(Dictionary<string, string> dictionary)
  109. {
  110. string email = string.Empty;
  111. dictionary.TryGetValue("emailAddress", out email);
  112. return email;
  113. }
  114. public static List<string> GetUsage(CPDFSignatureCertificate certificate)
  115. {
  116. int usage = certificate.KeyUsage;
  117. List<string> usageList = new List<string>();
  118. if ((usage & 1 << 0) != 0)
  119. {
  120. usageList.Add("Encipher Only");
  121. }
  122. if ((usage & 1 << 1) != 0)
  123. {
  124. usageList.Add("CRL Signature");
  125. }
  126. if ((usage & 1 << 2) != 0)
  127. {
  128. usageList.Add("Certificate Signature");
  129. }
  130. if ((usage & 1 << 3) != 0)
  131. {
  132. usageList.Add("Key Agreement");
  133. }
  134. if ((usage & 1 << 4) != 0)
  135. {
  136. usageList.Add("Data Encipherment");
  137. }
  138. if ((usage & 1 << 5) != 0)
  139. {
  140. usageList.Add("Key Encipherment");
  141. }
  142. if ((usage & 1 << 6) != 0)
  143. {
  144. usageList.Add("Non Repudiation");
  145. }
  146. if ((usage & 1 << 7) != 0)
  147. {
  148. usageList.Add("Digital Signature");
  149. }
  150. if ((usage & 1 << 15) != 0)
  151. {
  152. usageList.Add("Decipher Only");
  153. }
  154. return usageList;
  155. }
  156. }
  157. }