KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > encryption > XMLCipherInput


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package com.sun.org.apache.xml.internal.security.encryption;
19
20 import java.io.IOException JavaDoc;
21
22 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;
23 import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver;
24 import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException;
25 import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
26 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
27 import com.sun.org.apache.xml.internal.security.transforms.TransformationException;
28 import org.w3c.dom.Attr JavaDoc;
29 import com.sun.org.apache.xml.internal.security.utils.Base64;
30
31
32 /**
33  * <code>XMLCipherInput</code> is used to wrap input passed into the
34  * XMLCipher encryption operations.
35  *
36  * In decryption mode, it takes a <code>CipherData</code> object and allows
37  * callers to dereference the CipherData into the encrypted bytes that it
38  * actually represents. This takes care of all base64 encoding etc.
39  *
40  * While primarily an internal class, this can be used by applications to
41  * quickly and easily retrieve the encrypted bytes from an EncryptedType
42  * object
43  *
44  * @author Berin Lautenbach
45  */

46 public class XMLCipherInput {
47
48     private static java.util.logging.Logger JavaDoc logger =
49         java.util.logging.Logger.getLogger(XMLCipher.class.getName());
50
51     /** The data we are working with */
52     private CipherData _cipherData;
53
54     /** MODES */
55     private int _mode;
56
57     /**
58      * Constructor for processing encrypted octets
59      *
60      * @param data The <code>CipherData</code> object to read the bytes from
61      * @throws XMLEncryptionException {@link XMLEncryptionException}
62      */

63
64     public XMLCipherInput(CipherData data) throws XMLEncryptionException {
65
66         _cipherData = data;
67         _mode = XMLCipher.DECRYPT_MODE;
68         if (_cipherData == null) {
69             throw new XMLEncryptionException("CipherData is null");
70         }
71
72     }
73
74     /**
75      * Constructor for processing encrypted octets
76      *
77      * @param input The <code>EncryptedType</code> object to read
78      * the bytes from.
79      * @throws XMLEncryptionException {@link XMLEncryptionException}
80      */

81
82     public XMLCipherInput(EncryptedType input) throws XMLEncryptionException {
83
84         _cipherData = ((input == null) ? null : input.getCipherData());
85         _mode = XMLCipher.DECRYPT_MODE;
86         if (_cipherData == null) {
87             throw new XMLEncryptionException("CipherData is null");
88         }
89
90     }
91
92     /**
93      * Dereferences the input and returns it as a single byte array.
94      *
95      * @throws XMLEncryptionException
96      * @return The decripted bytes.
97      */

98
99     public byte[] getBytes() throws XMLEncryptionException {
100
101         if (_mode == XMLCipher.DECRYPT_MODE) {
102             return getDecryptBytes();
103         }
104         return null;
105     }
106
107     /**
108      * Internal method to get bytes in decryption mode
109      * @return the decripted bytes
110      * @throws XMLEncryptionException
111      */

112
113     private byte[] getDecryptBytes() throws XMLEncryptionException {
114
115         String JavaDoc base64EncodedEncryptedOctets = null;
116
117         if (_cipherData.getDataType() == CipherData.REFERENCE_TYPE) {
118             // Fun time!
119
if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Found a reference type CipherData");
120             CipherReference cr = _cipherData.getCipherReference();
121
122             // Need to wrap the uri in an Attribute node so that we can
123
// Pass to the resource resolvers
124

125             Attr JavaDoc uriAttr = cr.getURIAsAttr();
126             XMLSignatureInput input = null;
127
128             try {
129                 ResourceResolver resolver =
130                     ResourceResolver.getInstance(uriAttr, null);
131                 input = resolver.resolve(uriAttr, null);
132             } catch (ResourceResolverException ex) {
133                 throw new XMLEncryptionException("empty", ex);
134             }
135
136             if (input != null) {
137                 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Managed to resolve URI \"" + cr.getURI() + "\"");
138             }
139             else {
140                 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Failed to resolve URI \"" + cr.getURI() + "\"");
141             }
142         
143             // Lets see if there are any transforms
144
Transforms transforms = cr.getTransforms();
145             if (transforms != null) {
146                 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Have transforms in cipher reference");
147                 try {
148                     com.sun.org.apache.xml.internal.security.transforms.Transforms dsTransforms =
149                         transforms.getDSTransforms();
150                     input = dsTransforms.performTransforms(input);
151                 } catch (TransformationException ex) {
152                     throw new XMLEncryptionException("empty", ex);
153                 }
154             }
155
156             try {
157                 return input.getBytes();
158             }
159             catch (IOException JavaDoc ex) {
160                 throw new XMLEncryptionException("empty", ex);
161             } catch (CanonicalizationException ex) {
162                 throw new XMLEncryptionException("empty", ex);
163             }
164             
165             // retrieve the cipher text
166
} else if (_cipherData.getDataType() == CipherData.VALUE_TYPE) {
167             CipherValue cv = _cipherData.getCipherValue();
168             base64EncodedEncryptedOctets = new String JavaDoc(cv.getValue());
169         } else {
170             throw new XMLEncryptionException("CipherData.getDataType() returned unexpected value");
171         }
172
173         if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Encrypted octets:\n" + base64EncodedEncryptedOctets);
174
175         byte[] encryptedBytes = null;
176
177         try {
178             encryptedBytes = Base64.decode(base64EncodedEncryptedOctets);
179         } catch (Base64DecodingException bde) {
180             throw new XMLEncryptionException("empty", bde);
181         }
182
183         return (encryptedBytes);
184
185     }
186
187 }
188
189
190
Popular Tags