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.activemq.broker;
018
019import java.security.KeyManagementException;
020import java.security.NoSuchAlgorithmException;
021import java.security.NoSuchProviderException;
022import java.security.SecureRandom;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.List;
026
027import javax.net.ssl.KeyManager;
028import javax.net.ssl.SSLContext;
029import javax.net.ssl.TrustManager;
030
031/**
032 * A holder of SSL configuration.
033 */
034public class SslContext {
035    
036    protected String protocol = "TLS";
037    protected String provider = null;
038    protected List<KeyManager> keyManagers = new ArrayList<KeyManager>();
039    protected List<TrustManager> trustManagers = new ArrayList<TrustManager>();
040    protected SecureRandom secureRandom;
041    private SSLContext sslContext;
042    
043    private static final ThreadLocal<SslContext> current = new ThreadLocal<SslContext>();
044    
045    public SslContext() {
046    }
047    
048    public SslContext(KeyManager[] km, TrustManager[] tm, SecureRandom random) {
049        if( km!=null ) {
050            setKeyManagers(Arrays.asList(km));
051        }
052        if( tm!=null ) {
053            setTrustManagers(Arrays.asList(tm));
054        }
055        setSecureRandom(random);        
056    }
057    
058    static public void setCurrentSslContext(SslContext bs) {
059        current.set(bs);
060    }
061    static public SslContext getCurrentSslContext() {
062        return current.get();
063    }
064    
065    public KeyManager[] getKeyManagersAsArray() {
066        KeyManager rc[] = new KeyManager[keyManagers.size()];
067        return keyManagers.toArray(rc);
068    }
069    public TrustManager[] getTrustManagersAsArray() {
070        TrustManager rc[] = new TrustManager[trustManagers.size()];
071        return trustManagers.toArray(rc);
072    }
073    
074    public void addKeyManager(KeyManager km) {
075        keyManagers.add(km);
076    }
077    public boolean removeKeyManager(KeyManager km) {
078        return keyManagers.remove(km);
079    }
080    public void addTrustManager(TrustManager tm) {
081        trustManagers.add(tm);
082    }
083    public boolean removeTrustManager(TrustManager tm) {
084        return trustManagers.remove(tm);
085    }
086    
087    public List<KeyManager> getKeyManagers() {
088        return keyManagers;
089    }
090    public void setKeyManagers(List<KeyManager> keyManagers) {
091        this.keyManagers = keyManagers;
092    }
093    public List<TrustManager> getTrustManagers() {
094        return trustManagers;
095    }
096    public void setTrustManagers(List<TrustManager> trustManagers) {
097        this.trustManagers = trustManagers;
098    }
099    public SecureRandom getSecureRandom() {
100        return secureRandom;
101    }
102    public void setSecureRandom(SecureRandom secureRandom) {
103        this.secureRandom = secureRandom;
104    }
105        
106    public String getProtocol() {
107        return protocol;
108    }
109    public void setProtocol(String protocol) {
110        this.protocol = protocol;
111    }
112    public String getProvider() {
113        return provider;
114    }
115    public void setProvider(String provider) {
116        this.provider = provider;
117    }
118
119    public SSLContext getSSLContext() throws NoSuchProviderException, NoSuchAlgorithmException, KeyManagementException {
120        if( sslContext == null ) {
121            if( provider == null ) {
122                sslContext = SSLContext.getInstance(protocol);
123            } else {
124                sslContext = SSLContext.getInstance(protocol, provider);
125            }
126            sslContext.init(getKeyManagersAsArray(), getTrustManagersAsArray(), getSecureRandom());
127        }
128        return sslContext;
129    }
130    public void setSSLContext(SSLContext sslContext) {
131        this.sslContext = sslContext;
132    }
133    
134    
135}