All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ldaptive.ssl.DefaultSSLContextInitializer Maven / Gradle / Ivy

There is a newer version: 2.3.2
Show newest version
/*
  $Id: DefaultSSLContextInitializer.java 3121 2015-10-02 20:24:35Z daniel_fisher $

  Copyright (C) 2003-2014 Virginia Tech.
  All rights reserved.

  SEE LICENSE FOR MORE INFORMATION

  Author:  Middleware Services
  Email:   [email protected]
  Version: $Revision: 3121 $
  Updated: $Date: 2015-10-02 16:24:35 -0400 (Fri, 02 Oct 2015) $
*/
package org.ldaptive.ssl;

import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.Arrays;
import javax.net.ssl.KeyManager;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

/**
 * Provides a default implementation of SSL context initializer which allows the
 * setting of trust and key managers in order to create an SSL context.
 *
 * @author  Middleware Services
 * @version  $Revision: 3121 $ $Date: 2015-10-02 16:24:35 -0400 (Fri, 02 Oct 2015) $
 */
public class DefaultSSLContextInitializer extends AbstractSSLContextInitializer
{

  /** Key managers. */
  private KeyManager[] keyManagers;

  /** Whether default trust managers should be created. */
  private final boolean createDefaultTrustManagers;


  /**
   * Creates a new default ssl context initializer. Default trust managers will
   * be produced.
   */
  public DefaultSSLContextInitializer()
  {
    this(true);
  }


  /**
   * Creates a new default ssl context initializer.
   *
   * @param  defaultTrustManagers  whether default trust managers should be
   * created
   */
  public DefaultSSLContextInitializer(final boolean defaultTrustManagers)
  {
    createDefaultTrustManagers = defaultTrustManagers;
  }


  /** {@inheritDoc} */
  @Override
  protected TrustManager[] createTrustManagers()
    throws GeneralSecurityException
  {
    if (createDefaultTrustManagers) {
      final TrustManagerFactory tmf = TrustManagerFactory.getInstance(
        TrustManagerFactory.getDefaultAlgorithm());
      tmf.init((KeyStore) null);
      return tmf.getTrustManagers();
    }
    return null;
  }


  /** {@inheritDoc} */
  @Override
  public KeyManager[] getKeyManagers()
    throws GeneralSecurityException
  {
    return keyManagers;
  }


  /**
   * Sets the key managers.
   *
   * @param  managers  key managers
   */
  public void setKeyManagers(final KeyManager... managers)
  {
    keyManagers = managers;
  }


  @Override
  public String toString()
  {
    return
      String.format(
        "[%s@%d::trustManagers=%s, keyManagers=%s, " +
        "createDefaultTrustManagers=%s]",
        getClass().getName(),
        hashCode(),
        Arrays.toString(trustManagers),
        Arrays.toString(keyManagers),
        createDefaultTrustManagers);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy