src/java.xml.crypto/share/classes/org/jcp/xml/dsig/internal/dom/DOMX509IssuerSerial.java
author mullan
Tue, 05 Mar 2019 08:24:58 -0500
changeset 53998 d870bb08194a
parent 50614 3810c9a2efa1
child 54731 81de17a33575
permissions -rw-r--r--
8217878: ENVELOPING XML signature no longer works in JDK 11 8218629: XML Digital Signature throws NAMESPACE_ERR exception on OpenJDK 11, works 8/9/10 Summary: Backout and restore previous XML signature marshalling implementation Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     2
 * reserved comment block
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
     5
/**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
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: 5506
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: 5506
diff changeset
     8
 * distributed with this work for additional information
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
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: 5506
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: 5506
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: 5506
diff changeset
    12
 * with the License. You may obtain a copy of the License at
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    13
 *
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    14
 * http://www.apache.org/licenses/LICENSE-2.0
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    15
 *
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
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: 5506
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: 5506
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: 5506
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: 5506
diff changeset
    20
 * specific language governing permissions and limitations
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    21
 * under the License.
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    22
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    23
/*
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    24
 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    25
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    26
/*
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    27
 * $Id: DOMX509IssuerSerial.java 1788465 2017-03-24 15:10:51Z coheigea $
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
package org.jcp.xml.dsig.internal.dom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    31
import javax.xml.crypto.MarshalException;
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
    32
import javax.xml.crypto.dom.DOMCryptoContext;
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    33
import javax.xml.crypto.dsig.XMLSignature;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.xml.crypto.dsig.keyinfo.X509IssuerSerial;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.math.BigInteger;
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    37
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.security.auth.x500.X500Principal;
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
    39
import org.w3c.dom.Document;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import org.w3c.dom.Element;
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
    41
import org.w3c.dom.Node;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * DOM-based implementation of X509IssuerSerial.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
    47
public final class DOMX509IssuerSerial extends DOMStructure
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    implements X509IssuerSerial {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private final String issuerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private final BigInteger serialNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    54
     * Creates a {@code DOMX509IssuerSerial} containing the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * issuer distinguished name/serial number pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @param issuerName the X.509 issuer distinguished name in RFC 2253
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *    String format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param serialNumber the serial number
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    60
     * @throws IllegalArgumentException if the format of {@code issuerName}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *    is not RFC 2253 compliant
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    62
     * @throws NullPointerException if {@code issuerName} or
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    63
     *    {@code serialNumber} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public DOMX509IssuerSerial(String issuerName, BigInteger serialNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (issuerName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            throw new NullPointerException("issuerName cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (serialNumber == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            throw new NullPointerException("serialNumber cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // check that issuer distinguished name conforms to RFC 2253
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        new X500Principal(issuerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.issuerName = issuerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.serialNumber = serialNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    79
     * Creates a {@code DOMX509IssuerSerial} from an element.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param isElem an X509IssuerSerial element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
    83
    public DOMX509IssuerSerial(Element isElem) throws MarshalException {
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
    84
        Element iNElem = DOMUtils.getFirstChildElement(isElem,
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    85
                                                       "X509IssuerName",
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    86
                                                       XMLSignature.XMLNS);
19051
6c0cfc00b3ed 8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents: 18780
diff changeset
    87
        Element sNElem = DOMUtils.getNextSiblingElement(iNElem,
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    88
                                                        "X509SerialNumber",
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    89
                                                        XMLSignature.XMLNS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        issuerName = iNElem.getFirstChild().getNodeValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        serialNumber = new BigInteger(sNElem.getFirstChild().getNodeValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public String getIssuerName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return issuerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public BigInteger getSerialNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return serialNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   102
    @Override
53998
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   103
    public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   104
        throws MarshalException
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   105
    {
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   106
        Document ownerDoc = DOMUtils.getOwnerDocument(parent);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   107
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   108
        Element isElem = DOMUtils.createElement(ownerDoc, "X509IssuerSerial",
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   109
                                                XMLSignature.XMLNS, dsPrefix);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   110
        Element inElem = DOMUtils.createElement(ownerDoc, "X509IssuerName",
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   111
                                                XMLSignature.XMLNS, dsPrefix);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   112
        Element snElem = DOMUtils.createElement(ownerDoc, "X509SerialNumber",
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   113
                                                XMLSignature.XMLNS, dsPrefix);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   114
        inElem.appendChild(ownerDoc.createTextNode(issuerName));
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   115
        snElem.appendChild(ownerDoc.createTextNode(serialNumber.toString()));
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   116
        isElem.appendChild(inElem);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   117
        isElem.appendChild(snElem);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   118
        parent.appendChild(isElem);
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   119
    }
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   120
d870bb08194a 8217878: ENVELOPING XML signature no longer works in JDK 11
mullan
parents: 50614
diff changeset
   121
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (!(obj instanceof X509IssuerSerial)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   129
        X509IssuerSerial ois = (X509IssuerSerial)obj;
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   130
        return issuerName.equals(ois.getIssuerName()) &&
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
   131
                serialNumber.equals(ois.getSerialNumber());
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   132
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   133
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   134
    @Override
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   135
    public int hashCode() {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   136
        int result = 17;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   137
        result = 31 * result + issuerName.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   138
        result = 31 * result + serialNumber.hashCode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   139
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   140
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}