1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.ws.security.message;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import javax.security.auth.callback.CallbackHandler;
26  
27  import org.apache.ws.security.WSConstants;
28  import org.apache.ws.security.WSEncryptionPart;
29  import org.apache.ws.security.WSSConfig;
30  import org.apache.ws.security.WSSecurityEngine;
31  import org.apache.ws.security.WSSecurityEngineResult;
32  import org.apache.ws.security.common.KeystoreCallbackHandler;
33  import org.apache.ws.security.common.SOAPUtil;
34  import org.apache.ws.security.components.crypto.Crypto;
35  import org.apache.ws.security.components.crypto.CryptoFactory;
36  import org.apache.ws.security.util.WSSecurityUtil;
37  import org.w3c.dom.Document;
38  import org.w3c.dom.Element;
39  
40  /**
41   * This test encrypts a Timestamp and the SOAP Body, and appends the ReferenceList Element after the
42   * EncryptedData Element that is the Timestamp. When processing, the EncryptedData Element gets decrypted,
43   * and then the ReferenceListProcessor must check to see whether the Data Reference pointing to the 
44   * encrypted Timestamp needs to be decrypted or not.
45   */
46  public class EncryptedDataInHeaderTest extends org.junit.Assert {
47      private static final org.apache.commons.logging.Log LOG = 
48          org.apache.commons.logging.LogFactory.getLog(EncryptedDataInHeaderTest.class);
49  
50      private WSSecurityEngine secEngine = new WSSecurityEngine();
51      private CallbackHandler callbackHandler = new KeystoreCallbackHandler();
52      private Crypto crypto = null;
53      
54      public EncryptedDataInHeaderTest() throws Exception {
55          crypto = CryptoFactory.getInstance();
56          WSSConfig.init();
57      }
58  
59      @org.junit.Test
60      public void testEncryptedDataInHeader() throws Exception {
61          Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
62          WSSecHeader secHeader = new WSSecHeader();
63          secHeader.insertSecurityHeader(doc);
64          
65          WSSecTimestamp timestamp = new WSSecTimestamp();
66          timestamp.setTimeToLive(300);
67          timestamp.build(doc, secHeader);
68          
69          // Encrypt the Timestamp and SOAP Body
70          List<WSEncryptionPart> parts = new ArrayList<WSEncryptionPart>();
71          WSEncryptionPart encP =
72              new WSEncryptionPart(
73                  "Timestamp", WSConstants.WSU_NS, "");
74          parts.add(encP);
75          String soapNamespace = WSSecurityUtil.getSOAPNamespace(doc.getDocumentElement());
76          encP = 
77              new WSEncryptionPart(
78                  WSConstants.ELEM_BODY, soapNamespace, "Content"
79              );
80          parts.add(encP);
81          
82          WSSecEncrypt encrypt = new WSSecEncrypt();
83          encrypt.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
84          encrypt.setKeyIdentifierType(WSConstants.ISSUER_SERIAL);
85          encrypt.setParts(parts);
86          
87          encrypt.prepare(doc, crypto);
88          encrypt.prependToHeader(secHeader);
89          
90          // Append Reference List to security header
91          Element refs = encrypt.encryptForRef(null, parts);
92          secHeader.getSecurityHeader().appendChild(refs);
93  
94          if (LOG.isDebugEnabled()) {
95              String outputString = 
96                  org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
97              LOG.debug(outputString);
98          }
99          
100         List<WSSecurityEngineResult> results = verify(doc);
101         WSSecurityEngineResult actionResult = 
102             WSSecurityUtil.fetchActionResult(results, WSConstants.ENCR);
103         assertTrue(actionResult != null);
104         assertFalse(actionResult.isEmpty());
105     }
106     
107     
108     /**
109      * Verifies the soap envelope
110      * <p/>
111      * 
112      * @param doc 
113      * @throws Exception Thrown when there is a problem in verification
114      */
115     private List<WSSecurityEngineResult> verify(Document doc) throws Exception {
116         List<WSSecurityEngineResult> results = 
117             secEngine.processSecurityHeader(doc, null, callbackHandler, null, crypto);
118         if (LOG.isDebugEnabled()) {
119             LOG.debug("Verified and decrypted message:");
120             String outputString = 
121                 org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
122             LOG.debug(outputString);
123         }
124         return results;
125     }
126 
127 }