src/java.xml.crypto/share/classes/org/jcp/xml/dsig/internal/dom/DOMKeyInfo.java
author jlahoda
Tue, 24 Sep 2019 15:40:26 +0200
branchJDK-8226585-branch
changeset 58290 d885633d9de4
parent 54731 81de17a33575
permissions -rw-r--r--
Converting the test to a combo-framework test.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     2
 * reserved comment block
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 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
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    13
 *
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    14
 * http://www.apache.org/licenses/LICENSE-2.0
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    15
 *
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 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.
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    22
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    23
/*
54731
81de17a33575 8219013: Update Apache Santuario (XML Signature) to version 2.1.3
weijun
parents: 53998
diff changeset
    24
 * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    25
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    26
/*
54731
81de17a33575 8219013: Update Apache Santuario (XML Signature) to version 2.1.3
weijun
parents: 53998
diff changeset
    27
 * $Id: DOMKeyInfo.java 1854026 2019-02-21 09:30:01Z coheigea $
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
package org.jcp.xml.dsig.internal.dom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    31
import java.security.Provider;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    32
import java.util.ArrayList;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    33
import java.util.Collections;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    34
import java.util.List;
2
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.crypto.MarshalException;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    37
import javax.xml.crypto.XMLCryptoContext;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    38
import javax.xml.crypto.XMLStructure;
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
    39
import javax.xml.crypto.dom.DOMCryptoContext;
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    40
import javax.xml.crypto.dsig.XMLSignature;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    41
import javax.xml.crypto.dsig.dom.DOMSignContext;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    42
import javax.xml.crypto.dsig.keyinfo.KeyInfo;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    43
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
    44
import org.w3c.dom.Attr;
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
    45
import org.w3c.dom.Document;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import org.w3c.dom.Element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * DOM-based implementation of KeyInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
    53
public final class DOMKeyInfo extends DOMStructure implements KeyInfo {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private final String id;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    56
    private final List<XMLStructure> keyInfoTypes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    59
     * A utility function to suppress casting warnings.
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    60
     * @param ki
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    61
     * @return the content of a KeyInfo Object
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    62
     */
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    63
    @SuppressWarnings("unchecked")
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    64
    public static List<XMLStructure> getContent(KeyInfo ki) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    65
        return ki.getContent();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    66
    }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    67
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    68
    /**
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    69
     * Creates a {@code DOMKeyInfo}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param content a list of one or more {@link XMLStructure}s representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *    key information types. The list is defensively copied to protect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *    against subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param id an ID attribute
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    75
     * @throws NullPointerException if {@code content} is {@code null}
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    76
     * @throws IllegalArgumentException if {@code content} is empty
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    77
     * @throws ClassCastException if {@code content} contains any entries
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *    that are not of type {@link XMLStructure}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    80
    public DOMKeyInfo(List<? extends XMLStructure> content, String id) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (content == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throw new NullPointerException("content cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    84
        this.keyInfoTypes =
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    85
            Collections.unmodifiableList(new ArrayList<>(content));
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    86
        if (this.keyInfoTypes.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new IllegalArgumentException("content cannot be empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    89
        for (int i = 0, size = this.keyInfoTypes.size(); i < size; i++) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    90
            if (!(this.keyInfoTypes.get(i) instanceof XMLStructure)) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    91
                throw new ClassCastException
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    92
                    ("content["+i+"] is not a valid KeyInfo type");
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    93
            }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    94
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    99
     * Creates a {@code DOMKeyInfo} from XML.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   101
     * @param kiElem KeyInfo element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   103
    public DOMKeyInfo(Element kiElem, XMLCryptoContext context,
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   104
                      Provider provider)
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   105
        throws MarshalException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   106
    {
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   107
        // get Id attribute, if specified
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   108
        Attr attr = kiElem.getAttributeNodeNS(null, "Id");
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   109
        if (attr != null) {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   110
            id = attr.getValue();
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   111
            kiElem.setIdAttributeNode(attr, true);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   112
        } else {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   113
            id = null;
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   114
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // get all children nodes
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   117
        List<XMLStructure> content = new ArrayList<>();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   118
        Node firstChild = kiElem.getFirstChild();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   119
        if (firstChild == null) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   120
            throw new MarshalException("KeyInfo must contain at least one type");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   122
        while (firstChild != null) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   123
            if (firstChild.getNodeType() == Node.ELEMENT_NODE) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   124
                Element childElem = (Element)firstChild;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   125
                String localName = childElem.getLocalName();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   126
                String namespace = childElem.getNamespaceURI();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   127
                if ("X509Data".equals(localName) && XMLSignature.XMLNS.equals(namespace)) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   128
                    content.add(new DOMX509Data(childElem));
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   129
                } else if ("KeyName".equals(localName) && XMLSignature.XMLNS.equals(namespace)) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   130
                    content.add(new DOMKeyName(childElem));
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   131
                } else if ("KeyValue".equals(localName) && XMLSignature.XMLNS.equals(namespace)) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   132
                    content.add(DOMKeyValue.unmarshal(childElem));
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   133
                } else if ("RetrievalMethod".equals(localName) && XMLSignature.XMLNS.equals(namespace)) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   134
                    content.add(new DOMRetrievalMethod(childElem,
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   135
                                                       context, provider));
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   136
                } else if ("PGPData".equals(localName) && XMLSignature.XMLNS.equals(namespace)) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   137
                    content.add(new DOMPGPData(childElem));
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   138
                } else { //may be MgmtData, SPKIData or element from other namespace
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   139
                    content.add(new javax.xml.crypto.dom.DOMStructure(childElem));
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   140
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   142
            firstChild = firstChild.getNextSibling();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        keyInfoTypes = Collections.unmodifiableList(content);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public String getId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
24970
094bfaa699c3 8046044: Fix raw and unchecked lint warnings in XML Signature Impl
mullan
parents: 23010
diff changeset
   151
    public List<XMLStructure> getContent() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return keyInfoTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public void marshal(XMLStructure parent, XMLCryptoContext context)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   156
        throws MarshalException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   157
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (parent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new NullPointerException("parent is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   161
        if (!(parent instanceof javax.xml.crypto.dom.DOMStructure)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   162
            throw new ClassCastException("parent must be of type DOMStructure");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   163
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   165
        Node pNode = ((javax.xml.crypto.dom.DOMStructure)parent).getNode();
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   166
        String dsPrefix = DOMUtils.getSignaturePrefix(context);
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   167
        Element kiElem = DOMUtils.createElement
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   168
            (DOMUtils.getOwnerDocument(pNode), "KeyInfo",
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   169
             XMLSignature.XMLNS, dsPrefix);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   170
        if (dsPrefix == null || dsPrefix.length() == 0) {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   171
            kiElem.setAttributeNS("http://www.w3.org/2000/xmlns/",
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   172
                                  "xmlns", XMLSignature.XMLNS);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   173
        } else {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   174
            kiElem.setAttributeNS("http://www.w3.org/2000/xmlns/",
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   175
                                  "xmlns:" + dsPrefix, XMLSignature.XMLNS);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   176
        }
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   177
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   178
        Node nextSibling = null;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   179
        if (context instanceof DOMSignContext) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   180
            nextSibling = ((DOMSignContext)context).getNextSibling();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   181
        }
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   182
        marshal(pNode, kiElem, nextSibling, dsPrefix, (DOMCryptoContext)context);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   185
    @Override
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   186
    public void marshal(Node parent, String dsPrefix,
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   187
                        DOMCryptoContext context)
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   188
        throws MarshalException
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   189
    {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   190
        marshal(parent, null, dsPrefix, context);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   193
    public void marshal(Node parent, Node nextSibling, String dsPrefix,
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   194
                        DOMCryptoContext context)
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   195
        throws MarshalException
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   196
    {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   197
        Document ownerDoc = DOMUtils.getOwnerDocument(parent);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   198
        Element kiElem = DOMUtils.createElement(ownerDoc, "KeyInfo",
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   199
                                                XMLSignature.XMLNS, dsPrefix);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   200
        marshal(parent, kiElem, nextSibling, dsPrefix, context);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   201
    }
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   202
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   203
    private void marshal(Node parent, Element kiElem, Node nextSibling,
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   204
                         String dsPrefix, DOMCryptoContext context)
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   205
        throws MarshalException
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   206
    {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   207
        // create and append KeyInfoType elements
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   208
        for (XMLStructure kiType : keyInfoTypes) {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   209
            if (kiType instanceof DOMStructure) {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   210
                ((DOMStructure)kiType).marshal(kiElem, dsPrefix, context);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   211
            } else {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   212
                DOMUtils.appendChild(kiElem,
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   213
                    ((javax.xml.crypto.dom.DOMStructure)kiType).getNode());
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   214
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   217
        // append id attribute
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   218
        DOMUtils.setAttributeID(kiElem, "Id", id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   220
        parent.insertBefore(kiElem, nextSibling);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   223
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (this == o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (!(o instanceof KeyInfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   232
        KeyInfo oki = (KeyInfo)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   234
        boolean idsEqual = id == null ? oki.getId() == null
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   235
                                       : id.equals(oki.getId());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   237
        return keyInfoTypes.equals(oki.getContent()) && idsEqual;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   239
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   240
    @Override
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   241
    public int hashCode() {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   242
        int result = 17;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   243
        if (id != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   244
            result = 31 * result + id.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   245
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   246
        result = 31 * result + keyInfoTypes.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   247
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   248
        return result;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   249
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
}