View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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 java.io.File;
16  import java.io.FileInputStream;
17  import java.io.IOException;
18  import java.io.InputStream;
19  import java.security.KeyStore;
20  import java.util.Properties;
21  
22  import org.apache.commons.ssl.KeyStoreBuilder;
23  import org.apache.commons.ssl.Util;
24  import org.apache.ws.security.components.crypto.CredentialException;
25  import org.apache.ws.security.components.crypto.Crypto;
26  import org.apache.ws.security.components.crypto.Merlin;
27  
28  import com.eviware.soapui.config.KeyMaterialCryptoConfig;
29  import com.eviware.soapui.config.WSSCryptoConfig;
30  import com.eviware.soapui.impl.wsdl.support.wss.DefaultWssContainer;
31  import com.eviware.soapui.impl.wsdl.support.wss.WssContainer;
32  import com.eviware.soapui.impl.wsdl.support.wss.WssCrypto;
33  import com.eviware.soapui.support.StringUtils;
34  import com.eviware.soapui.support.UISupport;
35  
36  public class KeyMaterialWssCrypto implements WssCrypto
37  {
38  	private KeyMaterialCryptoConfig config;
39  	private final WssContainer container;
40  	private KeyStore keyStore;
41  
42  	public KeyMaterialWssCrypto( KeyMaterialCryptoConfig config2, WssContainer container, String source, String password )
43  	{
44  		this( config2, container );
45  		setSource( source );
46  		setPassword( password );
47  	}
48  
49  	public KeyMaterialWssCrypto( KeyMaterialCryptoConfig cryptoConfig, WssContainer container2 )
50  	{
51  		config = cryptoConfig;
52  		container = container2;
53  	}
54  
55  	public Crypto getCrypto()
56  	{
57  		try
58  		{
59  			Properties properties = new Properties();
60  			properties.put( "org.apache.ws.security.crypto.merlin.file", getSource() );
61  			properties.put( "org.apache.ws.security.crypto.merlin.keystore.provider", "this" );
62  			if( StringUtils.hasContent( getDefaultAlias() ))
63  				properties.put( "org.apache.ws.security.crypto.merlin.keystore.alias", getDefaultAlias() );
64  			if( StringUtils.hasContent( getAliasPassword() ))
65  				properties.put( "org.apache.ws.security.crypto.merlin.alias.password", getAliasPassword() );
66  			
67  			return new KeyMaterialCrypto( properties );
68  		}
69  		catch( Exception e )
70  		{
71  			e.printStackTrace();
72  		}
73  		return null;
74  	}
75  	
76  	public String getLabel()
77  	{
78  		String source = getSource();
79  		
80  		int ix = source.lastIndexOf( File.separatorChar );
81  		if( ix == -1 )
82  			ix = source.lastIndexOf( '/' );
83  		
84  		if( ix != -1 )
85  			source = source.substring( ix+1 );
86  		
87  		return source;
88  	}
89  
90  	public String getSource()
91  	{
92  		return config.getSource();
93  	}
94  
95  	public void udpateConfig( KeyMaterialCryptoConfig config )
96  	{
97  		this.config = config;
98  	}
99  
100 	protected void setSource( String source )
101 	{
102 		config.setSource( source );
103 	}
104 
105 	public KeyStore load() throws Exception
106 	{
107 		if( keyStore != null )
108 			return keyStore;
109 		
110 		try
111 		{
112 			UISupport.setHourglassCursor();
113 			keyStore = KeyStoreBuilder.build( Util.streamToBytes( new FileInputStream( getSource() ) ), getPassword().toCharArray() );
114 			if( StringUtils.hasContent( getDefaultAlias() ) && StringUtils.hasContent( getAliasPassword() ))
115 			{
116 				keyStore.getKey( getDefaultAlias(), getAliasPassword().toCharArray() );
117 			}
118 			
119 			return keyStore;
120 		}
121 		finally
122 		{
123 			UISupport.resetCursor();
124 		}
125 	}
126 	
127 	public String getStatus() 
128 	{
129 		try
130 		{
131 			if( StringUtils.hasContent( getSource() ) && StringUtils.hasContent( getPassword() ))
132 			{
133 				load();
134 				return "OK";
135 			}
136 			else
137 			{
138 				return "<unavailable>";
139 			}
140 		}
141 		catch( Exception e )
142 		{
143 			return "<error: " + e.getMessage() + ">";
144 		}
145 	}
146 	
147 	public String getPassword()
148 	{
149 		return config.getPassword();
150 	}
151 
152 	public String getAliasPassword()
153 	{
154 		return config.getAliasPassword();
155 	}
156 
157 	public String getDefaultAlias()
158 	{
159 		return config.getDefaultAlias();
160 	}
161 
162 	public void setAliasPassword( String arg0 )
163 	{
164 		config.setAliasPassword( arg0 );
165 	}
166 
167 	public void setDefaultAlias( String arg0 )
168 	{
169 		config.setDefaultAlias( arg0 );
170 	}
171 
172 	public void setPassword( String arg0 )
173 	{
174 		config.setPassword( arg0 );
175 		keyStore = null;
176 		getWssContainer().fireCryptoUpdated( this );
177 	}
178 
179 	public void udpateConfig( WSSCryptoConfig config )
180 	{
181 //		this.config = config;
182 	}
183 	
184 	public String toString()
185 	{
186 		return getLabel();
187 	}
188 
189 	public DefaultWssContainer getWssContainer()
190 	{
191 		return ( DefaultWssContainer ) container;
192 	}
193 	
194 	private class KeyMaterialCrypto extends Merlin
195 	{
196 		private KeyMaterialCrypto(Properties properties) throws CredentialException, IOException
197 		{
198 			super( properties );
199 		}
200 
201 		@Override
202 		public KeyStore load( InputStream input, String storepass, String provider, String type ) throws CredentialException
203 		{
204 			if( "this".equals( provider ))
205 			{
206 				try
207 				{
208 					return KeyMaterialWssCrypto.this.load();
209 				}
210 				catch( Exception e )
211 				{
212 					throw new CredentialException( 0, null, e );
213 				}
214 			}
215 			else return super.load( input, storepass, provider, type );
216 		}
217 
218 		@Override
219 		protected String getCryptoProvider()
220 		{
221 			return config.getCryptoProvider();
222 		}
223 	}
224 
225 	public String getCryptoProvider()
226 	{
227 		return config.getCryptoProvider();
228 	}
229 
230 	public void setCryptoProvider( String provider )
231 	{
232 		config.setCryptoProvider( provider );
233 	}
234 }