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  
21  import javax.net.ssl.ManagerFactoryParameters;
22  import javax.net.ssl.TrustManager;
23  import javax.net.ssl.TrustManagerFactory;
24  import javax.net.ssl.TrustManagerFactorySpi;
25  import java.security.InvalidAlgorithmParameterException;
26  import java.security.KeyStore;
27  import java.security.KeyStoreException;
28  import java.security.Provider;
29  
30  /**
31   * Helps to implement a custom {@link TrustManagerFactory}.
32   */
33  public abstract class SimpleTrustManagerFactory extends TrustManagerFactory {
34  
35      private static final Provider PROVIDER = new Provider("", 0.0, "") {
36          private static final long serialVersionUID = -2680540247105807895L;
37      };
38  
39      /**
40       * {@link SimpleTrustManagerFactorySpi} must have a reference to {@link SimpleTrustManagerFactory}
41       * to delegate its callbacks back to {@link SimpleTrustManagerFactory}.  However, it is impossible to do so,
42       * because {@link TrustManagerFactory} requires {@link TrustManagerFactorySpi} at construction time and
43       * does not provide a way to access it later.
44       *
45       * To work around this issue, we use an ugly hack which uses a {@link ThreadLocal}.
46       */
47      private static final FastThreadLocal<SimpleTrustManagerFactorySpi> CURRENT_SPI =
48              new FastThreadLocal<SimpleTrustManagerFactorySpi>() {
49                  @Override
50                  protected SimpleTrustManagerFactorySpi initialValue() {
51                      return new SimpleTrustManagerFactorySpi();
52                  }
53              };
54  
55      /**
56       * Creates a new instance.
57       */
58      protected SimpleTrustManagerFactory() {
59          this("");
60      }
61  
62      /**
63       * Creates a new instance.
64       *
65       * @param name the name of this {@link TrustManagerFactory}
66       */
67      protected SimpleTrustManagerFactory(String name) {
68          super(CURRENT_SPI.get(), PROVIDER, name);
69          CURRENT_SPI.get().init(this);
70          CURRENT_SPI.remove();
71  
72          if (name == null) {
73              throw new NullPointerException("name");
74          }
75      }
76  
77      /**
78       * Initializes this factory with a source of certificate authorities and related trust material.
79       *
80       * @see TrustManagerFactorySpi#engineInit(KeyStore)
81       */
82      protected abstract void engineInit(KeyStore keyStore) throws Exception;
83  
84      /**
85       * Initializes this factory with a source of provider-specific key material.
86       *
87       * @see TrustManagerFactorySpi#engineInit(ManagerFactoryParameters)
88       */
89      protected abstract void engineInit(ManagerFactoryParameters managerFactoryParameters) throws Exception;
90  
91      /**
92       * Returns one trust manager for each type of trust material.
93       *
94       * @see TrustManagerFactorySpi#engineGetTrustManagers()
95       */
96      protected abstract TrustManager[] engineGetTrustManagers();
97  
98      static final class SimpleTrustManagerFactorySpi extends TrustManagerFactorySpi {
99  
100         private SimpleTrustManagerFactory parent;
101 
102         void init(SimpleTrustManagerFactory parent) {
103             this.parent = parent;
104         }
105 
106         @Override
107         protected void engineInit(KeyStore keyStore) throws KeyStoreException {
108             try {
109                 parent.engineInit(keyStore);
110             } catch (KeyStoreException e) {
111                 throw e;
112             } catch (Exception e) {
113                 throw new KeyStoreException(e);
114             }
115         }
116 
117         @Override
118         protected void engineInit(
119                 ManagerFactoryParameters managerFactoryParameters) throws InvalidAlgorithmParameterException {
120             try {
121                 parent.engineInit(managerFactoryParameters);
122             } catch (InvalidAlgorithmParameterException e) {
123                 throw e;
124             } catch (Exception e) {
125                 throw new InvalidAlgorithmParameterException(e);
126             }
127         }
128 
129         @Override
130         protected TrustManager[] engineGetTrustManagers() {
131             return parent.engineGetTrustManagers();
132         }
133     }
134 }