<?xml version="1.0"?>
<!-- 
   - The contents of this file are subject to the Mozilla Public
   - License Version 1.1 (the "License"); you may not use this file
   - except in compliance with the License. You may obtain a copy of
   - the License at http://www.mozilla.org/MPL/
   - 
   - Software distributed under the License is distributed on an "AS
   - IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
   - implied. See the License for the specific language governing
   - rights and limitations under the License.
   - 
   - The Original Code is mozilla.org code.
   - 
   - The Initial Developer of the Original Code is Netscape
   - Communications Corp.  Portions created by Netscape are
   - Copyright (C) 2001 Netscape Communications Corp.  All
   - Rights Reserved.
   - 
   - Contributor(s):
   -   Terry Hayes <thayes@netscape.com>
  -->

<!-- This file extends "chrome://navigator/content/pageInfo.xul" -->

<!DOCTYPE overlay SYSTEM "chrome://pippki/locale/PageInfoOverlay.dtd">

<overlay id="pipPageInfoOverlayID"
         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script src="chrome://global/content/strres.js" />
  <script src="chrome://pippki/content/pippki.js" />
  <script type="application/x-javascript">
  <![CDATA[
    var security = {
      // Display the server certificate (static)
      viewCert : function () {
        var cert = security._cert;
        if (cert) cert.view();
      },

      _getSecurityInfo : function() {
        const nsIX509Cert = Components.interfaces.nsIX509Cert;
        const nsIX509CertDB = Components.interfaces.nsIX509CertDB;
        const nsX509CertDB = "@mozilla.org/security/x509certdb;1";
        const nsISSLStatusProvider = Components.interfaces.nsISSLStatusProvider;

        // Get the window for this information
        var w;
        if ("arguments" in window && window.arguments.length > 1 && window.arguments[0])
          w = window.arguments[0];
        else
          w = window.opener.frames[0];

        var ui = security._getSecurityUI();
        var sp = ui.QueryInterface(nsISSLStatusProvider);
        var status = sp.SSLStatus;
        if (status) {
          var cert = status.serverCert;
          var issuerName;

          issuerName = this.mapIssuerOrganization(cert.issuerOrganization);
          if (!issuerName) issuerName = cert.issuerName;

          return {
            hostName : w.location.host,
            cAName : issuerName,
            encryptionAlgorithm : status.cipherName,
            encryptionStrength : status.secretKeyLength,
            cert : cert,
          };
        } else {
          return {
            hostName : w.location.host,
            cAName : "",
            encryptionAlgorithm : "",
            encryptionStrength : 0,
            cert : null
          };
        } 
      },

      // Find the secureBrowserUI object (if present)
      _getSecurityUI : function() {
        return window.opener.gBrowser.boxObject.getPropertyAsSupports("secureBrowserUI");
      },

      // Interface for mapping a certificate issuer organization to
      // the value to be displayed.
      // Bug 82017 - this implementation should be moved to pipnss C++ code
      mapIssuerOrganization: function(name) {
        if (!name) return null;

        if (name == "RSA Data Security, Inc.") return "Verisign, Inc.";

        // No mapping required
        return name;
      },

      _cert : null,
    };

    function securityOnLoad() {
      var bundle = srGetStrBundle("chrome://pippki/locale/pippki.properties");

      var info = security._getSecurityInfo();
      var idHdr;
      var message1;
      var message2;

      /* Set the identification messages */
      if (info.cert)
      {
        idHdr = bundle.GetStringFromName("pageInfo_WebSiteVerified");
        document.getElementById("security-identity").setAttribute("value", idHdr);

        message1 = bundle.formatStringFromName("pageInfo_Identity_Verified", 
                                [ info.hostName, info.cAName ],
                                2);
        setText("security-identity-text", message1);

        var viewText = bundle.GetStringFromName("pageInfo_ViewCertificate");
        setText("security-view-text", viewText);
        security._cert = info.cert;
      } else {
        idHdr = bundle.GetStringFromName("pageInfo_SiteNotVerified");
        document.getElementById("security-identity").setAttribute("value", idHdr);

        document.getElementById("security-view-cert").setAttribute("disabled", "true");
        document.getElementById("security-view-cert").setAttribute("hidden", "true");
      }

      /* Set the encryption messages */
      if (info.encryptionStrength >= 90) {
        var hdr = bundle.formatStringFromName("pageInfo_StrongEncryption",
                             [ info.encryptionAlgorithm, info.encryptionStrength+"" ], 2);
        document.getElementById("security-privacy").setAttribute("value", hdr);

        var msg1 = bundle.GetStringFromName("pageInfo_Privacy_Strong1");
        setText("security-privacy-msg1", msg1);

        var msg2 = bundle.GetStringFromName("pageInfo_Privacy_Strong2");
        setText("security-privacy-msg2", msg2);

        security._cert = info.cert;
      } else if (info.encryptionStrength > 0) {
        var hdr = bundle.formatStringFromName("pageInfo_WeakEncryption",
                             [ info.encryptionAlgorithm, info.encryptionStrength+"" ], 2);
        document.getElementById("security-privacy").setAttribute("value", hdr);

        var msg1 = bundle.formatStringFromName("pageInfo_Privacy_Weak1",
                                               [ info.hostName ], 1);
        setText("security-privacy-msg1", msg1);

        var msg2 = bundle.GetStringFromName("pageInfo_Privacy_Weak2");
        setText("security-privacy-msg2", msg2);
      } else {
        var hdr = bundle.GetStringFromName("pageInfo_NoEncryption");
        document.getElementById("security-privacy").setAttribute("value", hdr);

        var msg1 = bundle.formatStringFromName("pageInfo_Privacy_None1",
                                               [ info.hostName ], 1);
        setText("security-privacy-msg1", msg1);

        var msg2 = bundle.GetStringFromName("pageInfo_Privacy_None2");
        setText("security-privacy-msg2", msg2);
      }
    }

    /* Register for pageInfo onload calls */
    onLoadRegistry.push(securityOnLoad);
  ]]>
  </script>
  <tabs id="tabs">
    <tab id="securityTab" label="&pageInfo.securityTab;" />
  </tabs>
  <tabpanels id="tabpanels">
    <vbox id="securityPanel" flex="1">
      <text id="security-identity" class="header label" />
      <html id="security-identity-text" flex="1" />
      <hbox>
        <button id="security-view-cert" class="normal" label="&pageInfo.view.label;" 
          oncommand="security.viewCert();" />
        <html id="security-view-text" flex="1" />
      </hbox>
      <separator class="groove" orient="horizontal" />
      <text id="security-privacy" class="header label" />
      <vbox flex="1" >
        <html id="security-privacy-msg1" />
        <html id="security-privacy-msg2" />
      </vbox>
    </vbox>
  </tabpanels>
</overlay>
