src/java.xml.crypto/share/classes/org/jcp/xml/dsig/internal/dom/Utils.java
author jlahoda
Tue, 24 Sep 2019 15:40:26 +0200
branchJDK-8226585-branch
changeset 58290 d885633d9de4
parent 50614 3810c9a2efa1
permissions -rw-r--r--
Converting the test to a combo-framework test.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     2
 * reserved comment block
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
     5
/**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
     7
 * or more contributor license agreements. See the NOTICE file
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
     8
 * distributed with this work for additional information
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
     9
 * regarding copyright ownership. The ASF licenses this file
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    10
 * to you under the Apache License, Version 2.0 (the
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    11
 * "License"); you may not use this file except in compliance
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    12
 * with the License. You may obtain a copy of the License at
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    13
 *
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    14
 * http://www.apache.org/licenses/LICENSE-2.0
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    15
 *
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    16
 * Unless required by applicable law or agreed to in writing,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    17
 * software distributed under the License is distributed on an
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    18
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    19
 * KIND, either express or implied. See the License for the
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    20
 * specific language governing permissions and limitations
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    21
 * under the License.
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    22
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    23
/*
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    24
 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    25
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    26
/*
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    27
 * $Id: Utils.java 1788465 2017-03-24 15:10:51Z coheigea $
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
package org.jcp.xml.dsig.internal.dom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    35
import javax.xml.crypto.XMLCryptoContext;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import org.w3c.dom.NamedNodeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Miscellaneous static utility methods for use in JSR 105 RI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public final class Utils {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private Utils() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static byte[] readBytesFromStream(InputStream is)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    48
        throws IOException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    49
    {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    50
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    51
            byte[] buf = new byte[1024];
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    52
            while (true) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    53
                int read = is.read(buf);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    54
                if (read == -1) { // EOF
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    55
                    break;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    56
                }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    57
                baos.write(buf, 0, read);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    58
                if (read < 1024) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    59
                    break;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    60
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            }
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    62
            return baos.toByteArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Converts an Iterator to a Set of Nodes, according to the XPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Data Model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @param i the Iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @return the Set of Nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
    73
    static Set<Node> toNodeSet(Iterator<?> i) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    74
        Set<Node> nodeSet = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        while (i.hasNext()) {
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
    76
            Node n = (Node)i.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            nodeSet.add(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            // insert attributes nodes to comply with XPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (n.getNodeType() == Node.ELEMENT_NODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                NamedNodeMap nnm = n.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                for (int j = 0, length = nnm.getLength(); j < length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    nodeSet.add(nnm.item(j));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return nodeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Returns the ID from a same-document URI (ex: "#id")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public static String parseIdFromSameDocumentURI(String uri) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (uri.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        String id = uri.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (id != null && id.startsWith("xpointer(id(")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            int i1 = id.indexOf('\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            int i2 = id.indexOf('\'', i1+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            id = id.substring(i1+1, i2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Returns true if uri is a same-document URI, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public static boolean sameDocumentURI(String uri) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   109
        return uri != null && (uri.length() == 0 || uri.charAt(0) == '#');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   111
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   112
    static boolean secureValidation(XMLCryptoContext xc) {
18266
26e69da689b9 8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents: 18240
diff changeset
   113
        if (xc == null) {
26e69da689b9 8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents: 18240
diff changeset
   114
            return false;
26e69da689b9 8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents: 18240
diff changeset
   115
        }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   116
        return getBoolean(xc, "org.jcp.xml.dsig.secureValidation");
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   117
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   118
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   119
    private static boolean getBoolean(XMLCryptoContext xc, String name) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   120
        Boolean value = (Boolean)xc.getProperty(name);
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   121
        return value != null && value.booleanValue();
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   122
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
}