10

Tôi bắt đầu câu hỏi của tôi bằng cách nhắc đến những gì tôi đã cố gắng cho đến nay:SSL Ghim cho sử dụng Google Volley

tôi không có giấy chứng nhận trong ứng dụng của tôi, tôi đang sử dụng chìa khóa SHA256 chỉ, Hầu hết các câu trả lời trên internet đòi hỏi vật lý chứng chỉ trong ứng dụng để tải nó trong kho khóa, tôi không có.

Tôi nhận được lỗi sau:

javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. 

1) TrustKit Nó cần biên dịch SDK 24 trở lên, nhưng tôi có 23 và rất nhiều thư viện hỗ trợ được đồng bộ với SDK 23 nên tôi không thể thay đổi tất cả chúng có thể làm hỏng ứng dụng của tôi vào một thời điểm nào đó.

2) CWAC-NetSecurity Tôi đã thực hiện điều này trong mã của mình mà không cần sử dụng thiết lập bảo mật Android N, tôi cũng đã làm theo hướng dẫn trên trang git, nhưng không thể chuyển sslSocketfactory sang Volley, nó có ví dụ với OkHTTP. vì vậy nó cũng đưa ra lỗi trên.

Tôi đã thử điều này với CertificatePinner của OKHttp bởi nó cũng không hoạt động đối với tôi. Lỗi tương tự. Tôi cũng đã cố gắng vượt qua hostNameVerifier và sslSocketFactory để HttpsUrlConnection nhưng lỗi tương tự.

JsonObjectRequestSolaire jsonRequest = new JsonObjectRequestSolaire(method, URL, object, headers, responseListener, errorListener); 
    RetryPolicy policy = new DefaultRetryPolicy(TIMEOUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT); 
    jsonRequest.setRetryPolicy(policy); 
    jsonRequest.setShouldCache(false); 

    OkHttpClient okHttpClient = new OkHttpClient.Builder() 
      .certificatePinner(new CertificatePinner.Builder() 
        .add("my_domain", "sha256/shaKey")//example.com 
        .add("my_domain", "sha256/shaKey")//also tried *.example.com 
        .build()) 
      .build(); 

    //HttpsURLConnection.setDefaultHostnameVerifier(okHttpClient.hostnameVerifier()); 
    //HttpsURLConnection.setDefaultSSLSocketFactory(okHttpClient.sslSocketFactory()); 

    RequestQueue requestQueue = Volley.newRequestQueue(activity.getApplicationContext(), new HurlStack(null, okHttpClient.sslSocketFactory())); 
    requestQueue.add(jsonRequest); 

bằng cách sử dụng trustKit tên iOS của chúng tôi đã triển khai và đang hoạt động cho anh ấy.

Cảm ơn trước.

Vui lòng chia sẻ đầu vào có giá trị của bạn tại đây, để tôi có thể hiểu khái niệm ghim SSL này.

+0

là chứng chỉ máy chủ sử dụng hợp lệ? Ghim có nghĩa là kiểm tra chứng chỉ thông thường + ghim của chứng chỉ gốc, trung gian hoặc để lại. BTW: Nếu bạn có hàm băm sha256 và tên miền, bạn có thể chỉ cần tải chứng chỉ xuống máy chủ để xác minh xem nó có đúng hay không. – Robert

+0

Kiểm tra [liên kết này] (http://blog.ostorlab.co/2016/05/ssl-pinning-in-android-networking.html). –

+0

Kiểm tra [this] (https://developer.android.com/training/articles/security-ssl.html#CommonProblems) cũng có thể là vấn đề với CA. –

Trả lời

3

Sử dụng VolleySingleton này:

public class VolleySingleton { 


    private static VolleySingleton mInstance; 
    private RequestQueue mRequestQueue; 
    private static Context mCtx; 

    private VolleySingleton(Context context) { 
    mCtx = context; 
    mRequestQueue = getRequestQueue(); 
    } 

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

    public RequestQueue getRequestQueue() { 
    if (mRequestQueue == null) { 
     // getApplicationContext() is key, it keeps you from leaking the 
     // Activity or BroadcastReceiver if someone passes one in. 
     mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext(), new HurlStack(null, newSslSocketFactory())); 
    } 
    return mRequestQueue; 
    } 

    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 { 
     // 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 = mCtx.getApplicationContext().getResources().openRawResource(R.raw.trusted); 
     try { 
     // Initialize the keystore with the provided trusted certificates 
     // Provide the password of the keystore 
     trusted.load(in, mCtx.getString(R.string.KEYSTORE_PASS).toCharArray()); 
     } finally { 
     in.close(); 
     } 

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

     SSLContext context = SSLContext.getInstance("TLSv1.2"); 
     context.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 = context.getSocketFactory(); 
     return sf; 
    } catch (Exception e) { 
     throw new AssertionError(e); 
    } 
    } 
} 
Các vấn đề liên quan