0001: /*
0002: * $Id: KeyInfoHeaderBlock.java,v 1.5 2007/08/14 12:16:01 kumarjayanti Exp $
0003: */
0004:
0005: /*
0006: * The contents of this file are subject to the terms
0007: * of the Common Development and Distribution License
0008: * (the License). You may not use this file except in
0009: * compliance with the License.
0010: *
0011: * You can obtain a copy of the license at
0012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
0013: * See the License for the specific language governing
0014: * permissions and limitations under the License.
0015: *
0016: * When distributing Covered Code, include this CDDL
0017: * Header Notice in each file and include the License file
0018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
0019: * If applicable, add the following below the CDDL Header,
0020: * with the fields enclosed by brackets [] replaced by
0021: * you own identifying information:
0022: * "Portions Copyrighted [year] [name of copyright owner]"
0023: *
0024: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
0025: */
0026: package com.sun.xml.wss.core;
0027:
0028: import java.security.PublicKey;
0029: import java.util.logging.Level;
0030: import java.util.logging.Logger;
0031:
0032: import javax.xml.soap.SOAPElement;
0033:
0034: import org.w3c.dom.Document;
0035: import org.w3c.dom.Element;
0036: import org.w3c.dom.Node;
0037: import org.w3c.dom.NodeList;
0038:
0039: import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
0040: import com.sun.org.apache.xml.internal.security.keys.KeyInfo;
0041: import com.sun.org.apache.xml.internal.security.keys.content.KeyName;
0042: import com.sun.org.apache.xml.internal.security.keys.content.KeyValue;
0043: import com.sun.org.apache.xml.internal.security.keys.content.MgmtData;
0044: import com.sun.org.apache.xml.internal.security.keys.content.PGPData;
0045: import com.sun.org.apache.xml.internal.security.keys.content.RetrievalMethod;
0046: import com.sun.org.apache.xml.internal.security.keys.content.SPKIData;
0047: import com.sun.org.apache.xml.internal.security.keys.content.X509Data;
0048: import com.sun.org.apache.xml.internal.security.keys.content.keyvalues.DSAKeyValue;
0049: import com.sun.org.apache.xml.internal.security.keys.content.keyvalues.RSAKeyValue;
0050: import com.sun.org.apache.xml.internal.security.transforms.Transforms;
0051: import com.sun.org.apache.xml.internal.security.utils.ElementProxy;
0052: import com.sun.xml.wss.logging.LogDomainConstants;
0053: import com.sun.xml.wss.impl.MessageConstants;
0054: import com.sun.xml.wss.XWSSecurityException;
0055: import com.sun.xml.wss.impl.misc.SecurityHeaderBlockImpl;
0056:
0057: import com.sun.xml.ws.security.trust.elements.BinarySecret;
0058: import com.sun.xml.ws.security.trust.WSTrustElementFactory;
0059: import com.sun.xml.ws.security.trust.WSTrustConstants;
0060: import com.sun.xml.ws.api.security.trust.WSTrustException;
0061:
0062: /**
0063: * Corresponds to the schema representation for a KeyInfo.
0064: <element name="KeyInfo" type="ds:KeyInfoType"/>
0065: <complexType name="KeyInfoType" mixed="true">
0066: <choice maxOccurs="unbounded">
0067: <element ref="ds:KeyName"/>
0068: <element ref="ds:KeyValue"/>
0069: <element ref="ds:RetrievalMethod"/>
0070: <element ref="ds:X509Data"/>
0071: <element ref="ds:PGPData"/>
0072: <element ref="ds:SPKIData"/>
0073: <element ref="ds:MgmtData"/>
0074: <element ref="wsse:SecurityTokenReference"/>
0075: <any processContents="lax" namespace="##other"/>
0076: <!-- (1,1) elements from (0,unbounded) namespaces -->
0077: </choice>
0078: <attribute name="Id" type="ID" use="optional"/>
0079: </complexType>
0080: */
0081: public class KeyInfoHeaderBlock extends SecurityHeaderBlockImpl {
0082:
0083: public static final String SignatureSpecNS = MessageConstants.DSIG_NS;
0084:
0085: public static final String SignatureSpecNSprefix = MessageConstants.DSIG_PREFIX;
0086:
0087: public static final String TAG_KEYINFO = "KeyInfo";
0088:
0089: // delegate ds:KeyInfo member from XML DSIG
0090: KeyInfo delegateKeyInfo = null;
0091:
0092: boolean dirty = false;
0093:
0094: private static Logger log = Logger.getLogger(
0095: LogDomainConstants.WSS_API_DOMAIN,
0096: LogDomainConstants.WSS_API_DOMAIN_BUNDLE);
0097:
0098: /**
0099: * baseURI URI to be used as context for all relative URIs.
0100: * Accepted by all Apache XMLSIG elements
0101: */
0102: String baseURI = null;
0103:
0104: /**
0105: * The Owner Document of this KeyInfo
0106: */
0107: private Document document;
0108:
0109: /**
0110: * constructor that creates an empty KeyInfo
0111: * @param ownerDoc the OwnerDocument of the KeyInfo
0112: */
0113: public KeyInfoHeaderBlock(Document ownerDoc)
0114: throws XWSSecurityException {
0115: try {
0116: this .document = ownerDoc;
0117: delegateKeyInfo = new KeyInfo(ownerDoc);
0118: dirty = true;
0119: setSOAPElement(getAsSoapElement());
0120: } catch (Exception e) {
0121: log.log(Level.SEVERE,
0122: "WSS0318.exception.while.creating.keyinfoblock", e);
0123: throw new XWSSecurityException(e);
0124: }
0125: }
0126:
0127: /**
0128: * constructor that takes Apache KeyInfo
0129: * @param keyinfo the KeyInfo from XML DSIG
0130: */
0131: public KeyInfoHeaderBlock(KeyInfo keyinfo)
0132: throws XWSSecurityException {
0133: this .document = keyinfo.getDocument();
0134: delegateKeyInfo = keyinfo;
0135: dirty = true;
0136: setSOAPElement(getAsSoapElement());
0137: }
0138:
0139: /**
0140: * Method addKeyName.
0141: *
0142: * @param keynameString
0143: */
0144: public void addKeyName(String keynameString) {
0145: delegateKeyInfo.addKeyName(keynameString);
0146: dirty = true;
0147: }
0148:
0149: /**
0150: * Method addBinarySecret
0151: *
0152: * @param secret byte[] representing secret
0153: */
0154: public void addBinarySecret(SOAPElement binarySecret) {
0155: delegateKeyInfo.addUnknownElement(binarySecret);
0156: dirty = true;
0157: }
0158:
0159: /**
0160: * Method add.
0161: *
0162: * @param keyname
0163: */
0164: public void addKeyName(SOAPElement keyname)
0165: throws XWSSecurityException {
0166: try {
0167: KeyName keynm = new KeyName(keyname, null);
0168: delegateKeyInfo.add(keynm);
0169: dirty = true;
0170: } catch (XMLSecurityException e) {
0171: log
0172: .log(Level.SEVERE,
0173: "WSS0319.exception.adding.keyname", e);
0174: throw new XWSSecurityException(e);
0175: }
0176: }
0177:
0178: /**
0179: * Method addKeyValue
0180: *
0181: * @param pk
0182: */
0183: public void addKeyValue(PublicKey pk) {
0184: delegateKeyInfo.addKeyValue(pk);
0185: dirty = true;
0186: }
0187:
0188: /**
0189: * Method addKeyValue
0190: *
0191: * @param unknownKeyValueElement
0192: */
0193: public void addUnknownKeyValue(SOAPElement unknownKeyValueElement) {
0194: delegateKeyInfo.addKeyValue(unknownKeyValueElement);
0195: dirty = true;
0196: }
0197:
0198: /**
0199: * Method add
0200: *
0201: * @param dsakeyvalue
0202: */
0203: public void addDSAKeyValue(SOAPElement dsakeyvalue)
0204: throws XWSSecurityException {
0205: try {
0206: DSAKeyValue dsaKval = new DSAKeyValue(dsakeyvalue, null);
0207: delegateKeyInfo.add(dsaKval);
0208: dirty = true;
0209: } catch (XMLSecurityException e) {
0210: log.log(Level.SEVERE, "WSS0355.error.creating.keyvalue",
0211: new Object[] { "DSA", e.getMessage() });
0212: throw new XWSSecurityException(e);
0213: }
0214: }
0215:
0216: /**
0217: * Method add
0218: *
0219: * @param rsakeyvalue
0220: */
0221: public void addRSAKeyValue(SOAPElement rsakeyvalue)
0222: throws XWSSecurityException {
0223: try {
0224: RSAKeyValue rsaKval = new RSAKeyValue(rsakeyvalue, null);
0225: delegateKeyInfo.add(rsaKval);
0226: dirty = true;
0227: } catch (XMLSecurityException e) {
0228: log.log(Level.SEVERE, "WSS0355.error.creating.keyvalue",
0229: new Object[] { "RSA", e.getMessage() });
0230: throw new XWSSecurityException(e);
0231: }
0232: }
0233:
0234: /**
0235: * Method addKeyValue
0236: *
0237: * @param keyvalue
0238: */
0239: public void addKeyValue(SOAPElement keyvalue)
0240: throws XWSSecurityException {
0241: try {
0242: KeyValue kval = new KeyValue(keyvalue, null);
0243: delegateKeyInfo.add(kval);
0244: dirty = true;
0245: } catch (XMLSecurityException e) {
0246: log.log(Level.SEVERE, "WSS0355.error.creating.keyvalue",
0247: new Object[] { "", e.getMessage() });
0248: throw new XWSSecurityException(e);
0249: }
0250: }
0251:
0252: /**
0253: * Method addMgmtData
0254: *
0255: * @param mgmtdata
0256: */
0257: public void addMgmtData(String mgmtdata) {
0258: delegateKeyInfo.addMgmtData(mgmtdata);
0259: dirty = true;
0260: }
0261:
0262: /**
0263: * Method add
0264: *
0265: * @param mgmtdata
0266: */
0267: public void addMgmtData(SOAPElement mgmtdata)
0268: throws XWSSecurityException {
0269: try {
0270: MgmtData mgmtData = new MgmtData(mgmtdata, null);
0271: delegateKeyInfo.add(mgmtData);
0272: dirty = true;
0273: } catch (XMLSecurityException e) {
0274: throw new XWSSecurityException(e);
0275: }
0276: }
0277:
0278: /**
0279: * Method addPGPData
0280: *
0281: * @param pgpdata
0282: */
0283: public void addPGPData(SOAPElement pgpdata)
0284: throws XWSSecurityException {
0285: try {
0286: PGPData pgpData = new PGPData(pgpdata, null);
0287: delegateKeyInfo.add(pgpData);
0288: dirty = true;
0289: } catch (XMLSecurityException e) {
0290: throw new XWSSecurityException(e);
0291: }
0292: }
0293:
0294: /**
0295: * Method addRetrievalMethod
0296: *
0297: * @param URI
0298: * @param transforms
0299: * @param type
0300: */
0301: public void addRetrievalMethod(String URI, Transforms transforms,
0302: String type) {
0303: delegateKeyInfo.addRetrievalMethod(URI, transforms, type);
0304: dirty = true;
0305: }
0306:
0307: /**
0308: * Method addRetrievalMethod
0309: *
0310: * @param retrievalmethod
0311: */
0312: public void addRetrievalMethod(SOAPElement retrievalmethod)
0313: throws XWSSecurityException {
0314: try {
0315: RetrievalMethod rm = new RetrievalMethod(retrievalmethod,
0316: null);
0317: delegateKeyInfo.add(rm);
0318: dirty = true;
0319: } catch (XMLSecurityException e) {
0320: throw new XWSSecurityException(e);
0321: }
0322: }
0323:
0324: /**
0325: * Method add
0326: *
0327: * @param spkidata
0328: */
0329: public void addSPKIData(SOAPElement spkidata)
0330: throws XWSSecurityException {
0331: try {
0332: SPKIData spki = new SPKIData(spkidata, null);
0333: delegateKeyInfo.add(spki);
0334: dirty = true;
0335: } catch (XMLSecurityException e) {
0336: throw new XWSSecurityException(e);
0337: }
0338: }
0339:
0340: /**
0341: * Method addX509Data
0342: *
0343: * @param x509data
0344: * @throws XWSSecurityException
0345: */
0346: public void addX509Data(SOAPElement x509data)
0347: throws XWSSecurityException {
0348: try {
0349: X509Data x509Data = new X509Data(x509data, null);
0350: delegateKeyInfo.add(x509Data);
0351: dirty = true;
0352: } catch (XMLSecurityException e) {
0353: log.log(Level.SEVERE, "WSS0356.error.creating.x509data", e
0354: .getMessage());
0355: throw new XWSSecurityException(e);
0356: }
0357: }
0358:
0359: /**
0360: * Method addUnknownElement
0361: *
0362: * @param element
0363: */
0364: public void addUnknownElement(SOAPElement element) {
0365: delegateKeyInfo.addUnknownElement(element);
0366: dirty = true;
0367: }
0368:
0369: /**
0370: * Method keyNameCount
0371: *
0372: *
0373: */
0374: public int keyNameCount() {
0375: return delegateKeyInfo.lengthKeyName();
0376: }
0377:
0378: /**
0379: * Method keyValueCount
0380: *
0381: *
0382: */
0383: public int keyValueCount() {
0384: return delegateKeyInfo.lengthKeyValue();
0385: }
0386:
0387: /**
0388: * Method mgmtDataCount
0389: *
0390: *
0391: */
0392: public int mgmtDataCount() {
0393: return delegateKeyInfo.lengthMgmtData();
0394: }
0395:
0396: /**
0397: * Method pgpDataCount
0398: *
0399: *
0400: */
0401: public int pgpDataCount() {
0402: return delegateKeyInfo.lengthPGPData();
0403: }
0404:
0405: /**
0406: * Method retrievalMethodCount
0407: *
0408: *
0409: */
0410: public int retrievalMethodCount() {
0411: return delegateKeyInfo.lengthRetrievalMethod();
0412: }
0413:
0414: /**
0415: * Method spkiDataCount
0416: *
0417: *
0418: */
0419: public int spkiDataCount() {
0420: return delegateKeyInfo.lengthSPKIData();
0421: }
0422:
0423: /**
0424: * Method x509DataCount
0425: *
0426: *
0427: */
0428: public int x509DataCount() {
0429: return delegateKeyInfo.lengthX509Data();
0430: }
0431:
0432: /**
0433: * Method unknownElementCount
0434: *
0435: *
0436: */
0437: public int unknownElementCount() {
0438: return delegateKeyInfo.lengthUnknownElement();
0439: }
0440:
0441: /**
0442: * Method getKeyName
0443: *
0444: * @param index
0445: * 0 is the lowest index
0446: *
0447: * @throws XWSSecurityException
0448: */
0449: public SOAPElement getKeyName(int index)
0450: throws XWSSecurityException {
0451: try {
0452: return convertToSoapElement(delegateKeyInfo
0453: .itemKeyName(index));
0454: } catch (XMLSecurityException e) {
0455: log.log(Level.SEVERE, "WSS0320.exception.getting.keyname",
0456: e);
0457: throw new XWSSecurityException(e);
0458: }
0459: }
0460:
0461: /**
0462: * Method getKeyNameString
0463: *
0464: * @param index
0465: * 0 is the lowest index
0466: *
0467: * @throws XWSSecurityException
0468: */
0469: public String getKeyNameString(int index)
0470: throws XWSSecurityException {
0471: try {
0472: return (delegateKeyInfo.itemKeyName(index)).getKeyName();
0473: } catch (XMLSecurityException e) {
0474: log.log(Level.SEVERE, "WSS0320.exception.getting.keyname",
0475: e);
0476: throw new XWSSecurityException(e);
0477: }
0478: }
0479:
0480: /**
0481: * Method getKeyValueElement
0482: *
0483: * @param index
0484: * 0 is the lowest index
0485: *
0486: * @throws XWSSecurityException
0487: */
0488: public SOAPElement getKeyValueElement(int index)
0489: throws XWSSecurityException {
0490: try {
0491: return convertToSoapElement(delegateKeyInfo
0492: .itemKeyValue(index));
0493: } catch (XMLSecurityException e) {
0494: throw new XWSSecurityException(e);
0495: }
0496: }
0497:
0498: /**
0499: * Method getKeyValue
0500: *
0501: * @param index
0502: * 0 is the lowest index
0503: *
0504: * @throws XWSSecurityException
0505: */
0506: public KeyValue getKeyValue(int index) throws XWSSecurityException {
0507: try {
0508: return delegateKeyInfo.itemKeyValue(index);
0509: } catch (XMLSecurityException e) {
0510: log.log(Level.SEVERE, "WSS0357.error.getting.keyvalue",
0511: new Object[] { Integer.valueOf(index),
0512: e.getMessage() });
0513: throw new XWSSecurityException(e);
0514: }
0515: }
0516:
0517: /**
0518: * Method getMgmtData
0519: *
0520: * @param index
0521: * 0 is the lowest index
0522: *
0523: * @throws XWSSecurityException
0524: */
0525: public SOAPElement getMgmtData(int index)
0526: throws XWSSecurityException {
0527: try {
0528: return convertToSoapElement(delegateKeyInfo
0529: .itemMgmtData(index));
0530: } catch (XMLSecurityException e) {
0531: throw new XWSSecurityException(e);
0532: }
0533: }
0534:
0535: /**
0536: * Method getPGPData
0537: *
0538: * @param index
0539: * 0 is the lowest index
0540: *
0541: * @throws XWSSecurityException
0542: */
0543: public SOAPElement getPGPData(int index)
0544: throws XWSSecurityException {
0545: try {
0546: return convertToSoapElement(delegateKeyInfo
0547: .itemPGPData(index));
0548: } catch (XMLSecurityException e) {
0549: throw new XWSSecurityException(e);
0550: }
0551: }
0552:
0553: /**
0554: * Method getRetrievalMethod
0555: *
0556: * @param index
0557: * 0 is the lowest index
0558: *
0559: * @throws XWSSecurityException
0560: */
0561: public SOAPElement getRetrievalMethod(int index)
0562: throws XWSSecurityException {
0563: try {
0564: return convertToSoapElement(delegateKeyInfo
0565: .itemRetrievalMethod(index));
0566: } catch (XMLSecurityException e) {
0567: throw new XWSSecurityException(e);
0568: }
0569: }
0570:
0571: /**
0572: * Method getSPKIData
0573: *
0574: * @param index
0575: * 0 is the lowest index
0576: *
0577: * @throws XWSSecurityException
0578: */
0579: public SOAPElement getSPKIData(int index)
0580: throws XWSSecurityException {
0581: try {
0582: return convertToSoapElement(delegateKeyInfo
0583: .itemSPKIData(index));
0584: } catch (XMLSecurityException e) {
0585: throw new XWSSecurityException(e);
0586: }
0587: }
0588:
0589: /**
0590: * Method getX509DataElement
0591: *
0592: * @param index
0593: * 0 is the lowest index
0594: *
0595: * @throws XWSSecurityException
0596: */
0597: public SOAPElement getX509DataElement(int index)
0598: throws XWSSecurityException {
0599: try {
0600: return convertToSoapElement(delegateKeyInfo
0601: .itemX509Data(index));
0602: } catch (XMLSecurityException e) {
0603: throw new XWSSecurityException(e);
0604: }
0605: }
0606:
0607: /**
0608: * Method getX509Data
0609: *
0610: * @param index
0611: * 0 is the lowest index
0612: *
0613: * @throws XWSSecurityException
0614: */
0615: public X509Data getX509Data(int index) throws XWSSecurityException {
0616: try {
0617: return delegateKeyInfo.itemX509Data(index);
0618: } catch (XMLSecurityException e) {
0619: log.log(Level.SEVERE, "WSS0358.error.getting.x509data",
0620: new Object[] { Integer.valueOf(index),
0621: e.getMessage() });
0622: throw new XWSSecurityException(e);
0623: }
0624: }
0625:
0626: /**
0627: * Method getUnknownElement
0628: *
0629: * @param index
0630: * 0 is the lowest index
0631: *
0632: */
0633: public SOAPElement getUnknownElement(int index)
0634: throws XWSSecurityException {
0635: // There is bug in Apache KeyInfo.itemUnknownElement()
0636: // the lowest index for it is 1 (not 0).
0637: try {
0638: Element unknownElem = delegateKeyInfo
0639: .itemUnknownElement(index + 1);
0640: if (unknownElem instanceof SOAPElement)
0641: return (SOAPElement) unknownElem;
0642: else
0643: return (SOAPElement) document.importNode(unknownElem,
0644: true);
0645: } catch (Exception e) {
0646: throw new XWSSecurityException(e);
0647: }
0648: }
0649:
0650: /**
0651: * Method containsKeyName
0652: *
0653: *
0654: */
0655: public boolean containsKeyName() {
0656: return delegateKeyInfo.containsKeyName();
0657: }
0658:
0659: /**
0660: * Method containsKeyValue
0661: *
0662: *
0663: */
0664: public boolean containsKeyValue() {
0665: return delegateKeyInfo.containsKeyValue();
0666: }
0667:
0668: /**
0669: * Method containsMgmtData
0670: *
0671: *
0672: */
0673: public boolean containsMgmtData() {
0674: return delegateKeyInfo.containsMgmtData();
0675: }
0676:
0677: /**
0678: * Method containsPGPData
0679: *
0680: *
0681: */
0682: public boolean containsPGPData() {
0683: return delegateKeyInfo.containsPGPData();
0684: }
0685:
0686: /**
0687: * Method containsRetrievalMethod
0688: *
0689: *
0690: */
0691: public boolean containsRetrievalMethod() {
0692: return delegateKeyInfo.containsRetrievalMethod();
0693: }
0694:
0695: /**
0696: * Method containsSPKIData
0697: *
0698: *
0699: */
0700: public boolean containsSPKIData() {
0701: return delegateKeyInfo.containsSPKIData();
0702: }
0703:
0704: /**
0705: * Method containsUnknownElement
0706: *
0707: *
0708: */
0709: public boolean containsUnknownElement() {
0710: return delegateKeyInfo.containsUnknownElement();
0711: }
0712:
0713: /**
0714: * Method containsX509Data
0715: *
0716: *
0717: */
0718: public boolean containsX509Data() {
0719: return delegateKeyInfo.containsX509Data();
0720: }
0721:
0722: // WSS spec allows a SecurityTokenReference element to be a direct
0723: // child of ds:KeyInfo
0724:
0725: /**
0726: * Method addSecurityTokenReference
0727: *
0728: * @param reference
0729: */
0730: public void addSecurityTokenReference(
0731: SecurityTokenReference reference)
0732: throws XWSSecurityException {
0733: delegateKeyInfo.addUnknownElement(reference.getAsSoapElement());
0734: dirty = true;
0735: }
0736:
0737: /**
0738: * Method getSecurityTokenReference
0739: *
0740: * @param index
0741: *
0742: * @throws XWSSecurityException
0743: *
0744: * @return the index^th token reference element from the KeyInfo
0745: * 0 is the lowest index.
0746: */
0747: public SecurityTokenReference getSecurityTokenReference(int index)
0748: throws XWSSecurityException {
0749: org.w3c.dom.Element delegateElement = delegateKeyInfo
0750: .getElement();
0751: int res = 0;
0752: NodeList nl = delegateElement.getChildNodes();
0753:
0754: for (int j = 0; j < nl.getLength(); j++) {
0755: Node current = nl.item(j);
0756: if (current.getNodeType() == Node.ELEMENT_NODE) {
0757:
0758: String lName = current.getLocalName();
0759: String nspac = current.getNamespaceURI();
0760:
0761: if (lName
0762: .equals(MessageConstants.WSSE_SECURITY_TOKEN_REFERENCE_LNAME)
0763: && nspac.equals(MessageConstants.WSSE_NS)) {
0764: if (res == index) {
0765: return new SecurityTokenReference(
0766: (SOAPElement) current);
0767: }
0768: res++;
0769: }
0770: }
0771: }
0772: return null;
0773: }
0774:
0775: /**
0776: * Method securityTokenReferenceCount
0777: *
0778: * @return the count of security token references
0779: */
0780: public int securityTokenReferenceCount() {
0781: org.w3c.dom.Element delegateElement = delegateKeyInfo
0782: .getElement();
0783: int res = 0;
0784: NodeList nl = delegateElement.getChildNodes();
0785:
0786: for (int j = 0; j < nl.getLength(); j++) {
0787: Node current = nl.item(j);
0788: if ((current.getNodeType() == Node.ELEMENT_NODE)
0789: && MessageConstants.WSSE_SECURITY_TOKEN_REFERENCE_LNAME
0790: .equals(current.getLocalName())
0791: && MessageConstants.WSSE_NS.equals(current
0792: .getNamespaceURI())) {
0793:
0794: res++;
0795: }
0796: }
0797: return res;
0798: }
0799:
0800: /**
0801: * Method containsSecurityTokenReference
0802: *
0803: * @return true if this KeyInfo contains wsse:SecurityTokenReference's
0804: */
0805: public boolean containsSecurityTokenReference() {
0806: return (securityTokenReferenceCount() > 0);
0807: }
0808:
0809: /**
0810: * Method addEncryptedKey
0811: *
0812: * @param reference
0813: */
0814: public void addEncryptedKey(EncryptedKeyToken reference)
0815: throws XWSSecurityException {
0816: delegateKeyInfo.addUnknownElement(reference.getAsSoapElement());
0817: dirty = true;
0818: }
0819:
0820: /**
0821: * Method getEncryptedKey
0822: *
0823: * @param index
0824: *
0825: * @throws XWSSecurityException
0826: *
0827: * @return the index^th token reference element from the KeyInfo
0828: * 0 is the lowest index.
0829: */
0830: public EncryptedKeyToken getEncryptedKey(int index)
0831: throws XWSSecurityException {
0832: org.w3c.dom.Element delegateElement = delegateKeyInfo
0833: .getElement();
0834: int res = 0;
0835: NodeList nl = delegateElement.getChildNodes();
0836:
0837: for (int j = 0; j < nl.getLength(); j++) {
0838: Node current = nl.item(j);
0839: if (current.getNodeType() == Node.ELEMENT_NODE) {
0840:
0841: String lName = current.getLocalName();
0842: String nspac = current.getNamespaceURI();
0843:
0844: if (lName
0845: .equals(MessageConstants.XENC_ENCRYPTED_KEY_LNAME)
0846: && nspac.equals(MessageConstants.XENC_NS)) {
0847: if (res == index) {
0848: return new EncryptedKeyToken(
0849: (SOAPElement) current);
0850: }
0851: res++;
0852: }
0853: }
0854: }
0855: return null;
0856: }
0857:
0858: /**
0859: * Method encryptedKeyTokenCount
0860: *
0861: * @return the count of encrypted key token references
0862: */
0863: public int encryptedKeyTokenCount() {
0864: org.w3c.dom.Element delegateElement = delegateKeyInfo
0865: .getElement();
0866: int res = 0;
0867: NodeList nl = delegateElement.getChildNodes();
0868:
0869: for (int j = 0; j < nl.getLength(); j++) {
0870: Node current = nl.item(j);
0871: if ((current.getNodeType() == Node.ELEMENT_NODE)
0872: && MessageConstants.XENC_ENCRYPTED_KEY_LNAME
0873: .equals(current.getLocalName())
0874: && MessageConstants.XENC_NS.equals(current
0875: .getNamespaceURI())) {
0876:
0877: res++;
0878: }
0879: }
0880: return res;
0881: }
0882:
0883: /**
0884: * Method containsEncryptedKeyToken
0885: *
0886: * @return true if this KeyInfo contains wsse:SecurityTokenReference's
0887: */
0888: public boolean containsEncryptedKeyToken() {
0889: return (encryptedKeyTokenCount() > 0);
0890: }
0891:
0892: public BinarySecret getBinarySecret(int index)
0893: throws XWSSecurityException {
0894: org.w3c.dom.Element delegateElement = delegateKeyInfo
0895: .getElement();
0896: int res = 0;
0897: NodeList nl = delegateElement.getChildNodes();
0898:
0899: for (int j = 0; j < nl.getLength(); j++) {
0900: Node current = nl.item(j);
0901: if (current.getNodeType() == Node.ELEMENT_NODE) {
0902:
0903: String lName = current.getLocalName();
0904: String nspac = current.getNamespaceURI();
0905:
0906: if (lName.equals(MessageConstants.BINARY_SECRET_LNAME)
0907: && nspac.equals(WSTrustConstants.WST_NAMESPACE)) {
0908: if (res == index) {
0909: try {
0910: return WSTrustElementFactory.newInstance()
0911: .createBinarySecret(
0912: (SOAPElement) current);
0913: } catch (WSTrustException ex) {
0914: throw new XWSSecurityException(ex);
0915: }
0916: }
0917: res++;
0918: }
0919: }
0920: }
0921: return null;
0922: }
0923:
0924: /**
0925: * Method binarySecretCount
0926: *
0927: * @return the count of binarySecret tokens
0928: */
0929: public int binarySecretCount() {
0930: org.w3c.dom.Element delegateElement = delegateKeyInfo
0931: .getElement();
0932: int res = 0;
0933: NodeList nl = delegateElement.getChildNodes();
0934:
0935: for (int j = 0; j < nl.getLength(); j++) {
0936: Node current = nl.item(j);
0937: if ((current.getNodeType() == Node.ELEMENT_NODE)
0938: && MessageConstants.BINARY_SECRET_LNAME
0939: .equals(current.getLocalName())
0940: && WSTrustConstants.WST_NAMESPACE.equals(current
0941: .getNamespaceURI())) {
0942:
0943: res++;
0944: }
0945: }
0946: return res;
0947: }
0948:
0949: /**
0950: * Method containsBinarySecret
0951: *
0952: * @return true if this KeyInfo contains BinarySecret
0953: */
0954: public boolean containsBinarySecret() {
0955: return (binarySecretCount() > 0);
0956: }
0957:
0958: /**
0959: * Method setId
0960: */
0961: public void setId(String id) {
0962: delegateKeyInfo.setId(id);
0963: }
0964:
0965: /**
0966: * Method getId
0967: *
0968: * @return the id
0969: */
0970: public String getId() {
0971: return delegateKeyInfo.getId();
0972: }
0973:
0974: /**
0975: * Method getKeyInfo
0976: *
0977: * @return the XML DSIG KeyInfo which is wrapped by this class
0978: */
0979: public final KeyInfo getKeyInfo() {
0980: return delegateKeyInfo;
0981: }
0982:
0983: /**
0984: * Method setBaseURI : BaseURI accepted by Apache KeyInfo Ctor
0985: * @param uri Base URI to be used as context for all relative URIs.
0986: */
0987: public void setBaseURI(String uri) {
0988: baseURI = uri;
0989: }
0990:
0991: /**
0992: * Method to return the KeyInfo as a SOAPElement.
0993: *
0994: * @return SOAPElement
0995: * @throws XWSSecurityException
0996: * If owner soap document is not set.
0997: * @see #setDocument(Document)
0998: */
0999: public SOAPElement getAsSoapElement() throws XWSSecurityException {
1000: if (document == null) {
1001: throw new XWSSecurityException("Document not set");
1002: }
1003: if (dirty) {
1004: setSOAPElement(convertToSoapElement(delegateKeyInfo));
1005: dirty = false;
1006: }
1007: return delegateElement;
1008: }
1009:
1010: /**
1011: * setDocument
1012: * @param doc The owner Document of this KeyInfo
1013: */
1014: public void setDocument(Document doc) {
1015: this .document = doc;
1016: }
1017:
1018: // this constructor should be protected.
1019: /**
1020: * parse and create the KeyInfo element
1021: * @param element the KeyInfo element
1022: * NOTE : this constructor assumes a fully initialized XML KeyInfo
1023: * No additions are allowed on the keyinfo, only we can get existing
1024: * values. For example addkeyName() will have no impact on the KeyInfo
1025: * will not append a KeyName child to the KeyInfo.
1026: */
1027: public KeyInfoHeaderBlock(SOAPElement element)
1028: throws XWSSecurityException {
1029: super (element);
1030: try {
1031: // The BaseURI in Apache KeyInfo is seems optional
1032: // However the purpose of it is not clear
1033: this .document = element.getOwnerDocument();
1034: delegateKeyInfo = new KeyInfo(element, baseURI);
1035: } catch (XMLSecurityException e) {
1036: log.log(Level.SEVERE,
1037: "WSS0318.exception.while.creating.keyinfoblock", e);
1038: throw new XWSSecurityException(e);
1039: }
1040: }
1041:
1042: /**
1043: * This method should be called when changes are made inside an object
1044: * through its reference obtained from any of the get methods of this
1045: * class. As an example, if getKeyInfo() call is made and then changes are made
1046: * inside the keyInfo, this method should be called to reflect changes
1047: * when getAsSoapElement() is called finally.
1048: *
1049: */
1050: public void saveChanges() {
1051: dirty = true;
1052: }
1053:
1054: public static SecurityHeaderBlock fromSoapElement(
1055: SOAPElement element) throws XWSSecurityException {
1056: return SecurityHeaderBlockImpl.fromSoapElement(element,
1057: KeyInfoHeaderBlock.class);
1058: }
1059:
1060: private SOAPElement convertToSoapElement(ElementProxy proxy)
1061: throws XWSSecurityException {
1062: try {
1063: Element elem = proxy.getElement();
1064: if (elem instanceof SOAPElement)
1065: return (SOAPElement) elem;
1066: else
1067: return (SOAPElement) document.importNode(elem, true);
1068: } catch (Exception e) {
1069: log.log(Level.SEVERE,
1070: "WSS0321.exception.converting.keyinfo.tosoapelem",
1071: e);
1072: throw new XWSSecurityException(e);
1073: }
1074: }
1075:
1076: public void addX509Data(X509Data x509Data)
1077: throws XWSSecurityException {
1078: try {
1079: delegateKeyInfo.add(x509Data);
1080: dirty = true;
1081: } catch (Exception e) {
1082: log.log(Level.SEVERE, "WSS0359.error.adding.x509data", e
1083: .getMessage());
1084: throw new XWSSecurityException(e);
1085: }
1086: }
1087: }
|