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.util.jsse;
018
019import java.security.GeneralSecurityException;
020import java.util.ArrayList;
021import java.util.Collections;
022import java.util.List;
023
024import javax.net.ssl.SNIHostName;
025import javax.net.ssl.SNIServerName;
026import javax.net.ssl.SSLContext;
027import javax.net.ssl.SSLEngine;
028import javax.net.ssl.SSLServerSocketFactory;
029
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033/**
034 * Configuration model for client side JSSE options.
035 */
036public class SSLContextClientParameters extends BaseSSLContextParameters {
037    
038    private static final Logger LOG = LoggerFactory.getLogger(SSLContextClientParameters.class);
039
040    private List<SNIServerName> sniHostNames = new ArrayList<>();
041
042    public void addAllSniHostNames(List<String> sniHostNames) {
043        for (String sniHostName : sniHostNames) {
044            this.sniHostNames.add(new SNIHostName(sniHostName));
045        }
046    }
047
048    public void setSniHostName(String sniHostName) {
049        this.sniHostNames.add(new SNIHostName(sniHostName));
050    }
051
052    @Override
053    protected List<SNIServerName> getSNIHostNames() {
054        return sniHostNames;
055    }
056
057    @Override
058    protected boolean getAllowPassthrough() {
059        return true;
060    }
061
062    @Override
063    protected void configureSSLContext(SSLContext context) throws GeneralSecurityException {
064        LOG.trace("Configuring client-side SSLContext parameters on SSLContext [{}]...", context);
065        if (this.getSessionTimeout() != null) {
066            LOG.info("Configuring client-side SSLContext session timeout on SSLContext [{}] to [{}].", context, this.getSessionTimeout());
067            this.configureSessionContext(context.getClientSessionContext(), this.getSessionTimeout());
068        }
069        LOG.trace("Configured client-side SSLContext parameters on SSLContext [{}].", context);
070    }
071
072    /**
073     * {@inheritDoc}
074     * <p/>
075     * This implementation returns the empty list as the enabled cipher suites
076     * and protocols are not client and server side specific in an
077     * {@code SSLEngine}. Consequently, overriding them here would be a bit odd
078     * as the client side specific configuration shouldn't really override a
079     * shared client/server configuration option.
080     */
081    @Override
082    protected List<Configurer<SSLEngine>> getSSLEngineConfigurers(SSLContext context) {
083        // NOTE: if the super class gets additional shared configuration options beyond
084        // cipher suites and protocols, this method needs to address that.
085        return Collections.emptyList();
086    }
087    
088    /**
089     * This class has no bearing on {@code SSLServerSocketFactory} instances and therefore provides no
090     * configurers for that purpose.
091     */
092    @Override
093    protected List<Configurer<SSLServerSocketFactory>> getSSLServerSocketFactoryConfigurers(SSLContext context) {
094        return Collections.emptyList();
095    }
096
097    @Override
098    public String toString() {
099        StringBuilder builder = new StringBuilder();
100        builder.append("SSLContextClientParameters[getCipherSuites()=");
101        builder.append(getCipherSuites());
102        builder.append(", getCipherSuitesFilter()=");
103        builder.append(getCipherSuitesFilter());
104        builder.append(", getSecureSocketProtocols()=");
105        builder.append(getSecureSocketProtocols());
106        builder.append(", getSecureSocketProtocolsFilter()=");
107        builder.append(getSecureSocketProtocolsFilter());
108        builder.append(", getSessionTimeout()=");
109        builder.append(getSessionTimeout());
110        builder.append("]");
111        return builder.toString();
112    }
113}