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.ArrayList;
19  import java.util.List;
20  import java.util.Vector;
21  
22  import javax.swing.JComponent;
23  import javax.swing.JScrollPane;
24  
25  import org.apache.ws.security.WSConstants;
26  import org.apache.ws.security.WSEncryptionPart;
27  import org.apache.ws.security.message.WSSecHeader;
28  import org.apache.ws.security.message.WSSecSignature;
29  import org.apache.xml.security.signature.XMLSignature;
30  import org.w3c.dom.Document;
31  
32  import com.eviware.soapui.SoapUI;
33  import com.eviware.soapui.config.WSSEntryConfig;
34  import com.eviware.soapui.impl.wsdl.support.wss.OutgoingWss;
35  import com.eviware.soapui.impl.wsdl.support.wss.WssCrypto;
36  import com.eviware.soapui.impl.wsdl.support.wss.support.KeystoresComboBoxModel;
37  import com.eviware.soapui.impl.wsdl.support.wss.support.WSPartsTable;
38  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
39  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionsResult;
40  import com.eviware.soapui.support.StringUtils;
41  import com.eviware.soapui.support.components.SimpleBindingForm;
42  import com.eviware.soapui.support.types.StringToStringMap;
43  import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
44  import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
45  import com.eviware.soapui.support.xml.XmlUtils;
46  import com.jgoodies.binding.PresentationModel;
47  
48  public class AddSignatureEntry extends WssEntryBase
49  {
50  	private static final String DEFAULT_OPTION = "<default>";
51  	public static final String TYPE = "Signature";
52  	private String crypto;
53  	private int keyIdentifierType = 0;
54  	private String signatureAlgorithm;
55  	private boolean useSingleCert;
56  	private String signatureCanonicalization;
57  	private List<StringToStringMap> parts = new ArrayList<StringToStringMap>();
58  	private com.eviware.soapui.impl.wsdl.support.wss.entries.WssEntryBase.KeyAliasComboBoxModel keyAliasComboBoxModel;
59  	private com.eviware.soapui.impl.wsdl.support.wss.entries.AddSignatureEntry.InternalWssContainerListener wssContainerListener;
60  
61  	public void init( WSSEntryConfig config, OutgoingWss container )
62  	{
63  		super.init( config, container, TYPE );
64  	}
65  
66  	@Override
67  	protected JComponent buildUI()
68  	{
69  		SimpleBindingForm form = new SimpleBindingForm( new PresentationModel<AddSignatureEntry>( this ) );
70  		form.addSpace(5);
71  		wssContainerListener = new InternalWssContainerListener();
72  		getWssContainer().addWssContainerListener( wssContainerListener );
73  
74  		form.appendComboBox( "crypto", "Keystore", new KeystoresComboBoxModel( getWssContainer(), getWssContainer()
75  					.getCryptoByName( crypto ) ), "Selects the Keystore containing the key to use for signing" ).addItemListener( 
76  								new ItemListener() {
77  
78  									public void itemStateChanged( ItemEvent e )
79  									{
80  										keyAliasComboBoxModel.update( getWssContainer().getCryptoByName( crypto ) );
81  									}} );
82  
83  		keyAliasComboBoxModel = new KeyAliasComboBoxModel( getWssContainer().getCryptoByName( crypto ) );
84  		form.appendComboBox( "username", "Alias", keyAliasComboBoxModel, "The alias for the key to use for encryption" );
85  		
86  //		form.appendTextField( "username", "Alias", "The certificate alias" );
87  		form.appendTextField( "password", "Password", "The certificate password" );
88  
89  		form.appendComboBox( "keyIdentifierType", "Key Identifier Type", new Integer[] { 0, 1, 2, 3, 4 },
90  					"Sets which key identifier to use" ).setRenderer( new KeyIdentifierTypeRenderer() );
91  		form.appendComboBox( "signatureAlgorithm", "Signature Algorithm",
92  					new String[] { DEFAULT_OPTION, WSConstants.RSA, WSConstants.DSA,
93  					XMLSignature.ALGO_ID_MAC_HMAC_SHA1, XMLSignature.ALGO_ID_MAC_HMAC_SHA256,
94  					XMLSignature.ALGO_ID_MAC_HMAC_SHA384, XMLSignature.ALGO_ID_MAC_HMAC_SHA512,
95  					XMLSignature.ALGO_ID_MAC_HMAC_RIPEMD160, XMLSignature.ALGO_ID_MAC_HMAC_NOT_RECOMMENDED_MD5,
96  					XMLSignature.ALGO_ID_SIGNATURE_ECDSA_SHA1, XMLSignature.ALGO_ID_SIGNATURE_NOT_RECOMMENDED_RSA_MD5,
97  					XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1, XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256, 
98  					XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA384, XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA512,
99  					XMLSignature.ALGO_ID_SIGNATURE_RSA_RIPEMD160
100 					},
101 					"Set the name of the signature encryption algorithm to use" );
102 		form.appendComboBox( "signatureCanonicalization", "Signature Canonicalization", new String[] { DEFAULT_OPTION,
103 					WSConstants.C14N_OMIT_COMMENTS, WSConstants.C14N_WITH_COMMENTS, WSConstants.C14N_EXCL_OMIT_COMMENTS,
104 					WSConstants.C14N_EXCL_WITH_COMMENTS }, "Set the canonicalization method to use." );
105 
106 		form.appendCheckBox( "useSingleCert", "Use Single Certificate", "Use single certificate for signing" );
107 		
108 		form.append( "Parts", new WSPartsTable( parts ) );
109 
110 		return new JScrollPane( form.getPanel() );
111 	}
112 	
113 	public void release()
114 	{
115 		if( wssContainerListener != null )
116 			getWssContainer().removeWssContainerListener( wssContainerListener );
117 	}
118 
119 	@Override
120 	protected void load( XmlObjectConfigurationReader reader )
121 	{
122 		crypto = reader.readString( "crypto", null );
123 		keyIdentifierType = reader.readInt( "keyIdentifierType", 0 );
124 		signatureAlgorithm = reader.readString( "signatureAlgorithm", null );
125 		signatureCanonicalization = reader.readString( "signatureCanonicalization", null );
126 		useSingleCert = reader.readBoolean( "useSingleCert", false );
127 		parts = readParts( reader, "signaturePart" );
128 	}
129 
130 	@Override
131 	protected void save( XmlObjectConfigurationBuilder builder )
132 	{
133 		builder.add( "crypto", crypto );
134 		builder.add( "keyIdentifierType", keyIdentifierType );
135 		builder.add( "signatureAlgorithm", signatureAlgorithm );
136 		builder.add( "signatureCanonicalization", signatureCanonicalization );
137 		builder.add( "useSingleCert", useSingleCert );
138 		saveParts( builder, parts, "signaturePart" );
139 	}
140 
141 	public void process( WSSecHeader secHeader, Document doc, PropertyExpansionContext context )
142 	{
143 		StringWriter writer = null;
144 		
145 		try
146 		{
147 			WSSecSignature wssSign = new WSSecSignature();
148 			wssSign.setUserInfo( context.expand( getUsername() ), context.expand( getPassword() ));
149 			WssCrypto wssCrypto = getWssContainer().getCryptoByName( crypto );
150 
151 			if( keyIdentifierType != 0 )
152 				wssSign.setKeyIdentifierType( keyIdentifierType );
153 
154 			if( StringUtils.hasContent( signatureAlgorithm ) )
155 				wssSign.setSignatureAlgorithm( signatureAlgorithm );
156 
157 			if( StringUtils.hasContent( signatureCanonicalization ) )
158 				wssSign.setSigCanonicalization( signatureCanonicalization );
159 
160 			wssSign.setUseSingleCertificate( useSingleCert );
161 
162 			Vector<WSEncryptionPart> wsParts = createWSParts( parts );
163 			if( wsParts.size() > 0 )
164 			{
165 				wssSign.setParts( wsParts );
166 			}
167 			
168 			writer = new StringWriter();
169 			XmlUtils.serialize( doc, writer );
170 			
171 			wssSign.build( doc, wssCrypto.getCrypto(), secHeader );
172 		}
173 		catch( Exception e )
174 		{
175 			SoapUI.logError( e );
176 			
177 			if( writer != null && writer.getBuffer().length() > 0 )
178 			{
179 				try
180 				{
181 					doc.replaceChild( doc.importNode( XmlUtils.parseXml( writer.toString() ).getDocumentElement(), true ), doc.getDocumentElement() );
182 				}
183 				catch( Exception e1 )
184 				{
185 					SoapUI.logError( e1 );
186 				}
187 			}
188 		}
189 	}
190 
191 	@Override
192 	protected void addPropertyExpansions( PropertyExpansionsResult result )
193 	{
194 		super.addPropertyExpansions( result );
195 	}
196 
197 	public String getCrypto()
198 	{
199 		return crypto;
200 	}
201 
202 	public void setCrypto( String crypto )
203 	{
204 		this.crypto = crypto;
205 		saveConfig();
206 	}
207 
208 	public int getKeyIdentifierType()
209 	{
210 		return keyIdentifierType;
211 	}
212 
213 	public void setKeyIdentifierType( int keyIdentifierType )
214 	{
215 		this.keyIdentifierType = keyIdentifierType;
216 		saveConfig();
217 	}
218 
219 	public String getSignatureAlgorithm()
220 	{
221 		return StringUtils.isNullOrEmpty( signatureAlgorithm ) ? DEFAULT_OPTION : signatureAlgorithm;
222 	}
223 
224 	public void setSignatureAlgorithm( String signatureAlgorithm )
225 	{
226 		if( DEFAULT_OPTION.equals( signatureAlgorithm ))
227 			signatureAlgorithm = null;
228 		
229 		this.signatureAlgorithm = signatureAlgorithm;
230 		saveConfig();
231 	}
232 
233 	public String getSignatureCanonicalization()
234 	{
235 		return StringUtils.isNullOrEmpty( signatureCanonicalization ) ? DEFAULT_OPTION : signatureCanonicalization;
236 	}
237 
238 	public void setSignatureCanonicalization( String signatureCanonicalization )
239 	{
240 		if( DEFAULT_OPTION.equals( signatureCanonicalization ))
241 			signatureCanonicalization = null;
242 		
243 		this.signatureCanonicalization = signatureCanonicalization;
244 		saveConfig();
245 	}
246 
247 	public boolean isUseSingleCert()
248 	{
249 		return useSingleCert;
250 	}
251 
252 	public void setUseSingleCert( boolean useSingleCert )
253 	{
254 		this.useSingleCert = useSingleCert;
255 		saveConfig();
256 	}
257 	
258 	private final class InternalWssContainerListener extends WssContainerListenerAdapter
259 	{
260 		@Override
261 		public void cryptoUpdated( WssCrypto crypto )
262 		{
263 			if( crypto.getLabel().equals( getCrypto()))
264 				keyAliasComboBoxModel.update( crypto );
265 		}
266 	}
267 }