Android Open Source - khandroid Khandroid X509 Trust Manager






From Project

Back to project page khandroid.

License

The source code is released under:

Apache License

If you think the Android project khandroid 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

/*
 * Copyright (C) 2012-2014 Ognyan Bankov
 */* ww  w.  ja v a  2  s .c o  m*/
 * 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 com.github.khandroid.http.ssl;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;

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

import org.slf4j.LoggerFactory;


public class KhandroidX509TrustManager implements javax.net.ssl.X509TrustManager {
    private ArrayList<X509TrustManager> mX509TrustManagers = new ArrayList<X509TrustManager>();

    private final org.slf4j.Logger mLogger = LoggerFactory.getLogger(KhandroidX509TrustManager.class
            .getSimpleName());


    public KhandroidX509TrustManager(InputStream keyStore, String keyStorePassword) throws GeneralSecurityException {
        // first add original trust manager
        final TrustManagerFactory originalFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        originalFactory.init((KeyStore) null);
        for (TrustManager tm : originalFactory.getTrustManagers()) {
            if (tm instanceof X509TrustManager) {
                mX509TrustManagers.add((X509TrustManager) tm);
            }
        }

        // them add our custom trust manager
        X509TrustManager mX509TrustManagerCustom = fetchTrustManager(keyStore, keyStorePassword);
        if (mX509TrustManagerCustom != null) {
            mX509TrustManagers.add(mX509TrustManagerCustom);
        } else {
            throw new IllegalArgumentException("Keystore is valid but cannot find TrustManagerFactory of type X509TrustManager.");
        }
    }


    private javax.net.ssl.X509TrustManager fetchTrustManager(InputStream keyStore,
                                                             String keyStorePassword) throws GeneralSecurityException {
        javax.net.ssl.X509TrustManager ret = null;

        TrustManagerFactory tmf = prepareTrustManagerFactory(keyStore, keyStorePassword);
        TrustManager tms[] = tmf.getTrustManagers();

        for (int i = 0; i < tms.length; i++) {
            if (tms[i] instanceof javax.net.ssl.X509TrustManager) {
                ret = (javax.net.ssl.X509TrustManager) tms[i];
                //        break;
            }
        }

        return ret;
    }


    private TrustManagerFactory prepareTrustManagerFactory(InputStream keyStore,
                                                           String keyStorePassword) throws GeneralSecurityException {
        TrustManagerFactory ret = null;

        KeyStore ks;
        ks = KeyStore.getInstance("BKS");
        try {
            ks.load(keyStore, keyStorePassword.toCharArray());
        } catch (IOException e) {
            throw new GeneralSecurityException("Problem reading keystore stream", e);
        }
        ret = TrustManagerFactory.getInstance("X509");
        ret.init(ks);

        return ret;
    }


    @Override
    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        // Oh, I am easy!
    }


    @Override
    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        boolean ok = false;
        for (X509TrustManager tm : mX509TrustManagers) {
            try {
                tm.checkServerTrusted(chain, authType);
                mLogger.debug("Certificate is trusted.");
                ok = true;
                break;
            } catch (CertificateException e) {
                // ignore
            }
        }
        if (!ok) {
            mLogger.debug("Certificate is NOT trusted.");
            throw new CertificateException();
        }
    }


    @Override
    public X509Certificate[] getAcceptedIssuers() {
        final ArrayList<X509Certificate> list = new ArrayList<X509Certificate>();
        for (X509TrustManager tm : mX509TrustManagers)
            list.addAll(Arrays.asList(tm.getAcceptedIssuers()));
        return list.toArray(new X509Certificate[list.size()]);
    }
}




Java Source Code List

com.github.khandroid.JsonFunctionality.java
com.github.khandroid.http.functionality.HttpFunctionalityImpl.java
com.github.khandroid.http.functionality.HttpFunctionalityWCookiesImpl.java
com.github.khandroid.http.functionality.HttpFunctionalityWCookies.java
com.github.khandroid.http.functionality.HttpFunctionality.java
com.github.khandroid.http.misc.FileDownloader.java
com.github.khandroid.http.misc.KhandroidBasicResponseHandler.java
com.github.khandroid.http.misc.SynchronizedCookieStore.java
com.github.khandroid.http.request.GetRequestBuilder.java
com.github.khandroid.http.request.PostRequestBuilder.java
com.github.khandroid.http.request.RequestBuilder.java
com.github.khandroid.http.ssl.DefaultSslHttpClient.java
com.github.khandroid.http.ssl.KhandroidX509TrustManager.java
com.github.khandroid.http.ssl.SslSocketFactory.java
com.github.khandroid.kat.ActivityKatExecutorFunctionality.java
com.github.khandroid.kat.FragmentKatExecutorFunctionality.java
com.github.khandroid.kat.KatExecutor.java
com.github.khandroid.kat.KhandroidAsyncTask.java
com.github.khandroid.misc.ActivityUtils.java
com.github.khandroid.misc.DateTimeHelper.java
com.github.khandroid.misc.ImageUtils.java
com.github.khandroid.misc.LocationUtils.java
com.github.khandroid.misc.NetUtils.java
com.github.khandroid.misc.StringUtils.java
com.github.khandroid.misc.ViewUtils.java
com.github.khandroid.rest.InvalidJsonString.java
com.github.khandroid.rest.InvalidResponseException.java
com.github.khandroid.rest.KhRestExchangeBuilder.java
com.github.khandroid.rest.KhRestFunctionalityImpl.java
com.github.khandroid.rest.KhRestFunctionality.java
com.github.khandroid.rest.KhRestResult.java
com.github.khandroid.rest.MalformedResponseException.java
com.github.khandroid.rest.RawRestResult.java
com.github.khandroid.rest.RestAsyncTask.java
com.github.khandroid.rest.RestExchangeBuilder.java
com.github.khandroid.rest.RestExchangeFailedException.java
com.github.khandroid.rest.RestExchangeOutcome.java
com.github.khandroid.rest.RestExchange.java
com.github.khandroid.rest.RestFragmentFunctionality.java
com.github.khandroid.rest.RestFunctionalityImpl.java
com.github.khandroid.rest.RestFunctionality.java
com.github.khandroid.rest.RestPersistFragment.java
com.github.khandroid.rest.RestResponse.java
com.github.khandroid.rest.RestResult.java
com.github.khandroid.rest.UnexpectedResponseException.java
com.github.khandroid.state.StateEvent.java
com.github.khandroid.state.StateMachineImpl.java
com.github.khandroid.state.StateMachine.java
com.github.khandroid.state.StateSkeleton.java
com.github.khandroid.state.State.java
com.github.khandroid.state_app.AppStateContextImpl.java
com.github.khandroid.state_app.AppStateContext.java
com.github.khandroid.state_app.AppState.java
com.github.khandroid.state_app.InvalidAppStateException.java
com.github.khandroid.state_app.SkeletonAppState.java
com.github.khandroid.state_app.StateEvent.java
com.github.khandroid.ui_functionality.ActivityAttachable.java
com.github.khandroid.ui_functionality.ActivityAttachedFunctionality.java
com.github.khandroid.ui_functionality.ActivityUniqueAttachedFunctionality.java
com.github.khandroid.ui_functionality.FragmentAttachable.java
com.github.khandroid.ui_functionality.FragmentAttachedFunctionality.java
com.github.khandroid.ui_functionality.FragmentUniqueAttachedFunctionality.java
com.github.khandroid.ui_functionality.HostActivity.java
com.github.khandroid.ui_functionality.HostFragment.java
com.github.khandroid.ui_functionality.SuperNotCalledException.java
com.github.khandroid.views.ImageViewWithContextMenuInfo.java
com.github.khandroid.volley.GetJsonObjectRequestBuilder.java
com.github.khandroid.volley.GetStringRequestBuilder.java
com.github.khandroid.volley.GetVRestRequestBuilder.java
com.github.khandroid.volley.PostJsonObjectRequestBuilder.java
com.github.khandroid.volley.PostRequestBuilderImpl.java
com.github.khandroid.volley.PostRequestBuilder.java
com.github.khandroid.volley.PostStringRequestBuilder.java
com.github.khandroid.volley.PostVRestRequestBuilder.java
com.github.khandroid.volley.RequestBuilder.java
com.github.khandroid.volley.VRestRequestBuilder.java
com.github.khandroid.volley.VRestRequest.java