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    public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket) {
049        return keyAlias == null ? keyManager.chooseClientAlias(keyType, issuers, socket) : keyAlias;
050    }
051
052    public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket) {
053        return keyAlias == null ? keyManager.chooseServerAlias(keyType, issuers, socket) : keyAlias;
054    }
055
056    public String[] getClientAliases(String keyType, Principal[] issuers) {
057        return keyManager.getClientAliases(keyType, issuers);
058    }
059
060    public String[] getServerAliases(String keyType, Principal[] issuers) {
061        return keyManager.getServerAliases(keyType, issuers);
062    }
063
064    public X509Certificate[] getCertificateChain(String alias) {
065        return keyManager.getCertificateChain(alias);
066    }
067
068    public PrivateKey getPrivateKey(String alias) {
069        return keyManager.getPrivateKey(alias);
070    }
071
072    @Override
073    public String chooseEngineServerAlias(String keyType, Principal[] issuers, SSLEngine engine) {
074        return keyAlias == null ? super.chooseEngineServerAlias(keyType, issuers, engine) : keyAlias;
075    }
076
077    @Override
078    public String chooseEngineClientAlias(String keyType[], Principal[] issuers, SSLEngine engine) {
079        return keyAlias == null ? super.chooseEngineClientAlias(keyType, issuers, engine) : keyAlias;
080    }
081}