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 */
017 package org.apache.activemq.spring;
018
019 import java.io.InputStream;
020 import java.net.MalformedURLException;
021 import java.security.KeyStore;
022 import java.security.NoSuchAlgorithmException;
023 import java.security.SecureRandom;
024 import java.util.ArrayList;
025 import java.util.Arrays;
026 import java.util.Collection;
027
028 import javax.annotation.PostConstruct;
029 import javax.net.ssl.KeyManager;
030 import javax.net.ssl.KeyManagerFactory;
031 import javax.net.ssl.TrustManager;
032 import javax.net.ssl.TrustManagerFactory;
033 import org.apache.activemq.broker.SslContext;
034
035 /**
036 * Extends the SslContext so that it's easier to configure from spring.
037 *
038 * @org.apache.xbean.XBean element="sslContext"
039 *
040 *
041 */
042 public class SpringSslContext extends SslContext {
043
044 private String keyStoreType="jks";
045 private String trustStoreType="jks";
046
047 private String secureRandomAlgorithm="SHA1PRNG";
048 private String keyStoreAlgorithm=KeyManagerFactory.getDefaultAlgorithm();
049 private String trustStoreAlgorithm=TrustManagerFactory.getDefaultAlgorithm();
050
051 private String keyStore;
052 private String trustStore;
053
054 private String keyStoreKeyPassword;
055 private String keyStorePassword;
056 private String trustStorePassword;
057
058 /**
059 *
060 * @throws Exception
061 * @org.apache.xbean.InitMethod
062 */
063 @PostConstruct
064 public void afterPropertiesSet() throws Exception {
065 keyManagers.addAll(createKeyManagers());
066 trustManagers.addAll(createTrustManagers());
067 if( secureRandom == null ) {
068 secureRandom = createSecureRandom();
069 }
070 }
071
072 private SecureRandom createSecureRandom() throws NoSuchAlgorithmException {
073 return SecureRandom.getInstance(secureRandomAlgorithm);
074 }
075
076 private Collection<TrustManager> createTrustManagers() throws Exception {
077 KeyStore ks = createTrustManagerKeyStore();
078 if( ks ==null ) {
079 return new ArrayList<TrustManager>(0);
080 }
081
082 TrustManagerFactory tmf = TrustManagerFactory.getInstance(trustStoreAlgorithm);
083 tmf.init(ks);
084 return Arrays.asList(tmf.getTrustManagers());
085 }
086
087 private Collection<KeyManager> createKeyManagers() throws Exception {
088 KeyStore ks = createKeyManagerKeyStore();
089 if( ks ==null ) {
090 return new ArrayList<KeyManager>(0);
091 }
092
093 KeyManagerFactory tmf = KeyManagerFactory.getInstance(keyStoreAlgorithm);
094 tmf.init(ks, keyStoreKeyPassword == null ? (keyStorePassword==null? null : keyStorePassword.toCharArray()) : keyStoreKeyPassword.toCharArray());
095 return Arrays.asList(tmf.getKeyManagers());
096 }
097
098 private KeyStore createTrustManagerKeyStore() throws Exception {
099 if( trustStore ==null ) {
100 return null;
101 }
102
103 KeyStore ks = KeyStore.getInstance(trustStoreType);
104 InputStream is=Utils.resourceFromString(trustStore).getInputStream();
105 try {
106 ks.load(is, trustStorePassword==null? null : trustStorePassword.toCharArray());
107 } finally {
108 is.close();
109 }
110 return ks;
111 }
112
113 private KeyStore createKeyManagerKeyStore() throws Exception {
114 if( keyStore ==null ) {
115 return null;
116 }
117
118 KeyStore ks = KeyStore.getInstance(keyStoreType);
119 InputStream is=Utils.resourceFromString(keyStore).getInputStream();
120 try {
121 ks.load(is, keyStorePassword==null? null : keyStorePassword.toCharArray());
122 } finally {
123 is.close();
124 }
125 return ks;
126 }
127
128 public String getTrustStoreType() {
129 return trustStoreType;
130 }
131
132 public String getKeyStoreType() {
133 return keyStoreType;
134 }
135
136 public String getKeyStore() {
137 return keyStore;
138 }
139
140 public void setKeyStore(String keyStore) throws MalformedURLException {
141 this.keyStore = keyStore;
142 }
143
144 public String getTrustStore() {
145 return trustStore;
146 }
147
148 public void setTrustStore(String trustStore) throws MalformedURLException {
149 this.trustStore = trustStore;
150 }
151
152 public String getKeyStoreAlgorithm() {
153 return keyStoreAlgorithm;
154 }
155
156 public void setKeyStoreAlgorithm(String keyAlgorithm) {
157 this.keyStoreAlgorithm = keyAlgorithm;
158 }
159
160 public String getTrustStoreAlgorithm() {
161 return trustStoreAlgorithm;
162 }
163
164 public void setTrustStoreAlgorithm(String trustAlgorithm) {
165 this.trustStoreAlgorithm = trustAlgorithm;
166 }
167
168 public String getKeyStoreKeyPassword() {
169 return keyStoreKeyPassword;
170 }
171
172 public void setKeyStoreKeyPassword(String keyPassword) {
173 this.keyStoreKeyPassword = keyPassword;
174 }
175
176 public String getKeyStorePassword() {
177 return keyStorePassword;
178 }
179
180 public void setKeyStorePassword(String keyPassword) {
181 this.keyStorePassword = keyPassword;
182 }
183
184 public String getTrustStorePassword() {
185 return trustStorePassword;
186 }
187
188 public void setTrustStorePassword(String trustPassword) {
189 this.trustStorePassword = trustPassword;
190 }
191
192 public void setKeyStoreType(String keyType) {
193 this.keyStoreType = keyType;
194 }
195
196 public void setTrustStoreType(String trustType) {
197 this.trustStoreType = trustType;
198 }
199
200 public String getSecureRandomAlgorithm() {
201 return secureRandomAlgorithm;
202 }
203
204 public void setSecureRandomAlgorithm(String secureRandomAlgorithm) {
205 this.secureRandomAlgorithm = secureRandomAlgorithm;
206 }
207
208 }