jdk/src/share/classes/org/jcp/xml/dsig/internal/dom/DOMRetrievalMethod.java
author mullan
Mon, 16 Jun 2014 13:47:42 -0400
changeset 24970 094bfaa699c3
parent 23010 6dadb192ad81
permissions -rw-r--r--
8046044: Fix raw and unchecked lint warnings in XML Signature Impl Reviewed-by: darcy
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: 18266
diff changeset
     5
/**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
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: 18266
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: 18266
diff changeset
     8
 * distributed with this work for additional information
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
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: 18266
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: 18266
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: 18266
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: 18266
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: 18266
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: 18266
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: 18266
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: 18266
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: 18266
diff changeset
    20
 * specific language governing permissions and limitations
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
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: 18266
diff changeset
    34
 * $Id: DOMRetrievalMethod.java 1333415 2012-05-03 12:03:51Z 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 java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.net.URISyntaxException;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    41
import java.security.Provider;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.*;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    43
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    44
import javax.xml.XMLConstants;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.xml.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.xml.crypto.dsig.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.xml.crypto.dom.DOMCryptoContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.xml.crypto.dom.DOMURIReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.xml.parsers.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import org.w3c.dom.Attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import org.w3c.dom.Document;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import org.w3c.dom.Element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * DOM-based implementation of RetrievalMethod.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author Joyce Leung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public final class DOMRetrievalMethod extends DOMStructure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    implements RetrievalMethod, DOMURIReference {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    65
    private final List<Transform> transforms;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private String uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private Attr here;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Creates a <code>DOMRetrievalMethod</code> containing the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * URIReference and List of Transforms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param uri the URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param type the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param transforms a list of {@link Transform}s. The list is defensively
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *    copied to prevent subsequent modification. May be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *    or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @throws IllegalArgumentException if the format of <code>uri</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *    invalid, as specified by Reference's URI attribute in the W3C
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *    specification for XML-Signature Syntax and Processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @throws NullPointerException if <code>uriReference</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *    is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @throws ClassCastException if <code>transforms</code> contains any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *    entries that are not of type {@link Transform}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    87
    public DOMRetrievalMethod(String uri, String type,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    88
                              List<? extends Transform> transforms)
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    89
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (uri == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new NullPointerException("uri cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (transforms == null || transforms.isEmpty()) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    94
            this.transforms = Collections.emptyList();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } else {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    96
            this.transforms = Collections.unmodifiableList(
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    97
                new ArrayList<Transform>(transforms));
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    98
            for (int i = 0, size = this.transforms.size(); i < size; i++) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
    99
                if (!(this.transforms.get(i) instanceof Transform)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    throw new ClassCastException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                        ("transforms["+i+"] is not a valid type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.uri = uri;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   106
        if (!uri.equals("")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                new URI(uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                throw new IllegalArgumentException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Creates a <code>DOMRetrievalMethod</code> from an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param rmElem a RetrievalMethod element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   122
    public DOMRetrievalMethod(Element rmElem, XMLCryptoContext context,
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   123
                              Provider provider)
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   124
        throws MarshalException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   125
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        // get URI and Type attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        uri = DOMUtils.getAttributeValue(rmElem, "URI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        type = DOMUtils.getAttributeValue(rmElem, "Type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        // get here node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        here = rmElem.getAttributeNodeNS(null, "URI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   133
        boolean secVal = Utils.secureValidation(context);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   134
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // get Transforms, if specified
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   136
        List<Transform> transforms = new ArrayList<Transform>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        Element transformsElem = DOMUtils.getFirstChildElement(rmElem);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   138
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (transformsElem != null) {
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   140
            String localName = transformsElem.getLocalName();
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   141
            if (!localName.equals("Transforms")) {
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   142
                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
   143
                                           localName + ", expected Transforms");
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   144
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            Element transformElem =
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   146
                DOMUtils.getFirstChildElement(transformsElem, "Transform");
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   147
            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
   148
            transformElem = DOMUtils.getNextSiblingElement(transformElem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            while (transformElem != null) {
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   150
                String name = transformElem.getLocalName();
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   151
                if (!name.equals("Transform")) {
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   152
                    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
   153
                                               name + ", expected Transform");
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   154
                }
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   155
                transforms.add
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   156
                    (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
   157
                if (secVal && (transforms.size() > DOMReference.MAXIMUM_TRANSFORM_COUNT)) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   158
                    String error = "A maxiumum of " + DOMReference.MAXIMUM_TRANSFORM_COUNT + " "
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   159
                        + "transforms per Reference are allowed with secure validation";
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   160
                    throw new MarshalException(error);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   161
                }
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
   162
                transformElem = DOMUtils.getNextSiblingElement(transformElem);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (transforms.isEmpty()) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   166
            this.transforms = Collections.emptyList();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            this.transforms = Collections.unmodifiableList(transforms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public String getURI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public String getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
24970
094bfaa699c3 8046044: Fix raw and unchecked lint warnings in XML Signature Impl
mullan
parents: 23010
diff changeset
   180
    public List<Transform> getTransforms() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return transforms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   185
        throws MarshalException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   186
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        Document ownerDoc = DOMUtils.getOwnerDocument(parent);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   188
        Element rmElem = DOMUtils.createElement(ownerDoc, "RetrievalMethod",
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   189
                                                XMLSignature.XMLNS, dsPrefix);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        // add URI and Type attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        DOMUtils.setAttribute(rmElem, "URI", uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        DOMUtils.setAttribute(rmElem, "Type", type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // add Transforms elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (!transforms.isEmpty()) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   197
            Element transformsElem = DOMUtils.createElement(ownerDoc,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   198
                                                            "Transforms",
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   199
                                                            XMLSignature.XMLNS,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   200
                                                            dsPrefix);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            rmElem.appendChild(transformsElem);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   202
            for (Transform transform : transforms) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   203
                ((DOMTransform)transform).marshal(transformsElem,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   204
                                                   dsPrefix, context);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        parent.appendChild(rmElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // save here node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        here = rmElem.getAttributeNodeNS(null, "URI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public Node getHere() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return here;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public Data dereference(XMLCryptoContext context)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   219
        throws URIReferenceException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   220
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (context == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            throw new NullPointerException("context cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
         * If URIDereferencer is specified in context; use it, otherwise use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
         * built-in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        URIDereferencer deref = context.getURIDereferencer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (deref == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            deref = DOMURIDereferencer.INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        Data data = deref.dereference(this, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        // pass dereferenced data through Transforms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        try {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   238
            for (Transform transform : transforms) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   239
                data = ((DOMTransform)transform).transform(data, context);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw new URIReferenceException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
18266
26e69da689b9 8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents: 18240
diff changeset
   244
26e69da689b9 8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents: 18240
diff changeset
   245
        // guard against RetrievalMethod loops
26e69da689b9 8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents: 18240
diff changeset
   246
        if ((data instanceof NodeSetData) && Utils.secureValidation(context)) {
26e69da689b9 8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents: 18240
diff changeset
   247
            NodeSetData nsd = (NodeSetData)data;
24970
094bfaa699c3 8046044: Fix raw and unchecked lint warnings in XML Signature Impl
mullan
parents: 23010
diff changeset
   248
            Iterator<?> i = nsd.iterator();
18266
26e69da689b9 8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents: 18240
diff changeset
   249
            if (i.hasNext()) {
26e69da689b9 8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents: 18240
diff changeset
   250
                Node root = (Node)i.next();
26e69da689b9 8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents: 18240
diff changeset
   251
                if ("RetrievalMethod".equals(root.getLocalName())) {
26e69da689b9 8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents: 18240
diff changeset
   252
                    throw new URIReferenceException(
26e69da689b9 8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents: 18240
diff changeset
   253
                        "It is forbidden to have one RetrievalMethod point " +
26e69da689b9 8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents: 18240
diff changeset
   254
                        "to another when secure validation is enabled");
26e69da689b9 8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents: 18240
diff changeset
   255
                }
26e69da689b9 8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents: 18240
diff changeset
   256
            }
26e69da689b9 8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents: 18240
diff changeset
   257
        }
26e69da689b9 8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents: 18240
diff changeset
   258
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public XMLStructure dereferenceAsXMLStructure(XMLCryptoContext context)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   263
        throws URIReferenceException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   264
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        try {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   266
            ApacheData data = (ApacheData)dereference(context);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            dbf.setNamespaceAware(true);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   269
            dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            DocumentBuilder db = dbf.newDocumentBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            Document doc = db.parse(new ByteArrayInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                (data.getXMLSignatureInput().getBytes()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            Element kiElem = doc.getDocumentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (kiElem.getLocalName().equals("X509Data")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                return new DOMX509Data(kiElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                return null; // unsupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            throw new URIReferenceException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   284
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (!(obj instanceof RetrievalMethod)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   292
        RetrievalMethod orm = (RetrievalMethod)obj;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   294
        boolean typesEqual = (type == null ? orm.getType() == null
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   295
                                           : type.equals(orm.getType()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return (uri.equals(orm.getURI()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            transforms.equals(orm.getTransforms()) && typesEqual);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   300
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   301
    @Override
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   302
    public int hashCode() {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   303
        int result = 17;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   304
        if (type != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   305
            result = 31 * result + type.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   306
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   307
        result = 31 * result + uri.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   308
        result = 31 * result + transforms.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   309
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   310
        return result;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18266
diff changeset
   311
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
}