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.servicemix.http;
018    
019    import java.security.Security;
020    
021    /**
022     * This class contains all parameters needed to create an SSL server or client socket.
023     * 
024     * @author gnodet
025     * @org.apache.xbean.XBean
026     */
027    public class SslParameters {
028    
029        private boolean managed;
030        private String keyAlias;
031        private String keyPassword;
032        private String keyStore;
033        private String keyStorePassword;
034        private String keyStoreType = "JKS"; // type of the key store
035        private String trustStore;
036        private String trustStorePassword;
037        private String trustStoreType = "JKS";
038        private String protocol = "TLS";
039        private String keyManagerFactoryAlgorithm; // cert algorithm
040        private String trustManagerFactoryAlgorithm; // cert algorithm
041        private String provider;
042        private boolean wantClientAuth;
043        private boolean needClientAuth;
044    
045        public SslParameters() {
046            keyManagerFactoryAlgorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
047            if (keyManagerFactoryAlgorithm == null) {
048                // Default to SunX509 only if we aren't using an ibm jdk
049                if ("IBM Corporation".equals(System.getProperty("java.vendor"))) {
050                    keyManagerFactoryAlgorithm = "IbmX509";
051                } else {
052                    keyManagerFactoryAlgorithm = "SunX509";
053                }
054            }
055            trustManagerFactoryAlgorithm = Security.getProperty("ssl.TrustManagerFactory.algorithm");
056            if (trustManagerFactoryAlgorithm == null) {
057                if ("IBM Corporation".equals(System.getProperty("java.vendor"))) {
058                    trustManagerFactoryAlgorithm = "PKIX";
059                } else {
060                    trustManagerFactoryAlgorithm = "SunX509";
061                }
062            }
063        }
064    
065        /**
066         * @return the provider
067         */
068        public String getProvider() {
069            return provider;
070        }
071    
072        /**
073         * @param provider
074         *            the provider to set
075         */
076        public void setProvider(String provider) {
077            this.provider = provider;
078        }
079    
080        /**
081         * @return the managed
082         */
083        public boolean isManaged() {
084            return managed;
085        }
086    
087        /**
088         * @param managed
089         *            the managed to set
090         */
091        public void setManaged(boolean managed) {
092            this.managed = managed;
093        }
094    
095        /**
096         * @return the keyAlias
097         */
098        public String getKeyAlias() {
099            return keyAlias;
100        }
101    
102        /**
103         * @param keyAlias
104         *            the keyAlias to set
105         */
106        public void setKeyAlias(String keyAlias) {
107            this.keyAlias = keyAlias;
108        }
109    
110        /**
111         * @return Returns the algorithm.
112         */
113        public String getKeyManagerFactoryAlgorithm() {
114            return keyManagerFactoryAlgorithm;
115        }
116    
117        /**
118         * @param algorithm
119         *            The algorithm to set.
120         */
121        public void setKeyManagerFactoryAlgorithm(String algorithm) {
122            this.keyManagerFactoryAlgorithm = algorithm;
123        }
124    
125        /**
126         * @return Returns the algorithm.
127         */
128        public String getTrustManagerFactoryAlgorithm() {
129            return trustManagerFactoryAlgorithm;
130        }
131    
132        /**
133         * @param algorithm
134         *            The algorithm to set.
135         */
136        public void setTrustManagerFactoryAlgorithm(String algorithm) {
137            this.trustManagerFactoryAlgorithm = algorithm;
138        }
139    
140        /**
141         * @return Returns the keyPassword.
142         */
143        public String getKeyPassword() {
144            return keyPassword;
145        }
146    
147        /**
148         * @param keyPassword
149         *            The keyPassword to set.
150         */
151        public void setKeyPassword(String keyPassword) {
152            this.keyPassword = keyPassword;
153        }
154    
155        /**
156         * @return Returns the keyStore.
157         */
158        public String getKeyStore() {
159            return keyStore;
160        }
161    
162        /**
163         * @param keyStore
164         *            The keyStore to set.
165         */
166        public void setKeyStore(String keyStore) {
167            this.keyStore = keyStore;
168        }
169    
170        /**
171         * @return Returns the keyStorePassword.
172         */
173        public String getKeyStorePassword() {
174            return keyStorePassword;
175        }
176    
177        /**
178         * @param keyStorePassword
179         *            The keyStorePassword to set.
180         */
181        public void setKeyStorePassword(String keyStorePassword) {
182            this.keyStorePassword = keyStorePassword;
183        }
184    
185        /**
186         * @return Returns the keyStoreType.
187         */
188        public String getKeyStoreType() {
189            return keyStoreType;
190        }
191    
192        /**
193         * @param keyStoreType
194         *            The keyStoreType to set.
195         */
196        public void setKeyStoreType(String keyStoreType) {
197            this.keyStoreType = keyStoreType;
198        }
199    
200        /**
201         * @return Returns the needClientAuth.
202         */
203        public boolean isNeedClientAuth() {
204            return needClientAuth;
205        }
206    
207        /**
208         * @param needClientAuth
209         *            The needClientAuth to set.
210         */
211        public void setNeedClientAuth(boolean needClientAuth) {
212            this.needClientAuth = needClientAuth;
213        }
214    
215        /**
216         * @return Returns the protocol.
217         */
218        public String getProtocol() {
219            return protocol;
220        }
221    
222        /**
223         * @param protocol
224         *            The protocol to set.
225         */
226        public void setProtocol(String protocol) {
227            this.protocol = protocol;
228        }
229    
230        /**
231         * @return Returns the wantClientAuth.
232         */
233        public boolean isWantClientAuth() {
234            return wantClientAuth;
235        }
236    
237        /**
238         * @param wantClientAuth
239         *            The wantClientAuth to set.
240         */
241        public void setWantClientAuth(boolean wantClientAuth) {
242            this.wantClientAuth = wantClientAuth;
243        }
244    
245        /**
246         * @return Returns the trustStore.
247         */
248        public String getTrustStore() {
249            return trustStore;
250        }
251    
252        /**
253         * @param trustStore
254         *            The trustStore to set.
255         */
256        public void setTrustStore(String trustStore) {
257            this.trustStore = trustStore;
258        }
259    
260        /**
261         * @return Returns the trustStorePassword.
262         */
263        public String getTrustStorePassword() {
264            return trustStorePassword;
265        }
266    
267        /**
268         * @param trustStorePassword
269         *            The trustStorePassword to set.
270         */
271        public void setTrustStorePassword(String trustStorePassword) {
272            this.trustStorePassword = trustStorePassword;
273        }
274    
275        /**
276         * @return Returns the trustStoreType.
277         */
278        public String getTrustStoreType() {
279            return trustStoreType;
280        }
281    
282        /**
283         * @param trustStoreType
284         *            The trustStoreType to set.
285         */
286        public void setTrustStoreType(String trustStoreType) {
287            this.trustStoreType = trustStoreType;
288        }
289    
290        public boolean equals(Object o) {
291            if (o == this) {
292                return true;
293            }
294            if (!(o instanceof SslParameters)) {
295                return false;
296            }
297            SslParameters s = (SslParameters) o;
298            return managed == s.managed && eq(keyAlias, s.keyAlias)
299                            && eq(keyManagerFactoryAlgorithm, s.keyManagerFactoryAlgorithm)
300                            && eq(trustManagerFactoryAlgorithm, s.trustManagerFactoryAlgorithm)
301                            && eq(keyPassword, s.keyPassword) && eq(keyStore, s.keyStore)
302                            && eq(keyStorePassword, s.keyStorePassword) && eq(keyStoreType, s.keyStoreType)
303                            && needClientAuth == s.needClientAuth && eq(protocol, s.protocol)
304                            && eq(trustStore, s.trustStore) && eq(trustStorePassword, s.trustStorePassword)
305                            && eq(trustStoreType, s.trustStoreType) && wantClientAuth == s.wantClientAuth;
306    
307        }
308    
309        public int hashCode() {
310            return Boolean.valueOf(managed).hashCode() 
311                        ^ Boolean.valueOf(needClientAuth).hashCode() 
312                        ^ Boolean.valueOf(wantClientAuth).hashCode()
313                        ^ hash(new String[] {keyAlias, keyManagerFactoryAlgorithm, trustManagerFactoryAlgorithm, keyPassword, keyStore,
314                            keyStorePassword, keyStoreType, protocol, trustStore, trustStorePassword, trustStoreType});
315        }
316    
317        private static boolean eq(String s1, String s2) {
318            return (s1 == null) ? s2 == null : s1.equals(s2);
319        }
320    
321        private static int hash(String[] strings) {
322            int result = 0;
323            for (String s : strings) {
324                result ^= hash(s);
325            }
326            return result;
327        }
328        
329        private static int hash(String s) {
330            return s != null ? s.hashCode() : 0;
331        }
332    
333    }