home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / firefox-3.0.14 / chrome / browser.jar / content / browser / pageinfo / security.js < prev   
Encoding:
JavaScript  |  2008-04-02  |  11.3 KB  |  343 lines

  1. //@line 40 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/base/content/pageinfo/security.js"
  2.  
  3. var security = {
  4.   // Display the server certificate (static)
  5.   viewCert : function () {
  6.     var cert = security._cert;
  7.     viewCertHelper(window, cert);
  8.   },
  9.  
  10.   _getSecurityInfo : function() {
  11.     const nsIX509Cert = Components.interfaces.nsIX509Cert;
  12.     const nsIX509CertDB = Components.interfaces.nsIX509CertDB;
  13.     const nsX509CertDB = "@mozilla.org/security/x509certdb;1";
  14.     const nsISSLStatusProvider = Components.interfaces.nsISSLStatusProvider;
  15.     const nsISSLStatus = Components.interfaces.nsISSLStatus;
  16.  
  17.     // We don't have separate info for a frame, return null until further notice
  18.     // (see bug 138479)
  19.     if (gWindow != gWindow.top)
  20.       return null;
  21.  
  22.     var hName = null;
  23.     try {
  24.       hName = gWindow.location.host;
  25.     }
  26.     catch (exception) { }
  27.  
  28.     var ui = security._getSecurityUI();
  29.     if (!ui)
  30.       return null;
  31.  
  32.     var isBroken =
  33.       (ui.state & Components.interfaces.nsIWebProgressListener.STATE_IS_BROKEN);
  34.     var isInsecure = 
  35.       (ui.state & Components.interfaces.nsIWebProgressListener.STATE_IS_INSECURE);
  36.     var isEV =
  37.       (ui.state & Components.interfaces.nsIWebProgressListener.STATE_IDENTITY_EV_TOPLEVEL);
  38.     ui.QueryInterface(nsISSLStatusProvider);
  39.     var status = ui.SSLStatus;
  40.  
  41.     if (!isInsecure && status) {
  42.       status.QueryInterface(nsISSLStatus);
  43.       var cert = status.serverCert;
  44.       var issuerName =
  45.         this.mapIssuerOrganization(cert.issuerOrganization) || cert.issuerName;
  46.  
  47.       var retval = {
  48.         hostName : hName,
  49.         cAName : issuerName,
  50.         encryptionAlgorithm : undefined,
  51.         encryptionStrength : undefined,
  52.         isBroken : isBroken,
  53.         isEV : isEV,
  54.         cert : cert,
  55.         fullLocation : gWindow.location
  56.       };
  57.  
  58.       try {
  59.         retval.encryptionAlgorithm = status.cipherName;
  60.         retval.encryptionStrength = status.secretKeyLength;
  61.       }
  62.       catch (e) {
  63.       }
  64.  
  65.       return retval;
  66.     } else {
  67.       return {
  68.         hostName : hName,
  69.         cAName : "",
  70.         encryptionAlgorithm : "",
  71.         encryptionStrength : 0,
  72.         isBroken : isBroken,
  73.         isEV : isEV,
  74.         cert : null,
  75.         fullLocation : gWindow.location        
  76.       };
  77.     }
  78.   },
  79.  
  80.   // Find the secureBrowserUI object (if present)
  81.   _getSecurityUI : function() {
  82.     if (window.opener.gBrowser)
  83.       return window.opener.gBrowser.securityUI;
  84.     return null;
  85.   },
  86.  
  87.   // Interface for mapping a certificate issuer organization to
  88.   // the value to be displayed.
  89.   // Bug 82017 - this implementation should be moved to pipnss C++ code
  90.   mapIssuerOrganization: function(name) {
  91.     if (!name) return null;
  92.  
  93.     if (name == "RSA Data Security, Inc.") return "Verisign, Inc.";
  94.  
  95.     // No mapping required
  96.     return name;
  97.   },
  98.   
  99.   /**
  100.    * Open the cookie manager window
  101.    */
  102.   viewCookies : function()
  103.   {
  104.     var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  105.                        .getService(Components.interfaces.nsIWindowMediator);
  106.     var win = wm.getMostRecentWindow("Browser:Cookies");
  107.     var eTLDService = Components.classes["@mozilla.org/network/effective-tld-service;1"].
  108.                       getService(Components.interfaces.nsIEffectiveTLDService);
  109.  
  110.     var eTLD;
  111.     var uri = gDocument.documentURIObject;
  112.     try {
  113.       eTLD = eTLDService.getBaseDomain(uri);
  114.     }
  115.     catch (e) {
  116.       // getBaseDomain will fail if the host is an IP address or is empty
  117.       eTLD = uri.asciiHost;
  118.     }
  119.  
  120.     if (win) {
  121.       win.gCookiesWindow.setFilter(eTLD);
  122.       win.focus();
  123.     }
  124.     else
  125.       window.openDialog("chrome://browser/content/preferences/cookies.xul",
  126.                         "Browser:Cookies", "", {filterString : eTLD});
  127.   },
  128.   
  129.   /**
  130.    * Open the login manager window
  131.    */
  132.   viewPasswords : function()
  133.   {
  134.     var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  135.                        .getService(Components.interfaces.nsIWindowMediator);
  136.     var win = wm.getMostRecentWindow("Toolkit:PasswordManager");
  137.     if (win) {
  138.       win.setFilter(this._getSecurityInfo().hostName);
  139.       win.focus();
  140.     }
  141.     else
  142.       window.openDialog("chrome://passwordmgr/content/passwordManager.xul",
  143.                         "Toolkit:PasswordManager", "", 
  144.                         {filterString : this._getSecurityInfo().hostName});
  145.   },
  146.  
  147.   _cert : null
  148. };
  149.  
  150. function securityOnLoad() {
  151.   var bundle = srGetStrBundle("chrome://pippki/locale/pippki.properties");
  152.   var pageInfoBundle = document.getElementById("pageinfobundle");
  153.  
  154.   var info = security._getSecurityInfo();
  155.   if (!info) {
  156.     document.getElementById("securityTab").hidden = true;
  157.     document.getElementById("securityBox").collapsed = true;
  158.     return;
  159.   }
  160.   else {
  161.     document.getElementById("securityTab").hidden = false;
  162.     document.getElementById("securityBox").collapsed = false;
  163.   }
  164.  
  165.   /* Set Identity section text */
  166.   setText("security-identity-domain-value", info.hostName);
  167.   
  168.   var owner, verifier, generalPageIdentityString;
  169.   if (info.cert && !info.isBroken) {
  170.     // Try to pull out meaningful values.  Technically these fields are optional
  171.     // so we'll employ fallbacks where appropriate.  The EV spec states that Org
  172.     // fields must be specified for subject and issuer so that case is simpler.
  173.     if (info.isEV) {
  174.       owner = info.cert.organization;
  175.       verifier = security.mapIssuerOrganization(info.cAName);
  176.       generalPageIdentityString = pageInfoBundle.getFormattedString("generalSiteIdentity",
  177.                                                                     [owner, verifier]);
  178.     }
  179.     else {
  180.       // Technically, a non-EV cert might specify an owner in the O field or not,
  181.       // depending on the CA's issuing policies.  However we don't have any programmatic
  182.       // way to tell those apart, and no policy way to establish which organization
  183.       // vetting standards are good enough (that's what EV is for) so we default to
  184.       // treating these certs as domain-validated only.
  185.       owner = pageInfoBundle.getString("securityNoIdentity");
  186.       verifier = security.mapIssuerOrganization(info.cAName ||
  187.                                                 info.cert.issuerCommonName ||
  188.                                                 info.cert.issuerName);
  189.       generalPageIdentityString = owner;
  190.     }
  191.   }
  192.   else {
  193.     // We don't have valid identity credentials.
  194.     owner = pageInfoBundle.getString("securityNoIdentity");
  195.     verifier = pageInfoBundle.getString("notset");
  196.     generalPageIdentityString = owner;
  197.   }
  198.  
  199.   setText("security-identity-owner-value", owner);
  200.   setText("security-identity-verifier-value", verifier);
  201.   setText("general-security-identity", generalPageIdentityString);
  202.  
  203.   /* Manage the View Cert button*/
  204.   var viewCert = document.getElementById("security-view-cert");
  205.   if (info.cert) {
  206.     var viewText = pageInfoBundle.getString("securityCertText");
  207.     setText("security-view-text", viewText);
  208.     security._cert = info.cert;
  209.     viewCert.collapsed = false;
  210.   }
  211.   else
  212.     viewCert.collapsed = true;
  213.  
  214.   /* Set Privacy & History section text */
  215.   var yesStr = pageInfoBundle.getString("yes");
  216.   var noStr = pageInfoBundle.getString("no");
  217.  
  218.   var uri = gDocument.documentURIObject;
  219.   setText("security-privacy-cookies-value",
  220.           hostHasCookies(uri) ? yesStr : noStr);
  221.   setText("security-privacy-passwords-value",
  222.           realmHasPasswords(uri) ? yesStr : noStr);
  223.   
  224.   var visitCount = previousVisitCount(info.hostName);
  225.   if(visitCount > 1) {
  226.     setText("security-privacy-history-value",
  227.             pageInfoBundle.getFormattedString("securityNVisits", [visitCount.toLocaleString()]));
  228.   }
  229.   else if (visitCount == 1) {
  230.     setText("security-privacy-history-value",
  231.             pageInfoBundle.getString("securityOneVisit"));
  232.   }
  233.   else {
  234.     setText("security-privacy-history-value", noStr);        
  235.   }
  236.  
  237.   /* Set the Technical Detail section messages */
  238.   var hdr;
  239.   var msg1;
  240.   var msg2;
  241.  
  242.   if (info.isBroken) {
  243.     hdr = bundle.GetStringFromName("pageInfo_MixedContent");
  244.     msg1 = bundle.GetStringFromName("pageInfo_Privacy_Mixed1");
  245.     msg2 = bundle.GetStringFromName("pageInfo_Privacy_None2");
  246.   }
  247.   else if (info.encryptionStrength >= 90) {
  248.     hdr = bundle.formatStringFromName("pageInfo_StrongEncryption",
  249.                           [ info.encryptionAlgorithm, info.encryptionStrength + "" ], 2);
  250.     msg1 = bundle.GetStringFromName("pageInfo_Privacy_Strong1");
  251.     msg2 = bundle.GetStringFromName("pageInfo_Privacy_Strong2");
  252.     security._cert = info.cert;
  253.   }
  254.   else if (info.encryptionStrength > 0) {
  255.     hdr  = bundle.formatStringFromName("pageInfo_WeakEncryption",
  256.                           [ info.encryptionAlgorithm, info.encryptionStrength + "" ], 2);
  257.     msg1 = bundle.formatStringFromName("pageInfo_Privacy_Weak1", [ info.hostName ], 1);
  258.     msg2 = bundle.GetStringFromName("pageInfo_Privacy_Weak2");
  259.   }
  260.   else {
  261.     hdr = bundle.GetStringFromName("pageInfo_NoEncryption");
  262.     if (info.hostName != null)
  263.       msg1 = bundle.formatStringFromName("pageInfo_Privacy_None1", [ info.hostName ], 1);
  264.     else
  265.       msg1 = bundle.GetStringFromName("pageInfo_Privacy_None3");
  266.     msg2 = bundle.GetStringFromName("pageInfo_Privacy_None2");
  267.   }
  268.   setText("security-technical-shortform", hdr);
  269.   setText("security-technical-longform1", msg1);
  270.   setText("security-technical-longform2", msg2); 
  271.   setText("general-security-privacy", hdr);
  272. }
  273.  
  274. function setText(id, value)
  275. {
  276.   var element = document.getElementById(id);
  277.   if (!element)
  278.     return;
  279.   if (element.localName == "textbox" || element.localName == "label")
  280.     element.value = value;
  281.   else {
  282.     if (element.hasChildNodes())
  283.       element.removeChild(element.firstChild);
  284.     var textNode = document.createTextNode(value);
  285.     element.appendChild(textNode);
  286.   }
  287. }
  288.  
  289. function viewCertHelper(parent, cert)
  290. {
  291.   if (!cert)
  292.     return;
  293.  
  294.   var cd = Components.classes[CERTIFICATEDIALOGS_CONTRACTID].getService(nsICertificateDialogs);
  295.   cd.viewCert(parent, cert);
  296. }
  297.  
  298. /**
  299.  * Return true iff we have cookies for uri
  300.  */
  301. function hostHasCookies(uri) {
  302.   var cookieManager = Components.classes["@mozilla.org/cookiemanager;1"]
  303.                                 .getService(Components.interfaces.nsICookieManager2);
  304.  
  305.   return cookieManager.countCookiesFromHost(uri.asciiHost) > 0;
  306. }
  307.  
  308. /**
  309.  * Return true iff realm (proto://host:port) (extracted from uri) has
  310.  * saved passwords
  311.  */
  312. function realmHasPasswords(uri) {
  313.   var passwordManager = Components.classes["@mozilla.org/login-manager;1"]
  314.                                   .getService(Components.interfaces.nsILoginManager);
  315.   return passwordManager.countLogins(uri.prePath, "", "") > 0;
  316. }
  317.  
  318. /**
  319.  * Return the number of previous visits recorded for host before today.
  320.  *
  321.  * @param host - the domain name to look for in history
  322.  */
  323. function previousVisitCount(host, endTimeReference) {
  324.   if (!host)
  325.     return false;
  326.   
  327.   var historyService = Components.classes["@mozilla.org/browser/nav-history-service;1"]
  328.                                  .getService(Components.interfaces.nsINavHistoryService);
  329.     
  330.   var options = historyService.getNewQueryOptions();
  331.   options.resultType = options.RESULTS_AS_VISIT;
  332.   
  333.   // Search for visits to this host before today
  334.   var query = historyService.getNewQuery();
  335.   query.endTimeReference = query.TIME_RELATIVE_TODAY;
  336.   query.endTime = 0;
  337.   query.domain = host;
  338.  
  339.   var result = historyService.executeQuery(query, options);
  340.   result.root.containerOpen = true;
  341.   return result.root.childCount;
  342. }
  343.