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