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.transport; 018 019import javax.net.ssl.SSLContext; 020 021import org.apache.activemq.broker.SslContext; 022import org.apache.activemq.util.IntrospectionSupport; 023import org.eclipse.jetty.server.Connector; 024import org.eclipse.jetty.server.Server; 025import org.eclipse.jetty.server.ServerConnector; 026import org.eclipse.jetty.util.ssl.SslContextFactory; 027 028public class SecureSocketConnectorFactory extends SocketConnectorFactory { 029 030 private String keyPassword = System.getProperty("javax.net.ssl.keyPassword"); 031 private String keyStorePassword = System.getProperty("javax.net.ssl.keyStorePassword"); 032 private String keyStore = System.getProperty("javax.net.ssl.keyStore"); 033 private String trustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword"); 034 private String trustStore = System.getProperty("javax.net.ssl.trustStore"); 035 private boolean needClientAuth; 036 private boolean wantClientAuth; 037 private String keyStoreType; 038 private String secureRandomCertficateAlgorithm; 039 private String trustCertificateAlgorithm; 040 private String keyCertificateAlgorithm; 041 private String protocol; 042 private String auth; 043 044 private SslContext context; 045 private SslContextFactory contextFactory; 046 047 public SecureSocketConnectorFactory() { 048 049 } 050 public SecureSocketConnectorFactory(SslContext context) { 051 this.context = context; 052 } 053 054 public SecureSocketConnectorFactory(SslContextFactory contextFactory) { 055 this.contextFactory = contextFactory; 056 } 057 058 @Override 059 public Connector createConnector(Server server) throws Exception { 060 if (getTransportOptions() != null) { 061 IntrospectionSupport.setProperties(this, getTransportOptions()); 062 } 063 064 SSLContext sslContext = context == null ? null : context.getSSLContext(); 065 066 // Get a reference to the current ssl context factory... 067 068 SslContextFactory factory; 069 if (contextFactory == null) { 070 factory = new SslContextFactory(); 071 if (context != null) { 072 // Should not be using this method since it does not use all of the values 073 // from the passed SslContext instance..... 074 factory.setSslContext(sslContext); 075 076 } else { 077 if (keyStore != null) { 078 factory.setKeyStorePath(keyStore); 079 } 080 if (keyStorePassword != null) { 081 factory.setKeyStorePassword(keyStorePassword); 082 } 083 // if the keyPassword hasn't been set, default it to the 084 // key store password 085 if (keyPassword == null && keyStorePassword != null) { 086 factory.setKeyStorePassword(keyStorePassword); 087 } 088 if (keyStoreType != null) { 089 factory.setKeyStoreType(keyStoreType); 090 } 091 if (secureRandomCertficateAlgorithm != null) { 092 factory.setSecureRandomAlgorithm(secureRandomCertficateAlgorithm); 093 } 094 if (keyCertificateAlgorithm != null) { 095 factory.setSslKeyManagerFactoryAlgorithm(keyCertificateAlgorithm); 096 } 097 if (trustCertificateAlgorithm != null) { 098 factory.setTrustManagerFactoryAlgorithm(trustCertificateAlgorithm); 099 } 100 if (protocol != null) { 101 factory.setProtocol(protocol); 102 } 103 if (trustStore != null) { 104 setTrustStore(factory, trustStore); 105 } 106 if (trustStorePassword != null) { 107 factory.setTrustStorePassword(trustStorePassword); 108 } 109 } 110 factory.setNeedClientAuth(needClientAuth); 111 factory.setWantClientAuth(wantClientAuth); 112 } else { 113 factory = contextFactory; 114 } 115 116 117 if ("KRB".equals(auth) || "BOTH".equals(auth) 118 && Server.getVersion().startsWith("8")) { 119 //return new Krb5AndCertsSslSocketConnector(factory, auth); 120 return null; 121 } else { 122 ServerConnector connector = new ServerConnector(server, factory); 123 server.setStopTimeout(500); 124 connector.setStopTimeout(500); 125 return connector; 126 } 127 } 128 private void setTrustStore(SslContextFactory factory, String trustStore2) throws Exception { 129 String mname = Server.getVersion().startsWith("8") ? "setTrustStore" : "setTrustStorePath"; 130 factory.getClass().getMethod(mname, String.class).invoke(factory, trustStore2); 131 } 132 133 134 135 // Properties 136 // -------------------------------------------------------------------------------- 137 138 public String getKeyStore() { 139 return keyStore; 140 } 141 142 public void setKeyStore(String keyStore) { 143 this.keyStore = keyStore; 144 } 145 146 public String getKeyPassword() { 147 return keyPassword; 148 } 149 150 public void setKeyPassword(String keyPassword) { 151 this.keyPassword = keyPassword; 152 } 153 154 public String getKeyStoreType() { 155 return keyStoreType; 156 } 157 158 public void setKeyStoreType(String keyStoreType) { 159 this.keyStoreType = keyStoreType; 160 } 161 162 public String getKeyStorePassword() { 163 return keyStorePassword; 164 } 165 166 public void setKeyStorePassword(String keyStorePassword) { 167 this.keyStorePassword = keyStorePassword; 168 } 169 170 public String getProtocol() { 171 return protocol; 172 } 173 174 public void setProtocol(String protocol) { 175 this.protocol = protocol; 176 } 177 178 public String getSecureRandomCertficateAlgorithm() { 179 return secureRandomCertficateAlgorithm; 180 } 181 182 public void setSecureRandomCertficateAlgorithm(String secureRandomCertficateAlgorithm) { 183 this.secureRandomCertficateAlgorithm = secureRandomCertficateAlgorithm; 184 } 185 186 public String getKeyCertificateAlgorithm() { 187 return keyCertificateAlgorithm; 188 } 189 190 public void setKeyCertificateAlgorithm(String keyCertificateAlgorithm) { 191 this.keyCertificateAlgorithm = keyCertificateAlgorithm; 192 } 193 194 public String getTrustCertificateAlgorithm() { 195 return trustCertificateAlgorithm; 196 } 197 198 public void setTrustCertificateAlgorithm(String trustCertificateAlgorithm) { 199 this.trustCertificateAlgorithm = trustCertificateAlgorithm; 200 } 201 202 /** 203 * @return the auth 204 */ 205 public String getAuth() { 206 return auth; 207 } 208 209 /** 210 * @param auth the auth to set 211 */ 212 public void setAuth(String auth) { 213 this.auth = auth; 214 } 215 216 public boolean isWantClientAuth() { 217 return wantClientAuth; 218 } 219 220 public void setWantClientAuth(boolean wantClientAuth) { 221 this.wantClientAuth = wantClientAuth; 222 } 223 224 public boolean isNeedClientAuth() { 225 return needClientAuth; 226 } 227 228 public void setNeedClientAuth(boolean needClientAuth) { 229 this.needClientAuth = needClientAuth; 230 } 231 232 public String getTrustStore() { 233 return trustStore; 234 } 235 236 public void setTrustStore(String trustStore) { 237 this.trustStore = trustStore; 238 } 239 240 public String getTrustStorePassword() { 241 return trustStorePassword; 242 } 243 244 public void setTrustStorePassword(String trustStorePassword) { 245 this.trustStorePassword = trustStorePassword; 246 } 247}