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