-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignedSoap.java
455 lines (399 loc) · 20.7 KB
/
SignedSoap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package com.att.eg.catalog.cms.ingest.gitignore;
import com.couchbase.client.deps.io.netty.buffer.SwappedByteBuf;
import org.apache.wss4j.dom.engine.WSSConfig;
import org.apache.xml.security.encryption.EncryptedData;
import org.apache.xml.security.encryption.EncryptedKey;
import org.apache.xml.security.encryption.XMLCipher;
import org.joda.time.LocalDateTime;
import org.springframework.core.io.FileSystemResource;
import org.springframework.ws.soap.security.wss4j2.callback.KeyStoreCallbackHandler;
import org.springframework.ws.soap.security.wss4j2.callback.SimplePasswordValidationCallbackHandler;
import org.springframework.ws.soap.security.wss4j2.support.CryptoFactoryBean;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import sun.security.x509.X509CertImpl;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.xml.crypto.dsig.*;
import javax.xml.crypto.dsig.dom.DOMSignContext;
import javax.xml.crypto.dsig.dom.DOMValidateContext;
import javax.xml.crypto.dsig.keyinfo.KeyInfo;
import javax.xml.crypto.dsig.keyinfo.KeyInfoFactory;
import javax.xml.crypto.dsig.keyinfo.KeyValue;
import javax.xml.crypto.dsig.spec.C14NMethodParameterSpec;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.soap.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.Charset;
import java.security.*;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.apache.wss4j.common.ConfigurationConstants.ENCRYPT;
import static org.apache.wss4j.common.ConfigurationConstants.SIGNATURE;
import static org.apache.wss4j.common.ConfigurationConstants.TIMESTAMP;
import static org.apache.wss4j.dom.WSConstants.BST_DIRECT_REFERENCE;
/**
* Construct a SOAP message, sign it and then validate the signature.
* This implementation follows the
* <a ref="http://www.w3.org/TR/SOAP-dsig/">
* W3C Note on digital signatures in SOAP messages
* </a>.
* The validating key is included in the signature.
* DOM Level 2 is used throughout.
* <p>
* The following SOAP message is signed:
* <pre><code>
*
* <?xml version="1.0" encoding="UTF-8"?>
* <soap-env:Envelope
* xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
* <soap-env:Header>
* <SOAP-SEC:Signature
* mustUnderstand="1"
* xmlns:SOAP-SEC="http://schemas.xmlsoap.org/soap/security/2000-12"/>
* </soap-env:Header>
* <soap-env:Body id="Body">
* <m:GetLastTradePrice xmlns:m="http://wombats.ztrade.com">
* <symbol>SUNW</symbol>
* </m:GetLastTradePrice>
* </soap-env:Body>
* </soap-env:Envelope>
*
* </code></pre>
*/
public class SignedSoap {
private static boolean debug = true;
public static void main(String[] args) throws Exception {
int argc = args.length;
if (argc == 1) {
if (args[0].equalsIgnoreCase("-help")) {
System.out.println("Usage: SignedSoap [-debug]");
System.out.println(" -debug\tactivates debug messages");
return;
}
debug = args[0].equalsIgnoreCase("-debug");
}
// Create the SOAP message
System.out.println("Creating the SOAP message...");
// SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
MessageFactory factory = MessageFactory.newInstance();
String xml="<soapenv:Envelope xmlns:dl=\"http://ws.dttas.ie/v1/DL-WS/\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header/><soapenv:Body/></soapenv:Envelope>";
SOAPMessage soapMessage = factory.createMessage(new MimeHeaders(), new ByteArrayInputStream(xml.getBytes(Charset.forName("UTF-8"))));
soapMessage.writeTo(System.out);
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
/* soapEnvelope.removeAttributeNS("http://schemas.xmlsoap.org/soap/envelope/","SOAP-ENV");
soapEnvelope.addNamespaceDeclaration("dl", "http://ws.dttas.ie/v1/DL-WS/");
soapEnvelope.addNamespaceDeclaration("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");*/
SOAPHeader soapHeader = soapEnvelope.getHeader();
/* SOAPHeaderElement headerElement = soapHeader.addHeaderElement(
soapEnvelope.createName("Signature", "SOAP-SEC",
"http://schemas.xmlsoap.org/soap/security/2000-12"));*/
SOAPHeaderElement security = soapHeader.addHeaderElement(
soapEnvelope.createName("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"));
security.addNamespaceDeclaration("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
// SOAPElement soapHeaderElement = security.addChildElement(soapEnvelope.createName("Timestamp", "wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"));
// soapHeaderElement.addAttribute(soapEnvelope.createName("id", "SOAP-SEC",
// "http://schemas.xmlsoap.org/soap/security/2000-12"), "uuid-a4acf53b-0139-4508-ac75-5baacb1e8553-1");
/*Name created = soapEnvelope.createName("Created", "wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
SOAPElement createdSoapElement = soapHeaderElement.addChildElement(created);
LocalDateTime localDateTime = LocalDateTime.now();
createdSoapElement.addTextNode(DateUtils.add(localDateTime));
createdSoapElement.addTextNode("2017-12-14T21:23:11.334Z");
Name expired = soapEnvelope.createName("Expires", "wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
SOAPElement expiredSoapElement = soapHeaderElement.addChildElement(expired);
// SOAPElement expiredSoapElement = soapHeaderElement.addChildElement(expired);
expiredSoapElement.addTextNode("2017-12-14T21:28:11.334Z");
// timeStamp.addChildElement("")
*/
SOAPBody soapBody = soapEnvelope.getBody();
/* soapBody.addAttribute(soapEnvelope.createName("id", "SOAP-SEC",
"http://schemas.xmlsoap.org/soap/security/2000-12"), "Body");*/
Name bodyName = soapEnvelope.createName("GetLastTradePrice", "m",
"http://wombats.ztrade.com");
SOAPBodyElement gltp = soapBody.addBodyElement(bodyName);
Name name = soapEnvelope.createName("symbol");
SOAPElement symbol = gltp.addChildElement(name);
symbol.addTextNode("SUNW");
// Generate a DOM representation of the SOAP message
System.out.println("Generating the DOM tree...");
// Get input source
Source source = soapPart.getContent();
org.w3c.dom.Node root = null;
if (source instanceof DOMSource) {
root = ((DOMSource) source).getNode();
} else if (source instanceof SAXSource) {
InputSource inSource = ((SAXSource) source).getInputSource();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = null;
synchronized (dbf) {
db = dbf.newDocumentBuilder();
}
Document doc = db.parse(inSource);
root = (org.w3c.dom.Node) doc.getDocumentElement();
} else {
System.err.println("error: cannot convert SOAP message (" +
source.getClass().getName() + ") into a W3C DOM tree");
System.exit(-1);
}
if (debug) {
dumpDOMDocument(root);
}
// Generate a DSA key pair
System.out.println("Generating the DSA keypair...");
KeyStore p12 = KeyStore.getInstance("pkcs12");
char[] password = "ndlsonline".toCharArray();
p12.load(new FileInputStream("/home/svarkey/D20160103/BUF/DLWSClientStore.p12"), password);
PrivateKey privateKey = (PrivateKey) p12.getKey("wsstest", password);
X509Certificate cert = (X509Certificate) p12.getCertificate("wsstest");
KeyPair keyPair = new KeyPair(cert.getPublicKey(), privateKey);
// Assemble the signature parts
System.out.println("Preparing the signature...");
String providerName = System.getProperty
("jsr105Provider", "org.jcp.xml.dsig.internal.dom.XMLDSigRI");
XMLSignatureFactory sigFactory = XMLSignatureFactory.getInstance("DOM",
(Provider) Class.forName(providerName).newInstance());
Reference bodyRef = sigFactory.newReference("#Body",
sigFactory.newDigestMethod(DigestMethod.SHA1, null));
Reference timeStampIdRef = sigFactory.newReference("#uuid-a4acf53b-0139-4508-ac75-5baacb1e8553-1",
sigFactory.newDigestMethod(DigestMethod.SHA1, null));
SignedInfo signedInfo = sigFactory.newSignedInfo(
sigFactory.newCanonicalizationMethod(
CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
(C14NMethodParameterSpec) null),
sigFactory.newSignatureMethod(SignatureMethod.RSA_SHA1, null),
Arrays.asList(bodyRef, timeStampIdRef));
KeyInfoFactory kif = sigFactory.getKeyInfoFactory();
KeyValue kv = kif.newKeyValue(keyPair.getPublic());
KeyInfo keyInfo = kif.newKeyInfo(Collections.singletonList(kv));
XMLSignature sig = sigFactory.newXMLSignature(signedInfo, keyInfo);
// Insert XML signature into DOM tree and sign
System.out.println("Signing the SOAP message...");
Element envelope = getFirstChildElement(root);
Element header = getFirstChildElement(envelope);
// Find where to insert signature
/*
DOMSignContext sigContext =
new DOMSignContext(keyPair.getPrivate(), header);
// Need to distinguish the Signature element in DSIG (from that in SOAP)
sigContext.putNamespacePrefix(XMLSignature.XMLNS, "ds");
// register Body ID attribute
sigContext.setIdAttributeNS
(getNextSiblingElement(header),
"http://schemas.xmlsoap.org/soap/security/2000-12", "id");
sigContext.setIdAttributeNS
(soapHeaderElement,
"http://schemas.xmlsoap.org/soap/security/2000-12", "id");
sig.sign(sigContext);*/
if (debug) {
dumpDOMDocument(root);
}
// Validate the XML signature
X509Certificate publicCert = (X509Certificate) p12.getCertificate("wsstestservice");
sun.security.x509.X509CertImpl x509Cert = (X509CertImpl) publicCert;
x509Cert.derEncode(System.out);
final String DESEDE_ENCRYPTION_SCHEME = "DESede";
SecretKeyFactory mySecretKeyFactory;
DESedeKeySpec myKeySpec = new DESedeKeySpec(publicCert.getEncoded());
mySecretKeyFactory = SecretKeyFactory.getInstance(DESEDE_ENCRYPTION_SCHEME);
SecretKey key = mySecretKeyFactory.generateSecret(myKeySpec);
SanchuTest securityInterceptor = new SanchuTest();
// securityInterceptor.setSecurementActions(ENCRYPT);
// securityInterceptor.setSecurementActions(TIMESTAMP) ;
securityInterceptor.setSecurementActions(TIMESTAMP + " " + SIGNATURE) ;
// WSSConfig newInstance = WSSConfig.getNewInstance();
//securityInterceptor.setWssConfig(newInstance);
securityInterceptor.setTimestampPrecisionInMilliseconds(true);
SimplePasswordValidationCallbackHandler samlCallbackHandler = new SimplePasswordValidationCallbackHandler();
// samlCallbackHandler.setPrivateKeyPassword("ndlsonline");
//securityInterceptor.setH(samlCallbackHandler);
securityInterceptor.setSecurementTimeToLive(5000);
CryptoFactoryBean cryptoFactoryBean = new CryptoFactoryBean();
cryptoFactoryBean.setKeyStoreLocation(new FileSystemResource(new File("/home/svarkey/D20160103/BUF/DLWSClientStore.p12")));
cryptoFactoryBean.setKeyStorePassword("ndlsonline");
securityInterceptor.setSecurementPassword("ndlsonline");
cryptoFactoryBean.afterPropertiesSet();
securityInterceptor.setSecurementSignatureCrypto(cryptoFactoryBean.getObject());
securityInterceptor.setSecurementUsername("wsstest");
//This will generate binarySecurityToken in header
securityInterceptor.setSecurementSignatureKeyIdentifier("DirectReference");
securityInterceptor.setSecurementSignatureParts("{Element}{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd}Timestamp;{Element}{http://schemas.xmlsoap.org/soap/envelope/}Body");
//securityInterceptor.setDerivedTokenReference
// securityInterceptor.setValidateRequest(true);
securityInterceptor.testA(soapMessage,samlCallbackHandler);
// soapMessage.writeTo(System.out);
// encryptUsingDOM(soapEnvelope.getOwnerDocument(), Arrays.asList(soapBody.getElementQName()),
// TRIPLEDES.toString(), key,
// null, null, true);
System.exit(0);
// Locate the signature element
Element sigElement = getFirstChildElement(header);
// Validate the signature using the public key generated above
DOMValidateContext valContext =
new DOMValidateContext(keyPair.getPublic(), sigElement);
// register Body ID attribute
valContext.setIdAttributeNS
(getNextSiblingElement(header),
"http://schemas.xmlsoap.org/soap/security/2000-12", "id");
boolean isValid = sig.validate(valContext);
System.out.println("Validating the signature... " +
(isValid ? "valid" : "invalid"));
}
/**
* Encrypt the document using the DOM API of Apache Santuario - XML Security for Java.
* It encrypts a list of QNames that it finds in the Document via XPath. If a wrappingKey
* is supplied, this is used to encrypt the encryptingKey + place it in an EncryptedKey
* structure.
*/
public static void encryptUsingDOM(
Document document,
List<QName> namesToEncrypt,
String algorithm,
Key encryptingKey,
String keyTransportAlgorithm,
PublicKey wrappingKey,
boolean content
) throws Exception {
org.apache.xml.security.Init.init();
System.out.println(encryptingKey.getAlgorithm());
System.out.println(encryptingKey.getClass());
XMLCipher xmlCipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES);
xmlCipher.init(XMLCipher.ENCRYPT_MODE, encryptingKey);
if (wrappingKey != null) {
XMLCipher newCipher = XMLCipher.getInstance(keyTransportAlgorithm);
newCipher.init(XMLCipher.WRAP_MODE, wrappingKey);
EncryptedKey encryptedKey = newCipher.encryptKey(document, encryptingKey);
// Create a KeyInfo for the EncryptedKey
org.apache.xml.security.keys.KeyInfo encryptedKeyKeyInfo = encryptedKey.getKeyInfo();
if (encryptedKeyKeyInfo == null) {
encryptedKeyKeyInfo = new org.apache.xml.security.keys.KeyInfo(document);
encryptedKeyKeyInfo.getElement().setAttributeNS(
"http://www.w3.org/2000/xmlns/", "xmlns:dsig", "http://www.w3.org/2000/09/xmldsig#"
);
encryptedKey.setKeyInfo(encryptedKeyKeyInfo);
}
encryptedKeyKeyInfo.add(wrappingKey);
// Create a KeyInfo for the EncryptedData
EncryptedData builder = xmlCipher.getEncryptedData();
org.apache.xml.security.keys.KeyInfo builderKeyInfo = builder.getKeyInfo();
if (builderKeyInfo == null) {
builderKeyInfo = new org.apache.xml.security.keys.KeyInfo(document);
builderKeyInfo.getElement().setAttributeNS(
"http://www.w3.org/2000/xmlns/", "xmlns:dsig", "http://www.w3.org/2000/09/xmldsig#"
);
builder.setKeyInfo(builderKeyInfo);
}
builderKeyInfo.add(encryptedKey);
}
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
// xpath.setNamespaceContext(new DSNamespaceContext());
for (QName nameToEncrypt : namesToEncrypt) {
String expression = "//*[local-name()='" + nameToEncrypt.getLocalPart() + "']";
NodeList elementsToEncrypt =
(NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET);
for (int i = 0; i < elementsToEncrypt.getLength(); i++) {
Element elementToEncrypt = (Element) elementsToEncrypt.item(i);
//Assert.assertNotNull(elementToEncrypt);
document = xmlCipher.doFinal(document, elementToEncrypt, content);
dumpDOMDocument(document);
}
}
String expression = "//xenc:EncryptedData[1]";
Element encElement =
(Element) xpath.evaluate(expression, document, XPathConstants.NODE);
// Assert.assertNotNull(encElement);
}
/*
void encryptElement(Document xmlDocument, Key encryptSymmetricKey,
EncryptedKey encryptedKey, Element element)
throws XMLEncryptionException, Exception {
String algorithmURI = XMLCipher.AES_128;
XMLCipher xmlCipher = XMLCipher.getInstance(algorithmURI);
xmlCipher.init(XMLCipher.ENCRYPT_MODE, encryptSymmetricKey);
*//*
* Setting keyinfo inside the encrypted data being prepared.
*//*
EncryptedData encryptedData = xmlCipher.getEncryptedData();
KeyInfo keyInfo = new DOMKeyInfo(xmlDocument);
keyInfo.add(encryptedKey);
encryptedData.setKeyInfo(keyInfo);
xmlCipher.doFinal(xmlDocument, element, true);
}*/
/*
* Outputs DOM representation to the standard output stream.
*
* @param root The DOM representation to be outputted
*/
private static void dumpDOMDocument(org.w3c.dom.Node root)
throws TransformerException, TransformerConfigurationException, ParserConfigurationException {
System.out.println("\n");
// Create a new transformer object
Transformer transformer =
TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// Dump the DOM representation to standard output
transformer.transform(new DOMSource(root), new StreamResult(System.out));
System.out.println(root.getOwnerDocument());
/* DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document newDocument = builder.newDocument();
org.w3c.dom.Node importedNode = newDocument.importNode(root, true);
newDocument.appendChild(importedNode);
.PrettyDocumentToString(newDocument);*/
System.out.println("\n");
}
/**
* Returns the first child element of the specified node, or null if there
* is no such element.
*
* @param node the node
* @return the first child element of the specified node, or null if there
* is no such element
* @throws NullPointerException if <code>node == null</code>
*/
private static Element getFirstChildElement(org.w3c.dom.Node node) {
org.w3c.dom.Node child = node.getFirstChild();
while (child != null &&
child.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) {
child = child.getNextSibling();
}
return (Element) child;
}
/**
* Returns the next sibling element of the specified node, or null if there
* is no such element.
*
* @param node the node
* @return the next sibling element of the specified node, or null if there
* is no such element
* @throws NullPointerException if <code>node == null</code>
*/
public static Element getNextSiblingElement(org.w3c.dom.Node node) {
org.w3c.dom.Node sibling = node.getNextSibling();
while (sibling != null &&
sibling.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) {
sibling = sibling.getNextSibling();
}
return (Element) sibling;
}
}