@@ -18,8 +18,8 @@ function FileKeyInfo(file) {
18
18
this . file = file
19
19
20
20
this . getKeyInfo = function ( key , prefix ) {
21
- prefix = prefix || ''
22
- prefix = prefix ? prefix + ':' : prefix
21
+ prefix = prefix || ''
22
+ prefix = prefix ? prefix + ':' : prefix
23
23
return "<" + prefix + "X509Data></" + prefix + "X509Data>"
24
24
}
25
25
@@ -361,21 +361,20 @@ SignedXml.prototype.validateSignatureValue = function(doc) {
361
361
var signedInfo = utils . findChilds ( this . signatureNode , "SignedInfo" )
362
362
if ( signedInfo . length == 0 ) throw new Error ( "could not find SignedInfo element in the message" )
363
363
364
- /**
365
- * When canonicalization algorithm is non-exclusive, search for ancestor namespaces
366
- * before validating signature.
367
- */
368
- var ancestorNamespaces = [ ] ;
369
364
if ( this . canonicalizationAlgorithm === "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"
370
365
|| this . canonicalizationAlgorithm === "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments" )
371
366
{
372
367
if ( ! doc || typeof ( doc ) !== "object" ) {
373
368
throw new Error ( "When canonicalization method is non-exclusive, whole xml dom must be provided as an argument" ) ;
374
369
}
375
-
376
- ancestorNamespaces = findAncestorNs ( doc , "//*[local-name()='SignedInfo']" ) ;
377
370
}
378
371
372
+ /**
373
+ * Search for ancestor namespaces before validating signature.
374
+ */
375
+ var ancestorNamespaces = [ ] ;
376
+ ancestorNamespaces = findAncestorNs ( doc , "//*[local-name()='SignedInfo']" ) ;
377
+
379
378
var c14nOptions = {
380
379
ancestorNamespaces : ancestorNamespaces
381
380
} ;
@@ -449,59 +448,21 @@ SignedXml.prototype.validateReferences = function(doc) {
449
448
}
450
449
451
450
/**
452
- * When canonicalization algorithm is non-exclusive, search for ancestor namespaces
453
- * before validating references.
451
+ * Search for ancestor namespaces before validating references.
454
452
*/
455
- if ( Array . isArray ( ref . transforms ) ) {
456
- var hasNonExcC14nTransform = false ;
457
- for ( var t in ref . transforms ) {
458
- if ( ! ref . transforms . hasOwnProperty ( t ) ) continue ;
459
-
460
- if ( ref . transforms [ t ] === "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"
461
- || ref . transforms [ t ] === "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments" )
462
- {
463
- hasNonExcC14nTransform = true ;
464
- break ;
465
- }
466
- }
467
-
468
- if ( hasNonExcC14nTransform ) {
469
- ref . ancestorNamespaces = findAncestorNs ( doc , elemXpath ) ;
470
- }
453
+ if ( Array . isArray ( ref . transforms ) ) {
454
+ ref . ancestorNamespaces = findAncestorNs ( doc , elemXpath ) ;
471
455
}
472
456
473
457
var c14nOptions = {
474
458
inclusiveNamespacesPrefixList : ref . inclusiveNamespacesPrefixList ,
475
459
ancestorNamespaces : ref . ancestorNamespaces
476
460
} ;
461
+
477
462
var canonXml = this . getCanonXml ( ref . transforms , elem [ 0 ] , c14nOptions ) ;
478
463
479
464
var hash = this . findHashAlgorithm ( ref . digestAlgorithm )
480
465
var digest = hash . getHash ( canonXml )
481
-
482
- if ( ! validateDigestValue ( digest , ref . digestValue ) ) {
483
- if ( ref . inclusiveNamespacesPrefixList ) {
484
- // fallback: apply InclusiveNamespaces workaround (https://github.com/yaronn/xml-crypto/issues/72)
485
- var prefixList = ref . inclusiveNamespacesPrefixList instanceof Array ? ref . inclusiveNamespacesPrefixList : ref . inclusiveNamespacesPrefixList . split ( ' ' ) ;
486
- var supported_definitions = {
487
- 'xs' : 'http://www.w3.org/2001/XMLSchema' ,
488
- 'xsi' : 'http://www.w3.org/2001/XMLSchema-instance' ,
489
- 'saml' : 'urn:oasis:names:tc:SAML:2.0:assertion'
490
- }
491
-
492
- prefixList . forEach ( function ( prefix ) {
493
- if ( supported_definitions [ prefix ] ) {
494
- elem [ 0 ] . setAttributeNS ( 'http://www.w3.org/2000/xmlns/' , 'xmlns:' + prefix , supported_definitions [ prefix ] ) ;
495
- }
496
- } ) ;
497
-
498
- canonXml = this . getCanonXml ( ref . transforms , elem [ 0 ] , { inclusiveNamespacesPrefixList : ref . inclusiveNamespacesPrefixList } ) ;
499
- digest = hash . getHash ( canonXml ) ;
500
- if ( digest === ref . digestValue ) {
501
- return true ;
502
- }
503
- }
504
- }
505
466
506
467
if ( ! validateDigestValue ( digest , ref . digestValue ) ) {
507
468
this . validationErrors . push ( "invalid signature: for uri " + ref . uri +
@@ -612,9 +573,16 @@ SignedXml.prototype.loadReference = function(ref) {
612
573
transforms . push ( utils . findAttr ( trans , "Algorithm" ) . value )
613
574
}
614
575
615
- var inclusiveNamespaces = xpath . select ( "//*[local-name(.)=' InclusiveNamespaces']" , transformsNode ) ;
576
+ var inclusiveNamespaces = utils . findChilds ( trans , " InclusiveNamespaces" )
616
577
if ( inclusiveNamespaces . length > 0 ) {
617
- inclusiveNamespacesPrefixList = inclusiveNamespaces [ 0 ] . getAttribute ( 'PrefixList' ) ;
578
+ //Should really only be one prefix list, but maybe there's some circumstances where more than one to lets handle it
579
+ for ( var i = 0 ; i < inclusiveNamespaces . length ; i ++ ) {
580
+ if ( inclusiveNamespacesPrefixList ) {
581
+ inclusiveNamespacesPrefixList = inclusiveNamespacesPrefixList + " " + inclusiveNamespaces [ i ] . getAttribute ( 'PrefixList' ) ;
582
+ } else {
583
+ inclusiveNamespacesPrefixList = inclusiveNamespaces [ i ] . getAttribute ( 'PrefixList' ) ;
584
+ }
585
+ }
618
586
}
619
587
}
620
588
@@ -916,10 +884,10 @@ SignedXml.prototype.createSignature = function(signedInfo, prefix) {
916
884
var xmlNsAttr = 'xmlns'
917
885
918
886
if ( prefix ) {
919
- xmlNsAttr += ':' + prefix ;
920
- prefix += ':' ;
887
+ xmlNsAttr += ':' + prefix ;
888
+ prefix += ':' ;
921
889
} else {
922
- prefix = '' ;
890
+ prefix = '' ;
923
891
}
924
892
925
893
//the canonicalization requires to get a valid xml node.
0 commit comments