예제 #1
0
  /**
   * Creates trust managers using the receiver's trust store configuration.
   *
   * @param context context for status messages
   * @return an array of trust managers or {@code null} if no trust store configuration was provided
   * @throws NoSuchProviderException if a provider specified for one of the trust manager components
   *     is not known to the platform
   * @throws NoSuchAlgorithmException if an algorithm specified for one of the trust manager
   *     components is not known to the relevant provider
   * @throws KeyStoreException if an error occurs in reading a key store containing trust anchors
   */
  private TrustManager[] createTrustManagers(ContextAware context)
      throws NoSuchProviderException, NoSuchAlgorithmException, KeyStoreException {

    if (getTrustStore() == null) return null;

    KeyStore trustStore = getTrustStore().createKeyStore();
    context.addInfo(
        "trust store of type '"
            + trustStore.getType()
            + "' provider '"
            + trustStore.getProvider()
            + "': "
            + getTrustStore().getLocation());

    TrustManagerFactory tmf = getTrustManagerFactory().createTrustManagerFactory();
    context.addInfo(
        "trust manager algorithm '"
            + tmf.getAlgorithm()
            + "' provider '"
            + tmf.getProvider()
            + "'");

    tmf.init(trustStore);
    return tmf.getTrustManagers();
  }
예제 #2
0
 public static void setSystemProperty(ContextAware contextAware, String key, String value) {
   try {
     System.setProperty(key, value);
   } catch (SecurityException e) {
     contextAware.addError("Failed to set system property [" + key + "]", e);
   }
 }
  @Override
  public void begin(InterpretationContext ec, String name, Attributes attributes)
      throws ActionException {

    inError = false;

    String className = attributes.getValue(CLASS_ATTRIBUTE);
    if (OptionHelper.isEmpty(className)) {
      addError(
          "Mandatory \""
              + CLASS_ATTRIBUTE
              + "\" attribute not set for <loggerContextListener> element");
      inError = true;
      return;
    }

    try {
      lcl =
          (LoggerContextListener)
              OptionHelper.instantiateByClassName(className, LoggerContextListener.class, context);

      if (lcl instanceof ContextAware) {
        ((ContextAware) lcl).setContext(context);
      }

      ec.pushObject(lcl);
      addInfo("Adding LoggerContextListener of type [" + className + "] to the object stack");

    } catch (Exception oops) {
      inError = true;
      addError("Could not create LoggerContextListener of type " + className + "].", oops);
    }
  }
예제 #4
0
  public void begin(InterpretationContext ec, String name, Attributes attributes)
      throws ActionException {
    inError = false;
    String className = attributes.getValue(CLASS_ATTRIBUTE);
    if (OptionHelper.isEmpty(className)) {
      addError(
          "Missing class name for statusListener. Near [" + name + "] line " + getLineNumber(ec));
      inError = true;
      return;
    }

    try {
      statusListener =
          (StatusListener)
              OptionHelper.instantiateByClassName(className, StatusListener.class, context);
      addInfo("Adding status listener of type [" + className + "]");
      ec.getContext().getStatusManager().add(statusListener);
      if (statusListener instanceof ContextAware) {
        ((ContextAware) statusListener).setContext(context);
      }
      ec.pushObject(statusListener);
    } catch (Exception e) {
      inError = true;
      addError("Could not create an StatusListener of type [" + className + "].", e);
      throw new ActionException(e);
    }
  }
예제 #5
0
  private SecureRandom createSecureRandom(ContextAware context)
      throws NoSuchProviderException, NoSuchAlgorithmException {

    SecureRandom secureRandom = getSecureRandom().createSecureRandom();
    context.addInfo(
        "secure random algorithm '"
            + secureRandom.getAlgorithm()
            + "' provider '"
            + secureRandom.getProvider()
            + "'");

    return secureRandom;
  }
예제 #6
0
  /**
   * Creates key managers using the receiver's key store configuration.
   *
   * @param context context for status messages
   * @return an array of key managers or {@code null} if no key store configuration was provided
   * @throws NoSuchProviderException if a provider specified for one of the key manager components
   *     is not known to the platform
   * @throws NoSuchAlgorithmException if an algorithm specified for one of the key manager
   *     components is not known to the relevant provider
   * @throws KeyStoreException if an error occurs in reading a key store
   */
  private KeyManager[] createKeyManagers(ContextAware context)
      throws NoSuchProviderException, NoSuchAlgorithmException, UnrecoverableKeyException,
          KeyStoreException {

    if (getKeyStore() == null) return null;

    KeyStore keyStore = getKeyStore().createKeyStore();
    context.addInfo(
        "key store of type '"
            + keyStore.getType()
            + "' provider '"
            + keyStore.getProvider()
            + "': "
            + getKeyStore().getLocation());

    KeyManagerFactory kmf = getKeyManagerFactory().createKeyManagerFactory();
    context.addInfo(
        "key manager algorithm '" + kmf.getAlgorithm() + "' provider '" + kmf.getProvider() + "'");

    char[] passphrase = getKeyStore().getPassword().toCharArray();
    kmf.init(keyStore, passphrase);
    return kmf.getKeyManagers();
  }
예제 #7
0
  /**
   * Creates a new {@link SSLContext} using the receiver's configuration.
   *
   * @param context context for status messages
   * @return {@link SSLContext} object
   * @throws NoSuchProviderException if a provider specified for one of the JCA or JSSE components
   *     utilized in creating the context is not known to the platform
   * @throws NoSuchAlgorithmException if a JCA or JSSE algorithm, protocol, or type name specified
   *     for one of the context's components is not known to a given provider (or platform default
   *     provider for the component)
   * @throws KeyManagementException if an error occurs in creating a {@link KeyManager} for the
   *     context
   * @throws UnrecoverableKeyException if a private key needed by a {@link KeyManager} cannot be
   *     obtained from a key store
   * @throws KeyStoreException if an error occurs in reading the contents of a key store
   * @throws CertificateException if an error occurs in reading the contents of a certificate
   */
  public SSLContext createContext(ContextAware context)
      throws NoSuchProviderException, NoSuchAlgorithmException, KeyManagementException,
          UnrecoverableKeyException, KeyStoreException, CertificateException {

    SSLContext sslContext =
        getProvider() != null
            ? SSLContext.getInstance(getProtocol(), getProvider())
            : SSLContext.getInstance(getProtocol());

    context.addInfo(
        "SSL protocol '"
            + sslContext.getProtocol()
            + "' provider '"
            + sslContext.getProvider()
            + "'");

    KeyManager[] keyManagers = createKeyManagers(context);
    TrustManager[] trustManagers = createTrustManagers(context);
    SecureRandom secureRandom = createSecureRandom(context);
    sslContext.init(keyManagers, trustManagers, secureRandom);
    return sslContext;
  }