简体   繁体   中英

SSLSocket Connection trust certificate

I'm currently developing an Android application on which I need to do a SSLSocket Connected to a test server.

Thing is we won't buy a certificate for this server since it is a test server, not production.

While it works fine on Simulator, however on my S3 it gives me this error :

06-01 00:36:17.355: I/System.out(13629): java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

The exception is throws by the last line of this code :

mSf = (SSLSocketFactory) SSLSocketFactory.getDefault();
mSocket = (SSLSocket) mSf.createSocket("myTestingServer.com", 443);
mSocket.startHandshake();

I don't really care that the solution is either code or lower the security check as an Android user, since it is to test on my S3, once in production, the certificate should be good. This is not a HTTP server, so I can't use org.apache.http.org.ssl

Maybe you want to ignore the certificate check

Warning : this code is not safe

TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
        return null;
    }

    @Override
    public void checkClientTrusted(X509Certificate[] certs, String authType) {
    }

    @Override
    public void checkServerTrusted(X509Certificate[] certs, String authType) {
    }
}};

// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());

SSLSocketFactory sslsocketfactory = sc.getSocketFactory();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("google.com", 443);
...

Source : Using Java to connect with SSLSocket, trusting all certificates

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM