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 org.apache.ws.security.SOAPConstants;
23 import org.apache.ws.security.WSEncryptionPart;
24 import org.apache.ws.security.WSConstants;
25 import org.apache.ws.security.WSSConfig;
26 import org.apache.ws.security.util.WSSecurityUtil;
27 import org.apache.ws.security.WSSecurityEngine;
28 import org.apache.ws.security.common.SOAPUtil;
29 import org.apache.ws.security.components.crypto.Crypto;
30 import org.apache.ws.security.components.crypto.CryptoFactory;
31 import org.w3c.dom.Document;
32
33 import java.util.List;
34 import java.util.ArrayList;
35
36
37
38
39
40
41
42 public class STRSignatureTest extends org.junit.Assert {
43 private static final org.apache.commons.logging.Log LOG =
44 org.apache.commons.logging.LogFactory.getLog(STRSignatureTest.class);
45 private WSSecurityEngine secEngine = new WSSecurityEngine();
46 private Crypto crypto = null;
47
48 public STRSignatureTest() throws Exception {
49 WSSConfig.init();
50 crypto = CryptoFactory.getInstance("wss40.properties");
51 }
52
53
54
55
56
57
58
59
60
61
62 @org.junit.Test
63 public void testX509SignatureDirectSTR() throws Exception {
64 WSSecSignature builder = new WSSecSignature();
65 builder.setUserInfo("wss40", "security");
66 Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
67 SOAPConstants soapConstants = WSSecurityUtil.getSOAPConstants(doc.getDocumentElement());
68 List<WSEncryptionPart> parts = new ArrayList<WSEncryptionPart>();
69
70
71
72
73
74 WSEncryptionPart encP =
75 new WSEncryptionPart(
76 soapConstants.getBodyQName().getLocalPart(),
77 soapConstants.getEnvelopeURI(),
78 "Content");
79 parts.add(encP);
80 encP =
81 new WSEncryptionPart(
82 "STRTransform",
83 soapConstants.getEnvelopeURI(),
84 "Content");
85 parts.add(encP);
86
87 builder.setParts(parts);
88 builder.setKeyIdentifierType(WSConstants.BST_DIRECT_REFERENCE);
89
90 LOG.info("Before Signing STR DirectReference....");
91
92 WSSecHeader secHeader = new WSSecHeader();
93 secHeader.insertSecurityHeader(doc);
94
95 Document signedDoc = builder.build(doc, crypto, secHeader);
96
97 if (LOG.isDebugEnabled()) {
98 LOG.debug("Signed message with STR DirectReference key identifier:");
99 String outputString =
100 org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(signedDoc);
101 LOG.debug(outputString);
102 }
103 LOG.info("After Signing STR DirectReference....");
104 verify(signedDoc);
105 }
106
107
108
109
110
111
112 @org.junit.Test
113 public void testWSS96() throws Exception {
114 WSSecSignature builder = new WSSecSignature();
115 builder.setUserInfo("wss40", "security");
116 Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
117 SOAPConstants soapConstants = WSSecurityUtil.getSOAPConstants(doc.getDocumentElement());
118 List<WSEncryptionPart> parts = new ArrayList<WSEncryptionPart>();
119
120 WSSecHeader secHeader = new WSSecHeader();
121 secHeader.insertSecurityHeader(doc);
122
123
124
125
126
127 WSEncryptionPart encP =
128 new WSEncryptionPart(
129 soapConstants.getBodyQName().getLocalPart(),
130 soapConstants.getEnvelopeURI(),
131 "Content");
132 parts.add(encP);
133 encP =
134 new WSEncryptionPart(
135 "STRTransform",
136 soapConstants.getEnvelopeURI(),
137 "Content");
138 parts.add(encP);
139
140 WSSecTimestamp timestamp = new WSSecTimestamp();
141 timestamp.setTimeToLive(600);
142 timestamp.build(doc, secHeader);
143 parts.add(new WSEncryptionPart(timestamp.getId()));
144
145 builder.setParts(parts);
146 builder.setKeyIdentifierType(WSConstants.BST_DIRECT_REFERENCE);
147
148 LOG.info("Before Signing STR DirectReference....");
149 Document signedDoc = builder.build(doc, crypto, secHeader);
150
151 if (LOG.isDebugEnabled()) {
152 LOG.debug("Signed message with STR DirectReference key identifier:");
153 String outputString =
154 org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(signedDoc);
155 LOG.debug(outputString);
156 }
157 LOG.info("After Signing STR DirectReference....");
158 verify(signedDoc);
159 }
160
161
162
163
164
165
166
167
168
169
170
171 @org.junit.Test
172 public void testX509SignatureISSTR() throws Exception {
173 WSSecSignature builder = new WSSecSignature();
174 builder.setUserInfo("wss40", "security");
175
176 Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
177 SOAPConstants soapConstants = WSSecurityUtil.getSOAPConstants(doc.getDocumentElement());
178 List<WSEncryptionPart> parts = new ArrayList<WSEncryptionPart>();
179
180
181
182
183
184 WSEncryptionPart encP =
185 new WSEncryptionPart(
186 soapConstants.getBodyQName().getLocalPart(),
187 soapConstants.getEnvelopeURI(),
188 "Content");
189 parts.add(encP);
190 encP =
191 new WSEncryptionPart(
192 "STRTransform",
193 soapConstants.getEnvelopeURI(),
194 "Content");
195 parts.add(encP);
196
197 builder.setParts(parts);
198 builder.setKeyIdentifierType(WSConstants.ISSUER_SERIAL);
199
200 LOG.info("Before Signing STR IS....");
201
202 WSSecHeader secHeader = new WSSecHeader();
203 secHeader.insertSecurityHeader(doc);
204
205 Document signedDoc = builder.build(doc, crypto, secHeader);
206
207 if (LOG.isDebugEnabled()) {
208 LOG.debug("Signed message with STR IssuerSerial key identifier:");
209 String outputString =
210 org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(signedDoc);
211 LOG.debug(outputString);
212 }
213 LOG.info("After Signing STR IS....");
214 verify(signedDoc);
215 }
216
217
218
219
220
221
222
223
224
225
226 @org.junit.Test
227 public void testX509SignatureSKISTR() throws Exception {
228 WSSecSignature builder = new WSSecSignature();
229 builder.setUserInfo("wss40", "security");
230 Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
231 SOAPConstants soapConstants = WSSecurityUtil.getSOAPConstants(doc.getDocumentElement());
232 List<WSEncryptionPart> parts = new ArrayList<WSEncryptionPart>();
233
234
235
236
237
238 WSEncryptionPart encP =
239 new WSEncryptionPart(
240 soapConstants.getBodyQName().getLocalPart(),
241 soapConstants.getEnvelopeURI(),
242 "Content");
243 parts.add(encP);
244 encP =
245 new WSEncryptionPart(
246 "STRTransform",
247 soapConstants.getEnvelopeURI(),
248 "Content");
249 parts.add(encP);
250
251 builder.setParts(parts);
252 builder.setKeyIdentifierType(WSConstants.SKI_KEY_IDENTIFIER);
253
254 LOG.info("Before Signing STR SKI....");
255
256 WSSecHeader secHeader = new WSSecHeader();
257 secHeader.insertSecurityHeader(doc);
258
259 Document signedDoc = builder.build(doc, crypto, secHeader);
260
261 if (LOG.isDebugEnabled()) {
262 LOG.debug("Signed message with STR SKI key identifier:");
263 String outputString =
264 org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(signedDoc);
265 LOG.debug(outputString);
266 }
267 LOG.info("After Signing STR SKI....");
268 verify(signedDoc);
269 }
270
271
272
273
274
275
276
277
278 private void verify(Document doc) throws Exception {
279 secEngine.processSecurityHeader(doc, null, null, crypto);
280 }
281 }