Skip to content

Commit

Permalink
PKCS11: review SunPKCS11ProviderHandler
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.code.sf.net/p/davmail/code/trunk@3571 3d1905a2-6b24-0410-a738-b14d5a86fcbd
  • Loading branch information
mguessan committed May 28, 2024
1 parent 689ffc3 commit ae9b444
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/java/davmail/http/SunPKCS11ProviderHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.security.AuthProvider;
import java.security.Provider;
import java.security.Security;

Expand All @@ -43,20 +44,14 @@ public static void registerProvider(String pkcs11Config) {
Provider p;

try {
Class sunPkcs11Class = Class.forName("sun.security.pkcs11.SunPKCS11");
@SuppressWarnings("unchecked") Constructor sunPkcs11Constructor = sunPkcs11Class.getDeclaredConstructor(InputStream.class);
p = (Provider) sunPkcs11Constructor.newInstance(new ByteArrayInputStream(pkcs11Config.getBytes(StandardCharsets.UTF_8)));
@SuppressWarnings("unchecked") Class<AuthProvider> sunPkcs11Class = (Class<AuthProvider>) Class.forName("sun.security.pkcs11.SunPKCS11");
Constructor<AuthProvider> sunPkcs11Constructor = sunPkcs11Class.getDeclaredConstructor(InputStream.class);
p = sunPkcs11Constructor.newInstance(new ByteArrayInputStream(pkcs11Config.getBytes(StandardCharsets.UTF_8)));
} catch (NoSuchMethodException e) {
// try java 9 configuration
p = configurePkcs11Provider(pkcs11Config);
} catch (Exception e) {
StringBuilder errorMessage = new StringBuilder("Unable to configure SunPKCS11 provider");
Throwable cause = e.getCause();
while (cause != null) {
errorMessage.append(" ").append(cause.getMessage());
cause = cause.getCause();
}
throw new RuntimeException(errorMessage.toString());
throw new PKCS11ProviderException(buildErrorMessage(e));
}

Security.addProvider(p);
Expand All @@ -66,20 +61,29 @@ private static Provider configurePkcs11Provider(String pkcs11Config) {
Provider p;
try {
p = Security.getProvider("SunPKCS11");
//p.configure("--"+pkcs11Config);
//noinspection JavaReflectionMemberAccess new Java 9 configure method
// new Java 9 configure method
Method configureMethod = Provider.class.getDeclaredMethod("configure", String.class);
configureMethod.invoke(p, "--"+pkcs11Config);
} catch (Exception e) {
StringBuilder errorMessage = new StringBuilder("Unable to configure SunPKCS11 provider");
Throwable cause = e.getCause();
while (cause != null) {
errorMessage.append(" ").append(cause.getMessage());
cause = cause.getCause();
}
throw new RuntimeException(errorMessage.toString());
throw new PKCS11ProviderException(buildErrorMessage(e));
}
return p;
}

private static String buildErrorMessage(Exception e) {
StringBuilder errorMessage = new StringBuilder("Unable to configure SunPKCS11 provider");
Throwable cause = e.getCause();
while (cause != null) {
errorMessage.append(" ").append(cause.getMessage());
cause = cause.getCause();
}
return errorMessage.toString();
}

static final class PKCS11ProviderException extends RuntimeException {
public PKCS11ProviderException(String message) {
super(message);
}
}

}

0 comments on commit ae9b444

Please sign in to comment.