jdk/src/java.xml.crypto/share/classes/org/jcp/xml/dsig/internal/dom/DOMReference.java
author mullan
Thu, 25 Aug 2016 15:06:26 -0400
changeset 40551 05eba5515cbb
parent 32003 acb12269398a
permissions -rw-r--r--
8151893: Add security property to configure XML Signature secure validation mode Reviewed-by: jnimeh, 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!
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
/*
40551
05eba5515cbb 8151893: Add security property to configure XML Signature secure validation mode
mullan
parents: 32003
diff changeset
    24
 * Copyright (c) 2005, 2016, 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
/*
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    34
 * $Id: DOMReference.java 1334007 2012-05-04 14:59:46Z 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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.xml.crypto.dsig.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.xml.crypto.dom.DOMCryptoContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.xml.crypto.dom.DOMURIReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.net.URISyntaxException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import org.w3c.dom.Attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import org.w3c.dom.Document;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import org.w3c.dom.Element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import org.jcp.xml.dsig.internal.DigesterOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import com.sun.org.apache.xml.internal.security.utils.Base64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import com.sun.org.apache.xml.internal.security.utils.UnsyncBufferedOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * DOM-based implementation of Reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @author Joyce Leung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public final class DOMReference extends DOMStructure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    implements Reference, DOMURIReference {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    68
   /**
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    69
    * Look up useC14N11 system property. If true, an explicit C14N11 transform
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    70
    * will be added if necessary when generating the signature. See section
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    71
    * 3.1.1 of http://www.w3.org/2007/xmlsec/Drafts/xmldsig-core/ for more info.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    72
    *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    73
    * If true, overrides the same property if set in the XMLSignContext.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    74
    */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    75
    private static boolean useC14N11 =
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    76
        AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    77
            public Boolean run() {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    78
                return Boolean.valueOf(Boolean.getBoolean
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    79
                    ("com.sun.org.apache.xml.internal.security.useC14N11"));
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    80
            }
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    81
        });
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    82
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    83
    private static java.util.logging.Logger log =
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    84
        java.util.logging.Logger.getLogger("org.jcp.xml.dsig.internal.dom");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private final DigestMethod digestMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private final String id;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    88
    private final List<Transform> transforms;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    89
    private List<Transform> allTransforms;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private final Data appliedTransformData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private Attr here;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private final String uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private final String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private byte[] digestValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private byte[] calcDigestValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private Element refElem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private boolean digested = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private boolean validated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private boolean validationStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private Data derefData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private InputStream dis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private MessageDigest md;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   103
    private Provider provider;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Creates a <code>Reference</code> from the specified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param uri the URI (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param type the type (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param dm the digest method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param transforms a list of {@link Transform}s. The list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *    is defensively copied to protect against subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *    May be <code>null</code> or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param id the reference ID (may be <code>null</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @throws NullPointerException if <code>dm</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @throws ClassCastException if any of the <code>transforms</code> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *    not of type <code>Transform</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public DOMReference(String uri, String type, DigestMethod dm,
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   120
                        List<? extends Transform> transforms, String id,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   121
                        Provider provider)
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   122
    {
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   123
        this(uri, type, dm, null, null, transforms, id, null, provider);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public DOMReference(String uri, String type, DigestMethod dm,
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   127
                        List<? extends Transform> appliedTransforms,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   128
                        Data result, List<? extends Transform> transforms,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   129
                        String id, Provider provider)
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   130
    {
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   131
        this(uri, type, dm, appliedTransforms,
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   132
             result, transforms, id, null, provider);
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   133
    }
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   134
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   135
    public DOMReference(String uri, String type, DigestMethod dm,
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   136
                        List<? extends Transform> appliedTransforms,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   137
                        Data result, List<? extends Transform> transforms,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   138
                        String id, byte[] digestValue, Provider provider)
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   139
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (dm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            throw new NullPointerException("DigestMethod must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   143
        List<Transform> tempList =
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   144
            Collections.checkedList(new ArrayList<Transform>(),
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   145
                                    Transform.class);
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   146
        if (appliedTransforms != null) {
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   147
            tempList.addAll(appliedTransforms);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   149
        List<Transform> tempList2 =
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   150
            Collections.checkedList(new ArrayList<Transform>(),
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   151
                                    Transform.class);
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   152
        if (transforms != null) {
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   153
            tempList.addAll(transforms);
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   154
            tempList2.addAll(transforms);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   156
        this.allTransforms = Collections.unmodifiableList(tempList);
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   157
        this.transforms = tempList2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        this.digestMethod = dm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        this.uri = uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if ((uri != null) && (!uri.equals(""))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                new URI(uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                throw new IllegalArgumentException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (digestValue != null) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 19051
diff changeset
   170
            this.digestValue = digestValue.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            this.digested = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        this.appliedTransformData = result;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   174
        this.provider = provider;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Creates a <code>DOMReference</code> from an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param refElem a Reference element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   182
    public DOMReference(Element refElem, XMLCryptoContext context,
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   183
                        Provider provider)
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   184
        throws MarshalException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   185
    {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   186
        boolean secVal = Utils.secureValidation(context);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   187
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // unmarshal Transforms, if specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        Element nextSibling = DOMUtils.getFirstChildElement(refElem);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   190
        List<Transform> transforms = new ArrayList<Transform>(5);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (nextSibling.getLocalName().equals("Transforms")) {
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   192
            Element transformElem = DOMUtils.getFirstChildElement(nextSibling,
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   193
                                                                  "Transform");
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   194
            transforms.add(new DOMTransform(transformElem, context, provider));
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   195
            transformElem = DOMUtils.getNextSiblingElement(transformElem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            while (transformElem != null) {
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   197
                String localName = transformElem.getLocalName();
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   198
                if (!localName.equals("Transform")) {
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   199
                    throw new MarshalException(
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   200
                        "Invalid element name: " + localName +
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   201
                        ", expected Transform");
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   202
                }
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   203
                transforms.add
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   204
                    (new DOMTransform(transformElem, context, provider));
40551
05eba5515cbb 8151893: Add security property to configure XML Signature secure validation mode
mullan
parents: 32003
diff changeset
   205
                if (secVal && Policy.restrictNumTransforms(transforms.size())) {
05eba5515cbb 8151893: Add security property to configure XML Signature secure validation mode
mullan
parents: 32003
diff changeset
   206
                    String error = "A maximum of " + Policy.maxTransforms()
05eba5515cbb 8151893: Add security property to configure XML Signature secure validation mode
mullan
parents: 32003
diff changeset
   207
                        + " transforms per Reference are allowed when"
05eba5515cbb 8151893: Add security property to configure XML Signature secure validation mode
mullan
parents: 32003
diff changeset
   208
                        + " secure validation is enabled";
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   209
                    throw new MarshalException(error);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   210
                }
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   211
                transformElem = DOMUtils.getNextSiblingElement(transformElem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            nextSibling = DOMUtils.getNextSiblingElement(nextSibling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   215
        if (!nextSibling.getLocalName().equals("DigestMethod")) {
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   216
            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
   217
                                       nextSibling.getLocalName() +
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   218
                                       ", expected DigestMethod");
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   219
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        // unmarshal DigestMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        Element dmElem = nextSibling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        this.digestMethod = DOMDigestMethod.unmarshal(dmElem);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   224
        String digestMethodAlgorithm = this.digestMethod.getAlgorithm();
40551
05eba5515cbb 8151893: Add security property to configure XML Signature secure validation mode
mullan
parents: 32003
diff changeset
   225
        if (secVal && Policy.restrictAlg(digestMethodAlgorithm)) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   226
            throw new MarshalException(
40551
05eba5515cbb 8151893: Add security property to configure XML Signature secure validation mode
mullan
parents: 32003
diff changeset
   227
                "It is forbidden to use algorithm " + digestMethodAlgorithm +
05eba5515cbb 8151893: Add security property to configure XML Signature secure validation mode
mullan
parents: 32003
diff changeset
   228
                " when secure validation is enabled"
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   229
            );
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   230
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // unmarshal DigestValue
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   233
        Element dvElem = DOMUtils.getNextSiblingElement(dmElem, "DigestValue");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            this.digestValue = Base64.decode(dvElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } catch (Base64DecodingException bde) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            throw new MarshalException(bde);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   240
        // check for extra elements
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   241
        if (DOMUtils.getNextSiblingElement(dvElem) != null) {
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   242
            throw new MarshalException(
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   243
                "Unexpected element after DigestValue element");
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   244
        }
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   245
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // unmarshal attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        this.uri = DOMUtils.getAttributeValue(refElem, "URI");
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   248
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   249
        Attr attr = refElem.getAttributeNodeNS(null, "Id");
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   250
        if (attr != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   251
            this.id = attr.getValue();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   252
            refElem.setIdAttributeNode(attr, true);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   253
        } else {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   254
            this.id = null;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   255
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        this.type = DOMUtils.getAttributeValue(refElem, "Type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        this.here = refElem.getAttributeNodeNS(null, "URI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        this.refElem = refElem;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   260
        this.transforms = transforms;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        this.allTransforms = transforms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        this.appliedTransformData = null;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   263
        this.provider = provider;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public DigestMethod getDigestMethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return digestMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public String getId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public String getURI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public String getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
24970
094bfaa699c3 8046044: Fix raw and unchecked lint warnings in XML Signature Impl
mullan
parents: 23010
diff changeset
   282
    public List<Transform> getTransforms() {
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   283
        return Collections.unmodifiableList(allTransforms);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public byte[] getDigestValue() {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 19051
diff changeset
   287
        return (digestValue == null ? null : digestValue.clone());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public byte[] getCalculatedDigestValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return (calcDigestValue == null ? null
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 19051
diff changeset
   292
                                        : calcDigestValue.clone());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    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
   296
        throws MarshalException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   297
    {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   298
        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
   299
            log.log(java.util.logging.Level.FINE, "Marshalling Reference");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        Document ownerDoc = DOMUtils.getOwnerDocument(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   303
        refElem = DOMUtils.createElement(ownerDoc, "Reference",
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   304
                                         XMLSignature.XMLNS, dsPrefix);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // set attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        DOMUtils.setAttributeID(refElem, "Id", id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        DOMUtils.setAttribute(refElem, "URI", uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        DOMUtils.setAttribute(refElem, "Type", type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // create and append Transforms element
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   312
        if (!allTransforms.isEmpty()) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   313
            Element transformsElem = DOMUtils.createElement(ownerDoc,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   314
                                                            "Transforms",
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   315
                                                            XMLSignature.XMLNS,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   316
                                                            dsPrefix);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            refElem.appendChild(transformsElem);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   318
            for (Transform transform : allTransforms) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   319
                ((DOMStructure)transform).marshal(transformsElem,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   320
                                                  dsPrefix, context);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // create and append DigestMethod element
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   325
        ((DOMDigestMethod)digestMethod).marshal(refElem, dsPrefix, context);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        // create and append DigestValue element
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   328
        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
   329
            log.log(java.util.logging.Level.FINE, "Adding digestValueElem");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   331
        Element digestValueElem = DOMUtils.createElement(ownerDoc,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   332
                                                         "DigestValue",
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   333
                                                         XMLSignature.XMLNS,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   334
                                                         dsPrefix);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (digestValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            digestValueElem.appendChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                (ownerDoc.createTextNode(Base64.encode(digestValue)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        refElem.appendChild(digestValueElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        parent.appendChild(refElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        here = refElem.getAttributeNodeNS(null, "URI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public void digest(XMLSignContext signContext)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   346
        throws XMLSignatureException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   347
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        Data data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (appliedTransformData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            data = dereference(signContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            data = appliedTransformData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        digestValue = transform(data, signContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        // insert digestValue into DigestValue element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        String encodedDV = Base64.encode(digestValue);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   358
        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
   359
            log.log(java.util.logging.Level.FINE, "Reference object uri = " + uri);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        Element digestElem = DOMUtils.getLastChildElement(refElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (digestElem == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            throw new XMLSignatureException("DigestValue element expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        DOMUtils.removeAllChildren(digestElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        digestElem.appendChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            (refElem.getOwnerDocument().createTextNode(encodedDV));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        digested = true;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   370
        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
   371
            log.log(java.util.logging.Level.FINE, "Reference digesting completed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public boolean validate(XMLValidateContext validateContext)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   376
        throws XMLSignatureException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   377
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (validateContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            throw new NullPointerException("validateContext cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (validated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            return validationStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        Data data = dereference(validateContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        calcDigestValue = transform(data, validateContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   387
        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
   388
            log.log(java.util.logging.Level.FINE, "Expected digest: " + Base64.encode(digestValue));
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   389
            log.log(java.util.logging.Level.FINE, "Actual digest: " + Base64.encode(calcDigestValue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        validationStatus = Arrays.equals(digestValue, calcDigestValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        validated = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return validationStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public Data getDereferencedData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return derefData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public InputStream getDigestInputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        return dis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    private Data dereference(XMLCryptoContext context)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   406
        throws XMLSignatureException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   407
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        Data data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        // use user-specified URIDereferencer if specified; otherwise use deflt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        URIDereferencer deref = context.getURIDereferencer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (deref == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            deref = DOMURIDereferencer.INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            data = deref.dereference(this, context);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   417
            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
   418
                log.log(java.util.logging.Level.FINE, "URIDereferencer class name: " + deref.getClass().getName());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   419
                log.log(java.util.logging.Level.FINE, "Data class name: " + data.getClass().getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        } catch (URIReferenceException ure) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            throw new XMLSignatureException(ure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    private byte[] transform(Data dereferencedData,
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   429
                             XMLCryptoContext context)
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   430
        throws XMLSignatureException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   431
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (md == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                md = MessageDigest.getInstance
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   435
                    (((DOMDigestMethod)digestMethod).getMessageDigestAlgorithm());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                throw new XMLSignatureException(nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        DigesterOutputStream dos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        Boolean cache = (Boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            context.getProperty("javax.xml.crypto.dsig.cacheReference");
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   444
        if (cache != null && cache.booleanValue()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            this.derefData = copyDerefData(dereferencedData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            dos = new DigesterOutputStream(md, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            dos = new DigesterOutputStream(md);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   450
        OutputStream os = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        Data data = dereferencedData;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   452
        try {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   453
            os = new UnsyncBufferedOutputStream(dos);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   454
            for (int i = 0, size = transforms.size(); i < size; i++) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   455
                DOMTransform transform = (DOMTransform)transforms.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                if (i < size - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    data = transform.transform(data, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    data = transform.transform(data, context, os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            if (data != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                XMLSignatureInput xi;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   465
                // explicitly use C14N 1.1 when generating signature
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   466
                // first check system property, then context property
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   467
                boolean c14n11 = useC14N11;
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   468
                String c14nalg = CanonicalizationMethod.INCLUSIVE;
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   469
                if (context instanceof XMLSignContext) {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   470
                    if (!c14n11) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   471
                        Boolean prop = (Boolean)context.getProperty
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   472
                            ("com.sun.org.apache.xml.internal.security.useC14N11");
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   473
                        c14n11 = (prop != null && prop.booleanValue());
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   474
                        if (c14n11) {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   475
                            c14nalg = "http://www.w3.org/2006/12/xml-c14n11";
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   476
                        }
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   477
                    } else {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   478
                        c14nalg = "http://www.w3.org/2006/12/xml-c14n11";
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   479
                    }
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   480
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                if (data instanceof ApacheData) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   482
                    xi = ((ApacheData)data).getXMLSignatureInput();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                } else if (data instanceof OctetStreamData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    xi = new XMLSignatureInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                        (((OctetStreamData)data).getOctetStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                } else if (data instanceof NodeSetData) {
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   487
                    TransformService spi = null;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   488
                    if (provider == null) {
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   489
                        spi = TransformService.getInstance(c14nalg, "DOM");
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   490
                    } else {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   491
                        try {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   492
                            spi = TransformService.getInstance(c14nalg, "DOM", provider);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   493
                        } catch (NoSuchAlgorithmException nsae) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   494
                            spi = TransformService.getInstance(c14nalg, "DOM");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   495
                        }
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   496
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    data = spi.transform(data, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    xi = new XMLSignatureInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                        (((OctetStreamData)data).getOctetStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    throw new XMLSignatureException("unrecognized Data type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                }
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   503
                if (context instanceof XMLSignContext && c14n11
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   504
                    && !xi.isOctetStream() && !xi.isOutputStreamSet()) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   505
                    TransformService spi = null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   506
                    if (provider == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   507
                        spi = TransformService.getInstance(c14nalg, "DOM");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   508
                    } else {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   509
                        try {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   510
                            spi = TransformService.getInstance(c14nalg, "DOM", provider);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   511
                        } catch (NoSuchAlgorithmException nsae) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   512
                            spi = TransformService.getInstance(c14nalg, "DOM");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   513
                        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   514
                    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   515
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   516
                    DOMTransform t = new DOMTransform(spi);
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   517
                    Element transformsElem = null;
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   518
                    String dsPrefix = DOMUtils.getSignaturePrefix(context);
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   519
                    if (allTransforms.isEmpty()) {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   520
                        transformsElem = DOMUtils.createElement(
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   521
                            refElem.getOwnerDocument(),
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   522
                            "Transforms", XMLSignature.XMLNS, dsPrefix);
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   523
                        refElem.insertBefore(transformsElem,
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   524
                            DOMUtils.getFirstChildElement(refElem));
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   525
                    } else {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   526
                        transformsElem = DOMUtils.getFirstChildElement(refElem);
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   527
                    }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   528
                    t.marshal(transformsElem, dsPrefix,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   529
                              (DOMCryptoContext)context);
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   530
                    allTransforms.add(t);
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   531
                    xi.updateOutputStream(os, true);
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   532
                } else {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   533
                    xi.updateOutputStream(os);
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   534
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            os.flush();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   537
            if (cache != null && cache.booleanValue()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                this.dis = dos.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            return dos.getDigestValue();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   541
        } catch (NoSuchAlgorithmException e) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   542
            throw new XMLSignatureException(e);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   543
        } catch (TransformException e) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   544
            throw new XMLSignatureException(e);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   545
        } catch (MarshalException e) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   546
            throw new XMLSignatureException(e);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   547
        } catch (IOException e) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   548
            throw new XMLSignatureException(e);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   549
        } catch (com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            throw new XMLSignatureException(e);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   551
        } finally {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   552
            if (os != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   553
                try {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   554
                    os.close();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   555
                } catch (IOException e) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   556
                    throw new XMLSignatureException(e);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   557
                }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   558
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   559
            if (dos != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   560
                try {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   561
                    dos.close();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   562
                } catch (IOException e) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   563
                    throw new XMLSignatureException(e);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   564
                }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   565
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    public Node getHere() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        return here;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   573
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        if (this == o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        if (!(o instanceof Reference)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   582
        Reference oref = (Reference)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   584
        boolean idsEqual = (id == null ? oref.getId() == null
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   585
                                       : id.equals(oref.getId()));
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   586
        boolean urisEqual = (uri == null ? oref.getURI() == null
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   587
                                         : uri.equals(oref.getURI()));
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   588
        boolean typesEqual = (type == null ? oref.getType() == null
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   589
                                           : type.equals(oref.getType()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        boolean digestValuesEqual =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            Arrays.equals(digestValue, oref.getDigestValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   593
        return digestMethod.equals(oref.getDigestMethod()) && idsEqual &&
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   594
            urisEqual && typesEqual &&
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   595
            allTransforms.equals(oref.getTransforms()) && digestValuesEqual;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   596
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   597
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   598
    @Override
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   599
    public int hashCode() {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   600
        int result = 17;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   601
        if (id != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   602
            result = 31 * result + id.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   603
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   604
        if (uri != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   605
            result = 31 * result + uri.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   606
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   607
        if (type != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   608
            result = 31 * result + type.hashCode();
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
        if (digestValue != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   611
            result = 31 * result + Arrays.hashCode(digestValue);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   612
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   613
        result = 31 * result + digestMethod.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   614
        result = 31 * result + allTransforms.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   615
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   616
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    boolean isDigested() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        return digested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    private static Data copyDerefData(Data dereferencedData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if (dereferencedData instanceof ApacheData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            // need to make a copy of the Data
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   626
            ApacheData ad = (ApacheData)dereferencedData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            XMLSignatureInput xsi = ad.getXMLSignatureInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            if (xsi.isNodeSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                try {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   630
                    final Set<Node> s = xsi.getNodeSet();
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   631
                    return new NodeSetData<Node>() {
24970
094bfaa699c3 8046044: Fix raw and unchecked lint warnings in XML Signature Impl
mullan
parents: 23010
diff changeset
   632
                        public Iterator<Node> iterator() { return s.iterator(); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                    // log a warning
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   636
                    log.log(java.util.logging.Level.WARNING, "cannot cache dereferenced data: " + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            } else if (xsi.isElement()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                return new DOMSubTreeData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    (xsi.getSubNode(), xsi.isExcludeComments());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            } else if (xsi.isOctetStream() || xsi.isByteArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                try {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   644
                    return new OctetStreamData
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   645
                        (xsi.getOctetStream(), xsi.getSourceURI(),
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   646
                         xsi.getMIMEType());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    // log a warning
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   649
                    log.log(java.util.logging.Level.WARNING, "cannot cache dereferenced data: " + ioe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        return dereferencedData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
}