|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
package com.sun.org.apache.xml.internal.security.transforms.implementations; |
|
|
|
import java.io.IOException; |
|
import java.io.OutputStream; |
|
|
|
import javax.xml.parsers.ParserConfigurationException; |
|
|
|
import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException; |
|
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput; |
|
import com.sun.org.apache.xml.internal.security.transforms.Transform; |
|
import com.sun.org.apache.xml.internal.security.transforms.TransformSpi; |
|
import com.sun.org.apache.xml.internal.security.transforms.TransformationException; |
|
import com.sun.org.apache.xml.internal.security.transforms.Transforms; |
|
import com.sun.org.apache.xml.internal.security.utils.XMLUtils; |
|
import org.w3c.dom.Document; |
|
import org.w3c.dom.Element; |
|
import org.w3c.dom.Node; |
|
import org.w3c.dom.Text; |
|
import org.xml.sax.SAXException; |
|
|
|
import com.sun.org.apache.xml.internal.security.utils.JavaUtils; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public class TransformBase64Decode extends TransformSpi { |
|
|
|
|
|
public static final String implementedTransformURI = |
|
Transforms.TRANSFORM_BASE64_DECODE; |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected String engineGetURI() { |
|
return TransformBase64Decode.implementedTransformURI; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected XMLSignatureInput enginePerformTransform( |
|
XMLSignatureInput input, Transform transformObject |
|
) throws IOException, CanonicalizationException, TransformationException { |
|
return enginePerformTransform(input, null, transformObject); |
|
} |
|
|
|
protected XMLSignatureInput enginePerformTransform( |
|
XMLSignatureInput input, OutputStream os, Transform transformObject |
|
) throws IOException, CanonicalizationException, TransformationException { |
|
if (input.isElement()) { |
|
Node el = input.getSubNode(); |
|
if (input.getSubNode().getNodeType() == Node.TEXT_NODE) { |
|
el = el.getParentNode(); |
|
} |
|
StringBuilder sb = new StringBuilder(); |
|
traverseElement((Element)el, sb); |
|
if (os == null) { |
|
byte[] decodedBytes = XMLUtils.decode(sb.toString()); |
|
XMLSignatureInput output = new XMLSignatureInput(decodedBytes); |
|
output.setSecureValidation(secureValidation); |
|
return output; |
|
} |
|
byte[] bytes = XMLUtils.decode(sb.toString()); |
|
os.write(bytes); |
|
XMLSignatureInput output = new XMLSignatureInput((byte[])null); |
|
output.setSecureValidation(secureValidation); |
|
output.setOutputStream(os); |
|
return output; |
|
} |
|
|
|
if (input.isOctetStream() || input.isNodeSet()) { |
|
if (os == null) { |
|
byte[] base64Bytes = input.getBytes(); |
|
byte[] decodedBytes = XMLUtils.decode(base64Bytes); |
|
XMLSignatureInput output = new XMLSignatureInput(decodedBytes); |
|
output.setSecureValidation(secureValidation); |
|
return output; |
|
} |
|
if (input.isByteArray() || input.isNodeSet()) { |
|
byte[] bytes = XMLUtils.decode(input.getBytes()); |
|
os.write(bytes); |
|
} else { |
|
byte[] inputBytes = JavaUtils.getBytesFromStream(input.getOctetStreamReal()); |
|
byte[] bytes = XMLUtils.decode(inputBytes); |
|
os.write(bytes); |
|
} |
|
XMLSignatureInput output = new XMLSignatureInput((byte[])null); |
|
output.setSecureValidation(secureValidation); |
|
output.setOutputStream(os); |
|
return output; |
|
} |
|
|
|
try { |
|
//Exceptional case there is current not text case testing this(Before it was a |
|
|
|
Document doc = |
|
XMLUtils.createDocumentBuilder(false, secureValidation).parse(input.getOctetStream()); |
|
|
|
Element rootNode = doc.getDocumentElement(); |
|
StringBuilder sb = new StringBuilder(); |
|
traverseElement(rootNode, sb); |
|
byte[] decodedBytes = XMLUtils.decode(sb.toString()); |
|
XMLSignatureInput output = new XMLSignatureInput(decodedBytes); |
|
output.setSecureValidation(secureValidation); |
|
return output; |
|
} catch (ParserConfigurationException e) { |
|
throw new TransformationException(e, "c14n.Canonicalizer.Exception"); |
|
} catch (SAXException e) { |
|
throw new TransformationException(e, "SAX exception"); |
|
} |
|
} |
|
|
|
void traverseElement(Element node, StringBuilder sb) { |
|
Node sibling = node.getFirstChild(); |
|
while (sibling != null) { |
|
switch (sibling.getNodeType()) { |
|
case Node.ELEMENT_NODE: |
|
traverseElement((Element)sibling, sb); |
|
break; |
|
case Node.TEXT_NODE: |
|
sb.append(((Text)sibling).getData()); |
|
} |
|
sibling = sibling.getNextSibling(); |
|
} |
|
} |
|
} |