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