001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.support.jsse; 018 019import java.net.Socket; 020import java.security.Principal; 021import java.security.PrivateKey; 022import java.security.cert.X509Certificate; 023 024import javax.net.ssl.SSLEngine; 025import javax.net.ssl.X509ExtendedKeyManager; 026import javax.net.ssl.X509KeyManager; 027 028/** 029 * KeyManager to select a key with desired alias while delegating processing to specified KeyManager Can be 030 * used both with server and client sockets 031 */ 032public class AliasedX509ExtendedKeyManager extends X509ExtendedKeyManager { 033 private String keyAlias; 034 private X509KeyManager keyManager; 035 036 /** 037 * Construct KeyManager instance 038 * 039 * @param keyAlias Alias of the key to be selected 040 * @param keyManager Instance of KeyManager to be wrapped 041 * @throws Exception 042 */ 043 public AliasedX509ExtendedKeyManager(String keyAlias, X509KeyManager keyManager) throws Exception { 044 this.keyAlias = keyAlias; 045 this.keyManager = keyManager; 046 } 047 048 @Override 049 public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket) { 050 return keyAlias == null ? keyManager.chooseClientAlias(keyType, issuers, socket) : keyAlias; 051 } 052 053 @Override 054 public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket) { 055 return keyAlias == null ? keyManager.chooseServerAlias(keyType, issuers, socket) : keyAlias; 056 } 057 058 @Override 059 public String[] getClientAliases(String keyType, Principal[] issuers) { 060 return keyManager.getClientAliases(keyType, issuers); 061 } 062 063 @Override 064 public String[] getServerAliases(String keyType, Principal[] issuers) { 065 return keyManager.getServerAliases(keyType, issuers); 066 } 067 068 @Override 069 public X509Certificate[] getCertificateChain(String alias) { 070 return keyManager.getCertificateChain(alias); 071 } 072 073 @Override 074 public PrivateKey getPrivateKey(String alias) { 075 return keyManager.getPrivateKey(alias); 076 } 077 078 @Override 079 public String chooseEngineServerAlias(String keyType, Principal[] issuers, SSLEngine engine) { 080 return keyAlias == null ? super.chooseEngineServerAlias(keyType, issuers, engine) : keyAlias; 081 } 082 083 @Override 084 public String chooseEngineClientAlias(String[] keyType, Principal[] issuers, SSLEngine engine) { 085 return keyAlias == null ? super.chooseEngineClientAlias(keyType, issuers, engine) : keyAlias; 086 } 087}