1 17 18 package com.sun.org.apache.xml.internal.security.encryption; 19 20 import java.io.IOException ; 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 ; 29 import com.sun.org.apache.xml.internal.security.utils.Base64; 30 31 32 46 public class XMLCipherInput { 47 48 private static java.util.logging.Logger logger = 49 java.util.logging.Logger.getLogger(XMLCipher.class.getName()); 50 51 52 private CipherData _cipherData; 53 54 55 private int _mode; 56 57 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 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 98 99 public byte[] getBytes() throws XMLEncryptionException { 100 101 if (_mode == XMLCipher.DECRYPT_MODE) { 102 return getDecryptBytes(); 103 } 104 return null; 105 } 106 107 112 113 private byte[] getDecryptBytes() throws XMLEncryptionException { 114 115 String base64EncodedEncryptedOctets = null; 116 117 if (_cipherData.getDataType() == CipherData.REFERENCE_TYPE) { 118 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 125 Attr 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 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 ex) { 160 throw new XMLEncryptionException("empty", ex); 161 } catch (CanonicalizationException ex) { 162 throw new XMLEncryptionException("empty", ex); 163 } 164 165 } else if (_cipherData.getDataType() == CipherData.VALUE_TYPE) { 167 CipherValue cv = _cipherData.getCipherValue(); 168 base64EncodedEncryptedOctets = new String (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 |