001: /*
002: * $Id: X509IssuerSerial.java,v 1.4 2007/01/08 09:28:57 ashutoshshahi Exp $
003: */
004:
005: /*
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
025: */
026:
027: package com.sun.xml.wss.core.reference;
028:
029: import java.math.BigInteger;
030: import java.security.cert.X509Certificate;
031: import java.util.Iterator;
032: import java.util.logging.Level;
033: import java.util.logging.Logger;
034:
035: import javax.xml.soap.SOAPElement;
036: import javax.xml.soap.SOAPException;
037:
038: import org.w3c.dom.Document;
039:
040: import com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509IssuerSerial;
041: import com.sun.xml.wss.logging.LogDomainConstants;
042: import com.sun.xml.wss.impl.MessageConstants;
043: import com.sun.xml.wss.core.ReferenceElement;
044: import com.sun.xml.wss.impl.XMLUtil;
045: import com.sun.xml.wss.XWSSecurityException;
046:
047: /**
048: * @author Vishal Mahajan
049: */
050: public class X509IssuerSerial extends ReferenceElement {
051:
052: private static Logger log = Logger.getLogger(
053: LogDomainConstants.WSS_API_DOMAIN,
054: LogDomainConstants.WSS_API_DOMAIN_BUNDLE);
055:
056: private XMLX509IssuerSerial delegate;
057: private X509Certificate cert = null;
058:
059: private Document ownerDoc;
060:
061: /**
062: * Constructor X509IssuerSerial
063: *
064: * @param element
065: */
066: public X509IssuerSerial(SOAPElement element)
067: throws XWSSecurityException {
068:
069: boolean throwAnException = false;
070: if (!(element.getLocalName().equals("X509Data") && XMLUtil
071: .inSignatureNS(element))) {
072: throwAnException = true;
073: }
074:
075: SOAPElement issuerSerialElement;
076: try {
077: issuerSerialElement = (SOAPElement) element
078: .getChildElements(
079: soapFactory.createName("X509IssuerSerial",
080: MessageConstants.DSIG_PREFIX,
081: MessageConstants.DSIG_NS)).next();
082: } catch (Exception e) {
083: log.log(Level.SEVERE, "WSS0750.soap.exception",
084: new Object[] { "ds:X509IssuerSerial",
085: e.getMessage() });
086: throw new XWSSecurityException(e);
087: }
088:
089: Iterator issuerNames;
090: try {
091: issuerNames = issuerSerialElement
092: .getChildElements(soapFactory.createName(
093: "X509IssuerName",
094: MessageConstants.DSIG_PREFIX,
095: MessageConstants.DSIG_NS));
096: } catch (SOAPException e) {
097: log
098: .log(Level.SEVERE, "WSS0758.soap.exception",
099: new Object[] { "ds:X509IssuerName",
100: e.getMessage() });
101: throw new XWSSecurityException(e);
102: }
103: if (!issuerNames.hasNext())
104: throwAnException = true;
105: SOAPElement issuerNameElement = (SOAPElement) issuerNames
106: .next();
107: String issuerName = XMLUtil
108: .getFullTextFromChildren(issuerNameElement);
109:
110: Iterator serialNumbers;
111: try {
112: serialNumbers = issuerSerialElement
113: .getChildElements(soapFactory.createName(
114: "X509SerialNumber",
115: MessageConstants.DSIG_PREFIX,
116: MessageConstants.DSIG_NS));
117: } catch (SOAPException e) {
118: log.log(Level.SEVERE, "WSS0758.soap.exception",
119: new Object[] { "ds:X509SerialNumber",
120: e.getMessage() });
121: throw new XWSSecurityException(e);
122: }
123: if (!serialNumbers.hasNext())
124: throwAnException = true;
125: SOAPElement serialNumberElement = (SOAPElement) serialNumbers
126: .next();
127: String serialNumberString = XMLUtil
128: .getFullTextFromChildren(serialNumberElement);
129: BigInteger serialNumber = new BigInteger(serialNumberString);
130:
131: if (throwAnException) {
132: log
133: .log(Level.SEVERE,
134: "WSS0759.error.creating.issuerserial");
135: throw new XWSSecurityException(
136: "Cannot create X509IssuerSerial object out of given element");
137: }
138: ownerDoc = element.getOwnerDocument();
139: delegate = new XMLX509IssuerSerial(ownerDoc, issuerName,
140: serialNumber);
141: }
142:
143: /**
144: * Constructor X509IssuerSerial
145: *
146: * @param doc
147: * @param X509IssuerName
148: * @param X509SerialNumber
149: */
150: public X509IssuerSerial(Document doc, String X509IssuerName,
151: BigInteger X509SerialNumber) {
152: delegate = new XMLX509IssuerSerial(doc, X509IssuerName,
153: X509SerialNumber);
154: ownerDoc = doc;
155: }
156:
157: /**
158: * Constructor X509IssuerSerial
159: *
160: * @param doc
161: * @param X509IssuerName
162: * @param X509SerialNumber
163: */
164: public X509IssuerSerial(Document doc, String X509IssuerName,
165: String X509SerialNumber) {
166: delegate = new XMLX509IssuerSerial(doc, X509IssuerName,
167: X509SerialNumber);
168: ownerDoc = doc;
169: }
170:
171: /**
172: * Constructor X509IssuerSerial
173: *
174: * @param doc
175: * @param X509IssuerName
176: * @param X509SerialNumber
177: */
178: public X509IssuerSerial(Document doc, String X509IssuerName,
179: int X509SerialNumber) {
180: delegate = new XMLX509IssuerSerial(doc, X509IssuerName,
181: X509SerialNumber);
182: ownerDoc = doc;
183: }
184:
185: /**
186: * Constructor X509IssuerSerial
187: *
188: * @param doc
189: * @param x509certificate
190: */
191: public X509IssuerSerial(Document doc,
192: X509Certificate x509certificate) {
193: delegate = new XMLX509IssuerSerial(doc, x509certificate);
194: ownerDoc = doc;
195: }
196:
197: /**
198: * Method getSerialNumber
199: *
200: * @throws XWSSecurityException
201: */
202: public BigInteger getSerialNumber() throws XWSSecurityException {
203: try {
204: return delegate.getSerialNumber();
205: } catch (Exception e) {
206: throw new XWSSecurityException(e);
207: }
208: }
209:
210: /**
211: * Method getSerialNumberInteger
212: *
213: * @throws XWSSecurityException
214: */
215: public int getSerialNumberInteger() throws XWSSecurityException {
216: try {
217: return delegate.getSerialNumberInteger();
218: } catch (Exception e) {
219: throw new XWSSecurityException(e);
220: }
221: }
222:
223: /**
224: * Method getIssuerName
225: *
226: * @throws XWSSecurityException
227: */
228: public String getIssuerName() throws XWSSecurityException {
229: try {
230: return delegate.getIssuerName();
231: } catch (Exception e) {
232: log.log(Level.SEVERE, "WSS0763.exception.issuername",
233: new Object[] { e.getMessage() });
234: throw new XWSSecurityException(e);
235: }
236: }
237:
238: public SOAPElement getAsSoapElement() throws XWSSecurityException {
239: try {
240: SOAPElement issuerSerialElement = (SOAPElement) delegate
241: .getElement();
242: SOAPElement x509DataElement = (SOAPElement) ownerDoc
243: .createElementNS(MessageConstants.DSIG_NS,
244: MessageConstants.DSIG_PREFIX + ":X509Data");
245: x509DataElement.addNamespaceDeclaration(
246: MessageConstants.DSIG_PREFIX,
247: MessageConstants.DSIG_NS);
248: x509DataElement.addChildElement(issuerSerialElement);
249: setSOAPElement(x509DataElement);
250: return x509DataElement;
251: } catch (Exception e) {
252: log.log(Level.SEVERE, "WSS0750.soap.exception",
253: new Object[] { "ds:X509IssuerSerial",
254: e.getMessage() });
255: throw new XWSSecurityException(e);
256: }
257: }
258:
259: public void setCertificate(X509Certificate cert) {
260: this .cert = cert;
261: }
262:
263: public X509Certificate getCertificate() {
264: return cert;
265: }
266: }
|