Skip to content

Commit d06dd9c

Browse files
committed
add support for generating .NET compliant signed xml
1 parent 4643877 commit d06dd9c

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

lib/exclusive-canonicalization.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,5 @@ ExclusiveCanonicalization.prototype.process = function(node, options) {
151151
};
152152

153153
ExclusiveCanonicalization.prototype.getAlgorithmName = function() {
154-
return "http://www.w3.org/2001/10/xml-exc-c14n#";
154+
return "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
155155
};

lib/signed-xml.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function SignedXml(idMode, options) {
140140
this.signingKey = null
141141
this.signatureAlgorithm = this.options.signatureAlgorithm || "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
142142
this.keyInfoProvider = null
143-
this.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#"
143+
this.canonicalizationAlgorithm = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"
144144
this.signedXml = ""
145145
this.signatureXml = ""
146146
this.signatureXmlDoc = null
@@ -153,7 +153,7 @@ function SignedXml(idMode, options) {
153153
}
154154

155155
SignedXml.CanonicalizationAlgorithms = {
156-
'http://www.w3.org/2001/10/xml-exc-c14n#': ExclusiveCanonicalization,
156+
'http://www.w3.org/TR/2001/REC-xml-c14n-20010315': ExclusiveCanonicalization,
157157
'http://www.w3.org/2000/09/xmldsig#enveloped-signature': EnvelopedSignature
158158
}
159159

@@ -247,7 +247,8 @@ SignedXml.prototype.validateReferences = function(doc) {
247247
ref.uri + " but could not find such element in the xml")
248248
return false
249249
}
250-
var canonXml = this.getCanonXml(ref.transforms, elem[0], { inclusiveNamespacesPrefixList: ref.inclusiveNamespacesPrefixList });
250+
var canonXml = this.getCanonXml(ref.transforms, elem[0], { inclusiveNamespacesPrefixList: ref.inclusiveNamespacesPrefixList });
251+
fs.writeFileSync("../canon.xml", canonXml);
251252

252253
var hash = this.findHashAlgorithm(ref.digestAlgorithm)
253254
var digest = hash.getHash(canonXml)
@@ -334,15 +335,15 @@ SignedXml.prototype.loadReference = function(ref) {
334335

335336
//***workaround for validating windows mobile store signatures - it uses c14n but does not state it in the transforms
336337
if (transforms.length==1 && transforms[0]=="http://www.w3.org/2000/09/xmldsig#enveloped-signature")
337-
transforms.push("http://www.w3.org/2001/10/xml-exc-c14n#")
338+
transforms.push("http://www.w3.org/TR/2001/REC-xml-c14n-20010315")
338339

339340
this.addReference(null, transforms, digestAlgo, utils.findAttr(ref, "URI").value, digestValue, inclusiveNamespacesPrefixList, false)
340341
}
341342

342343
SignedXml.prototype.addReference = function(xpath, transforms, digestAlgorithm, uri, digestValue, inclusiveNamespacesPrefixList, isEmptyUri) {
343344
this.references.push({
344345
"xpath": xpath,
345-
"transforms": transforms ? transforms : ["http://www.w3.org/2001/10/xml-exc-c14n#"] ,
346+
"transforms": transforms ? transforms : ["http://www.w3.org/TR/2001/REC-xml-c14n-20010315"] ,
346347
"digestAlgorithm": digestAlgorithm ? digestAlgorithm : "http://www.w3.org/2000/09/xmldsig#sha1",
347348
"uri": uri,
348349
"digestValue": digestValue,
@@ -447,10 +448,16 @@ SignedXml.prototype.createReferences = function(doc) {
447448

448449
SignedXml.prototype.getCanonXml = function(transforms, node, options) {
449450
var canonXml = node
450-
for (var t in transforms) {
451-
if (!transforms.hasOwnProperty(t)) continue;
452451

453-
var transform = this.findCanonicalizationAlgorithm(transforms[t])
452+
//***workaround for creating signatures that can be validated by .NET's SignedXml class - it uses c14n but does not state it in the transforms
453+
var transformsCopy = transforms.slice()
454+
if (transformsCopy.length==1 && transformsCopy[0]=="http://www.w3.org/2000/09/xmldsig#enveloped-signature")
455+
transformsCopy.push("http://www.w3.org/TR/2001/REC-xml-c14n-20010315")
456+
457+
for (var t in transformsCopy) {
458+
if (!transformsCopy.hasOwnProperty(t)) continue;
459+
460+
var transform = this.findCanonicalizationAlgorithm(transformsCopy[t])
454461
canonXml = transform.process(canonXml, options);
455462
//TODO: currently transform.process may return either Node or String value (enveloped transformation returns Node, exclusive-canonicalization returns String).
456463
//This eitehr needs to be more explicit in the API, or all should return the same.

0 commit comments

Comments
 (0)