@@ -333,28 +333,64 @@ SignedXml.defaultNsForPrefix = {
333
333
334
334
SignedXml . findAncestorNs = findAncestorNs ;
335
335
336
- SignedXml . prototype . checkSignature = function ( xml ) {
336
+ SignedXml . prototype . checkSignature = function ( xml , callback ) {
337
+ if ( callback != null && typeof callback !== 'function' ) {
338
+ throw new Error ( "Last paramater must be a callback function" )
339
+ }
340
+
337
341
this . validationErrors = [ ]
338
342
this . signedXml = xml
339
343
340
344
if ( ! this . keyInfoProvider ) {
341
- throw new Error ( "cannot validate signature since no key info resolver was provided" )
345
+ var err = new Error ( "cannot validate signature since no key info resolver was provided" )
346
+ if ( ! callback ) {
347
+ throw err
348
+ } else {
349
+ callback ( err )
350
+ return
351
+ }
342
352
}
343
353
344
354
this . signingKey = this . keyInfoProvider . getKey ( this . keyInfo )
345
- if ( ! this . signingKey ) throw new Error ( "key info provider could not resolve key info " + this . keyInfo )
355
+ if ( ! this . signingKey ) {
356
+ var err = new Error ( "key info provider could not resolve key info " + this . keyInfo )
357
+ if ( ! callback ) {
358
+ throw err
359
+ } else {
360
+ callback ( err )
361
+ return
362
+ }
363
+ }
346
364
347
365
var doc = new Dom ( ) . parseFromString ( xml )
348
366
349
367
if ( ! this . validateReferences ( doc ) ) {
350
- return false ;
351
- }
352
-
353
- if ( ! this . validateSignatureValue ( doc ) ) {
354
- return false ;
368
+ if ( ! callback ) {
369
+ return false ;
370
+ } else {
371
+ callback ( new Error ( 'Could not validate references' ) )
372
+ return
373
+ }
355
374
}
356
375
357
- return true
376
+ if ( ! callback ) {
377
+ //Syncronous flow
378
+ if ( ! this . validateSignatureValue ( doc ) ) {
379
+ return false
380
+ }
381
+ return true
382
+ } else {
383
+ //Asyncronous flow
384
+ this . validateSignatureValue ( doc , function ( err , isValidSignature ) {
385
+ if ( err ) {
386
+ this . validationErrors . push ( "invalid signature: the signature value " +
387
+ this . signatureValue + " is incorrect" )
388
+ callback ( err )
389
+ } else {
390
+ callback ( null , isValidSignature )
391
+ }
392
+ } )
393
+ }
358
394
}
359
395
360
396
SignedXml . prototype . getCanonSignedInfoXml = function ( doc ) {
@@ -399,19 +435,19 @@ SignedXml.prototype.getCanonReferenceXml = function(doc, ref, node) {
399
435
return this . getCanonXml ( ref . transforms , node , c14nOptions )
400
436
}
401
437
402
- SignedXml . prototype . validateSignatureValue = function ( doc ) {
438
+ SignedXml . prototype . validateSignatureValue = function ( doc , callback ) {
403
439
var signedInfoCanon = this . getCanonSignedInfoXml ( doc )
404
440
var signer = this . findSignatureAlgorithm ( this . signatureAlgorithm )
405
- var res = signer . verifySignature ( signedInfoCanon , this . signingKey , this . signatureValue )
406
- if ( ! res ) this . validationErrors . push ( "invalid signature: the signature value " +
441
+ var res = signer . verifySignature ( signedInfoCanon , this . signingKey , this . signatureValue , callback )
442
+ if ( ! res && ! callback ) this . validationErrors . push ( "invalid signature: the signature value " +
407
443
this . signatureValue + " is incorrect" )
408
444
return res
409
445
}
410
446
411
- SignedXml . prototype . calculateSignatureValue = function ( doc ) {
447
+ SignedXml . prototype . calculateSignatureValue = function ( doc , callback ) {
412
448
var signedInfoCanon = this . getCanonSignedInfoXml ( doc )
413
449
var signer = this . findSignatureAlgorithm ( this . signatureAlgorithm )
414
- this . signatureValue = signer . getSignature ( signedInfoCanon , this . signingKey )
450
+ this . signatureValue = signer . getSignature ( signedInfoCanon , this . signingKey , callback )
415
451
}
416
452
417
453
SignedXml . prototype . findSignatureAlgorithm = function ( name ) {
@@ -654,7 +690,15 @@ SignedXml.prototype.addReference = function(xpath, transforms, digestAlgorithm,
654
690
* `append`, `prepend`, `before`, `after`
655
691
*
656
692
*/
657
- SignedXml . prototype . computeSignature = function ( xml , opts ) {
693
+ SignedXml . prototype . computeSignature = function ( xml , opts , callback ) {
694
+ if ( typeof opts === 'function' && callback == null ) {
695
+ callback = opts
696
+ }
697
+
698
+ if ( callback != null && typeof callback !== 'function' ) {
699
+ throw new Error ( "Last paramater must be a callback function" )
700
+ }
701
+
658
702
var doc = new Dom ( ) . parseFromString ( xml ) ,
659
703
xmlNsAttr = "xmlns" ,
660
704
signatureAttrs = [ ] ,
@@ -676,8 +720,14 @@ SignedXml.prototype.computeSignature = function(xml, opts) {
676
720
location . action = location . action || "append" ;
677
721
678
722
if ( validActions . indexOf ( location . action ) === - 1 ) {
679
- throw new Error ( "location.action option has an invalid action: " + location . action +
680
- ", must be any of the following values: " + validActions . join ( ", " ) ) ;
723
+ var err = new Error ( "location.action option has an invalid action: " + location . action +
724
+ ", must be any of the following values: " + validActions . join ( ", " ) ) ;
725
+ if ( ! callback ) {
726
+ throw err ;
727
+ } else {
728
+ callback ( err , null )
729
+ return
730
+ }
681
731
}
682
732
683
733
// automatic insertion of `:`
@@ -719,7 +769,13 @@ SignedXml.prototype.computeSignature = function(xml, opts) {
719
769
var referenceNode = xpath . select ( location . reference , doc ) ;
720
770
721
771
if ( ! referenceNode || referenceNode . length === 0 ) {
722
- throw new Error ( "the following xpath cannot be used because it was not found: " + location . reference ) ;
772
+ var err = new Error ( "the following xpath cannot be used because it was not found: " + location . reference ) ;
773
+ if ( ! callback ) {
774
+ throw err
775
+ } else {
776
+ callback ( err , null )
777
+ return
778
+ }
723
779
}
724
780
725
781
referenceNode = referenceNode [ 0 ] ;
@@ -735,16 +791,39 @@ SignedXml.prototype.computeSignature = function(xml, opts) {
735
791
}
736
792
737
793
this . signatureNode = signatureDoc
738
- this . calculateSignatureValue ( doc )
739
-
740
794
var signedInfoNode = utils . findChilds ( this . signatureNode , "SignedInfo" )
741
- if ( signedInfoNode . length == 0 ) throw new Error ( "could not find SignedInfo element in the message" )
742
-
795
+ if ( signedInfoNode . length == 0 ) {
796
+ var err = new Error ( "could not find SignedInfo element in the message" )
797
+ if ( ! callback ) {
798
+ throw err
799
+ } else {
800
+ callback ( err )
801
+ return
802
+ }
803
+ }
743
804
signedInfoNode = signedInfoNode [ 0 ] ;
744
- signatureDoc . insertBefore ( this . createSignature ( prefix ) , signedInfoNode . nextSibling )
745
805
746
- this . signatureXml = signatureDoc . toString ( )
747
- this . signedXml = doc . toString ( )
806
+ if ( ! callback ) {
807
+ //Synchronous flow
808
+ this . calculateSignatureValue ( doc )
809
+ signatureDoc . insertBefore ( this . createSignature ( prefix ) , signedInfoNode . nextSibling )
810
+ this . signatureXml = signatureDoc . toString ( )
811
+ this . signedXml = doc . toString ( )
812
+ } else {
813
+ var self = this
814
+ //Asynchronous flow
815
+ this . calculateSignatureValue ( doc , function ( err , signature ) {
816
+ if ( err ) {
817
+ callback ( err )
818
+ } else {
819
+ self . signatureValue = signature
820
+ signatureDoc . insertBefore ( self . createSignature ( prefix ) , signedInfoNode . nextSibling )
821
+ self . signatureXml = signatureDoc . toString ( )
822
+ self . signedXml = doc . toString ( )
823
+ callback ( )
824
+ }
825
+ } )
826
+ }
748
827
}
749
828
750
829
SignedXml . prototype . getKeyInfo = function ( prefix ) {
0 commit comments