View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * Helps to implement a custom {@link TrustManagerFactory}.
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       * {@link SimpleTrustManagerFactorySpi} must have a reference to {@link SimpleTrustManagerFactory}
44       * to delegate its callbacks back to {@link SimpleTrustManagerFactory}.  However, it is impossible to do so,
45       * because {@link TrustManagerFactory} requires {@link TrustManagerFactorySpi} at construction time and
46       * does not provide a way to access it later.
47       *
48       * To work around this issue, we use an ugly hack which uses a {@link ThreadLocal}.
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       * Creates a new instance.
60       */
61      protected SimpleTrustManagerFactory() {
62          this("");
63      }
64  
65      /**
66       * Creates a new instance.
67       *
68       * @param name the name of this {@link TrustManagerFactory}
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       * Initializes this factory with a source of certificate authorities and related trust material.
80       *
81       * @see TrustManagerFactorySpi#engineInit(KeyStore)
82       */
83      protected abstract void engineInit(KeyStore keyStore) throws Exception;
84  
85      /**
86       * Initializes this factory with a source of provider-specific key material.
87       *
88       * @see TrustManagerFactorySpi#engineInit(ManagerFactoryParameters)
89       */
90      protected abstract void engineInit(ManagerFactoryParameters managerFactoryParameters) throws Exception;
91  
92      /**
93       * Returns one trust manager for each type of trust material.
94       *
95       * @see TrustManagerFactorySpi#engineGetTrustManagers()
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 }