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  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   * Helps to implement a custom {@link TrustManagerFactory}.
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       * {@link SimpleTrustManagerFactorySpi} must have a reference to {@link SimpleTrustManagerFactory}
46       * to delegate its callbacks back to {@link SimpleTrustManagerFactory}.  However, it is impossible to do so,
47       * because {@link TrustManagerFactory} requires {@link TrustManagerFactorySpi} at construction time and
48       * does not provide a way to access it later.
49       *
50       * To work around this issue, we use an ugly hack which uses a {@link ThreadLocal}.
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       * Creates a new instance.
62       */
63      protected SimpleTrustManagerFactory() {
64          this("");
65      }
66  
67      /**
68       * Creates a new instance.
69       *
70       * @param name the name of this {@link TrustManagerFactory}
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       * Initializes this factory with a source of certificate authorities and related trust material.
82       *
83       * @see TrustManagerFactorySpi#engineInit(KeyStore)
84       */
85      protected abstract void engineInit(KeyStore keyStore) throws Exception;
86  
87      /**
88       * Initializes this factory with a source of provider-specific key material.
89       *
90       * @see TrustManagerFactorySpi#engineInit(ManagerFactoryParameters)
91       */
92      protected abstract void engineInit(ManagerFactoryParameters managerFactoryParameters) throws Exception;
93  
94      /**
95       * Returns one trust manager for each type of trust material.
96       *
97       * @see TrustManagerFactorySpi#engineGetTrustManagers()
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 }