J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | /* |
| 26 | * $Id: DOMX509IssuerSerial.java,v 1.13 2005/05/10 18:15:35 mullan Exp $ |
| 27 | */ |
| 28 | package org.jcp.xml.dsig.internal.dom; |
| 29 | |
| 30 | import javax.xml.crypto.*; |
| 31 | import javax.xml.crypto.dom.DOMCryptoContext; |
| 32 | import javax.xml.crypto.dsig.*; |
| 33 | import javax.xml.crypto.dsig.keyinfo.X509IssuerSerial; |
| 34 | |
| 35 | import java.math.BigInteger; |
| 36 | import javax.security.auth.x500.X500Principal; |
| 37 | import org.w3c.dom.Document; |
| 38 | import org.w3c.dom.Element; |
| 39 | import org.w3c.dom.Node; |
| 40 | |
| 41 | /** |
| 42 | * DOM-based implementation of X509IssuerSerial. |
| 43 | * |
| 44 | * @author Sean Mullan |
| 45 | */ |
| 46 | public final class DOMX509IssuerSerial extends DOMStructure |
| 47 | implements X509IssuerSerial { |
| 48 | |
| 49 | private final String issuerName; |
| 50 | private final BigInteger serialNumber; |
| 51 | |
| 52 | /** |
| 53 | * Creates a <code>DOMX509IssuerSerial</code> containing the specified |
| 54 | * issuer distinguished name/serial number pair. |
| 55 | * |
| 56 | * @param issuerName the X.509 issuer distinguished name in RFC 2253 |
| 57 | * String format |
| 58 | * @param serialNumber the serial number |
| 59 | * @throws IllegalArgumentException if the format of <code>issuerName</code> |
| 60 | * is not RFC 2253 compliant |
| 61 | * @throws NullPointerException if <code>issuerName</code> or |
| 62 | * <code>serialNumber</code> is <code>null</code> |
| 63 | */ |
| 64 | public DOMX509IssuerSerial(String issuerName, BigInteger serialNumber) { |
| 65 | if (issuerName == null) { |
| 66 | throw new NullPointerException("issuerName cannot be null"); |
| 67 | } |
| 68 | if (serialNumber == null) { |
| 69 | throw new NullPointerException("serialNumber cannot be null"); |
| 70 | } |
| 71 | // check that issuer distinguished name conforms to RFC 2253 |
| 72 | new X500Principal(issuerName); |
| 73 | this.issuerName = issuerName; |
| 74 | this.serialNumber = serialNumber; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Creates a <code>DOMX509IssuerSerial</code> from an element. |
| 79 | * |
| 80 | * @param isElem an X509IssuerSerial element |
| 81 | */ |
| 82 | public DOMX509IssuerSerial(Element isElem) { |
| 83 | Element iNElem = DOMUtils.getFirstChildElement(isElem); |
| 84 | Element sNElem = DOMUtils.getNextSiblingElement(iNElem); |
| 85 | issuerName = iNElem.getFirstChild().getNodeValue(); |
| 86 | serialNumber = new BigInteger(sNElem.getFirstChild().getNodeValue()); |
| 87 | } |
| 88 | |
| 89 | public String getIssuerName() { |
| 90 | return issuerName; |
| 91 | } |
| 92 | |
| 93 | public BigInteger getSerialNumber() { |
| 94 | return serialNumber; |
| 95 | } |
| 96 | |
| 97 | public void marshal(Node parent, String dsPrefix, DOMCryptoContext context) |
| 98 | throws MarshalException { |
| 99 | Document ownerDoc = DOMUtils.getOwnerDocument(parent); |
| 100 | |
| 101 | Element isElem = DOMUtils.createElement |
| 102 | (ownerDoc, "X509IssuerSerial", XMLSignature.XMLNS, dsPrefix); |
| 103 | Element inElem = DOMUtils.createElement |
| 104 | (ownerDoc, "X509IssuerName", XMLSignature.XMLNS, dsPrefix); |
| 105 | Element snElem = DOMUtils.createElement |
| 106 | (ownerDoc, "X509SerialNumber", XMLSignature.XMLNS, dsPrefix); |
| 107 | inElem.appendChild(ownerDoc.createTextNode(issuerName)); |
| 108 | snElem.appendChild(ownerDoc.createTextNode(serialNumber.toString())); |
| 109 | isElem.appendChild(inElem); |
| 110 | isElem.appendChild(snElem); |
| 111 | parent.appendChild(isElem); |
| 112 | } |
| 113 | |
| 114 | public boolean equals(Object obj) { |
| 115 | if (this == obj) { |
| 116 | return true; |
| 117 | } |
| 118 | if (!(obj instanceof X509IssuerSerial)) { |
| 119 | return false; |
| 120 | } |
| 121 | X509IssuerSerial ois = (X509IssuerSerial) obj; |
| 122 | return (issuerName.equals(ois.getIssuerName()) && |
| 123 | serialNumber.equals(ois.getSerialNumber())); |
| 124 | } |
| 125 | } |