Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve Cryptography Changes in Android P?

First of all, I am new to this encryption operations and I don't know whether my question is proper to ask or not! any solution is appreciated ...

In my project I use this code to create SSLSocketFactory for services:

  public static SSLSocketFactory getGlobalSSlFactory() {
   try {
    CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");
    InputStream caInput = context.getResources().openRawResource(xxxxxxx);

    Certificate ca = cf.generateCertificate(caInput);
    caInput.close();

    KeyStore keyStore = KeyStore.getInstance("BKS");
    keyStore.load(null, null);

    keyStore.setCertificateEntry("ca", ca);

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

    KeyManagerFactory kmf = KeyManagerFactory.getInstance(
            KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(keyStore, "xxxxxxx".toCharArray());

    final SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, tmf.getTrustManagers(), null); 
       return sslContext.getSocketFactory();
  } catch (Exception e) {
     e.printStackTrace();
     return null;
  }
}

The error I get when testing on device with android ( Pie ) :

error

google says: https://android-developers.googleblog.com/2018/03/cryptography-changes-in-android-p.html

Important part :

"To resolve this, you should stop specifying a provider and use the default implementation."

How should I change my code ?

** more explanations **

I did what @sonhvp said but after testing that this error comes :

error in log

And error come to this line :

 Certificate ca = cf.generateCertificate(caInput);

This is my android version :

my android version

like image 982
iDeveloper Avatar asked Jun 16 '19 04:06

iDeveloper


1 Answers

As the document said, "From Android N, we don’t recommend specifying the provider". So just remove all provider in your crypto.
In your method

CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");

remove provider ("BC")

CertificateFactory cf = CertificateFactory.getInstance("X.509");

Other methods is fine. Because it doesn't specifying a provider

KeyStore.getInstance("BKS"); //no provider
TrustManagerFactory.getInstance(tmfAlgorithm); //no provider
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); //no provider
SSLContext.getInstance("TLS"); //no provider

Something is wrong with your cert file in raw folder. I created an cert file an run it with your code without any issue. Just change to CertificateFactory.getInstance("X.509"). Try with my cert file

public class Test {

    public Test(AppCompatActivity activity) {
        context = activity;
    }

    private Context context;

    public  SSLSocketFactory getGlobalSSlFactory() {
        try {
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            InputStream caInput = context.getResources().openRawResource(R.raw.test);

            Certificate ca = cf.generateCertificate(caInput);
            caInput.close();

            KeyStore keyStore = KeyStore.getInstance("BKS");
            keyStore.load(null, null);

            keyStore.setCertificateEntry("ca", ca);

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

            KeyManagerFactory kmf = KeyManagerFactory.getInstance(
                    KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(keyStore, "xxxxxxx".toCharArray());

            final SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, tmf.getTrustManagers(), null);
            return sslContext.getSocketFactory();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}

My Activity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val test = Test(this)
        test.globalSSlFactory.createSocket()
    }

}
like image 60
Son Huynh Avatar answered Sep 30 '22 19:09

Son Huynh