Commit d05ef795 authored by Mickaël Guessant's avatar Mickaël Guessant

Smartcard: try to implement code compatible with all java versions including Java 9

git-svn-id: https://svn.code.sf.net/p/davmail/code/trunk@2493 3d1905a2-6b24-0410-a738-b14d5a86fcbd
parent cc213415
......@@ -21,6 +21,11 @@ package davmail.http;
import sun.security.pkcs11.SunPKCS11;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.security.Provider;
import java.security.Security;
......@@ -38,8 +43,39 @@ public final class SunPKCS11ProviderHandler {
* @param pkcs11Config PKCS11 config string
*/
public static void registerProvider(String pkcs11Config) {
Provider p = new SunPKCS11(new ByteArrayInputStream(pkcs11Config.getBytes()));
Provider p = null;
Class<SunPKCS11> sunPkcs11Class = sun.security.pkcs11.SunPKCS11.class;
try {
Constructor<SunPKCS11> sunPkcs11Constructor = sunPkcs11Class.getDeclaredConstructor(InputStream.class);
p = sunPkcs11Constructor.newInstance(new ByteArrayInputStream(pkcs11Config.getBytes("UTF-8")));
} catch (Exception e) {
// try java 9 configuration
p = configurePkcs11Provider(pkcs11Config);
}
Security.addProvider(p);
}
private static Provider configurePkcs11Provider(String pkcs11Config) {
Provider p;
File file = null;
try {
file = File.createTempFile("pkcs11", "config");
FileOutputStream fos = new FileOutputStream(file);
fos.write(pkcs11Config.getBytes("UTF-8"));
fos.close();
p = Security.getProvider("SunPKCS11");
Method configureMethod = Provider.class.getDeclaredMethod("configure", String.class);
configureMethod.invoke(p, file.getAbsolutePath());
} catch (Exception e) {
throw new RuntimeException("Unable to configure SunPKCS11 provider");
} finally {
if (file != null) {
file.delete();
}
}
return p;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment