|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.zookeeper.common; |
| 20 | + |
| 21 | +import java.net.Socket; |
| 22 | +import java.security.Principal; |
| 23 | +import java.security.PrivateKey; |
| 24 | +import java.security.cert.X509Certificate; |
| 25 | +import javax.net.ssl.SSLEngine; |
| 26 | +import javax.net.ssl.X509ExtendedKeyManager; |
| 27 | +import javax.net.ssl.X509KeyManager; |
| 28 | + |
| 29 | +/** |
| 30 | + * An {@link X509ExtendedKeyManager} that delegates client-mode and server-mode |
| 31 | + * key selection to separate underlying key managers. This allows a ZooKeeper |
| 32 | + * node to use different keystores (and therefore different certificates) for |
| 33 | + * its client role (outgoing connections) and server role (incoming connections), |
| 34 | + * enabling the use of certificates with a single Extended Key Usage (EKU). |
| 35 | + */ |
| 36 | +public class ClientServerX509KeyManager extends X509ExtendedKeyManager { |
| 37 | + |
| 38 | + private static final String CLIENT_PREFIX = "client:"; |
| 39 | + private static final String SERVER_PREFIX = "server:"; |
| 40 | + |
| 41 | + private final X509KeyManager clientKeyManager; |
| 42 | + private final X509KeyManager serverKeyManager; |
| 43 | + |
| 44 | + public ClientServerX509KeyManager(X509KeyManager clientKeyManager, X509KeyManager serverKeyManager) { |
| 45 | + this.clientKeyManager = clientKeyManager; |
| 46 | + this.serverKeyManager = serverKeyManager; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket) { |
| 51 | + String alias = clientKeyManager.chooseClientAlias(keyType, issuers, socket); |
| 52 | + return alias != null ? CLIENT_PREFIX + alias : null; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket) { |
| 57 | + String alias = serverKeyManager.chooseServerAlias(keyType, issuers, socket); |
| 58 | + return alias != null ? SERVER_PREFIX + alias : null; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public String chooseEngineClientAlias(String[] keyType, Principal[] issuers, SSLEngine engine) { |
| 63 | + if (clientKeyManager instanceof X509ExtendedKeyManager) { |
| 64 | + String alias = ((X509ExtendedKeyManager) clientKeyManager) |
| 65 | + .chooseEngineClientAlias(keyType, issuers, engine); |
| 66 | + return alias != null ? CLIENT_PREFIX + alias : null; |
| 67 | + } |
| 68 | + return chooseClientAlias(keyType, issuers, null); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public String chooseEngineServerAlias(String keyType, Principal[] issuers, SSLEngine engine) { |
| 73 | + if (serverKeyManager instanceof X509ExtendedKeyManager) { |
| 74 | + String alias = ((X509ExtendedKeyManager) serverKeyManager) |
| 75 | + .chooseEngineServerAlias(keyType, issuers, engine); |
| 76 | + return alias != null ? SERVER_PREFIX + alias : null; |
| 77 | + } |
| 78 | + return chooseServerAlias(keyType, issuers, null); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public X509Certificate[] getCertificateChain(String alias) { |
| 83 | + if (alias == null) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + if (alias.startsWith(CLIENT_PREFIX)) { |
| 87 | + return clientKeyManager.getCertificateChain(alias.substring(CLIENT_PREFIX.length())); |
| 88 | + } |
| 89 | + if (alias.startsWith(SERVER_PREFIX)) { |
| 90 | + return serverKeyManager.getCertificateChain(alias.substring(SERVER_PREFIX.length())); |
| 91 | + } |
| 92 | + return serverKeyManager.getCertificateChain(alias); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public PrivateKey getPrivateKey(String alias) { |
| 97 | + if (alias == null) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + if (alias.startsWith(CLIENT_PREFIX)) { |
| 101 | + return clientKeyManager.getPrivateKey(alias.substring(CLIENT_PREFIX.length())); |
| 102 | + } |
| 103 | + if (alias.startsWith(SERVER_PREFIX)) { |
| 104 | + return serverKeyManager.getPrivateKey(alias.substring(SERVER_PREFIX.length())); |
| 105 | + } |
| 106 | + return serverKeyManager.getPrivateKey(alias); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public String[] getClientAliases(String keyType, Principal[] issuers) { |
| 111 | + String[] aliases = clientKeyManager.getClientAliases(keyType, issuers); |
| 112 | + return prefixAliases(aliases, CLIENT_PREFIX); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public String[] getServerAliases(String keyType, Principal[] issuers) { |
| 117 | + String[] aliases = serverKeyManager.getServerAliases(keyType, issuers); |
| 118 | + return prefixAliases(aliases, SERVER_PREFIX); |
| 119 | + } |
| 120 | + |
| 121 | + private static String[] prefixAliases(String[] aliases, String prefix) { |
| 122 | + if (aliases == null) { |
| 123 | + return null; |
| 124 | + } |
| 125 | + String[] prefixed = new String[aliases.length]; |
| 126 | + for (int i = 0; i < aliases.length; i++) { |
| 127 | + prefixed[i] = prefix + aliases[i]; |
| 128 | + } |
| 129 | + return prefixed; |
| 130 | + } |
| 131 | +} |
0 commit comments