ViewCertificateDialog.xaml.cs 5.3 KB

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