DSignatureDetailsViewController.swift 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. //
  2. // DSignatureDetailsViewController.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2023/10/10.
  6. //
  7. import Cocoa
  8. class CDSPDFSigntureModel : NSObject {
  9. var certificate: CPDFSignatureCertificate!
  10. var level: Int = 0
  11. var hide: Int = 0
  12. var count: Int = 0
  13. var isShow: Bool = false
  14. }
  15. class DSignatureDetailsViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource, NSTabViewDelegate {
  16. @IBOutlet weak var titleLabel: NSTextField!
  17. @IBOutlet weak var subTitleLabel: NSTextField!
  18. @IBOutlet weak var closeButton: NSButton!
  19. @IBOutlet weak var leftScrollView: NSScrollView!
  20. @IBOutlet weak var tableView: NSTableView!
  21. @IBOutlet weak var tabView: NSTabView!
  22. @IBOutlet weak var summaryImageView: NSImageView!
  23. @IBOutlet weak var summaryIssuesLabel: NSTextField!
  24. @IBOutlet weak var summaryVaildFromLabel: NSTextField!
  25. @IBOutlet weak var summaryVaildtoLabel: NSTextField!
  26. @IBOutlet weak var summaryUsageLabel: NSTextField!
  27. @IBOutlet var summaryUsageTextView: NSTextView!
  28. @IBOutlet weak var summaryNameTextField: NSTextField!
  29. @IBOutlet weak var summaryIssuesTextField: NSTextField!
  30. @IBOutlet weak var summaryVaildFromTextField: NSTextField!
  31. @IBOutlet weak var summaryVaildtoTextField: NSTextField!
  32. @IBOutlet weak var summaryExportBtn: NSButton!
  33. @IBOutlet weak var detailsTableView: NSTableView!
  34. @IBOutlet weak var detailsDatasLabel: NSTextField!
  35. @IBOutlet var detailsTextView: NSTextView!
  36. @IBOutlet weak var trustTitleLabel: NSTextField!
  37. @IBOutlet weak var trustSubtitleLabel: NSTextField!
  38. @IBOutlet weak var trustDesBox: NSBox!
  39. @IBOutlet weak var trustDesTitleLabel: NSTextField!
  40. @IBOutlet weak var trustDes1ImageView: NSImageView!
  41. @IBOutlet weak var trustDes1Label: NSTextField!
  42. @IBOutlet weak var trustDes2ImageView: NSImageView!
  43. @IBOutlet weak var trustDes2Label: NSTextField!
  44. @IBOutlet weak var trustButton: NSButton!
  45. @IBOutlet weak var policiesSubLabel: NSTextField!
  46. @IBOutlet weak var legalTitleLabel: NSTextField!
  47. @IBOutlet weak var legalSubLabel: NSTextField!
  48. var certificates: [CPDFSignatureCertificate] = []
  49. var signer: CPDFSigner!
  50. var signature:CPDFSignature!
  51. var pdfListView: CPDFView!
  52. var isDigitalFile: Bool = false
  53. var models: NSMutableArray = NSMutableArray.init()
  54. var certDatas: NSMutableArray = NSMutableArray.init()
  55. deinit {
  56. print("\(self.className) dealloc")
  57. }
  58. convenience init() {
  59. self.init(nibName: "DSignatureDetailsViewController", bundle: nil)
  60. }
  61. override func viewWillAppear() {
  62. super.viewWillAppear()
  63. self.view.window?.title = ""
  64. self.view.window?.titlebarAppearsTransparent = true
  65. self.view.window?.standardWindowButton(.closeButton)?.isHidden = true
  66. self.view.window?.standardWindowButton(.miniaturizeButton)?.isHidden = true
  67. self.view.window?.standardWindowButton(.zoomButton)?.isHidden = true
  68. }
  69. override func viewDidLoad() {
  70. super.viewDidLoad()
  71. // Do view setup here.
  72. self.localizedLanguage()
  73. self.tableView.delegate = self
  74. self.tableView.dataSource = self
  75. self.detailsTableView.delegate = self
  76. self.detailsTableView.dataSource = self
  77. self.tabView.delegate = self;
  78. let kMaxDeep = 10
  79. let kShowFlag = (1 << kMaxDeep)-1
  80. if self.certificates.count > 0 {
  81. self.models.removeAllObjects()
  82. for i in 0...self.certificates.count-1 {
  83. let cert = self.certificates[i]
  84. let model = CDSPDFSigntureModel.init()
  85. model.certificate = cert
  86. model.level = i
  87. model.hide = kShowFlag
  88. model.isShow = true
  89. model.count = self.certificates.count - i - 1
  90. self.models.add(model)
  91. }
  92. }
  93. self.tableView.reloadData()
  94. self.summaryUsageTextView.backgroundColor = NSColor.clear
  95. if (self.models.count > 0) {
  96. let set = NSIndexSet.init(index: self.models.count-1)
  97. self.tableView.selectRowIndexes(set as IndexSet, byExtendingSelection: true)
  98. self.updateCertificateData()
  99. }
  100. if (self.isDigitalFile) {
  101. self.summaryImageView.image = NSImage(named: "ImageNameDSignatureFromFile")
  102. } else {
  103. self.summaryImageView.image = NSImage(named: "ImageNameDSignatSaveKechain")
  104. }
  105. }
  106. func localizedLanguage () {
  107. self.titleLabel.stringValue = NSLocalizedString("Certificate Viewer", comment: "")
  108. self.subTitleLabel.stringValue = NSLocalizedString("This dialog allows you to view the details of a certificate and its entire issuance chain. The details correspond to the selected entry. Multiple issuance chains are being displayed because none of the chains were issued by a trust anchor.", comment: "")
  109. self.closeButton.title = NSLocalizedString("OK", comment: "")
  110. self.trustButton.title = NSLocalizedString("Add to Trusted Certificates...", comment: "")
  111. self.summaryIssuesLabel.stringValue = NSLocalizedString("Issued by:", comment: "")
  112. self.summaryVaildFromLabel.stringValue = NSLocalizedString("Valid from:", comment: "")
  113. self.summaryVaildtoLabel.stringValue = NSLocalizedString("Valid to:", comment: "")
  114. self.summaryUsageLabel.stringValue = NSLocalizedString("Intended usage:", comment: "")
  115. self.summaryExportBtn.title = NSLocalizedString("Export", comment: "")
  116. self.detailsDatasLabel.stringValue = NSLocalizedString("Certificate Data:", comment: "")
  117. self.policiesSubLabel.stringValue = NSLocalizedString("Signatures will be valid if the certificate matches this policy restriction. Policy restrictions are provided by your computer administrator or the certificate authority that issued this certificate. Certificates sometimes contain an identifier to indicate the certificate authority's policy for issuing the certificate. An example policy might be one which indicates that the signer was required to be personally present when issued his or her certificate. Only certificates that have been directly trusted can have policy restrictions.", comment: "")
  118. self.legalTitleLabel.stringValue = NSLocalizedString("Legal Disclaimer:", comment: "")
  119. self.legalSubLabel.stringValue = NSLocalizedString("Validation of a digitally signed document may require certificate-related services provided by independent third-party service vendors. PDF Reader Pro does not provide any warranties of any kind with respect to digitally signed documents, certificates used to create digitally signed documents, and any related services. ", comment: "")
  120. self.trustSubtitleLabel.stringValue = NSLocalizedString("Trust Settings", comment: "")
  121. self.trustDesTitleLabel.stringValue = NSLocalizedString("This certificate is trusted to:", comment: "")
  122. self.trustDes1Label.stringValue = NSLocalizedString("Sign documents or data", comment: "")
  123. self.trustDes2Label.stringValue = NSLocalizedString("Certify documents", comment: "")
  124. self.tabView.tabViewItem(at: 0).label = NSLocalizedString("Summary", comment: "")
  125. self.tabView.tabViewItem(at: 1).label = NSLocalizedString("Details", comment: "")
  126. self.tabView.tabViewItem(at: 2).label = NSLocalizedString("Trust", comment: "")
  127. self.tabView.tabViewItem(at: 3).label = NSLocalizedString("Policies", comment: "")
  128. self.tabView.tabViewItem(at: 4).label = NSLocalizedString("Legal Notice", comment: "")
  129. for column in self.detailsTableView.tableColumns {
  130. column.headerCell.title = NSLocalizedString(column.headerCell.title, comment: "")
  131. }
  132. self.titleLabel.textColor = NSColor.labelColor
  133. self.subTitleLabel.textColor = NSColor.labelColor
  134. self.summaryVaildFromLabel.textColor = NSColor.labelColor
  135. self.summaryVaildtoLabel.textColor = NSColor.labelColor
  136. self.summaryUsageLabel.textColor = NSColor.labelColor
  137. self.summaryIssuesLabel.textColor = NSColor.labelColor
  138. self.detailsDatasLabel.textColor = NSColor.labelColor
  139. self.legalTitleLabel.textColor = NSColor.labelColor
  140. self.legalSubLabel.textColor = NSColor.labelColor
  141. self.summaryNameTextField.textColor = NSColor.labelColor
  142. self.summaryIssuesTextField.textColor = NSColor.labelColor
  143. self.summaryVaildFromTextField.textColor = NSColor.labelColor
  144. self.summaryVaildtoTextField.textColor = NSColor.labelColor
  145. self.policiesSubLabel.textColor = NSColor.labelColor
  146. }
  147. func updateCertificateData () {
  148. for certificate in self.certificates {
  149. certificate.checkIsTrusted()
  150. }
  151. let row = self.tableView?.selectedRow
  152. self.certDatas.removeAllObjects()
  153. if row! > -1 && row! < self.models.count {
  154. let model = self.models.object(at: row!) as! CDSPDFSigntureModel
  155. let cert = model.certificate
  156. let keys = ["CN","OU","O", "emailAddress"]
  157. var subString = String()
  158. for key in keys {
  159. let subDic = cert?.subjectDict as? NSDictionary
  160. if subDic?.object(forKey: key) != nil {
  161. let keyValue = String(format: "%@", cert?.subjectDict[key] as! NSString)
  162. if keyValue.isEmpty == false {
  163. if subString.isEmpty == false {
  164. subString = subString + "\n" + keyValue
  165. } else {
  166. subString = keyValue
  167. }
  168. }
  169. }
  170. }
  171. var issuesString = String()
  172. for key in keys {
  173. let subDic = cert?.issuerDict as? NSDictionary
  174. if subDic?.object(forKey: key) != nil {
  175. let keyValue = cert?.issuerDict[key] as! String
  176. if keyValue.isEmpty == false {
  177. if issuesString.isEmpty == false {
  178. issuesString = issuesString + "\n" + keyValue
  179. } else {
  180. issuesString = keyValue
  181. }
  182. }
  183. }
  184. }
  185. self.summaryNameTextField.stringValue = subString
  186. self.summaryIssuesTextField.stringValue = issuesString
  187. let dateFormatter = DateFormatter.init()
  188. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  189. if cert?.validityStarts != nil {
  190. self.summaryVaildFromTextField.stringValue = dateFormatter.string(from: cert!.validityStarts)
  191. } else {
  192. self.summaryVaildFromTextField.stringValue = ""
  193. }
  194. if cert?.validityEnds != nil {
  195. self.summaryVaildtoTextField.stringValue = dateFormatter.string(from: cert!.validityEnds)
  196. } else {
  197. self.summaryVaildtoTextField.stringValue = ""
  198. }
  199. let att = NSMutableArray.init()
  200. if (cert?.keyUsage.contains(.encipherOnly) == true) {
  201. att.add(NSLocalizedString("Encipher Only", comment: ""))
  202. }
  203. if (cert?.keyUsage.contains(.crlSignature) == true) {
  204. att.add(NSLocalizedString("CRL Signature", comment: ""))
  205. }
  206. if (cert?.keyUsage.contains(.certificateSignature) == true) {
  207. att.add(NSLocalizedString("Certificate Signature", comment: ""))
  208. }
  209. if (cert?.keyUsage.contains(.keyAgreement) == true) {
  210. att.add(NSLocalizedString("Key Agreement", comment: ""))
  211. }
  212. if (cert?.keyUsage.contains(.dataEncipherment) == true) {
  213. att.add(NSLocalizedString("Data Encipherment", comment: ""))
  214. }
  215. if (cert?.keyUsage.contains(.keyEncipherment) == true) {
  216. att.add(NSLocalizedString("Key Encipherment", comment: ""))
  217. }
  218. if (cert?.keyUsage.contains(.nonRepudiation) == true) {
  219. att.add(NSLocalizedString("Non-Repudiation", comment: ""))
  220. }
  221. if (cert?.keyUsage.contains(.digitalSignature) == true) {
  222. att.add(NSLocalizedString("Digital Signature", comment: ""))
  223. }
  224. if (cert?.keyUsage.contains(.dDecipherOnly) == true) {
  225. att.add(NSLocalizedString("Decipher Only", comment: ""))
  226. }
  227. var usageString = String()
  228. for us in att {
  229. var usValue = String(format: "%@", us as! String)
  230. if usValue.isEmpty == false{
  231. if usageString.isEmpty == true {
  232. usageString = NSLocalizedString(usValue, comment: "")
  233. } else {
  234. usValue = NSLocalizedString(usValue, comment: "")
  235. usageString = String(format: "%@,%@", usageString, usValue)
  236. }
  237. }
  238. }
  239. self.summaryUsageTextView.string = usageString
  240. var dic = NSMutableDictionary.init()
  241. dic.setValue(cert?.version, forKey: NSLocalizedString("Version", comment: ""))
  242. self.certDatas.add(dic)
  243. dic = NSMutableDictionary.init()
  244. switch cert?.signatureAlgorithmType {
  245. case .RSA_RSA:
  246. dic.setValue("RSA_RSA", forKey: NSLocalizedString("Signature algorithm", comment: ""))
  247. break
  248. case .MD2RSA:
  249. dic.setValue("MD2RSA", forKey: NSLocalizedString("Signature algorithm", comment: ""))
  250. break
  251. case .MD4RSA:
  252. dic.setValue("MD4RSA", forKey: NSLocalizedString("Signature algorithm", comment: ""))
  253. break
  254. case .MD5RSA:
  255. break
  256. case .SHA1RSA:
  257. dic.setValue("SHA1RSA", forKey: NSLocalizedString("Signature algorithm", comment: ""))
  258. break
  259. case .SHA256RSA:
  260. dic.setValue("SHA256RSA", forKey: NSLocalizedString("Signature algorithm", comment: ""))
  261. break
  262. case .SM3SM2:
  263. dic.setValue("SM3SM2", forKey: NSLocalizedString("Signature algorithm", comment: ""))
  264. break
  265. default:
  266. break
  267. }
  268. self.certDatas.add(dic)
  269. dic = NSMutableDictionary.init()
  270. dic.setValue(cert?.subject, forKey: NSLocalizedString("Subject", comment: ""))
  271. self.certDatas.add(dic)
  272. dic = NSMutableDictionary.init()
  273. dic.setValue(cert?.issuer, forKey: NSLocalizedString("Issuer", comment: ""))
  274. self.certDatas.add(dic)
  275. dic = NSMutableDictionary.init()
  276. dic.setValue(cert?.serialNumber, forKey: NSLocalizedString("Serial number", comment: ""))
  277. self.certDatas.add(dic)
  278. if cert?.validityStarts != nil {
  279. dic = NSMutableDictionary.init()
  280. dic.setValue(dateFormatter.string(from: cert!.validityStarts), forKey: NSLocalizedString("Validity Starts", comment: ""))
  281. self.certDatas.add(dic)
  282. }
  283. if cert?.validityEnds != nil {
  284. dic = NSMutableDictionary.init()
  285. dic.setValue(dateFormatter.string(from: cert!.validityEnds), forKey: NSLocalizedString("Validity ends", comment: ""))
  286. self.certDatas.add(dic)
  287. }
  288. dic = NSMutableDictionary.init()
  289. dic.setValue(NSLocalizedString("<<see details>>", comment: ""), forKey: NSLocalizedString("Authority info access", comment: ""))
  290. self.certDatas.add(dic)
  291. dic = NSMutableDictionary.init()
  292. dic.setValue(NSLocalizedString("<<see details>>", comment: ""), forKey: NSLocalizedString("Subject key identifier", comment: ""))
  293. self.certDatas.add(dic)
  294. dic = NSMutableDictionary.init()
  295. dic.setValue(usageString, forKey: NSLocalizedString("Key usage", comment: ""))
  296. self.certDatas.add(dic)
  297. dic = NSMutableDictionary.init()
  298. dic.setValue(NSLocalizedString("<<see details>>", comment: ""), forKey: NSLocalizedString("Certificate policies", comment: ""))
  299. self.certDatas.add(dic)
  300. dic = NSMutableDictionary.init()
  301. dic.setValue(NSLocalizedString("<<see details>>", comment: ""), forKey: NSLocalizedString("Authority key identifier", comment: ""))
  302. self.certDatas.add(dic)
  303. dic = NSMutableDictionary.init()
  304. dic.setValue(NSLocalizedString("<<see details>>", comment: ""), forKey: NSLocalizedString("CRL distribution points", comment: ""))
  305. self.certDatas.add(dic)
  306. dic = NSMutableDictionary.init()
  307. dic.setValue(NSLocalizedString("<<see details>>", comment: ""), forKey: NSLocalizedString("Basic constraints", comment: ""))
  308. self.certDatas.add(dic)
  309. dic = NSMutableDictionary.init()
  310. dic.setValue(NSLocalizedString("<<see details>>", comment: ""), forKey: NSLocalizedString("Public key", comment: ""))
  311. self.certDatas.add(dic)
  312. dic = NSMutableDictionary.init()
  313. dic.setValue(NSLocalizedString("<<see details>>", comment: ""), forKey: NSLocalizedString("SHA1 digest of Public key", comment: ""))
  314. self.certDatas.add(dic)
  315. dic = NSMutableDictionary.init()
  316. dic.setValue(cert?.x509Data, forKey: NSLocalizedString("X.509 data", comment: ""))
  317. self.certDatas.add(dic)
  318. dic = NSMutableDictionary.init()
  319. dic.setValue(cert?.sha1Digest, forKey: NSLocalizedString("SHA1 digest", comment: ""))
  320. self.certDatas.add(dic)
  321. dic = NSMutableDictionary.init()
  322. dic.setValue(cert?.md5Digest, forKey: NSLocalizedString("MD5 digest", comment: ""))
  323. self.certDatas.add(dic)
  324. self.detailsTableView.reloadData()
  325. if cert?.isTrusted == false {
  326. self.trustTitleLabel.stringValue = NSLocalizedString("This certificate is not trusted.", comment: "")
  327. self.trustButton.isEnabled = true
  328. self.trustDes1ImageView.image = NSImage(named: "ImageNameSigntureTrustedFailureIcon")
  329. self.trustDes2ImageView.image = NSImage(named: "ImageNameSigntureTrustedFailureIcon")
  330. } else {
  331. self.trustTitleLabel.stringValue = NSLocalizedString("This certificate is trusted because you have the corresponding private key.", comment: "")
  332. self.trustButton.isEnabled = false
  333. self.trustDes1ImageView.image = NSImage(named: "ImageNameSigntureTrustedIcon")
  334. self.trustDes2ImageView.image = NSImage(named: "ImageNameSigntureTrustedIcon")
  335. }
  336. }
  337. }
  338. func sizeOfString(_ string: String, _ font: NSFont) -> (CGFloat) {
  339. let attributes: [NSAttributedString.Key: Any] = [
  340. .font: font
  341. ]
  342. let size = (string as NSString).boundingRect(with: NSSize(width: CGFloat(MAXFLOAT), height: font.pointSize+5),
  343. options: .usesLineFragmentOrigin,
  344. attributes: attributes,
  345. context: nil).size
  346. return size.width + 10
  347. }
  348. //MARK: IBAction
  349. @IBAction func actionClose(_ sender: Any) {
  350. self.dismiss(self)
  351. }
  352. @IBAction func exportAction(_ sender: NSButton) {
  353. let model = self.models[self.tableView.selectedRow] as! CDSPDFSigntureModel
  354. let cert = model.certificate
  355. var fileName = cert?.subjectDict["CN"] as! String
  356. if fileName.count == 0 {
  357. fileName = NSFullUserName()
  358. }
  359. fileName.append("_CertExchange")
  360. let outputSavePanel = NSSavePanel()
  361. outputSavePanel.allowedFileTypes = ["cer"]
  362. outputSavePanel.nameFieldStringValue = fileName
  363. let result = outputSavePanel.runModal()
  364. if result == .OK {
  365. let filePath = outputSavePanel.url?.path
  366. let success = cert?.export(toFilePath: filePath)
  367. if success == true {
  368. NSWorkspace.shared.selectFile(filePath, inFileViewerRootedAtPath: "")
  369. } else {
  370. let alert = NSAlert.init()
  371. alert.messageText = NSLocalizedString("Export Failure!", comment: "")
  372. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  373. alert.runModal()
  374. }
  375. }
  376. }
  377. @IBAction func buttonItemClick_TrustCer(_ sender: NSButton) {
  378. let model = self.models.object(at: self.tableView.selectedRow) as! CDSPDFSigntureModel
  379. let cert = model.certificate!
  380. let success = cert.addToTrustedCertificates()
  381. if success {
  382. self.updateCertificateData()
  383. if self.signature != nil {
  384. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "CSignatureTrustCerDidChangeNotification"), object: nil)
  385. self.signature.verifySignature(with: self.pdfListView.document)
  386. }
  387. let alert = NSAlert.init()
  388. alert.messageText = NSLocalizedString("Add to Trusted Certificate Successfully!", comment: "")
  389. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  390. alert.runModal()
  391. } else {
  392. let alert = NSAlert.init()
  393. alert.messageText = NSLocalizedString("Add to Trusted Certificate Failed!", comment: "")
  394. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  395. alert.runModal()
  396. }
  397. }
  398. //MARK: NSTableViewDelegate
  399. func numberOfRows(in tableView: NSTableView) -> Int {
  400. if tableView.isEqual(self.detailsTableView) {
  401. return self.certDatas.count
  402. } else {
  403. return self.models.count
  404. }
  405. }
  406. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  407. return 22.0
  408. }
  409. func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
  410. return NSTableRowView()
  411. }
  412. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  413. if self.detailsTableView.isEqual(tableView) {
  414. let dic = self.certDatas.object(at: row) as! NSDictionary
  415. let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(tableColumn?.identifier.rawValue ?? ""), owner: self) as! NSTableCellView
  416. if ((tableColumn?.identifier.rawValue) == "name") {
  417. cell.textField?.stringValue = dic.allKeys.first as! String
  418. } else if ((tableColumn?.identifier.rawValue) == "value") {
  419. // tableColumn?.title = NSLocalizedString("Value:", comment: "")
  420. let key = dic.allKeys.first as! String
  421. cell.textField?.stringValue = dic.object(forKey: key) as! String
  422. }
  423. return cell
  424. } else {
  425. let cellView = DSignDetailTypeACellView()
  426. cellView.model = self.models.object(at: row) as! CDSPDFSigntureModel
  427. let width = self.sizeOfString(cellView.contendLabel.stringValue, cellView.contendLabel.font!)
  428. let max = max(width, tableColumn!.width)
  429. tableColumn?.width = max
  430. return cellView
  431. }
  432. }
  433. func tableViewSelectionDidChange(_ notification: Notification) {
  434. let tableView = notification.object
  435. if self.detailsTableView.isEqual(tableView) {
  436. let model = self.models.object(at: self.tableView.selectedRow) as! CDSPDFSigntureModel
  437. let cert = model.certificate!
  438. let dateFormatter = DateFormatter.init()
  439. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  440. let selectedRow = self.detailsTableView.selectedRow
  441. if selectedRow == 0 {
  442. self.detailsTextView.string = cert.version ?? ""
  443. } else if selectedRow == 1 {
  444. switch cert.signatureAlgorithmType {
  445. case .RSA_RSA:
  446. self.detailsTextView.string = String(format: "%@(%@)","RSA_RSA",cert.signatureAlgorithmOID)
  447. break;
  448. case .MD2RSA:
  449. self.detailsTextView.string = String(format: "%@(%@)","MD2RSA",cert.signatureAlgorithmOID)
  450. break;
  451. case .MD4RSA:
  452. self.detailsTextView.string = String(format: "%@(%@)", "MD4RSA",cert.signatureAlgorithmOID)
  453. break;
  454. case .SHA1RSA:
  455. self.detailsTextView.string = String(format: "%@(%@)", "SHA1RSA",cert.signatureAlgorithmOID)
  456. break;
  457. case .SHA256RSA:
  458. self.detailsTextView.string = String(format: "%@(%@)", "SHA256RSA",cert.signatureAlgorithmOID)
  459. break;
  460. case .SM3SM2:
  461. self.detailsTextView.string = String(format: "%@(%@)", "SM3SM2",cert.signatureAlgorithmOID)
  462. break;
  463. default: break
  464. }
  465. } else if selectedRow == 2 {
  466. var string = ""
  467. for key in cert.subjectDict.keys {
  468. if (string.count == 0) {
  469. string = String(format: "%@=%@",key as! String, cert.subjectDict[key] as! String)
  470. } else {
  471. string = String(format: "%@\n%@=%@",string, (key as! String), cert.subjectDict[key] as! String)
  472. }
  473. }
  474. self.detailsTextView.string = string
  475. } else if selectedRow == 3 {
  476. var string = ""
  477. for key in cert.issuerDict.keys {
  478. if (string.count == 0) {
  479. string = String(format: "%@=%@",key as! String, cert.issuerDict[key] as! String)
  480. } else {
  481. string = String(format: "%@\n%@=%@",string, (key as! String), cert.issuerDict[key] as! String)
  482. }
  483. }
  484. self.detailsTextView.string = string
  485. } else if selectedRow == 4 {
  486. self.detailsTextView.string = cert.serialNumber ?? ""
  487. } else if selectedRow == 5 {
  488. self.detailsTextView.string = dateFormatter.string(from: cert.validityStarts)
  489. } else if selectedRow == 6 {
  490. self.detailsTextView.string = dateFormatter.string(from: cert.validityEnds)
  491. } else if selectedRow == 7 {
  492. var content = ""
  493. for infoDic in cert.authorityInfoAccess {
  494. let dic: Dictionary = infoDic
  495. if content.count == 0 {
  496. content = String(format: "%@ = %@", dic["Method"] as! String, (dic["Method"] as! String))
  497. content = content + "\n"
  498. let tString = String(format: "URL = %@", (dic["URI"] as! String))
  499. content = content + tString
  500. } else {
  501. content = content + "\n"
  502. content = content + "\n"
  503. var tString = String(format: "Method = %@", (dic["Method"] as! String))
  504. content = content + tString
  505. content = content + "\n"
  506. tString = String(format: "URL = %@",(dic["URI"] as! String))
  507. content = content + tString
  508. }
  509. }
  510. self.detailsTextView.string = content
  511. } else if selectedRow == 8 {
  512. self.detailsTextView.string = cert.subjectKeyIdentifier.uppercased()
  513. } else if selectedRow == 9 {
  514. let att = NSMutableArray()
  515. if cert.keyUsage.contains(.encipherOnly) == true {
  516. att.add(NSLocalizedString("Encipher Only", comment: ""))
  517. } else if cert.keyUsage.contains(.crlSignature) == true {
  518. att.add(NSLocalizedString("CRL Signature", comment: ""))
  519. } else if cert.keyUsage.contains(.certificateSignature) == true {
  520. att.add(NSLocalizedString("Certificate Signature", comment: ""))
  521. } else if cert.keyUsage.contains(.keyAgreement) == true {
  522. att.add(NSLocalizedString("Key Agreement", comment: ""))
  523. } else if cert.keyUsage.contains(.dataEncipherment) == true {
  524. att.add(NSLocalizedString("Data Encipherment", comment: ""))
  525. } else if cert.keyUsage.contains(.keyEncipherment) == true {
  526. att.add(NSLocalizedString("Key Encipherment", comment: ""))
  527. } else if cert.keyUsage.contains(.nonRepudiation) == true {
  528. att.add(NSLocalizedString("Non-Repudiation", comment: ""))
  529. } else if cert.keyUsage.contains(.digitalSignature) == true {
  530. att.add(NSLocalizedString("Digital Signature", comment: ""))
  531. } else if cert.keyUsage.contains(.dDecipherOnly) == true {
  532. att.add(NSLocalizedString("Decipher Only", comment: ""))
  533. }
  534. var usageString = ""
  535. for us in att {
  536. if usageString.count == 0 {
  537. usageString = NSLocalizedString(us as! String, comment: "")
  538. } else {
  539. usageString = String(format: "%@\n%@",usageString, NSLocalizedString(us as! String, comment: ""))
  540. }
  541. if usageString.count == 0 {
  542. } else {
  543. usageString = String(format: "%@\n%@",usageString, NSLocalizedString(us as! String, comment: ""))
  544. }
  545. }
  546. self.detailsTextView.string = usageString
  547. } else if selectedRow == 10 {
  548. self.detailsTextView.string = cert.certificatePolicies ?? ""
  549. } else if selectedRow == 11 {
  550. self.detailsTextView.string = cert.authorityKeyIdentifier.uppercased()
  551. } else if selectedRow == 12 {
  552. var content = ""
  553. for tString in cert.crlDistributionPoints {
  554. if content.count == 0 {
  555. content = tString
  556. } else {
  557. content = content + "\n"
  558. content = content + tString
  559. }
  560. }
  561. self.detailsTextView.string = content
  562. } else if selectedRow == 13 {
  563. self.detailsTextView.string = cert.basicConstraints.replacingOccurrences(of: ",", with: "\n")
  564. } else if selectedRow == 14 {
  565. self.detailsTextView.string = cert.publicKey.replacingOccurrences(of: ":", with: " ")
  566. } else if selectedRow == 15 {
  567. self.detailsTextView.string = cert.subjectKeyIdentifier.uppercased()
  568. } else if selectedRow == 16 {
  569. self.detailsTextView.string = cert.x509Data.replacingOccurrences(of: ":", with: " ")
  570. } else if selectedRow == 17 {
  571. self.detailsTextView.string = cert.sha1Digest.replacingOccurrences(of: ":", with: " ")
  572. } else if selectedRow == 18 {
  573. self.detailsTextView.string = cert.md5Digest.replacingOccurrences(of: ":", with: " ")
  574. }
  575. } else {
  576. self.detailsTextView.string = ""
  577. self.updateCertificateData()
  578. }
  579. }
  580. }