Example usage for org.w3c.dom Document getElementsByTagNameNS

List of usage examples for org.w3c.dom Document getElementsByTagNameNS

Introduction

In this page you can find the example usage for org.w3c.dom Document getElementsByTagNameNS.

Prototype

public NodeList getElementsByTagNameNS(String namespaceURI, String localName);

Source Link

Document

Returns a NodeList of all the Elements with a given local name and namespace URI in document order.

Usage

From source file:org.owasp.webscarab.plugin.saml.SamlModel.java

public int getSAMLVersion(ConversationID id) {
    Document document = getSAMLDocument(id);
    if (null == document) {
        return 0;
    }/*  www. ja va 2s  .c om*/
    NodeList saml1ResponseNodeList = document.getElementsByTagNameNS("urn:oasis:names:tc:SAML:1.0:protocol",
            "Response");
    if (0 != saml1ResponseNodeList.getLength()) {
        return SAML_VERSION_1_1;
    }
    NodeList saml2AuthnRequestNodeList = document.getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:protocol",
            "AuthnRequest");
    if (0 != saml2AuthnRequestNodeList.getLength()) {
        return SAML_VERSION_2;
    }
    NodeList saml2ResponseNodeList = document.getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:protocol",
            "Response");
    if (0 != saml2ResponseNodeList.getLength()) {
        return SAML_VERSION_2;
    }
    return 0;
}

From source file:org.owasp.webscarab.plugin.saml.SamlModel.java

public boolean hasDestinationIndication(ConversationID id) {
    Document document = getSAMLDocument(id);
    if (null == document) {
        return false;
    }/*www  .j a v  a  2 s . c  om*/
    NodeList saml2ResponseNodeList = document.getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:protocol",
            "Response");
    if (0 != saml2ResponseNodeList.getLength()) {
        return hasDestinationIndicationSaml2Response((Element) saml2ResponseNodeList.item(0));
    }
    NodeList saml2AuthnRequestNodeList = document.getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:protocol",
            "AuthnRequest");
    if (0 != saml2AuthnRequestNodeList.getLength()) {
        return hasDestinationIndicationSaml2AuthnRequest((Element) saml2AuthnRequestNodeList.item(0));
    }
    NodeList saml1ResponseNodeList = document.getElementsByTagNameNS("urn:oasis:names:tc:SAML:1.0:protocol",
            "Response");
    if (0 != saml1ResponseNodeList.getLength()) {
        return hasDestinationIndicationSaml1Response((Element) saml1ResponseNodeList.item(0));
    }
    return false;
}

From source file:org.owasp.webscarab.plugin.saml.SamlModel.java

public static Element findAssertionSignatureElement(Document document) {
    NodeList assertionNodeList = document.getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion",
            "Assertion");
    if (0 == assertionNodeList.getLength()) {
        assertionNodeList = document.getElementsByTagNameNS("urn:oasis:names:tc:SAML:1.0:assertion",
                "Assertion");
        if (0 == assertionNodeList.getLength()) {
            return null;
        }/*from  w  w w .  jav a2  s . c o  m*/
    }
    Node assertionNode = assertionNodeList.item(0);
    NodeList assertionChildrenNodeList = assertionNode.getChildNodes();
    int assertionChildrenNodeCount = assertionChildrenNodeList.getLength();
    for (int nodeIdx = 0; nodeIdx < assertionChildrenNodeCount; nodeIdx++) {
        Node node = assertionChildrenNodeList.item(nodeIdx);
        if (Node.ELEMENT_NODE == node.getNodeType()) {
            Element element = (Element) node;
            if (false == "http://www.w3.org/2000/09/xmldsig#".equals(element.getNamespaceURI())) {
                continue;
            }
            if (false == "Signature".equals(element.getLocalName())) {
                continue;
            }
            return element;
        }
    }
    return null;
}

From source file:org.owasp.webscarab.plugin.saml.SamlModel.java

public boolean protocolSignatureDigestsAssertions(ConversationID id) {
    Document document = getSAMLDocument(id);
    if (null == document) {
        return false;
    }//from w ww.  j  av a2 s  . co  m
    Element protocolSignatureElement = findProtocolSignatureElement(document);
    if (null == protocolSignatureElement) {
        return false;
    }

    NodeList saml2AssertionNodeList = document.getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion",
            "Assertion");
    if (0 != saml2AssertionNodeList.getLength()) {
        try {
            return isDigested(saml2AssertionNodeList, protocolSignatureElement);
        } catch (XMLSignatureException ex) {
            this._logger.log(Level.WARNING, "XML signature error: {0}", ex.getMessage());
        } catch (XMLSecurityException ex) {
            this._logger.log(Level.WARNING, "XML security error: {0}", ex.getMessage());
        }
    }

    NodeList saml1AssertionNodeList = document.getElementsByTagNameNS("urn:oasis:names:tc:SAML:1.0:assertion",
            "Assertion");
    if (0 != saml1AssertionNodeList.getLength()) {
        try {
            return isDigested(saml1AssertionNodeList, protocolSignatureElement);
        } catch (XMLSignatureException ex) {
            this._logger.log(Level.WARNING, "XML signature error: {0}", ex.getMessage());
        } catch (XMLSecurityException ex) {
            this._logger.log(Level.WARNING, "XML security error: {0}", ex.getMessage());
        }
    }

    return false;
}

From source file:org.owasp.webscarab.plugin.saml.SamlModel.java

public List<NamedValue> getSAMLAttributes(ConversationID id) {
    List<NamedValue> samlAttributes = new ArrayList<NamedValue>();

    Document document = getSAMLDocument(id);
    if (null == document) {
        return samlAttributes;
    }//  w  w  w  . j a  v  a2  s.c  o m

    NodeList attributeNodeList = document.getElementsByTagNameNS("urn:oasis:names:tc:SAML:1.0:assertion",
            "Attribute");
    for (int idx = 0; idx < attributeNodeList.getLength(); idx++) {
        Element attributeElement = (Element) attributeNodeList.item(idx);
        String attributeName = attributeElement.getAttribute("AttributeName");
        NodeList attributeValueNodeList = attributeElement
                .getElementsByTagNameNS("urn:oasis:names:tc:SAML:1.0:assertion", "AttributeValue");
        if (0 == attributeValueNodeList.getLength()) {
            continue;
        }
        Element attributeValueElement = (Element) attributeValueNodeList.item(0);
        String attributeValue = attributeValueElement.getChildNodes().item(0).getNodeValue();
        NamedValue attribute = new NamedValue(attributeName, attributeValue);
        samlAttributes.add(attribute);
    }

    NodeList attribute2NodeList = document.getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion",
            "Attribute");
    for (int idx = 0; idx < attribute2NodeList.getLength(); idx++) {
        Element attributeElement = (Element) attribute2NodeList.item(idx);
        String attributeName = attributeElement.getAttribute("Name");
        NodeList attributeValueNodeList = attributeElement
                .getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion", "AttributeValue");
        if (0 == attributeValueNodeList.getLength()) {
            continue;
        }
        Element attributeValueElement = (Element) attributeValueNodeList.item(0);
        String attributeValue = attributeValueElement.getChildNodes().item(0).getNodeValue();
        NamedValue attribute = new NamedValue(attributeName, attributeValue);
        samlAttributes.add(attribute);
    }

    return samlAttributes;
}

From source file:org.owasp.webscarab.plugin.saml.SamlModel.java

public boolean hasValidityIntervalIndication(ConversationID id) {
    Document document = getSAMLDocument(id);
    if (null == document) {
        return false;
    }/*from w  w w. j  a v a  2 s  .co m*/

    NodeList saml1AssertionNodeList = document.getElementsByTagNameNS("urn:oasis:names:tc:SAML:1.0:assertion",
            "Assertion");
    if (0 != saml1AssertionNodeList.getLength()) {
        Element assertionElement = (Element) saml1AssertionNodeList.item(0);
        NodeList conditionsNodeList = assertionElement
                .getElementsByTagNameNS("urn:oasis:names:tc:SAML:1.0:assertion", "Conditions");
        if (0 != conditionsNodeList.getLength()) {
            Element conditionsElement = (Element) conditionsNodeList.item(0);
            if (null != conditionsElement.getAttributeNode("NotBefore")
                    && null != conditionsElement.getAttributeNode("NotOnOrAfter")) {
                return true;
            }
        }
    }

    NodeList saml2AssertionNodeList = document.getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion",
            "Assertion");
    if (0 != saml2AssertionNodeList.getLength()) {
        Element assertionElement = (Element) saml2AssertionNodeList.item(0);
        NodeList conditionsNodeList = assertionElement
                .getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion", "Conditions");
        if (0 != conditionsNodeList.getLength()) {
            Element conditionsElement = (Element) conditionsNodeList.item(0);
            if (null != conditionsElement.getAttributeNode("NotBefore")
                    && null != conditionsElement.getAttributeNode("NotOnOrAfter")) {
                return true;
            }
        }
    }

    NodeList saml2AuthnRequestNodeList = document.getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:protocol",
            "AuthnRequest");
    if (0 != saml2AuthnRequestNodeList.getLength()) {
        Element authnRequestElement = (Element) saml2AuthnRequestNodeList.item(0);
        NodeList conditionsNodeList = authnRequestElement
                .getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion", "Conditions");
        if (0 != conditionsNodeList.getLength()) {
            Element conditionsElement = (Element) conditionsNodeList.item(0);
            if (null != conditionsElement.getAttributeNode("NotBefore")
                    && null != conditionsElement.getAttributeNode("NotOnOrAfter")) {
                return true;
            }
        }
    }

    return false;
}

From source file:org.owasp.webscarab.plugin.saml.SamlModel.java

public boolean hasEncryptedAttributes(ConversationID id) {
    Document document = getSAMLDocument(id);
    if (null == document) {
        return false;
    }// w  w w. j av a2  s. co m

    NodeList encryptedAttributeNodeList = document
            .getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion", "EncryptedAttribute");
    if (0 != encryptedAttributeNodeList.getLength()) {
        return true;
    }
    return false;
}

From source file:org.owasp.webscarab.plugin.saml.SamlModel.java

public List getDecryptedAttributes(ConversationID id, String hexKey) throws Exception {
    List samlAttributes = new ArrayList();

    /*//from   w ww .j  av a  2 s.c o  m
     * We create a new DOM tree as XMLCipher will change the tree.
     */
    String encodedSamlMessage = getEncodedSAMLMessage(id);
    String decodedSamlMessage = getDecodedSAMLMessage(encodedSamlMessage, id);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(decodedSamlMessage.getBytes());
    Document document = this.builder.parse(inputStream);

    byte[] keyBytes = Hex.decode(hexKey);
    SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
    XMLCipher xmlCipher = XMLCipher.getInstance(XMLCipher.AES_128);
    xmlCipher.init(XMLCipher.DECRYPT_MODE, secretKeySpec);

    NodeList encryptedAttributeNodeList = document
            .getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion", "EncryptedAttribute");
    for (int encryptedAttributeIdx = 0; encryptedAttributeIdx < encryptedAttributeNodeList
            .getLength(); encryptedAttributeIdx++) {
        Element encryptedAttributeElement = (Element) encryptedAttributeNodeList.item(encryptedAttributeIdx);
        NodeList encryptedDataNodeList = encryptedAttributeElement
                .getElementsByTagNameNS("http://www.w3.org/2001/04/xmlenc#", "EncryptedData");
        if (1 != encryptedDataNodeList.getLength()) {
            continue;
        }
        Element encryptedDataElement = (Element) encryptedDataNodeList.item(0);
        xmlCipher.doFinal(document, encryptedDataElement);
        NodeList attributeNodeList = encryptedAttributeElement
                .getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion", "Attribute");
        if (1 != attributeNodeList.getLength()) {
            continue;
        }
        Element attributeElement = (Element) attributeNodeList.item(0);
        String attributeName = attributeElement.getAttribute("Name");
        NodeList attributeValueNodeList = attributeElement
                .getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion", "AttributeValue");
        if (0 == attributeValueNodeList.getLength()) {
            continue;
        }
        Element attributeValueElement = (Element) attributeValueNodeList.item(0);
        String attributeValue = attributeValueElement.getChildNodes().item(0).getNodeValue();
        NamedValue attribute = new NamedValue(attributeName, attributeValue);
        samlAttributes.add(attribute);
    }

    return samlAttributes;
}

From source file:org.owasp.webscarab.plugin.saml.SamlModel.java

public byte[] getEncryptedAssertion(ConversationID id) {
    Document samlDocument = getSAMLDocument(id);
    NodeList encryptedAssertionNodeList = samlDocument
            .getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion", "EncryptedAssertion");
    if (encryptedAssertionNodeList.getLength() == 0) {
        return null;
    }//from   ww  w . ja  v  a2s.  co m
    Element encryptedAssertionElement = (Element) encryptedAssertionNodeList.item(0);
    try {
        return toString(encryptedAssertionElement).getBytes();
    } catch (TransformerException ex) {
        return null;
    }
}

From source file:org.owasp.webscarab.plugin.saml.SamlModel.java

public byte[] getDecryptedAssertion(ConversationID id, PrivateKey privateKey)
        throws ParserConfigurationException, SAXException, IOException, TransformerException,
        XMLEncryptionException, Exception {
    byte[] encryptedAssertion = getEncryptedAssertion(id);
    if (null == encryptedAssertion) {
        return null;
    }/*w ww .  j  a va2 s . co  m*/
    if (null == privateKey) {
        return "<error>null private key</error>".getBytes();
    }
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(new ByteArrayInputStream(encryptedAssertion));

    Element encryptedDataElement = (Element) document
            .getElementsByTagNameNS("http://www.w3.org/2001/04/xmlenc#", "EncryptedData").item(0);
    if (null == encryptedDataElement) {
        return "missing encrypted data element".getBytes();
    }
    XMLCipher xmlCipher = XMLCipher.getInstance(XMLCipher.AES_128);
    xmlCipher.init(XMLCipher.DECRYPT_MODE, null);
    xmlCipher.setKEK(privateKey);
    document = xmlCipher.doFinal(document, encryptedDataElement);

    return toString(document).getBytes();
}