src/java.xml.crypto/share/classes/org/jcp/xml/dsig/internal/dom/DOMXMLSignature.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!
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
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
 */
1337
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
 * Portions 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
 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * ===========================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * (C) Copyright IBM Corp. 2003 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * ===========================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/*
54731
81de17a33575 8219013: Update Apache Santuario (XML Signature) to version 2.1.3
weijun
parents: 53998
diff changeset
    34
 * $Id: DOMXMLSignature.java 1854026 2019-02-21 09:30:01Z coheigea $
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package org.jcp.xml.dsig.internal.dom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.xml.crypto.*;
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
    39
import javax.xml.crypto.dom.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.xml.crypto.dsig.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.xml.crypto.dsig.dom.DOMSignContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.xml.crypto.dsig.dom.DOMValidateContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.xml.crypto.dsig.keyinfo.KeyInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.Key;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    47
import java.security.Provider;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.util.List;
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    52
import java.util.Map;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    53
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
    54
import org.w3c.dom.Attr;
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
    55
import org.w3c.dom.Document;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import org.w3c.dom.Element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import org.w3c.dom.Node;
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
import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * DOM-based implementation of XMLSignature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public final class DOMXMLSignature extends DOMStructure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    implements XMLSignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    68
    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
    69
        com.sun.org.slf4j.internal.LoggerFactory.getLogger(DOMXMLSignature.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private String id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private SignatureValue sv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private KeyInfo ki;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    73
    private List<XMLObject> objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private SignedInfo si;
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
    75
    private Document ownerDoc = null;
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
    76
    private Element localSigElem = null;
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
    77
    private Element sigElem = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private boolean validationStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private boolean validated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private KeySelectorResult ksr;
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    81
    private Map<String, XMLStructure> signatureIdMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        com.sun.org.apache.xml.internal.security.Init.init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    88
     * Creates a {@code DOMXMLSignature} from the specified components.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    90
     * @param si the {@code SignedInfo}
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    91
     * @param ki the {@code KeyInfo}, or {@code null} if not specified
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    92
     * @param objs a list of {@code XMLObject}s or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *  if not specified. The list is copied to protect against subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *  modification.
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    95
     * @param id an optional id (specify {@code null} to omit)
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    96
     * @param signatureValueId an optional id (specify {@code null} to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *  omit)
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    98
     * @throws NullPointerException if {@code si} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   100
    public DOMXMLSignature(SignedInfo si, KeyInfo ki,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   101
                           List<? extends XMLObject> objs,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   102
                           String id, String signatureValueId)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (si == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new NullPointerException("signedInfo cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.si = si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this.sv = new DOMSignatureValue(signatureValueId);
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   110
        if (objs == null) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   111
            this.objects = Collections.emptyList();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   112
        } else {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   113
            this.objects =
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   114
                Collections.unmodifiableList(new ArrayList<>(objs));
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   115
            for (int i = 0, size = this.objects.size(); i < size; i++) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   116
                if (!(this.objects.get(i) instanceof XMLObject)) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   117
                    throw new ClassCastException
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   118
                        ("objs["+i+"] is not an XMLObject");
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   119
                }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   120
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        this.ki = ki;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   126
     * Creates a {@code DOMXMLSignature} from XML.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param sigElem Signature element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @throws MarshalException if XMLSignature cannot be unmarshalled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   131
    public DOMXMLSignature(Element sigElem, XMLCryptoContext context,
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   132
                           Provider provider)
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   133
        throws MarshalException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   134
    {
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   135
        localSigElem = sigElem;
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   136
        ownerDoc = localSigElem.getOwnerDocument();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // get Id attribute, if specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        id = DOMUtils.getAttributeValue(localSigElem, "Id");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // unmarshal SignedInfo
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   141
        Element siElem = DOMUtils.getFirstChildElement(localSigElem,
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   142
                                                       "SignedInfo",
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   143
                                                       XMLSignature.XMLNS);
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   144
        si = new DOMSignedInfo(siElem, context, provider);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // unmarshal SignatureValue
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   147
        Element sigValElem = DOMUtils.getNextSiblingElement(siElem,
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   148
                                                            "SignatureValue",
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   149
                                                            XMLSignature.XMLNS);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   150
        sv = new DOMSignatureValue(sigValElem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // unmarshal KeyInfo, if specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        Element nextSibling = DOMUtils.getNextSiblingElement(sigValElem);
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   154
        if (nextSibling != null && nextSibling.getLocalName().equals("KeyInfo")
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   155
            && XMLSignature.XMLNS.equals(nextSibling.getNamespaceURI())) {
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   156
            ki = new DOMKeyInfo(nextSibling, context, provider);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            nextSibling = DOMUtils.getNextSiblingElement(nextSibling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // unmarshal Objects, if specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (nextSibling == null) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   162
            objects = Collections.emptyList();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        } else {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   164
            List<XMLObject> tempObjects = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            while (nextSibling != null) {
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   166
                String name = nextSibling.getLocalName();
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   167
                String namespace = nextSibling.getNamespaceURI();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   168
                if (!"Object".equals(name) || !XMLSignature.XMLNS.equals(namespace)) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   169
                    throw new MarshalException("Invalid element name: " + namespace + ":" + name +
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   170
                                               ", expected KeyInfo or Object");
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   171
                }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   172
                tempObjects.add(new DOMXMLObject(nextSibling,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   173
                                                 context, provider));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                nextSibling = DOMUtils.getNextSiblingElement(nextSibling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            objects = Collections.unmodifiableList(tempObjects);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public String getId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public KeyInfo getKeyInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return ki;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public SignedInfo getSignedInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
24970
094bfaa699c3 8046044: Fix raw and unchecked lint warnings in XML Signature Impl
mullan
parents: 23010
diff changeset
   192
    public List<XMLObject> getObjects() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return objects;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public SignatureValue getSignatureValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return sv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public KeySelectorResult getKeySelectorResult() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return ksr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   204
    @Override
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   205
    public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   206
        throws MarshalException
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   207
    {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   208
        marshal(parent, null, dsPrefix, context);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   209
    }
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   210
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   211
    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
   212
                        DOMCryptoContext context)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   213
        throws MarshalException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   214
    {
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   215
        ownerDoc = DOMUtils.getOwnerDocument(parent);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   216
        sigElem = DOMUtils.createElement(ownerDoc, "Signature",
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   217
                                         XMLSignature.XMLNS, dsPrefix);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   218
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   219
        // append xmlns attribute
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   220
        if (dsPrefix == null || dsPrefix.length() == 0) {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   221
            sigElem.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns",
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   222
                                   XMLSignature.XMLNS);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   223
        } else {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   224
            sigElem.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" +
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   225
                                   dsPrefix, XMLSignature.XMLNS);
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   226
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // create and append SignedInfo element
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   229
        ((DOMSignedInfo)si).marshal(sigElem, dsPrefix, context);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        // create and append SignatureValue element
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   232
        ((DOMSignatureValue)sv).marshal(sigElem, dsPrefix, context);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // create and append KeyInfo element if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (ki != null) {
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   236
            ((DOMKeyInfo)ki).marshal(sigElem, null, dsPrefix, context);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // create and append Object elements if necessary
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   240
        for (int i = 0, size = objects.size(); i < size; i++) {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   241
            ((DOMXMLObject)objects.get(i)).marshal(sigElem, dsPrefix, context);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   244
        // append Id attribute
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   245
        DOMUtils.setAttributeID(sigElem, "Id", id);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   246
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   247
        parent.insertBefore(sigElem, nextSibling);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   250
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public boolean validate(XMLValidateContext vc)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   252
        throws XMLSignatureException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   253
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (vc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throw new NullPointerException("validateContext is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (!(vc instanceof DOMValidateContext)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            throw new ClassCastException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                ("validateContext must be of type DOMValidateContext");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (validated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            return validationStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        // validate the signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        boolean sigValidity = sv.validate(vc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (!sigValidity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            validationStatus = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            validated = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            return validationStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // validate all References
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   276
        @SuppressWarnings("unchecked")
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   277
        List<Reference> refs = this.si.getReferences();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        boolean validateRefs = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        for (int i = 0, size = refs.size(); validateRefs && i < size; i++) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   280
            Reference ref = refs.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            boolean refValid = ref.validate(vc);
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   282
            LOG.debug("Reference [{}] is valid: {}", ref.getURI(), refValid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            validateRefs &= refValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (!validateRefs) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   286
            LOG.debug("Couldn't validate the References");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            validationStatus = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            validated = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            return validationStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        // validate Manifests, if property set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        boolean validateMans = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (Boolean.TRUE.equals(vc.getProperty
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   295
                                ("org.jcp.xml.dsig.validateManifests")))
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   296
        {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            for (int i=0, size=objects.size(); validateMans && i < size; i++) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   298
                XMLObject xo = objects.get(i);
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   299
                @SuppressWarnings("unchecked")
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   300
                List<XMLStructure> content = xo.getContent();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                int csize = content.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                for (int j = 0; validateMans && j < csize; j++) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   303
                    XMLStructure xs = content.get(j);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    if (xs instanceof Manifest) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   305
                        LOG.debug("validating manifest");
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   306
                        Manifest man = (Manifest)xs;
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   307
                        @SuppressWarnings("unchecked")
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   308
                        List<Reference> manRefs = man.getReferences();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                        int rsize = manRefs.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        for (int k = 0; validateMans && k < rsize; k++) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   311
                            Reference ref = manRefs.get(k);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                            boolean refValid = ref.validate(vc);
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   313
                            LOG.debug(
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   314
                                "Manifest ref [{}] is valid: {}", ref.getURI(),  refValid
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   315
                            );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                            validateMans &= refValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        validationStatus = validateMans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        validated = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return validationStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   328
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public void sign(XMLSignContext signContext)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   330
        throws MarshalException, XMLSignatureException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   331
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (signContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            throw new NullPointerException("signContext cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   335
        DOMSignContext context = (DOMSignContext)signContext;
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   336
        marshal(context.getParent(), context.getNextSibling(),
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   337
                DOMUtils.getSignaturePrefix(context), context);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        // generate references and signature value
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   340
        List<Reference> allReferences = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // traverse the Signature and register all objects with IDs that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // may contain References
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   344
        signatureIdMap = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        signatureIdMap.put(id, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        signatureIdMap.put(si.getId(), si);
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   347
        @SuppressWarnings("unchecked")
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   348
        List<Reference> refs = si.getReferences();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   349
        for (Reference ref : refs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            signatureIdMap.put(ref.getId(), ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   352
        for (XMLObject obj : objects) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            signatureIdMap.put(obj.getId(), obj);
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   354
            @SuppressWarnings("unchecked")
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   355
            List<XMLStructure> content = obj.getContent();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   356
            for (XMLStructure xs : content) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                if (xs instanceof Manifest) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   358
                    Manifest man = (Manifest)xs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    signatureIdMap.put(man.getId(), man);
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   360
                    @SuppressWarnings("unchecked")
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   361
                    List<Reference> manRefs = man.getReferences();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   362
                    for (Reference ref : manRefs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        allReferences.add(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                        signatureIdMap.put(ref.getId(), ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
4506
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 1337
diff changeset
   369
        // always add SignedInfo references after Manifest references so
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 1337
diff changeset
   370
        // that Manifest reference are digested first
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   371
        allReferences.addAll(refs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        // generate/digest each reference
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   374
        for (Reference ref : allReferences) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   375
            digestReference((DOMReference)ref, signContext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        // do final sweep to digest any references that were skipped or missed
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   379
        for (Reference ref : allReferences) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   380
            if (((DOMReference)ref).isDigested()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   383
            ((DOMReference)ref).digest(signContext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        Key signingKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        try {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   388
            KeySelectorResult keySelectorResult = signContext.getKeySelector().select(ki,
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   389
                                                      KeySelector.Purpose.SIGN,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   390
                                                      si.getSignatureMethod(),
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   391
                                                      signContext);
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   392
            signingKey = keySelectorResult.getKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if (signingKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                throw new XMLSignatureException("the keySelector did not " +
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   395
                                                "find a signing key");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   397
            ksr = keySelectorResult;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        } catch (KeySelectorException kse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            throw new XMLSignatureException("cannot find signing key", kse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        // calculate signature value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        try {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   404
            byte[] val = ((AbstractDOMSignatureMethod)
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   405
                si.getSignatureMethod()).sign(signingKey, si, signContext);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   406
            ((DOMSignatureValue)sv).setValue(val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        } catch (InvalidKeyException ike) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            throw new XMLSignatureException(ike);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   410
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   411
        this.localSigElem = sigElem;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   414
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        if (this == o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if (!(o instanceof XMLSignature)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   423
        XMLSignature osig = (XMLSignature)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        boolean idEqual =
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   426
            id == null ? osig.getId() == null : id.equals(osig.getId());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        boolean keyInfoEqual =
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   428
            ki == null ? osig.getKeyInfo() == null
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   429
                        : ki.equals(osig.getKeyInfo());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   431
        return idEqual && keyInfoEqual &&
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   432
                sv.equals(osig.getSignatureValue()) &&
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   433
                si.equals(osig.getSignedInfo()) &&
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   434
                objects.equals(osig.getObjects());
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   435
    }
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
    @Override
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   438
    public int hashCode() {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   439
        int result = 17;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   440
        if (id != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   441
            result = 31 * result + id.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   442
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   443
        if (ki != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   444
            result = 31 * result + ki.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   445
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   446
        result = 31 * result + sv.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   447
        result = 31 * result + si.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   448
        result = 31 * result + objects.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   449
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   450
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    private void digestReference(DOMReference ref, XMLSignContext signContext)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   454
        throws XMLSignatureException
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
        if (ref.isDigested()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        // check dependencies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        String uri = ref.getURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if (Utils.sameDocumentURI(uri)) {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   462
            String parsedId = Utils.parseIdFromSameDocumentURI(uri);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   463
            if (parsedId != null && signatureIdMap.containsKey(parsedId)) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   464
                XMLStructure xs = signatureIdMap.get(parsedId);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   465
                if (xs instanceof DOMReference) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   466
                    digestReference((DOMReference)xs, signContext);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   467
                } else if (xs instanceof Manifest) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   468
                    Manifest man = (Manifest)xs;
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   469
                    List<Reference> manRefs = DOMManifest.getManifestReferences(man);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    for (int i = 0, size = manRefs.size(); i < size; i++) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   471
                        digestReference((DOMReference)manRefs.get(i),
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   472
                                        signContext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            // if uri="" and there are XPath Transforms, there may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            // reference dependencies in the XPath Transform - so be on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            // the safe side, and skip and do at end in the final sweep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            if (uri.length() == 0) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   480
                List<Transform> transforms = ref.getTransforms();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   481
                for (Transform transform : transforms) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    String transformAlg = transform.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    if (transformAlg.equals(Transform.XPATH) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                        transformAlg.equals(Transform.XPATH2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        ref.digest(signContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    public class DOMSignatureValue extends DOMStructure
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   494
        implements SignatureValue
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   495
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        private String id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        private byte[] value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        private String valueBase64;
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   499
        private Element sigValueElem;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        private boolean validated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        private boolean validationStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        DOMSignatureValue(String id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   507
        DOMSignatureValue(Element sigValueElem)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   508
            throws MarshalException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   509
        {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   510
            // base64 decode signatureValue
54731
81de17a33575 8219013: Update Apache Santuario (XML Signature) to version 2.1.3
weijun
parents: 53998
diff changeset
   511
            String content = XMLUtils.getFullTextChildrenFromNode(sigValueElem);
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   512
            value = XMLUtils.decode(content);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   514
            Attr attr = sigValueElem.getAttributeNodeNS(null, "Id");
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   515
            if (attr != null) {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   516
                id = attr.getValue();
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   517
                sigValueElem.setIdAttributeNode(attr, true);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   518
            } else {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   519
                id = null;
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   520
            }
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   521
            this.sigValueElem = sigValueElem;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        public String getId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        public byte[] getValue() {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 19051
diff changeset
   529
            return (value == null) ? null : value.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   532
        public String getEncodedValue() {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   533
            return valueBase64;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   534
        }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   535
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   536
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        public boolean validate(XMLValidateContext validateContext)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   538
            throws XMLSignatureException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   539
        {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            if (validateContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                throw new NullPointerException("context cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            if (validated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                return validationStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            // get validating key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            SignatureMethod sm = si.getSignatureMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            Key validationKey = null;
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   551
            KeySelectorResult ksResult = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            try {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   553
                KeySelector keySelector = validateContext.getKeySelector();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   554
                if (keySelector != null) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   555
                    ksResult = keySelector.select
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   556
                        (ki, KeySelector.Purpose.VERIFY, sm, validateContext);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   557
                    if (ksResult != null) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   558
                        validationKey = ksResult.getKey();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   559
                    }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   560
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                if (validationKey == null) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   562
                    throw new XMLSignatureException("the keyselector did not " +
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   563
                                                    "find a validation key");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            } catch (KeySelectorException kse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                throw new XMLSignatureException("cannot find validation " +
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   567
                                                "key", kse);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            // canonicalize SignedInfo and verify signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            try {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   572
                validationStatus = ((AbstractDOMSignatureMethod)sm).verify
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   573
                    (validationKey, si, value, validateContext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                throw new XMLSignatureException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            validated = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            ksr = ksResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            return validationStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   583
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            if (this == o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            if (!(o instanceof SignatureValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   592
            SignatureValue osv = (SignatureValue)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            boolean idEqual =
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   595
                id == null ? osv.getId() == null : id.equals(osv.getId());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            //XXX compare signature values?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            return idEqual;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   601
        @Override
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   602
        public int hashCode() {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   603
            int result = 17;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   604
            if (id != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   605
                result = 31 * result + id.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   606
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   608
            return result;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   609
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   610
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   611
        public void marshal(Node parent, String dsPrefix,
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   612
                            DOMCryptoContext context)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   613
            throws MarshalException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   614
        {
54731
81de17a33575 8219013: Update Apache Santuario (XML Signature) to version 2.1.3
weijun
parents: 53998
diff changeset
   615
            // create SignatureValue element
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   616
            sigValueElem = DOMUtils.createElement(ownerDoc, "SignatureValue",
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   617
                                                  XMLSignature.XMLNS, dsPrefix);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   618
            if (valueBase64 != null) {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   619
                sigValueElem.appendChild(ownerDoc.createTextNode(valueBase64));
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   620
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            // append Id attribute, if specified
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   623
            DOMUtils.setAttributeID(sigValueElem, "Id", id);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   624
            parent.appendChild(sigValueElem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   627
        void setValue(byte[] value) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            this.value = value;
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   629
            valueBase64 = XMLUtils.encodeToString(value);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   630
            sigValueElem.appendChild(ownerDoc.createTextNode(valueBase64));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
}