<commit_before>/*
 * Copyright (C) 2012 the diamond:dogs|group
 *
 * 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 at.diamonddogs.net.ssl;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.content.Context;
import at.diamonddogs.net.WebClient;
import at.diamonddogs.net.WebClientDefaultHttpClient;
import at.diamonddogs.net.WebClientHttpURLConnection;

/**
 * Enables SSL sockets on all included {@link WebClient}
 * 
 * TODO: save 2 SSL states, one for each {@link WebClient}
 */
public class SSLHelper {

	private static final Logger LOGGER = LoggerFactory.getLogger(SSLHelper.class.getSimpleName());
	private static SSLHelper INSTANCE;
	private SSLState sslState;

	/**
	 * Stores the SSL factory for the apache {@link WebClient} ->
	 * {@link WebClientDefaultHttpClient}
	 */
	public CustomSSLSocketFactory SSL_FACTORY_APACHE = null;

	/**
	 * Stores the SSL factory for the Java {@link WebClient} ->
	 * {@link WebClientHttpURLConnection}
	 */
	public javax.net.ssl.SSLSocketFactory SSL_FACTORY_JAVA = null;

	private SSLHelper() {
	}

	/**
	 * Singleton SSLHelper
	 * 
	 * @return the singleton {@link SSLHelper} instance
	 */
	public static SSLHelper getInstance() {

		synchronized (SSLHelper.class) {

			if (INSTANCE == null) {
				INSTANCE = new SSLHelper();
				INSTANCE.sslState = new SSLState();
			}
			return INSTANCE;

		}
	}

	/**
	 * Register a keystore with SSL (APACHE)
	 * 
	 * @param c
	 *            a {@link Context}
	 * @param resourceId
	 *            the resource id of the keystore
	 * @param password
	 *            the password of the keystore
	 * @return true on success, false otherwise
	 */
	public boolean initSSLFactoryApache(Context c, int resourceId, String password) {
		try {
			if (c == null || resourceId == -1 || password == null) {
				LOGGER.info("No keystore specified, using alltrust");
				makeAllTrustManagerForApache();
				return true;
			} else {
				KeyStore store = getKeyStore(c, resourceId, password);
				SSL_FACTORY_APACHE = new CustomSSLSocketFactory(store);
				sslState.trustAll = false;
				return true;
			}
		} catch (Throwable tr) {
			LOGGER.warn("Error initializing SSLFactoryApache, trusting all certs", tr);
			try {
				makeAllTrustManagerForApache();
				sslState.tr = tr;
				return true;
			} catch (Throwable tr1) {<commit_msg>added possibility to init alls ssl factories<commit_after>/*
 * Copyright (C) 2012 the diamond:dogs|group
 *
 * 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 at.diamonddogs.net.ssl;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.content.Context;
import at.diamonddogs.net.WebClient;
import at.diamonddogs.net.WebClientDefaultHttpClient;
import at.diamonddogs.net.WebClientHttpURLConnection;

/**
 * Enables SSL sockets on all included {@link WebClient}
 * 
 * TODO: save 2 SSL states, one for each {@link WebClient}
 */
public class SSLHelper {

	private static final Logger LOGGER = LoggerFactory.getLogger(SSLHelper.class.getSimpleName());
	private static SSLHelper INSTANCE;
	private SSLState sslState;

	/**
	 * Stores the SSL factory for the apache {@link WebClient} ->
	 * {@link WebClientDefaultHttpClient}
	 */
	public CustomSSLSocketFactory SSL_FACTORY_APACHE = null;

	/**
	 * Stores the SSL factory for the Java {@link WebClient} ->
	 * {@link WebClientHttpURLConnection}
	 */
	public javax.net.ssl.SSLSocketFactory SSL_FACTORY_JAVA = null;

	private SSLHelper() {
	}

	/**
	 * Singleton SSLHelper
	 * 
	 * @return the singleton {@link SSLHelper} instance
	 */
	public static SSLHelper getInstance() {

		synchronized (SSLHelper.class) {

			if (INSTANCE == null) {
				INSTANCE = new SSLHelper();
				INSTANCE.sslState = new SSLState();
			}
			return INSTANCE;

		}
	}

	/**
	 * Register a keystore with SSL (APACHE && JAVA)
	 * 
	 * @param c
	 *            a {@link Context}
	 * @param resourceId
	 *            the resource id of the keystore
	 * @param password
	 *            the password of the keystore
	 * @return true on success, false otherwise
	 */
	public boolean initAllSSLFactory(Context c, int resourceId, String password) {
		return initSSLFactoryApache(c, resourceId, password) && initSSLFactoryJava(c, resourceId, password);
	}

	/**
	 * Register a keystore with SSL (APACHE)
	 * 
	 * @param c
	 *            a {@link Context}
	 * @param resourceId
	 *            the resource id of the keystore
	 * @param password
	 *            the password of the keystore
	 * @return true on success, false otherwise
	 */
	public boolean initSSLFactoryApache(Context c, int resourceId, String password) {
		try {
			if (c == null || resourceId == -1 || password == null) {
				LOGGER.info("No keystore specified, using alltrust");
				makeAllTrustManagerForApache();
				return true;
			} else {
				KeyStore store = getKeyStore(c, resourceId, password);
				SSL_FACTORY_APACHE = new CustomSSLSocketFactory(store);
				sslState.trustAll = false;
				return true;
			}
		} catch (Throwable tr) {
			LOGGER.warn("Error initializing SSLFactoryApache, trusting all certs", tr);
			try {
				makeAllTrustManagerForApache();
				sslState.tr = tr;
				return true;
			} catch (Throwable tr1) {
{'fraction_non_alphanumeric': 0.0947716926472617, 'fraction_numerical': 0.004414401986480894, 'mean_word_length': 3.8333333333333335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4635524', 'n_tokens_mistral': 2326, 'n_tokens_neox': 2078, 'n_words': 664}