Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Digital signature: sample code for verification and for extracting certification information

I use a third party tool to verify signature and to get certificate detail(like serial number, CA etc..) from signature. The problem with this utility is it is licensed and works on certain machines only.

Can i validate the signature against the data using simple java or .net code?(instead of using paid application). I dont have private key to extract certificate information from signed data.

Or if someone can suggest sample code in java or .net to extract certificate detail if i have pfx file. Of from signed data.

Data is signed with asymmetric encryption.

like image 835
Amit Kumar Gupta Avatar asked May 27 '11 06:05

Amit Kumar Gupta


2 Answers

To extract detail from certificate:

  1. Make a string which keeps certificate data. Just ensure it has -----BEGIN CERTIFICATE----- in starting and -----END CERTIFICATE----- in end.
  2. Now use the following code in Java to extract certificate detail.

InputStream inStream = new ByteArrayInputStream(certString.toString().getBytes("UTF-8"));
BufferedInputStream bis = new BufferedInputStream(inStream);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate cert = cf.generateCertificate(bis);
X509Certificate xCert = (X509Certificate)cert;

System.out.println("Certificate Type: "+cert.getType());
System.out.println("Public Key: \n"+cert.getPublicKey());
try{
      System.out.println("Signature Algorithm"+xCert.getSigAlgName());
      System.out.println("IssuerDN : "+xCert.getIssuerDN());
      System.out.println("Serial Number : "+xCert.getSerialNumber());
      System.out.println("SubjectDN : "+xCert.getSubjectDN());
}catch(Exception exp){
      :
}

like image 142
noquery Avatar answered Oct 04 '22 16:10

noquery


If you are having the PFX file, then that may contain the public key certificate which will be required to verify the signature.

Alternatively, if your signature is a PKCS#7 signature, then the signature itself will hold the data, signature and the certificate. Assuming PKCS#7 is not detached.

You need to ask your signer, how is he transferring his certificate for validation.

like image 43
Raj Avatar answered Oct 04 '22 14:10

Raj