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.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 public void setSource( String source )
101 {
102 config.setSource( source );
103 keyStore = null;
104 getWssContainer().fireCryptoUpdated( this );
105 }
106
107 public KeyStore load() throws Exception
108 {
109 if( keyStore != null )
110 return keyStore;
111
112 try
113 {
114 UISupport.setHourglassCursor();
115 keyStore = KeyStoreBuilder.build( Util.streamToBytes( new FileInputStream( getSource() ) ), getPassword().toCharArray(), getCryptoProvider() );
116 if( StringUtils.hasContent( getDefaultAlias() ) && StringUtils.hasContent( getAliasPassword() ))
117 {
118 keyStore.getKey( getDefaultAlias(), getAliasPassword().toCharArray() );
119 }
120
121 return keyStore;
122 }
123 catch( Throwable t )
124 {
125 throw new Exception( t );
126 }
127 finally
128 {
129 UISupport.resetCursor();
130 }
131 }
132
133 public String getStatus()
134 {
135 try
136 {
137 if( StringUtils.hasContent( getSource() ) && StringUtils.hasContent( getPassword() ))
138 {
139 load();
140 return "OK";
141 }
142 else
143 {
144 return "<unavailable>";
145 }
146 }
147 catch( Exception e )
148 {
149 return "<error: " + e.getMessage() + ">";
150 }
151 }
152
153 public String getPassword()
154 {
155 return config.getPassword();
156 }
157
158 public String getAliasPassword()
159 {
160 return config.getAliasPassword();
161 }
162
163 public String getDefaultAlias()
164 {
165 return config.getDefaultAlias();
166 }
167
168 public void setAliasPassword( String arg0 )
169 {
170 config.setAliasPassword( arg0 );
171 }
172
173 public void setDefaultAlias( String arg0 )
174 {
175 config.setDefaultAlias( arg0 );
176 }
177
178 public void setPassword( String arg0 )
179 {
180 config.setPassword( arg0 );
181 keyStore = null;
182 getWssContainer().fireCryptoUpdated( this );
183 }
184
185 public void udpateConfig( WSSCryptoConfig config )
186 {
187
188 }
189
190 public String toString()
191 {
192 return getLabel();
193 }
194
195 public DefaultWssContainer getWssContainer()
196 {
197 return ( DefaultWssContainer ) container;
198 }
199
200 private class KeyMaterialCrypto extends Merlin
201 {
202 private KeyMaterialCrypto(Properties properties) throws CredentialException, IOException
203 {
204 super( properties );
205 }
206
207 @Override
208 public KeyStore load( InputStream input, String storepass, String provider, String type ) throws CredentialException
209 {
210 if( "this".equals( provider ))
211 {
212 try
213 {
214 return KeyMaterialWssCrypto.this.load();
215 }
216 catch( Exception e )
217 {
218 throw new CredentialException( 0, null, e );
219 }
220 }
221 else return super.load( input, storepass, provider, type );
222 }
223
224 @Override
225 protected String getCryptoProvider()
226 {
227 return config.getCryptoProvider();
228 }
229 }
230
231 public String getCryptoProvider()
232 {
233 return config.getCryptoProvider();
234 }
235
236 public void setCryptoProvider( String provider )
237 {
238 config.setCryptoProvider( provider );
239 keyStore = null;
240 getWssContainer().fireCryptoUpdated( this );
241 }
242 }