jdk/src/share/classes/org/jcp/xml/dsig/internal/dom/DOMManifest.java
author mullan
Mon, 22 Apr 2013 11:23:33 +0100
changeset 18240 cda839ac048f
parent 5506 202f599c92aa
child 18780 f47b920867e7
permissions -rw-r--r--
6741606: Integrate Apache Santuario Reviewed-by: vinnie, hawtin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     2
 * reserved comment block
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
/*
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     6
 * Copyright 2005 The Apache Software Foundation.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     7
 *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     8
 *  Licensed under the Apache License, Version 2.0 (the "License");
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     9
 *  you may not use this file except in compliance with the License.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    10
 *  You may obtain a copy of the License at
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    11
 *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    12
 *      http://www.apache.org/licenses/LICENSE-2.0
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    13
 *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    14
 *  Unless required by applicable law or agreed to in writing, software
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    15
 *  distributed under the License is distributed on an "AS IS" BASIS,
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    16
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    17
 *  See the License for the specific language governing permissions and
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    18
 *  limitations under the License.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    19
 *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    20
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    21
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * Copyright (c) 2005, 2008, 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
    23
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    24
/*
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    25
 * $Id: DOMManifest.java,v 1.2 2008/07/24 15:20:32 mullan Exp $
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package org.jcp.xml.dsig.internal.dom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.xml.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.xml.crypto.dom.DOMCryptoContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.xml.crypto.dsig.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    33
import java.security.Provider;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    35
import org.w3c.dom.Attr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import org.w3c.dom.Document;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import org.w3c.dom.Element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * DOM-based implementation of Manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public final class DOMManifest extends DOMStructure implements Manifest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private final List references;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private final String id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Creates a <code>DOMManifest</code> containing the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * list of {@link Reference}s and optional id.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * @param references a list of one or more <code>Reference</code>s. The list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *    is defensively copied to protect against subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @param id the id (may be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @throws NullPointerException if <code>references</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *    <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @throws IllegalArgumentException if <code>references</code> is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @throws ClassCastException if <code>references</code> contains any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *    entries that are not of type {@link Reference}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public DOMManifest(List references, String id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (references == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            throw new NullPointerException("references cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        List refCopy = new ArrayList(references);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (refCopy.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            throw new IllegalArgumentException("list of references must " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                "contain at least one entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        for (int i = 0, size = refCopy.size(); i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (!(refCopy.get(i) instanceof Reference)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                throw new ClassCastException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    ("references["+i+"] is not a valid type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        this.references = Collections.unmodifiableList(refCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Creates a <code>DOMManifest</code> from an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param manElem a Manifest element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    87
    public DOMManifest(Element manElem, XMLCryptoContext context,
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    88
        Provider provider) throws MarshalException {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    89
        Attr attr = manElem.getAttributeNodeNS(null, "Id");
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    90
        if (attr != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    91
            this.id = attr.getValue();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    92
            manElem.setIdAttributeNode(attr, true);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    93
        } else {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    94
            this.id = null;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    95
        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    96
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
    97
        boolean secVal = Utils.secureValidation(context);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        Element refElem = DOMUtils.getFirstChildElement(manElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        List refs = new ArrayList();
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   100
        int refCount = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        while (refElem != null) {
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   102
            refs.add(new DOMReference(refElem, context, provider));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            refElem = DOMUtils.getNextSiblingElement(refElem);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   104
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   105
            refCount++;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   106
            if (secVal && (refCount > DOMSignedInfo.MAXIMUM_REFERENCE_COUNT)) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   107
                String error = "A maxiumum of " +
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   108
                               DOMSignedInfo.MAXIMUM_REFERENCE_COUNT +
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   109
                               " references per Manifest are allowed with" +
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   110
                               " secure validation";
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   111
                throw new MarshalException(error);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 5506
diff changeset
   112
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        this.references = Collections.unmodifiableList(refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public String getId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public List getReferences() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return references;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        throws MarshalException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        Document ownerDoc = DOMUtils.getOwnerDocument(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        Element manElem = DOMUtils.createElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            (ownerDoc, "Manifest", XMLSignature.XMLNS, dsPrefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        DOMUtils.setAttributeID(manElem, "Id", id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // add references
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        for (int i = 0, size = references.size(); i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            DOMReference ref = (DOMReference) references.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            ref.marshal(manElem, dsPrefix, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        parent.appendChild(manElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (this == o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (!(o instanceof Manifest)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        Manifest oman = (Manifest) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        boolean idsEqual = (id == null ? oman.getId() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            id.equals(oman.getId()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return (idsEqual && references.equals(oman.getReferences()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
}