ViewCertificateDialog.xaml.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 GetEmailFormDictionary(Dictionary<string, string> dictionary)
  72. {
  73. string email = string.Empty;
  74. dictionary.TryGetValue("emailAddress", out email);
  75. return email;
  76. }
  77. public static List<string> GetUsage(CPDFSignatureCertificate certificate)
  78. {
  79. int usage = certificate.KeyUsage;
  80. List<string> usageList = new List<string>();
  81. if ((usage & 1 << 0) != 0)
  82. {
  83. usageList.Add("Encipher Only");
  84. }
  85. if ((usage & 1 << 1) != 0)
  86. {
  87. usageList.Add("CRL Signature");
  88. }
  89. if ((usage & 1 << 2) != 0)
  90. {
  91. usageList.Add("Certificate Signature");
  92. }
  93. if ((usage & 1 << 3) != 0)
  94. {
  95. usageList.Add("Key Agreement");
  96. }
  97. if ((usage & 1 << 4) != 0)
  98. {
  99. usageList.Add("Data Encipherment");
  100. }
  101. if ((usage & 1 << 5) != 0)
  102. {
  103. usageList.Add("Key Encipherment");
  104. }
  105. if ((usage & 1 << 6) != 0)
  106. {
  107. usageList.Add("Non Repudiation");
  108. }
  109. if ((usage & 1 << 7) != 0)
  110. {
  111. usageList.Add("Digital Signature");
  112. }
  113. if ((usage & 1 << 15) != 0)
  114. {
  115. usageList.Add("Decipher Only");
  116. }
  117. return usageList;
  118. }
  119. }
  120. }