|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * 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, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.ssl; |
| 18 | + |
| 19 | +import java.net.Socket; |
| 20 | +import java.security.InvalidAlgorithmParameterException; |
| 21 | +import java.security.KeyStore; |
| 22 | +import java.security.KeyStoreException; |
| 23 | +import java.security.NoSuchAlgorithmException; |
| 24 | +import java.security.Principal; |
| 25 | +import java.security.PrivateKey; |
| 26 | +import java.security.UnrecoverableKeyException; |
| 27 | +import java.security.cert.X509Certificate; |
| 28 | +import java.util.Arrays; |
| 29 | + |
| 30 | +import javax.net.ssl.KeyManager; |
| 31 | +import javax.net.ssl.KeyManagerFactory; |
| 32 | +import javax.net.ssl.KeyManagerFactorySpi; |
| 33 | +import javax.net.ssl.ManagerFactoryParameters; |
| 34 | +import javax.net.ssl.SSLEngine; |
| 35 | +import javax.net.ssl.X509ExtendedKeyManager; |
| 36 | + |
| 37 | +/** |
| 38 | + * {@link KeyManagerFactory} that allows a configurable key alias to be used. Due to the |
| 39 | + * fact that the actual calls to retrieve the key by alias are done at request time the |
| 40 | + * approach is to wrap the actual key managers with a {@link AliasX509ExtendedKeyManager}. |
| 41 | + * The actual SPI has to be wrapped as well due to the fact that |
| 42 | + * {@link KeyManagerFactory#getKeyManagers()} is final. |
| 43 | + * |
| 44 | + * @author Scott Frederick |
| 45 | + */ |
| 46 | +final class AliasKeyManagerFactory extends KeyManagerFactory { |
| 47 | + |
| 48 | + AliasKeyManagerFactory(KeyManagerFactory delegate, String alias, String algorithm) { |
| 49 | + super(new AliasKeyManagerFactorySpi(delegate, alias), delegate.getProvider(), algorithm); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * {@link KeyManagerFactorySpi} that allows a configurable key alias to be used. |
| 54 | + */ |
| 55 | + private static final class AliasKeyManagerFactorySpi extends KeyManagerFactorySpi { |
| 56 | + |
| 57 | + private final KeyManagerFactory delegate; |
| 58 | + |
| 59 | + private final String alias; |
| 60 | + |
| 61 | + private AliasKeyManagerFactorySpi(KeyManagerFactory delegate, String alias) { |
| 62 | + this.delegate = delegate; |
| 63 | + this.alias = alias; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected void engineInit(KeyStore keyStore, char[] chars) |
| 68 | + throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException { |
| 69 | + this.delegate.init(keyStore, chars); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + protected void engineInit(ManagerFactoryParameters managerFactoryParameters) |
| 74 | + throws InvalidAlgorithmParameterException { |
| 75 | + throw new InvalidAlgorithmParameterException("Unsupported ManagerFactoryParameters"); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + protected KeyManager[] engineGetKeyManagers() { |
| 80 | + return Arrays.stream(this.delegate.getKeyManagers()) |
| 81 | + .filter(X509ExtendedKeyManager.class::isInstance) |
| 82 | + .map(X509ExtendedKeyManager.class::cast) |
| 83 | + .map(this::wrap) |
| 84 | + .toArray(KeyManager[]::new); |
| 85 | + } |
| 86 | + |
| 87 | + private AliasKeyManagerFactory.AliasX509ExtendedKeyManager wrap(X509ExtendedKeyManager keyManager) { |
| 88 | + return new AliasX509ExtendedKeyManager(keyManager, this.alias); |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * {@link X509ExtendedKeyManager} that allows a configurable key alias to be used. |
| 95 | + */ |
| 96 | + static final class AliasX509ExtendedKeyManager extends X509ExtendedKeyManager { |
| 97 | + |
| 98 | + private final X509ExtendedKeyManager delegate; |
| 99 | + |
| 100 | + private final String alias; |
| 101 | + |
| 102 | + private AliasX509ExtendedKeyManager(X509ExtendedKeyManager keyManager, String alias) { |
| 103 | + this.delegate = keyManager; |
| 104 | + this.alias = alias; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public String chooseEngineClientAlias(String[] strings, Principal[] principals, SSLEngine sslEngine) { |
| 109 | + return this.delegate.chooseEngineClientAlias(strings, principals, sslEngine); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String chooseEngineServerAlias(String s, Principal[] principals, SSLEngine sslEngine) { |
| 114 | + return this.alias; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket) { |
| 119 | + return this.delegate.chooseClientAlias(keyType, issuers, socket); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket) { |
| 124 | + return this.delegate.chooseServerAlias(keyType, issuers, socket); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public X509Certificate[] getCertificateChain(String alias) { |
| 129 | + return this.delegate.getCertificateChain(alias); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public String[] getClientAliases(String keyType, Principal[] issuers) { |
| 134 | + return this.delegate.getClientAliases(keyType, issuers); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public PrivateKey getPrivateKey(String alias) { |
| 139 | + return this.delegate.getPrivateKey(alias); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public String[] getServerAliases(String keyType, Principal[] issuers) { |
| 144 | + return this.delegate.getServerAliases(keyType, issuers); |
| 145 | + } |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments