Coder Social home page Coder Social logo

xml-c14n's Introduction

xml-c14n

XML canonicalisation (xml-c14n)

Overview

This module performs XML canonicalisation as specified in xml-c14n.

To operate, a preconstructed DOM object is required. Any object that implements the DOM Level 1 API will suffice. I recommend xmldom if you're working with node, or your browser's native DOM implementation if you're not.

This module was originally adapted from some code in xml-crypto by Yaron Naveh, and as such is also covered by any additional conditions in the license of that codebase (which just so happens to be the MIT license).

Apology

Look, I know the API feels like Java. It pains me as much to work on it as it will pain you to use it. This whole XML thing is a crock of shit, and is so over-engineered that a factory (yeah, you heard me right) was the best way to implement it. So yeah... Sorry.

No Apology

I spell canonicalise with an "s". Deal with it.

Super Quickstart

Also see example.js.

#!/usr/bin/env node

var xmldom = require("xmldom");

var c14n = require("xml-c14n")();

var xmlData = require("fs").readFileSync(process.argv[2], "utf8"),
    document = (new xmldom.DOMParser()).parseFromString(xmlData);

var canonicaliser = c14n.createCanonicaliser("http://www.w3.org/2001/10/xml-exc-c14n#WithComments");

console.log("canonicalising with algorithm: " + canonicaliser.name());
console.log("");

console.log("INPUT");
console.log("");
console.log(xmlData);

console.log("");

canonicaliser.canonicalise(document.documentElement, function(err, res) {
  if (err) {
    return console.warn(err.stack);
  }

  console.log("RESULT");
  console.log("");
  console.log(res);
});
โžœ  xml-c14n git:(master) ./example.js small.xml
canonicalising with algorithm: http://www.w3.org/2001/10/xml-exc-c14n#WithComments

INPUT

<?xml version="1.0"?>

<?xml-stylesheet   href="doc.xsl"
   type="text/xsl"   ?>

<!DOCTYPE doc SYSTEM "doc.dtd">

<doc>Hello, world!<!-- Comment 1 --></doc>

<?pi-without-data     ?>

<!-- Comment 2 -->

<!-- Comment 3 -->


RESULT

<doc>Hello, world!<!-- Comment 1 --></doc>

Installation

Available via npm:

$ npm install xml-c14n

Or via git:

$ git clone git://github.com/deoxxa/xml-c14n.git node_modules/xml-c14n

API

CanonicalisationFactory

This is what you get when you require("xml-c14n"). It's a factory for getting canonicaliser implementation instances.

CanonicalisationFactory(options)
var c14n = new CanonicalisationFactory();
// OR
var c14n = CanonicalisationFactory();
// THUS
var c14n = require("xml-c14n")();

CanonicalisationFactory.registerAlgorithm

This is how you get a specific canonicalisation algorithm implementation into a factory so that it can be instantiated within and returned to callers. You give it a URI and a factory function (sup dog) and it'll shove it into an object internally so it can be retrieved later on.

c14n.registerAlgorithm(uri, factoryFunction)
c14n.registerAlgorithm("http://herp.derp/", function(options) {
  return new HerpDerp(options);
});

Arguments

  • uri - a URI to identify the algorithm
  • factoryFunction - a function that creates instances of the algorithm's implementation

CanonicalisationFactory.getAlgorithm

This lets you get the factory function for an algorithm. Not incredibly useful, but here for completeness.

c14n.getAlgorithm(uri)
var herpDerpFactory = c14n.getAlgorithm("http://herp.derp/");

Arguments

  • uri - the URI identifying the algorithm to fetch the factory function for

CanonicalisationFactory.createCanonicaliser

Creates an instance of a canonicaliser, referenced by its URI, and optionally passing along something for the new instance.

c14n.createCanonicaliser(uri, [options]);
// creates an xml-exc-c14n canonicaliser
var canonicaliser = c14n.createCanonicaliser("http://www.w3.org/2001/10/xml-exc-c14n#");

Algorithm

This is the abstract "class" that all specific algorithms (should) extend. It provides some stubbed out methods that do nothing useful aside from serving as documentation. These methods should be overridden by specific implementations. What you get back from CanonicalisationFactory.createCanonicaliser will be an extension of this class.

Algorithm.name

This gives you the name (URI) that the algorithm instance goes by.

algorithm.name();
var uri = algorithm.name();

Algorithm.canonicalise

This is what does the meat of the work in most implementations.

algorithm.canonicalise(node, cb);
algorithm.canonicalise(node, function(err, data) {
  if (err) {
    return console.warn(err);
  }

  console.log(data);
});

Included Canonicalisation Algorithms

There are two included algorithms, one of which is a specialisation of the other.

http://www.w3.org/2001/10/xml-exc-c14n#

  • uri - http://www.w3.org/2001/10/xml-exc-c14n#
  • options
    • includeComments
    • inclusiveNamespaces

For a description of this algorithm and its options, see the xml-exc-c14n specification.

http://www.w3.org/2001/10/xml-exc-c14n#WithComments

  • uri - http://www.w3.org/2001/10/xml-exc-c14n#WithComments
  • options
    • inclusiveNamespaces

This is just a special version of http://www.w3.org/2001/10/xml-exc-c14n# with includeComments enabled.

License

3-clause BSD. A copy is included with the source.

Contact

xml-c14n's People

Contributors

deoxxa avatar scottylogan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

xml-c14n's Issues

Signature verification fails on Shibboleth 2.4 assertions

I tried using this code (along with saml2 and xml-c14n) to process SAML 2.0 assertions from a Shibboeth 2.4 IdP. The verification failed because xml-dsig / xml-c14n ignore the InclusiveNamespaces child element of the canonicalization transform:

<ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
  <ec:InclusiveNamespaces
    xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#"
    PrefixList="xs"/>
</ds:Transform>

For example, when the original SAML Assertion starts with

<saml2:Assertion
  xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion"
  ID="_7cb1dd592c72247faaa5a9e29cb95939"
  IssueInstant="2013-08-31T09:56:50.182Z"
  Version="2.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">

Shibboleth / OpenSAML canonicalizes it to

<saml2:Assertion
  xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  ID="_7cb1dd592c72247faaa5a9e29cb95939"
  IssueInstant="2013-08-31T09:56:50.182Z"
  Version="2.0">

but xml-dsig canonicalizes it to

<saml2:Assertion
  xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion"
  ID="_7cb1dd592c72247faaa5a9e29cb95939"
  IssueInstant="2013-08-31T09:56:50.182Z"
  Version="2.0">

Browser version troubles

I performed browserify exclusive-canonicalisation -o > bundled.js, included script to my page but couldn't instantiate c14n object.

I tried:

var c14n = new CanonicalisationFactory();
// OR
var c14n = CanonicalisationFactory();
// THUS
var c14n = require("xml-c14n")();

but c14n is not defined. How make xml-c14n browser compatible?

Thanks.

Exclusive Canonicalization _compareNamespaces is not spec complaint and creates invalid digests

This issue is basically identical to the issue I just posted here: node-saml/xml-crypto#25

Basically both libraries fail to sort the namespaces correctly. I'm guessing that this library was ported from or started from xml-crypto since this specific code looks similar and has the same problem.

I'm pasting the same issue here, modified for this library:

The namespace compare method '_compareNamespaces ' in exclusive-canonicalization.js is not compliant with the XML Canonicalization specs. The bug in this code is causing the canonical XML to be ordered slightly differently and thus failing signature validation because the digest values are being hashed over malformed canonicalXML.
http://www.w3.org/TR/xml-exc-c14n/ which refers to the rules specified in http://www.ietf.org/rfc/rfc3076.txt .

Specifically, the spec states:
"

An element's namespace nodes are sorted lexicographically by local name (the default namespace node, if one exists, has no local name and is therefore lexicographically least).
An element's attribute nodes are sorted lexicographically with namespace URI as the primary key and local name as the secondary key (an empty namespace URI is lexicographically least). "
For the namespace comparison, instead of sorting by just local name (per spec) the code does this instead:
var attr1 = a.prefix + a.namespaceURI,
attr2 = b.prefix + b.namespaceURI;

if (attr1 === attr2) {
return 0;
}

return attr1.localeCompare(attr2);

For clarification, the 'prefix' here is actually the local name.

But the code is incorrect because the namespaceURI should not be a part of the comparison.

What ends up happening is that if you have something like this:
xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"

by doing the namespace comparison of the localName appended with the namespaceURI this string comparison will compare 'samlpurn:oasis:names:tc:SAML:2.0:protocol' to 'samlurnoasis:names:tc:SAML:2.0:assertion'
and the resulting canonicalXML will be incorrectly out of order.

EXPECTED (correct) canonical XML:
<Response xmlns="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" Destination....

ACTUAL (incorrect) canonicalXML as a result of the bad namespace comparison:
<Response xmlns="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" Destination=....

Verification of the digest value will fail since it is performed on the incorrect canonicalXML.

The namespace comparison should sort based on just the localNames, here as 'saml' to 'samlp'.

The following code snippet shows one way this can be corrected to be spec compliant:
var _compareNamespaces = function _compareNamespaces(a, b) {
var attr1 = a.prefix; // don't compare a.namespaceURI,
var attr2 = b.prefix; // don't compare b.namespaceURI;

if (attr1 === attr2) {
return 0;
}

return attr1.localeCompare(attr2);
};
While you're in there, you might also want to double check the attribute comparison too to make sure that is spec compliant as well (at a glance I'm not sure if the attribute comparison is correct either because it doesn't look like it's using the namespaceURI as a primary key for sorting first , it looks like it may use the localName if present instead of namespaceURI).

setImmediate is not defined

Hi, thanks for the code :)

I am trying to use it to canonalize xml in browser and I get:

ReferenceError: setImmediate is not defined
at ExclusiveCanonicalisation.canonicalise (exclusive-canonicalisation.js:23)

Any help is highly appreciated

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.