FileDocCategorySizeDatePackage
XMLCipherInput.javaAPI DocJava SE 6 API6347Tue Jun 10 00:23:02 BST 2008com.sun.org.apache.xml.internal.security.encryption

XMLCipherInput

public class XMLCipherInput extends Object
XMLCipherInput is used to wrap input passed into the XMLCipher encryption operations. In decryption mode, it takes a CipherData object and allows callers to dereference the CipherData into the encrypted bytes that it actually represents. This takes care of all base64 encoding etc. While primarily an internal class, this can be used by applications to quickly and easily retrieve the encrypted bytes from an EncryptedType object
author
Berin Lautenbach

Fields Summary
private static Logger
logger
private CipherData
_cipherData
The data we are working with
private int
_mode
MODES
Constructors Summary
public XMLCipherInput(CipherData data)
Constructor for processing encrypted octets

param
data The CipherData object to read the bytes from
throws
XMLEncryptionException {@link XMLEncryptionException}


	                   	 

	     

		_cipherData = data;
		_mode = XMLCipher.DECRYPT_MODE;
		if (_cipherData == null) {
			throw new XMLEncryptionException("CipherData is null");
		}

	
public XMLCipherInput(EncryptedType input)
Constructor for processing encrypted octets

param
input The EncryptedType object to read the bytes from.
throws
XMLEncryptionException {@link XMLEncryptionException}


		_cipherData = ((input == null) ? null : input.getCipherData());
		_mode = XMLCipher.DECRYPT_MODE;
		if (_cipherData == null) {
			throw new XMLEncryptionException("CipherData is null");
		}

	
Methods Summary
public byte[]getBytes()
Dereferences the input and returns it as a single byte array.

throws
XMLEncryptionException
return
The decripted bytes.


		if (_mode == XMLCipher.DECRYPT_MODE) {
			return getDecryptBytes();
		}
		return null;
	
private byte[]getDecryptBytes()
Internal method to get bytes in decryption mode

return
the decripted bytes
throws
XMLEncryptionException


		String base64EncodedEncryptedOctets = null;

        if (_cipherData.getDataType() == CipherData.REFERENCE_TYPE) {
			// Fun time!
			if (logger.isLoggable(java.util.logging.Level.FINE))                                     logger.log(java.util.logging.Level.FINE, "Found a reference type CipherData");
			CipherReference cr = _cipherData.getCipherReference();

			// Need to wrap the uri in an Attribute node so that we can
			// Pass to the resource resolvers

			Attr uriAttr = cr.getURIAsAttr();
			XMLSignatureInput input = null;

			try {
				ResourceResolver resolver = 
					ResourceResolver.getInstance(uriAttr, null);
				input = resolver.resolve(uriAttr, null);
			} catch (ResourceResolverException ex) {
				throw new XMLEncryptionException("empty", ex);
			} 

			if (input != null) {
				if (logger.isLoggable(java.util.logging.Level.FINE))                                     logger.log(java.util.logging.Level.FINE, "Managed to resolve URI \"" + cr.getURI() + "\"");
			}
			else {
				if (logger.isLoggable(java.util.logging.Level.FINE))                                     logger.log(java.util.logging.Level.FINE, "Failed to resolve URI \"" + cr.getURI() + "\"");
			}
		
			// Lets see if there are any transforms
			Transforms transforms = cr.getTransforms();
			if (transforms != null) {
				if (logger.isLoggable(java.util.logging.Level.FINE))                                  logger.log(java.util.logging.Level.FINE, "Have transforms in cipher reference");
				try {
 				    com.sun.org.apache.xml.internal.security.transforms.Transforms dsTransforms =
						transforms.getDSTransforms();
				    input =	dsTransforms.performTransforms(input);
				} catch (TransformationException ex) {
					throw new XMLEncryptionException("empty", ex);
				}
			}

			try {
				return input.getBytes();
			}
			catch (IOException ex) {
				throw new XMLEncryptionException("empty", ex);
			} catch (CanonicalizationException ex) {
				throw new XMLEncryptionException("empty", ex);
			}
			
            // retrieve the cipher text
        } else if (_cipherData.getDataType() == CipherData.VALUE_TYPE) {
            CipherValue cv = _cipherData.getCipherValue();
            base64EncodedEncryptedOctets = new String(cv.getValue());
        } else {
			throw new XMLEncryptionException("CipherData.getDataType() returned unexpected value");
		}

        if (logger.isLoggable(java.util.logging.Level.FINE))                                     logger.log(java.util.logging.Level.FINE, "Encrypted octets:\n" + base64EncodedEncryptedOctets);

        byte[] encryptedBytes = null;

        try {
			encryptedBytes = Base64.decode(base64EncodedEncryptedOctets);
        } catch (Base64DecodingException bde) {
            throw new XMLEncryptionException("empty", bde);
        }

		return (encryptedBytes);