Wednesday 7 July 2021

SSL Pinning in Volley using SHA256 & CertificatePinner

I am trying to implement SSL Pinning in volley using HurlStack.

In OkHttpStack class, how do I provide my mClient to the library to create the connection? Currently mClient is not being used so SSL Pinning is not working. Where should I pass this mClient so that library can use it for SSL Pinning?

I need to implement SSL Pinning with volley using current architecture. Any way for this?

public class OkHttpStack extends HurlStack {
    private final OkHttpClient mClient;

    public OkHttpStack(Context context) {
        this(new OkHttpClient(), context);
    }

    public OkHttpStack(OkHttpClient client, Context context) {
        if (client == null) {
            throw new NullPointerException("Client must not be null.");
        }
        CertificatePinner pinner = new CertificatePinner.Builder()
                .add("xyz.com", "sha256/XXXXXXXSKBC8dHnQYY6ncwwUtv2ydjxGAlXXXXXXXXs=").build();
        mClient = client.newBuilder().certificatePinner(pinner).build();


    }

    @Override
    protected HttpURLConnection createConnection(URL url) throws IOException {
        return (HttpURLConnection) url.openConnection();
    }
}

public class VolleyQueueUtils {

    private static final String DEFAULT_CACHE_DIR = "volley";
    private static final int DISK_CACHE_MAX_SIZE = 20 * 1024 * 1024;

    private static RequestQueue sGeneralRequestQueue;

    private static DiskBasedCache sDiskCache;

    private static RequestQueue sImageQueue;

//    private static ImageLoader sImageLoader;

    private static RequestQueue sJobQueue;

    private static RequestQueue sSingleThreadedRequestQueue;

    static {
        File cacheDir = new File(App.context.getCacheDir(), DEFAULT_CACHE_DIR);
        sDiskCache = new DiskBasedCache(cacheDir, DISK_CACHE_MAX_SIZE);

        ResponseDelivery delivery = new ExecutorDelivery(Executors.newFixedThreadPool(4));
        ResponseDelivery deliverySingle = new ExecutorDelivery(Executors.newFixedThreadPool(1));

        sGeneralRequestQueue =
                new RequestQueue(sDiskCache, new BasicNetwork(new OkHttpStack(App.context)), 4, delivery);

//        sGeneralRequestQueue = Volley.newRequestQueue(App.context, new OkHttpStack(App.context));
        sGeneralRequestQueue.start();

        sImageQueue = new RequestQueue(sDiskCache ,new BasicNetwork(new OkHttpStack(App.context)), 4, delivery);
        sImageQueue.start();

        sSingleThreadedRequestQueue = new RequestQueue(sDiskCache, new BasicNetwork(new OkHttpStack(App.context)), 1,
                deliverySingle);
        //sSingleThreadedRequestQueue.start();

//        sImageLoader = new ImageLoader(sImageQueue, new LruBitmapCache());

        // Job queue for background tasks
        sJobQueue = new RequestQueue(new NoCache(), new BasicNetwork(new OkHttpStack(App.context)), 4, delivery);
        sJobQueue.start();
    }

    public static ImageLoader getImageLoader() {
        return BitmapQueueUtils.getLoaderInstance();
    }

    public static RequestQueue getGeneralRequestQueue() {
        return sGeneralRequestQueue;
    }

    public static RequestQueue getSingleThreadedRequestQueue() {
        return sSingleThreadedRequestQueue;
    }

    public static RequestQueue getJobQueue () {
        return sJobQueue;
    }
}


from SSL Pinning in Volley using SHA256 & CertificatePinner

No comments:

Post a Comment