View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl.support.wss.crypto;
14  
15  import com.eviware.soapui.config.KeyMaterialCryptoConfig;
16  import com.eviware.soapui.config.WSSCryptoConfig;
17  import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem;
18  import com.eviware.soapui.impl.wsdl.support.wss.DefaultWssContainer;
19  import com.eviware.soapui.impl.wsdl.support.wss.WssContainer;
20  import com.eviware.soapui.impl.wsdl.support.wss.WssCrypto;
21  import com.eviware.soapui.impl.wsdl.teststeps.BeanPathPropertySupport;
22  import com.eviware.soapui.support.StringUtils;
23  import com.eviware.soapui.support.UISupport;
24  import com.eviware.soapui.support.resolver.ResolveContext;
25  import org.apache.commons.ssl.KeyStoreBuilder;
26  import org.apache.commons.ssl.Util;
27  import org.apache.ws.security.components.crypto.CredentialException;
28  import org.apache.ws.security.components.crypto.Crypto;
29  import org.apache.ws.security.components.crypto.Merlin;
30  
31  import java.io.File;
32  import java.io.FileInputStream;
33  import java.io.IOException;
34  import java.io.InputStream;
35  import java.security.KeyStore;
36  import java.util.Properties;
37  
38  public class KeyMaterialWssCrypto implements WssCrypto
39  {
40  	private KeyMaterialCryptoConfig config;
41  	private final WssContainer container;
42  	private KeyStore keyStore;
43  	private BeanPathPropertySupport sourceProperty;
44  
45  	public KeyMaterialWssCrypto( KeyMaterialCryptoConfig config2, WssContainer container, String source, String password )
46  	{
47  		this( config2, container );
48  		setSource( source );
49  		setPassword( password );
50  	}
51  
52  	public KeyMaterialWssCrypto( KeyMaterialCryptoConfig cryptoConfig, WssContainer container2 )
53  	{
54  		config = cryptoConfig;
55  		container = container2;
56  		
57  		sourceProperty = new BeanPathPropertySupport( (AbstractWsdlModelItem<?>)container.getModelItem(), config, "source" )
58  		{
59  			@Override
60  			protected void notifyUpdate(String value, String old)
61  			{
62  				getWssContainer().fireCryptoUpdated( KeyMaterialWssCrypto.this );
63  			}
64  		};
65  	}
66  
67  	public Crypto getCrypto()
68  	{
69  		try
70  		{
71  			Properties properties = new Properties();
72  			properties.put( "org.apache.ws.security.crypto.merlin.file", sourceProperty.expand() );
73  			properties.put( "org.apache.ws.security.crypto.merlin.keystore.provider", "this" );
74  			if( StringUtils.hasContent( getDefaultAlias() ))
75  				properties.put( "org.apache.ws.security.crypto.merlin.keystore.alias", getDefaultAlias() );
76  			if( StringUtils.hasContent( getAliasPassword() ))
77  				properties.put( "org.apache.ws.security.crypto.merlin.alias.password", getAliasPassword() );
78  			
79  			return new KeyMaterialCrypto( properties );
80  		}
81  		catch( Exception e )
82  		{
83  			e.printStackTrace();
84  		}
85  		return null;
86  	}
87  	
88  	public String getLabel()
89  	{
90  		String source = getSource();
91  		
92  		int ix = source.lastIndexOf( File.separatorChar );
93  		if( ix == -1 )
94  			ix = source.lastIndexOf( '/' );
95  		
96  		if( ix != -1 )
97  			source = source.substring( ix+1 );
98  		
99  		return source;
100 	}
101 
102 	public String getSource()
103 	{
104 		return sourceProperty.expand();
105 	}
106 
107 	public void udpateConfig( KeyMaterialCryptoConfig config )
108 	{
109 		this.config = config;
110 		sourceProperty.setConfig( config );
111 	}
112 
113 	public void setSource( String source )
114 	{
115 		sourceProperty.set( source, true );
116 		keyStore = null;
117 	}
118 
119 	public KeyStore load() throws Exception
120 	{
121 		if( keyStore != null )
122 			return keyStore;
123 		
124 		try
125 		{
126 			UISupport.setHourglassCursor();
127 			
128 			if( StringUtils.hasContent( getDefaultAlias() ) && StringUtils.hasContent( getAliasPassword() ))
129 			{
130 				keyStore = KeyStoreBuilder.build( Util.streamToBytes( new FileInputStream( sourceProperty.expand() ) ), 
131 						getDefaultAlias().getBytes(), getPassword().toCharArray(), getAliasPassword().toCharArray() );
132 			}
133 			else keyStore = KeyStoreBuilder.build( Util.streamToBytes( new FileInputStream( sourceProperty.expand() ) ), getPassword().toCharArray() );
134 			
135 			return keyStore;
136 		}
137 		catch( Throwable t )
138 		{
139 			throw new Exception( t );
140 		}
141 		finally
142 		{
143 			UISupport.resetCursor();
144 		}
145 	}
146 	
147 	public String getStatus() 
148 	{
149 		try
150 		{
151 			if( StringUtils.hasContent( getSource() ) && StringUtils.hasContent( getPassword() ))
152 			{
153 				load();
154 				return "OK";
155 			}
156 			else
157 			{
158 				return "<unavailable>";
159 			}
160 		}
161 		catch( Exception e )
162 		{
163 			return "<error: " + e.getMessage() + ">";
164 		}
165 	}
166 	
167 	public String getPassword()
168 	{
169 		return config.getPassword();
170 	}
171 
172 	public String getAliasPassword()
173 	{
174 		return config.getAliasPassword();
175 	}
176 
177 	public String getDefaultAlias()
178 	{
179 		return config.getDefaultAlias();
180 	}
181 
182 	public void setAliasPassword( String arg0 )
183 	{
184 		config.setAliasPassword( arg0 );
185 	}
186 
187 	public void setDefaultAlias( String arg0 )
188 	{
189 		config.setDefaultAlias( arg0 );
190 	}
191 
192 	public void setPassword( String arg0 )
193 	{
194 		config.setPassword( arg0 );
195 		keyStore = null;
196 		getWssContainer().fireCryptoUpdated( this );
197 	}
198 
199 	public void udpateConfig( WSSCryptoConfig config )
200 	{
201 //		this.config = config;
202 	}
203 	
204 	public String toString()
205 	{
206 		return getLabel();
207 	}
208 
209 	public DefaultWssContainer getWssContainer()
210 	{
211 		return ( DefaultWssContainer ) container;
212 	}
213 	
214 	private class KeyMaterialCrypto extends Merlin
215 	{
216 		private KeyMaterialCrypto(Properties properties) throws CredentialException, IOException
217 		{
218 			super( properties );
219 		}
220 
221 		@Override
222 		public KeyStore load( InputStream input, String storepass, String provider, String type ) throws CredentialException
223 		{
224 			if( "this".equals( provider ))
225 			{
226 				try
227 				{
228 					return KeyMaterialWssCrypto.this.load();
229 				}
230 				catch( Exception e )
231 				{
232 					throw new CredentialException( 0, null, e );
233 				}
234 			}
235 			else return super.load( input, storepass, provider, type );
236 		}
237 
238 		@Override
239 		protected String getCryptoProvider()
240 		{
241 			return config.getCryptoProvider();
242 		}
243 	}
244 
245 	public String getCryptoProvider()
246 	{
247 		return config.getCryptoProvider();
248 	}
249 
250 	public void setCryptoProvider( String provider )
251 	{
252 		config.setCryptoProvider( provider );
253 		keyStore = null;
254 		getWssContainer().fireCryptoUpdated( this );
255 	}
256 
257 	public void resolve( ResolveContext context)
258 	{
259 		sourceProperty.resolveFile( context, "Missing keystore/certificate file");
260 	}
261 }