mobile/android/components/NSSDialogService.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/components/NSSDialogService.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,212 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const Ci = Components.interfaces;
     1.9 +const Cu = Components.utils;
    1.10 +const Cc = Components.classes;
    1.11 +
    1.12 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.13 +Cu.import("resource://gre/modules/Services.jsm");
    1.14 +Cu.import("resource://gre/modules/Prompt.jsm");
    1.15 +
    1.16 +// -----------------------------------------------------------------------
    1.17 +// NSS Dialog Service
    1.18 +// -----------------------------------------------------------------------
    1.19 +
    1.20 +function dump(a) {
    1.21 +  Components.classes["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(a);
    1.22 +}
    1.23 +
    1.24 +function NSSDialogs() { }
    1.25 +
    1.26 +NSSDialogs.prototype = {
    1.27 +  classID: Components.ID("{cbc08081-49b6-4561-9c18-a7707a50bda1}"),
    1.28 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsICertificateDialogs, Ci.nsIClientAuthDialogs]),
    1.29 +
    1.30 +  getString: function(aName) {
    1.31 +    if (!this.bundle) {
    1.32 +        this.bundle = Services.strings.createBundle("chrome://browser/locale/pippki.properties");
    1.33 +    }
    1.34 +    return this.bundle.GetStringFromName(aName);
    1.35 +  },
    1.36 +
    1.37 +  formatString: function(aName, argList) {
    1.38 +    if (!this.bundle) {
    1.39 +      this.bundle = Services.strings.createBundle("chrome://browser/locale/pippki.properties");
    1.40 +    }
    1.41 +    return this.bundle.formatStringFromName(aName, argList, 1);
    1.42 +  },
    1.43 +
    1.44 +  getPrompt: function(aTitle, aText, aButtons) {
    1.45 +    return new Prompt({
    1.46 +      title: aTitle,
    1.47 +      text: aText,
    1.48 +      buttons: aButtons,
    1.49 +    });
    1.50 +  },
    1.51 +
    1.52 +  showPrompt: function(aPrompt) {
    1.53 +    let response = null;
    1.54 +    aPrompt.show(function(data) {
    1.55 +      response = data;
    1.56 +    });
    1.57 +
    1.58 +    // Spin this thread while we wait for a result
    1.59 +    let thread = Services.tm.currentThread;
    1.60 +    while (response === null)
    1.61 +      thread.processNextEvent(true);
    1.62 +
    1.63 +    return response;
    1.64 +  },
    1.65 +
    1.66 +  confirmDownloadCACert: function(aCtx, aCert, aTrust) {
    1.67 +    while (true) {
    1.68 +      let prompt = this.getPrompt(this.getString("downloadCert.title"),
    1.69 +                                  this.getString("downloadCert.message1"),
    1.70 +                                  [ this.getString("nssdialogs.ok.label"),
    1.71 +                                    this.getString("downloadCert.viewCert.label"),
    1.72 +                                    this.getString("nssdialogs.cancel.label")
    1.73 +                                  ]);
    1.74 +
    1.75 +      prompt.addCheckbox({ id: "trustSSL", label: this.getString("downloadCert.trustSSL"), checked: false })
    1.76 +            .addCheckbox({ id: "trustEmail", label: this.getString("downloadCert.trustEmail"), checked: false })
    1.77 +            .addCheckbox({ id: "trustSign", label: this.getString("downloadCert.trustObjSign"), checked: false });
    1.78 +      let response = this.showPrompt(prompt);
    1.79 +
    1.80 +      // they hit the "view cert" button, so show the cert and try again
    1.81 +      if (response.button == 1) {
    1.82 +        this.viewCert(aCtx, aCert);
    1.83 +        continue;
    1.84 +      } else if (response.button != 0) {
    1.85 +        return false;
    1.86 +      }
    1.87 +
    1.88 +      aTrust.value = Ci.nsIX509CertDB.UNTRUSTED;
    1.89 +      if (response.trustSSL == "true") aTrust.value |= Ci.nsIX509CertDB.TRUSTED_SSL;
    1.90 +      if (response.trustEmail == "true") aTrust.value |= Ci.nsIX509CertDB.TRUSTED_EMAIL;
    1.91 +      if (response.trustSign == "true") aTrust.value |= Ci.nsIX509CertDB.TRUSTED_OBJSIGN;
    1.92 +      return true;
    1.93 +    }
    1.94 +  },
    1.95 +
    1.96 +  notifyCACertExists: function(aCtx) {
    1.97 +    let p = this.getPrompt(this.getString("caCertExists.title"), this.getString("caCertExists.message"));
    1.98 +    this.showPrompt(p);
    1.99 +  },
   1.100 +
   1.101 +  setPKCS12FilePassword: function(aCtx, aPassword) {
   1.102 +    // this dialog is never shown in Fennec; in Desktop it is shown while backing up a personal
   1.103 +    // certificate to a file via Preferences->Advanced->Encryption->View Certificates->Your Certificates
   1.104 +    throw "Unimplemented";
   1.105 +  },
   1.106 +
   1.107 +  getPKCS12FilePassword: function(aCtx, aPassword) {
   1.108 +    let prompt = this.getPrompt(this.getString("pkcs12.getpassword.title"),
   1.109 +                                this.getString("pkcs12.getpassword.message"),
   1.110 +                                [ this.getString("nssdialogs.ok.label"),
   1.111 +                                  this.getString("nssdialogs.cancel.label")
   1.112 +                                ]).addPassword({id: "pw"});
   1.113 +    let response = this.showPrompt(prompt);
   1.114 +    if (response.button != 0) {
   1.115 +      return false;
   1.116 +    }
   1.117 +
   1.118 +    aPassword.value = response.pw;
   1.119 +    return true;
   1.120 +  },
   1.121 +
   1.122 +  certInfoSection: function(aHeading, aDataPairs, aTrailingNewline = true) {
   1.123 +    var str = "<big>" + this.getString(aHeading) + "</big><br/>";
   1.124 +    for (var i = 0; i < aDataPairs.length; i += 2) {
   1.125 +      str += this.getString(aDataPairs[i]) + ": " + aDataPairs[i+1] + "<br/>";
   1.126 +    }
   1.127 +    return str + (aTrailingNewline ? "<br/>" : "");
   1.128 +  },
   1.129 +
   1.130 +  viewCert: function(aCtx, aCert) {
   1.131 +    let p = this.getPrompt(this.getString("certmgr.title"),
   1.132 +                    "",
   1.133 +                    [ this.getString("nssdialogs.ok.label") ])
   1.134 +    p.addLabel({ label: this.certInfoSection("certmgr.subjectinfo.label",
   1.135 +                          ["certmgr.certdetail.cn", aCert.commonName,
   1.136 +                           "certmgr.certdetail.o", aCert.organization,
   1.137 +                           "certmgr.certdetail.ou", aCert.organizationalUnit,
   1.138 +                           "certmgr.certdetail.serialnumber", aCert.serialNumber])})
   1.139 +     .addLabel({ label: this.certInfoSection("certmgr.issuerinfo.label",
   1.140 +                          ["certmgr.certdetail.cn", aCert.issuerCommonName,
   1.141 +                           "certmgr.certdetail.o", aCert.issuerOrganization,
   1.142 +                           "certmgr.certdetail.ou", aCert.issuerOrganizationUnit])})
   1.143 +     .addLabel({ label: this.certInfoSection("certmgr.periodofvalidity.label",
   1.144 +                          ["certmgr.begins", aCert.validity.notBeforeLocalDay,
   1.145 +                           "certmgr.expires", aCert.validity.notAfterLocalDay])})
   1.146 +     .addLabel({ label: this.certInfoSection("certmgr.fingerprints.label",
   1.147 +                          ["certmgr.certdetail.sha1fingerprint", aCert.sha1Fingerprint,
   1.148 +                           "certmgr.certdetail.md5fingerprint", aCert.md5Fingerprint], false) });
   1.149 +    this.showPrompt(p);
   1.150 +  },
   1.151 +
   1.152 +  viewCertDetails: function(details) {
   1.153 +    let p = this.getPrompt(this.getString("clientAuthAsk.message3"),
   1.154 +                    '',
   1.155 +                    [ this.getString("nssdialogs.ok.label") ]);
   1.156 +    p.addLabel({ label: details });
   1.157 +    this.showPrompt(p);
   1.158 +  },
   1.159 +
   1.160 +  ChooseCertificate: function(aCtx, cn, organization, issuer, certNickList, certDetailsList, count, selectedIndex, canceled) {
   1.161 +    let rememberSetting = true;
   1.162 +    var pref = Cc['@mozilla.org/preferences-service;1']
   1.163 +               .getService(Components.interfaces.nsIPrefService);
   1.164 +    if (pref) {
   1.165 +      pref = pref.getBranch(null);
   1.166 +      try {
   1.167 +        rememberSetting = pref.getBoolPref("security.remember_cert_checkbox_default_setting");
   1.168 +      } catch (e) {
   1.169 +        // pref is missing
   1.170 +      }
   1.171 +    }
   1.172 +
   1.173 +    let organizationString = this.formatString("clientAuthAsk.organization",
   1.174 +                                               [organization]);
   1.175 +    let issuerString = this.formatString("clientAuthAsk.issuer",
   1.176 +                                         [issuer]);
   1.177 +    let serverRequestedDetails = cn + '<br/>' + organizationString + '<br/>' + issuerString;
   1.178 +
   1.179 +    selectedIndex = 0;
   1.180 +    while (true) {
   1.181 +      let prompt = this.getPrompt(this.getString("clientAuthAsk.title"),
   1.182 +                                     this.getString("clientAuthAsk.message1"),
   1.183 +                                     [ this.getString("nssdialogs.ok.label"),
   1.184 +                                       this.getString("clientAuthAsk.viewCert.label"),
   1.185 +                                       this.getString("nssdialogs.cancel.label")
   1.186 +                                     ])
   1.187 +      .addLabel({ id: "requestedDetails", label: serverRequestedDetails } )
   1.188 +      .addMenulist({
   1.189 +        id: "nicknames",
   1.190 +        label: this.getString("clientAuthAsk.message2"),
   1.191 +        values: certNickList, selected: selectedIndex
   1.192 +      }).addCheckbox({
   1.193 +        id: "rememberBox",
   1.194 +        label: this.getString("clientAuthAsk.remember.label"),
   1.195 +        checked: rememberSetting
   1.196 +      });
   1.197 +      let response = this.showPrompt(prompt);
   1.198 +      selectedIndex = response.nicknames;
   1.199 +      if (response.button == 1) {
   1.200 +        this.viewCertDetails(certDetailsList[selectedIndex]);
   1.201 +        continue;
   1.202 +      } else if (response.button == 0) {
   1.203 +        canceled.value = false;
   1.204 +        if (response.rememberBox == "true") {
   1.205 +          aCtx.QueryInterface(Ci.nsIClientAuthUserDecision).rememberClientAuthCertificate = true;
   1.206 +        }
   1.207 +        return true;
   1.208 +      }
   1.209 +      canceled.value = true;
   1.210 +      return false;
   1.211 +    }
   1.212 +  }
   1.213 +};
   1.214 +
   1.215 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([NSSDialogs]);

mercurial