android самозаверяющий сертификат BKS и nginx интеграция - PullRequest
0 голосов
/ 08 марта 2020

Я создал сертификат .bks и поместил его в мою папку raw следующим образом:

enter image description here

И код, который с ним работает:

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

    private static char[] KEYSTORE_PASSWORD = "111111".toCharArray();

    private MySingleton(Context context) {
        mCtx = context;
        mRequestQueue = getRequestQueue();

    }


    public static synchronized MySingleton getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new MySingleton(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()));
        }
        mRequestQueue.getCache().clear(); // clear the cache
        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req) {
        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.mytruststore);
            try {
                // Initialize the keystore with the provided trusted certificates
                // Provide the password of the keystore
                trusted.load(in, KEYSTORE_PASSWORD);
            } finally {
                in.close();
            }

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

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

            SSLSocketFactory sf = context.getSocketFactory();
            return sf;

        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }


}

Теперь, к сожалению для меня, сервер, к которому синглтон делает запрос, был удален, и я восстановил файлы без сертификата для NGINX.

Могу ли я создать fre sh сертификатов (самозаверяющий nginx), и вы сможете делать запросы из приложения? ИЛИ есть ли способ извлечь cert / pem из этого файла bks? Является ли .bks в android приложении связанным с самозаверяющим сертификатом на стороне сервера?

Я помню, что следовал этому руководству, но там не говорится о том, какой сертификат использовать для nginx / на стороне сервера. https://www.codeproject.com/Articles/826045/Android-security-Implementation-of-Self-signed-SSL

Спасибо, я так растерялся.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...