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