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
20 import javax.net.ssl.ManagerFactoryParameters;
21 import javax.net.ssl.TrustManager;
22 import javax.net.ssl.TrustManagerFactory;
23 import javax.net.ssl.TrustManagerFactorySpi;
24 import javax.net.ssl.X509ExtendedTrustManager;
25 import javax.net.ssl.X509TrustManager;
26 import java.security.InvalidAlgorithmParameterException;
27 import java.security.KeyStore;
28 import java.security.KeyStoreException;
29 import java.security.Provider;
30
31 import static java.util.Objects.requireNonNull;
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<>() {
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 requireNonNull(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 for (int i = 0; i < trustManagers.length; i++) {
137 final TrustManager tm = trustManagers[i];
138 if (tm instanceof X509TrustManager && !(tm instanceof X509ExtendedTrustManager)) {
139 trustManagers[i] = new X509TrustManagerWrapper((X509TrustManager) tm);
140 }
141 }
142 this.trustManagers = trustManagers;
143 }
144 return trustManagers.clone();
145 }
146 }
147 }