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.incoming.processor;
024:
025: import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
026: import com.sun.xml.ws.security.opt.impl.util.StreamUtil;
027: import com.sun.xml.wss.XWSSecurityException;
028: import com.sun.xml.wss.impl.misc.Base64;
029: import com.sun.xml.wss.logging.LogDomainConstants;
030: import java.io.ByteArrayInputStream;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.util.logging.Level;
034: import java.util.logging.Logger;
035: import javax.xml.stream.XMLStreamException;
036: import javax.xml.stream.XMLStreamReader;
037: import org.jvnet.staxex.Base64Data;
038: import org.jvnet.staxex.XMLStreamReaderEx;
039: import com.sun.xml.wss.logging.impl.opt.crypto.LogStringsMessages;
040:
041: /**
042: *
043: * @author K.Venugopal@sun.com
044: */
045: public class CipherDataProcessor {
046:
047: private static final Logger logger = Logger.getLogger(
048: LogDomainConstants.IMPL_OPT_CRYPTO_DOMAIN,
049: LogDomainConstants.IMPL_OPT_CRYPTO_DOMAIN_BUNDLE);
050:
051: private static String CIPHER_VALUE = "CipherValue".intern();
052: private Base64Data bd = null;
053: private byte[] cipherValue;
054:
055: /** Creates a new instance of CipherDataProcessor */
056: public CipherDataProcessor() {
057: }
058:
059: public void process(XMLStreamReader reader)
060: throws XWSSecurityException {
061: try {
062: if (StreamUtil.moveToNextElement(reader)) {
063: if (reader.getLocalName() == CIPHER_VALUE) {
064: if (reader instanceof XMLStreamReaderEx) {
065: reader.next();
066: if (reader.getEventType() == XMLStreamReader.CHARACTERS) {
067: CharSequence charSeq = ((XMLStreamReaderEx) reader)
068: .getPCDATA();
069: if (charSeq instanceof Base64Data) {
070: bd = (Base64Data) charSeq;
071: } else {
072: try {
073: cipherValue = Base64
074: .decode(StreamUtil
075: .getCV((XMLStreamReaderEx) reader));
076: } catch (Base64DecodingException ex) {
077: logger
078: .log(
079: Level.SEVERE,
080: LogStringsMessages
081: .WSS_1922_ERROR_DECODING_CIPHERVAL(ex));
082: throw new XWSSecurityException(
083: LogStringsMessages
084: .WSS_1922_ERROR_DECODING_CIPHERVAL(ex));
085: }
086: }
087: }
088: } else {
089:
090: try {
091: //cipherValue = Base64.decode(reader.getElementText());
092: cipherValue = Base64.decode(StreamUtil
093: .getCV(reader));
094: } catch (Base64DecodingException ex) {
095: logger
096: .log(
097: Level.SEVERE,
098: LogStringsMessages
099: .WSS_1922_ERROR_DECODING_CIPHERVAL(ex));
100: throw new XWSSecurityException(
101: LogStringsMessages
102: .WSS_1922_ERROR_DECODING_CIPHERVAL(ex));
103: }
104:
105: }
106: //reader.next();//move to END OF CIPHER VALUE
107: reader.next();//move to END OF CIPHER DATA
108: reader.next();//move to NEXT ELEMENT
109: return;
110: }
111: }
112: reader.next();
113: } catch (XMLStreamException ex) {
114: logger.log(Level.SEVERE, LogStringsMessages
115: .WSS_1923_ERROR_PROCESSING_CIPHERVAL(ex));
116: throw new XWSSecurityException(LogStringsMessages
117: .WSS_1923_ERROR_PROCESSING_CIPHERVAL(ex));
118: }
119: logger
120: .log(
121: Level.SEVERE,
122: LogStringsMessages
123: .WSS_1923_ERROR_PROCESSING_CIPHERVAL("unexpected element:"
124: + reader.getLocalName()));
125: throw new XWSSecurityException(
126: LogStringsMessages
127: .WSS_1923_ERROR_PROCESSING_CIPHERVAL("unexpected element:"
128: + reader.getLocalName()));
129:
130: }
131:
132: public InputStream readAsStream() throws XWSSecurityException {
133:
134: if (bd != null) {
135: try {
136: return bd.getInputStream();
137: } catch (IOException ex) {
138: logger.log(Level.SEVERE, LogStringsMessages
139: .WSS_1923_ERROR_PROCESSING_CIPHERVAL(ex));
140: throw new XWSSecurityException(LogStringsMessages
141: .WSS_1923_ERROR_PROCESSING_CIPHERVAL(ex));
142: }
143: }
144: if (cipherValue != null) {
145: return new ByteArrayInputStream(cipherValue);
146: }
147: logger.log(Level.SEVERE, LogStringsMessages
148: .WSS_1924_CIPHERVAL_MISSINGIN_CIPHERDATA());
149: throw new XWSSecurityException(LogStringsMessages
150: .WSS_1924_CIPHERVAL_MISSINGIN_CIPHERDATA());
151: }
152:
153: public byte[] readAsBytes() throws XWSSecurityException {
154: if (cipherValue != null) {
155: return cipherValue;
156: }
157: if (bd != null) {
158: cipherValue = bd.getExact();
159: return cipherValue;
160: }
161: logger.log(Level.SEVERE, LogStringsMessages
162: .WSS_1924_CIPHERVAL_MISSINGIN_CIPHERDATA());
163: throw new XWSSecurityException(LogStringsMessages
164: .WSS_1924_CIPHERVAL_MISSINGIN_CIPHERDATA());
165: }
166: }
|