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  
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   * 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<SimpleTrustManagerFactorySpi>() {
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          ObjectUtil.checkNotNull(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                 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 }