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.EncryptedKeyProcessor</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 EncryptedKey. The
49 * EncryptedData elements contain a KeyInfo that references the EncryptedKey via
50 * a STR/Reference structure.
51 *
52 * WSDataRef object must contain the correct QName of the decrypted element.
53 *
54 */
55 public class EncryptedKeyDataRefTest extends org.junit.Assert {
56 private static final org.apache.commons.logging.Log LOG =
57 org.apache.commons.logging.LogFactory.getLog(EncryptedKeyDataRefTest.class);
58 private WSSecurityEngine secEngine = new WSSecurityEngine();
59 private CallbackHandler callbackHandler = new KeystoreCallbackHandler();
60 private Crypto crypto = null;
61
62 public EncryptedKeyDataRefTest() throws Exception {
63 crypto = CryptoFactory.getInstance("wss40.properties");
64 WSSConfig.init();
65 }
66
67 /**
68 * Test that check for correct WSDataRef object from EncryptedKey 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 testDataRefEncryptedKeyProcessor() 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 EncryptedKey
103 * itself as a child.
104 */
105 Element refs = builder.encryptForRef(null, parts);
106
107 /*
108 * We use this method because we want the reference list to be inside the
109 * EncryptedKey element
110 */
111 builder.addInternalRefElement(refs);
112
113 /*
114 * now add (prepend) the EncryptedKey element, then a
115 * BinarySecurityToken if one was setup during prepare
116 */
117 builder.prependToHeader(secHeader);
118
119 builder.prependBSTElementToHeader(secHeader);
120
121 Document encryptedDoc = doc;
122 LOG.info("After Encryption Triple DES....");
123
124 checkDataRef(encryptedDoc);
125 }
126
127 /**
128 * Verifies the soap envelope <p/>
129 *
130 * @param envelope
131 * @throws Exception
132 * Thrown when there is a problem in verification
133 */
134 @SuppressWarnings("unchecked")
135 private void checkDataRef(Document doc) throws Exception {
136
137 // Retrieve the wsResults List
138 List<WSSecurityEngineResult> wsResults =
139 secEngine.processSecurityHeader(doc, null, callbackHandler, crypto);
140 boolean found = false;
141
142 for (int i = 0; i < wsResults.size(); i++) {
143 WSSecurityEngineResult wsSecEngineResult =
144 (WSSecurityEngineResult)wsResults.get(i);
145 int action = ((java.lang.Integer)
146 wsSecEngineResult.get(WSSecurityEngineResult.TAG_ACTION)).intValue();
147
148 // We want to filter only encryption results
149 if (action != WSConstants.ENCR) {
150 continue;
151 }
152 List<WSDataRef> dataRefs = (List<WSDataRef>)wsSecEngineResult
153 .get(WSSecurityEngineResult.TAG_DATA_REF_URIS);
154
155 //We want check only the DATA_REF_URIS
156 if (dataRefs != null && dataRefs.size() > 0) {
157 for (int j = 0; j < dataRefs.size(); j++) {
158 Object obj = dataRefs.get(i);
159
160 // ReferenceList Processor must Return a WSDataRef objects
161 assertTrue(obj instanceof WSDataRef);
162
163 WSDataRef dataRef = (WSDataRef) obj;
164
165 // Check whether QName is correctly set
166 assertEquals("add", dataRef.getName().getLocalPart());
167 assertEquals(
168 "http://ws.apache.org/counter/counter_port_type",
169 dataRef.getName().getNamespaceURI()
170 );
171
172 // Check whether wsu:Id is set
173 assertNotNull(dataRef.getWsuId());
174
175 // Check the encryption algorithm was set
176 assertEquals(WSConstants.TRIPLE_DES, dataRef.getAlgorithm());
177
178 // flag to indicate the element was found in TAG_DATA_REF_URIS
179 found = true;
180
181 }
182 }
183 }
184
185 // Make sure the element is actually found in the decrypted elements
186 assertTrue(found);
187
188 }
189
190 }