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.enc;
024:
025: import com.sun.xml.security.core.xenc.CVAdapter;
026: import com.sun.xml.security.core.xenc.EncryptedKeyType;
027: import com.sun.xml.security.core.xenc.ReferenceList;
028: import com.sun.xml.ws.api.SOAPVersion;
029: import com.sun.xml.ws.security.opt.api.EncryptedKey;
030: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
031: import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
032: import com.sun.xml.ws.security.opt.impl.util.JAXBUtil;
033: import com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.KeyInfo;
034: import com.sun.xml.wss.logging.LogDomainConstants;
035: import java.io.OutputStream;
036: import java.security.Key;
037: import java.util.HashMap;
038: import java.util.Iterator;
039: import java.util.List;
040: import java.util.Map;
041: import java.util.logging.Level;
042: import java.util.logging.Logger;
043: import javax.xml.bind.JAXBElement;
044: import javax.xml.bind.Marshaller;
045: import javax.xml.bind.Unmarshaller;
046: import javax.xml.namespace.QName;
047: import org.xml.sax.ContentHandler;
048: import org.xml.sax.ErrorHandler;
049: import org.xml.sax.SAXException;
050: import javax.crypto.Cipher;
051: import javax.xml.crypto.Data;
052: import javax.xml.bind.JAXBException;
053: import com.sun.xml.wss.XWSSecurityException;
054: import com.sun.xml.security.core.xenc.ReferenceType;
055: import javax.xml.stream.XMLStreamException;
056: import com.sun.xml.wss.logging.impl.opt.crypto.LogStringsMessages;
057:
058: /**
059: *
060: * @author K.Venugopal@sun.com
061: */
062: public class JAXBEncryptedKey implements EncryptedKey,
063: SecurityHeaderElement, SecurityElementWriter {
064:
065: private static final Logger logger = Logger.getLogger(
066: LogDomainConstants.IMPL_OPT_CRYPTO_DOMAIN,
067: LogDomainConstants.IMPL_OPT_CRYPTO_DOMAIN_BUNDLE);
068:
069: private EncryptedKeyType ekt = null;
070: //private Data data = null;
071: private Key dataEnckey = null;
072: private Key dkEK = null;
073: CryptoProcessor dep = null;
074: private SOAPVersion soapVersion = SOAPVersion.SOAP_11;
075:
076: /** Creates a new instance of JAXBEncryptedKey */
077: public JAXBEncryptedKey(EncryptedKeyType ekt, Key kk, Key dk,
078: SOAPVersion soapVersion) throws XWSSecurityException {
079: this .ekt = ekt;
080: this .dkEK = kk;
081: this .dataEnckey = dk;
082: this .soapVersion = soapVersion;
083: dep = new CryptoProcessor(Cipher.WRAP_MODE, ekt
084: .getEncryptionMethod().getAlgorithm(), dataEnckey, dkEK);
085:
086: }
087:
088: public void encrypt() {
089: }
090:
091: public void decrypt() {
092: }
093:
094: public String getId() {
095: return ekt.getId();
096: }
097:
098: public void setId(String id) {
099: ekt.setId(id);
100: }
101:
102: public String getNamespaceURI() {
103: return "http://www.w3.org/2001/04/xmlenc#";
104: }
105:
106: public String getLocalPart() {
107: return "EncryptedKey";
108: }
109:
110: public String getAttribute(String nsUri, String localName) {
111: throw new UnsupportedOperationException();
112: }
113:
114: public String getAttribute(QName name) {
115: throw new UnsupportedOperationException();
116: }
117:
118: public javax.xml.stream.XMLStreamReader readHeader()
119: throws javax.xml.stream.XMLStreamException {
120: throw new UnsupportedOperationException();
121: }
122:
123: public <T> T readAsJAXB(Unmarshaller unmarshaller)
124: throws javax.xml.bind.JAXBException {
125: throw new UnsupportedOperationException();
126: }
127:
128: public <T> T readAsJAXB(com.sun.xml.bind.api.Bridge<T> bridge,
129: com.sun.xml.bind.api.BridgeContext context)
130: throws javax.xml.bind.JAXBException {
131: throw new UnsupportedOperationException();
132: }
133:
134: public <T> T readAsJAXB(com.sun.xml.bind.api.Bridge<T> bridge)
135: throws javax.xml.bind.JAXBException {
136: throw new UnsupportedOperationException();
137: }
138:
139: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter)
140: throws javax.xml.stream.XMLStreamException {
141: Marshaller writer;
142: try {
143: if (streamWriter instanceof Map) {
144: OutputStream os = (OutputStream) ((Map) streamWriter)
145: .get("sjsxp-outputstream");
146: if (os != null) {
147: streamWriter.writeCharacters(""); // Force completion of open elems
148: writeTo(os);
149: return;
150: }
151: }
152: writer = getMarshaller();
153: JAXBElement ed = getEK(writer);
154: writer.marshal(ed, streamWriter);
155: } catch (javax.xml.bind.JAXBException ex) {
156: logger.log(Level.SEVERE, LogStringsMessages
157: .WSS_1921_ERROR_WRITING_ENCRYPTEDKEY(ex
158: .getMessage()), ex);
159: } catch (com.sun.xml.wss.XWSSecurityException ex) {
160: logger.log(Level.SEVERE, LogStringsMessages
161: .WSS_1921_ERROR_WRITING_ENCRYPTEDKEY(ex
162: .getMessage()), ex);
163: }
164: }
165:
166: public void writeTo(OutputStream os) {
167: Marshaller writer;
168: try {
169: writer = getMarshaller();
170:
171: JAXBElement ed = getEK(writer);
172: writer.marshal(ed, os);
173: } catch (javax.xml.bind.JAXBException ex) {
174: logger.log(Level.SEVERE, LogStringsMessages
175: .WSS_1921_ERROR_WRITING_ENCRYPTEDKEY(ex
176: .getMessage()), ex);
177: } catch (com.sun.xml.wss.XWSSecurityException ex) {
178: logger.log(Level.SEVERE, LogStringsMessages
179: .WSS_1921_ERROR_WRITING_ENCRYPTEDKEY(ex
180: .getMessage()), ex);
181: }
182: }
183:
184: private JAXBElement getEK(Marshaller writer) throws JAXBException,
185: XWSSecurityException {
186:
187: CVAdapter adapter = new CVAdapter(dep);
188: writer.setAdapter(CVAdapter.class, adapter);
189: com.sun.xml.security.core.xenc.ObjectFactory obj = new com.sun.xml.security.core.xenc.ObjectFactory();
190: return obj.createEncryptedKey(ekt);
191: }
192:
193: public void writeTo(javax.xml.soap.SOAPMessage saaj)
194: throws javax.xml.soap.SOAPException {
195: throw new UnsupportedOperationException();
196: }
197:
198: public void writeTo(ContentHandler contentHandler,
199: ErrorHandler errorHandler) throws SAXException {
200: throw new UnsupportedOperationException();
201: }
202:
203: public byte[] canonicalize(String algorithm,
204: List<com.sun.xml.wss.impl.c14n.AttributeNS> namespaceDecls) {
205: throw new UnsupportedOperationException();
206: }
207:
208: public boolean isCanonicalized() {
209: throw new UnsupportedOperationException();
210: }
211:
212: private Marshaller getMarshaller() throws JAXBException {
213: return JAXBUtil.createMarshaller(soapVersion);
214: }
215:
216: public ReferenceList getReferenceList() {
217: return ekt.getReferenceList();
218: }
219:
220: public boolean hasReferenceList() {
221: return (ekt.getReferenceList() != null);
222: }
223:
224: public boolean refersToSecHdrWithId(String id) {
225: KeyInfo ki = (KeyInfo) this .ekt.getKeyInfo();
226: if (ki != null) {
227: List list = ki.getContent();
228: if (list.size() > 0) {
229: Object data = ((JAXBElement) list.get(0)).getValue();
230: if (data instanceof SecurityHeaderElement) {
231: if (((SecurityHeaderElement) data)
232: .refersToSecHdrWithId(id)) {
233: return true;
234: }
235: }
236: }
237: }
238: List list = null;
239: if (getReferenceList() != null) {
240: list = getReferenceList().getDataReferenceOrKeyReference();
241: }
242: if (list == null) {
243: return false;
244: }
245: StringBuffer sb = new StringBuffer();
246: sb.append("#");
247: sb.append(id);
248: String idref = sb.toString();
249: for (int i = 0; i < list.size(); i++) {
250: JAXBElement<ReferenceType> rt = (JAXBElement<ReferenceType>) list
251: .get(i);
252: ReferenceType ref = (ReferenceType) rt.getValue();
253: if (ref.getURI().equals(idref)) {
254: return true;
255: }
256: }
257: return false;
258: }
259:
260: public void setReferenceList(ReferenceList list) {
261: ekt.setReferenceList(list);
262: }
263:
264: public Key getKey() {
265: return dataEnckey;
266: }
267:
268: public byte[] getCipherValue() {
269: return dep.getCipherValueOfEK();
270: }
271:
272: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter,
273: HashMap props) throws XMLStreamException {
274: try {
275: Marshaller marshaller = getMarshaller();
276: Iterator<Map.Entry<Object, Object>> itr = props.entrySet()
277: .iterator();
278: while (itr.hasNext()) {
279: Map.Entry<Object, Object> entry = itr.next();
280: marshaller.setProperty((String) entry.getKey(), entry
281: .getValue());
282: }
283: writeTo(streamWriter);
284: } catch (JAXBException jbe) {
285: logger.log(Level.SEVERE, LogStringsMessages
286: .WSS_1921_ERROR_WRITING_ENCRYPTEDKEY(jbe
287: .getMessage()), jbe);
288: throw new XMLStreamException(jbe);
289: }
290: }
291:
292: }
|