@@ -11,10 +11,11 @@ import type {
1111 Reference ,
1212 SignatureAlgorithmName ,
1313 SignedXmlOptions ,
14- HashAlgorithmMap ,
14+ DigestAlgorithmMap ,
1515 SignatureAlgorithmMap ,
1616 CanonicalizationAlgorithmMap ,
1717 TransformAlgorithmMap ,
18+ VerificationIdAttributeType ,
1819} from "./types" ;
1920
2021import * as isDomNode from "@xmldom/is-dom-node" ;
@@ -28,7 +29,14 @@ import * as execC14n from "./exclusive-canonicalization";
2829import * as hashAlgorithms from "./hash-algorithms" ;
2930import * as signatureAlgorithms from "./signature-algorithms" ;
3031import * as utils from "./utils" ;
31- import { Algorithms , Namespaces } from "./constants" ;
32+ import { XMLDSIG_URIS } from "./xmldsig-uris" ;
33+ const {
34+ CANONICALIZATION_ALGORITHMS ,
35+ DIGEST_ALGORITHMS ,
36+ SIGNATURE_ALGORITHMS ,
37+ TRANSFORM_ALGORITHMS ,
38+ NAMESPACES ,
39+ } = XMLDSIG_URIS ;
3240
3341export class SignedXml {
3442 /**
@@ -37,11 +45,12 @@ export class SignedXml {
3745 */
3846 idMode ?: "wssecurity" ;
3947 /**
40- * Specifies the Id attributes which will be usedd to resolve reference URIs.
41- * When signing, if no Id attribute is found on the element to be signed, the first one from this list will be added.
48+ * Specifies the Id attributes which will be used to resolve reference URIs.
49+ * When signing, if no Id attribute is found on the element to be signed the first one from this list will be added.
50+ * If idAttribute is also specified, it will be added to the start of this list.
4251 *
43- * @default ["Id", "ID", "id"]
44- * @example [{ prefix: "wsu", localName: "Id", namespaceUri: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" }]
52+ * @default { @link SignedXml.getDefaultIdAttributes() }
53+ * @example [{localName: "Id", namespaceUri: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" }]
4554 */
4655 idAttributes : IdAttributeType [ ] ;
4756 /**
@@ -107,7 +116,7 @@ export class SignedXml {
107116 /**
108117 * To add a new hash algorithm create a new class that implements the {@link HashAlgorithm} interface, and register it here. More info: {@link https://github.com/node-saml/xml-crypto#customizing-algorithms|Customizing Algorithms}
109118 */
110- HashAlgorithms : HashAlgorithmMap ;
119+ HashAlgorithms : DigestAlgorithmMap ;
111120
112121 /**
113122 * To add a new signature algorithm create a new class that implements the {@link SignatureAlgorithm} interface, and register it here. More info: {@link https://github.com/node-saml/xml-crypto#customizing-algorithms|Customizing Algorithms}
@@ -120,42 +129,42 @@ export class SignedXml {
120129 TransformAlgorithms : TransformAlgorithmMap | undefined ;
121130
122131 static defaultNsForPrefix = {
123- ds : Namespaces . ds ,
132+ ds : NAMESPACES . ds ,
124133 } ;
125134
126135 static noop = ( ) => null ;
127136
128- static readonly getDefaultCanonicalizationAlgorithms = ( ) => ( {
129- [ Algorithms . canonicalization . C14N ] : c14n . C14nCanonicalization ,
130- [ Algorithms . canonicalization . C14N_WITH_COMMENTS ] : c14n . C14nCanonicalizationWithComments ,
131- [ Algorithms . canonicalization . EXCLUSIVE_C14N ] : execC14n . ExclusiveCanonicalization ,
132- [ Algorithms . canonicalization . EXCLUSIVE_C14N_WITH_COMMENTS ] :
137+ static readonly getDefaultCanonicalizationAlgorithms = ( ) : CanonicalizationAlgorithmMap => ( {
138+ [ CANONICALIZATION_ALGORITHMS . C14N ] : c14n . C14nCanonicalization ,
139+ [ CANONICALIZATION_ALGORITHMS . C14N_WITH_COMMENTS ] : c14n . C14nCanonicalizationWithComments ,
140+ [ CANONICALIZATION_ALGORITHMS . EXCLUSIVE_C14N ] : execC14n . ExclusiveCanonicalization ,
141+ [ CANONICALIZATION_ALGORITHMS . EXCLUSIVE_C14N_WITH_COMMENTS ] :
133142 execC14n . ExclusiveCanonicalizationWithComments ,
134143 // TODO: separate TransformAlgorithms from CanonicalizationAlgorithms
135- [ Algorithms . transform . ENVELOPED_SIGNATURE ] : envelopedSignatures . EnvelopedSignature ,
144+ [ TRANSFORM_ALGORITHMS . ENVELOPED_SIGNATURE ] : envelopedSignatures . EnvelopedSignature ,
136145 } ) ;
137146
138- static readonly getDefaultHashAlgorithms = ( ) => ( {
147+ static readonly getDefaultDigestAlgorithms = ( ) : DigestAlgorithmMap => ( {
139148 // TODO: In v7.x we may consider removing sha1 from defaults
140- [ Algorithms . hash . SHA1 ] : hashAlgorithms . Sha1 ,
141- [ Algorithms . hash . SHA256 ] : hashAlgorithms . Sha256 ,
142- [ Algorithms . hash . SHA512 ] : hashAlgorithms . Sha512 ,
149+ [ DIGEST_ALGORITHMS . SHA1 ] : hashAlgorithms . Sha1 ,
150+ [ DIGEST_ALGORITHMS . SHA256 ] : hashAlgorithms . Sha256 ,
151+ [ DIGEST_ALGORITHMS . SHA512 ] : hashAlgorithms . Sha512 ,
143152 } ) ;
144153
145- static readonly getDefaultSignatureAlgorithms = ( ) => ( {
154+ static readonly getDefaultSignatureAlgorithms = ( ) : SignatureAlgorithmMap => ( {
146155 // TODO: In v7.x we may consider removing rsa-sha1 from defaults
147- [ Algorithms . signature . RSA_SHA1 ] : signatureAlgorithms . RsaSha1 ,
148- [ Algorithms . signature . RSA_SHA256 ] : signatureAlgorithms . RsaSha256 ,
149- [ Algorithms . signature . RSA_SHA256_MGF1 ] : signatureAlgorithms . RsaSha256Mgf1 ,
150- [ Algorithms . signature . RSA_SHA512 ] : signatureAlgorithms . RsaSha512 ,
156+ [ SIGNATURE_ALGORITHMS . RSA_SHA1 ] : signatureAlgorithms . RsaSha1 ,
157+ [ SIGNATURE_ALGORITHMS . RSA_SHA256 ] : signatureAlgorithms . RsaSha256 ,
158+ [ SIGNATURE_ALGORITHMS . RSA_SHA256_MGF1 ] : signatureAlgorithms . RsaSha256Mgf1 ,
159+ [ SIGNATURE_ALGORITHMS . RSA_SHA512 ] : signatureAlgorithms . RsaSha512 ,
151160 // Disabled by default due to key confusion concerns.
152161 // 'http://www.w3.org/2000/09/xmldsig#hmac-sha1': SignatureAlgorithms.HmacSha1
153162 } ) ;
154163
155- static readonly getDefaultTransformAlgorithms = ( ) =>
164+ static readonly getDefaultTransformAlgorithms = ( ) : TransformAlgorithmMap =>
156165 SignedXml . getDefaultCanonicalizationAlgorithms ( ) ;
157166
158- static readonly getDefaultIdAttributes = ( ) => [ "Id" , "ID" , "id" ] ;
167+ static readonly getDefaultIdAttributes = ( ) : VerificationIdAttributeType [ ] => [ "Id" , "ID" , "id" ] ;
159168
160169 /**
161170 * The SignedXml constructor provides an abstraction for sign and verify xml documents. The object is constructed using
@@ -178,7 +187,7 @@ export class SignedXml {
178187 getCertFromKeyInfo,
179188 objects,
180189 allowedSignatureAlgorithms,
181- allowedHashAlgorithms ,
190+ allowedDigestAlgorithms ,
182191 allowedCanonicalizationAlgorithms,
183192 allowedTransformAlgorithms,
184193 } = options ;
@@ -206,7 +215,7 @@ export class SignedXml {
206215 this . objects = objects ;
207216 this . CanonicalizationAlgorithms =
208217 allowedCanonicalizationAlgorithms ?? SignedXml . getDefaultCanonicalizationAlgorithms ( ) ;
209- this . HashAlgorithms = allowedHashAlgorithms ?? SignedXml . getDefaultHashAlgorithms ( ) ;
218+ this . HashAlgorithms = allowedDigestAlgorithms ?? SignedXml . getDefaultDigestAlgorithms ( ) ;
210219 this . SignatureAlgorithms =
211220 allowedSignatureAlgorithms ?? SignedXml . getDefaultSignatureAlgorithms ( ) ;
212221 // TODO: use default transform algorithms if not provided (breaking change)
@@ -220,7 +229,7 @@ export class SignedXml {
220229 */
221230 enableHMAC ( ) : void {
222231 this . SignatureAlgorithms = {
223- [ Algorithms . signature . HMAC_SHA1 ] : signatureAlgorithms . HmacSha1 ,
232+ [ SIGNATURE_ALGORITHMS . HMAC_SHA1 ] : signatureAlgorithms . HmacSha1 ,
224233 } ;
225234 this . getKeyInfoContent = SignedXml . noop ;
226235 }
@@ -443,8 +452,8 @@ export class SignedXml {
443452 }
444453
445454 if (
446- this . canonicalizationAlgorithm === Algorithms . canonicalization . C14N ||
447- this . canonicalizationAlgorithm === Algorithms . canonicalization . C14N_WITH_COMMENTS
455+ this . canonicalizationAlgorithm === CANONICALIZATION_ALGORITHMS . C14N ||
456+ this . canonicalizationAlgorithm === CANONICALIZATION_ALGORITHMS . C14N_WITH_COMMENTS
448457 ) {
449458 if ( ! doc || typeof doc !== "object" ) {
450459 throw new Error (
@@ -663,7 +672,7 @@ export class SignedXml {
663672
664673 findSignatures ( doc : Node ) : Node [ ] {
665674 const nodes = xpath . select (
666- `//*[local-name(.)='Signature' and namespace-uri(.)='${ Namespaces . ds } ']` ,
675+ `//*[local-name(.)='Signature' and namespace-uri(.)='${ NAMESPACES . ds } ']` ,
667676 doc ,
668677 ) ;
669678
@@ -727,10 +736,10 @@ export class SignedXml {
727736 let canonicalizationAlgorithmForSignedInfo = this . canonicalizationAlgorithm ;
728737 if (
729738 ! canonicalizationAlgorithmForSignedInfo ||
730- canonicalizationAlgorithmForSignedInfo === Algorithms . canonicalization . C14N ||
731- canonicalizationAlgorithmForSignedInfo === Algorithms . canonicalization . C14N_WITH_COMMENTS
739+ canonicalizationAlgorithmForSignedInfo === CANONICALIZATION_ALGORITHMS . C14N ||
740+ canonicalizationAlgorithmForSignedInfo === CANONICALIZATION_ALGORITHMS . C14N_WITH_COMMENTS
732741 ) {
733- canonicalizationAlgorithmForSignedInfo = Algorithms . canonicalization . EXCLUSIVE_C14N ;
742+ canonicalizationAlgorithmForSignedInfo = CANONICALIZATION_ALGORITHMS . EXCLUSIVE_C14N ;
734743 }
735744
736745 const temporaryCanonSignedInfo = this . getCanonXml (
@@ -1403,16 +1412,8 @@ export class SignedXml {
14031412 const id = `_${ this . id ++ } ` ;
14041413
14051414 if ( this . idMode === "wssecurity" ) {
1406- node . setAttributeNS (
1407- Namespaces . xmlns ,
1408- "xmlns:wsu" ,
1409- "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" ,
1410- ) ;
1411- node . setAttributeNS (
1412- "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" ,
1413- "wsu:Id" ,
1414- id ,
1415- ) ;
1415+ node . setAttributeNS ( NAMESPACES . xmlns , "xmlns:wsu" , NAMESPACES . wsu ) ;
1416+ node . setAttributeNS ( NAMESPACES . wsu , "wsu:Id" , id ) ;
14161417 } else {
14171418 // Use the first idAttribute to set the new ID
14181419 const firstIdAttr = this . idAttributes [ 0 ] ;
@@ -1421,7 +1422,7 @@ export class SignedXml {
14211422 } else {
14221423 if ( "prefix" in firstIdAttr && firstIdAttr . prefix ) {
14231424 node . setAttributeNS (
1424- Namespaces . xmlns ,
1425+ NAMESPACES . xmlns ,
14251426 `xmlns:${ firstIdAttr . prefix } ` ,
14261427 firstIdAttr . namespaceUri ,
14271428 ) ;
@@ -1488,7 +1489,7 @@ export class SignedXml {
14881489 const signatureValueXml = `<${ prefix } SignatureValue>${ this . signatureValue } </${ prefix } SignatureValue>` ;
14891490 //the canonicalization requires to get a valid xml node.
14901491 //we need to wrap the info in a dummy signature since it contains the default namespace.
1491- const dummySignatureWrapper = `<${ prefix } Signature ${ xmlNsAttr } ="${ Namespaces . ds } ">${ signatureValueXml } </${ prefix } Signature>` ;
1492+ const dummySignatureWrapper = `<${ prefix } Signature ${ xmlNsAttr } ="${ NAMESPACES . ds } ">${ signatureValueXml } </${ prefix } Signature>` ;
14921493
14931494 const doc = new xmldom . DOMParser ( ) . parseFromString ( dummySignatureWrapper ) ;
14941495
0 commit comments