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.PlatformDependent;
21
22 import javax.net.ssl.ManagerFactoryParameters;
23 import javax.net.ssl.TrustManager;
24 import javax.net.ssl.TrustManagerFactory;
25 import javax.net.ssl.TrustManagerFactorySpi;
26 import javax.net.ssl.X509ExtendedTrustManager;
27 import javax.net.ssl.X509TrustManager;
28 import java.security.InvalidAlgorithmParameterException;
29 import java.security.KeyStore;
30 import java.security.KeyStoreException;
31 import java.security.Provider;
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 if (name == null) {
76 throw new NullPointerException("name");
77 }
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 for (int i = 0; i < trustManagers.length; i++) {
140 final TrustManager tm = trustManagers[i];
141 if (tm instanceof X509TrustManager && !(tm instanceof X509ExtendedTrustManager)) {
142 trustManagers[i] = new X509TrustManagerWrapper((X509TrustManager) tm);
143 }
144 }
145 }
146 this.trustManagers = trustManagers;
147 }
148 return trustManagers.clone();
149 }
150 }
151 }