src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/utils/XMLUtils.java
author weijun
Tue, 19 Jun 2018 08:06:35 +0800
changeset 50614 3810c9a2efa1
parent 47216 71c04702a3d5
child 53998 d870bb08194a
permissions -rw-r--r--
8177334: Update xmldsig implementation to Apache Santuario 2.1.1 Reviewed-by: mullan
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.io.OutputStream;
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    27
import java.math.BigInteger;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    28
import java.security.AccessController;
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    29
import java.security.PrivilegedAction;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    30
import java.util.ArrayList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Iterator;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    33
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    36
import javax.xml.parsers.DocumentBuilder;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    37
import javax.xml.parsers.DocumentBuilderFactory;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    38
import javax.xml.parsers.ParserConfigurationException;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    39
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import org.w3c.dom.Attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import org.w3c.dom.Document;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import org.w3c.dom.Element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import org.w3c.dom.NamedNodeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import org.w3c.dom.NodeList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import org.w3c.dom.Text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * DOM and XML accessibility and comfort functions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    55
public final class XMLUtils {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    57
    private static boolean ignoreLineBreaks =
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    58
        AccessController.doPrivileged(
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    59
            (PrivilegedAction<Boolean>) () -> Boolean.getBoolean("com.sun.org.apache.xml.internal.security.ignoreLineBreaks"));
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    60
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    61
    private static volatile String dsPrefix = "ds";
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    62
    private static volatile String ds11Prefix = "dsig11";
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    63
    private static volatile String xencPrefix = "xenc";
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    64
    private static volatile String xenc11Prefix = "xenc11";
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    65
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    66
    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
    67
        com.sun.org.slf4j.internal.LoggerFactory.getLogger(XMLUtils.class);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    68
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    70
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    71
     * Constructor XMLUtils
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    72
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    73
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    74
    private XMLUtils() {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    75
        // we don't allow instantiation
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    76
    }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    77
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    78
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    79
     * Set the prefix for the digital signature namespace
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    80
     * @param prefix the new prefix for the digital signature namespace
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    81
     * @throws SecurityException if a security manager is installed and the
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    82
     *    caller does not have permission to set the prefix
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    83
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    84
    public static void setDsPrefix(String prefix) {
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    85
        JavaUtils.checkRegisterPermission();
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    86
        dsPrefix = prefix;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    87
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    88
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    89
    /**
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    90
     * 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
    91
     * @param prefix the new prefix for the digital signature 1.1 namespace
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    92
     * @throws SecurityException if a security manager is installed and the
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    93
     *    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
    94
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    95
    public static void setDs11Prefix(String prefix) {
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    96
        JavaUtils.checkRegisterPermission();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    97
        ds11Prefix = prefix;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    98
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    99
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   100
    /**
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   101
     * Set the prefix for the encryption namespace
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   102
     * @param prefix the new prefix for the encryption namespace
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   103
     * @throws SecurityException if a security manager is installed and the
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   104
     *    caller does not have permission to set the prefix
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   105
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   106
    public static void setXencPrefix(String prefix) {
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   107
        JavaUtils.checkRegisterPermission();
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   108
        xencPrefix = prefix;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   109
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   110
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
     * 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
   113
     * @param prefix the new prefix for the encryption namespace 1.1
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   114
     * @throws SecurityException if a security manager is installed and the
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   115
     *    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
   116
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   117
    public static void setXenc11Prefix(String prefix) {
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   118
        JavaUtils.checkRegisterPermission();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   119
        xenc11Prefix = prefix;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   120
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   122
    public static Element getNextElement(Node el) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   123
        Node node = el;
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   124
        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
   125
            node = node.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   126
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   127
        return (Element)node;
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
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   130
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   131
     * @param rootNode
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   132
     * @param result
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   133
     * @param exclude
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   134
     * @param com whether comments or not
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   135
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   136
    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
   137
        if (exclude != null && isDescendantOrSelf(exclude, rootNode)) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   138
            return;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   139
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   140
        getSetRec(rootNode, result, exclude, com);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   141
    }
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   142
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   143
    @SuppressWarnings("fallthrough")
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   144
    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
   145
                                final Node exclude, final boolean com) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   146
        if (rootNode == exclude) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   147
            return;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   148
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   149
        switch (rootNode.getNodeType()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   150
        case Node.ELEMENT_NODE:
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   151
            result.add(rootNode);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   152
            Element el = (Element)rootNode;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   153
            if (el.hasAttributes()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   154
                NamedNodeMap nl = el.getAttributes();
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   155
                int length = nl.getLength();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   156
                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
   157
                    result.add(nl.item(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   159
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   160
            //no return keep working
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   161
        case Node.DOCUMENT_NODE:
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   162
            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
   163
                if (r.getNodeType() == Node.TEXT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   164
                    result.add(r);
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   165
                    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
   166
                        r = r.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   167
                    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   168
                    if (r == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   169
                        return;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   170
                    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   171
                }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   172
                getSetRec(r, result, exclude, com);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   173
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   174
            return;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   175
        case Node.COMMENT_NODE:
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   176
            if (com) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   177
                result.add(rootNode);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   178
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   179
            return;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   180
        case Node.DOCUMENT_TYPE_NODE:
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   181
            return;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   182
        default:
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   183
            result.add(rootNode);
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
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
18780
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
     * 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
   190
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   191
     * @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
   192
     * @param os the {@link OutputStream}
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   193
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   194
    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
   195
        XMLUtils.outputDOM(contextNode, os, false);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   196
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   198
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   199
     * 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
   200
     * 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
   201
     * Exception is not re-thrown.</I>
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   202
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   203
     * @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
   204
     * @param os the {@link OutputStream}
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   205
     * @param addPreamble
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
    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
   208
        try {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   209
            if (addPreamble) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   210
                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
   211
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   213
            os.write(Canonicalizer.getInstance(
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   214
                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
   215
            );
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   216
        } catch (IOException ex) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   217
            LOG.debug(ex.getMessage(), ex);
18780
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
        catch (InvalidCanonicalizerException ex) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   220
            LOG.debug(ex.getMessage(), ex);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   221
        } catch (CanonicalizationException ex) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   222
            LOG.debug(ex.getMessage(), ex);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   223
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   224
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   226
    /**
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   227
     * 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
   228
     * suppresses all Exceptions</I>.
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   229
     * <p></p>
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   230
     * 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
   231
     * 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
   232
     * 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
   233
     * 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
   234
     * {@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
   235
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   236
     * @param contextNode
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   237
     * @param os
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   238
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   239
    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
   240
        try {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   241
            os.write(Canonicalizer.getInstance(
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   242
                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
   243
            );
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   244
        } catch (IOException ex) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   245
            LOG.debug(ex.getMessage(), ex);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   246
            // throw new RuntimeException(ex.getMessage());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   247
        } catch (InvalidCanonicalizerException ex) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   248
            LOG.debug(ex.getMessage(), ex);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   249
            // throw new RuntimeException(ex.getMessage());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   250
        } catch (CanonicalizationException ex) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   251
            LOG.debug(ex.getMessage(), ex);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   252
            // throw new RuntimeException(ex.getMessage());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   253
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   254
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   256
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   257
     * Method getFullTextChildrenFromElement
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   258
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   259
     * @param element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   260
     * @return the string of children
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   261
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   262
    public static String getFullTextChildrenFromElement(Element element) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   263
        StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   265
        Node child = element.getFirstChild();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   266
        while (child != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   267
            if (child.getNodeType() == Node.TEXT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   268
                sb.append(((Text)child).getData());
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
            child = child.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   271
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   273
        return sb.toString();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   274
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   276
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   277
     * 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
   278
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   279
     * @param doc the factory Document
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   280
     * @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
   281
     * @return the Element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   282
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   283
    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
   284
        if (doc == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   285
            throw new RuntimeException("Document is null");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   286
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   288
        if (dsPrefix == null || dsPrefix.length() == 0) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   289
            return doc.createElementNS(Constants.SignatureSpecNS, elementName);
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
        return doc.createElementNS(Constants.SignatureSpecNS, dsPrefix + ":" + elementName);
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
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   295
     * 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
   296
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   297
     * @param doc the factory Document
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   298
     * @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
   299
     * @return the Element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   300
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   301
    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
   302
        if (doc == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   303
            throw new RuntimeException("Document is null");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   304
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   306
        if (ds11Prefix == null || ds11Prefix.length() == 0) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   307
            return doc.createElementNS(Constants.SignatureSpec11NS, elementName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   308
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   309
        return doc.createElementNS(Constants.SignatureSpec11NS, ds11Prefix + ":" + elementName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   310
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   312
    /**
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   313
     * Returns true if the element is in XML Signature namespace and the local
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   314
     * name equals the supplied one.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   315
     *
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   316
     * @param element
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   317
     * @param localName
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   318
     * @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
   319
     * the supplied one
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   320
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   321
    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
   322
        if (element == null){
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   323
            return false;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   324
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   325
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   326
        return Constants.SignatureSpecNS.equals(element.getNamespaceURI())
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   327
            && element.getLocalName().equals(localName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   328
    }
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
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   331
     * 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
   332
     * name equals the supplied one.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   333
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   334
     * @param element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   335
     * @param localName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   336
     * @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
   337
     * the supplied one
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   338
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   339
    public static boolean elementIsInSignature11Space(Element element, String localName) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   340
        if (element == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   341
            return false;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   342
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   344
        return Constants.SignatureSpec11NS.equals(element.getNamespaceURI())
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   345
            && element.getLocalName().equals(localName);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   346
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   347
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   348
    /**
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   349
     * 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
   350
     * 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
   351
     * {@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
   352
     * 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
   353
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   354
     * @param node
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   355
     * @return the owner document of the node
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   356
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   357
    public static Document getOwnerDocument(Node node) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   358
        if (node.getNodeType() == Node.DOCUMENT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   359
            return (Document) node;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   360
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   361
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            return node.getOwnerDocument();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   363
        } catch (NullPointerException npe) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            throw new NullPointerException(I18n.translate("endorsed.jdk1.4.0")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                                           + " Original message was \""
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                                           + npe.getMessage() + "\"");
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   367
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   368
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   371
     * This method returns the first non-null owner document of the Nodes in this Set.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * 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
   373
     * {@link Document}. {@link Node#getOwnerDocument} returns {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * if the {@link Node} is a {@link Document}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @param xpathNodeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @return the owner document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   379
    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
   380
        NullPointerException npe = null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   381
        for (Node node : xpathNodeSet) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   382
            int nodeType = node.getNodeType();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   383
            if (nodeType == Node.DOCUMENT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   384
                return (Document) node;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   385
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   386
            try {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   387
                if (nodeType == Node.ATTRIBUTE_NODE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    return ((Attr)node).getOwnerElement().getOwnerDocument();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   389
                }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   390
                return node.getOwnerDocument();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   391
            } catch (NullPointerException e) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   392
                npe = e;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   393
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   394
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   396
        throw new NullPointerException(I18n.translate("endorsed.jdk1.4.0")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                                       + " Original message was \""
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                                       + (npe == null ? "" : npe.getMessage()) + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   401
    /**
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   402
     * Method createDSctx
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   403
     *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   404
     * @param doc
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   405
     * @param prefix
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   406
     * @param namespace
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   407
     * @return the element.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   408
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   409
    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
   410
        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
   411
            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
   412
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   413
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   414
        Element ctx = doc.createElementNS(null, "namespaceContext");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   415
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   416
        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
   417
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   418
        return ctx;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   419
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   421
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   422
     * Method addReturnToElement
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   423
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   424
     * @param e
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   425
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   426
    public static void addReturnToElement(Element e) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   427
        if (!ignoreLineBreaks) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   428
            Document doc = e.getOwnerDocument();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   429
            e.appendChild(doc.createTextNode("\n"));
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   430
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   431
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   433
    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
   434
        if (!ignoreLineBreaks) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   435
            nl.appendChild(doc.createTextNode("\n"));
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
    }
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
    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
   440
        if (!ignoreLineBreaks) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   441
            Document doc = e.getOwnerDocument();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   442
            e.insertBefore(doc.createTextNode("\n"), child);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   443
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   444
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
18780
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
     * Method convertNodelistToSet
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   448
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   449
     * @param xpathNodeSet
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   450
     * @return the set with the nodelist
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
    public static Set<Node> convertNodelistToSet(NodeList xpathNodeSet) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   453
        if (xpathNodeSet == null) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   454
            return new HashSet<>();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   455
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   457
        int length = xpathNodeSet.getLength();
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   458
        Set<Node> set = new HashSet<>(length);
18780
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
        for (int i = 0; i < length; i++) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   461
            set.add(xpathNodeSet.item(i));
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   462
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   463
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   464
        return set;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   465
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   467
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   468
     * 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
   469
     * 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
   470
     * 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
   471
     * 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
   472
     * 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
   473
     * 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
   474
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   475
     * @param doc
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   476
     * @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
   477
     * 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
   478
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   479
    public static void circumventBug2650(Document doc) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   481
        Element documentElement = doc.getDocumentElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   483
        // 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
   484
        Attr xmlnsAttr =
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   485
            documentElement.getAttributeNodeNS(Constants.NamespaceSpecNS, "xmlns");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   487
        if (xmlnsAttr == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   488
            documentElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   489
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   491
        XMLUtils.circumventBug2650internal(doc);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   492
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   494
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   495
     * 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
   496
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   497
     * @param node
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   498
     * @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
   499
     * 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
   500
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   501
    @SuppressWarnings("fallthrough")
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   502
    private static void circumventBug2650internal(Node node) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   503
        Node parent = null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   504
        Node sibling = null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   505
        final String namespaceNs = Constants.NamespaceSpecNS;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   506
        do {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   507
            switch (node.getNodeType()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   508
            case Node.ELEMENT_NODE :
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   509
                Element element = (Element) node;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   510
                if (!element.hasChildNodes()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   511
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   513
                if (element.hasAttributes()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   514
                    NamedNodeMap attributes = element.getAttributes();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   515
                    int attributesLength = attributes.getLength();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   516
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   517
                    for (Node child = element.getFirstChild(); child!=null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   518
                        child = child.getNextSibling()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   520
                        if (child.getNodeType() != Node.ELEMENT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   521
                            continue;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   522
                        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   523
                        Element childElement = (Element) child;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   524
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   525
                        for (int i = 0; i < attributesLength; i++) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   526
                            Attr currentAttr = (Attr) attributes.item(i);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   527
                            if (!namespaceNs.equals(currentAttr.getNamespaceURI())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                                continue;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   529
                            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   530
                            if (childElement.hasAttributeNS(namespaceNs,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   531
                                                            currentAttr.getLocalName())) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   532
                                continue;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   533
                            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   534
                            childElement.setAttributeNS(namespaceNs,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   535
                                                        currentAttr.getName(),
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   536
                                                        currentAttr.getNodeValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                        }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   538
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   540
            case Node.ENTITY_REFERENCE_NODE :
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   541
            case Node.DOCUMENT_NODE :
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   542
                parent = node;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   543
                sibling = node.getFirstChild();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   544
                break;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   545
            }
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   546
            while (sibling == null && parent != null) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   547
                sibling = parent.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   548
                parent = parent.getParentNode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   549
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   550
            if (sibling == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   551
                return;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   552
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   554
            node = sibling;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   555
            sibling = node.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   556
        } while (true);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   557
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   559
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   560
     * @param sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   561
     * @param nodeName
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   562
     * @param number
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   563
     * @return nodes with the constraint
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   564
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   565
    public static Element selectDsNode(Node sibling, String nodeName, int number) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   566
        while (sibling != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   567
            if (Constants.SignatureSpecNS.equals(sibling.getNamespaceURI())
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   568
                && sibling.getLocalName().equals(nodeName)) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   569
                if (number == 0){
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   570
                    return (Element)sibling;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   572
                number--;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   573
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   574
            sibling = sibling.getNextSibling();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        return null;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   577
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   579
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   580
     * @param sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   581
     * @param nodeName
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   582
     * @param number
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   583
     * @return nodes with the constraint
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   584
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   585
    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
   586
        while (sibling != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   587
            if (Constants.SignatureSpec11NS.equals(sibling.getNamespaceURI())
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   588
                && sibling.getLocalName().equals(nodeName)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   589
                if (number == 0){
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   590
                    return (Element)sibling;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   591
                }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   592
                number--;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   593
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   594
            sibling = sibling.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   595
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   596
        return null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   597
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   598
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   599
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   600
     * @param sibling
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   601
     * @param nodeName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   602
     * @param number
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   603
     * @return nodes with the constrain
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   604
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   605
    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
   606
        Node n = selectDsNode(sibling, nodeName, number);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   607
        if (n == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   608
            return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   610
        n = n.getFirstChild();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   611
        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
   612
            n = n.getNextSibling();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        return (Text)n;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   615
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   617
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   618
     * @param sibling
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   619
     * @param nodeName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   620
     * @param number
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   621
     * @return nodes with the constrain
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   622
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   623
    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
   624
        Node n = selectDs11Node(sibling, nodeName, number);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   625
        if (n == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   626
            return null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   627
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   628
        n = n.getFirstChild();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   629
        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
   630
            n = n.getNextSibling();
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
        return (Text)n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   634
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
     * @param sibling
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   637
     * @param uri
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   638
     * @param nodeName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   639
     * @param number
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   640
     * @return nodes with the constrain
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   641
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   642
    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
   643
        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
   644
        if (n == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   645
            return null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   646
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   647
        n = n.getFirstChild();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   648
        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
   649
            n = n.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   650
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   651
        return (Text)n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   654
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   655
     * @param sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   656
     * @param uri
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   657
     * @param nodeName
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   658
     * @param number
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   659
     * @return nodes with the constrain
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   660
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   661
    public static Element selectNode(Node sibling, String uri, String nodeName, int number) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   662
        while (sibling != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   663
            if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri)
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   664
                && sibling.getLocalName().equals(nodeName)) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   665
                if (number == 0){
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   666
                    return (Element)sibling;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   668
                number--;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   669
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   670
            sibling = sibling.getNextSibling();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        return null;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   673
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   674
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   675
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   676
     * @param sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   677
     * @param nodeName
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   678
     * @return nodes with the constrain
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   679
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   680
    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
   681
        return selectNodes(sibling, Constants.SignatureSpecNS, nodeName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   682
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   683
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   684
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   685
     * @param sibling
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   686
     * @param nodeName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   687
     * @return nodes with the constrain
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   688
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   689
    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
   690
        return selectNodes(sibling, Constants.SignatureSpec11NS, nodeName);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   691
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   693
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   694
     * @param sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   695
     * @param uri
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   696
     * @param nodeName
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   697
     * @return nodes with the constraint
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   698
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   699
    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
   700
        List<Element> list = new ArrayList<>();
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   701
        while (sibling != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   702
            if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri)
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   703
                && sibling.getLocalName().equals(nodeName)) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   704
                list.add((Element)sibling);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   705
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   706
            sibling = sibling.getNextSibling();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   708
        return list.toArray(new Element[list.size()]);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   709
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   711
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   712
     * @param signatureElement
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   713
     * @param inputSet
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   714
     * @return nodes with the constrain
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   715
     */
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   716
    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
   717
        Set<Node> resultSet = new HashSet<>();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   718
        Iterator<Node> iterator = inputSet.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   720
        while (iterator.hasNext()) {
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   721
            Node inputNode = iterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   723
            if (!XMLUtils.isDescendantOrSelf(signatureElement, inputNode)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   724
                resultSet.add(inputNode);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   726
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   727
        return resultSet;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   728
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   730
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   731
     * Method getStrFromNode
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   732
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   733
     * @param xpathnode
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   734
     * @return the string for the node.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   735
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   736
    public static String getStrFromNode(Node xpathnode) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   737
        if (xpathnode.getNodeType() == Node.TEXT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   738
            // 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
   739
            // 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
   740
            StringBuilder sb = new StringBuilder();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   741
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   742
            for (Node currentSibling = xpathnode.getParentNode().getFirstChild();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   743
                currentSibling != null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   744
                currentSibling = currentSibling.getNextSibling()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   745
                if (currentSibling.getNodeType() == Node.TEXT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   746
                    sb.append(((Text) currentSibling).getData());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   747
                }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   748
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   749
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   750
            return sb.toString();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   751
        } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   752
            return xpathnode.getNodeValue();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   753
        } 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
   754
            return xpathnode.getNodeValue();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   755
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   757
        return null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   758
    }
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
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   761
     * 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
   762
     * of the context node.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   763
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   764
     * @param ctx
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   765
     * @param descendantOrSelf
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   766
     * @return true if the node is descendant
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   767
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   768
    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
   769
        if (ctx == descendantOrSelf) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   770
            return true;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   771
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   773
        Node parent = descendantOrSelf;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   774
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   775
        while (true) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   776
            if (parent == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   777
                return false;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   778
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   780
            if (parent == ctx) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   781
                return true;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   782
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   783
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   784
            if (parent.getNodeType() == Node.ATTRIBUTE_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   785
                parent = ((Attr) parent).getOwnerElement();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   786
            } else {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   787
                parent = parent.getParentNode();
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
    }
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   791
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   792
    public static boolean ignoreLineBreaks() {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   793
        return ignoreLineBreaks;
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   794
    }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   795
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   796
    /**
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   797
     * 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
   798
     * 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
   799
     * 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
   800
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   801
     * <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
   802
     * {@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
   803
     * 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
   804
     * "" (it returns "" for both cases).
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   805
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   806
     * @param elem the element containing the attribute
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   807
     * @param name the name of the attribute
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   808
     * @return the attribute value (may be null if unspecified)
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   809
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   810
    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
   811
        Attr attr = elem.getAttributeNodeNS(null, name);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   812
        return (attr == null) ? null : attr.getValue();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   813
    }
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
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   816
     * 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
   817
     * 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
   818
     * "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
   819
     * 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
   820
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   821
    public static boolean protectAgainstWrappingAttack(Node startNode, String value) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   822
        String id = value.trim();
23930
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18780
diff changeset
   823
        if (!id.isEmpty() && id.charAt(0) == '#') {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   824
            id = id.substring(1);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   825
        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   826
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   827
        Node startParent = null;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   828
        Node processedNode = null;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   829
        Element foundElement = null;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   830
        if (startNode != null) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   831
            startParent = startNode.getParentNode();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   832
        }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   833
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   834
        while (startNode != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   835
            if (startNode.getNodeType() == Node.ELEMENT_NODE) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   836
                Element se = (Element) startNode;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   837
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   838
                NamedNodeMap attributes = se.getAttributes();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   839
                if (attributes != null) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   840
                    int length = attributes.getLength();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   841
                    for (int i = 0; i < length; i++) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   842
                        Attr attr = (Attr)attributes.item(i);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   843
                        if (attr.isId() && id.equals(attr.getValue())) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   844
                            if (foundElement == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   845
                                // Continue searching to find duplicates
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   846
                                foundElement = attr.getOwnerElement();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   847
                            } else {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   848
                                LOG.debug("Multiple elements with the same 'Id' attribute value!");
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   849
                                return false;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   850
                            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   851
                        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   852
                    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   853
                }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   854
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   855
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   856
            processedNode = startNode;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   857
            startNode = startNode.getFirstChild();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   858
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   859
            // no child, this node is done.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   860
            if (startNode == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   861
                // close node processing, get sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   862
                startNode = processedNode.getNextSibling();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   863
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   864
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   865
            // no more siblings, get parent, all children
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   866
            // of parent are processed.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   867
            while (startNode == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   868
                processedNode = processedNode.getParentNode();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   869
                if (processedNode == startParent) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   870
                    return true;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   871
                }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   872
                // close parent node processing (processed node now)
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   873
                startNode = processedNode.getNextSibling();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   874
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   875
        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   876
        return true;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   877
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   878
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   879
    /**
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   880
     * 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
   881
     * 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
   882
     * 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
   883
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   884
    public static boolean protectAgainstWrappingAttack(
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   885
        Node startNode, Element knownElement, String value
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   886
    ) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   887
        String id = value.trim();
23930
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18780
diff changeset
   888
        if (!id.isEmpty() && id.charAt(0) == '#') {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   889
            id = id.substring(1);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   890
        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   891
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   892
        Node startParent = null;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   893
        Node processedNode = null;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   894
        if (startNode != null) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   895
            startParent = startNode.getParentNode();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   896
        }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   897
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   898
        while (startNode != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   899
            if (startNode.getNodeType() == Node.ELEMENT_NODE) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   900
                Element se = (Element) startNode;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   901
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   902
                NamedNodeMap attributes = se.getAttributes();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   903
                if (attributes != null) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   904
                    int length = attributes.getLength();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   905
                    for (int i = 0; i < length; i++) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   906
                        Attr attr = (Attr)attributes.item(i);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   907
                        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
   908
                            LOG.debug("Multiple elements with the same 'Id' attribute value!");
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   909
                            return false;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   910
                        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   911
                    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   912
                }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   913
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   914
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   915
            processedNode = startNode;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   916
            startNode = startNode.getFirstChild();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   917
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   918
            // no child, this node is done.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   919
            if (startNode == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   920
                // close node processing, get sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   921
                startNode = processedNode.getNextSibling();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   922
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   923
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   924
            // no more siblings, get parent, all children
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   925
            // of parent are processed.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   926
            while (startNode == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   927
                processedNode = processedNode.getParentNode();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   928
                if (processedNode == startParent) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   929
                    return true;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   930
                }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   931
                // close parent node processing (processed node now)
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   932
                startNode = processedNode.getNextSibling();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   933
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   934
        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   935
        return true;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   936
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   937
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   938
    public static DocumentBuilder createDocumentBuilder(boolean validating)
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   939
            throws ParserConfigurationException {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   940
        return createDocumentBuilder(validating, true);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   941
    }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   942
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   943
    // The current implementation does not throw a ParserConfigurationException.
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   944
    // Kept here in case we create the DocumentBuilder inline again.
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   945
    public static DocumentBuilder createDocumentBuilder(
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   946
        boolean validating, boolean disAllowDocTypeDeclarations
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   947
    ) throws ParserConfigurationException {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   948
        DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   949
        dfactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   950
        if (disAllowDocTypeDeclarations) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   951
            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
   952
        }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   953
        dfactory.setValidating(validating);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   954
        dfactory.setNamespaceAware(true);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   955
        return dfactory.newDocumentBuilder();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   956
    }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   957
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   958
    /**
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   959
     * 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
   960
     * No sign-bit is output.
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   961
     *
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   962
     * <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
   963
     * 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
   964
     *
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   965
     * @param big {@code BigInteger} to be converted
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   966
     * @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
   967
     * @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
   968
     */
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   969
    public static byte[] getBytes(BigInteger big, int bitlen) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   970
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   971
        //round bitlen
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   972
        bitlen = ((bitlen + 7) >> 3) << 3;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   973
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   974
        if (bitlen < big.bitLength()) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   975
            throw new IllegalArgumentException(I18n.translate("utils.Base64.IllegalBitlength"));
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   976
        }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   977
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   978
        byte[] bigBytes = big.toByteArray();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   979
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   980
        if (big.bitLength() % 8 != 0
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   981
            && big.bitLength() / 8 + 1 == bitlen / 8) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   982
            return bigBytes;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   983
        }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   984
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   985
        // some copying needed
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   986
        int startSrc = 0;    // no need to skip anything
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   987
        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
   988
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   989
        if (big.bitLength() % 8 == 0) {    // correct values
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   990
            startSrc = 1;    // skip sign bit
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   991
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   992
            bigLen--;    // valid length of the string
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   993
        }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   994
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   995
        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
   996
        byte[] resizedBytes = new byte[bitlen / 8];
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   997
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   998
        System.arraycopy(bigBytes, startSrc, resizedBytes, startDst, bigLen);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   999
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1000
        return resizedBytes;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
  1001
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
}