自签名证书和循环J为Android(Self-signed certificate and loop

2019-07-30 12:26发布

我试图用循环J作出异步 HTTP请求。 伟大的作品,除了当我尝试使用自签名证书访问HTTPS站点。 我得到

javax.net.ssl.SSLPeerUnverifiedException: No peer certificate.

我猜默认SSL选项可以使用覆盖setSSLSocketFactory(SSLSocketFactory sslSocketFactory)方法,但我不知道该怎么做,或者它可能不是正确的方式在所有。

请建议我该如何解决这个问题呢?

Answer 1:

你这样做几乎完全一样,这里的HttpClient解释只是一点点简单的- 信任的HttpClient使用HTTPS上的所有证书

创建一个自定义类:

import java.io.IOException;
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 java.security.cert.X509Certificate;

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

import org.apache.http.conn.ssl.SSLSocketFactory;
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();
    }
}

然后,当你建立你的客户机实例:

try {
      KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
      trustStore.load(null, null);
      sf = new MySSLSocketFactory(trustStore);
      sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
      client.setSSLSocketFactory(sf);   
    }
    catch (Exception e) {   
    }


Answer 2:

您可以使用构造AsyncHttpClient(布尔fixNoHttpResponseException,INT HTTPPORT,INT httpsPort)。 从版本循环J库1.4.4大。 例如

mClient = new AsyncHttpClient(true, 80, 443);

你会得到警告信息的详细日志来logcat的。

Beware! Using the fix is insecure, as it doesn't verify SSL certificates. 


Answer 3:

更简单的方法是使用内置的循环J MySSLSocketFactory,所以您不必创建另一个类

try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        client.setSSLSocketFactory(sf);
}
catch (Exception e) {}


Answer 4:

由于在很多地方解释的证书的简单绕过验证错误在很多层面上。 不要那样做!

你应该做的,而不是为创建.bks从您的证书文件(用于这一目的,你会需要充气城堡 ):

keytool -importcert -v -trustcacerts -file "path/to/certfile/certfile.crt" -alias IntermediateCA -keystore "outputname.bks" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "path/to/bouncycastle/bcprov-jdk15on-154.jar" -storetype BKS -storepass atleastsix

下一个地方新创建outputname.bksres/raw文件夹中。

创建辅助函数(可以是自己的类或任何你喜欢里面):

private static SSLSocketFactory getSocketFactory(Context ctx) {
        try {
            // Get an instance of the Bouncy Castle KeyStore format
            KeyStore trusted = KeyStore.getInstance("BKS");
            // Get the raw resource, which contains the keystore with
            // your trusted certificates (root and any intermediate certs)
            InputStream in = ctx.getResources().openRawResource(R.raw.outputname); //name of your keystore file here
            try {
                // Initialize the keystore with the provided trusted certificates
                // Provide the password of the keystore
                trusted.load(in, "atleastsix".toCharArray());
            } finally {
                in.close();
            }
            // Pass the keystore to the SSLSocketFactory. The factory is responsible
            // for the verification of the server certificate.
            SSLSocketFactory sf = new SSLSocketFactory(trusted);
            // Hostname verification from certificate
            // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
            sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); // This can be changed to less stricter verifiers, according to need
            return sf;
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }

最后但并非最不重要的,设置您的AsyncHttpClient使用新的套接字工厂:

AsyncHttpClient client = new AsyncHttpClient();
client.setSSLSocketFactory(getSocketFactory(context));


Answer 5:

随着Httpscertificate我有两个文档的帮助下成功地做到了HttpsURLConnection的和Portecle 。



Answer 6:

不要NUKE所有SSL证书..信任所有证书是一种不好的做法!

  • 只接受你的SSL证书。

看看我的解决方案。 从这个要点的一些内容可以帮助您推测如何做到这一点。

OBS:我使用的是Android的截击

https://gist.github.com/ivanlmj/f11fb50d35fa1f2b9698bfb06aedcbcd



文章来源: Self-signed certificate and loopj for Android
标签: android loopj