1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2004-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: SSLUtil.java,v 1.2 2005/05/03 13:46:03 tanderson Exp $
44   */
45  package org.exolab.jms.net.util;
46  
47  import java.io.IOException;
48  import java.io.File;
49  import java.util.Map;
50  import java.util.HashMap;
51  import java.util.Properties;
52  
53  
54  /***
55   * Helper class for TCPS and HTTPS tests.
56   *
57   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
58   * @version $Revision: 1.2 $ $Date: 2005/05/03 13:46:03 $
59   */
60  public class SSLUtil {
61  
62      /***
63       * Helper to return connection properties for the HTTPS connector.
64       *
65       * @param keystore the name of the keystore
66       * @param password the keystore password
67       * @throws IOException if the keystore can't be found
68       */
69      public static Map getHTTPSProperties(String keystore, String password)
70          throws IOException {
71          String path = getKeyStorePath(keystore);
72          Map result = new HashMap();
73          result.put("org.exolab.jms.net.https.keyStore", path);
74          result.put("org.exolab.jms.net.https.keyStorePassword", password);
75          result.put("org.exolab.jms.net.https.trustStore", path);
76          return result;
77      }
78  
79      /***
80       * Helper to return connection properties for the TCPS connector.
81       *
82       * @param keystore the name of the keystore
83       * @param password the keystore password
84       * @throws IOException if the keystore can't be found
85       */
86      public static Map getTCPSProperties(String keystore, String password)
87          throws IOException {
88          String path = getKeyStorePath(keystore);
89          Map result = new HashMap();
90          result.put("org.exolab.jms.net.tcps.keyStore", path);
91          result.put("org.exolab.jms.net.tcps.keyStorePassword", password);
92          result.put("org.exolab.jms.net.tcps.trustStore", path);
93          return result;
94      }
95  
96      /***
97       * Helper to return a populated {@link SSLProperties}.
98       * <p/>
99       * The truststore is assumed to be the same as the keystore.
100      *
101      * @param keystore the name of the keystore
102      * @param password the keystore password
103      * @throws IOException if the keystore can't be found
104      */
105     public static SSLProperties getSSLProperties(String keystore,
106                                                  String password)
107             throws IOException {
108         String path = getKeyStorePath(keystore);
109         SSLProperties result = new SSLProperties();
110         result.setKeyStore(path);
111         result.setKeyStorePassword(password);
112         result.setTrustStore(path);
113         return result;
114     }
115 
116     /***
117      * Helper to locate a keystore wthin the filesystem
118      *
119      * @param name the name of the keystore
120      * @throws IOException if the keystore can't be found
121      */
122     public static String getKeyStorePath(String name) throws IOException {
123         File keystore = new File(name);
124         if (!keystore.isAbsolute() || !keystore.exists()) {
125             final String paths[] = {"modules", "net", "target"};
126             String workingDir = System.getProperty("user.dir");
127             for (int i = 0; i < paths.length; ++i) {
128                 if (workingDir.indexOf(paths[i]) == -1) {
129                     if (!workingDir.endsWith(File.separator)) {
130                         workingDir += File.separator;
131                     }
132                     workingDir += paths[i];
133                 }
134             }
135             keystore = new File(workingDir + File.separator + name);
136         }
137         if (!keystore.exists()) {
138             throw new IOException("Failed to locate keystore: " + keystore);
139         }
140         return keystore.getPath();
141     }
142 
143     /***
144      * Clears javax.net.ssl.* properties set during tests, to help ensure
145      * that they don't interfere in subsequent tests.
146      */
147     public static void clearProperties() {
148         Properties properties = System.getProperties();
149         properties.remove(SSLHelper.KEY_STORE);
150         properties.remove(SSLHelper.KEY_STORE_PASSWORD);
151         properties.remove(SSLHelper.KEY_STORE_TYPE);
152         properties.remove(SSLHelper.TRUST_STORE);
153         properties.remove(SSLHelper.TRUST_STORE_PASSWORD);
154         properties.remove(SSLHelper.TRUST_STORE_TYPE);
155     }
156     
157 
158 }