Java Code Examples for com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput#setMIMEType()

The following examples show how to use com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput#setMIMEType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DOMURIDereferencer.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public Data dereference(URIReference uriRef, XMLCryptoContext context)
    throws URIReferenceException {

    if (uriRef == null) {
        throw new NullPointerException("uriRef cannot be null");
    }
    if (context == null) {
        throw new NullPointerException("context cannot be null");
    }

    DOMURIReference domRef = (DOMURIReference) uriRef;
    Attr uriAttr = (Attr) domRef.getHere();
    String uri = uriRef.getURI();
    DOMCryptoContext dcc = (DOMCryptoContext) context;
    String baseURI = context.getBaseURI();

    boolean secVal = Utils.secureValidation(context);

    if (secVal && Policy.restrictReferenceUriScheme(uri)) {
        throw new URIReferenceException(
            "Uri " + uri + " is forbidden when secure validation is enabled");
    }

    // Check if same-document URI and already registered on the context
    if (uri != null && uri.length() != 0 && uri.charAt(0) == '#') {
        String id = uri.substring(1);

        if (id.startsWith("xpointer(id(")) {
            int i1 = id.indexOf('\'');
            int i2 = id.indexOf('\'', i1+1);
            id = id.substring(i1+1, i2);
        }

        // check if element is registered by Id
        Node referencedElem = uriAttr.getOwnerDocument().getElementById(id);
        if (referencedElem == null) {
           // see if element is registered in DOMCryptoContext
           referencedElem = dcc.getElementById(id);
        }
        if (referencedElem != null) {
            if (secVal && Policy.restrictDuplicateIds()) {
                Element start = referencedElem.getOwnerDocument().getDocumentElement();
                if (!XMLUtils.protectAgainstWrappingAttack(start, (Element)referencedElem, id)) {
                    String error = "Multiple Elements with the same ID "
                        + id + " detected when secure validation"
                        + " is enabled";
                    throw new URIReferenceException(error);
                }
            }

            XMLSignatureInput result = new XMLSignatureInput(referencedElem);
            if (!uri.substring(1).startsWith("xpointer(id(")) {
                result.setExcludeComments(true);
            }

            result.setMIMEType("text/xml");
            if (baseURI != null && baseURI.length() > 0) {
                result.setSourceURI(baseURI.concat(uriAttr.getNodeValue()));
            } else {
                result.setSourceURI(uriAttr.getNodeValue());
            }
            return new ApacheNodeSetData(result);
        }
    }

    try {
        ResourceResolver apacheResolver =
            ResourceResolver.getInstance(uriAttr, baseURI, false);
        XMLSignatureInput in = apacheResolver.resolve(uriAttr,
                                                      baseURI, false);
        if (in.isOctetStream()) {
            return new ApacheOctetStreamData(in);
        } else {
            return new ApacheNodeSetData(in);
        }
    } catch (Exception e) {
        throw new URIReferenceException(e);
    }
}
 
Example 2
Source File: DOMURIDereferencer.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public Data dereference(URIReference uriRef, XMLCryptoContext context)
    throws URIReferenceException {

    if (uriRef == null) {
        throw new NullPointerException("uriRef cannot be null");
    }
    if (context == null) {
        throw new NullPointerException("context cannot be null");
    }

    DOMURIReference domRef = (DOMURIReference) uriRef;
    Attr uriAttr = (Attr) domRef.getHere();
    String uri = uriRef.getURI();
    DOMCryptoContext dcc = (DOMCryptoContext) context;
    String baseURI = context.getBaseURI();

    boolean secVal = Utils.secureValidation(context);

    if (secVal && Policy.restrictReferenceUriScheme(uri)) {
        throw new URIReferenceException(
            "Uri " + uri + " is forbidden when secure validation is enabled");
    }

    // Check if same-document URI and already registered on the context
    if (uri != null && uri.length() != 0 && uri.charAt(0) == '#') {
        String id = uri.substring(1);

        if (id.startsWith("xpointer(id(")) {
            int i1 = id.indexOf('\'');
            int i2 = id.indexOf('\'', i1+1);
            id = id.substring(i1+1, i2);
        }

        // check if element is registered by Id
        Node referencedElem = uriAttr.getOwnerDocument().getElementById(id);
        if (referencedElem == null) {
           // see if element is registered in DOMCryptoContext
           referencedElem = dcc.getElementById(id);
        }
        if (referencedElem != null) {
            if (secVal && Policy.restrictDuplicateIds()) {
                Element start = referencedElem.getOwnerDocument().getDocumentElement();
                if (!XMLUtils.protectAgainstWrappingAttack(start, (Element)referencedElem, id)) {
                    String error = "Multiple Elements with the same ID "
                        + id + " detected when secure validation"
                        + " is enabled";
                    throw new URIReferenceException(error);
                }
            }

            XMLSignatureInput result = new XMLSignatureInput(referencedElem);
            if (!uri.substring(1).startsWith("xpointer(id(")) {
                result.setExcludeComments(true);
            }

            result.setMIMEType("text/xml");
            if (baseURI != null && baseURI.length() > 0) {
                result.setSourceURI(baseURI.concat(uriAttr.getNodeValue()));
            } else {
                result.setSourceURI(uriAttr.getNodeValue());
            }
            return new ApacheNodeSetData(result);
        }
    }

    try {
        ResourceResolver apacheResolver =
            ResourceResolver.getInstance(uriAttr, baseURI, false);
        XMLSignatureInput in = apacheResolver.resolve(uriAttr,
                                                      baseURI, false);
        if (in.isOctetStream()) {
            return new ApacheOctetStreamData(in);
        } else {
            return new ApacheNodeSetData(in);
        }
    } catch (Exception e) {
        throw new URIReferenceException(e);
    }
}
 
Example 3
Source File: DOMURIDereferencer.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public Data dereference(URIReference uriRef, XMLCryptoContext context)
    throws URIReferenceException {

    if (uriRef == null) {
        throw new NullPointerException("uriRef cannot be null");
    }
    if (context == null) {
        throw new NullPointerException("context cannot be null");
    }

    DOMURIReference domRef = (DOMURIReference) uriRef;
    Attr uriAttr = (Attr) domRef.getHere();
    String uri = uriRef.getURI();
    DOMCryptoContext dcc = (DOMCryptoContext) context;
    String baseURI = context.getBaseURI();

    boolean secVal = Utils.secureValidation(context);

    // Check if same-document URI and already registered on the context
    if (uri != null && uri.length() != 0 && uri.charAt(0) == '#') {
        String id = uri.substring(1);

        if (id.startsWith("xpointer(id(")) {
            int i1 = id.indexOf('\'');
            int i2 = id.indexOf('\'', i1+1);
            id = id.substring(i1+1, i2);
        }

        Node referencedElem = dcc.getElementById(id);
        if (referencedElem != null) {
            if (secVal) {
                Element start = referencedElem.getOwnerDocument().getDocumentElement();
                if (!XMLUtils.protectAgainstWrappingAttack(start, (Element)referencedElem, id)) {
                    String error = "Multiple Elements with the same ID " + id + " were detected";
                    throw new URIReferenceException(error);
                }
            }

            XMLSignatureInput result = new XMLSignatureInput(referencedElem);
            if (!uri.substring(1).startsWith("xpointer(id(")) {
                result.setExcludeComments(true);
            }

            result.setMIMEType("text/xml");
            if (baseURI != null && baseURI.length() > 0) {
                result.setSourceURI(baseURI.concat(uriAttr.getNodeValue()));
            } else {
                result.setSourceURI(uriAttr.getNodeValue());
            }
            return new ApacheNodeSetData(result);
        }
    }

    try {
        ResourceResolver apacheResolver =
            ResourceResolver.getInstance(uriAttr, baseURI, secVal);
        XMLSignatureInput in = apacheResolver.resolve(uriAttr, baseURI);
        if (in.isOctetStream()) {
            return new ApacheOctetStreamData(in);
        } else {
            return new ApacheNodeSetData(in);
        }
    } catch (Exception e) {
        throw new URIReferenceException(e);
    }
}
 
Example 4
Source File: DOMURIDereferencer.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public Data dereference(URIReference uriRef, XMLCryptoContext context)
    throws URIReferenceException {

    if (uriRef == null) {
        throw new NullPointerException("uriRef cannot be null");
    }
    if (context == null) {
        throw new NullPointerException("context cannot be null");
    }

    DOMURIReference domRef = (DOMURIReference) uriRef;
    Attr uriAttr = (Attr) domRef.getHere();
    String uri = uriRef.getURI();
    DOMCryptoContext dcc = (DOMCryptoContext) context;
    String baseURI = context.getBaseURI();

    boolean secVal = Utils.secureValidation(context);

    if (secVal && Policy.restrictReferenceUriScheme(uri)) {
        throw new URIReferenceException(
            "Uri " + uri + " is forbidden when secure validation is enabled");
    }

    // Check if same-document URI and already registered on the context
    if (uri != null && uri.length() != 0 && uri.charAt(0) == '#') {
        String id = uri.substring(1);

        if (id.startsWith("xpointer(id(")) {
            int i1 = id.indexOf('\'');
            int i2 = id.indexOf('\'', i1+1);
            id = id.substring(i1+1, i2);
        }

        // check if element is registered by Id
        Node referencedElem = uriAttr.getOwnerDocument().getElementById(id);
        if (referencedElem == null) {
           // see if element is registered in DOMCryptoContext
           referencedElem = dcc.getElementById(id);
        }
        if (referencedElem != null) {
            if (secVal && Policy.restrictDuplicateIds()) {
                Element start = referencedElem.getOwnerDocument().getDocumentElement();
                if (!XMLUtils.protectAgainstWrappingAttack(start, (Element)referencedElem, id)) {
                    String error = "Multiple Elements with the same ID "
                        + id + " detected when secure validation"
                        + " is enabled";
                    throw new URIReferenceException(error);
                }
            }

            XMLSignatureInput result = new XMLSignatureInput(referencedElem);
            if (!uri.substring(1).startsWith("xpointer(id(")) {
                result.setExcludeComments(true);
            }

            result.setMIMEType("text/xml");
            if (baseURI != null && baseURI.length() > 0) {
                result.setSourceURI(baseURI.concat(uriAttr.getNodeValue()));
            } else {
                result.setSourceURI(uriAttr.getNodeValue());
            }
            return new ApacheNodeSetData(result);
        }
    }

    try {
        ResourceResolver apacheResolver =
            ResourceResolver.getInstance(uriAttr, baseURI, false);
        XMLSignatureInput in = apacheResolver.resolve(uriAttr,
                                                      baseURI, false);
        if (in.isOctetStream()) {
            return new ApacheOctetStreamData(in);
        } else {
            return new ApacheNodeSetData(in);
        }
    } catch (Exception e) {
        throw new URIReferenceException(e);
    }
}
 
Example 5
Source File: DOMURIDereferencer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public Data dereference(URIReference uriRef, XMLCryptoContext context)
    throws URIReferenceException {

    if (uriRef == null) {
        throw new NullPointerException("uriRef cannot be null");
    }
    if (context == null) {
        throw new NullPointerException("context cannot be null");
    }

    DOMURIReference domRef = (DOMURIReference) uriRef;
    Attr uriAttr = (Attr) domRef.getHere();
    String uri = uriRef.getURI();
    DOMCryptoContext dcc = (DOMCryptoContext) context;
    String baseURI = context.getBaseURI();

    boolean secVal = Utils.secureValidation(context);

    if (secVal && Policy.restrictReferenceUriScheme(uri)) {
        throw new URIReferenceException(
            "Uri " + uri + " is forbidden when secure validation is enabled");
    }

    // Check if same-document URI and already registered on the context
    if (uri != null && uri.length() != 0 && uri.charAt(0) == '#') {
        String id = uri.substring(1);

        if (id.startsWith("xpointer(id(")) {
            int i1 = id.indexOf('\'');
            int i2 = id.indexOf('\'', i1+1);
            id = id.substring(i1+1, i2);
        }

        // check if element is registered by Id
        Node referencedElem = uriAttr.getOwnerDocument().getElementById(id);
        if (referencedElem == null) {
           // see if element is registered in DOMCryptoContext
           referencedElem = dcc.getElementById(id);
        }
        if (referencedElem != null) {
            if (secVal && Policy.restrictDuplicateIds()) {
                Element start = referencedElem.getOwnerDocument().getDocumentElement();
                if (!XMLUtils.protectAgainstWrappingAttack(start, (Element)referencedElem, id)) {
                    String error = "Multiple Elements with the same ID "
                        + id + " detected when secure validation"
                        + " is enabled";
                    throw new URIReferenceException(error);
                }
            }

            XMLSignatureInput result = new XMLSignatureInput(referencedElem);
            if (!uri.substring(1).startsWith("xpointer(id(")) {
                result.setExcludeComments(true);
            }

            result.setMIMEType("text/xml");
            if (baseURI != null && baseURI.length() > 0) {
                result.setSourceURI(baseURI.concat(uriAttr.getNodeValue()));
            } else {
                result.setSourceURI(uriAttr.getNodeValue());
            }
            return new ApacheNodeSetData(result);
        }
    }

    try {
        ResourceResolver apacheResolver =
            ResourceResolver.getInstance(uriAttr, baseURI, false);
        XMLSignatureInput in = apacheResolver.resolve(uriAttr,
                                                      baseURI, false);
        if (in.isOctetStream()) {
            return new ApacheOctetStreamData(in);
        } else {
            return new ApacheNodeSetData(in);
        }
    } catch (Exception e) {
        throw new URIReferenceException(e);
    }
}
 
Example 6
Source File: DOMURIDereferencer.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public Data dereference(URIReference uriRef, XMLCryptoContext context)
    throws URIReferenceException {

    if (uriRef == null) {
        throw new NullPointerException("uriRef cannot be null");
    }
    if (context == null) {
        throw new NullPointerException("context cannot be null");
    }

    DOMURIReference domRef = (DOMURIReference) uriRef;
    Attr uriAttr = (Attr) domRef.getHere();
    String uri = uriRef.getURI();
    DOMCryptoContext dcc = (DOMCryptoContext) context;
    String baseURI = context.getBaseURI();

    boolean secVal = Utils.secureValidation(context);

    if (secVal && Policy.restrictReferenceUriScheme(uri)) {
        throw new URIReferenceException(
            "Uri " + uri + " is forbidden when secure validation is enabled");
    }

    // Check if same-document URI and already registered on the context
    if (uri != null && uri.length() != 0 && uri.charAt(0) == '#') {
        String id = uri.substring(1);

        if (id.startsWith("xpointer(id(")) {
            int i1 = id.indexOf('\'');
            int i2 = id.indexOf('\'', i1+1);
            id = id.substring(i1+1, i2);
        }

        // check if element is registered by Id
        Node referencedElem = uriAttr.getOwnerDocument().getElementById(id);
        if (referencedElem == null) {
           // see if element is registered in DOMCryptoContext
           referencedElem = dcc.getElementById(id);
        }
        if (referencedElem != null) {
            if (secVal && Policy.restrictDuplicateIds()) {
                Element start = referencedElem.getOwnerDocument().getDocumentElement();
                if (!XMLUtils.protectAgainstWrappingAttack(start, (Element)referencedElem, id)) {
                    String error = "Multiple Elements with the same ID "
                        + id + " detected when secure validation"
                        + " is enabled";
                    throw new URIReferenceException(error);
                }
            }

            XMLSignatureInput result = new XMLSignatureInput(referencedElem);
            if (!uri.substring(1).startsWith("xpointer(id(")) {
                result.setExcludeComments(true);
            }

            result.setMIMEType("text/xml");
            if (baseURI != null && baseURI.length() > 0) {
                result.setSourceURI(baseURI.concat(uriAttr.getNodeValue()));
            } else {
                result.setSourceURI(uriAttr.getNodeValue());
            }
            return new ApacheNodeSetData(result);
        }
    }

    try {
        ResourceResolver apacheResolver =
            ResourceResolver.getInstance(uriAttr, baseURI, false);
        XMLSignatureInput in = apacheResolver.resolve(uriAttr,
                                                      baseURI, false);
        if (in.isOctetStream()) {
            return new ApacheOctetStreamData(in);
        } else {
            return new ApacheNodeSetData(in);
        }
    } catch (Exception e) {
        throw new URIReferenceException(e);
    }
}
 
Example 7
Source File: DOMURIDereferencer.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public Data dereference(URIReference uriRef, XMLCryptoContext context)
    throws URIReferenceException {

    if (uriRef == null) {
        throw new NullPointerException("uriRef cannot be null");
    }
    if (context == null) {
        throw new NullPointerException("context cannot be null");
    }

    DOMURIReference domRef = (DOMURIReference) uriRef;
    Attr uriAttr = (Attr) domRef.getHere();
    String uri = uriRef.getURI();
    DOMCryptoContext dcc = (DOMCryptoContext) context;
    String baseURI = context.getBaseURI();

    boolean secVal = Utils.secureValidation(context);

    if (secVal && Policy.restrictReferenceUriScheme(uri)) {
        throw new URIReferenceException(
            "Uri " + uri + " is forbidden when secure validation is enabled");
    }

    // Check if same-document URI and already registered on the context
    if (uri != null && uri.length() != 0 && uri.charAt(0) == '#') {
        String id = uri.substring(1);

        if (id.startsWith("xpointer(id(")) {
            int i1 = id.indexOf('\'');
            int i2 = id.indexOf('\'', i1+1);
            id = id.substring(i1+1, i2);
        }

        // check if element is registered by Id
        Node referencedElem = uriAttr.getOwnerDocument().getElementById(id);
        if (referencedElem == null) {
           // see if element is registered in DOMCryptoContext
           referencedElem = dcc.getElementById(id);
        }
        if (referencedElem != null) {
            if (secVal && Policy.restrictDuplicateIds()) {
                Element start = referencedElem.getOwnerDocument().getDocumentElement();
                if (!XMLUtils.protectAgainstWrappingAttack(start, (Element)referencedElem, id)) {
                    String error = "Multiple Elements with the same ID "
                        + id + " detected when secure validation"
                        + " is enabled";
                    throw new URIReferenceException(error);
                }
            }

            XMLSignatureInput result = new XMLSignatureInput(referencedElem);
            if (!uri.substring(1).startsWith("xpointer(id(")) {
                result.setExcludeComments(true);
            }

            result.setMIMEType("text/xml");
            if (baseURI != null && baseURI.length() > 0) {
                result.setSourceURI(baseURI.concat(uriAttr.getNodeValue()));
            } else {
                result.setSourceURI(uriAttr.getNodeValue());
            }
            return new ApacheNodeSetData(result);
        }
    }

    try {
        ResourceResolver apacheResolver =
            ResourceResolver.getInstance(uriAttr, baseURI, false);
        XMLSignatureInput in = apacheResolver.resolve(uriAttr,
                                                      baseURI, false);
        if (in.isOctetStream()) {
            return new ApacheOctetStreamData(in);
        } else {
            return new ApacheNodeSetData(in);
        }
    } catch (Exception e) {
        throw new URIReferenceException(e);
    }
}
 
Example 8
Source File: DOMURIDereferencer.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public Data dereference(URIReference uriRef, XMLCryptoContext context)
    throws URIReferenceException {

    if (uriRef == null) {
        throw new NullPointerException("uriRef cannot be null");
    }
    if (context == null) {
        throw new NullPointerException("context cannot be null");
    }

    DOMURIReference domRef = (DOMURIReference) uriRef;
    Attr uriAttr = (Attr) domRef.getHere();
    String uri = uriRef.getURI();
    DOMCryptoContext dcc = (DOMCryptoContext) context;
    String baseURI = context.getBaseURI();

    boolean secVal = Utils.secureValidation(context);

    // Check if same-document URI and already registered on the context
    if (uri != null && uri.length() != 0 && uri.charAt(0) == '#') {
        String id = uri.substring(1);

        if (id.startsWith("xpointer(id(")) {
            int i1 = id.indexOf('\'');
            int i2 = id.indexOf('\'', i1+1);
            id = id.substring(i1+1, i2);
        }

        Node referencedElem = dcc.getElementById(id);
        if (referencedElem != null) {
            if (secVal) {
                Element start = referencedElem.getOwnerDocument().getDocumentElement();
                if (!XMLUtils.protectAgainstWrappingAttack(start, (Element)referencedElem, id)) {
                    String error = "Multiple Elements with the same ID " + id + " were detected";
                    throw new URIReferenceException(error);
                }
            }

            XMLSignatureInput result = new XMLSignatureInput(referencedElem);
            if (!uri.substring(1).startsWith("xpointer(id(")) {
                result.setExcludeComments(true);
            }

            result.setMIMEType("text/xml");
            if (baseURI != null && baseURI.length() > 0) {
                result.setSourceURI(baseURI.concat(uriAttr.getNodeValue()));
            } else {
                result.setSourceURI(uriAttr.getNodeValue());
            }
            return new ApacheNodeSetData(result);
        }
    }

    try {
        ResourceResolver apacheResolver =
            ResourceResolver.getInstance(uriAttr, baseURI, secVal);
        XMLSignatureInput in = apacheResolver.resolve(uriAttr,
                                                      baseURI, secVal);
        if (in.isOctetStream()) {
            return new ApacheOctetStreamData(in);
        } else {
            return new ApacheNodeSetData(in);
        }
    } catch (Exception e) {
        throw new URIReferenceException(e);
    }
}
 
Example 9
Source File: DOMURIDereferencer.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public Data dereference(URIReference uriRef, XMLCryptoContext context)
    throws URIReferenceException {

    if (uriRef == null) {
        throw new NullPointerException("uriRef cannot be null");
    }
    if (context == null) {
        throw new NullPointerException("context cannot be null");
    }

    DOMURIReference domRef = (DOMURIReference) uriRef;
    Attr uriAttr = (Attr) domRef.getHere();
    String uri = uriRef.getURI();
    DOMCryptoContext dcc = (DOMCryptoContext) context;
    String baseURI = context.getBaseURI();

    boolean secVal = Utils.secureValidation(context);

    // Check if same-document URI and already registered on the context
    if (uri != null && uri.length() != 0 && uri.charAt(0) == '#') {
        String id = uri.substring(1);

        if (id.startsWith("xpointer(id(")) {
            int i1 = id.indexOf('\'');
            int i2 = id.indexOf('\'', i1+1);
            id = id.substring(i1+1, i2);
        }

        Node referencedElem = dcc.getElementById(id);
        if (referencedElem != null) {
            if (secVal) {
                Element start = referencedElem.getOwnerDocument().getDocumentElement();
                if (!XMLUtils.protectAgainstWrappingAttack(start, (Element)referencedElem, id)) {
                    String error = "Multiple Elements with the same ID " + id + " were detected";
                    throw new URIReferenceException(error);
                }
            }

            XMLSignatureInput result = new XMLSignatureInput(referencedElem);
            if (!uri.substring(1).startsWith("xpointer(id(")) {
                result.setExcludeComments(true);
            }

            result.setMIMEType("text/xml");
            if (baseURI != null && baseURI.length() > 0) {
                result.setSourceURI(baseURI.concat(uriAttr.getNodeValue()));
            } else {
                result.setSourceURI(uriAttr.getNodeValue());
            }
            return new ApacheNodeSetData(result);
        }
    }

    try {
        ResourceResolver apacheResolver =
            ResourceResolver.getInstance(uriAttr, baseURI, secVal);
        XMLSignatureInput in = apacheResolver.resolve(uriAttr, baseURI);
        if (in.isOctetStream()) {
            return new ApacheOctetStreamData(in);
        } else {
            return new ApacheNodeSetData(in);
        }
    } catch (Exception e) {
        throw new URIReferenceException(e);
    }
}
 
Example 10
Source File: DOMURIDereferencer.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public Data dereference(URIReference uriRef, XMLCryptoContext context)
    throws URIReferenceException {

    if (uriRef == null) {
        throw new NullPointerException("uriRef cannot be null");
    }
    if (context == null) {
        throw new NullPointerException("context cannot be null");
    }

    DOMURIReference domRef = (DOMURIReference) uriRef;
    Attr uriAttr = (Attr) domRef.getHere();
    String uri = uriRef.getURI();
    DOMCryptoContext dcc = (DOMCryptoContext) context;
    String baseURI = context.getBaseURI();

    boolean secVal = Utils.secureValidation(context);

    // Check if same-document URI and already registered on the context
    if (uri != null && uri.length() != 0 && uri.charAt(0) == '#') {
        String id = uri.substring(1);

        if (id.startsWith("xpointer(id(")) {
            int i1 = id.indexOf('\'');
            int i2 = id.indexOf('\'', i1+1);
            id = id.substring(i1+1, i2);
        }

        Node referencedElem = dcc.getElementById(id);
        if (referencedElem != null) {
            if (secVal) {
                Element start = referencedElem.getOwnerDocument().getDocumentElement();
                if (!XMLUtils.protectAgainstWrappingAttack(start, (Element)referencedElem, id)) {
                    String error = "Multiple Elements with the same ID " + id + " were detected";
                    throw new URIReferenceException(error);
                }
            }

            XMLSignatureInput result = new XMLSignatureInput(referencedElem);
            if (!uri.substring(1).startsWith("xpointer(id(")) {
                result.setExcludeComments(true);
            }

            result.setMIMEType("text/xml");
            if (baseURI != null && baseURI.length() > 0) {
                result.setSourceURI(baseURI.concat(uriAttr.getNodeValue()));
            } else {
                result.setSourceURI(uriAttr.getNodeValue());
            }
            return new ApacheNodeSetData(result);
        }
    }

    try {
        ResourceResolver apacheResolver =
            ResourceResolver.getInstance(uriAttr, baseURI, secVal);
        XMLSignatureInput in = apacheResolver.resolve(uriAttr, baseURI);
        if (in.isOctetStream()) {
            return new ApacheOctetStreamData(in);
        } else {
            return new ApacheNodeSetData(in);
        }
    } catch (Exception e) {
        throw new URIReferenceException(e);
    }
}
 
Example 11
Source File: DOMURIDereferencer.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public Data dereference(URIReference uriRef, XMLCryptoContext context)
    throws URIReferenceException {

    if (uriRef == null) {
        throw new NullPointerException("uriRef cannot be null");
    }
    if (context == null) {
        throw new NullPointerException("context cannot be null");
    }

    DOMURIReference domRef = (DOMURIReference) uriRef;
    Attr uriAttr = (Attr) domRef.getHere();
    String uri = uriRef.getURI();
    DOMCryptoContext dcc = (DOMCryptoContext) context;
    String baseURI = context.getBaseURI();

    boolean secVal = Utils.secureValidation(context);

    if (secVal && Policy.restrictReferenceUriScheme(uri)) {
        throw new URIReferenceException(
            "Uri " + uri + " is forbidden when secure validation is enabled");
    }

    // Check if same-document URI and already registered on the context
    if (uri != null && uri.length() != 0 && uri.charAt(0) == '#') {
        String id = uri.substring(1);

        if (id.startsWith("xpointer(id(")) {
            int i1 = id.indexOf('\'');
            int i2 = id.indexOf('\'', i1+1);
            id = id.substring(i1+1, i2);
        }

        // check if element is registered by Id
        Node referencedElem = uriAttr.getOwnerDocument().getElementById(id);
        if (referencedElem == null) {
           // see if element is registered in DOMCryptoContext
           referencedElem = dcc.getElementById(id);
        }
        if (referencedElem != null) {
            if (secVal && Policy.restrictDuplicateIds()) {
                Element start = referencedElem.getOwnerDocument().getDocumentElement();
                if (!XMLUtils.protectAgainstWrappingAttack(start, (Element)referencedElem, id)) {
                    String error = "Multiple Elements with the same ID "
                        + id + " detected when secure validation"
                        + " is enabled";
                    throw new URIReferenceException(error);
                }
            }

            XMLSignatureInput result = new XMLSignatureInput(referencedElem);
            if (!uri.substring(1).startsWith("xpointer(id(")) {
                result.setExcludeComments(true);
            }

            result.setMIMEType("text/xml");
            if (baseURI != null && baseURI.length() > 0) {
                result.setSourceURI(baseURI.concat(uriAttr.getNodeValue()));
            } else {
                result.setSourceURI(uriAttr.getNodeValue());
            }
            return new ApacheNodeSetData(result);
        }
    }

    try {
        ResourceResolver apacheResolver =
            ResourceResolver.getInstance(uriAttr, baseURI, false);
        XMLSignatureInput in = apacheResolver.resolve(uriAttr,
                                                      baseURI, false);
        if (in.isOctetStream()) {
            return new ApacheOctetStreamData(in);
        } else {
            return new ApacheNodeSetData(in);
        }
    } catch (Exception e) {
        throw new URIReferenceException(e);
    }
}
 
Example 12
Source File: DOMURIDereferencer.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public Data dereference(URIReference uriRef, XMLCryptoContext context)
    throws URIReferenceException {

    if (uriRef == null) {
        throw new NullPointerException("uriRef cannot be null");
    }
    if (context == null) {
        throw new NullPointerException("context cannot be null");
    }

    DOMURIReference domRef = (DOMURIReference) uriRef;
    Attr uriAttr = (Attr) domRef.getHere();
    String uri = uriRef.getURI();
    DOMCryptoContext dcc = (DOMCryptoContext) context;
    String baseURI = context.getBaseURI();

    boolean secVal = Utils.secureValidation(context);

    // Check if same-document URI and already registered on the context
    if (uri != null && uri.length() != 0 && uri.charAt(0) == '#') {
        String id = uri.substring(1);

        if (id.startsWith("xpointer(id(")) {
            int i1 = id.indexOf('\'');
            int i2 = id.indexOf('\'', i1+1);
            id = id.substring(i1+1, i2);
        }

        Node referencedElem = dcc.getElementById(id);
        if (referencedElem != null) {
            if (secVal) {
                Element start = referencedElem.getOwnerDocument().getDocumentElement();
                if (!XMLUtils.protectAgainstWrappingAttack(start, (Element)referencedElem, id)) {
                    String error = "Multiple Elements with the same ID " + id + " were detected";
                    throw new URIReferenceException(error);
                }
            }

            XMLSignatureInput result = new XMLSignatureInput(referencedElem);
            if (!uri.substring(1).startsWith("xpointer(id(")) {
                result.setExcludeComments(true);
            }

            result.setMIMEType("text/xml");
            if (baseURI != null && baseURI.length() > 0) {
                result.setSourceURI(baseURI.concat(uriAttr.getNodeValue()));
            } else {
                result.setSourceURI(uriAttr.getNodeValue());
            }
            return new ApacheNodeSetData(result);
        }
    }

    try {
        ResourceResolver apacheResolver =
            ResourceResolver.getInstance(uriAttr, baseURI, secVal);
        XMLSignatureInput in = apacheResolver.resolve(uriAttr, baseURI);
        if (in.isOctetStream()) {
            return new ApacheOctetStreamData(in);
        } else {
            return new ApacheNodeSetData(in);
        }
    } catch (Exception e) {
        throw new URIReferenceException(e);
    }
}
 
Example 13
Source File: DOMURIDereferencer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public Data dereference(URIReference uriRef, XMLCryptoContext context)
    throws URIReferenceException {

    if (uriRef == null) {
        throw new NullPointerException("uriRef cannot be null");
    }
    if (context == null) {
        throw new NullPointerException("context cannot be null");
    }

    DOMURIReference domRef = (DOMURIReference) uriRef;
    Attr uriAttr = (Attr) domRef.getHere();
    String uri = uriRef.getURI();
    DOMCryptoContext dcc = (DOMCryptoContext) context;
    String baseURI = context.getBaseURI();

    boolean secVal = Utils.secureValidation(context);

    // Check if same-document URI and already registered on the context
    if (uri != null && uri.length() != 0 && uri.charAt(0) == '#') {
        String id = uri.substring(1);

        if (id.startsWith("xpointer(id(")) {
            int i1 = id.indexOf('\'');
            int i2 = id.indexOf('\'', i1+1);
            id = id.substring(i1+1, i2);
        }

        Node referencedElem = dcc.getElementById(id);
        if (referencedElem != null) {
            if (secVal) {
                Element start = referencedElem.getOwnerDocument().getDocumentElement();
                if (!XMLUtils.protectAgainstWrappingAttack(start, (Element)referencedElem, id)) {
                    String error = "Multiple Elements with the same ID " + id + " were detected";
                    throw new URIReferenceException(error);
                }
            }

            XMLSignatureInput result = new XMLSignatureInput(referencedElem);
            if (!uri.substring(1).startsWith("xpointer(id(")) {
                result.setExcludeComments(true);
            }

            result.setMIMEType("text/xml");
            if (baseURI != null && baseURI.length() > 0) {
                result.setSourceURI(baseURI.concat(uriAttr.getNodeValue()));
            } else {
                result.setSourceURI(uriAttr.getNodeValue());
            }
            return new ApacheNodeSetData(result);
        }
    }

    try {
        ResourceResolver apacheResolver =
            ResourceResolver.getInstance(uriAttr, baseURI, secVal);
        XMLSignatureInput in = apacheResolver.resolve(uriAttr, baseURI);
        if (in.isOctetStream()) {
            return new ApacheOctetStreamData(in);
        } else {
            return new ApacheNodeSetData(in);
        }
    } catch (Exception e) {
        throw new URIReferenceException(e);
    }
}