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  package io.netty5.handler.ssl.util;
17  
18  import io.netty5.util.concurrent.FastThreadLocal;
19  import io.netty5.util.internal.StringUtil;
20  
21  import javax.net.ssl.KeyManager;
22  import javax.net.ssl.KeyManagerFactory;
23  import javax.net.ssl.KeyManagerFactorySpi;
24  import javax.net.ssl.ManagerFactoryParameters;
25  import javax.net.ssl.X509ExtendedKeyManager;
26  import javax.net.ssl.X509KeyManager;
27  import java.security.InvalidAlgorithmParameterException;
28  import java.security.KeyStore;
29  import java.security.KeyStoreException;
30  import java.security.Provider;
31  import java.util.Objects;
32  
33  /**
34   * Helps to implement a custom {@link KeyManagerFactory}.
35   */
36  public abstract class SimpleKeyManagerFactory extends KeyManagerFactory {
37  
38      private static final Provider PROVIDER = new Provider("", "0.0", "") {
39          private static final long serialVersionUID = -2680540247105807895L;
40      };
41  
42      /**
43       * {@link SimpleKeyManagerFactorySpi} must have a reference to {@link SimpleKeyManagerFactory}
44       * to delegate its callbacks back to {@link SimpleKeyManagerFactory}.  However, it is impossible to do so,
45       * because {@link KeyManagerFactory} requires {@link KeyManagerFactorySpi} 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 FastThreadLocal }.
49       */
50      private static final FastThreadLocal<SimpleKeyManagerFactorySpi> CURRENT_SPI =
51              new FastThreadLocal<>() {
52                  @Override
53                  protected SimpleKeyManagerFactorySpi initialValue() {
54                      return new SimpleKeyManagerFactorySpi();
55                  }
56              };
57  
58      /**
59       * Creates a new instance.
60       */
61      protected SimpleKeyManagerFactory() {
62          this(StringUtil.EMPTY_STRING);
63      }
64  
65      /**
66       * Creates a new instance.
67       *
68       * @param name the name of this {@link KeyManagerFactory}
69       */
70      protected SimpleKeyManagerFactory(String name) {
71          super(CURRENT_SPI.get(), PROVIDER, Objects.requireNonNull(name, "name"));
72          CURRENT_SPI.get().init(this);
73          CURRENT_SPI.remove();
74      }
75  
76      /**
77       * Initializes this factory with a source of certificate authorities and related key material.
78       *
79       * @see KeyManagerFactorySpi#engineInit(KeyStore, char[])
80       */
81      protected abstract void engineInit(KeyStore keyStore, char[] var2) throws Exception;
82  
83      /**
84       * Initializes this factory with a source of provider-specific key material.
85       *
86       * @see KeyManagerFactorySpi#engineInit(ManagerFactoryParameters)
87       */
88      protected abstract void engineInit(ManagerFactoryParameters managerFactoryParameters) throws Exception;
89  
90      /**
91       * Returns one key manager for each type of key material.
92       *
93       * @see KeyManagerFactorySpi#engineGetKeyManagers()
94       */
95      protected abstract KeyManager[] engineGetKeyManagers();
96  
97      private static final class SimpleKeyManagerFactorySpi extends KeyManagerFactorySpi {
98  
99          private SimpleKeyManagerFactory parent;
100         private volatile KeyManager[] keyManagers;
101 
102         void init(SimpleKeyManagerFactory parent) {
103             this.parent = parent;
104         }
105 
106         @Override
107         protected void engineInit(KeyStore keyStore, char[] pwd) throws KeyStoreException {
108             try {
109                 parent.engineInit(keyStore, pwd);
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 KeyManager[] engineGetKeyManagers() {
131             KeyManager[] keyManagers = this.keyManagers;
132             if (keyManagers == null) {
133                 keyManagers = parent.engineGetKeyManagers();
134                 wrapIfNeeded(keyManagers);
135                 this.keyManagers = keyManagers;
136             }
137             return keyManagers.clone();
138         }
139 
140         private static void wrapIfNeeded(KeyManager[] keyManagers) {
141             for (int i = 0; i < keyManagers.length; i++) {
142                 final KeyManager tm = keyManagers[i];
143                 if (tm instanceof X509KeyManager && !(tm instanceof X509ExtendedKeyManager)) {
144                     keyManagers[i] = new X509KeyManagerWrapper((X509KeyManager) tm);
145                 }
146             }
147         }
148     }
149 }