src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/utils/XMLUtils.java
author weijun
Thu, 10 Oct 2019 17:36:38 +0300
changeset 59240 b3116877866f
parent 54731 81de17a33575
permissions -rw-r--r--
8231507: Update Apache Santuario (XML Signature) to version 2.1.4 Reviewed-by: weijun Contributed-by: fedor.burdun@azulsystems.com, weijun.wang@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
     5
/**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
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: 18240
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: 18240
diff changeset
     8
 * distributed with this work for additional information
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
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: 18240
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: 18240
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: 18240
diff changeset
    12
 * with the License. You may obtain a copy of the License at
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    14
 * http://www.apache.org/licenses/LICENSE-2.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
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: 18240
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: 18240
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: 18240
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: 18240
diff changeset
    20
 * specific language governing permissions and limitations
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    21
 * under the License.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
package com.sun.org.apache.xml.internal.security.utils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.io.IOException;
59240
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
    26
import java.io.InputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.OutputStream;
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    28
import java.math.BigInteger;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    29
import java.security.AccessController;
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    30
import java.security.PrivilegedAction;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    31
import java.util.ArrayList;
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
    32
import java.util.Base64;
59240
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
    33
import java.util.Collections;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Iterator;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    36
import java.util.List;
59240
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
    37
import java.util.Map;
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
    38
import java.util.Queue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Set;
59240
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
    40
import java.util.WeakHashMap;
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
    41
import java.util.concurrent.ArrayBlockingQueue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    43
import javax.xml.parsers.DocumentBuilder;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    44
import javax.xml.parsers.DocumentBuilderFactory;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    45
import javax.xml.parsers.ParserConfigurationException;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    46
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import org.w3c.dom.Attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import org.w3c.dom.Document;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import org.w3c.dom.Element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import org.w3c.dom.NamedNodeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import org.w3c.dom.NodeList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import org.w3c.dom.Text;
59240
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
    57
import org.xml.sax.InputSource;
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
    58
import org.xml.sax.SAXException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * DOM and XML accessibility and comfort functions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    64
public final class XMLUtils {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    66
    private static boolean ignoreLineBreaks =
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    67
        AccessController.doPrivileged(
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    68
            (PrivilegedAction<Boolean>) () -> Boolean.getBoolean("com.sun.org.apache.xml.internal.security.ignoreLineBreaks"));
59240
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
    69
    private static int parserPoolSize =
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
    70
        AccessController.doPrivileged(
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
    71
            (PrivilegedAction<Integer>) () -> Integer.getInteger("com.sun.org.apache.xml.internal.security.parser.pool-size", 20));
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    72
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    73
    private static volatile String dsPrefix = "ds";
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    74
    private static volatile String ds11Prefix = "dsig11";
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    75
    private static volatile String xencPrefix = "xenc";
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    76
    private static volatile String xenc11Prefix = "xenc11";
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    77
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    78
    private static final com.sun.org.slf4j.internal.Logger LOG =
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    79
        com.sun.org.slf4j.internal.LoggerFactory.getLogger(XMLUtils.class);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    80
59240
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
    81
    private static final Map<ClassLoader, Queue<DocumentBuilder>> DOCUMENT_BUILDERS =
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
    82
        Collections.synchronizedMap(new WeakHashMap<ClassLoader, Queue<DocumentBuilder>>());
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
    83
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
    84
    private static final Map<ClassLoader, Queue<DocumentBuilder>> DOCUMENT_BUILDERS_DISALLOW_DOCTYPE =
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
    85
        Collections.synchronizedMap(new WeakHashMap<ClassLoader, Queue<DocumentBuilder>>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    87
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    88
     * Constructor XMLUtils
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    89
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    90
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    91
    private XMLUtils() {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    92
        // we don't allow instantiation
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    93
    }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    94
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    95
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    96
     * Set the prefix for the digital signature namespace
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    97
     * @param prefix the new prefix for the digital signature namespace
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    98
     * @throws SecurityException if a security manager is installed and the
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    99
     *    caller does not have permission to set the prefix
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   100
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   101
    public static void setDsPrefix(String prefix) {
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   102
        JavaUtils.checkRegisterPermission();
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   103
        dsPrefix = prefix;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   104
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   105
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   106
    /**
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   107
     * Set the prefix for the digital signature 1.1 namespace
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   108
     * @param prefix the new prefix for the digital signature 1.1 namespace
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   109
     * @throws SecurityException if a security manager is installed and the
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   110
     *    caller does not have permission to set the prefix
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   111
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   112
    public static void setDs11Prefix(String prefix) {
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   113
        JavaUtils.checkRegisterPermission();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   114
        ds11Prefix = prefix;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   115
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   116
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   117
    /**
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   118
     * Set the prefix for the encryption namespace
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   119
     * @param prefix the new prefix for the encryption namespace
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   120
     * @throws SecurityException if a security manager is installed and the
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   121
     *    caller does not have permission to set the prefix
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   122
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   123
    public static void setXencPrefix(String prefix) {
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   124
        JavaUtils.checkRegisterPermission();
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   125
        xencPrefix = prefix;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   126
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   127
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   128
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   129
     * Set the prefix for the encryption namespace 1.1
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   130
     * @param prefix the new prefix for the encryption namespace 1.1
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   131
     * @throws SecurityException if a security manager is installed and the
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   132
     *    caller does not have permission to set the prefix
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   133
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   134
    public static void setXenc11Prefix(String prefix) {
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   135
        JavaUtils.checkRegisterPermission();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   136
        xenc11Prefix = prefix;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   137
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   139
    public static Element getNextElement(Node el) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   140
        Node node = el;
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   141
        while (node != null && node.getNodeType() != Node.ELEMENT_NODE) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   142
            node = node.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   143
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   144
        return (Element)node;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   145
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   146
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   147
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   148
     * @param rootNode
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   149
     * @param result
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   150
     * @param exclude
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   151
     * @param com whether comments or not
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   152
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   153
    public static void getSet(Node rootNode, Set<Node> result, Node exclude, boolean com) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   154
        if (exclude != null && isDescendantOrSelf(exclude, rootNode)) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   155
            return;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   156
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   157
        getSetRec(rootNode, result, exclude, com);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   158
    }
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   159
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   160
    @SuppressWarnings("fallthrough")
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   161
    private static void getSetRec(final Node rootNode, final Set<Node> result,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   162
                                final Node exclude, final boolean com) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   163
        if (rootNode == exclude) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   164
            return;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   165
        }
59240
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   166
        switch (rootNode.getNodeType()) { //NOPMD
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   167
        case Node.ELEMENT_NODE:
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   168
            result.add(rootNode);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   169
            Element el = (Element)rootNode;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   170
            if (el.hasAttributes()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   171
                NamedNodeMap nl = el.getAttributes();
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   172
                int length = nl.getLength();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   173
                for (int i = 0; i < length; i++) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   174
                    result.add(nl.item(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   176
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   177
            //no return keep working
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   178
        case Node.DOCUMENT_NODE:
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   179
            for (Node r = rootNode.getFirstChild(); r != null; r = r.getNextSibling()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   180
                if (r.getNodeType() == Node.TEXT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   181
                    result.add(r);
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   182
                    while (r != null && r.getNodeType() == Node.TEXT_NODE) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   183
                        r = r.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   184
                    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   185
                    if (r == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   186
                        return;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   187
                    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   188
                }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   189
                getSetRec(r, result, exclude, com);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   190
            }
59240
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   191
            break;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   192
        case Node.COMMENT_NODE:
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   193
            if (com) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   194
                result.add(rootNode);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   195
            }
59240
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   196
            break;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   197
        case Node.DOCUMENT_TYPE_NODE:
59240
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   198
            break;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   199
        default:
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   200
            result.add(rootNode);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   201
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   202
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   204
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   205
     * Outputs a DOM tree to an {@link OutputStream}.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   206
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   207
     * @param contextNode root node of the DOM tree
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   208
     * @param os the {@link OutputStream}
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   209
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   210
    public static void outputDOM(Node contextNode, OutputStream os) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   211
        XMLUtils.outputDOM(contextNode, os, false);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   212
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   214
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   215
     * Outputs a DOM tree to an {@link OutputStream}. <I>If an Exception is
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   216
     * thrown during execution, it's StackTrace is output to System.out, but the
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   217
     * Exception is not re-thrown.</I>
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   218
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   219
     * @param contextNode root node of the DOM tree
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   220
     * @param os the {@link OutputStream}
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   221
     * @param addPreamble
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   222
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   223
    public static void outputDOM(Node contextNode, OutputStream os, boolean addPreamble) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   224
        try {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   225
            if (addPreamble) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   226
                os.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".getBytes(java.nio.charset.StandardCharsets.UTF_8));
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   227
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   229
            os.write(Canonicalizer.getInstance(
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   230
                Canonicalizer.ALGO_ID_C14N_PHYSICAL).canonicalizeSubtree(contextNode)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   231
            );
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   232
        } catch (IOException ex) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   233
            LOG.debug(ex.getMessage(), ex);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   234
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   235
        catch (InvalidCanonicalizerException ex) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   236
            LOG.debug(ex.getMessage(), ex);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   237
        } catch (CanonicalizationException ex) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   238
            LOG.debug(ex.getMessage(), ex);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   239
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   240
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   242
    /**
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   243
     * Serializes the {@code contextNode} into the OutputStream, <I>but
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   244
     * suppresses all Exceptions</I>.
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   245
     * <p></p>
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   246
     * NOTE: <I>This should only be used for debugging purposes,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   247
     * NOT in a production environment; this method ignores all exceptions,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   248
     * so you won't notice if something goes wrong. If you're asking what is to
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   249
     * be used in a production environment, simply use the code inside the
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   250
     * {@code try{}} statement, but handle the Exceptions appropriately.</I>
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   251
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   252
     * @param contextNode
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   253
     * @param os
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   254
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   255
    public static void outputDOMc14nWithComments(Node contextNode, OutputStream os) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   256
        try {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   257
            os.write(Canonicalizer.getInstance(
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   258
                Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS).canonicalizeSubtree(contextNode)
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   259
            );
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   260
        } catch (IOException ex) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   261
            LOG.debug(ex.getMessage(), ex);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   262
            // throw new RuntimeException(ex.getMessage());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   263
        } catch (InvalidCanonicalizerException ex) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   264
            LOG.debug(ex.getMessage(), ex);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   265
            // throw new RuntimeException(ex.getMessage());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   266
        } catch (CanonicalizationException ex) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   267
            LOG.debug(ex.getMessage(), ex);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   268
            // throw new RuntimeException(ex.getMessage());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   269
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   270
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
54731
81de17a33575 8219013: Update Apache Santuario (XML Signature) to version 2.1.3
weijun
parents: 53998
diff changeset
   272
    @Deprecated
81de17a33575 8219013: Update Apache Santuario (XML Signature) to version 2.1.3
weijun
parents: 53998
diff changeset
   273
    public static String getFullTextChildrenFromElement(Element element) {
81de17a33575 8219013: Update Apache Santuario (XML Signature) to version 2.1.3
weijun
parents: 53998
diff changeset
   274
        return getFullTextChildrenFromNode(element);
81de17a33575 8219013: Update Apache Santuario (XML Signature) to version 2.1.3
weijun
parents: 53998
diff changeset
   275
    }
81de17a33575 8219013: Update Apache Santuario (XML Signature) to version 2.1.3
weijun
parents: 53998
diff changeset
   276
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   277
    /**
54731
81de17a33575 8219013: Update Apache Santuario (XML Signature) to version 2.1.3
weijun
parents: 53998
diff changeset
   278
     * Method getFullTextChildrenFromNode
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   279
     *
54731
81de17a33575 8219013: Update Apache Santuario (XML Signature) to version 2.1.3
weijun
parents: 53998
diff changeset
   280
     * @param node
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   281
     * @return the string of children
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   282
     */
54731
81de17a33575 8219013: Update Apache Santuario (XML Signature) to version 2.1.3
weijun
parents: 53998
diff changeset
   283
    public static String getFullTextChildrenFromNode(Node node) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   284
        StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
54731
81de17a33575 8219013: Update Apache Santuario (XML Signature) to version 2.1.3
weijun
parents: 53998
diff changeset
   286
        Node child = node.getFirstChild();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   287
        while (child != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   288
            if (child.getNodeType() == Node.TEXT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   289
                sb.append(((Text)child).getData());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   290
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   291
            child = child.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   292
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   294
        return sb.toString();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   295
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   297
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   298
     * Creates an Element in the XML Signature specification namespace.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   299
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   300
     * @param doc the factory Document
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   301
     * @param elementName the local name of the Element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   302
     * @return the Element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   303
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   304
    public static Element createElementInSignatureSpace(Document doc, String elementName) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   305
        if (doc == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   306
            throw new RuntimeException("Document is null");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   307
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   309
        if (dsPrefix == null || dsPrefix.length() == 0) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   310
            return doc.createElementNS(Constants.SignatureSpecNS, elementName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   311
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   312
        return doc.createElementNS(Constants.SignatureSpecNS, dsPrefix + ":" + elementName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   313
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   315
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   316
     * Creates an Element in the XML Signature 1.1 specification namespace.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   317
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   318
     * @param doc the factory Document
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   319
     * @param elementName the local name of the Element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   320
     * @return the Element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   321
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   322
    public static Element createElementInSignature11Space(Document doc, String elementName) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   323
        if (doc == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   324
            throw new RuntimeException("Document is null");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   325
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   327
        if (ds11Prefix == null || ds11Prefix.length() == 0) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   328
            return doc.createElementNS(Constants.SignatureSpec11NS, elementName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   329
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   330
        return doc.createElementNS(Constants.SignatureSpec11NS, ds11Prefix + ":" + elementName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   331
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   333
    /**
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   334
     * Returns true if the element is in XML Signature namespace and the local
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   335
     * name equals the supplied one.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   336
     *
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   337
     * @param element
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   338
     * @param localName
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   339
     * @return true if the element is in XML Signature namespace and the local name equals
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   340
     * the supplied one
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   341
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   342
    public static boolean elementIsInSignatureSpace(Element element, String localName) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   343
        if (element == null){
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   344
            return false;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   345
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   346
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   347
        return Constants.SignatureSpecNS.equals(element.getNamespaceURI())
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   348
            && element.getLocalName().equals(localName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   349
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   350
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   351
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   352
     * Returns true if the element is in XML Signature 1.1 namespace and the local
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   353
     * name equals the supplied one.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   354
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   355
     * @param element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   356
     * @param localName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   357
     * @return true if the element is in XML Signature namespace and the local name equals
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   358
     * the supplied one
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   359
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   360
    public static boolean elementIsInSignature11Space(Element element, String localName) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   361
        if (element == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   362
            return false;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   363
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   365
        return Constants.SignatureSpec11NS.equals(element.getNamespaceURI())
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   366
            && element.getLocalName().equals(localName);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   367
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   368
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   369
    /**
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   370
     * This method returns the owner document of a particular node.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   371
     * This method is necessary because it <I>always</I> returns a
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   372
     * {@link Document}. {@link Node#getOwnerDocument} returns {@code null}
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   373
     * if the {@link Node} is a {@link Document}.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   374
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   375
     * @param node
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   376
     * @return the owner document of the node
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   377
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   378
    public static Document getOwnerDocument(Node node) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   379
        if (node.getNodeType() == Node.DOCUMENT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   380
            return (Document) node;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   381
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   382
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            return node.getOwnerDocument();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   384
        } catch (NullPointerException npe) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            throw new NullPointerException(I18n.translate("endorsed.jdk1.4.0")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                           + " Original message was \""
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                                           + npe.getMessage() + "\"");
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   388
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   389
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   392
     * This method returns the first non-null owner document of the Nodes in this Set.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * This method is necessary because it <I>always</I> returns a
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   394
     * {@link Document}. {@link Node#getOwnerDocument} returns {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * if the {@link Node} is a {@link Document}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @param xpathNodeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @return the owner document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   400
    public static Document getOwnerDocument(Set<Node> xpathNodeSet) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   401
        NullPointerException npe = null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   402
        for (Node node : xpathNodeSet) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   403
            int nodeType = node.getNodeType();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   404
            if (nodeType == Node.DOCUMENT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   405
                return (Document) node;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   406
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   407
            try {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   408
                if (nodeType == Node.ATTRIBUTE_NODE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    return ((Attr)node).getOwnerElement().getOwnerDocument();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   410
                }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   411
                return node.getOwnerDocument();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   412
            } catch (NullPointerException e) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   413
                npe = e;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   414
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   415
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   417
        throw new NullPointerException(I18n.translate("endorsed.jdk1.4.0")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                                       + " Original message was \""
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                                       + (npe == null ? "" : npe.getMessage()) + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   422
    /**
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   423
     * Method createDSctx
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   424
     *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   425
     * @param doc
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   426
     * @param prefix
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   427
     * @param namespace
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   428
     * @return the element.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   429
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   430
    public static Element createDSctx(Document doc, String prefix, String namespace) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   431
        if (prefix == null || prefix.trim().length() == 0) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   432
            throw new IllegalArgumentException("You must supply a prefix");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   433
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   434
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   435
        Element ctx = doc.createElementNS(null, "namespaceContext");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   436
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   437
        ctx.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:" + prefix.trim(), namespace);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   438
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   439
        return ctx;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   440
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   442
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   443
     * Method addReturnToElement
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   444
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   445
     * @param e
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   446
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   447
    public static void addReturnToElement(Element e) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   448
        if (!ignoreLineBreaks) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   449
            Document doc = e.getOwnerDocument();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   450
            e.appendChild(doc.createTextNode("\n"));
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   451
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   452
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   454
    public static void addReturnToElement(Document doc, HelperNodeList nl) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   455
        if (!ignoreLineBreaks) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   456
            nl.appendChild(doc.createTextNode("\n"));
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   457
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   458
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   459
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   460
    public static void addReturnBeforeChild(Element e, Node child) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   461
        if (!ignoreLineBreaks) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   462
            Document doc = e.getOwnerDocument();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   463
            e.insertBefore(doc.createTextNode("\n"), child);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   464
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   465
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   467
    public static String encodeToString(byte[] bytes) {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   468
        if (ignoreLineBreaks) {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   469
            return Base64.getEncoder().encodeToString(bytes);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   470
        }
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   471
        return Base64.getMimeEncoder().encodeToString(bytes);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   472
    }
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   473
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   474
    public static byte[] decode(String encodedString) {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   475
        return Base64.getMimeDecoder().decode(encodedString);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   476
    }
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   477
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   478
    public static byte[] decode(byte[] encodedBytes) {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   479
        return Base64.getMimeDecoder().decode(encodedBytes);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   480
    }
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   481
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   482
    public static boolean isIgnoreLineBreaks() {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   483
        return ignoreLineBreaks;
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   484
    }
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   485
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   486
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   487
     * Method convertNodelistToSet
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   488
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   489
     * @param xpathNodeSet
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   490
     * @return the set with the nodelist
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   491
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   492
    public static Set<Node> convertNodelistToSet(NodeList xpathNodeSet) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   493
        if (xpathNodeSet == null) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   494
            return new HashSet<>();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   495
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   497
        int length = xpathNodeSet.getLength();
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   498
        Set<Node> set = new HashSet<>(length);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   499
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   500
        for (int i = 0; i < length; i++) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   501
            set.add(xpathNodeSet.item(i));
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   502
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   503
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   504
        return set;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   505
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   507
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   508
     * This method spreads all namespace attributes in a DOM document to their
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   509
     * children. This is needed because the XML Signature XPath transform
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   510
     * must evaluate the XPath against all nodes in the input, even against
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   511
     * XPath namespace nodes. Through a bug in XalanJ2, the namespace nodes are
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   512
     * not fully visible in the Xalan XPath model, so we have to do this by
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   513
     * hand in DOM spaces so that the nodes become visible in XPath space.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   514
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   515
     * @param doc
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   516
     * @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2650">
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   517
     * Namespace axis resolution is not XPath compliant </A>
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   518
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   519
    public static void circumventBug2650(Document doc) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   521
        Element documentElement = doc.getDocumentElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   523
        // if the document element has no xmlns definition, we add xmlns=""
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   524
        Attr xmlnsAttr =
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   525
            documentElement.getAttributeNodeNS(Constants.NamespaceSpecNS, "xmlns");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   527
        if (xmlnsAttr == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   528
            documentElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   529
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   531
        XMLUtils.circumventBug2650internal(doc);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   532
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   534
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   535
     * This is the work horse for {@link #circumventBug2650}.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   536
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   537
     * @param node
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   538
     * @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2650">
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   539
     * Namespace axis resolution is not XPath compliant </A>
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   540
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   541
    @SuppressWarnings("fallthrough")
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   542
    private static void circumventBug2650internal(Node node) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   543
        Node parent = null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   544
        Node sibling = null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   545
        final String namespaceNs = Constants.NamespaceSpecNS;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   546
        do {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   547
            switch (node.getNodeType()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   548
            case Node.ELEMENT_NODE :
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   549
                Element element = (Element) node;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   550
                if (!element.hasChildNodes()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   551
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   553
                if (element.hasAttributes()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   554
                    NamedNodeMap attributes = element.getAttributes();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   555
                    int attributesLength = attributes.getLength();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   556
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   557
                    for (Node child = element.getFirstChild(); child!=null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   558
                        child = child.getNextSibling()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   560
                        if (child.getNodeType() != Node.ELEMENT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   561
                            continue;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   562
                        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   563
                        Element childElement = (Element) child;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   564
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   565
                        for (int i = 0; i < attributesLength; i++) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   566
                            Attr currentAttr = (Attr) attributes.item(i);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   567
                            if (!namespaceNs.equals(currentAttr.getNamespaceURI())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                                continue;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   569
                            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   570
                            if (childElement.hasAttributeNS(namespaceNs,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   571
                                                            currentAttr.getLocalName())) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   572
                                continue;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   573
                            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   574
                            childElement.setAttributeNS(namespaceNs,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   575
                                                        currentAttr.getName(),
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   576
                                                        currentAttr.getNodeValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                        }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   578
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   580
            case Node.ENTITY_REFERENCE_NODE :
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   581
            case Node.DOCUMENT_NODE :
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   582
                parent = node;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   583
                sibling = node.getFirstChild();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   584
                break;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   585
            }
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   586
            while (sibling == null && parent != null) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   587
                sibling = parent.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   588
                parent = parent.getParentNode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   589
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   590
            if (sibling == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   591
                return;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   592
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   594
            node = sibling;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   595
            sibling = node.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   596
        } while (true);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   597
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   599
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   600
     * @param sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   601
     * @param nodeName
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   602
     * @param number
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   603
     * @return nodes with the constraint
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   604
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   605
    public static Element selectDsNode(Node sibling, String nodeName, int number) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   606
        while (sibling != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   607
            if (Constants.SignatureSpecNS.equals(sibling.getNamespaceURI())
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   608
                && sibling.getLocalName().equals(nodeName)) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   609
                if (number == 0){
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   610
                    return (Element)sibling;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   612
                number--;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   613
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   614
            sibling = sibling.getNextSibling();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        return null;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   617
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   619
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   620
     * @param sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   621
     * @param nodeName
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   622
     * @param number
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   623
     * @return nodes with the constraint
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   624
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   625
    public static Element selectDs11Node(Node sibling, String nodeName, int number) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   626
        while (sibling != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   627
            if (Constants.SignatureSpec11NS.equals(sibling.getNamespaceURI())
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   628
                && sibling.getLocalName().equals(nodeName)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   629
                if (number == 0){
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   630
                    return (Element)sibling;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   631
                }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   632
                number--;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   633
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   634
            sibling = sibling.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   635
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   636
        return null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   637
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   638
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   639
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   640
     * @param sibling
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   641
     * @param nodeName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   642
     * @param number
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   643
     * @return nodes with the constrain
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   644
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   645
    public static Text selectDsNodeText(Node sibling, String nodeName, int number) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   646
        Node n = selectDsNode(sibling, nodeName, number);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   647
        if (n == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   648
            return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   650
        n = n.getFirstChild();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   651
        while (n != null && n.getNodeType() != Node.TEXT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   652
            n = n.getNextSibling();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        return (Text)n;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   655
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   657
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   658
     * @param sibling
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   659
     * @param nodeName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   660
     * @param number
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   661
     * @return nodes with the constrain
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   662
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   663
    public static Text selectDs11NodeText(Node sibling, String nodeName, int number) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   664
        Node n = selectDs11Node(sibling, nodeName, number);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   665
        if (n == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   666
            return null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   667
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   668
        n = n.getFirstChild();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   669
        while (n != null && n.getNodeType() != Node.TEXT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   670
            n = n.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   671
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   672
        return (Text)n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   674
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   675
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   676
     * @param sibling
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   677
     * @param uri
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   678
     * @param nodeName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   679
     * @param number
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   680
     * @return nodes with the constrain
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   681
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   682
    public static Text selectNodeText(Node sibling, String uri, String nodeName, int number) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   683
        Node n = selectNode(sibling, uri, nodeName, number);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   684
        if (n == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   685
            return null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   686
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   687
        n = n.getFirstChild();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   688
        while (n != null && n.getNodeType() != Node.TEXT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   689
            n = n.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   690
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   691
        return (Text)n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   694
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   695
     * @param sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   696
     * @param uri
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   697
     * @param nodeName
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   698
     * @param number
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   699
     * @return nodes with the constrain
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   700
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   701
    public static Element selectNode(Node sibling, String uri, String nodeName, int number) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   702
        while (sibling != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   703
            if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri)
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   704
                && sibling.getLocalName().equals(nodeName)) {
54731
81de17a33575 8219013: Update Apache Santuario (XML Signature) to version 2.1.3
weijun
parents: 53998
diff changeset
   705
                if (number == 0) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   706
                    return (Element)sibling;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   708
                number--;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   709
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   710
            sibling = sibling.getNextSibling();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        return null;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   713
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   714
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   715
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   716
     * @param sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   717
     * @param nodeName
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   718
     * @return nodes with the constrain
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   719
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   720
    public static Element[] selectDsNodes(Node sibling, String nodeName) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   721
        return selectNodes(sibling, Constants.SignatureSpecNS, nodeName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   722
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   723
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   724
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   725
     * @param sibling
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   726
     * @param nodeName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   727
     * @return nodes with the constrain
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   728
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   729
    public static Element[] selectDs11Nodes(Node sibling, String nodeName) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   730
        return selectNodes(sibling, Constants.SignatureSpec11NS, nodeName);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   731
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   733
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   734
     * @param sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   735
     * @param uri
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   736
     * @param nodeName
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   737
     * @return nodes with the constraint
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   738
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   739
    public static Element[] selectNodes(Node sibling, String uri, String nodeName) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   740
        List<Element> list = new ArrayList<>();
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   741
        while (sibling != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   742
            if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri)
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   743
                && sibling.getLocalName().equals(nodeName)) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   744
                list.add((Element)sibling);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   745
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   746
            sibling = sibling.getNextSibling();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   748
        return list.toArray(new Element[list.size()]);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   749
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   751
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   752
     * @param signatureElement
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   753
     * @param inputSet
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   754
     * @return nodes with the constrain
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   755
     */
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   756
    public static Set<Node> excludeNodeFromSet(Node signatureElement, Set<Node> inputSet) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   757
        Set<Node> resultSet = new HashSet<>();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   758
        Iterator<Node> iterator = inputSet.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   760
        while (iterator.hasNext()) {
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   761
            Node inputNode = iterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   763
            if (!XMLUtils.isDescendantOrSelf(signatureElement, inputNode)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   764
                resultSet.add(inputNode);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   766
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   767
        return resultSet;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   768
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   770
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   771
     * Method getStrFromNode
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   772
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   773
     * @param xpathnode
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   774
     * @return the string for the node.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   775
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   776
    public static String getStrFromNode(Node xpathnode) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   777
        if (xpathnode.getNodeType() == Node.TEXT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   778
            // we iterate over all siblings of the context node because eventually,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   779
            // the text is "polluted" with pi's or comments
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   780
            StringBuilder sb = new StringBuilder();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   781
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   782
            for (Node currentSibling = xpathnode.getParentNode().getFirstChild();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   783
                currentSibling != null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   784
                currentSibling = currentSibling.getNextSibling()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   785
                if (currentSibling.getNodeType() == Node.TEXT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   786
                    sb.append(((Text) currentSibling).getData());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   787
                }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   788
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   789
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   790
            return sb.toString();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   791
        } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   792
            return xpathnode.getNodeValue();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   793
        } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   794
            return xpathnode.getNodeValue();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   795
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   797
        return null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   798
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   800
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   801
     * Returns true if the descendantOrSelf is on the descendant-or-self axis
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   802
     * of the context node.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   803
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   804
     * @param ctx
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   805
     * @param descendantOrSelf
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   806
     * @return true if the node is descendant
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   807
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   808
    public static boolean isDescendantOrSelf(Node ctx, Node descendantOrSelf) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   809
        if (ctx == descendantOrSelf) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   810
            return true;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   811
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   813
        Node parent = descendantOrSelf;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   814
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   815
        while (true) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   816
            if (parent == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   817
                return false;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   818
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   820
            if (parent == ctx) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   821
                return true;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   822
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   823
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   824
            if (parent.getNodeType() == Node.ATTRIBUTE_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   825
                parent = ((Attr) parent).getOwnerElement();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   826
            } else {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   827
                parent = parent.getParentNode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   828
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   829
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   830
    }
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   831
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   832
    public static boolean ignoreLineBreaks() {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   833
        return ignoreLineBreaks;
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   834
    }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   835
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   836
    /**
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   837
     * Returns the attribute value for the attribute with the specified name.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   838
     * Returns null if there is no such attribute, or
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   839
     * the empty string if the attribute value is empty.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   840
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   841
     * <p>This works around a limitation of the DOM
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   842
     * {@code Element.getAttributeNode} method, which does not distinguish
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   843
     * between an unspecified attribute and an attribute with a value of
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   844
     * "" (it returns "" for both cases).
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   845
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   846
     * @param elem the element containing the attribute
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   847
     * @param name the name of the attribute
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   848
     * @return the attribute value (may be null if unspecified)
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   849
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   850
    public static String getAttributeValue(Element elem, String name) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   851
        Attr attr = elem.getAttributeNodeNS(null, name);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   852
        return (attr == null) ? null : attr.getValue();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   853
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   854
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   855
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   856
     * This method is a tree-search to help prevent against wrapping attacks. It checks that no
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   857
     * two Elements have ID Attributes that match the "value" argument, if this is the case then
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   858
     * "false" is returned. Note that a return value of "true" does not necessarily mean that
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   859
     * a matching Element has been found, just that no wrapping attack has been detected.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   860
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   861
    public static boolean protectAgainstWrappingAttack(Node startNode, String value) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   862
        String id = value.trim();
23930
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18780
diff changeset
   863
        if (!id.isEmpty() && id.charAt(0) == '#') {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   864
            id = id.substring(1);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   865
        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   866
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   867
        Node startParent = null;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   868
        Node processedNode = null;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   869
        Element foundElement = null;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   870
        if (startNode != null) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   871
            startParent = startNode.getParentNode();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   872
        }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   873
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   874
        while (startNode != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   875
            if (startNode.getNodeType() == Node.ELEMENT_NODE) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   876
                Element se = (Element) startNode;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   877
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   878
                NamedNodeMap attributes = se.getAttributes();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   879
                if (attributes != null) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   880
                    int length = attributes.getLength();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   881
                    for (int i = 0; i < length; i++) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   882
                        Attr attr = (Attr)attributes.item(i);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   883
                        if (attr.isId() && id.equals(attr.getValue())) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   884
                            if (foundElement == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   885
                                // Continue searching to find duplicates
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   886
                                foundElement = attr.getOwnerElement();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   887
                            } else {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   888
                                LOG.debug("Multiple elements with the same 'Id' attribute value!");
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   889
                                return false;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   890
                            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   891
                        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   892
                    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   893
                }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   894
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   895
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   896
            processedNode = startNode;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   897
            startNode = startNode.getFirstChild();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   898
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   899
            // no child, this node is done.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   900
            if (startNode == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   901
                // close node processing, get sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   902
                startNode = processedNode.getNextSibling();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   903
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   904
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   905
            // no more siblings, get parent, all children
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   906
            // of parent are processed.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   907
            while (startNode == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   908
                processedNode = processedNode.getParentNode();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   909
                if (processedNode == startParent) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   910
                    return true;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   911
                }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   912
                // close parent node processing (processed node now)
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   913
                startNode = processedNode.getNextSibling();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   914
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   915
        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   916
        return true;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   917
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   918
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   919
    /**
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   920
     * This method is a tree-search to help prevent against wrapping attacks. It checks that no other
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   921
     * Element than the given "knownElement" argument has an ID attribute that matches the "value"
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   922
     * argument, which is the ID value of "knownElement". If this is the case then "false" is returned.
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   923
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   924
    public static boolean protectAgainstWrappingAttack(
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   925
        Node startNode, Element knownElement, String value
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   926
    ) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   927
        String id = value.trim();
23930
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18780
diff changeset
   928
        if (!id.isEmpty() && id.charAt(0) == '#') {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   929
            id = id.substring(1);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   930
        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   931
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   932
        Node startParent = null;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   933
        Node processedNode = null;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   934
        if (startNode != null) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   935
            startParent = startNode.getParentNode();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   936
        }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   937
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   938
        while (startNode != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   939
            if (startNode.getNodeType() == Node.ELEMENT_NODE) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   940
                Element se = (Element) startNode;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   941
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   942
                NamedNodeMap attributes = se.getAttributes();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   943
                if (attributes != null) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   944
                    int length = attributes.getLength();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   945
                    for (int i = 0; i < length; i++) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   946
                        Attr attr = (Attr)attributes.item(i);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   947
                        if (attr.isId() && id.equals(attr.getValue()) && se != knownElement) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   948
                            LOG.debug("Multiple elements with the same 'Id' attribute value!");
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   949
                            return false;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   950
                        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   951
                    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   952
                }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   953
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   954
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   955
            processedNode = startNode;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   956
            startNode = startNode.getFirstChild();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   957
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   958
            // no child, this node is done.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   959
            if (startNode == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   960
                // close node processing, get sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   961
                startNode = processedNode.getNextSibling();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   962
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   963
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   964
            // no more siblings, get parent, all children
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   965
            // of parent are processed.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   966
            while (startNode == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   967
                processedNode = processedNode.getParentNode();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   968
                if (processedNode == startParent) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   969
                    return true;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   970
                }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   971
                // close parent node processing (processed node now)
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   972
                startNode = processedNode.getNextSibling();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   973
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   974
        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   975
        return true;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   976
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   977
59240
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   978
    public static Document newDocument() throws ParserConfigurationException {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   979
        ClassLoader loader = getContextClassLoader();
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   980
        if (loader == null) {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   981
            loader = getClassLoader(XMLUtils.class);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   982
        }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   983
        // If the ClassLoader is null then just create a DocumentBuilder and use it
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   984
        if (loader == null) {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   985
            DocumentBuilder documentBuilder = buildDocumentBuilder(true);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   986
            return documentBuilder.newDocument();
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   987
        }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   988
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   989
        Queue<DocumentBuilder> queue = getDocumentBuilderQueue(true, loader);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   990
        DocumentBuilder documentBuilder = getDocumentBuilder(true, queue);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   991
        Document doc = documentBuilder.newDocument();
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   992
        repoolDocumentBuilder(documentBuilder, queue);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   993
        return doc;
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   994
    }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   995
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   996
    public static Document read(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   997
        return read(inputStream, true);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   998
    }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
   999
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1000
    public static Document read(InputStream inputStream, boolean disAllowDocTypeDeclarations) throws ParserConfigurationException, SAXException, IOException {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1001
        ClassLoader loader = getContextClassLoader();
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1002
        if (loader == null) {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1003
            loader = getClassLoader(XMLUtils.class);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1004
        }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1005
        // If the ClassLoader is null then just create a DocumentBuilder and use it
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1006
        if (loader == null) {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1007
            DocumentBuilder documentBuilder = buildDocumentBuilder(disAllowDocTypeDeclarations);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1008
            return documentBuilder.parse(inputStream);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1009
        }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1010
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1011
        Queue<DocumentBuilder> queue = getDocumentBuilderQueue(disAllowDocTypeDeclarations, loader);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1012
        DocumentBuilder documentBuilder = getDocumentBuilder(disAllowDocTypeDeclarations, queue);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1013
        Document doc = documentBuilder.parse(inputStream);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1014
        repoolDocumentBuilder(documentBuilder, queue);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1015
        return doc;
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1016
    }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1017
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1018
    public static Document read(String uri, boolean disAllowDocTypeDeclarations)
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1019
        throws ParserConfigurationException, SAXException, IOException {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1020
        ClassLoader loader = getContextClassLoader();
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1021
        if (loader == null) {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1022
            loader = getClassLoader(XMLUtils.class);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1023
        }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1024
        // If the ClassLoader is null then just create a DocumentBuilder and use it
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1025
        if (loader == null) {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1026
            DocumentBuilder documentBuilder = buildDocumentBuilder(disAllowDocTypeDeclarations);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1027
            return documentBuilder.parse(uri);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1028
        }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1029
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1030
        Queue<DocumentBuilder> queue = getDocumentBuilderQueue(disAllowDocTypeDeclarations, loader);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1031
        DocumentBuilder documentBuilder = getDocumentBuilder(disAllowDocTypeDeclarations, queue);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1032
        Document doc = documentBuilder.parse(uri);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1033
        repoolDocumentBuilder(documentBuilder, queue);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1034
        return doc;
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1035
    }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1036
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1037
    public static Document read(InputSource inputSource) throws ParserConfigurationException, SAXException, IOException {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1038
        return read(inputSource, true);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1039
    }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1040
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1041
    public static Document read(InputSource inputSource, boolean disAllowDocTypeDeclarations)
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1042
        throws ParserConfigurationException, SAXException, IOException {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1043
        ClassLoader loader = getContextClassLoader();
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1044
        if (loader == null) {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1045
            loader = getClassLoader(XMLUtils.class);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1046
        }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1047
        // If the ClassLoader is null then just create a DocumentBuilder and use it
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1048
        if (loader == null) {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1049
            DocumentBuilder documentBuilder = buildDocumentBuilder(disAllowDocTypeDeclarations);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1050
            return documentBuilder.parse(inputSource);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1051
        }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1052
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1053
        Queue<DocumentBuilder> queue = getDocumentBuilderQueue(disAllowDocTypeDeclarations, loader);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1054
        DocumentBuilder documentBuilder = getDocumentBuilder(disAllowDocTypeDeclarations, queue);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1055
        Document doc = documentBuilder.parse(inputSource);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1056
        repoolDocumentBuilder(documentBuilder, queue);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1057
        return doc;
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1058
    }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1059
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1060
    /**
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1061
     * @deprecated Use XMLUtils.read instead to directly read a document.
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1062
     */
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1063
    @Deprecated
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1064
    public static DocumentBuilder createDocumentBuilder(boolean validating) throws ParserConfigurationException {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1065
        return createDocumentBuilder(validating, true);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1066
    }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1067
59240
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1068
    /**
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1069
     * @deprecated Use XMLUtils.read instead to directly read a document.
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1070
     */
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1071
    @Deprecated
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1072
    public static DocumentBuilder createDocumentBuilder(
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1073
        boolean validating, boolean disAllowDocTypeDeclarations
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1074
    ) throws ParserConfigurationException {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1075
        DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
59240
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1076
        dfactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1077
        if (disAllowDocTypeDeclarations) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1078
            dfactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1079
        }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1080
        dfactory.setValidating(validating);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1081
        dfactory.setNamespaceAware(true);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1082
        return dfactory.newDocumentBuilder();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1083
    }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1084
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1085
    /**
59240
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1086
     * @deprecated This method has no effect in Santuario 2.1.4
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1087
     */
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1088
    @Deprecated
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1089
    public static boolean repoolDocumentBuilder(DocumentBuilder db) {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1090
        return true;
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1091
    }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1092
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1093
    /**
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1094
     * Returns a byte-array representation of a {@code {@link BigInteger}}.
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1095
     * No sign-bit is output.
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1096
     *
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1097
     * <b>N.B.:</B> {@code {@link BigInteger}}'s toByteArray
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1098
     * returns eventually longer arrays because of the leading sign-bit.
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1099
     *
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1100
     * @param big {@code BigInteger} to be converted
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1101
     * @param bitlen {@code int} the desired length in bits of the representation
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1102
     * @return a byte array with {@code bitlen} bits of {@code big}
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1103
     */
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1104
    public static byte[] getBytes(BigInteger big, int bitlen) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1105
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1106
        //round bitlen
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1107
        bitlen = ((bitlen + 7) >> 3) << 3;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1108
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1109
        if (bitlen < big.bitLength()) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1110
            throw new IllegalArgumentException(I18n.translate("utils.Base64.IllegalBitlength"));
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1111
        }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1112
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1113
        byte[] bigBytes = big.toByteArray();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1114
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1115
        if (big.bitLength() % 8 != 0
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1116
            && big.bitLength() / 8 + 1 == bitlen / 8) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1117
            return bigBytes;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1118
        }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1119
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1120
        // some copying needed
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1121
        int startSrc = 0;    // no need to skip anything
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1122
        int bigLen = bigBytes.length;    //valid length of the string
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1123
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1124
        if (big.bitLength() % 8 == 0) {    // correct values
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1125
            startSrc = 1;    // skip sign bit
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1126
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1127
            bigLen--;    // valid length of the string
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1128
        }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1129
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1130
        int startDst = bitlen / 8 - bigLen;    //pad with leading nulls
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1131
        byte[] resizedBytes = new byte[bitlen / 8];
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1132
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1133
        System.arraycopy(bigBytes, startSrc, resizedBytes, startDst, bigLen);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1134
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1135
        return resizedBytes;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1136
    }
59240
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1137
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1138
    private static Queue<DocumentBuilder> getDocumentBuilderQueue(boolean disAllowDocTypeDeclarations, ClassLoader loader) throws ParserConfigurationException {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1139
        Map<ClassLoader, Queue<DocumentBuilder>> docBuilderCache =
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1140
            disAllowDocTypeDeclarations ? DOCUMENT_BUILDERS_DISALLOW_DOCTYPE : DOCUMENT_BUILDERS;
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1141
        Queue<DocumentBuilder> queue = docBuilderCache.get(loader);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1142
        if (queue == null) {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1143
            queue = new ArrayBlockingQueue<>(parserPoolSize);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1144
            docBuilderCache.put(loader, queue);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1145
        }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1146
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1147
        return queue;
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1148
    }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1149
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1150
    private static DocumentBuilder getDocumentBuilder(boolean disAllowDocTypeDeclarations, Queue<DocumentBuilder> queue) throws ParserConfigurationException {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1151
        DocumentBuilder db = queue.poll();
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1152
        if (db == null) {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1153
            db = buildDocumentBuilder(disAllowDocTypeDeclarations);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1154
        }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1155
        return db;
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1156
    }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1157
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1158
    private static DocumentBuilder buildDocumentBuilder(boolean disAllowDocTypeDeclarations) throws ParserConfigurationException {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1159
        DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1160
        f.setNamespaceAware(true);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1161
        f.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1162
        f.setFeature("http://apache.org/xml/features/disallow-doctype-decl", disAllowDocTypeDeclarations);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1163
        return f.newDocumentBuilder();
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1164
    }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1165
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1166
    private static void repoolDocumentBuilder(DocumentBuilder db, Queue<DocumentBuilder> queue) {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1167
        if (queue != null) {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1168
            db.reset();
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1169
            queue.offer(db);
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1170
        }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1171
    }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1172
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1173
    private static ClassLoader getContextClassLoader() {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1174
        final SecurityManager sm = System.getSecurityManager();
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1175
        if (sm != null) {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1176
            return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1177
                public ClassLoader run() {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1178
                    return Thread.currentThread().getContextClassLoader();
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1179
                }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1180
            });
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1181
        }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1182
        return Thread.currentThread().getContextClassLoader();
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1183
    }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1184
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1185
    private static ClassLoader getClassLoader(final Class<?> clazz) {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1186
        final SecurityManager sm = System.getSecurityManager();
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1187
        if (sm != null) {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1188
            return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1189
                public ClassLoader run() {
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1190
                    return clazz.getClassLoader();
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1191
                }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1192
            });
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1193
        }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1194
        return clazz.getClassLoader();
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1195
    }
b3116877866f 8231507: Update Apache Santuario (XML Signature) to version 2.1.4
weijun
parents: 54731
diff changeset
  1196
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
}