Android Open Source - dcchat Naive Trust Provider






From Project

Back to project page dcchat.

License

The source code is released under:

Apache License

If you think the Android project dcchat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.kaleidos.dcchat;
import java.security.*;
import java.security.cert.*;
import javax.net.ssl.*;
//  w  ww . jav  a  2 s.  co  m
/**
 * Provides all secure socket factories, with a socket that ignores
 * problems in the chain of certificate trust. This is good for embedded
 * applications that just want the encryption aspect of SSL communication,
 * without worrying too much about validating the identify of the server at the
 * other end of the connection. In other words, this may leave you vulnerable
 * to a man-in-the-middle attack.
 */

public final class NaiveTrustProvider extends Provider
{
  /** The name of our algorithm **/
  private static final String TRUST_PROVIDER_ALG = "NaiveTrustAlgorithm";

  /** Need to refer to ourselves somehow to know if we're already registered **/
  private static final String TRUST_PROVIDER_ID  = "NaiveTrustProvider";

  /**
   * Hook in at the provider level to handle libraries and 3rd party 
   * utilities that use their own factory. Requires permission to 
   * execute AccessController.doPrivileged,
   * so this probably won't work in applets or other high-security jvms
   **/

  public NaiveTrustProvider ()
  {
    super ( TRUST_PROVIDER_ID,
            (double) 0.1,
            "NaiveTrustProvider (provides all secure socket factories by ignoring problems in the chain of certificate trust)" );

    AccessController.doPrivileged ( new PrivilegedAction() {
      public Object run()
      {
        put ("TrustManagerFactory." + NaiveTrustManagerFactory.getAlgorithm(),
             NaiveTrustManagerFactory.class.getName() );
        return null;
      }
    });
  }

  /**
   * This is the only method the client code need to call. Yup, just put
   * NaiveTrustProvider.setAlwaysTrust() into your initialization code
   * and you're good to go
   * 
   * @param enableNaiveTrustProvider set to true to always trust (set to false
   *          it not yet implemented)
   **/

  public static void setAlwaysTrust (boolean enableNaiveTrustProvider)
  {
    if (enableNaiveTrustProvider) {
      Provider registered = Security.getProvider (TRUST_PROVIDER_ID);
      if (null == registered) {
        Security.insertProviderAt (new NaiveTrustProvider (), 1);
        Security.setProperty ("ssl.TrustManagerFactory.algorithm",
                              TRUST_PROVIDER_ALG);
      }
    } else {
      throw new UnsupportedOperationException (
          "Disable Naive trust provider not yet implemented");
    }
  }

  /**
   * The factory for the NaiveTrustProvider
   **/
  public final static class NaiveTrustManagerFactory 
         extends TrustManagerFactorySpi
  {
    public NaiveTrustManagerFactory () { }

    protected void engineInit (ManagerFactoryParameters mgrparams)
    {
    }

    protected void engineInit (KeyStore keystore)
    {
    }

    /**
     * Returns a collection of trust managers that are naive. 
     * This collection is just a single element array containing
     * our {@link NaiveTrustManager} class.
     **/
    protected TrustManager[] engineGetTrustManagers ()
    {
      // Returns a new array of just a single NaiveTrustManager.
      return new TrustManager[] { new NaiveTrustManager() } ; 
    }

    /**
     * Returns our "NaiveTrustAlgorithm" string.
     * @return The string, "NaiveTrustAlgorithm"
     */
    public static String getAlgorithm()
    {
      return TRUST_PROVIDER_ALG;
    }
  }
}




Java Source Code List

interfaces.NotificationListener.java
net.kaleidos.dcchat.Base32.java
net.kaleidos.dcchat.ChatActivity.java
net.kaleidos.dcchat.DCChat.java
net.kaleidos.dcchat.Data.java
net.kaleidos.dcchat.Main.java
net.kaleidos.dcchat.Message.java
net.kaleidos.dcchat.NaiveTrustManager.java
net.kaleidos.dcchat.NaiveTrustProvider.java
net.kaleidos.dcchat.NotificationListenerImp.java
net.kaleidos.dcchat.ServersActivity.java
net.kaleidos.dcchat.async.DcChatAsyncTask.java
net.kaleidos.dcchat.async.SendMessageAsyncTask.java
net.kaleidos.dcchat.listener.DccNotificationListener.java
net.kaleidos.dcchat.listener.Messageable.java