<commit_before>/**
 * Copyright (c) <NAME>, 2006-2012.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.italiangrid.voms.util;

import org.italiangrid.voms.ac.impl.DefaultVOMSValidator;

import eu.emi.security.authn.x509.CrlCheckingMode;
import eu.emi.security.authn.x509.NamespaceCheckingMode;
import eu.emi.security.authn.x509.OCSPCheckingMode;
import eu.emi.security.authn.x509.OCSPParametes;
import eu.emi.security.authn.x509.ProxySupport;
import eu.emi.security.authn.x509.ValidationErrorListener;
import eu.emi.security.authn.x509.helpers.pkipath.AbstractValidator;
import eu.emi.security.authn.x509.impl.CRLParameters;
import eu.emi.security.authn.x509.impl.OpensslCertChainValidator;
import eu.emi.security.authn.x509.impl.RevocationParametersExt;
import eu.emi.security.authn.x509.impl.ValidatorParamsExt;

/**
 * A utility class which provides convenient methods to build a certificate
 * validator with defaults that are meaningful for VOMS usage.
 * 
 * @author cecco
 * 
 */
public class CertificateValidatorBuilder {

	/**
	 * The default CRL checking policy.
	 */
	public static final CrlCheckingMode DEFAULT_CRL_CHECKS = CrlCheckingMode.REQUIRE;
	/**
	 * The default OCSP checking policy.
	 */
	public static final OCSPCheckingMode DEFAULT_OCSP_CHECKS = OCSPCheckingMode.IGNORE;
	/**
	 * The default namespace checking policy.
	 */
	public static final NamespaceCheckingMode DEFAULT_NS_CHECKS = NamespaceCheckingMode.GLOBUS_EUGRIDPMA;

	/** Private constructor which prevents object instantiation **/
	private CertificateValidatorBuilder() {
	}

	/**
	 * Builds an Openssl-style certificate validator configured as specified in
	 * the parameters
	 * 
	 * @param trustAnchorsDir
	 *            the directory where trust anchors are loaded from
	 * @param validationErrorListener
	 *            the listener that will receive notification about validation
	 *            errors
	 * @param updateInterval
	 *            the trust anchor store update interval
	 * @param namespaceChecks
	 *            the namespace checking policy
	 * @param crlChecks
	 *            the crl checking policy
	 * @param ocspChecks
	 *            the ocsp checking policy
	 * @return
	 */
	public static AbstractValidator buildCertificateValidator(String trustAnchorsDir,
			ValidationErrorListener validationErrorListener, long updateInterval,
			NamespaceCheckingMode namespaceChecks, CrlCheckingMode crlChecks,
			OCSPCheckingMode ocspChecks) {

		RevocationParametersExt revocationParameters = new RevocationParametersExt(crlChecks,
				new CRLParameters(), new OCSPParametes(ocspChecks));

		ValidatorParamsExt validationParams = new ValidatorParamsExt(revocationParameters,
				ProxySupport.ALLOW);

		OpensslCertChainValidator validator = new OpensslCertChainValidator(trustAnchorsDir,
				namespaceChecks, updateInterval, validationParams);

		if (validationErrorListener != null)
			validator.addValidationListener(validationErrorListener);

		return validator;
	}

	/**
	 * Builds an Openssl-style certificate validator configured as specified in
	 * the parameters
	 * 
	 * @param trustAnchorsDir
	 *            the directory where trust anchors are loaded from
	 * @param validationErrorListener
	 *            the listener that will receive notification about validation
	 *            errors
	 * 
	 * @return
	 */
	public static AbstractValidator buildCertificateValidator(String trustAnchorsDir,
			ValidationErrorListener validationErrorListener) {

		return buildCertificateValidator(trustAnchorsDir, validationErrorListener, 0L,
				DEFAULT_NS_CHECKS, DEFAULT_CRL_CHECKS, DEFAULT_OCSP_CHECKS);
	}

	/**
	 * Builds an Openssl-style certificate validator configured as specified in
	 * the parameters
	 * 
	 * @param trustAnchorsDir
	 *            the directory where trust anchors are loaded from
	 * @param validationErrorListener
	 *            the listener that will receive notification about validation
	 *            errors
	 * @param updateInterval
	 *            the trust anchor store update interval
	 *            
	 * @return
	 */
	public static AbstractValidator buildCertificateValidator(String trustAnchorsDir,
			ValidationErrorListener validationErrorListener, long updateInterval) {

		return buildCertificateValidator(trustAnchorsDir, validationErrorListener, updateInterval,
				DEFAULT_NS_CHECKS, DEFAULT_CRL_CHECKS, DEFAULT_OCSP_CHECKS);
	}

	/**
	 * Builds an Openssl-style certificate validator configured as specified in
	 * the parameters
	 * 
	 * @param trustAnchorsDir
	 *            the directory where trust anchors are loaded from
	 * @return
	 */
	public static AbstractValidator buildCertificateValidator(String trustAnchorsDir) {
		return buildCertificateValidator(trustAnchorsDir, null, 0L, DEFAULT_NS_CHECKS,
				DEFAULT_CRL_CHECKS, DEFAULT_OCSP_CHECKS);
	}
	
	public static AbstractValidator buildCertificateValidator(){
		return buildCertificateValidator(DefaultVOMSValidator.DEFAULT_TRUST_ANCHORS_DIR, null, 0L, DEFAULT_NS_CHECKS,
				DEFAULT_CRL_CHECKS, DEFAULT_OCSP_CHECKS);
	}
}<commit_msg>Now it's possible to pass a StoreUpdateListener to buildCertificateValidator.<commit_after>/**
 * Copyright (c) <NAME>, 2006-2012.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.italiangrid.voms.util;

import java.util.Arrays;

import org.italiangrid.voms.ac.impl.DefaultVOMSValidator;

import eu.emi.security.authn.x509.CrlCheckingMode;
import eu.emi.security.authn.x509.NamespaceCheckingMode;
import eu.emi.security.authn.x509.OCSPCheckingMode;
import eu.emi.security.authn.x509.OCSPParametes;
import eu.emi.security.authn.x509.ProxySupport;
import eu.emi.security.authn.x509.StoreUpdateListener;
import eu.emi.security.authn.x509.ValidationErrorListener;
import eu.emi.security.authn.x509.helpers.pkipath.AbstractValidator;
import eu.emi.security.authn.x509.impl.CRLParameters;
import eu.emi.security.authn.x509.impl.OpensslCertChainValidator;
import eu.emi.security.authn.x509.impl.RevocationParametersExt;
import eu.emi.security.authn.x509.impl.ValidatorParamsExt;

/**
 * A utility class which provides convenient methods to build a certificate
 * validator with defaults that are meaningful for VOMS usage.
 * 
 * @author cecco
 * 
 */
public class CertificateValidatorBuilder {

	/**
	 * The default CRL checking policy.
	 */
	public static final CrlCheckingMode DEFAULT_CRL_CHECKS = CrlCheckingMode.REQUIRE;
	/**
	 * The default OCSP checking policy.
	 */
	public static final OCSPCheckingMode DEFAULT_OCSP_CHECKS = OCSPCheckingMode.IGNORE;
	/**
	 * The default namespace checking policy.
	 */
	public static final NamespaceCheckingMode DEFAULT_NS_CHECKS = NamespaceCheckingMode.GLOBUS_EUGRIDPMA;

	/** Private constructor which prevents object instantiation **/
	private CertificateValidatorBuilder() {
	}

	/**
	 * Builds an Openssl-style certificate validator configured as specified in
	 * the parameters
	 * 
	 * @param trustAnchorsDir
	 *            the directory where trust anchors are loaded from
	 * @param validationErrorListener
	 *            the listener that will receive notification about validation
	 *            errors
	 * @param updateInterval
	 *            the trust anchor store update interval
	 * @param namespaceChecks
	 *            the namespace checking policy
	 * @param crlChecks
	 *            the crl checking policy
	 * @param ocspChecks
	 *            the ocsp checking policy
	 * @return
	 */
	public static AbstractValidator buildCertificateValidator(
			String trustAnchorsDir,
			ValidationErrorListener validationErrorListener,
			StoreUpdateListener storeUpdateListener, long updateInterval,
			NamespaceCheckingMode namespaceChecks, CrlCheckingMode crlChecks,
			OCSPCheckingMode ocspChecks) {

		RevocationParametersExt revocationParameters = new RevocationParametersExt(
				crlChecks, new CRLParameters(), new OCSPParametes(ocspChecks));

		ValidatorParamsExt validationParams = new ValidatorParamsExt(
				revocationParameters, ProxySupport.ALLOW);

		if (storeUpdateListener != null)
			validationParams.setInitialListeners(Arrays
					.asList(storeUpdateListener));

		OpensslCertChainValidator validator = new OpensslCertChainValidator(
				trustAnchorsDir, namespaceChecks, updateInterval,
				validationParams);

		if (validationErrorListener != null)
			validator.addValidationListener(validationErrorListener);

		return validator;
	}

	/**
	 * Builds an Openssl-style certificate validator configured as specified in
	 * the parameters
	 * 
	 * @param trustAnchorsDir
	 *            the directory where trust anchors are loaded from
	 * @param validationErrorListener
	 *            the listener that will receive notification about validation
	 *            errors
	 * 
	 * @return
	 */
	public static AbstractValidator buildCertificateValidator(
			String trustAnchorsDir,
			ValidationErrorListener validationErrorListener) {

		return buildCertificateValidator(trustAnchorsDir,
				validationErrorListener, null, 0L, DEFAULT_NS_CHECKS,
				DEFAULT_CRL_CHECKS, DEFAULT_OCSP_CHECKS);
	}

	/**
	 * Builds an Openssl-style certificate validator configured as specified in
	 * the parameters
	 * 
	 * @param trustAnchorsDir
	 *            the directory where trust anchors are loaded from
	 * @param validationErrorListener
	 *            the listener that will receive notification about validation
	 *            errors
	 * @param storeListener
	 *            the listener that will be informed of trust store load errors
	 * @return
	 */
	public static AbstractValidator buildCertificateValidator(
			String trustAnchorsDir,
			ValidationErrorListener validationErrorListener,
			StoreUpdateListener storeListener) {

		return buildCertificateValidator(trustAnchorsDir,
				validationErrorListener, storeListener, 0L, DEFAULT_NS_CHECKS,
				DEFAULT_CRL_CHECKS, DEFAULT_OCSP_CHECKS);
	}

	/**
	 * Builds an Openssl-style certificate validator configured as specified in
	 * the parameters
	 * 
	 * @param trustAnchorsDir
	 *            the directory where trust anchors are loaded from
	 * @param validationErrorListener
	 *            the listener that will receive notification about validation
	 *            errors
	 * @param updateInterval
	 *            the trust anchor store update interval
	 * 
	 * @return
	 */
	public static AbstractValidator buildCertificateValidator(
			String trustAnchorsDir,
			ValidationErrorListener validationErrorListener, long updateInterval) {

		return buildCertificateValidator(trustAnchorsDir,
				validationErrorListener, null, updateInterval,
				DEFAULT_NS_CHECKS, DEFAULT_CRL_CHECKS, DEFAULT_OCSP_CHECKS);
	}

	/**
	 * Builds an Openssl-style certificate validator configured as specified in
	 * the parameters
	 * 
	 * @param trustAnchorsDir
	 *            the directory where trust anchors are loaded from
	 * @return
	 */
	public static AbstractValidator buildCertificateValidator(
			String trustAnchorsDir) {
		return buildCertificateValidator(trustAnchorsDir, null, null, 0L,
				DEFAULT_NS_CHECKS, DEFAULT_CRL_CHECKS, DEFAULT_OCSP_CHECKS);
	}

	/**
	 * Builds an Openssl-style certificate validator. the parameters
	 * 
	 * @return
	 */
	public static AbstractValidator buildCertificateValidator() {
		return buildCertificateValidator(
				DefaultVOMSValidator.DEFAULT_TRUST_ANCHORS_DIR, null, null, 0L,
				DEFAULT_NS_CHECKS, DEFAULT_CRL_CHECKS, DEFAULT_OCSP_CHECKS);
	}
}
{'fraction_non_alphanumeric': 0.06506821667877613, 'fraction_numerical': 0.00807297973682086, 'mean_word_length': 4.133858267716535, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7661959', 'n_tokens_mistral': 3623, 'n_tokens_neox': 3219, 'n_words': 951}