org.apache.xml.security.c14n.CanonicalizationException Java Examples

The following examples show how to use org.apache.xml.security.c14n.CanonicalizationException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XmlContentCanonicalizer.java    From apicurio-registry with Apache License 2.0 5 votes vote down vote up
/**
 * @see ContentCanonicalizer#canonicalize(io.apicurio.registry.content.ContentHandle)
 */
@Override
public ContentHandle canonicalize(ContentHandle content) {
    try {
        Canonicalizer canon = xmlCanonicalizer.get();
        String canonicalized = IoUtil.toString(canon.canonicalize(content.bytes()));
        return ContentHandle.create(canonicalized);
    } catch (CanonicalizationException | ParserConfigurationException | IOException | SAXException e) {
    }
    return content;
}
 
Example #2
Source File: XAdESCanonicalizationTest.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void checkKeyInfo(Document doc) throws InvalidCanonicalizerException, CanonicalizationException {
	// ------------------------------------ KEY INFO
	// -----------------------------------------------------
	// Key info extraction + Verification
	NodeList keyInfoNodeList = DomUtils.getNodeList(doc, AbstractPaths.all(XMLDSigElement.KEY_INFO));
	assertNotNull(keyInfoNodeList);
	assertEquals(1, keyInfoNodeList.getLength());

	Node keyInfo = keyInfoNodeList.item(0);

	NamedNodeMap keyInfoAttributes = keyInfo.getAttributes();
	Node keyInfoId = keyInfoAttributes.getNamedItem("Id");
	assertNotNull(keyInfoId);

	Canonicalizer canonicalizer = Canonicalizer.getInstance(canonicalizationKeyInfo);

	// Verify KeyInfo Canonicalization Algorithm
	NodeList transformNodes = getReferenceTransforms(doc, "#" + keyInfoId.getNodeValue());
	String keyInfoTransformAlgo = getTransformAlgo(transformNodes.item(0));
	assertEquals(canonicalizer.getURI(), keyInfoTransformAlgo);

	// Verify KeyInfo Digest
	String keyInfoDigest = getReferenceDigest(doc, "#" + keyInfoId.getNodeValue());
	byte[] canonicalizedKeyInfo = canonicalizer.canonicalizeSubtree(keyInfo);
	byte[] digestKeyInfo = DSSUtils.digest(DigestAlgorithm.SHA256, canonicalizedKeyInfo);
	String keyInfoBase64 = Base64.getEncoder().encodeToString(digestKeyInfo);
	assertEquals(keyInfoBase64, keyInfoDigest);
}
 
Example #3
Source File: XMLSigner.java    From signer with GNU Lesser General Public License v3.0 4 votes vote down vote up
private byte[] getShaCanonizedValue(String Alg, Node xml) throws InvalidCanonicalizerException, NoSuchAlgorithmException, CanonicalizationException, ParserConfigurationException, IOException, SAXException {
	Init.init();
	Canonicalizer c14n = Canonicalizer.getInstance("http://www.w3.org/TR/2001/REC-xml-c14n-20010315");
	MessageDigest messageDigest = MessageDigest.getInstance(Alg);
	return messageDigest.digest(c14n.canonicalizeSubtree(xml));
}
 
Example #4
Source File: XMLSigner.java    From signer with GNU Lesser General Public License v3.0 4 votes vote down vote up
private Document buildXML(String fileName) throws FileNotFoundException, SAXException, IOException, ParserConfigurationException, InvalidCanonicalizerException, NoSuchAlgorithmException, CanonicalizationException {
	
	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	dbf.setNamespaceAware(true);
	Document bodyDoc = dbf.newDocumentBuilder().parse(
			new InputSource(new InputStreamReader(new FileInputStream(fileName), "UTF-8")));
	Element docData = getDocumentData(bodyDoc);
	Element signatureTag = bodyDoc.createElementNS(XMLNS, "ds:Signature");
	signatureTag.setAttribute(XMLNS_DS, XMLNS);
	signatureTag.setAttribute("Id", id);
	
	Element sigInfTag = bodyDoc.createElementNS(XMLNS, "ds:SignedInfo");
	signatureTag.appendChild(sigInfTag);
	
	Element canonicalizationMethodTag = bodyDoc.createElementNS(XMLNS, "ds:CanonicalizationMethod");
	canonicalizationMethodTag.setAttribute("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315");
	sigInfTag.appendChild(canonicalizationMethodTag);
	
	Element signatureMethodTag = bodyDoc.createElementNS(XMLNS, "ds:SignatureMethod");
	signatureMethodTag.setAttribute("Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
	sigInfTag.appendChild(signatureMethodTag );
	
	HashMap<String, String> param = new HashMap<String, String>();
	param.put("type", "");
	param.put("uri", "");
	param.put("id", "r-id-1");
	param.put("text", "not(ancestor-or-self::ds:Signature)");
	param.put("alg", "http://www.w3.org/TR/1999/REC-xpath-19991116");
	param.put("digAlg", "http://www.w3.org/2001/04/xmlenc#sha256");
	
	byte[] docHash = getShaCanonizedValue("SHA-256", docData); //bodyDoc.getDocumentElement().getFirstChild());
	param.put("digVal", Base64.toBase64String(docHash));
	param.put("transAlg", "http://www.w3.org/2001/10/xml-exc-c14n#");
	
	Element referenceTag = createReferenceTag(bodyDoc, param);
	sigInfTag.appendChild(referenceTag);
	
	bodyDoc.getDocumentElement().appendChild(signatureTag);
	
	return bodyDoc;
}