Java源码示例:org.apache.xml.security.c14n.InvalidCanonicalizerException
示例1
/**
* 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;
}
}
示例2
@Override
protected Canonicalizer initialValue() {
try {
return Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
} catch (InvalidCanonicalizerException e) {
throw new RuntimeException(e);
}
}
示例3
/**
* 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);
}
}
示例4
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);
}
示例5
public CanonXMLValueFactory() throws InvalidCanonicalizerException, ParserConfigurationException {
org.apache.xml.security.Init.init();
c14n = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
}
示例6
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));
}
示例7
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;
}
示例8
public StaxSerializer() throws InvalidCanonicalizerException {
super(Canonicalizer.ALGO_ID_C14N_PHYSICAL, true);
}