commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Srijan Basu (Created) (JIRA)" <j...@apache.org>
Subject [jira] [Created] (COMMONSSITE-70) org.apache.harmony.security.asn1 throwing null pointer exception
Date Thu, 22 Mar 2012 08:58:22 GMT
org.apache.harmony.security.asn1 throwing null pointer exception
----------------------------------------------------------------

                 Key: COMMONSSITE-70
                 URL: https://issues.apache.org/jira/browse/COMMONSSITE-70
             Project: Commons All
          Issue Type: Bug
          Components: Commons Build
    Affects Versions: 19
         Environment: Android application development.
            Reporter: Srijan Basu
             Fix For: 19


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) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		CertificateParserUtil certParserUtil = new CertificateParserUtil(
				"sdcard/Thwate.cer","null");
		certParserUtil.parse(); 

	//	Log.i(TAG, "Type:" + certParserUtil.getCertType());
		Log.i(TAG, "Version:" + certParserUtil.getVersion());
		Log.i(TAG, "Basic Constraint Extensions:" + certParserUtil.getBasicConstraints());
		Log.i(TAG, "IssuerUniqueID:" + certParserUtil.getIssuerUniqueID());
		Log.i(TAG, "IssuerName:" + certParserUtil.getIssuerX500Principal());
		Log.i(TAG, "Public Key:"+ certParserUtil.getPublicKey());
		Log.i(TAG, "Issuer DN:" + certParserUtil.getIssuerDN());
		Log.i(TAG, "Subject:" + certParserUtil.getSubjectDN());
		Log.i(TAG, "Valid From:" + certParserUtil.getValidFrom());
		Log.i(TAG, "Valid Till:" + certParserUtil.getValidTill());
		Log.i(TAG, "Public Key:" + certParserUtil.getPublicKey().getAlgorithm());
		Log.i(TAG, "Serial Number:" + certParserUtil.getSerialNumber());
		Log.i(TAG, "Key:" + certParserUtil.getKeyUsage());
		Log.i(TAG, "Signature:" + certParserUtil.getSignature());
		Log.i(TAG, "SignSlgoName:" + certParserUtil.getSigAlgName());
		
			//Log.i(TAG, "Signature:" + certParserUtil.getTBSCertificate());

	}
}

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()) {
					String alias = (String) aliasesEnum.nextElement();
					cert = (X509Certificate) ks.getCertificate(alias);

				}
			} 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) {
					cert = (X509Certificate) cf.generateCertificate(bf);

				}
			} 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{com.ams/com.ams.CertificateParserTestActivity}: 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


--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Mime
View raw message