Java Code Examples for org.apache.xml.security.c14n.Canonicalizer#canonicalize()

The following examples show how to use org.apache.xml.security.c14n.Canonicalizer#canonicalize() . 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: XAdESLevelBEnvelopedTest.java    From dss with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
protected void verifyOriginalDocuments(SignedDocumentValidator validator, DiagnosticData diagnosticData) {
	super.verifyOriginalDocuments(validator, diagnosticData);
	
	List<DSSDocument> originals = validator.getOriginalDocuments(diagnosticData.getFirstSignatureId());
	assertEquals(1, originals.size());

	DSSDocument original = originals.get(0);

	try {
		Canonicalizer canon = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N11_OMIT_COMMENTS);
		String firstDocument = new String(canon.canonicalize(DSSUtils.toByteArray(documentToSign)));
		String secondDocument = new String(canon.canonicalize(DSSUtils.toByteArray(original)));
		assertEquals(firstDocument, secondDocument);
	} catch (Exception e) {
		fail(e);
	}
}
 
Example 2
Source File: XAdESLevelBEnvelopingTest.java    From dss with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
protected void verifyOriginalDocuments(SignedDocumentValidator validator, DiagnosticData diagnosticData) {
	super.verifyOriginalDocuments(validator, diagnosticData);
	
	List<DSSDocument> originals = validator.getOriginalDocuments(diagnosticData.getFirstSignatureId());
	assertEquals(1, originals.size());

	DSSDocument original = originals.get(0);

	try {
		Canonicalizer canon = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N11_OMIT_COMMENTS);
		String firstDocument = new String(canon.canonicalize(DSSUtils.toByteArray(documentToSign)));
		String secondDocument = new String(canon.canonicalize(DSSUtils.toByteArray(original)));
		assertEquals(firstDocument, secondDocument);
	} catch (Exception e) {
		fail(e);
	}
}
 
Example 3
Source File: XAdESLevelBEnvelopingWithRefsTest.java    From dss with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
protected void verifyOriginalDocuments(SignedDocumentValidator validator, DiagnosticData diagnosticData) {
	super.verifyOriginalDocuments(validator, diagnosticData);
	
	List<DSSDocument> originals = validator.getOriginalDocuments(diagnosticData.getFirstSignatureId());
	assertEquals(2, originals.size());

	DSSDocument orig1 = originals.get(0);
	DSSDocument orig2 = originals.get(1);

	try {
		Canonicalizer canon = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N11_OMIT_COMMENTS);
		String firstDocument = new String(canon.canonicalize(DSSUtils.toByteArray(doc1)));
		String secondDocument = new String(canon.canonicalize(DSSUtils.toByteArray(orig1)));
		assertEquals(firstDocument, secondDocument);

		firstDocument = new String(canon.canonicalize(DSSUtils.toByteArray(doc2)));
		secondDocument = new String(canon.canonicalize(DSSUtils.toByteArray(orig2)));
		assertEquals(firstDocument, secondDocument);
	} catch (Exception e) {
		fail(e);
	}
	
	assertEquals(doc1.getDigest(DigestAlgorithm.SHA256), orig1.getDigest(DigestAlgorithm.SHA256));
	assertEquals(doc2.getDigest(DigestAlgorithm.SHA256), orig2.getDigest(DigestAlgorithm.SHA256));
}
 
Example 4
Source File: XAdESLevelBDetachedTest.java    From dss with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
protected void verifyOriginalDocuments(SignedDocumentValidator validator, DiagnosticData diagnosticData) {
	super.verifyOriginalDocuments(validator, diagnosticData);
	
	List<DSSDocument> originals = validator.getOriginalDocuments(diagnosticData.getFirstSignatureId());
	assertEquals(1, originals.size());

	DSSDocument original = originals.get(0);

	try {
		Canonicalizer canon = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N11_OMIT_COMMENTS);
		String firstDocument = new String(canon.canonicalize(DSSUtils.toByteArray(documentToSign)));
		String secondDocument = new String(canon.canonicalize(DSSUtils.toByteArray(original)));
		assertEquals(firstDocument, secondDocument);
	} catch (Exception e) {
		fail(e);
	}
}
 
Example 5
Source File: AbstractPkiFactoryTestSignature.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private String getDigest(DSSDocument doc, boolean toBeCanonicalized) {
	byte[] byteArray = DSSUtils.toByteArray(doc);
	if (toBeCanonicalized) {
		try {
			// we canonicalize to ignore the header (which is not covered by the signature)
			Canonicalizer c14n = Canonicalizer.getInstance(getCanonicalizationMethod());
			byteArray = c14n.canonicalize(byteArray);
		} catch (XMLSecurityException | ParserConfigurationException | IOException | SAXException e) {
			// Not always able to canonicalize (more than one file can be covered (XML +
			// something else) )
		}
	}
	// LOG.info("Bytes : {}", new String(byteArray));
	return Utils.toBase64(DSSUtils.digest(DigestAlgorithm.SHA256, byteArray));
}
 
Example 6
Source File: XAdESCanonicalizationTest.java    From dss with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void checkOriginalDocument(Document doc) throws Exception {
	// ------------------------------------ ORIGINAL FILE
	// -----------------------------------------------------
	String originalFileDigest = "";
	byte[] originalFileByteArray = null;

	if (packaging == SignaturePackaging.ENVELOPED) {
		// Original File base64 extraction + Verification
		originalFileDigest = getReferenceDigest(doc, "");

		NodeList transformNodes = getReferenceTransforms(doc, "");
		String algo = getTransformAlgo(transformNodes.item(1));

		Canonicalizer canonicalizer = Canonicalizer.getInstance(algo);

		File orginalFile = new File("src/test/resources/sample.xml");
		// Transform original file into byte array
		byte[] fileContent = Files.readAllBytes(orginalFile.toPath());
		originalFileByteArray = canonicalizer.canonicalize(fileContent);
	} else {
		// Original File base64 extraction + Verification
		NodeList originalFileNodeList = DomUtils.getNodeList(doc, AbstractPaths.all(XMLDSigElement.OBJECT));
		assertNotNull(originalFileNodeList);
		assertEquals(2, originalFileNodeList.getLength());

		Node orignalFile = originalFileNodeList.item(1);

		NamedNodeMap originalFileAttributes = orignalFile.getAttributes();
		Node originalFileId = originalFileAttributes.getNamedItem("Id");
		assertNotNull(originalFileId);

		// Extract original file digest
		originalFileDigest = getReferenceDigest(doc, "#" + originalFileId.getNodeValue());

		// Calculate Original File digest from retrieved base64
		String originalBase64String = orignalFile.getTextContent();
		// Get byte array from base64 string
		originalFileByteArray = Base64.getDecoder().decode(originalBase64String);
	}

	// Calculate Original File Digest
	byte[] digestOriginalFile = DSSUtils.digest(DigestAlgorithm.SHA256, originalFileByteArray);
	String originalDigestBase64 = Base64.getEncoder().encodeToString(digestOriginalFile);

	// Assert that both values are equivalent
	assertEquals(originalFileDigest, originalDigestBase64);
}
 
Example 7
Source File: DSSXMLUtils.java    From dss with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * This method canonicalizes the given array of bytes using the {@code canonicalizationMethod} parameter.
 *
 * @param canonicalizationMethod
 *            canonicalization method
 * @param toCanonicalizeBytes
 *            array of bytes to canonicalize
 * @return array of canonicalized bytes
 * @throws DSSException
 *             if any error is encountered
 */
public static byte[] canonicalize(final String canonicalizationMethod, final byte[] toCanonicalizeBytes) throws DSSException {
	try {
		final Canonicalizer c14n = Canonicalizer.getInstance(getCanonicalizationMethod(canonicalizationMethod));
		return c14n.canonicalize(toCanonicalizeBytes);
	} catch (Exception e) {
		throw new DSSException("Cannot canonicalize the binaries", e);
	}
}