0

I am trying to import self signed certificate into Keystore. I places cert in asset folder and trying to read inputstream in CertificateFactory.

But CertificateFactory object is null. It is not able to read inputstream. Inputstream is having value I can check that. Please see my code here:

CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");
        AssetManager assetManager = getAssets();
        InputStream caInput = assetManager.open("somecert.pem");
        X509Certificate caCertificate = (X509Certificate)cf.generateCertificate(caInput);
 //  Certificate ca = cf.generateCertificate(caInput);  //Also did this.

caCertificate is having null value. I am trying to use this in my keystore.

        String keyStoreType = KeyStore.getDefaultType();
        keyStore = KeyStore.getInstance(keyStoreType);
        keyStore.load(null, null);
        keyStore.setCertificateEntry("ca", caCertificate);
Shivv
  • 65
  • 2
  • 11

1 Answers1

0

when I need to upload a certificate crt, use this code (This code can serve you, I think it`s the same):

// Loading the certificate in asserts
CertificateFactory cf = CertificateFactory.getInstance("X.509");
AssetManager assManager = context.getAssets();
caInput = assManager.open("certif.crt");
Certificate ca = cf.generateCertificate(caInput);
// Create a KeyStore containing different CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);

// Create a TrustManager to store certificates
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);

// Creates a context for use ssl certificate
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);

But in this question may be found the solution How to Load RSA Private Key From File

Community
  • 1
  • 1
lûr
  • 191
  • 12