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    *   http://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.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   * 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          if (name == null) {
76              throw new NullPointerException("name");
77          }
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                     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 }