Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | const Ci = Components.interfaces; |
michael@0 | 6 | const Cu = Components.utils; |
michael@0 | 7 | const Cc = Components.classes; |
michael@0 | 8 | |
michael@0 | 9 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 10 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 11 | Cu.import("resource://gre/modules/Prompt.jsm"); |
michael@0 | 12 | |
michael@0 | 13 | // ----------------------------------------------------------------------- |
michael@0 | 14 | // NSS Dialog Service |
michael@0 | 15 | // ----------------------------------------------------------------------- |
michael@0 | 16 | |
michael@0 | 17 | function dump(a) { |
michael@0 | 18 | Components.classes["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(a); |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | function NSSDialogs() { } |
michael@0 | 22 | |
michael@0 | 23 | NSSDialogs.prototype = { |
michael@0 | 24 | classID: Components.ID("{cbc08081-49b6-4561-9c18-a7707a50bda1}"), |
michael@0 | 25 | QueryInterface: XPCOMUtils.generateQI([Ci.nsICertificateDialogs, Ci.nsIClientAuthDialogs]), |
michael@0 | 26 | |
michael@0 | 27 | getString: function(aName) { |
michael@0 | 28 | if (!this.bundle) { |
michael@0 | 29 | this.bundle = Services.strings.createBundle("chrome://browser/locale/pippki.properties"); |
michael@0 | 30 | } |
michael@0 | 31 | return this.bundle.GetStringFromName(aName); |
michael@0 | 32 | }, |
michael@0 | 33 | |
michael@0 | 34 | formatString: function(aName, argList) { |
michael@0 | 35 | if (!this.bundle) { |
michael@0 | 36 | this.bundle = Services.strings.createBundle("chrome://browser/locale/pippki.properties"); |
michael@0 | 37 | } |
michael@0 | 38 | return this.bundle.formatStringFromName(aName, argList, 1); |
michael@0 | 39 | }, |
michael@0 | 40 | |
michael@0 | 41 | getPrompt: function(aTitle, aText, aButtons) { |
michael@0 | 42 | return new Prompt({ |
michael@0 | 43 | title: aTitle, |
michael@0 | 44 | text: aText, |
michael@0 | 45 | buttons: aButtons, |
michael@0 | 46 | }); |
michael@0 | 47 | }, |
michael@0 | 48 | |
michael@0 | 49 | showPrompt: function(aPrompt) { |
michael@0 | 50 | let response = null; |
michael@0 | 51 | aPrompt.show(function(data) { |
michael@0 | 52 | response = data; |
michael@0 | 53 | }); |
michael@0 | 54 | |
michael@0 | 55 | // Spin this thread while we wait for a result |
michael@0 | 56 | let thread = Services.tm.currentThread; |
michael@0 | 57 | while (response === null) |
michael@0 | 58 | thread.processNextEvent(true); |
michael@0 | 59 | |
michael@0 | 60 | return response; |
michael@0 | 61 | }, |
michael@0 | 62 | |
michael@0 | 63 | confirmDownloadCACert: function(aCtx, aCert, aTrust) { |
michael@0 | 64 | while (true) { |
michael@0 | 65 | let prompt = this.getPrompt(this.getString("downloadCert.title"), |
michael@0 | 66 | this.getString("downloadCert.message1"), |
michael@0 | 67 | [ this.getString("nssdialogs.ok.label"), |
michael@0 | 68 | this.getString("downloadCert.viewCert.label"), |
michael@0 | 69 | this.getString("nssdialogs.cancel.label") |
michael@0 | 70 | ]); |
michael@0 | 71 | |
michael@0 | 72 | prompt.addCheckbox({ id: "trustSSL", label: this.getString("downloadCert.trustSSL"), checked: false }) |
michael@0 | 73 | .addCheckbox({ id: "trustEmail", label: this.getString("downloadCert.trustEmail"), checked: false }) |
michael@0 | 74 | .addCheckbox({ id: "trustSign", label: this.getString("downloadCert.trustObjSign"), checked: false }); |
michael@0 | 75 | let response = this.showPrompt(prompt); |
michael@0 | 76 | |
michael@0 | 77 | // they hit the "view cert" button, so show the cert and try again |
michael@0 | 78 | if (response.button == 1) { |
michael@0 | 79 | this.viewCert(aCtx, aCert); |
michael@0 | 80 | continue; |
michael@0 | 81 | } else if (response.button != 0) { |
michael@0 | 82 | return false; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | aTrust.value = Ci.nsIX509CertDB.UNTRUSTED; |
michael@0 | 86 | if (response.trustSSL == "true") aTrust.value |= Ci.nsIX509CertDB.TRUSTED_SSL; |
michael@0 | 87 | if (response.trustEmail == "true") aTrust.value |= Ci.nsIX509CertDB.TRUSTED_EMAIL; |
michael@0 | 88 | if (response.trustSign == "true") aTrust.value |= Ci.nsIX509CertDB.TRUSTED_OBJSIGN; |
michael@0 | 89 | return true; |
michael@0 | 90 | } |
michael@0 | 91 | }, |
michael@0 | 92 | |
michael@0 | 93 | notifyCACertExists: function(aCtx) { |
michael@0 | 94 | let p = this.getPrompt(this.getString("caCertExists.title"), this.getString("caCertExists.message")); |
michael@0 | 95 | this.showPrompt(p); |
michael@0 | 96 | }, |
michael@0 | 97 | |
michael@0 | 98 | setPKCS12FilePassword: function(aCtx, aPassword) { |
michael@0 | 99 | // this dialog is never shown in Fennec; in Desktop it is shown while backing up a personal |
michael@0 | 100 | // certificate to a file via Preferences->Advanced->Encryption->View Certificates->Your Certificates |
michael@0 | 101 | throw "Unimplemented"; |
michael@0 | 102 | }, |
michael@0 | 103 | |
michael@0 | 104 | getPKCS12FilePassword: function(aCtx, aPassword) { |
michael@0 | 105 | let prompt = this.getPrompt(this.getString("pkcs12.getpassword.title"), |
michael@0 | 106 | this.getString("pkcs12.getpassword.message"), |
michael@0 | 107 | [ this.getString("nssdialogs.ok.label"), |
michael@0 | 108 | this.getString("nssdialogs.cancel.label") |
michael@0 | 109 | ]).addPassword({id: "pw"}); |
michael@0 | 110 | let response = this.showPrompt(prompt); |
michael@0 | 111 | if (response.button != 0) { |
michael@0 | 112 | return false; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | aPassword.value = response.pw; |
michael@0 | 116 | return true; |
michael@0 | 117 | }, |
michael@0 | 118 | |
michael@0 | 119 | certInfoSection: function(aHeading, aDataPairs, aTrailingNewline = true) { |
michael@0 | 120 | var str = "<big>" + this.getString(aHeading) + "</big><br/>"; |
michael@0 | 121 | for (var i = 0; i < aDataPairs.length; i += 2) { |
michael@0 | 122 | str += this.getString(aDataPairs[i]) + ": " + aDataPairs[i+1] + "<br/>"; |
michael@0 | 123 | } |
michael@0 | 124 | return str + (aTrailingNewline ? "<br/>" : ""); |
michael@0 | 125 | }, |
michael@0 | 126 | |
michael@0 | 127 | viewCert: function(aCtx, aCert) { |
michael@0 | 128 | let p = this.getPrompt(this.getString("certmgr.title"), |
michael@0 | 129 | "", |
michael@0 | 130 | [ this.getString("nssdialogs.ok.label") ]) |
michael@0 | 131 | p.addLabel({ label: this.certInfoSection("certmgr.subjectinfo.label", |
michael@0 | 132 | ["certmgr.certdetail.cn", aCert.commonName, |
michael@0 | 133 | "certmgr.certdetail.o", aCert.organization, |
michael@0 | 134 | "certmgr.certdetail.ou", aCert.organizationalUnit, |
michael@0 | 135 | "certmgr.certdetail.serialnumber", aCert.serialNumber])}) |
michael@0 | 136 | .addLabel({ label: this.certInfoSection("certmgr.issuerinfo.label", |
michael@0 | 137 | ["certmgr.certdetail.cn", aCert.issuerCommonName, |
michael@0 | 138 | "certmgr.certdetail.o", aCert.issuerOrganization, |
michael@0 | 139 | "certmgr.certdetail.ou", aCert.issuerOrganizationUnit])}) |
michael@0 | 140 | .addLabel({ label: this.certInfoSection("certmgr.periodofvalidity.label", |
michael@0 | 141 | ["certmgr.begins", aCert.validity.notBeforeLocalDay, |
michael@0 | 142 | "certmgr.expires", aCert.validity.notAfterLocalDay])}) |
michael@0 | 143 | .addLabel({ label: this.certInfoSection("certmgr.fingerprints.label", |
michael@0 | 144 | ["certmgr.certdetail.sha1fingerprint", aCert.sha1Fingerprint, |
michael@0 | 145 | "certmgr.certdetail.md5fingerprint", aCert.md5Fingerprint], false) }); |
michael@0 | 146 | this.showPrompt(p); |
michael@0 | 147 | }, |
michael@0 | 148 | |
michael@0 | 149 | viewCertDetails: function(details) { |
michael@0 | 150 | let p = this.getPrompt(this.getString("clientAuthAsk.message3"), |
michael@0 | 151 | '', |
michael@0 | 152 | [ this.getString("nssdialogs.ok.label") ]); |
michael@0 | 153 | p.addLabel({ label: details }); |
michael@0 | 154 | this.showPrompt(p); |
michael@0 | 155 | }, |
michael@0 | 156 | |
michael@0 | 157 | ChooseCertificate: function(aCtx, cn, organization, issuer, certNickList, certDetailsList, count, selectedIndex, canceled) { |
michael@0 | 158 | let rememberSetting = true; |
michael@0 | 159 | var pref = Cc['@mozilla.org/preferences-service;1'] |
michael@0 | 160 | .getService(Components.interfaces.nsIPrefService); |
michael@0 | 161 | if (pref) { |
michael@0 | 162 | pref = pref.getBranch(null); |
michael@0 | 163 | try { |
michael@0 | 164 | rememberSetting = pref.getBoolPref("security.remember_cert_checkbox_default_setting"); |
michael@0 | 165 | } catch (e) { |
michael@0 | 166 | // pref is missing |
michael@0 | 167 | } |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | let organizationString = this.formatString("clientAuthAsk.organization", |
michael@0 | 171 | [organization]); |
michael@0 | 172 | let issuerString = this.formatString("clientAuthAsk.issuer", |
michael@0 | 173 | [issuer]); |
michael@0 | 174 | let serverRequestedDetails = cn + '<br/>' + organizationString + '<br/>' + issuerString; |
michael@0 | 175 | |
michael@0 | 176 | selectedIndex = 0; |
michael@0 | 177 | while (true) { |
michael@0 | 178 | let prompt = this.getPrompt(this.getString("clientAuthAsk.title"), |
michael@0 | 179 | this.getString("clientAuthAsk.message1"), |
michael@0 | 180 | [ this.getString("nssdialogs.ok.label"), |
michael@0 | 181 | this.getString("clientAuthAsk.viewCert.label"), |
michael@0 | 182 | this.getString("nssdialogs.cancel.label") |
michael@0 | 183 | ]) |
michael@0 | 184 | .addLabel({ id: "requestedDetails", label: serverRequestedDetails } ) |
michael@0 | 185 | .addMenulist({ |
michael@0 | 186 | id: "nicknames", |
michael@0 | 187 | label: this.getString("clientAuthAsk.message2"), |
michael@0 | 188 | values: certNickList, selected: selectedIndex |
michael@0 | 189 | }).addCheckbox({ |
michael@0 | 190 | id: "rememberBox", |
michael@0 | 191 | label: this.getString("clientAuthAsk.remember.label"), |
michael@0 | 192 | checked: rememberSetting |
michael@0 | 193 | }); |
michael@0 | 194 | let response = this.showPrompt(prompt); |
michael@0 | 195 | selectedIndex = response.nicknames; |
michael@0 | 196 | if (response.button == 1) { |
michael@0 | 197 | this.viewCertDetails(certDetailsList[selectedIndex]); |
michael@0 | 198 | continue; |
michael@0 | 199 | } else if (response.button == 0) { |
michael@0 | 200 | canceled.value = false; |
michael@0 | 201 | if (response.rememberBox == "true") { |
michael@0 | 202 | aCtx.QueryInterface(Ci.nsIClientAuthUserDecision).rememberClientAuthCertificate = true; |
michael@0 | 203 | } |
michael@0 | 204 | return true; |
michael@0 | 205 | } |
michael@0 | 206 | canceled.value = true; |
michael@0 | 207 | return false; |
michael@0 | 208 | } |
michael@0 | 209 | } |
michael@0 | 210 | }; |
michael@0 | 211 | |
michael@0 | 212 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([NSSDialogs]); |