1   /*
2    * Copyright 2008 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.apache.ws.security.components.crypto.Crypto;
22  import org.w3c.dom.Document;
23  
24  import org.springframework.ws.context.DefaultMessageContext;
25  import org.springframework.ws.context.MessageContext;
26  import org.springframework.ws.soap.SoapMessage;
27  import org.springframework.ws.soap.security.wss4j.callback.KeyStoreCallbackHandler;
28  import org.springframework.ws.soap.security.wss4j.support.CryptoFactoryBean;
29  
30  public abstract class Wss4jMessageInterceptorEncryptionTestCase extends Wss4jTestCase {
31  
32      protected Wss4jSecurityInterceptor interceptor;
33  
34      protected void onSetup() throws Exception {
35          interceptor = new Wss4jSecurityInterceptor();
36          interceptor.setValidationActions("Encrypt");
37          interceptor.setSecurementActions("Encrypt");
38  
39          KeyStoreCallbackHandler callbackHandler = new KeyStoreCallbackHandler();
40          callbackHandler.setPrivateKeyPassword("123456");
41          interceptor.setValidationCallbackHandler(callbackHandler);
42  
43          CryptoFactoryBean cryptoFactoryBean = new CryptoFactoryBean();
44  
45          Properties cryptoFactoryBeanConfig = new Properties();
46          cryptoFactoryBeanConfig.setProperty("org.apache.ws.security.crypto.provider",
47                  "org.apache.ws.security.components.crypto.Merlin");
48          cryptoFactoryBeanConfig.setProperty("org.apache.ws.security.crypto.merlin.keystore.type", "jceks");
49          cryptoFactoryBeanConfig.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", "123456");
50  
51          // from the class path
52          cryptoFactoryBeanConfig.setProperty("org.apache.ws.security.crypto.merlin.file", "private.jks");
53          cryptoFactoryBean.setConfiguration(cryptoFactoryBeanConfig);
54          cryptoFactoryBean.afterPropertiesSet();
55          interceptor.setValidationDecryptionCrypto((Crypto) cryptoFactoryBean
56                  .getObject());
57          interceptor.setSecurementEncryptionCrypto((Crypto) cryptoFactoryBean
58                  .getObject());
59  
60          interceptor.afterPropertiesSet();
61      }
62  
63      public void testDecryptRequest() throws Exception {
64          SoapMessage message = loadSoap11Message("encrypted-soap.xml");
65          MessageContext messageContext = new DefaultMessageContext(message, getSoap11MessageFactory());
66          interceptor.validateMessage(message, messageContext);
67          Document document = getDocument((SoapMessage) messageContext.getRequest());
68          assertXpathEvaluatesTo("Decryption error", "Hello", "/SOAP-ENV:Envelope/SOAP-ENV:Body/echo:echoRequest/text()",
69                  document);
70          assertXpathNotExists("Security Header not removed", "/SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security",
71                  getDocument(message));
72      }
73  
74      public void testEncryptResponse() throws Exception {
75          SoapMessage message = loadSoap11Message("empty-soap.xml");
76          MessageContext messageContext = getSoap11MessageContext(message);
77          interceptor.setSecurementEncryptionUser("rsakey");
78          interceptor.secureMessage(message, messageContext);
79          Document document = getDocument(message);
80          assertXpathExists("Encryption error", "/SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security/xenc:EncryptedKey",
81                  document);
82      }
83  }