0

I'm trying throw my VolleySingleton make requests to my https server, I used keystore to save the certificate to .bks file but still getting no response from the request I'm doing the keystore with the certificate I got from ssl company, without the intermediate certs. I'm using:

keytool -importcert -v -trustcacerts -file "C:/cert.cer" -alias test -keystore "C:/certificate.bks" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "C:/bcprov-jdk16-145.jar" -storetype BKS -storepass testpass

my volley singleton:

public class VolleySingleton{

private static VolleySingleton instance;
private RequestQueue requestQueue;
private static Context ctx;

private VolleySingleton(Context context) {
    ctx = context;
    requestQueue = getRequestQueue();

}

public static synchronized VolleySingleton getInstance(Context context) {
    if (instance == null) {
        instance = new VolleySingleton(context);
    }
    return instance;
}

public RequestQueue getRequestQueue() {
    if (requestQueue == null) {
            requestQueue = Volley.newRequestQueue(ctx.getApplicationContext(), new HurlStack(null, newSslSocketFactory()));
    }
    return requestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {

    int socketTimeout = 90000;
    RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    req.setRetryPolicy(policy);
    getRequestQueue().add(req);
}

private SSLSocketFactory newSslSocketFactory() {

    try {
        KeyStore trusted = KeyStore.getInstance("BKS");
        InputStream in = ctx.getApplicationContext().getResources().openRawResource(R.raw.certificate);
        try {
            trusted.load(in, ctx.getString(R.string.keystore).toCharArray());
        } finally {
            in.close();
        }

        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(trusted);

        SSLContext context1 = SSLContext.getInstance("TLS");
        context1.init(null, tmf.getTrustManagers(), null);

        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                Log.i("Volley","Verifing host:"+hostname);
                return true;
            }
        });

        SSLSocketFactory sf = context1.getSocketFactory();
        return sf;
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}

}

The application worked fine with request to http domain, the problem came when I installed ssl on the domain. I'm not getting error response from the stringrequest of volley

What I'm doing wrong? I think the problem is in the keystore, which certificate I've to keystore? I tried with root and intermediate also separated but got nothing

Gextech L
  • 1
  • 1
  • Could you please add any specific errors that the program returns? Also there seems to be related question https://stackoverflow.com/questions/47941438/proper-way-to-build-a-volley-singleton – Fanick Mar 05 '20 at 19:56
  • Does this answer your question? [Proper way to build a Volley Singleton](https://stackoverflow.com/questions/47941438/proper-way-to-build-a-volley-singleton) – Fanick Mar 05 '20 at 19:59
  • @fanick I'm not getting errors, just no response from the stringrequest, no error response, I was working with a domain without ssl and all was working fine, the problem is when I installed the ssl to the domain, the volleysingleton worked well I want to adapt it to works with the ssl – Gextech L Mar 05 '20 at 20:02

0 Answers0