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