001: /*
002: * JAXBSignatureHeaderElement.java
003: *
004: * Created on August 18, 2006, 2:29 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package com.sun.xml.ws.security.opt.impl.dsig;
011:
012: import com.sun.xml.ws.api.SOAPVersion;
013: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
014: import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
015: import com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.KeyInfo;
016: import com.sun.xml.wss.impl.MessageConstants;
017:
018: import java.util.HashMap;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022: import java.io.OutputStream;
023:
024: import javax.xml.bind.JAXBElement;
025: import javax.xml.bind.Marshaller;
026: import javax.xml.bind.JAXBException;
027: import javax.xml.crypto.MarshalException;
028: import javax.xml.crypto.dsig.XMLSignContext;
029: import javax.xml.crypto.dsig.XMLSignatureException;
030: import com.sun.xml.ws.security.opt.crypto.dsig.Signature;
031: import com.sun.xml.ws.security.opt.impl.util.JAXBUtil;
032: import com.sun.xml.stream.buffer.XMLStreamBufferResult;
033: import javax.xml.stream.XMLStreamException;
034:
035: /**
036: *
037: * @author Ashutosh.Shahi@sun.com
038: */
039: public class JAXBSignatureHeaderElement implements
040: SecurityHeaderElement, SecurityElementWriter {
041:
042: /* true if this signature header element is canonicalized before*/
043: private boolean isCanonicalized = false;
044: /*canonicalized signature value - for future use*/
045: private byte[] cs = null;
046:
047: private Signature signature = null;
048: private SOAPVersion soapVersion = SOAPVersion.SOAP_11;
049: private Marshaller marshaller = null;
050: private XMLSignContext signContext = null;
051:
052: /** Creates a new instance of JAXBSignatureHeaderElement */
053: public JAXBSignatureHeaderElement(Signature signature,
054: SOAPVersion soapVersion) {
055: this .signature = signature;
056: this .soapVersion = soapVersion;
057:
058: }
059:
060: public JAXBSignatureHeaderElement(Signature signature,
061: SOAPVersion soapVersion, XMLSignContext signctx) {
062: this .signature = signature;
063: this .soapVersion = soapVersion;
064: this .signContext = signctx;
065: }
066:
067: public String getId() {
068: return signature.getId();
069: }
070:
071: public void setId(String id) {
072: throw new UnsupportedOperationException();
073: }
074:
075: public String getNamespaceURI() {
076: return MessageConstants.DSIG_NS;
077: }
078:
079: public String getLocalPart() {
080: return MessageConstants.SIGNATURE_LNAME;
081: }
082:
083: public javax.xml.stream.XMLStreamReader readHeader()
084: throws XMLStreamException {
085: XMLStreamBufferResult xbr = new XMLStreamBufferResult();
086: try {
087: getMarshaller().marshal(signature, xbr);
088: } catch (JAXBException je) {
089: throw new XMLStreamException(je);
090: }
091: return xbr.getXMLStreamBuffer().readAsXMLStreamReader();
092: }
093:
094: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter)
095: throws XMLStreamException {
096: try {
097: // If writing to Zephyr, get output stream and use JAXB UTF-8 writer
098: if (streamWriter instanceof Map) {
099: OutputStream os = (OutputStream) ((Map) streamWriter)
100: .get("sjsxp-outputstream");
101: if (os != null) {
102: streamWriter.writeCharacters(""); // Force completion of open elems
103: getMarshaller().marshal(signature, os);
104: return;
105: }
106: }
107: getMarshaller().marshal(signature, streamWriter);
108: } catch (JAXBException e) {
109: throw new XMLStreamException(e);
110: }
111: }
112:
113: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter,
114: HashMap props) throws XMLStreamException {
115: try {
116: Marshaller marshaller = getMarshaller();
117: Iterator<Map.Entry<Object, Object>> itr = props.entrySet()
118: .iterator();
119: while (itr.hasNext()) {
120: Map.Entry<Object, Object> entry = itr.next();
121: marshaller.setProperty((String) entry.getKey(), entry
122: .getValue());
123: }
124:
125: //writeTo(streamWriter);
126: marshaller.marshal(signature, streamWriter);
127: } catch (JAXBException jbe) {
128: throw new XMLStreamException(jbe);
129: }
130: }
131:
132: public byte[] canonicalize(
133: final String algorithm,
134: final List<com.sun.xml.wss.impl.c14n.AttributeNS> namespaceDecls) {
135: if (!isCanonicalized()) {
136: canonicalizeSignature();
137: }
138: return cs;
139: }
140:
141: public boolean isCanonicalized() {
142: return isCanonicalized;
143: }
144:
145: private Marshaller getMarshaller() throws JAXBException {
146: if (marshaller == null) {
147: marshaller = JAXBUtil.createMarshaller(soapVersion);
148: }
149: return marshaller;
150: }
151:
152: private void canonicalizeSignature() {
153: throw new UnsupportedOperationException("Not yet implemented");
154: }
155:
156: public void writeTo(OutputStream os) {
157: throw new UnsupportedOperationException("Not yet implemented");
158: }
159:
160: public boolean refersToSecHdrWithId(final String id) {
161:
162: StringBuffer sb = new StringBuffer();
163: sb.append("#");
164: sb.append(id);
165: String refId = sb.toString();
166: KeyInfo ki = signature.getKeyInfo();
167: if (ki != null) {
168: List list = ki.getContent();
169: if (list.size() > 0) {
170: JAXBElement je = (JAXBElement) list.get(0);
171: Object data = je.getValue();
172:
173: if (data instanceof SecurityHeaderElement) {
174: if (((SecurityHeaderElement) data)
175: .refersToSecHdrWithId(id)) {
176: return true;
177: }
178: }
179: }
180: }
181: List refList = signature.getSignedInfo().getReferences();
182: for (int i = 0; i < refList.size(); i++) {
183: com.sun.xml.ws.security.opt.crypto.dsig.Reference ref = (com.sun.xml.ws.security.opt.crypto.dsig.Reference) refList
184: .get(i);
185: if (ref.getURI().equals(refId)) {
186: return true;
187: }
188: }
189: return false;
190: }
191:
192: public void sign() throws XMLStreamException {
193: try {
194: signature.sign(signContext);
195: } catch (MarshalException me) {
196: throw new XMLStreamException(me);
197: } catch (XMLSignatureException xse) {
198: throw new XMLStreamException(xse);
199: }
200: }
201: }
|