How can I read the certificate from a signed Java archive (.jar file)?

x x

To make this sample work, put the function into an applet and the applet into a signed .jar file. Replace the code for the URL, jar file name and classfile name.

The function will not work unless the applet is called via http (i.e., it doesn't work locally). Tested only with the plugin 1.3 on Microsoft platforms.

public void ReadJar(){

    try{
       	    URL urlSource = null;
	    // Make sure you don't forget the "/" at the end of the following line:
            String sURL = "jar:http://your.domain.xx/yourdirectory/YourJar.jar!/";
            JarURLConnection jarConn = null;
            JarFile jf = null;
            try{
                urlSource = new URL(sURL);
            }
            catch(MalformedURLException exp){
                System.out.println("Malformed URL: " + urlSource + "!");
            }
            try{
                jarConn = (JarURLConnection)urlSource.openConnection();
            }
            catch(IOException exp){
                System.out.println("IOException while opening connection: return.");
                return;
            }
            try{
                jf = jarConn.getJarFile();
            }
            catch(IOException exp){
                System.out.println("IOException while getting JarFile: return.");
                return;
            }
        	System.out.println("Got JarFile");
        	Enumeration enum = jf.entries();
        	System.out.println("Got Entries");

        	while(enum.hasMoreElements()){
        	    System.out.println("---------------------------------------");
        	    JarEntry je = (JarEntry)enum.nextElement();
        	    if(je == null){
            	    System.out.println("Entry is null!");
        	    }
        	    else{
        	        String sEntry = je.getName();
        	        System.out.println("Entry is " + sEntry);
        	        if(sEntry.equalsIgnoreCase("YourClass.class")){
	                        InputStream is = jf.getInputStream(je);
        	                System.out.println("Got InputStream");
                	        int c;
                        	while ((c = is.read()) != -1) {
				    //do nothing here, just to make sure the whole stream is read
        	                }
                	        System.out.println("Finished reading!");
            	        	Certificate[] certs = je.getCertificates();
	                        if(certs == null){
        	                    System.out.println("Certificates are null!");
                	        }
                        	else{
	                            System.out.println("Got Certificate(s)!");
        	                    try{
                	                byte[] enc = certs[0].getEncoded();
                        	        System.out.println("Encoded: " + new String(enc));
	                            }
        	                    catch(CertificateEncodingException exp){
                	                System.out.println("CertificateEncodingException while getEncoded()");
                        	    }

	                            PublicKey pKey = certs[0].getPublicKey();
        	                    Long sVer = new Long(pKey.serialVersionUID);
                	            System.out.println("SerialVersion of Public Key: " + sVer.toString());
                        	    System.out.println("Format of Public Key: " + pKey.getFormat());
	                            System.out.println("Algorithm of Public Key: " + pKey.getAlgorithm());
        	                    System.out.println("Type of Certificate: " + certs[0].getType());
                	            System.out.println("---------------------------------------");
                        	    System.out.println("Whole Certificate as String:");
	                            System.out.println("" + certs[0].toString());
        	                    System.out.println("---------------------------------------");
                	        }
	                    }//end if .class
        	    }//end else
        	}//end while
	}//end try
	catch(IOException exp){
		System.out.println("IOException: " + exp.getMessage());
	}
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact