【问题标题】:Dealing with pc12 keystore in android在android中处理pc12 keystore
【发布时间】:2013-12-03 09:44:41
【问题描述】:

我想在 android 中启用 ssl。Android 应用程序在 servlet 上运行,这些 servlet 托管在 tomcat 中。Tomcat 由前端的 apache web 服务器保护。我使用 open ssl 创建了密钥库。keystore 是 pc12 格式。

我使用 portecle 工具创建了 bks。我正在使用以下代码。仍然 ssl 无法正常工作。

源代码:

    import android.content.Context;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import com.myclinicmyway.*;
import java.io.InputStream;
import java.security.KeyStore;

 public class MyHttpClient extends DefaultHttpClient {

   public final Context context;


  public MyHttpClient(Context context) {
    this.context = context;
  }


  @Override protected ClientConnectionManager createClientConnectionManager() {

    SchemeRegistry registry = new SchemeRegistry();
    registry.register(
        new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    registry.register(new Scheme("https", newSslSocketFactory(), 443));
    return new SingleClientConnManager(getParams(), registry);

  }// end of client connection


  private SSLSocketFactory newSslSocketFactory() {

    try {
      KeyStore trusted = KeyStore.getInstance("BKS");
      InputStream in = context.getResources().openRawResource(R.raw.docinbangalore);

      try {
        trusted.load(in, "docinbangalore".toCharArray());
      } finally {
        in.close();    
      }
      return new SSLSocketFactory(trusted);

    }
    catch (Exception e) {
      throw new AssertionError(e);
    }// end of catch

  }// end of ssl socket

}// end of class

【问题讨论】:

    标签: android ssl openssl


    【解决方案1】:

    我得到了解决方案。以下代码对我有用

    public class MyHttpClient extends DefaultHttpClient {
    final Context context;
    TrustManager easyTrustManager = new X509TrustManager() {
        @Override
        public void checkClientTrusted(
                X509Certificate[] chain,
                String authType) throws CertificateException {
        }
    
        @Override
        public void checkServerTrusted(
                X509Certificate[] chain,
                String authType) throws CertificateException {
        }
    
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }    
    };
      public MyHttpClient(Context context) {
        this.context = context;
      }
    
      @Override protected ClientConnectionManager createClientConnectionManager() {
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(
            new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", newSslSocketFactory(), 443));
        return new SingleClientConnManager(getParams(), registry);
      }
    
    
      private MySSLSocketFactory newSslSocketFactory() {
        try {
          KeyStore trusted = KeyStore.getInstance("BKS");      
          try {
             trusted.load(null, null);
    
          } finally {
          }
    
          MySSLSocketFactory sslfactory =  new MySSLSocketFactory(trusted);
            sslfactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            return sslfactory;
        } catch (Exception e) {
          throw new AssertionError(e);
        }
    
      }
      public class MySSLSocketFactory extends SSLSocketFactory {
            SSLContext sslContext = SSLContext.getInstance("TLS");
    
            public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
                super(truststore);
    
                TrustManager tm = new X509TrustManager() {
                    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    }
    
                    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    }
    
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
                };
    
                sslContext.init(null, new TrustManager[] { tm }, null);
            }
    
            @Override
            public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
                return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
            }
    
            @Override
            public Socket createSocket() throws IOException {
                return sslContext.getSocketFactory().createSocket();
            }
        }
       }
    

    【讨论】:

      猜你喜欢
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      • 2016-08-11
      • 2021-03-31
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多