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.entries;
14  
15  import java.awt.event.ItemEvent;
16  import java.awt.event.ItemListener;
17  import java.io.StringWriter;
18  import java.util.List;
19  
20  import javax.swing.JComponent;
21  import javax.swing.JScrollPane;
22  import javax.swing.JTextField;
23  
24  import org.apache.ws.security.WSConstants;
25  import org.apache.ws.security.components.crypto.Crypto;
26  import org.apache.ws.security.message.WSSecEncrypt;
27  import org.apache.ws.security.message.WSSecHeader;
28  import org.w3c.dom.Document;
29  
30  import com.eviware.soapui.SoapUI;
31  import com.eviware.soapui.config.WSSEntryConfig;
32  import com.eviware.soapui.impl.wsdl.support.wss.OutgoingWss;
33  import com.eviware.soapui.impl.wsdl.support.wss.WssCrypto;
34  import com.eviware.soapui.impl.wsdl.support.wss.support.KeystoresComboBoxModel;
35  import com.eviware.soapui.impl.wsdl.support.wss.support.WSPartsTable;
36  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
37  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionsResult;
38  import com.eviware.soapui.support.StringUtils;
39  import com.eviware.soapui.support.components.SimpleBindingForm;
40  import com.eviware.soapui.support.types.StringToStringMap;
41  import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
42  import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
43  import com.eviware.soapui.support.xml.XmlUtils;
44  import com.jgoodies.binding.PresentationModel;
45  
46  public class AddEncryptionEntry extends WssEntryBase
47  {
48  	private static final String DEFAULT_OPTION = "<default>";
49  	public static final String TYPE = "Encryption";
50  	private String crypto;
51  	private int keyIdentifierType;
52  	private String symmetricEncAlgorithm;
53  	private String encKeyTransport;
54  	private List<StringToStringMap> parts;
55  	private String embeddedKeyName;
56  	private String embeddedKeyPassword;
57  	private String encryptionCanonicalization;
58  	private JTextField embeddedKeyNameTextField;
59  	private JTextField embeddedKeyNamePassword;
60  	private boolean encryptSymmetricKey;
61  	private KeyAliasComboBoxModel keyAliasComboBoxModel;
62  	private InternalWssContainerListener wssContainerListener;
63  
64  	public void init( WSSEntryConfig config, OutgoingWss container )
65  	{
66  		super.init( config, container, TYPE );
67  	}
68  
69  	@Override
70  	protected JComponent buildUI()
71  	{
72  		SimpleBindingForm form = new SimpleBindingForm( new PresentationModel<AddSignatureEntry>( this ) );
73  		
74  		form.addSpace(5);
75  		wssContainerListener = new InternalWssContainerListener();
76  		getWssContainer().addWssContainerListener( wssContainerListener );
77  
78  		KeystoresComboBoxModel keystoresComboBoxModel = new KeystoresComboBoxModel( getWssContainer(), getWssContainer().getCryptoByName( crypto ) );
79  		form.appendComboBox( "crypto", "Keystore", keystoresComboBoxModel, "Selects the Keystore containing the key to use for signing" ).addItemListener( 
80  								new ItemListener() {
81  
82  									public void itemStateChanged( ItemEvent e )
83  									{
84  										keyAliasComboBoxModel.update( getWssContainer().getCryptoByName( crypto ) );
85  									}} );
86  
87  		keyAliasComboBoxModel = new KeyAliasComboBoxModel( getWssContainer().getCryptoByName( crypto ) );
88  		form.appendComboBox( "username", "Alias", keyAliasComboBoxModel, "The alias for the key to use for encryption" );
89  
90  		
91  		form.appendTextField( "password", "Password", "The password for the key to use for encryption (if it is private)" );
92  
93  		form.appendComboBox( "keyIdentifierType", "Key Identifier Type", new Integer[] { 0, 1, 2, 3, 4, 5, 6, 8 },
94  					"Sets which key identifier to use" ).setRenderer( new KeyIdentifierTypeRenderer() );
95  
96  		( embeddedKeyNameTextField = form.appendTextField( "embeddedKeyName", "Embedded Key Name" ) )
97  					.setEnabled( keyIdentifierType == WSConstants.EMBEDDED_KEYNAME );
98  		( embeddedKeyNamePassword = form.appendTextField( "embeddedKeyPassword", "Embedded Key Password" ) )
99  					.setEnabled( keyIdentifierType == WSConstants.EMBEDDED_KEYNAME );
100 
101 		form.appendComboBox( "symmetricEncAlgorithm", "Symmetric Encoding Algorithm", new String[] { DEFAULT_OPTION,
102 					WSConstants.AES_128, WSConstants.AES_192, WSConstants.AES_256, WSConstants.TRIPLE_DES },
103 					"Set the name of the symmetric encryption algorithm to use" );
104 
105 		form.appendComboBox( "encKeyTransport", "Key Encryption Algorithm", new String[] { DEFAULT_OPTION,
106 					WSConstants.KEYTRANSPORT_RSA15, WSConstants.KEYTRANSPORT_RSAOEP },
107 					"Sets the algorithm to encode the symmetric key" );
108 
109 		form.appendComboBox( "encryptionCanonicalization", "Encryption Canonicalization", new String[] { DEFAULT_OPTION,
110 					WSConstants.C14N_OMIT_COMMENTS, WSConstants.C14N_WITH_COMMENTS, WSConstants.C14N_EXCL_OMIT_COMMENTS,
111 					WSConstants.C14N_EXCL_WITH_COMMENTS },
112 					"Set the name of an optional canonicalization algorithm to use before encryption" );
113 
114 		form.appendCheckBox( "encryptSymmetricKey", "Create Encrypted Key",
115 					"Indicates whether to encrypt the symmetric key into an EncryptedKey or not" );
116 
117 		form.append( "Parts", new WSPartsTable( parts ) );
118 
119 		return new JScrollPane( form.getPanel() );
120 	}
121 	
122 	public void release()
123 	{
124 		if( wssContainerListener != null )
125 			getWssContainer().removeWssContainerListener( wssContainerListener );
126 	}
127 
128 	@Override
129 	protected void load( XmlObjectConfigurationReader reader )
130 	{
131 		crypto = reader.readString( "crypto", null );
132 		keyIdentifierType = reader.readInt( "keyIdentifierType", 0 );
133 		symmetricEncAlgorithm = reader.readString( "symmetricEncAlgorithm", null );
134 		encKeyTransport = reader.readString( "encKeyTransport", null );
135 		embeddedKeyName = reader.readString( "embeddedKeyName", null );
136 		embeddedKeyPassword = reader.readString( "embeddedKeyPassword", null );
137 		encryptionCanonicalization = reader.readString( "encryptionCanonicalization", null );
138 		encryptSymmetricKey = reader.readBoolean( "encryptSymmetricKey", true );
139 		parts = readParts( reader, "encryptionPart" );
140 	}
141 
142 	@Override
143 	protected void save( XmlObjectConfigurationBuilder builder )
144 	{
145 		builder.add( "crypto", crypto );
146 		builder.add( "keyIdentifierType", keyIdentifierType );
147 		builder.add( "symmetricEncAlgorithm", symmetricEncAlgorithm );
148 		builder.add( "encKeyTransport", encKeyTransport );
149 		builder.add( "embeddedKeyName", embeddedKeyName );
150 		builder.add( "embeddedKeyPassword", embeddedKeyPassword );
151 		builder.add( "encryptionCanonicalization", encryptionCanonicalization );
152 		builder.add( "encryptSymmetricKey", encryptSymmetricKey );
153 		saveParts( builder, parts, "encryptionPart" );
154 	}
155 
156 	public String getEmbeddedKeyName()
157 	{
158 		return embeddedKeyName;
159 	}
160 
161 	public void setEmbeddedKeyName( String embeddedKeyName )
162 	{
163 		this.embeddedKeyName = embeddedKeyName;
164 		saveConfig();
165 	}
166 
167 	public String getEmbeddedKeyPassword()
168 	{
169 		return embeddedKeyPassword;
170 	}
171 
172 	public void setEmbeddedKeyPassword( String embeddedKeyPassword )
173 	{
174 		this.embeddedKeyPassword = embeddedKeyPassword;
175 		saveConfig();
176 	}
177 
178 	public String getEncKeyTransport()
179 	{
180 		return StringUtils.isNullOrEmpty( encKeyTransport ) ? DEFAULT_OPTION : encKeyTransport;
181 	}
182 
183 	public void setEncKeyTransport( String encKeyTransport )
184 	{
185 		if( DEFAULT_OPTION.equals( encKeyTransport ) )
186 			encKeyTransport = null;
187 
188 		this.encKeyTransport = encKeyTransport;
189 		saveConfig();
190 	}
191 
192 	public String getEncryptionCanonicalization()
193 	{
194 		return StringUtils.isNullOrEmpty( encryptionCanonicalization ) ? DEFAULT_OPTION : encryptionCanonicalization;
195 	}
196 
197 	public void setEncryptionCanonicalization( String encryptionCanonicalization )
198 	{
199 		if( DEFAULT_OPTION.equals( encryptionCanonicalization ) )
200 			encryptionCanonicalization = null;
201 
202 		this.encryptionCanonicalization = encryptionCanonicalization;
203 		saveConfig();
204 	}
205 
206 	public boolean isEncryptSymmetricKey()
207 	{
208 		return encryptSymmetricKey;
209 	}
210 
211 	public void setEncryptSymmetricKey( boolean encryptSymmetricKey )
212 	{
213 		this.encryptSymmetricKey = encryptSymmetricKey;
214 		saveConfig();
215 	}
216 
217 	public int getKeyIdentifierType()
218 	{
219 		return keyIdentifierType;
220 	}
221 
222 	public void setKeyIdentifierType( int keyIdentifierType )
223 	{
224 		this.keyIdentifierType = keyIdentifierType;
225 
226 		if( embeddedKeyNameTextField != null )
227 		{
228 			embeddedKeyNameTextField.setEnabled( keyIdentifierType == WSConstants.EMBEDDED_KEYNAME );
229 			embeddedKeyNamePassword.setEnabled( keyIdentifierType == WSConstants.EMBEDDED_KEYNAME );
230 		}
231 		saveConfig();
232 	}
233 
234 	public String getSymmetricEncAlgorithm()
235 	{
236 		return StringUtils.isNullOrEmpty( symmetricEncAlgorithm ) ? DEFAULT_OPTION : symmetricEncAlgorithm;
237 	}
238 
239 	public void setSymmetricEncAlgorithm( String symmetricEncAlgorithm )
240 	{
241 		if( DEFAULT_OPTION.equals( symmetricEncAlgorithm ) )
242 			symmetricEncAlgorithm = null;
243 		
244 		this.symmetricEncAlgorithm = symmetricEncAlgorithm;
245 		saveConfig();
246 	}
247 
248 	public void process( WSSecHeader secHeader, Document doc, PropertyExpansionContext context )
249 	{
250 		StringWriter writer = null;
251 		
252 		try
253 		{
254 			WSSecEncrypt wsEncrypt = new WSSecEncrypt();
255 			WssCrypto wssCrypto = getWssContainer().getCryptoByName( crypto );
256 			Crypto crypto = wssCrypto.getCrypto();
257 
258 			wsEncrypt.setUserInfo( getUsername() );
259 
260 			if( getKeyIdentifierType() != 0 )
261 			{
262 				wsEncrypt.setKeyIdentifierType( getKeyIdentifierType() );
263 			}
264 
265 			if( getKeyIdentifierType() == WSConstants.EMBEDDED_KEYNAME )
266 			{
267 				wsEncrypt.setEmbeddedKeyName( getEmbeddedKeyName() );
268 				wsEncrypt.setKey( crypto.getPrivateKey( getEmbeddedKeyName(), getEmbeddedKeyPassword() ).getEncoded() );
269 			}
270 
271 			if( getSymmetricEncAlgorithm() != null )
272 			{
273 				wsEncrypt.setSymmetricEncAlgorithm( getSymmetricEncAlgorithm() );
274 			}
275 			
276 			if( getEncKeyTransport() != null )
277 			{
278 				wsEncrypt.setKeyEnc( getEncKeyTransport() );
279 			}
280 			
281 			if( getEncryptionCanonicalization() != null )
282 			{
283 				wsEncrypt.setEncCanonicalization( getEncryptionCanonicalization() );
284 			}
285 			
286 			wsEncrypt.setEncryptSymmKey( isEncryptSymmetricKey() );
287 
288 			if( parts.size() > 0 )
289 			{
290 				wsEncrypt.setParts( createWSParts( parts ) );
291 			}
292 
293 			// create backup
294 			writer = new StringWriter();
295 			XmlUtils.serialize( doc, writer );
296 			
297 			wsEncrypt.build( doc, crypto, secHeader );
298 		}
299 		catch( Exception e )
300 		{
301 			SoapUI.logError( e );
302 			
303 			if( writer != null && writer.getBuffer().length() > 0)
304 			{
305 				try
306 				{
307 					// try to restore.. 
308 					doc.replaceChild( doc.importNode( XmlUtils.parseXml( writer.toString() ).getDocumentElement(), true ), doc.getDocumentElement() );
309 				}
310 				catch( Exception e1 )
311 				{
312 					SoapUI.logError( e1 );
313 				}
314 			}
315 		}
316 	}
317 
318 	@Override
319 	protected void addPropertyExpansions( PropertyExpansionsResult result )
320 	{
321 		super.addPropertyExpansions( result );
322 	}
323 
324 	public String getCrypto()
325 	{
326 		return crypto;
327 	}
328 
329 	public void setCrypto( String crypto )
330 	{
331 		this.crypto = crypto;
332 		saveConfig();
333 	}
334 	
335 	private final class InternalWssContainerListener extends WssContainerListenerAdapter
336 	{
337 		@Override
338 		public void cryptoUpdated( WssCrypto crypto )
339 		{
340 			if( crypto.getLabel().equals( getCrypto()))
341 				keyAliasComboBoxModel.update( crypto );
342 		}
343 	}
344 }