jdk/src/java.xml.crypto/share/classes/org/jcp/xml/dsig/internal/dom/DOMSignedInfo.java
author mullan
Fri, 21 Nov 2014 15:23:36 -0500
changeset 27747 3a271dc8b758
parent 25859 3317bb8137f4
child 40551 05eba5515cbb
permissions -rw-r--r--
8046949: Generify the javax.xml.crypto API Reviewed-by: xuelei
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
/*
24970
094bfaa699c3 8046044: Fix raw and unchecked lint warnings in XML Signature Impl
mullan
parents: 23010
diff changeset
    24
 * Copyright (c) 2005, 2014, 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
/*
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    27
 * $Id: DOMSignedInfo.java 1333415 2012-05-03 12:03:51Z coheigea $
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
package org.jcp.xml.dsig.internal.dom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.xml.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.xml.crypto.dom.DOMCryptoContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.xml.crypto.dsig.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.InputStream;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    38
import java.io.OutputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.IOException;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    40
import java.security.Provider;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.*;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    42
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import org.w3c.dom.Document;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import org.w3c.dom.Element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import com.sun.org.apache.xml.internal.security.utils.Base64;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    48
import com.sun.org.apache.xml.internal.security.utils.Constants;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import com.sun.org.apache.xml.internal.security.utils.UnsyncBufferedOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * DOM-based implementation of SignedInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public final class DOMSignedInfo extends DOMStructure implements SignedInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    58
    /**
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    59
     * The maximum number of references per Manifest, if secure validation is enabled.
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    60
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    61
    public static final int MAXIMUM_REFERENCE_COUNT = 30;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    62
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    63
    private static java.util.logging.Logger log =
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    64
        java.util.logging.Logger.getLogger("org.jcp.xml.dsig.internal.dom");
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    65
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    66
    /** Signature - NOT Recommended RSAwithMD5 */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    67
    private static final String ALGO_ID_SIGNATURE_NOT_RECOMMENDED_RSA_MD5 =
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    68
        Constants.MoreAlgorithmsSpecNS + "rsa-md5";
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    69
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    70
    /** HMAC - NOT Recommended HMAC-MD5 */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    71
    private static final String ALGO_ID_MAC_HMAC_NOT_RECOMMENDED_MD5 =
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    72
        Constants.MoreAlgorithmsSpecNS + "hmac-md5";
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    73
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    74
    private List<Reference> references;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private CanonicalizationMethod canonicalizationMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private SignatureMethod signatureMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private String id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private Document ownerDoc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private Element localSiElem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private InputStream canonData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Creates a <code>DOMSignedInfo</code> from the specified parameters. Use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * this constructor when the <code>Id</code> is not specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param cm the canonicalization method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param sm the signature method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param references the list of references. The list is copied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @throws NullPointerException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *    <code>cm</code>, <code>sm</code>, or <code>references</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *    <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @throws IllegalArgumentException if <code>references</code> is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @throws ClassCastException if any of the references are not of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *    type <code>Reference</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public DOMSignedInfo(CanonicalizationMethod cm, SignatureMethod sm,
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    97
                         List<? extends Reference> references) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (cm == null || sm == null || references == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.canonicalizationMethod = cm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.signatureMethod = sm;
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   103
        List<Reference> tempList =
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   104
            Collections.checkedList(new ArrayList<Reference>(),
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   105
                                    Reference.class);
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   106
        tempList.addAll(references);
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   107
        if (tempList.isEmpty()) {
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   108
            throw new IllegalArgumentException("references cannot be empty");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   110
        this.references = Collections.unmodifiableList(tempList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Creates a <code>DOMSignedInfo</code> from the specified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param cm the canonicalization method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param sm the signature method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param references the list of references. The list is copied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param id an optional identifer that will allow this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *    <code>SignedInfo</code> to be referenced by other signatures and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *    objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @throws NullPointerException if <code>cm</code>, <code>sm</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *    or <code>references</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @throws IllegalArgumentException if <code>references</code> is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @throws ClassCastException if any of the references are not of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *    type <code>Reference</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public DOMSignedInfo(CanonicalizationMethod cm, SignatureMethod sm,
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   129
                         List<? extends Reference> references, String id) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this(cm, sm, references);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Creates a <code>DOMSignedInfo</code> from an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param siElem a SignedInfo element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   139
    public DOMSignedInfo(Element siElem, XMLCryptoContext context, Provider provider)
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   140
        throws MarshalException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        localSiElem = siElem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        ownerDoc = siElem.getOwnerDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // get Id attribute, if specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        id = DOMUtils.getAttributeValue(siElem, "Id");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // unmarshal CanonicalizationMethod
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   148
        Element cmElem = DOMUtils.getFirstChildElement(siElem,
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   149
                                                       "CanonicalizationMethod");
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   150
        canonicalizationMethod = new DOMCanonicalizationMethod(cmElem, context,
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   151
                                                               provider);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // unmarshal SignatureMethod
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   154
        Element smElem = DOMUtils.getNextSiblingElement(cmElem,
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   155
                                                        "SignatureMethod");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        signatureMethod = DOMSignatureMethod.unmarshal(smElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   158
        boolean secVal = Utils.secureValidation(context);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   159
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   160
        String signatureMethodAlgorithm = signatureMethod.getAlgorithm();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   161
        if (secVal && ((ALGO_ID_MAC_HMAC_NOT_RECOMMENDED_MD5.equals(signatureMethodAlgorithm)
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   162
                || ALGO_ID_SIGNATURE_NOT_RECOMMENDED_RSA_MD5.equals(signatureMethodAlgorithm)))) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   163
            throw new MarshalException(
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   164
                "It is forbidden to use algorithm " + signatureMethod + " when secure validation is enabled"
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   165
            );
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   166
        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   167
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // unmarshal References
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   169
        ArrayList<Reference> refList = new ArrayList<Reference>(5);
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   170
        Element refElem = DOMUtils.getNextSiblingElement(smElem, "Reference");
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   171
        refList.add(new DOMReference(refElem, context, provider));
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   172
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   173
        refElem = DOMUtils.getNextSiblingElement(refElem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        while (refElem != null) {
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   175
            String name = refElem.getLocalName();
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   176
            if (!name.equals("Reference")) {
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   177
                throw new MarshalException("Invalid element name: " +
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   178
                                           name + ", expected Reference");
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   179
            }
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   180
            refList.add(new DOMReference(refElem, context, provider));
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   181
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   182
            if (secVal && (refList.size() > MAXIMUM_REFERENCE_COUNT)) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   183
                String error = "A maxiumum of " + MAXIMUM_REFERENCE_COUNT + " "
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   184
                    + "references per Manifest are allowed with secure validation";
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   185
                throw new MarshalException(error);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   186
            }
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   187
            refElem = DOMUtils.getNextSiblingElement(refElem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        references = Collections.unmodifiableList(refList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public CanonicalizationMethod getCanonicalizationMethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return canonicalizationMethod;
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 SignatureMethod getSignatureMethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return signatureMethod;
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 String getId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
24970
094bfaa699c3 8046044: Fix raw and unchecked lint warnings in XML Signature Impl
mullan
parents: 23010
diff changeset
   204
    public List<Reference> getReferences() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return references;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public InputStream getCanonicalizedData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return canonData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   212
    public void canonicalize(XMLCryptoContext context, ByteArrayOutputStream bos)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        throws XMLSignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (context == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            throw new NullPointerException("context cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        OutputStream os = new UnsyncBufferedOutputStream(bos);
25391
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   219
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   220
        DOMSubTreeData subTree = new DOMSubTreeData(localSiElem, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        try {
25391
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   222
            ((DOMCanonicalizationMethod)
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   223
                canonicalizationMethod).canonicalize(subTree, context, os);
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   224
        } catch (TransformException te) {
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   225
            throw new XMLSignatureException(te);
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   226
        }
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   227
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   228
        try {
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   229
            os.flush();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        } catch (IOException e) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   231
            if (log.isLoggable(java.util.logging.Level.FINE)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   232
                log.log(java.util.logging.Level.FINE, e.getMessage(), e);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   233
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            // Impossible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        byte[] signedInfoBytes = bos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // this whole block should only be done if logging is enabled
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   240
        if (log.isLoggable(java.util.logging.Level.FINE)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   241
            log.log(java.util.logging.Level.FINE, "Canonicalized SignedInfo:");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   242
            StringBuilder sb = new StringBuilder(signedInfoBytes.length);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   243
            for (int i = 0; i < signedInfoBytes.length; i++) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   244
                sb.append((char)signedInfoBytes[i]);
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   245
            }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   246
            log.log(java.util.logging.Level.FINE, sb.toString());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   247
            log.log(java.util.logging.Level.FINE, "Data to be signed/verified:" + Base64.encode(signedInfoBytes));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        this.canonData = new ByteArrayInputStream(signedInfoBytes);
25391
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   251
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   252
        try {
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   253
            os.close();
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   254
        } catch (IOException e) {
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   255
            if (log.isLoggable(java.util.logging.Level.FINE)) {
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   256
                log.log(java.util.logging.Level.FINE, e.getMessage(), e);
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   257
            }
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   258
            // Impossible
f5e21d481e41 8049244: XML Signature performance issue caused by unbuffered signature data
mullan
parents: 24970
diff changeset
   259
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   263
        throws MarshalException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   264
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        ownerDoc = DOMUtils.getOwnerDocument(parent);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   266
        Element siElem = DOMUtils.createElement(ownerDoc, "SignedInfo",
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   267
                                                XMLSignature.XMLNS, dsPrefix);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        // create and append CanonicalizationMethod element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        DOMCanonicalizationMethod dcm =
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   271
            (DOMCanonicalizationMethod)canonicalizationMethod;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        dcm.marshal(siElem, dsPrefix, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // create and append SignatureMethod element
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   275
        ((DOMStructure)signatureMethod).marshal(siElem, dsPrefix, context);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // create and append Reference elements
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   278
        for (Reference reference : references) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   279
            ((DOMReference)reference).marshal(siElem, dsPrefix, context);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // append Id attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        DOMUtils.setAttributeID(siElem, "Id", id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        parent.appendChild(siElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        localSiElem = siElem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   289
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (this == o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (!(o instanceof SignedInfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   298
        SignedInfo osi = (SignedInfo)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   300
        boolean idEqual = (id == null ? osi.getId() == null
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   301
                                      : id.equals(osi.getId()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return (canonicalizationMethod.equals(osi.getCanonicalizationMethod())
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   304
                && signatureMethod.equals(osi.getSignatureMethod()) &&
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   305
                references.equals(osi.getReferences()) && idEqual);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   306
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   307
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   308
    @Override
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   309
    public int hashCode() {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   310
        int result = 17;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   311
        if (id != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   312
            result = 31 * result + id.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   313
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   314
        result = 31 * result + canonicalizationMethod.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   315
        result = 31 * result + signatureMethod.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   316
        result = 31 * result + references.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   317
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   318
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
}