1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
42
43
44
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
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
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
110
111
112
113
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 }