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
22 import java.security.InvalidAlgorithmParameterException;
23 import java.security.KeyStore;
24 import java.security.KeyStoreException;
25 import java.security.Provider;
26 import javax.net.ssl.ManagerFactoryParameters;
27 import javax.net.ssl.TrustManager;
28 import javax.net.ssl.TrustManagerFactory;
29 import javax.net.ssl.TrustManagerFactorySpi;
30 import javax.net.ssl.X509ExtendedTrustManager;
31 import javax.net.ssl.X509TrustManager;
32
33
34
35
36 public abstract class SimpleTrustManagerFactory extends TrustManagerFactory {
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<SimpleTrustManagerFactorySpi> CURRENT_SPI =
51 new FastThreadLocal<SimpleTrustManagerFactorySpi>() {
52 @Override
53 protected SimpleTrustManagerFactorySpi initialValue() {
54 return new SimpleTrustManagerFactorySpi();
55 }
56 };
57
58
59
60
61 protected SimpleTrustManagerFactory() {
62 this("");
63 }
64
65
66
67
68
69
70 protected SimpleTrustManagerFactory(String name) {
71 super(CURRENT_SPI.get(), PROVIDER, name);
72 CURRENT_SPI.get().init(this);
73 CURRENT_SPI.remove();
74
75 ObjectUtil.checkNotNull(name, "name");
76 }
77
78
79
80
81
82
83 protected abstract void engineInit(KeyStore keyStore) throws Exception;
84
85
86
87
88
89
90 protected abstract void engineInit(ManagerFactoryParameters managerFactoryParameters) throws Exception;
91
92
93
94
95
96
97 protected abstract TrustManager[] engineGetTrustManagers();
98
99 static final class SimpleTrustManagerFactorySpi extends TrustManagerFactorySpi {
100
101 private SimpleTrustManagerFactory parent;
102 private volatile TrustManager[] trustManagers;
103
104 void init(SimpleTrustManagerFactory parent) {
105 this.parent = parent;
106 }
107
108 @Override
109 protected void engineInit(KeyStore keyStore) throws KeyStoreException {
110 try {
111 parent.engineInit(keyStore);
112 } catch (KeyStoreException e) {
113 throw e;
114 } catch (Exception e) {
115 throw new KeyStoreException(e);
116 }
117 }
118
119 @Override
120 protected void engineInit(
121 ManagerFactoryParameters managerFactoryParameters) throws InvalidAlgorithmParameterException {
122 try {
123 parent.engineInit(managerFactoryParameters);
124 } catch (InvalidAlgorithmParameterException e) {
125 throw e;
126 } catch (Exception e) {
127 throw new InvalidAlgorithmParameterException(e);
128 }
129 }
130
131 @Override
132 protected TrustManager[] engineGetTrustManagers() {
133 TrustManager[] trustManagers = this.trustManagers;
134 if (trustManagers == null) {
135 trustManagers = parent.engineGetTrustManagers();
136 wrapIfNeeded(trustManagers);
137 this.trustManagers = trustManagers;
138 }
139 return trustManagers.clone();
140 }
141
142 private static void wrapIfNeeded(TrustManager[] trustManagers) {
143 for (int i = 0; i < trustManagers.length; i++) {
144 final TrustManager tm = trustManagers[i];
145 if (tm instanceof X509TrustManager && !(tm instanceof X509ExtendedTrustManager)) {
146 trustManagers[i] = new X509TrustManagerWrapper((X509TrustManager) tm);
147 }
148 }
149 }
150 }
151 }