0

I have a java code that executed successfully but JVM doesn't exit even after adding system.exit(0);

class GetSignatureNew {

    public static void main (String args[])
    {
        String pwd = "*******";
        char[] pin = new char[pwd.length()]; 
        try
        {
            for (int i = 0; i < pwd.length(); i++) {
                pin[i] = pwd.charAt(i);
            }
            // Get Certificate and private key from token
            KeyStore ks = KeyStore.getInstance("PKCS11");
            ks.load(null, pin);
            Enumeration enu = ks.aliases();
            String alias = String.valueOf(enu.nextElement());           
            X509Certificate cert =  (X509Certificate) ks.getCertificate(alias);
            PrivateKey pk = (PrivateKey) ks.getKey(alias, pin);
            byte [] output = cert.getEncoded();         
            String b64 = Base64.getEncoder().encodeToString(output);
            System.out.println(b64);
            System.exit(0);


        }
        catch (Exception e) {
            e.printStackTrace(); 
            System.out.println("Error");
        }
    }
}

Code should Terminated after execution

  • 1
    Sorry, I don't get it. Is **is** executed successfully but **also** not terminated? What does that mean? Does it hang and if so, on what line? Did you step through the code with a debugger? What's in the PKCS#11 config file? – Maarten Bodewes Dec 08 '22 at 23:33
  • OK, thanks for the edit. So the code does complete but the JVM doesn't exit? That might be due to some issue with a DLL that gets initialized & called. – Maarten Bodewes Dec 08 '22 at 23:36
  • It's print certificate but the session still opened. because the terminate button in eclipse is enabled and i have to press to terminate code. and this behavior only happened after adding provider dynamically – Sameh Sewilam Dec 08 '22 at 23:36
  • thanks for your reply, do you have any solution or workaround for this. – Sameh Sewilam Dec 08 '22 at 23:38
  • This is probably specific to the PKCS#11 library used. I cannot remember seeing that with the PKCS#11 libs that I've used, but tbh, that was some time ago. I'm afraid that you will have to share details on it and hope that somebody has seen the same response. – Maarten Bodewes Dec 08 '22 at 23:38
  • if i hash all the code and only this line Provider p = new sun.security.pkcs11.SunPKCS11(configName); in the code the JVM not terminated. – Sameh Sewilam Dec 08 '22 at 23:40
  • Yeah, that makes sense, that line loads the native DLLs (or `.so` on Linux etc.) That will probably call some entry point in the DLL which somehow prevents process exit. However, to try and find out more you have to share information on your environment. – Maarten Bodewes Dec 08 '22 at 23:44

1 Answers1

1

Since you're adding the provider dynamically (vs. statically), try adding Security.removeProvider().

Here is the complete documentation:

https://docs.oracle.com/javase/8/docs/technotes/guides/security/p11guide.html

paulsm4
  • 114,292
  • 17
  • 138
  • 190