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