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.processor;
21  
22  import java.util.List;
23  import java.util.ArrayList;
24  
25  import javax.security.auth.callback.CallbackHandler;
26  
27  import org.apache.ws.security.WSConstants;
28  import org.apache.ws.security.WSDataRef;
29  import org.apache.ws.security.WSEncryptionPart;
30  import org.apache.ws.security.WSSConfig;
31  import org.apache.ws.security.WSSecurityEngine;
32  import org.apache.ws.security.WSSecurityEngineResult;
33  import org.apache.ws.security.common.KeystoreCallbackHandler;
34  import org.apache.ws.security.common.SOAPUtil;
35  import org.apache.ws.security.components.crypto.Crypto;
36  import org.apache.ws.security.components.crypto.CryptoFactory;
37  import org.apache.ws.security.message.WSSecEncrypt;
38  import org.apache.ws.security.message.WSSecHeader;
39  import org.w3c.dom.Document;
40  import org.w3c.dom.Element;
41  
42  /**
43   * Test that checks for correct WSDataRef which should be returned by 
44   * <code>org.apache.ws.security.processor.ReferenceListProcessor</code> 
45   * 
46   * This test uses the RSA_15 algorithm to transport (wrap) the symmetric key.
47   * The test case creates a ReferenceList element that references EncryptedData
48   * elements. The ReferencesList element is put into the Security header, not
49   * as child of the EncryptedKey. The EncryptedData elements contain a KeyInfo
50   * that references the EncryptedKey via a STR/Reference structure.
51   * 
52   * WSDataRef object must contain the correct QName of the decrypted element. 
53   * 
54   */
55  public class ReferenceListDataRefTest extends org.junit.Assert {
56      private static final org.apache.commons.logging.Log LOG = 
57          org.apache.commons.logging.LogFactory.getLog(ReferenceListDataRefTest.class);
58      private WSSecurityEngine secEngine = new WSSecurityEngine();
59      private CallbackHandler callbackHandler = new KeystoreCallbackHandler();
60      private Crypto crypto = null;
61      
62      public ReferenceListDataRefTest() throws Exception {
63          crypto = CryptoFactory.getInstance("wss40.properties");
64          WSSConfig.init();
65      }
66  
67      /**
68       * Test that check for correct WSDataRef object from ReferenceList Processor 
69       * 
70       * 
71       * @throws Exception
72       *             Thrown when there is an error in encryption or decryption
73       */
74      @org.junit.Test
75      public void testDataRefReferenceListProcessor() throws Exception {
76          Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
77          WSSecEncrypt builder = new WSSecEncrypt();
78          builder.setUserInfo("wss40");
79          builder.setKeyIdentifierType(WSConstants.BST_DIRECT_REFERENCE);
80          builder.setSymmetricEncAlgorithm(WSConstants.TRIPLE_DES);
81          WSSecHeader secHeader = new WSSecHeader();
82          secHeader.insertSecurityHeader(doc);
83          LOG.info("Before Encryption Triple DES....");
84  
85          /*
86           * Prepare the Encrypt object with the token, setup data structure
87           */
88          builder.prepare(doc, crypto);
89  
90          /*
91           * Set up the parts structure to encrypt the body
92           */
93          List<WSEncryptionPart> parts = new ArrayList<WSEncryptionPart>();
94          WSEncryptionPart encP = 
95              new WSEncryptionPart(
96                  "add", "http://ws.apache.org/counter/counter_port_type", "Element"
97              );
98          parts.add(encP);
99  
100         /*
101          * Encrypt the element (testMethod), create EncryptedData elements that reference
102          * the EncryptedKey, and get a ReferenceList that can be put into the
103          * Security header. Be sure that the ReferenceList is after the
104          * EncryptedKey element in the Security header (strict layout)
105          */
106         Element refs = builder.encryptForRef(null, parts);
107         builder.addExternalRefElement(refs, secHeader);
108 
109         /*
110          * now add (prepend) the EncryptedKey element, then a
111          * BinarySecurityToken if one was setup during prepare
112          */
113         builder.prependToHeader(secHeader);
114 
115         builder.prependBSTElementToHeader(secHeader);
116 
117         Document encryptedDoc = doc;
118         LOG.info("After Encryption Triple DES....");
119 
120         checkDataRef(encryptedDoc);
121     }
122 
123     /**
124      * Verifies the soap envelope <p/>
125      * 
126      * @param envelope
127      * @throws Exception
128      *             Thrown when there is a problem in verification
129      */
130     @SuppressWarnings("unchecked")
131     private void checkDataRef(Document doc) throws Exception {
132         
133         // Retrieve the wsResults List 
134         List<WSSecurityEngineResult> wsResults = 
135             secEngine.processSecurityHeader(doc, null, callbackHandler, crypto);
136         boolean found = false;
137                 
138         for (int i = 0; i < wsResults.size(); i++) {
139             WSSecurityEngineResult wsSecEngineResult = 
140                 (WSSecurityEngineResult)wsResults.get(i);           
141             int action = ((java.lang.Integer) 
142                 wsSecEngineResult.get(WSSecurityEngineResult.TAG_ACTION)).intValue();
143             
144             // We want to filter only encryption results
145             if (action != WSConstants.ENCR) {
146                 continue;
147             }
148             List<WSDataRef> dataRefs = 
149                 (List<WSDataRef>)wsSecEngineResult.get(WSSecurityEngineResult.TAG_DATA_REF_URIS);
150             
151             //We want check only the DATA_REF_URIS 
152             if (dataRefs != null && dataRefs.size() > 0) {
153                 for (int j = 0; j < dataRefs.size(); j++) {
154                     Object obj = dataRefs.get(i);                            
155 
156                     // ReferenceList Processor must Return a WSDataRef objects
157                     assertTrue(obj instanceof WSDataRef);
158 
159                     WSDataRef dataRef = (WSDataRef) obj;
160 
161                     // Check whether QName is correctly set
162                     assertEquals("add", dataRef.getName().getLocalPart());
163                     assertEquals(
164                         "http://ws.apache.org/counter/counter_port_type", 
165                         dataRef.getName().getNamespaceURI()
166                     );
167 
168                     // Check whether wsu:Id is set
169                     assertNotNull(dataRef.getWsuId());
170                     
171                     // Check the encryption algorithm was set
172                     assertEquals(WSConstants.TRIPLE_DES, dataRef.getAlgorithm());
173 
174                     // flag to indicate the element was found in TAG_DATA_REF_URIS
175                     found = true;
176 
177                 }
178             }
179         }
180         
181         // Make sure the element is actually found in the decrypted elements
182         assertTrue(found);
183         
184     }
185 
186 }