1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.handler.ssl;
17
18 import io.netty5.util.internal.ObjectUtil;
19
20 import javax.net.ssl.KeyManager;
21 import javax.net.ssl.KeyManagerFactory;
22 import javax.net.ssl.KeyManagerFactorySpi;
23 import javax.net.ssl.ManagerFactoryParameters;
24 import javax.net.ssl.X509KeyManager;
25 import java.security.InvalidAlgorithmParameterException;
26 import java.security.KeyStore;
27 import java.security.KeyStoreException;
28 import java.security.NoSuchAlgorithmException;
29 import java.security.PrivateKey;
30 import java.security.UnrecoverableKeyException;
31 import java.security.cert.X509Certificate;
32
33
34
35
36
37
38
39
40 public final class OpenSslCachingX509KeyManagerFactory extends KeyManagerFactory {
41
42 private final int maxCachedEntries;
43
44 public OpenSslCachingX509KeyManagerFactory(final KeyManagerFactory factory) {
45 this(factory, 1024);
46 }
47
48 public OpenSslCachingX509KeyManagerFactory(final KeyManagerFactory factory, int maxCachedEntries) {
49 super(new KeyManagerFactorySpi() {
50 @Override
51 protected void engineInit(KeyStore keyStore, char[] chars)
52 throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
53 factory.init(keyStore, chars);
54 }
55
56 @Override
57 protected void engineInit(ManagerFactoryParameters managerFactoryParameters)
58 throws InvalidAlgorithmParameterException {
59 factory.init(managerFactoryParameters);
60 }
61
62 @Override
63 protected KeyManager[] engineGetKeyManagers() {
64 return factory.getKeyManagers();
65 }
66 }, factory.getProvider(), factory.getAlgorithm());
67 this.maxCachedEntries = ObjectUtil.checkPositive(maxCachedEntries, "maxCachedEntries");
68 }
69
70 OpenSslKeyMaterialProvider newProvider(String password) {
71 X509KeyManager keyManager = ReferenceCountedOpenSslContext.chooseX509KeyManager(getKeyManagers());
72 if ("sun.security.ssl.X509KeyManagerImpl".equals(keyManager.getClass().getName())) {
73
74
75 return new OpenSslKeyMaterialProvider(keyManager, password);
76 }
77 return new OpenSslCachingKeyMaterialProvider(
78 ReferenceCountedOpenSslContext.chooseX509KeyManager(getKeyManagers()), password, maxCachedEntries);
79 }
80 }