List of usage examples for org.w3c.dom Element cloneNode
public Node cloneNode(boolean deep);
From source file:org.apache.ws.security.message.TestMessageTransformer.java
public static Element duplicateEncryptedDataInWsseHeader(Element saaj, boolean moveReferenceList) { if (moveReferenceList) { moveReferenceList(saaj);//from w w w.j a v a 2s . c om } Element body = getFirstChildElement(saaj, new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"), true); Element encData = getFirstChildElement(body, new QName("http://www.w3.org/2001/04/xmlenc#", "EncryptedData"), true); Element newEncData = createNewEncryptedData(encData); Element sh = getFirstChildElement(saaj, new QName("http://schemas.xmlsoap.org/soap/envelope/", "Header"), true); Element wsseHeader = getFirstChildElement(sh, new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security"), true); Node newWsseHeader = wsseHeader.cloneNode(false); Node cur = wsseHeader.getFirstChild(); String newId = newEncData.getAttributeNS(null, "Id"); while (cur != null) { cur = copyHeadersAndUpdateRefList(cur, newWsseHeader, newId); } newWsseHeader.appendChild(newEncData); if (!moveReferenceList) { updateEncryptedKeyRefList(newWsseHeader, newId); } Node parent = wsseHeader.getParentNode(); parent.removeChild(wsseHeader); parent.appendChild(newWsseHeader); print(saaj.getOwnerDocument()); return newEncData; }
From source file:org.apache.ws.security.message.TestMessageTransformer.java
public static Element duplicateEncryptedDataInExternalWrapperElement(Element saaj, boolean moveReferenceList) { if (moveReferenceList) { moveReferenceList(saaj);//from w ww . ja v a2s. com } Element body = getFirstChildElement(saaj, new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"), true); Element encData = getFirstChildElement(body, new QName("http://www.w3.org/2001/04/xmlenc#", "EncryptedData"), true); Element newEncData = createNewEncryptedData(encData); Element sh = getFirstChildElement(saaj, new QName("http://schemas.xmlsoap.org/soap/envelope/", "Header"), true); Element wsseHeader = getFirstChildElement(sh, new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security"), true); Node newWsseHeader = wsseHeader.cloneNode(false); Node cur = wsseHeader.getFirstChild(); String newId = newEncData.getAttributeNS(null, "Id"); while (cur != null) { cur = copyHeadersAndUpdateRefList(cur, newWsseHeader, newId); } sh.removeChild(wsseHeader); sh.appendChild(newWsseHeader); if (!moveReferenceList) { updateEncryptedKeyRefList(newWsseHeader, newId); } Element wrapper = encData.getOwnerDocument().createElementNS(null, "a"); wrapper.setAttributeNS("http://schemas.xmlsoap.org/soap/envelope/", "mustUnderstand", "0"); wrapper.setAttributeNS("http://schemas.xmlsoap.org/soap/envelope/", "actor", "foo"); wrapper.appendChild(newEncData); sh.appendChild(wrapper); print(saaj.getOwnerDocument()); return newEncData; }
From source file:org.apache.ws.security.message.TestMessageTransformer.java
public static Element addEncryptedDataWithEmbeddedEncryptedKeyInWsseHeader(Element saaj) { moveReferenceList(saaj);/*from w w w .jav a2 s . c o m*/ Element body = getFirstChildElement(saaj, new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"), true); Element encData = getFirstChildElement(body, new QName("http://www.w3.org/2001/04/xmlenc#", "EncryptedData"), true); Element newEncData = (Element) encData.cloneNode(true); String newId = newEncData.getAttributeNS(null, "Id") + "b"; newEncData.setAttributeNS(null, "Id", newId); Element encKey = getFirstChildElement(saaj, new QName("http://www.w3.org/2001/04/xmlenc#", "EncryptedKey"), true); Element newEncKey = (Element) encKey.cloneNode(true); String newEcId = newEncKey.getAttributeNS(null, "Id") + "b"; newEncKey.setAttributeNS(null, "Id", newEcId); Element keyInfo = getFirstChildElement(newEncData, new QName("http://www.w3.org/2000/09/xmldsig#", "KeyInfo"), true); Element str = getFirstChildElement(newEncData, new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "SecurityTokenReference"), true); keyInfo.replaceChild(newEncKey, str); Element wsseHeader = getFirstChildElement(saaj, new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security"), true); Node newWsseHeader = wsseHeader.cloneNode(false); Node cur = wsseHeader.getFirstChild(); while (cur != null) { cur = copyHeadersAndUpdateRefList(cur, newWsseHeader, newId); } newWsseHeader.appendChild(newEncData); Node parent = wsseHeader.getParentNode(); parent.removeChild(wsseHeader); parent.appendChild(newWsseHeader); print(saaj.getOwnerDocument()); return newEncData; }
From source file:org.apache.ws.security.message.TestMessageTransformer.java
private static void updateEncryptedKeyRefList(Node wsseHeader, String newId) { Element encryptedKey = getFirstChildElement(wsseHeader, new QName("http://www.w3.org/2001/04/xmlenc#", "EncryptedKey"), true); Element ref = getFirstChildElement(encryptedKey, new QName("http://www.w3.org/2001/04/xmlenc#", "DataReference"), true); Element newRef = (Element) ref.cloneNode(true); newRef.setAttributeNS(null, "URI", "#" + newId); ref.getParentNode().appendChild(newRef); }
From source file:org.apache.ws.security.message.TestMessageTransformer.java
private static Element createNewEncryptedData(Element encData) { Element newEncData = (Element) encData.cloneNode(true); String id = newEncData.getAttributeNS(null, "Id"); String newId = id + "b"; newEncData.setAttributeNS(null, "Id", newId); return newEncData; }
From source file:org.apache.ws.security.message.TestMessageTransformer.java
private static Node copyHeadersAndUpdateRefList(Node cur, Node dest, String newId) { Node temp = cur.cloneNode(true); dest.appendChild(temp);/* www. j a va 2 s .c o m*/ if (newId != null && temp.getNodeType() == Node.ELEMENT_NODE) { Element t = (Element) temp; if (t.getLocalName().equals("ReferenceList")) { Element ref = getFirstChildElement(t, new QName("http://www.w3.org/2001/04/xmlenc#", "DataReference"), true); Element newRef = (Element) ref.cloneNode(true); newRef.setAttributeNS(null, "URI", "#" + newId); t.appendChild(newRef); } } return cur.getNextSibling(); }
From source file:org.apache.ws.security.message.TimestampTest.java
/** * This is a test for processing an Timestamp where it contains multiple "Created" elements. * This Timestamp should be rejected.// w ww . j av a 2 s.co m */ @org.junit.Test public void testMultipleCreated() throws Exception { Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG); WSSecHeader secHeader = new WSSecHeader(); secHeader.insertSecurityHeader(doc); Element timestampElement = doc.createElementNS(WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN); DateFormat zulu = new XmlSchemaDateFormat(); Element elementCreated = doc.createElementNS(WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + WSConstants.CREATED_LN); Date createdDate = new Date(); long currentTime = createdDate.getTime() + 300000; createdDate.setTime(currentTime); elementCreated.appendChild(doc.createTextNode(zulu.format(createdDate))); timestampElement.appendChild(elementCreated); timestampElement.appendChild(elementCreated.cloneNode(true)); secHeader.getSecurityHeader().appendChild(timestampElement); if (LOG.isDebugEnabled()) { String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc); LOG.debug(outputString); } // // Do some processing // try { verify(doc, WSSConfig.getNewInstance()); fail("The timestamp validation should have failed on multiple Created elements"); } catch (WSSecurityException ex) { // expected } }
From source file:org.apache.ws.security.message.TimestampTest.java
/** * This is a test for processing an Timestamp where it contains multiple "Expires" elements. * This Timestamp should be rejected./* ww w . ja v a 2 s . c o m*/ */ @org.junit.Test public void testMultipleExpires() throws Exception { Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG); WSSecHeader secHeader = new WSSecHeader(); secHeader.insertSecurityHeader(doc); Element timestampElement = doc.createElementNS(WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN); DateFormat zulu = new XmlSchemaDateFormat(); Element elementCreated = doc.createElementNS(WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + WSConstants.EXPIRES_LN); Date createdDate = new Date(); long currentTime = createdDate.getTime() + 300000; createdDate.setTime(currentTime); elementCreated.appendChild(doc.createTextNode(zulu.format(createdDate))); timestampElement.appendChild(elementCreated); timestampElement.appendChild(elementCreated.cloneNode(true)); secHeader.getSecurityHeader().appendChild(timestampElement); if (LOG.isDebugEnabled()) { String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc); LOG.debug(outputString); } // // Do some processing // try { verify(doc, WSSConfig.getNewInstance()); fail("The timestamp validation should have failed on multiple Expires elements"); } catch (WSSecurityException ex) { // expected } }
From source file:org.apache.ws.security.message.WSSecEncrypt.java
private Vector doEncryption(Document doc, SecretKey secretKey, KeyInfo keyInfo, Vector references) throws WSSecurityException { XMLCipher xmlCipher = null;/*from w ww .j a v a 2 s . c o m*/ try { xmlCipher = XMLCipher.getInstance(symEncAlgo); } catch (XMLEncryptionException e3) { throw new WSSecurityException(WSSecurityException.UNSUPPORTED_ALGORITHM, null, null, e3); } Vector encDataRef = new Vector(); boolean cloneKeyInfo = false; for (int part = 0; part < references.size(); part++) { WSEncryptionPart encPart = (WSEncryptionPart) references.get(part); String idToEnc = encPart.getId(); String elemName = encPart.getName(); String nmSpace = encPart.getNamespace(); String modifier = encPart.getEncModifier(); // // Third step: get the data to encrypt. // Element body = null; if (idToEnc != null) { body = WSSecurityUtil.findElementById(document.getDocumentElement(), idToEnc, WSConstants.WSU_NS); if (body == null) { body = WSSecurityUtil.findElementById(document.getDocumentElement(), idToEnc, null); } } else { body = (Element) WSSecurityUtil.findElement(document, elemName, nmSpace); } if (body == null) { throw new WSSecurityException(WSSecurityException.FAILURE, "noEncElement", new Object[] { "{" + nmSpace + "}" + elemName }); } boolean content = modifier.equals("Content") ? true : false; String xencEncryptedDataId = wssConfig.getIdAllocator().createId("EncDataId-", body); encPart.setEncId(xencEncryptedDataId); cloneKeyInfo = true; if (keyInfo == null) { keyInfo = new KeyInfo(document); SecurityTokenReference secToken = new SecurityTokenReference(document); if (useKeyIdentifier && SecurityTokenReference.SAML_ID_URI.equals(customReferenceValue)) { secToken.setSAMLKeyIdentifier((encKeyIdDirectId ? "" : "#") + encKeyId); } else { Reference ref = new Reference(document); if (encKeyIdDirectId) { ref.setURI(encKeyId); } else { ref.setURI("#" + encKeyId); } if (encKeyValueType != null) { ref.setValueType(encKeyValueType); } secToken.setReference(ref); } keyInfo.addUnknownElement(secToken.getElement()); Element keyInfoElement = keyInfo.getElement(); keyInfoElement.setAttributeNS(WSConstants.XMLNS_NS, "xmlns:" + WSConstants.SIG_PREFIX, WSConstants.SIG_NS); } // // Fourth step: encrypt data, and set necessary attributes in // xenc:EncryptedData // try { if (modifier.equals("Header")) { Element elem = doc.createElementNS(WSConstants.WSSE11_NS, "wsse11:" + WSConstants.ENCRYPTED_HEADER); WSSecurityUtil.setNamespace(elem, WSConstants.WSSE11_NS, WSConstants.WSSE11_PREFIX); String wsuPrefix = WSSecurityUtil.setNamespace(elem, WSConstants.WSU_NS, WSConstants.WSU_PREFIX); elem.setAttributeNS(WSConstants.WSU_NS, wsuPrefix + ":Id", wssConfig.getIdAllocator().createId("EncHeader-", body)); NamedNodeMap map = body.getAttributes(); for (int i = 0; i < map.getLength(); i++) { Attr attr = (Attr) map.item(i); if (attr.getNamespaceURI().equals(WSConstants.URI_SOAP11_ENV) || attr.getNamespaceURI().equals(WSConstants.URI_SOAP12_ENV)) { String soapEnvPrefix = WSSecurityUtil.setNamespace(elem, attr.getNamespaceURI(), WSConstants.DEFAULT_SOAP_PREFIX); elem.setAttributeNS(attr.getNamespaceURI(), soapEnvPrefix + ":" + attr.getLocalName(), attr.getValue()); } } xmlCipher.init(XMLCipher.ENCRYPT_MODE, secretKey); EncryptedData encData = xmlCipher.getEncryptedData(); encData.setId(xencEncryptedDataId); encData.setKeyInfo(keyInfo); xmlCipher.doFinal(doc, body, content); Element encDataElem = WSSecurityUtil.findElementById(document.getDocumentElement(), xencEncryptedDataId, null); Node clone = encDataElem.cloneNode(true); elem.appendChild(clone); encDataElem.getParentNode().appendChild(elem); encDataElem.getParentNode().removeChild(encDataElem); } else { xmlCipher.init(XMLCipher.ENCRYPT_MODE, secretKey); EncryptedData encData = xmlCipher.getEncryptedData(); encData.setId(xencEncryptedDataId); encData.setKeyInfo(keyInfo); xmlCipher.doFinal(doc, body, content); } if (cloneKeyInfo) { keyInfo = new KeyInfo((Element) keyInfo.getElement().cloneNode(true), null); } } catch (Exception e2) { throw new WSSecurityException(WSSecurityException.FAILED_ENCRYPTION, null, null, e2); } encDataRef.add("#" + xencEncryptedDataId); } return encDataRef; }
From source file:org.asimba.idp.profile.catalog.saml2.SAML2Catalog.java
private <T extends XMLObject> T cloneXMLObject_usingDOM(XMLObject oSource) throws MarshallingException, UnmarshallingException { try {/* w w w.ja v a2 s. com*/ Element elSource = oSource.getDOM(); // Element elSource = oMarshaller.marshall(oSource, oDocument); // is this right: .getDOM() ?? Element clonedElement = (Element) elSource.cloneNode(true); // deep clone Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(clonedElement); T clonedXMLObject = (T) unmarshaller.unmarshall(clonedElement); return clonedXMLObject; } catch (UnmarshallingException e) { _oLogger.warn("Could not unmarshall element '" + oSource.getElementQName() + "'"); return null; } finally { } }