browser/metro/base/content/exceptions.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/metro/base/content/exceptions.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,115 @@
     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 +let Cc = Components.classes;
     1.9 +let Ci = Components.interfaces;
    1.10 +let Cu = Components.utils;
    1.11 +
    1.12 +Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
    1.13 +
    1.14 +/**
    1.15 +  A class to add exceptions to override SSL certificate problems. The functionality
    1.16 +  itself is borrowed from exceptionDialog.js.
    1.17 +*/
    1.18 +function SSLExceptions() {
    1.19 +  this._overrideService = Cc["@mozilla.org/security/certoverride;1"]
    1.20 +                          .getService(Ci.nsICertOverrideService);
    1.21 +}
    1.22 +
    1.23 +
    1.24 +SSLExceptions.prototype = {
    1.25 +  _overrideService: null,
    1.26 +  _sslStatus: null,
    1.27 +
    1.28 +  getInterface: function SSLE_getInterface(aIID) {
    1.29 +    return this.QueryInterface(aIID);
    1.30 +  },
    1.31 +  QueryInterface: function SSLE_QueryInterface(aIID) {
    1.32 +    if (aIID.equals(Ci.nsIBadCertListener2) ||
    1.33 +        aIID.equals(Ci.nsISupports))
    1.34 +      return this;
    1.35 +
    1.36 +    throw Components.results.NS_ERROR_NO_INTERFACE;
    1.37 +  },
    1.38 +
    1.39 +  /**
    1.40 +    To collect the SSL status we intercept the certificate error here
    1.41 +    and store the status for later use.
    1.42 +  */
    1.43 +  notifyCertProblem: function SSLE_notifyCertProblem(socketInfo, sslStatus, targetHost) {
    1.44 +    this._sslStatus = sslStatus.QueryInterface(Ci.nsISSLStatus);
    1.45 +    return true; // suppress error UI
    1.46 +  },
    1.47 +
    1.48 +  /**
    1.49 +    Attempt to download the certificate for the location specified to get the SSLState
    1.50 +    for the certificate and the errors.
    1.51 +   */
    1.52 +  _checkCert: function SSLE_checkCert(aURI) {
    1.53 +    this._sslStatus = null;
    1.54 +  
    1.55 +    var req = new XMLHttpRequest();
    1.56 +    try {
    1.57 +      if(aURI) {
    1.58 +        req.open("GET", aURI.prePath, false);
    1.59 +        req.channel.notificationCallbacks = this;
    1.60 +        req.send(null);
    1.61 +      }
    1.62 +    } catch (e) {
    1.63 +      // We *expect* exceptions if there are problems with the certificate
    1.64 +      // presented by the site.  Log it, just in case, but we can proceed here,
    1.65 +      // with appropriate sanity checks
    1.66 +      Components.utils.reportError("Attempted to connect to a site with a bad certificate in the add exception dialog. " +
    1.67 +                                   "This results in a (mostly harmless) exception being thrown. " +
    1.68 +                                   "Logged for information purposes only: " + e);
    1.69 +    }
    1.70 +
    1.71 +    return this._sslStatus;
    1.72 +  },
    1.73 +
    1.74 +  /**
    1.75 +    Internal method to create an override.
    1.76 +  */
    1.77 +  _addOverride: function SSLE_addOverride(aURI, aWindow, temporary) {
    1.78 +    var SSLStatus = this._checkCert(aURI);
    1.79 +    var certificate = SSLStatus.serverCert;
    1.80 +
    1.81 +    var flags = 0;
    1.82 +
    1.83 +    // in private browsing do not store exceptions permanently ever
    1.84 +    if (PrivateBrowsingUtils.isWindowPrivate(aWindow)) {
    1.85 +      temporary = true;
    1.86 +    }
    1.87 +
    1.88 +    if(SSLStatus.isUntrusted)
    1.89 +      flags |= this._overrideService.ERROR_UNTRUSTED;
    1.90 +    if(SSLStatus.isDomainMismatch)
    1.91 +      flags |= this._overrideService.ERROR_MISMATCH;
    1.92 +    if(SSLStatus.isNotValidAtThisTime)
    1.93 +      flags |= this._overrideService.ERROR_TIME;
    1.94 +
    1.95 +    this._overrideService.rememberValidityOverride(
    1.96 +      aURI.asciiHost,
    1.97 +      aURI.port,
    1.98 +      certificate,
    1.99 +      flags,
   1.100 +      temporary);
   1.101 +  },
   1.102 +
   1.103 +  /**
   1.104 +    Creates a permanent exception to override all overridable errors for
   1.105 +    the given URL.
   1.106 +  */
   1.107 +  addPermanentException: function SSLE_addPermanentException(aURI, aWindow) {
   1.108 +    this._addOverride(aURI, aWindow, false);
   1.109 +  },
   1.110 +
   1.111 +  /**
   1.112 +    Creates a temporary exception to override all overridable errors for
   1.113 +    the given URL.
   1.114 +  */
   1.115 +  addTemporaryException: function SSLE_addTemporaryException(aURI, aWindow) {
   1.116 +    this._addOverride(aURI, aWindow, true);
   1.117 +  }
   1.118 +};

mercurial