Author: trustin Date: Wed Nov 28 21:56:19 2007 New Revision: 599286 URL: http://svn.apache.org/viewvc?rev=599286&view=rev Log: * Added KeyStoreFactory (moved and modified from KeyStoreFactoryBean in integration-spring) * Added SslContextFactory (moved and modified from SslContextFactoryBean in integration-spring)
Added: mina/trunk/core/src/main/java/org/apache/mina/filter/ssl/KeyStoreFactory.java (with props) mina/trunk/core/src/main/java/org/apache/mina/filter/ssl/SslContextFactory.java (with props) mina/trunk/core/src/test/java/org/apache/mina/filter/ssl/ mina/trunk/core/src/test/java/org/apache/mina/filter/ssl/KeyStoreFactoryTest.java (with props) mina/trunk/core/src/test/resources/org/ mina/trunk/core/src/test/resources/org/apache/ mina/trunk/core/src/test/resources/org/apache/mina/ mina/trunk/core/src/test/resources/org/apache/mina/filter/ mina/trunk/core/src/test/resources/org/apache/mina/filter/ssl/ mina/trunk/core/src/test/resources/org/apache/mina/filter/ssl/keystore.cert (with props) Added: mina/trunk/core/src/main/java/org/apache/mina/filter/ssl/KeyStoreFactory.java URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/filter/ssl/KeyStoreFactory.java?rev=599286&view=auto ============================================================================== --- mina/trunk/core/src/main/java/org/apache/mina/filter/ssl/KeyStoreFactory.java (added) +++ mina/trunk/core/src/main/java/org/apache/mina/filter/ssl/KeyStoreFactory.java Wed Nov 28 21:56:19 2007 @@ -0,0 +1,175 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.mina.filter.ssl; + +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.cert.CertificateException; + +/** + * A factory that creates and configures a new [EMAIL PROTECTED] KeyStore} instance. + * + * @author The Apache MINA Project ([EMAIL PROTECTED]) + * @version $Rev$, $Date$ + */ +public class KeyStoreFactory { + + private String type = "JKS"; + private String provider = null; + private char[] password = null; + private byte[] data = null; + + /** + * Creates a new [EMAIL PROTECTED] KeyStore}. This method will be called + * by the base class when Spring creates a bean using this FactoryBean. + * + * @return a new [EMAIL PROTECTED] KeyStore} instance. + */ + public KeyStore newInstance() throws KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException, CertificateException, IOException { + if (data == null) { + throw new IllegalStateException("data property is not set."); + } + + KeyStore ks = null; + if (provider == null) { + ks = KeyStore.getInstance(type); + } else { + ks = KeyStore.getInstance(type, provider); + } + + InputStream is = new ByteArrayInputStream(data); + try { + ks.load(is, password); + } finally { + try { + is.close(); + } catch (IOException ignored) { + } + } + + return ks; + } + + /** + * Sets the type of key store to create. The default is to create a + * JKS key store. + * + * @param type the type to use when creating the key store. + * @throws IllegalArgumentException if the specified value is + * <code>null</code>. + */ + public void setType(String type) { + if (type == null) { + throw new NullPointerException("type"); + } + this.type = type; + } + + /** + * Sets the key store password. If this value is <code>null</code> no + * password will be used to check the integrity of the key store. + * + * @param password the password or <code>null</code> if no password is + * needed. + */ + public void setPassword(String password) { + if (password != null) { + this.password = password.toCharArray(); + } else { + this.password = null; + } + } + + /** + * Sets the name of the provider to use when creating the key store. The + * default is to use the platform default provider. + * + * @param provider the name of the provider, e.g. <tt>"SUN"</tt>. + */ + public void setProvider(String provider) { + this.provider = provider; + } + + /** + * Sets the data which contains the key store. + * + * @param data the byte array that contains the key store + */ + public void setData(byte[] data) { + byte[] copy = new byte[data.length]; + System.arraycopy(data, 0, copy, 0, data.length); + this.data = copy; + } + + /** + * Sets the data which contains the key store. + * + * @param dataStream the [EMAIL PROTECTED] InputStream} that contains the key store + */ + public void setData(InputStream dataStream) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try { + for (;;) { + int data = dataStream.read(); + if (data < 0) { + break; + } + + out.write(data); + } + + setData(out.toByteArray()); + } finally { + try { + dataStream.close(); + } catch (IOException e) { + // Ignore. + } + } + } + + /** + * Sets the data which contains the key store. + * + * @param dataFile the [EMAIL PROTECTED] File} that contains the key store + */ + public void setData(File dataFile) throws IOException { + setData(new BufferedInputStream(new FileInputStream(dataFile))); + } + + /** + * Sets the data which contains the key store. + * + * @param dataUrl the [EMAIL PROTECTED] URL} that contains the key store. + */ + public void setData(URL dataUrl) throws IOException { + setData(dataUrl.openStream()); + } +} Propchange: mina/trunk/core/src/main/java/org/apache/mina/filter/ssl/KeyStoreFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: mina/trunk/core/src/main/java/org/apache/mina/filter/ssl/KeyStoreFactory.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: mina/trunk/core/src/main/java/org/apache/mina/filter/ssl/SslContextFactory.java URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/filter/ssl/SslContextFactory.java?rev=599286&view=auto ============================================================================== --- mina/trunk/core/src/main/java/org/apache/mina/filter/ssl/SslContextFactory.java (added) +++ mina/trunk/core/src/main/java/org/apache/mina/filter/ssl/SslContextFactory.java Wed Nov 28 21:56:19 2007 @@ -0,0 +1,422 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.mina.filter.ssl; + +import java.security.KeyStore; +import java.security.SecureRandom; + +import javax.net.ssl.KeyManager; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.ManagerFactoryParameters; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSessionContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; + +/** + * A factory that creates and configures a new [EMAIL PROTECTED] SSLContext}. + * <p> + * If no properties are set the returned [EMAIL PROTECTED] SSLContext} will + * be equivalent to what the following creates: + * <pre> + * SSLContext c = SSLContext.getInstance( "TLS" ); + * c.init(null, null, null); + * </pre> + * </p> + * <p> + * Use the properties prefixed with <code>keyManagerFactory</code> to control + * the creation of the [EMAIL PROTECTED] KeyManager} to be used. + * </p> + * <p> + * Use the properties prefixed with <code>trustManagerFactory</code> to control + * the creation of the [EMAIL PROTECTED] TrustManagerFactory} to be used. + * </p> + * + * @author The Apache MINA Project ([EMAIL PROTECTED]) + * @version $Rev$, $Date$ + */ +public class SslContextFactory { + + private String provider = null; + private String protocol = "TLS"; + private SecureRandom secureRandom = null; + private KeyStore keyManagerFactoryKeyStore = null; + private char[] keyManagerFactoryKeyStorePassword = null; + private KeyManagerFactory keyManagerFactory = null; + private String keyManagerFactoryAlgorithm = null; + private String keyManagerFactoryProvider = null; + private boolean keyManagerFactoryAlgorithmUseDefault = false; + private KeyStore trustManagerFactoryKeyStore = null; + private TrustManagerFactory trustManagerFactory = null; + private String trustManagerFactoryAlgorithm = null; + private String trustManagerFactoryProvider = null; + private boolean trustManagerFactoryAlgorithmUseDefault = false; + private ManagerFactoryParameters trustManagerFactoryParameters = null; + private int clientSessionCacheSize = -1; + private int clientSessionTimeout = -1; + private int serverSessionCacheSize = -1; + private int serverSessionTimeout = -1; + + public SSLContext newInstance() throws Exception { + KeyManagerFactory kmf = this.keyManagerFactory; + TrustManagerFactory tmf = this.trustManagerFactory; + + if (kmf == null) { + String algorithm = keyManagerFactoryAlgorithm; + if (algorithm == null && keyManagerFactoryAlgorithmUseDefault) { + algorithm = KeyManagerFactory.getDefaultAlgorithm(); + } + if (algorithm != null) { + if (keyManagerFactoryProvider == null) { + kmf = KeyManagerFactory.getInstance(algorithm); + } else { + kmf = KeyManagerFactory.getInstance(algorithm, + keyManagerFactoryProvider); + } + } + } + + if (tmf == null) { + String algorithm = trustManagerFactoryAlgorithm; + if (algorithm == null && trustManagerFactoryAlgorithmUseDefault) { + algorithm = TrustManagerFactory.getDefaultAlgorithm(); + } + if (algorithm != null) { + if (trustManagerFactoryProvider == null) { + tmf = TrustManagerFactory.getInstance(algorithm); + } else { + tmf = TrustManagerFactory.getInstance(algorithm, + trustManagerFactoryProvider); + } + } + } + + KeyManager[] keyManagers = null; + if (kmf != null) { + kmf.init(keyManagerFactoryKeyStore, + keyManagerFactoryKeyStorePassword); + keyManagers = kmf.getKeyManagers(); + } + TrustManager[] trustManagers = null; + if (tmf != null) { + if (trustManagerFactoryParameters != null) { + tmf.init(trustManagerFactoryParameters); + } else { + tmf.init(trustManagerFactoryKeyStore); + } + trustManagers = tmf.getTrustManagers(); + } + + SSLContext context = null; + if (provider == null) { + context = SSLContext.getInstance(protocol); + } else { + context = SSLContext.getInstance(protocol, provider); + } + + context.init(keyManagers, trustManagers, secureRandom); + + if (clientSessionCacheSize >= 0) { + context.getClientSessionContext().setSessionCacheSize( + clientSessionCacheSize); + } + + if (clientSessionTimeout >= 0) { + context.getClientSessionContext().setSessionTimeout( + clientSessionTimeout); + } + + if (serverSessionCacheSize >= 0) { + context.getServerSessionContext().setSessionCacheSize( + serverSessionCacheSize); + } + + if (serverSessionTimeout >= 0) { + context.getServerSessionContext().setSessionTimeout( + serverSessionTimeout); + } + + return context; + } + + /** + * Sets the provider of the new [EMAIL PROTECTED] SSLContext}. The default value is + * <tt>null</tt>, which means the default provider will be used. + * + * @param provider the name of the [EMAIL PROTECTED] SSLContext} provider + */ + public void setProvider(String provider) { + this.provider = provider; + } + + /** + * Sets the protocol to use when creating the [EMAIL PROTECTED] SSLContext}. The + * default is <code>TLS</code>. + * + * @param protocol the name of the protocol. + */ + public void setProtocol(String protocol) { + if (protocol == null) { + throw new NullPointerException("protocol"); + } + this.protocol = protocol; + } + + /** + * If this is set to <code>true</code> while no [EMAIL PROTECTED] KeyManagerFactory} + * has been set using [EMAIL PROTECTED] #setKeyManagerFactory(KeyManagerFactory)} and + * no algorithm has been set using + * [EMAIL PROTECTED] #setKeyManagerFactoryAlgorithm(String)} the default algorithm + * return by [EMAIL PROTECTED] KeyManagerFactory#getDefaultAlgorithm()} will be used. + * + * @param useDefault + * <code>true</code> or <code>false</code>. + */ + public void setKeyManagerFactoryAlgorithmUseDefault(boolean useDefault) { + this.keyManagerFactoryAlgorithmUseDefault = useDefault; + } + + /** + * If this is set to <code>true</code> while no [EMAIL PROTECTED] TrustManagerFactory} + * has been set using [EMAIL PROTECTED] #setTrustManagerFactory(TrustManagerFactory)} and + * no algorithm has been set using + * [EMAIL PROTECTED] #setTrustManagerFactoryAlgorithm(String)} the default algorithm + * return by [EMAIL PROTECTED] TrustManagerFactory#getDefaultAlgorithm()} will be used. + * + * @param useDefault <code>true</code> or <code>false</code>. + */ + public void setTrustManagerFactoryAlgorithmUseDefault(boolean useDefault) { + this.trustManagerFactoryAlgorithmUseDefault = useDefault; + } + + /** + * Sets the [EMAIL PROTECTED] KeyManagerFactory} to use. If this is set the properties + * which are used by this factory bean to create a [EMAIL PROTECTED] KeyManagerFactory} + * will all be ignored. + * + * @param factory the factory. + */ + public void setKeyManagerFactory(KeyManagerFactory factory) { + this.keyManagerFactory = factory; + } + + /** + * Sets the algorithm to use when creating the [EMAIL PROTECTED] KeyManagerFactory} + * using [EMAIL PROTECTED] KeyManagerFactory#getInstance(java.lang.String)} or + * [EMAIL PROTECTED] KeyManagerFactory#getInstance(java.lang.String, java.lang.String)}. + * <p> + * This property will be ignored if a [EMAIL PROTECTED] KeyManagerFactory} has been + * set directly using [EMAIL PROTECTED] #setKeyManagerFactory(KeyManagerFactory)}. + * </p> + * <p> + * If this property isn't set while no [EMAIL PROTECTED] KeyManagerFactory} has been + * set using [EMAIL PROTECTED] #setKeyManagerFactory(KeyManagerFactory)} and + * [EMAIL PROTECTED] #setKeyManagerFactoryAlgorithmUseDefault(boolean)} has been set to + * <code>true</code> the value returned + * by [EMAIL PROTECTED] KeyManagerFactory#getDefaultAlgorithm()} will be used instead. + * </p> + * + * @param algorithm the algorithm to use. + */ + public void setKeyManagerFactoryAlgorithm(String algorithm) { + this.keyManagerFactoryAlgorithm = algorithm; + } + + /** + * Sets the provider to use when creating the [EMAIL PROTECTED] KeyManagerFactory} + * using + * [EMAIL PROTECTED] KeyManagerFactory#getInstance(java.lang.String, java.lang.String)}. + * <p> + * This property will be ignored if a [EMAIL PROTECTED] KeyManagerFactory} has been + * set directly using [EMAIL PROTECTED] #setKeyManagerFactory(KeyManagerFactory)}. + * </p> + * <p> + * If this property isn't set and no [EMAIL PROTECTED] KeyManagerFactory} has been set + * using [EMAIL PROTECTED] #setKeyManagerFactory(KeyManagerFactory)} + * [EMAIL PROTECTED] KeyManagerFactory#getInstance(java.lang.String)} will be used + * to create the [EMAIL PROTECTED] KeyManagerFactory}. + * </p> + * + * @param provider the name of the provider. + */ + public void setKeyManagerFactoryProvider(String provider) { + this.keyManagerFactoryProvider = provider; + } + + /** + * Sets the [EMAIL PROTECTED] KeyStore} which will be used in the call to + * [EMAIL PROTECTED] KeyManagerFactory#init(java.security.KeyStore, char[])} when + * the [EMAIL PROTECTED] SSLContext} is created. + * + * @param keyStore the key store. + */ + public void setKeyManagerFactoryKeyStore(KeyStore keyStore) { + this.keyManagerFactoryKeyStore = keyStore; + } + + /** + * Sets the password which will be used in the call to + * [EMAIL PROTECTED] KeyManagerFactory#init(java.security.KeyStore, char[])} when + * the [EMAIL PROTECTED] SSLContext} is created. + * + * @param password the password. Use <code>null</code> to disable password. + */ + public void setKeyManagerFactoryKeyStorePassword(String password) { + if (password != null) { + this.keyManagerFactoryKeyStorePassword = password.toCharArray(); + } else { + this.keyManagerFactoryKeyStorePassword = null; + } + } + + /** + * Sets the [EMAIL PROTECTED] TrustManagerFactory} to use. If this is set the + * properties which are used by this factory bean to create a + * [EMAIL PROTECTED] TrustManagerFactory} will all be ignored. + * + * @param factory + * the factory. + */ + public void setTrustManagerFactory(TrustManagerFactory factory) { + this.trustManagerFactory = factory; + } + + /** + * Sets the algorithm to use when creating the [EMAIL PROTECTED] TrustManagerFactory} + * using [EMAIL PROTECTED] TrustManagerFactory#getInstance(java.lang.String)} or + * [EMAIL PROTECTED] TrustManagerFactory#getInstance(java.lang.String, java.lang.String)}. + * <p> + * This property will be ignored if a [EMAIL PROTECTED] TrustManagerFactory} has been + * set directly using [EMAIL PROTECTED] #setTrustManagerFactory(TrustManagerFactory)}. + * </p> + * <p> + * If this property isn't set while no [EMAIL PROTECTED] TrustManagerFactory} has been + * set using [EMAIL PROTECTED] #setTrustManagerFactory(TrustManagerFactory)} and + * [EMAIL PROTECTED] #setTrustManagerFactoryAlgorithmUseDefault(boolean)} has been set to + * <code>true</code> the value returned + * by [EMAIL PROTECTED] TrustManagerFactory#getDefaultAlgorithm()} will be used instead. + * </p> + * + * @param algorithm the algorithm to use. + */ + public void setTrustManagerFactoryAlgorithm(String algorithm) { + this.trustManagerFactoryAlgorithm = algorithm; + } + + /** + * Sets the [EMAIL PROTECTED] KeyStore} which will be used in the call to + * [EMAIL PROTECTED] TrustManagerFactory#init(java.security.KeyStore)} when + * the [EMAIL PROTECTED] SSLContext} is created. + * <p> + * This property will be ignored if [EMAIL PROTECTED] ManagerFactoryParameters} has been + * set directly using [EMAIL PROTECTED] #setTrustManagerFactoryParameters(ManagerFactoryParameters)}. + * </p> + * + * @param keyStore the key store. + */ + public void setTrustManagerFactoryKeyStore(KeyStore keyStore) { + this.trustManagerFactoryKeyStore = keyStore; + } + + /** + * Sets the [EMAIL PROTECTED] ManagerFactoryParameters} which will be used in the call to + * [EMAIL PROTECTED] TrustManagerFactory#init(javax.net.ssl.ManagerFactoryParameters)} when + * the [EMAIL PROTECTED] SSLContext} is created. + * + * @param parameters describing provider-specific trust material. + */ + public void setTrustManagerFactoryParameters( + ManagerFactoryParameters parameters) { + this.trustManagerFactoryParameters = parameters; + } + + /** + * Sets the provider to use when creating the [EMAIL PROTECTED] TrustManagerFactory} + * using + * [EMAIL PROTECTED] TrustManagerFactory#getInstance(java.lang.String, java.lang.String)}. + * <p> + * This property will be ignored if a [EMAIL PROTECTED] TrustManagerFactory} has been + * set directly using [EMAIL PROTECTED] #setTrustManagerFactory(TrustManagerFactory)}. + * </p> + * <p> + * If this property isn't set and no [EMAIL PROTECTED] TrustManagerFactory} has been set + * using [EMAIL PROTECTED] #setTrustManagerFactory(TrustManagerFactory)} + * [EMAIL PROTECTED] TrustManagerFactory#getInstance(java.lang.String)} will be used + * to create the [EMAIL PROTECTED] TrustManagerFactory}. + * </p> + * + * @param provider the name of the provider. + */ + public void setTrustManagerFactoryProvider(String provider) { + this.trustManagerFactoryProvider = provider; + } + + /** + * Sets the [EMAIL PROTECTED] SecureRandom} to use when initializing the + * [EMAIL PROTECTED] SSLContext}. The JVM's default will be used if this isn't set. + * + * @param secureRandom the [EMAIL PROTECTED] SecureRandom} or <code>null</code> if the + * JVM's default should be used. + * @see SSLContext#init(javax.net.ssl.KeyManager[], javax.net.ssl.TrustManager[], java.security.SecureRandom) + */ + public void setSecureRandom(SecureRandom secureRandom) { + this.secureRandom = secureRandom; + } + + /** + * Sets the SSLSession cache size for the [EMAIL PROTECTED] SSLSessionContext} for use in client mode. + * + * @param size the new session cache size limit; zero means there is no limit. + * @see SSLSessionContext#setSessionCacheSize(int size) + */ + public void setClientSessionCacheSize(int size) { + this.clientSessionCacheSize = size; + } + + /** + * Set the SSLSession timeout limit for the [EMAIL PROTECTED] SSLSessionContext} for use in client mode. + * + * @param seconds the new session timeout limit in seconds; zero means there is no limit. + * @see SSLSessionContext#setSessionTimeout(int seconds) + */ + public void setClientSessionTimeout(int seconds) { + this.clientSessionTimeout = seconds; + } + + /** + * Sets the SSLSession cache size for the [EMAIL PROTECTED] SSLSessionContext} for use in server mode. + * + * @param serverSessionCacheSize the new session cache size limit; zero means there is no limit. + * @see SSLSessionContext#setSessionCacheSize(int) + */ + public void setServerSessionCacheSize(int serverSessionCacheSize) { + this.serverSessionCacheSize = serverSessionCacheSize; + } + + /** + * Set the SSLSession timeout limit for the [EMAIL PROTECTED] SSLSessionContext} for use in server mode. + * + * @param serverSessionTimeout the new session timeout limit in seconds; zero means there is no limit. + * @see SSLSessionContext#setSessionTimeout(int) + */ + public void setServerSessionTimeout(int serverSessionTimeout) { + this.serverSessionTimeout = serverSessionTimeout; + } +} Propchange: mina/trunk/core/src/main/java/org/apache/mina/filter/ssl/SslContextFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: mina/trunk/core/src/main/java/org/apache/mina/filter/ssl/SslContextFactory.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: mina/trunk/core/src/test/java/org/apache/mina/filter/ssl/KeyStoreFactoryTest.java URL: http://svn.apache.org/viewvc/mina/trunk/core/src/test/java/org/apache/mina/filter/ssl/KeyStoreFactoryTest.java?rev=599286&view=auto ============================================================================== --- mina/trunk/core/src/test/java/org/apache/mina/filter/ssl/KeyStoreFactoryTest.java (added) +++ mina/trunk/core/src/test/java/org/apache/mina/filter/ssl/KeyStoreFactoryTest.java Wed Nov 28 21:56:19 2007 @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.mina.filter.ssl; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.security.KeyStore; + +import junit.framework.TestCase; + +/** + * Tests [EMAIL PROTECTED] KeyStoreFactory}. + * + * @author The Apache MINA Project ([EMAIL PROTECTED]) + * @version $Rev$, $Date$ + */ +public class KeyStoreFactoryTest extends TestCase { + public void testCreateInstanceFromResource() throws Exception { + // Test using default for now. + KeyStoreFactory factory = new KeyStoreFactory(); + factory.setData(getClass().getResource("keystore.cert")); + factory.setPassword("boguspw"); + + KeyStore ks = factory.newInstance(); + + ks.getCertificate("bogus"); + ks.getKey("bogus", "boguspw".toCharArray()); + } + + public void testCreateInstanceFromFile() throws Exception { + // Copy the keystore from the class path to a temporary file. + File file = File.createTempFile("keystoretest ", null); + file.deleteOnExit(); + InputStream in = getClass().getResourceAsStream("keystore.cert"); + OutputStream out = new FileOutputStream(file); + int b; + while ((b = in.read()) != -1) { + out.write(b); + } + in.close(); + out.close(); + + // Test using default for now. + KeyStoreFactory factory = new KeyStoreFactory(); + factory.setData(file); + factory.setPassword("boguspw"); + + KeyStore ks = factory.newInstance(); + + ks.getCertificate("bogus"); + ks.getKey("bogus", "boguspw".toCharArray()); + } + +} Propchange: mina/trunk/core/src/test/java/org/apache/mina/filter/ssl/KeyStoreFactoryTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: mina/trunk/core/src/test/java/org/apache/mina/filter/ssl/KeyStoreFactoryTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: mina/trunk/core/src/test/resources/org/apache/mina/filter/ssl/keystore.cert URL: http://svn.apache.org/viewvc/mina/trunk/core/src/test/resources/org/apache/mina/filter/ssl/keystore.cert?rev=599286&view=auto ============================================================================== Binary file - no diff available. Propchange: mina/trunk/core/src/test/resources/org/apache/mina/filter/ssl/keystore.cert ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream