Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Invalid
-
19
-
None
-
Android application development.
Description
I am trying to parse a .cer certificate issued by Thwate(find it attacthed).I am getting a null pointer exception while parsing the certificate.But I am able to parse other .cer certificates without any problem.
package com.ams;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.ams.certparser.CertificateParserUtil;
public class CertificateParserTestActivity extends Activity {
/** Called when the activity is first created. */
private static String TAG = "CertificateParserTestActivity";
@Override
public void onCreate(Bundle savedInstanceState)
}
package com.ams.certparser;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
import java.security.PublicKey;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Date;
import java.util.Enumeration;
import javax.security.auth.x500.X500Principal;
import android.os.Environment;
import android.util.Log;
public class CertificateParserUtil {
private static final String TAG = "CertificateParserUtil";
public static final String PFX = "PFX";
public static final String CER = "CER";
FileInputStream fin = null;
private String filePath;
private String password;
X509Certificate cert;
public CertificateParserUtil(String filePath, String password)
{ this.filePath = filePath; this.password = password; }private String getExtension()
{ String ext = ""; int mid = filePath.lastIndexOf("."); ext = filePath.substring(mid + 1, filePath.length()); return ext; } public void parse() {
String ext = getExtension();
File file = new File(filePath);
if (PFX.equalsIgnoreCase(ext)) {
KeyStore ks = null;
try {
ks = KeyStore.getInstance("PKCS12");
//fin = new FileInputStream(file);
ks.load(fin, password.toCharArray());
Enumeration<String> aliasesEnum = null;
aliasesEnum = ks.aliases();
while (aliasesEnum.hasMoreElements())
} catch (KeyStoreException e)
{ Log.i(TAG, "KeyStoreException:" + e.getMessage()); }catch (FileNotFoundException e)
{ Log.i(TAG, "FileNOTFoundException:" + e.getMessage()); }catch (CertificateException e)
{ Log.i(TAG, "CertificateException:" + e.getMessage()); }catch (IOException e)
{ Log.i(TAG, "IOException:" + e.getMessage()); }catch (NoSuchAlgorithmException e)
{ Log.i(TAG, "AlgorithmException:" + e.getMessage()); }catch (Exception e)
{ Log.i(TAG, "Exception:" + e.getMessage()); } } else if (CER.equalsIgnoreCase(ext)) {
try {
fin = new FileInputStream(file);
BufferedInputStream bf = new BufferedInputStream(fin);
CertificateFactory cf = null;
cf = CertificateFactory.getInstance("X.509");
while (bf.available() > 0)
} catch (FileNotFoundException e)
{ Log.i(TAG, "FileNOTFoundException:" + e.getMessage()); }catch (CertificateException e)
{ Log.i(TAG, "CertificateException:" + e.getMessage()); }catch (IOException e)
{ Log.i(TAG, "IOException:" + e.getMessage()); }catch (Exception e)
{ Log.i(TAG, "Exception:" + e.getMessage()); }}
}
public String getCertType()
{ return cert.getType(); }public int getVersion()
{ return cert.getVersion(); }public int getBasicConstraints()
{ return cert.getBasicConstraints(); }public boolean[] getIssuerUniqueID()
{ return cert.getIssuerUniqueID(); }public X500Principal getIssuerX500Principal()
{ return cert.getIssuerX500Principal(); }public PublicKey getPublicKey()
{ return cert.getPublicKey(); }public Principal getIssuerDN()
{ return cert.getIssuerDN(); }public Principal getSubjectDN()
{ return cert.getSubjectDN(); }public Date getValidFrom()
{ return cert.getNotBefore(); }public Date getValidTill()
{ return cert.getNotAfter(); }public String getAlgorithm()
{ return cert.getPublicKey().getAlgorithm(); }public BigInteger getSerialNumber()
{ return cert.getSerialNumber(); }public boolean[] getKeyUsage()
{ return cert.getKeyUsage(); }public byte[] getSignature()
{ return cert.getSignature(); }public String getSigAlgName()
{ return cert.getSigAlgName(); }}
CertificateException:org.apache.harmony.security.asn1.ASN1Exception: ASN.1 Sequence: mandatory value is missing at [4].
The folllowing is the stacktrace for your refrence.
03-22 12:16:30.667: W/dalvikvm(1749): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
03-22 12:16:30.717: E/AndroidRuntime(1749): FATAL EXCEPTION: main
03-22 12:16:30.717: E/AndroidRuntime(1749): java.lang.RuntimeException: Unable to start activity ComponentInfo
: java.lang.NullPointerException
03-22 12:16:30.717: E/AndroidRuntime(1749): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
03-22 12:16:30.717: E/AndroidRuntime(1749): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
03-22 12:16:30.717: E/AndroidRuntime(1749): at android.app.ActivityThread.access$600(ActivityThread.java:123)
03-22 12:16:30.717: E/AndroidRuntime(1749): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
03-22 12:16:30.717: E/AndroidRuntime(1749): at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 12:16:30.717: E/AndroidRuntime(1749): at android.os.Looper.loop(Looper.java:137)
03-22 12:16:30.717: E/AndroidRuntime(1749): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-22 12:16:30.717: E/AndroidRuntime(1749): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 12:16:30.717: E/AndroidRuntime(1749): at java.lang.reflect.Method.invoke(Method.java:511)
03-22 12:16:30.717: E/AndroidRuntime(1749): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-22 12:16:30.717: E/AndroidRuntime(1749): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-22 12:16:30.717: E/AndroidRuntime(1749): at dalvik.system.NativeStart.main(Native Method)
03-22 12:16:30.717: E/AndroidRuntime(1749): Caused by: java.lang.NullPointerException
03-22 12:16:30.717: E/AndroidRuntime(1749): at com.ams.certparser.CertificateParserUtil.getVersion(CertificateParserUtil.java:115)
03-22 12:16:30.717: E/AndroidRuntime(1749): at com.ams.CertificateParserTestActivity.onCreate(CertificateParserTestActivity.java:23)
03-22 12:16:30.717: E/AndroidRuntime(1749): at android.app.Activity.performCreate(Activity.java:4465)
03-22 12:16:30.717: E/AndroidRuntime(1749): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-22 12:16:30.717: E/AndroidRuntime(1749): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-22 12:16:30.717: E/AndroidRuntime(1749): ... 11 more