1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.handler.ssl.util;
17
18 import io.netty5.util.concurrent.FastThreadLocal;
19 import io.netty5.util.internal.StringUtil;
20
21 import javax.net.ssl.KeyManager;
22 import javax.net.ssl.KeyManagerFactory;
23 import javax.net.ssl.KeyManagerFactorySpi;
24 import javax.net.ssl.ManagerFactoryParameters;
25 import javax.net.ssl.X509ExtendedKeyManager;
26 import javax.net.ssl.X509KeyManager;
27 import java.security.InvalidAlgorithmParameterException;
28 import java.security.KeyStore;
29 import java.security.KeyStoreException;
30 import java.security.Provider;
31 import java.util.Objects;
32
33
34
35
36 public abstract class SimpleKeyManagerFactory extends KeyManagerFactory {
37
38 private static final Provider PROVIDER = new Provider("", "0.0", "") {
39 private static final long serialVersionUID = -2680540247105807895L;
40 };
41
42
43
44
45
46
47
48
49
50 private static final FastThreadLocal<SimpleKeyManagerFactorySpi> CURRENT_SPI =
51 new FastThreadLocal<>() {
52 @Override
53 protected SimpleKeyManagerFactorySpi initialValue() {
54 return new SimpleKeyManagerFactorySpi();
55 }
56 };
57
58
59
60
61 protected SimpleKeyManagerFactory() {
62 this(StringUtil.EMPTY_STRING);
63 }
64
65
66
67
68
69
70 protected SimpleKeyManagerFactory(String name) {
71 super(CURRENT_SPI.get(), PROVIDER, Objects.requireNonNull(name, "name"));
72 CURRENT_SPI.get().init(this);
73 CURRENT_SPI.remove();
74 }
75
76
77
78
79
80
81 protected abstract void engineInit(KeyStore keyStore, char[] var2) throws Exception;
82
83
84
85
86
87
88 protected abstract void engineInit(ManagerFactoryParameters managerFactoryParameters) throws Exception;
89
90
91
92
93
94
95 protected abstract KeyManager[] engineGetKeyManagers();
96
97 private static final class SimpleKeyManagerFactorySpi extends KeyManagerFactorySpi {
98
99 private SimpleKeyManagerFactory parent;
100 private volatile KeyManager[] keyManagers;
101
102 void init(SimpleKeyManagerFactory parent) {
103 this.parent = parent;
104 }
105
106 @Override
107 protected void engineInit(KeyStore keyStore, char[] pwd) throws KeyStoreException {
108 try {
109 parent.engineInit(keyStore, pwd);
110 } catch (KeyStoreException e) {
111 throw e;
112 } catch (Exception e) {
113 throw new KeyStoreException(e);
114 }
115 }
116
117 @Override
118 protected void engineInit(
119 ManagerFactoryParameters managerFactoryParameters) throws InvalidAlgorithmParameterException {
120 try {
121 parent.engineInit(managerFactoryParameters);
122 } catch (InvalidAlgorithmParameterException e) {
123 throw e;
124 } catch (Exception e) {
125 throw new InvalidAlgorithmParameterException(e);
126 }
127 }
128
129 @Override
130 protected KeyManager[] engineGetKeyManagers() {
131 KeyManager[] keyManagers = this.keyManagers;
132 if (keyManagers == null) {
133 keyManagers = parent.engineGetKeyManagers();
134 wrapIfNeeded(keyManagers);
135 this.keyManagers = keyManagers;
136 }
137 return keyManagers.clone();
138 }
139
140 private static void wrapIfNeeded(KeyManager[] keyManagers) {
141 for (int i = 0; i < keyManagers.length; i++) {
142 final KeyManager tm = keyManagers[i];
143 if (tm instanceof X509KeyManager && !(tm instanceof X509ExtendedKeyManager)) {
144 keyManagers[i] = new X509KeyManagerWrapper((X509KeyManager) tm);
145 }
146 }
147 }
148 }
149 }