1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package davmail.http;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.InputStream;
23 import java.lang.reflect.Constructor;
24 import java.lang.reflect.Method;
25 import java.nio.charset.StandardCharsets;
26 import java.security.AuthProvider;
27 import java.security.Provider;
28 import java.security.Security;
29
30
31
32
33 public final class SunPKCS11ProviderHandler {
34
35 private SunPKCS11ProviderHandler() {
36 }
37
38
39
40
41
42
43 public static void registerProvider(String pkcs11Config) {
44 Provider p;
45
46 try {
47 @SuppressWarnings("unchecked") Class<AuthProvider> sunPkcs11Class = (Class<AuthProvider>) Class.forName("sun.security.pkcs11.SunPKCS11");
48 Constructor<AuthProvider> sunPkcs11Constructor = sunPkcs11Class.getDeclaredConstructor(InputStream.class);
49 p = sunPkcs11Constructor.newInstance(new ByteArrayInputStream(pkcs11Config.getBytes(StandardCharsets.UTF_8)));
50 } catch (NoSuchMethodException e) {
51
52 p = configurePkcs11Provider(pkcs11Config);
53 } catch (Exception e) {
54 throw new PKCS11ProviderException(buildErrorMessage(e));
55 }
56
57 Security.addProvider(p);
58 }
59
60 private static Provider configurePkcs11Provider(String pkcs11Config) {
61 Provider p;
62 try {
63 p = Security.getProvider("SunPKCS11");
64
65 Method configureMethod = Provider.class.getDeclaredMethod("configure", String.class);
66 configureMethod.invoke(p, "--"+pkcs11Config);
67 } catch (Exception e) {
68 throw new PKCS11ProviderException(buildErrorMessage(e));
69 }
70 return p;
71 }
72
73 private static String buildErrorMessage(Exception e) {
74 StringBuilder errorMessage = new StringBuilder("Unable to configure SunPKCS11 provider");
75 Throwable cause = e.getCause();
76 while (cause != null) {
77 errorMessage.append(" ").append(cause.getMessage());
78 cause = cause.getCause();
79 }
80 return errorMessage.toString();
81 }
82
83 static final class PKCS11ProviderException extends RuntimeException {
84 public PKCS11ProviderException(String message) {
85 super(message);
86 }
87 }
88
89 }