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

The following examples show how to use org.apache.xml.security.c14n.InvalidCanonicalizerException. 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: CanonicalizerUtils.java    From xades4j with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Checks if all the transforms in a ds:Reference are canonicalization transforms.
 * @param r the reference
 * @return true if all transforms are c14n, false otherwise.
 * @throws XMLSecurityException
 */
public static boolean allTransformsAreC14N(Reference r) throws XMLSecurityException
{
    Transforms transforms = r.getTransforms();
    try
    {
        for (int i = 0; i < transforms.getLength(); ++i)
        {
            Canonicalizer.getInstance(transforms.item(i).getURI());
        }
        return true;
    }
    catch (InvalidCanonicalizerException ex)
    {
        return false;
    }
}
 
Example #2
Source File: XmlContentCanonicalizer.java    From apicurio-registry with Apache License 2.0 5 votes vote down vote up
@Override
protected Canonicalizer initialValue() {
    try {
        return Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
    } catch (InvalidCanonicalizerException e) {
        throw new RuntimeException(e);
    }
}
 
Example #3
Source File: CanonicalizerUtils.java    From xades4j with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Verifies input C14N Algorithm is in fact a C14N Algorithm by querying the
 * default Apache Canonicalizer.
 *
 * @param c14n - A C14N algorithm.
 * @throws UnsupportedAlgorithmException - If the URI is not registered in
 * the default Canonicalizer.
 */
public static void checkC14NAlgorithm(Algorithm c14n) throws UnsupportedAlgorithmException
{
    // HACK: since we're not using Canonicalizer, do a quick check to ensure
    // that 'c14n' refers to a configured C14N algorithm.
    try
    {
        Canonicalizer.getInstance(c14n.getUri());
    } catch (InvalidCanonicalizerException ex)
    {
        throw new UnsupportedAlgorithmException("Unsupported canonicalization method", c14n.getUri(), ex);
    }
}
 
Example #4
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 #5
Source File: RDFXMLParserTestCase.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public CanonXMLValueFactory() throws InvalidCanonicalizerException, ParserConfigurationException {
	org.apache.xml.security.Init.init();

	c14n = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
}
 
Example #6
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 #7
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;
}
 
Example #8
Source File: StaxSerializer.java    From cxf with Apache License 2.0 4 votes vote down vote up
public StaxSerializer() throws InvalidCanonicalizerException {
    super(Canonicalizer.ALGO_ID_C14N_PHYSICAL, true);
}