michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const nsIFilePicker = Components.interfaces.nsIFilePicker; michael@0: const nsFilePicker = "@mozilla.org/filepicker;1"; michael@0: const nsIX509CertDB = Components.interfaces.nsIX509CertDB; michael@0: const nsX509CertDB = "@mozilla.org/security/x509certdb;1"; michael@0: const nsIX509Cert = Components.interfaces.nsIX509Cert; michael@0: const nsICertTree = Components.interfaces.nsICertTree; michael@0: const nsCertTree = "@mozilla.org/security/nsCertTree;1"; michael@0: const nsIDialogParamBlock = Components.interfaces.nsIDialogParamBlock; michael@0: const nsDialogParamBlock = "@mozilla.org/embedcomp/dialogparam;1"; michael@0: const nsIPKIParamBlock = Components.interfaces.nsIPKIParamBlock; michael@0: const nsPKIParamBlock = "@mozilla.org/security/pkiparamblock;1"; michael@0: const nsINSSCertCache = Components.interfaces.nsINSSCertCache; michael@0: const nsNSSCertCache = "@mozilla.org/security/nsscertcache;1"; michael@0: michael@0: var key; michael@0: michael@0: var selected_certs = []; michael@0: var selected_tree_items = []; michael@0: var selected_index = []; michael@0: var certdb; michael@0: michael@0: var caTreeView; michael@0: var serverTreeView; michael@0: var emailTreeView; michael@0: var userTreeView; michael@0: var orphanTreeView; michael@0: michael@0: function LoadCerts() michael@0: { michael@0: window.crypto.enableSmartCardEvents = true; michael@0: document.addEventListener("smartcard-insert", onSmartCardChange, false); michael@0: document.addEventListener("smartcard-remove", onSmartCardChange, false); michael@0: michael@0: certdb = Components.classes[nsX509CertDB].getService(nsIX509CertDB); michael@0: var certcache = Components.classes[nsNSSCertCache].createInstance(nsINSSCertCache); michael@0: michael@0: certcache.cacheAllCerts(); michael@0: michael@0: caTreeView = Components.classes[nsCertTree] michael@0: .createInstance(nsICertTree); michael@0: caTreeView.loadCertsFromCache(certcache, nsIX509Cert.CA_CERT); michael@0: document.getElementById('ca-tree') michael@0: .treeBoxObject.view = caTreeView; michael@0: michael@0: serverTreeView = Components.classes[nsCertTree] michael@0: .createInstance(nsICertTree); michael@0: serverTreeView.loadCertsFromCache(certcache, nsIX509Cert.SERVER_CERT); michael@0: document.getElementById('server-tree') michael@0: .treeBoxObject.view = serverTreeView; michael@0: michael@0: emailTreeView = Components.classes[nsCertTree] michael@0: .createInstance(nsICertTree); michael@0: emailTreeView.loadCertsFromCache(certcache, nsIX509Cert.EMAIL_CERT); michael@0: document.getElementById('email-tree') michael@0: .treeBoxObject.view = emailTreeView; michael@0: michael@0: userTreeView = Components.classes[nsCertTree] michael@0: .createInstance(nsICertTree); michael@0: userTreeView.loadCertsFromCache(certcache, nsIX509Cert.USER_CERT); michael@0: document.getElementById('user-tree') michael@0: .treeBoxObject.view = userTreeView; michael@0: michael@0: orphanTreeView = Components.classes[nsCertTree] michael@0: .createInstance(nsICertTree); michael@0: orphanTreeView.loadCertsFromCache(certcache, nsIX509Cert.UNKNOWN_CERT); michael@0: document.getElementById('orphan-tree') michael@0: .treeBoxObject.view = orphanTreeView; michael@0: michael@0: enableBackupAllButton(); michael@0: } michael@0: michael@0: function enableBackupAllButton() michael@0: { michael@0: var rowCnt = userTreeView.rowCount; michael@0: var backupAllButton=document.getElementById('mine_backupAllButton'); michael@0: backupAllButton.disabled = (rowCnt < 1); michael@0: } michael@0: michael@0: function getSelectedCerts() michael@0: { michael@0: var ca_tab = document.getElementById("ca_tab"); michael@0: var mine_tab = document.getElementById("mine_tab"); michael@0: var others_tab = document.getElementById("others_tab"); michael@0: var websites_tab = document.getElementById("websites_tab"); michael@0: var orphan_tab = document.getElementById("orphan_tab"); michael@0: var items = null; michael@0: if (ca_tab.selected) { michael@0: items = caTreeView.selection; michael@0: } else if (mine_tab.selected) { michael@0: items = userTreeView.selection; michael@0: } else if (others_tab.selected) { michael@0: items = emailTreeView.selection; michael@0: } else if (websites_tab.selected) { michael@0: items = serverTreeView.selection; michael@0: } else if (orphan_tab.selected) { michael@0: items = orphanTreeView.selection; michael@0: } michael@0: selected_certs = []; michael@0: var cert = null; michael@0: var nr = 0; michael@0: if (items != null) nr = items.getRangeCount(); michael@0: if (nr > 0) { michael@0: for (var i=0; i 0) { michael@0: for (var i=0; i 0) { michael@0: for (var i=0; i 0) { michael@0: enable_delete = true; michael@0: } michael@0: michael@0: if (count_ranges == 1) { michael@0: var o1 = {}; michael@0: var o2 = {}; michael@0: items.getRangeAt(0, o1, o2); // the first range michael@0: if (o1.value == o2.value) { michael@0: // only a single item is selected michael@0: try { michael@0: var ti = serverTreeView.getTreeItem(o1.value); michael@0: if (ti) { michael@0: if (ti.cert) { michael@0: enable_view = true; michael@0: } michael@0: } michael@0: } michael@0: catch (e) { michael@0: } michael@0: } michael@0: } michael@0: michael@0: var enableViewButton=document.getElementById('websites_viewButton'); michael@0: enableViewButton.setAttribute("disabled", !enable_view); michael@0: var enableExportButton=document.getElementById('websites_exportButton'); michael@0: enableExportButton.setAttribute("disabled", !enable_view); michael@0: var enableDeleteButton=document.getElementById('websites_deleteButton'); michael@0: enableDeleteButton.setAttribute("disabled", !enable_delete); michael@0: } michael@0: michael@0: function email_enableButtons() michael@0: { michael@0: var items = emailTreeView.selection; michael@0: var toggle="false"; michael@0: if (items.getRangeCount() == 0) { michael@0: toggle="true"; michael@0: } michael@0: var enableViewButton=document.getElementById('email_viewButton'); michael@0: enableViewButton.setAttribute("disabled",toggle); michael@0: var enableEditButton=document.getElementById('email_editButton'); michael@0: enableEditButton.setAttribute("disabled",toggle); michael@0: var enableExportButton=document.getElementById('email_exportButton'); michael@0: enableExportButton.setAttribute("disabled",toggle); michael@0: var enableDeleteButton=document.getElementById('email_deleteButton'); michael@0: enableDeleteButton.setAttribute("disabled",toggle); michael@0: } michael@0: michael@0: function orphan_enableButtons() michael@0: { michael@0: var items = orphanTreeView.selection; michael@0: var toggle="false"; michael@0: if (items.getRangeCount() == 0) { michael@0: toggle="true"; michael@0: } michael@0: var enableViewButton=document.getElementById('orphan_viewButton'); michael@0: enableViewButton.setAttribute("disabled",toggle); michael@0: var enableExportButton=document.getElementById('orphan_exportButton'); michael@0: enableExportButton.setAttribute("disabled",toggle); michael@0: var enableDeleteButton=document.getElementById('orphan_deleteButton'); michael@0: enableDeleteButton.setAttribute("disabled",toggle); michael@0: } michael@0: michael@0: function backupCerts() michael@0: { michael@0: getSelectedCerts(); michael@0: var numcerts = selected_certs.length; michael@0: if (!numcerts) michael@0: return; michael@0: var bundle = document.getElementById("pippki_bundle"); michael@0: var fp = Components.classes[nsFilePicker].createInstance(nsIFilePicker); michael@0: fp.init(window, michael@0: bundle.getString("chooseP12BackupFileDialog"), michael@0: nsIFilePicker.modeSave); michael@0: fp.appendFilter(bundle.getString("file_browse_PKCS12_spec"), michael@0: "*.p12"); michael@0: fp.appendFilters(nsIFilePicker.filterAll); michael@0: var rv = fp.show(); michael@0: if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) { michael@0: certdb.exportPKCS12File(null, fp.file, michael@0: selected_certs.length, selected_certs); michael@0: } michael@0: } michael@0: michael@0: function backupAllCerts() michael@0: { michael@0: // Select all rows, then call doBackup() michael@0: var items = userTreeView.selection.selectAll(); michael@0: backupCerts(); michael@0: } michael@0: michael@0: function editCerts() michael@0: { michael@0: getSelectedCerts(); michael@0: var numcerts = selected_certs.length; michael@0: if (!numcerts) michael@0: return; michael@0: for (var t=0; t=0; t--) michael@0: { michael@0: treeView.deleteEntryObject(selected_index[t]); michael@0: } michael@0: michael@0: selected_tree_items = []; michael@0: selected_index = []; michael@0: treeView.selection.clearSelection(); michael@0: if (selTabID == 'mine_tab') { michael@0: enableBackupAllButton(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: function viewCerts() michael@0: { michael@0: getSelectedCerts(); michael@0: var numcerts = selected_certs.length; michael@0: if (!numcerts) michael@0: return; michael@0: michael@0: for (var t=0; t