/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ellectra.certhelper;

import java.security.cert.*;
import java.io.*;
import java.security.Principal;
import java.util.Date;
/**
 *
 * @author tdi
 */
public class certreader {
    String name="";
    String surname="";
    public boolean ReadCert(String szCert)
    {
         try
         {
         InputStream inStream = new FileInputStream(szCert);
         CertificateFactory cf = CertificateFactory.getInstance( "X.509" );
         X509Certificate cert = (X509Certificate )cf.generateCertificate( inStream );
         inStream.close() ;
          
        try
        {
         
            cert.checkValidity(new Date());
         
        }
        catch (Exception e)
        {
            return false;
        }
         Principal dd=cert.getSubjectDN();
        
         
         String dall=dd.getName();
         int icn=dall.indexOf("CN=", 0);
         int iou=dall.indexOf("OU=", 0);
         String dcn=dall.substring(icn, iou);
         dcn=dcn.substring(3, dcn.length()-2);
         int ispace=dcn.indexOf(" ");
         name=dcn.substring(0, ispace);
         surname=dcn.substring(ispace+1, dcn.length());
         name=name.trim();
         surname=surname.trim();
        
         return true;
         }
      catch ( CertificateException cex )
         {
        return false;
         }
      catch ( IOException ioe )
         {
         return false;
         }
    }
    
    public String GetName()
    {
        return name;
    }
    
    public String GetSurname()
    {
        return surname;
    }
}
