use of com.sun.org.apache.xml.internal.security.c14n.Canonicalizer in project jdk8u_jdk by JetBrains.
the class SignedInfo method reparseSignedInfoElem.
private static Element reparseSignedInfoElem(Element element) throws XMLSecurityException {
/*
* If a custom canonicalizationMethod is used, canonicalize
* ds:SignedInfo, reparse it into a new document
* and replace the original not-canonicalized ds:SignedInfo by
* the re-parsed canonicalized one.
*/
Element c14nMethod = XMLUtils.getNextElement(element.getFirstChild());
String c14nMethodURI = c14nMethod.getAttributeNS(null, Constants._ATT_ALGORITHM);
if (!(c14nMethodURI.equals(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS) || c14nMethodURI.equals(Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS) || c14nMethodURI.equals(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS) || c14nMethodURI.equals(Canonicalizer.ALGO_ID_C14N_EXCL_WITH_COMMENTS) || c14nMethodURI.equals(Canonicalizer.ALGO_ID_C14N11_OMIT_COMMENTS) || c14nMethodURI.equals(Canonicalizer.ALGO_ID_C14N11_WITH_COMMENTS))) {
// so reparse the SignedInfo to be sure
try {
Canonicalizer c14nizer = Canonicalizer.getInstance(c14nMethodURI);
byte[] c14nizedBytes = c14nizer.canonicalizeSubtree(element);
javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
Document newdoc = db.parse(new ByteArrayInputStream(c14nizedBytes));
Node imported = element.getOwnerDocument().importNode(newdoc.getDocumentElement(), true);
element.getParentNode().replaceChild(imported, element);
return (Element) imported;
} catch (ParserConfigurationException ex) {
throw new XMLSecurityException("empty", ex);
} catch (IOException ex) {
throw new XMLSecurityException("empty", ex);
} catch (SAXException ex) {
throw new XMLSecurityException("empty", ex);
}
}
return element;
}
use of com.sun.org.apache.xml.internal.security.c14n.Canonicalizer in project jdk8u_jdk by JetBrains.
the class SignedInfo method getCanonicalizedOctetStream.
/**
* Returns getCanonicalizedOctetStream
*
* @return the canonicalization result octet stream of <code>SignedInfo</code> element
* @throws CanonicalizationException
* @throws InvalidCanonicalizerException
* @throws XMLSecurityException
*/
public byte[] getCanonicalizedOctetStream() throws CanonicalizationException, InvalidCanonicalizerException, XMLSecurityException {
if (this.c14nizedBytes == null) {
Canonicalizer c14nizer = Canonicalizer.getInstance(this.getCanonicalizationMethodURI());
this.c14nizedBytes = c14nizer.canonicalizeSubtree(this.constructionElement);
}
// make defensive copy
return this.c14nizedBytes.clone();
}
use of com.sun.org.apache.xml.internal.security.c14n.Canonicalizer in project jdk8u_jdk by JetBrains.
the class SignedInfo method signInOctetStream.
/**
* Output the C14n stream to the given OutputStream.
* @param os
* @throws CanonicalizationException
* @throws InvalidCanonicalizerException
* @throws XMLSecurityException
*/
public void signInOctetStream(OutputStream os) throws CanonicalizationException, InvalidCanonicalizerException, XMLSecurityException {
if (this.c14nizedBytes == null) {
Canonicalizer c14nizer = Canonicalizer.getInstance(this.getCanonicalizationMethodURI());
c14nizer.setWriter(os);
String inclusiveNamespaces = this.getInclusiveNamespaces();
if (inclusiveNamespaces == null) {
c14nizer.canonicalizeSubtree(this.constructionElement);
} else {
c14nizer.canonicalizeSubtree(this.constructionElement, inclusiveNamespaces);
}
} else {
try {
os.write(this.c14nizedBytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Aggregations