Skip to content

Commit e0fe511

Browse files
Sri Raghavansrir
authored andcommitted
Fixes for canonicalization and signing
- removed an old (~5y ago) hack for windows store XML canonicalization (and removed the test) - fixed an issue with over-eager prefix inclusion in non-exclusive c14n algorithm (+ test) - replaced a test for empty-URI that is impossible to update (because the private key is missing)
1 parent 8d9571f commit e0fe511

File tree

4 files changed

+39
-69
lines changed

4 files changed

+39
-69
lines changed

lib/c14n-canonicalization.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ C14nCanonicalization.prototype.renderNs = function(node, prefixesInScope, defaul
9595
attr = node.attributes[i];
9696

9797
//handle all prefixed attributes that are included in the prefix list and where
98-
//the prefix is not defined already
99-
if (attr.prefix && prefixesInScope.indexOf(attr.localName) === -1) {
98+
//the prefix is not defined already. New prefixes can only be defined by `xmlns:`.
99+
if (attr.prefix === "xmlns" && prefixesInScope.indexOf(attr.localName) === -1) {
100100
nsListToRender.push({"prefix": attr.localName, "namespaceURI": attr.value});
101101
prefixesInScope.push(attr.localName);
102102
}

lib/signed-xml.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -570,10 +570,6 @@ SignedXml.prototype.loadReference = function(ref) {
570570
});
571571
}
572572

573-
//***workaround for validating windows mobile store signatures - it uses c14n but does not state it in the transforms
574-
if (!hasImplicitTransforms && transforms.length==1 && transforms[0]=="http://www.w3.org/2000/09/xmldsig#enveloped-signature")
575-
transforms.push("http://www.w3.org/2001/10/xml-exc-c14n#")
576-
577573
this.addReference(null, transforms, digestAlgo, utils.findAttr(ref, "URI").value, digestValue, inclusiveNamespacesPrefixList, false)
578574
}
579575

test/c14n-non-exclusive-unit-test.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ var test_findAncestorNs = function(test, xml, xpath, expected){
2323
test.done();
2424
};
2525

26-
27-
28-
29-
30-
26+
// Tests for findAncestorNs
3127
exports["findAncestorNs: Correctly picks up root ancestor namespace"] = function(test){
3228
var xml = "<root xmlns:aaa='bbb'><child1><child2></child2></child1></root>";
3329
var xpath = "/root/child1/child2";
@@ -108,15 +104,7 @@ exports["findAncestorNs: Ignores namespace declared in the target xpath node"] =
108104
test_findAncestorNs(test, xml, xpath, expected);
109105
};
110106

111-
112-
113-
114-
115-
116-
117-
118-
119-
107+
// Tests for c14nCanonicalization
120108
exports["C14n: Correctly picks up root ancestor namespace"] = function(test){
121109
var xml = "<root xmlns:aaa='bbb'><child1><child2></child2></child1></root>";
122110
var xpath = "/root/child1/child2";
@@ -179,4 +167,12 @@ exports["C14n: Preserve namespace declared in the target xpath node"] = function
179167
var expected = '<child2 xmlns:aaa="bbb" xmlns:ccc="ddd"></child2>';
180168

181169
test_C14nCanonicalization(test, xml, xpath, expected);
182-
};
170+
};
171+
172+
exports["C14n: Don't redeclare an attribute's namespace prefix if already in scope"] = function(test) {
173+
var xml = "<root xmlns:aaa='bbb'><child1><child2 xmlns:aaa='bbb' aaa:foo='bar'></child2></child1></root>"
174+
var xpath = "/root/child1/child2";
175+
var expected = '<child2 xmlns:aaa="bbb" aaa:foo="bar"></child2>';
176+
177+
test_C14nCanonicalization(test, xml, xpath, expected);
178+
}

test/signature-integration-tests.js

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -33,60 +33,38 @@ module.exports = {
3333

3434

3535

36-
"empty URI reference should consider the whole document": function(test) {
37-
38-
var sampleXml=["<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
39-
"<root>",
40-
" <a>",
41-
" <b/>",
42-
" </a>",
43-
" <Seal><Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\"/><SignatureMethod Algorithm=\"http://www.w3.org/2001/04/xmldsig-more#rsa-sha256\"/><Reference URI=\"\"><Transforms><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\"/></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2001/04/xmlenc#sha256\"/><DigestValue>FOezc5yb1O+LfQaD4UBKEUphrGzFAq5DM9B9ll37JOA=</DigestValue></Reference></SignedInfo><SignatureValue>AjkQ5NF71bwJ2YHIs8jbqva9qaNv66BYZiZw0JJZ1cW6jf3mjWShIMQZWcw78QGpzzr+ZspzUbs4",
44-
"6VAnHApJElOTDylSf3rDSvzsklKcFpHJ9yCJV+PnipEsY8qWhzKHlKCdtEn1xH0BCP/2JfMYgLQl",
45-
"PCvaR8XrgdODeQ2Gn6g=</SignatureValue><KeyInfo><KeyValue><RSAKeyValue><Modulus>t+qknJd/Kdo09fvQrRThqh/3EyDQj8zT1ZT7uXmivni4Vaysf6zHv+oORIvAt9ntZE2ZCif9v6CC",
46-
"W+hmRFkdgRoVpmD2TErjykzowx6Ffyf5BkVnVB89+g/ZqNyyvXiBe8SmpBrRLOMifnbacyrJcsrH",
47-
"fwlCnuyGKXj1LfzDcR8=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue></KeyInfo></Signature></Seal>",
48-
" <c>",
49-
" <d e=\"f\"/>",
50-
" </c>",
51-
"</root>"].join("\n");
52-
53-
var doc = new Dom().parseFromString(sampleXml);
36+
"empty URI reference should consider the whole document": function(test) {
37+
var xml = "<library>" +
38+
"<book>" +
39+
"<name>Harry Potter</name>" +
40+
"</book>" +
41+
"</library>";
42+
43+
var signature = '<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">' +
44+
'<SignedInfo>' +
45+
'<CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>' +
46+
'<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>' +
47+
'<Reference URI="">' +
48+
'<Transforms>' +
49+
'<Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>' +
50+
'</Transforms>' +
51+
'<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>' +
52+
'<DigestValue>1tjZsV007JgvE1YFe1C8sMQ+iEg=</DigestValue>' +
53+
'</Reference>' +
54+
'</SignedInfo>' +
55+
'<SignatureValue>FONRc5/nnQE2GMuEV0wK5/ofUJMHH7dzZ6VVd+oHDLfjfWax/lCMzUahJxW1i/dtm9Pl0t2FbJONVd3wwDSZzy6u5uCnj++iWYkRpIEN19RAzEMD1ejfZET8j3db9NeBq2JjrPbw81Fm7qKvte6jGa9ThTTB+1MHFRkC8qjukRM=</SignatureValue>' +
56+
'</Signature>';
57+
58+
var sig = new crypto.SignedXml()
59+
sig.keyInfoProvider = new crypto.FileKeyInfo("./test/static/client_public.pem")
60+
sig.loadSignature(signature);
5461

55-
var signature = crypto.xpath(doc, "//*//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']")[0];
56-
var sig = new crypto.SignedXml();
57-
sig.keyInfoProvider = new crypto.FileKeyInfo("./test/static/empty_uri.pem");
58-
sig.loadSignature(signature);
59-
var result = sig.checkSignature(sampleXml);
60-
test.equal(result, true);
61-
test.done();
62-
},
63-
64-
65-
66-
"windows store signature": function(test) {
67-
68-
var xml = fs.readFileSync('./test/static/windows_store_signature.xml', 'utf-8');
69-
70-
// Make sure that whitespace in the source document is removed -- see xml-crypto issue #23 and post at
71-
// http://webservices20.blogspot.co.il/2013/06/validating-windows-mobile-app-store.html
72-
// This regex is naive but works for this test case; for a more general solution consider
73-
// the xmldom-fork-fixed library which can pass {ignoreWhiteSpace: true} into the Dom constructor.
74-
xml = xml.replace(/>\s*</g, '><');
75-
76-
var doc = new Dom().parseFromString(xml);
77-
xml = doc.firstChild.toString()
78-
79-
var signature = crypto.xpath(doc, "//*//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']")[0];
80-
var sig = new crypto.SignedXml();
81-
sig.keyInfoProvider = new crypto.FileKeyInfo("./test/static/windows_store_certificate.pem");
82-
sig.loadSignature(signature);
8362
var result = sig.checkSignature(xml);
8463
test.equal(result, true);
8564
test.done();
8665
},
8766

8867

89-
9068
"signature with inclusive namespaces": function(test) {
9169

9270
var xml = fs.readFileSync('./test/static/signature_with_inclusivenamespaces.xml', 'utf-8');
@@ -190,7 +168,7 @@ function verifySignature(test, xml, expected, xpath) {
190168

191169
var sig = new SignedXml()
192170
sig.signingKey = fs.readFileSync("./test/static/client.pem")
193-
sig.keyInfoCaluse = null
171+
sig.keyInfo = null;
194172

195173
xpath.map(function(n) { sig.addReference(n) })
196174

0 commit comments

Comments
 (0)