1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.soap.security.wss4j;
18  
19  import java.util.Properties;
20  
21  import org.springframework.ws.context.DefaultMessageContext;
22  import org.springframework.ws.context.MessageContext;
23  import org.springframework.ws.soap.SoapMessage;
24  import org.springframework.ws.soap.security.wss4j.callback.KeyStoreCallbackHandler;
25  import org.springframework.ws.soap.security.wss4j.support.CryptoFactoryBean;
26  
27  import org.apache.ws.security.components.crypto.Crypto;
28  import org.junit.Test;
29  import org.w3c.dom.Document;
30  
31  public abstract class Wss4jMessageInterceptorEncryptionTestCase extends Wss4jTestCase {
32  
33      protected Wss4jSecurityInterceptor interceptor;
34  
35      @Override
36      protected void onSetup() throws Exception {
37          interceptor = new Wss4jSecurityInterceptor();
38          interceptor.setValidationActions("Encrypt");
39          interceptor.setSecurementActions("Encrypt");
40  
41          KeyStoreCallbackHandler callbackHandler = new KeyStoreCallbackHandler();
42          callbackHandler.setPrivateKeyPassword("123456");
43          interceptor.setValidationCallbackHandler(callbackHandler);
44  
45          CryptoFactoryBean cryptoFactoryBean = new CryptoFactoryBean();
46  
47          Properties cryptoFactoryBeanConfig = new Properties();
48          cryptoFactoryBeanConfig.setProperty("org.apache.ws.security.crypto.provider",
49                  "org.apache.ws.security.components.crypto.Merlin");
50          cryptoFactoryBeanConfig.setProperty("org.apache.ws.security.crypto.merlin.keystore.type", "jceks");
51          cryptoFactoryBeanConfig.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", "123456");
52  
53          // from the class path
54          cryptoFactoryBeanConfig.setProperty("org.apache.ws.security.crypto.merlin.file", "private.jks");
55          cryptoFactoryBean.setConfiguration(cryptoFactoryBeanConfig);
56          cryptoFactoryBean.afterPropertiesSet();
57          interceptor.setValidationDecryptionCrypto((Crypto) cryptoFactoryBean
58                  .getObject());
59          interceptor.setSecurementEncryptionCrypto((Crypto) cryptoFactoryBean
60                  .getObject());
61  
62          interceptor.afterPropertiesSet();
63      }
64  
65      @Test
66      public void testDecryptRequest() throws Exception {
67          SoapMessage message = loadSoap11Message("encrypted-soap.xml");
68          MessageContext messageContext = new DefaultMessageContext(message, getSoap11MessageFactory());
69          interceptor.validateMessage(message, messageContext);
70          Document document = getDocument((SoapMessage) messageContext.getRequest());
71          assertXpathEvaluatesTo("Decryption error", "Hello", "/SOAP-ENV:Envelope/SOAP-ENV:Body/echo:echoRequest/text()",
72                  document);
73          assertXpathNotExists("Security Header not removed", "/SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security",
74                  getDocument(message));
75      }
76  
77      @Test
78      public void testEncryptResponse() throws Exception {
79          SoapMessage message = loadSoap11Message("empty-soap.xml");
80          MessageContext messageContext = getSoap11MessageContext(message);
81          interceptor.setSecurementEncryptionUser("rsakey");
82          interceptor.secureMessage(message, messageContext);
83          Document document = getDocument(message);
84          assertXpathExists("Encryption error", "/SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security/xenc:EncryptedKey",
85                  document);
86      }
87  }