001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the License). You may not use this file except in
005: * compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * Header Notice in each file and include the License file
014: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * If applicable, add the following below the CDDL Header,
016: * with the fields enclosed by brackets [] replaced by
017: * you own identifying information:
018: * "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
021: */
022:
023: package com.sun.xml.ws.security.opt.impl.keyinfo;
024:
025: import com.sun.xml.ws.api.SOAPVersion;
026: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
027: import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
028: import com.sun.xml.ws.security.opt.api.reference.DirectReference;
029: import com.sun.xml.ws.security.opt.impl.util.JAXBUtil;
030: import com.sun.xml.ws.security.secconv.impl.bindings.DerivedKeyTokenType;
031: import com.sun.xml.ws.security.secext10.KeyIdentifierType;
032: import com.sun.xml.ws.security.secext10.SecurityTokenReferenceType;
033: import com.sun.xml.wss.impl.MessageConstants;
034: import com.sun.xml.wss.impl.XWSSecurityRuntimeException;
035:
036: import java.io.OutputStream;
037: import java.math.BigInteger;
038: import java.util.HashMap;
039: import java.util.Iterator;
040: import java.util.List;
041: import java.util.Map;
042: import javax.xml.bind.JAXBElement;
043: import javax.xml.bind.JAXBException;
044: import javax.xml.bind.Marshaller;
045: import javax.xml.stream.XMLStreamException;
046:
047: /**
048: *
049: * @author K.Venugopal@sun.com
050: */
051: public class DerivedKey implements
052: com.sun.xml.ws.security.opt.api.keyinfo.DerivedKeyToken,
053: SecurityHeaderElement, SecurityElementWriter {
054:
055: private DerivedKeyTokenType derivedKey = null;
056: private SOAPVersion soapVersion = SOAPVersion.SOAP_11;
057: private String refId = "";
058:
059: /** Creates a new instance of DerivedKey */
060: public DerivedKey(DerivedKeyTokenType dkt, SOAPVersion soapVersion) {
061: this .derivedKey = dkt;
062: this .soapVersion = soapVersion;
063: }
064:
065: public DerivedKey(DerivedKeyTokenType dkt, SOAPVersion soapVersion,
066: String refId) {
067: this .derivedKey = dkt;
068: this .soapVersion = soapVersion;
069: this .refId = refId;
070: }
071:
072: public String getAlgorithm() {
073: return derivedKey.getAlgorithm();
074: }
075:
076: public BigInteger getGeneration() {
077: return derivedKey.getGeneration();
078: }
079:
080: public String getId() {
081: return derivedKey.getId();
082: }
083:
084: public String getLabel() {
085: return derivedKey.getLabel();
086: }
087:
088: public BigInteger getLength() {
089: return derivedKey.getLength();
090: }
091:
092: public byte[] getNonce() {
093: return derivedKey.getNonce();
094: }
095:
096: public BigInteger getOffset() {
097: return derivedKey.getOffset();
098: }
099:
100: public SecurityTokenReferenceType getSecurityTokenReference() {
101: return derivedKey.getSecurityTokenReference();
102: }
103:
104: public void setAlgorithm(String value) {
105: derivedKey.setAlgorithm(value);
106: }
107:
108: public void setGeneration(BigInteger value) {
109: derivedKey.setGeneration(value);
110: }
111:
112: public void setId(String value) {
113: derivedKey.setId(value);
114: }
115:
116: public void setLabel(String value) {
117: derivedKey.setLabel(value);
118: }
119:
120: public void setLength(BigInteger value) {
121: derivedKey.setLength(value);
122: }
123:
124: public void setNonce(byte[] value) {
125: derivedKey.setNonce(value);
126: }
127:
128: public void setOffset(BigInteger value) {
129: derivedKey.setOffset(value);
130: }
131:
132: public void setSecurityTokenReference(
133: SecurityTokenReferenceType value) {
134: derivedKey.setSecurityTokenReference(value);
135: }
136:
137: public String getNamespaceURI() {
138: return MessageConstants.WSSC_NS;
139: }
140:
141: public String getLocalPart() {
142: return MessageConstants.DERIVEDKEY_TOKEN_LNAME;
143: }
144:
145: public javax.xml.stream.XMLStreamReader readHeader()
146: throws javax.xml.stream.XMLStreamException {
147: throw new UnsupportedOperationException();
148: }
149:
150: public void writeTo(OutputStream os) {
151: try {
152: JAXBElement<DerivedKeyTokenType> dkt = new com.sun.xml.ws.security.secconv.impl.bindings.ObjectFactory()
153: .createDerivedKeyToken(derivedKey);
154: Marshaller writer = getMarshaller();
155: writer.marshal(dkt, os);
156: } catch (javax.xml.bind.JAXBException ex) {
157: throw new XWSSecurityRuntimeException(ex);
158: }
159: }
160:
161: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter)
162: throws javax.xml.stream.XMLStreamException {
163: JAXBElement<DerivedKeyTokenType> dkt = new com.sun.xml.ws.security.secconv.impl.bindings.ObjectFactory()
164: .createDerivedKeyToken(derivedKey);
165: try {
166: // If writing to Zephyr, get output stream and use JAXB UTF-8 writer
167: Marshaller writer = getMarshaller();
168: if (streamWriter instanceof Map) {
169: OutputStream os = (OutputStream) ((Map) streamWriter)
170: .get("sjsxp-outputstream");
171: if (os != null) {
172: streamWriter.writeCharacters(""); // Force completion of open elems
173:
174: writer.marshal(dkt, os);
175: return;
176: }
177: }
178: writer.marshal(dkt, streamWriter);
179: } catch (JAXBException e) {
180: throw new XMLStreamException(e);
181: }
182: }
183:
184: private Marshaller getMarshaller() throws JAXBException {
185: return JAXBUtil.createMarshaller(soapVersion);
186: }
187:
188: public boolean refersToSecHdrWithId(String id) {
189: if (refId != null && refId.length() > 0) {
190: if (refId.equals(id)) {
191: return true;
192: }
193: }
194: if (this .getSecurityTokenReference() != null) {
195: SecurityTokenReferenceType ref = this
196: .getSecurityTokenReference();
197: List list = ref.getAny();
198: if (list.size() > 0) {
199: JAXBElement je = (JAXBElement) list.get(0);
200: Object obj = je.getValue();
201: if (obj instanceof DirectReference) {
202: StringBuffer sb = new StringBuffer();
203: sb.append("#");
204: sb.append(id);
205: return ((DirectReference) obj).getURI().equals(
206: sb.toString());
207: } else if (obj instanceof KeyIdentifierType) {
208: KeyIdentifierType ki = (KeyIdentifierType) obj;
209: String valueType = ki.getValueType();
210: if (valueType
211: .equals(MessageConstants.WSSE_SAML_KEY_IDENTIFIER_VALUE_TYPE)
212: || valueType
213: .equals(MessageConstants.WSSE_SAML_v2_0_KEY_IDENTIFIER_VALUE_TYPE)) {
214: if (id.equals(ki.getValue())) {
215: return true;
216: }
217: }
218: }
219: }
220: }
221: return false;
222: }
223:
224: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter,
225: HashMap props) throws javax.xml.stream.XMLStreamException {
226: try {
227: Marshaller marshaller = getMarshaller();
228: Iterator<Map.Entry<Object, Object>> itr = props.entrySet()
229: .iterator();
230: while (itr.hasNext()) {
231: Map.Entry<Object, Object> entry = itr.next();
232: marshaller.setProperty((String) entry.getKey(), entry
233: .getValue());
234: }
235: writeTo(streamWriter);
236: } catch (JAXBException jbe) {
237: throw new XMLStreamException(jbe);
238: }
239: }
240:
241: }
|