jdk/src/share/classes/org/jcp/xml/dsig/internal/dom/DOMX509IssuerSerial.java
author mullan
Mon, 22 Sep 2008 10:43:17 -0400
changeset 1337 e8d6cef36199
parent 2 90ce3da70b43
child 1639 a97859015238
permissions -rw-r--r--
6469266: Integrate Apache XMLSec 1.4.2 into JDK 7 Reviewed-by: valeriep
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
/*
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    22
 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
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: DOMX509IssuerSerial.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
import javax.xml.crypto.dsig.keyinfo.X509IssuerSerial;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.security.auth.x500.X500Principal;
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 X509IssuerSerial.
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 DOMX509IssuerSerial extends DOMStructure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    implements X509IssuerSerial {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private final String issuerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final BigInteger serialNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Creates a <code>DOMX509IssuerSerial</code> containing the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * issuer distinguished name/serial number pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @param issuerName the X.509 issuer distinguished name in RFC 2253
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *    String format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @param serialNumber the serial number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @throws IllegalArgumentException if the format of <code>issuerName</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *    is not RFC 2253 compliant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @throws NullPointerException if <code>issuerName</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *    <code>serialNumber</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public DOMX509IssuerSerial(String issuerName, BigInteger serialNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (issuerName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            throw new NullPointerException("issuerName cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (serialNumber == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            throw new NullPointerException("serialNumber cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // check that issuer distinguished name conforms to RFC 2253
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        new X500Principal(issuerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        this.issuerName = issuerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        this.serialNumber = serialNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Creates a <code>DOMX509IssuerSerial</code> from an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param isElem an X509IssuerSerial element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public DOMX509IssuerSerial(Element isElem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Element iNElem = DOMUtils.getFirstChildElement(isElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        Element sNElem = DOMUtils.getNextSiblingElement(iNElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        issuerName = iNElem.getFirstChild().getNodeValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        serialNumber = new BigInteger(sNElem.getFirstChild().getNodeValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public String getIssuerName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return issuerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public BigInteger getSerialNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return serialNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        throws MarshalException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        Document ownerDoc = DOMUtils.getOwnerDocument(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        Element isElem = DOMUtils.createElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            (ownerDoc, "X509IssuerSerial", XMLSignature.XMLNS, dsPrefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        Element inElem = DOMUtils.createElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            (ownerDoc, "X509IssuerName", XMLSignature.XMLNS, dsPrefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        Element snElem = DOMUtils.createElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            (ownerDoc, "X509SerialNumber", XMLSignature.XMLNS, dsPrefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        inElem.appendChild(ownerDoc.createTextNode(issuerName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        snElem.appendChild(ownerDoc.createTextNode(serialNumber.toString()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        isElem.appendChild(inElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        isElem.appendChild(snElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        parent.appendChild(isElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (!(obj instanceof X509IssuerSerial)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        X509IssuerSerial ois = (X509IssuerSerial) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return (issuerName.equals(ois.getIssuerName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            serialNumber.equals(ois.getSerialNumber()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
}