Skip to content

Commit afd156a

Browse files
authored
Add option for digest algorithm (#1273)
* Add option for digest algorithm * Set default value for digestAlgorithm to sha256, add test, and update documentation.
1 parent 1c5c857 commit afd156a

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,7 @@ The `options` object is optional and can contain the following properties:
981981
* `hasTimeStamp`: Includes Timestamp tags (default: `true`)
982982
* `signatureTransformations`: sets the Reference Transforms Algorithm (default ['http://www.w3.org/2000/09/xmldsig#enveloped-signature', 'http://www.w3.org/2001/10/xml-exc-c14n#']). Type is a string array
983983
* `signatureAlgorithm`: set to `http://www.w3.org/2001/04/xmldsig-more#rsa-sha256` to use sha256
984+
* `digestAlgorithm`: set to `http://www.w3.org/2000/09/xmldsig#sha1` to use sha1 (default `http://www.w3.org/2001/04/xmlenc#sha256`)
984985
* `additionalReferences` : (optional) Array of Soap headers that need to be signed. This need to be added using `client.addSoapHeader('header')`
985986
* `signerOptions`: (optional) passes options to the XML Signer package - from (https://github.com/yaronn/xml-crypto)
986987
* `existingPrefixes`: (optional) A hash of prefixes and namespaces prefix: namespace that shouldn't be in the signature because they already exist in the xml (default: `{ 'wsse': 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' }`)

src/security/WSSecurityCert.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export interface IWSSecurityCertOptions {
4343
hasTimeStamp?: boolean;
4444
signatureTransformations?: string[];
4545
signatureAlgorithm?: string;
46+
digestAlgorithm?: string;
4647
additionalReferences?: string[];
4748
signerOptions?: IXmlSignerOptions;
4849
}
@@ -73,8 +74,10 @@ export class WSSecurityCert implements ISecurity {
7374

7475
this.signer = new SignedXml({
7576
idMode: options?.signerOptions?.idMode,
76-
signatureAlgorithm: options?.signatureAlgorithm });
77+
signatureAlgorithm: options?.signatureAlgorithm,
78+
});
7779

80+
this.signer.digestAlgorithm = options.digestAlgorithm ?? 'http://www.w3.org/2001/04/xmlenc#sha256';
7881
if (options.signatureAlgorithm === 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256') {
7982
this.signer.signatureAlgorithm = options.signatureAlgorithm;
8083
this.signer.addReference({
@@ -180,19 +183,19 @@ export class WSSecurityCert implements ISecurity {
180183
resolvePlaceholderInReferences(this.signer.references, bodyXpath);
181184

182185
if (!(this.signer.references.filter((ref: { xpath: string; }) => (ref.xpath === bodyXpath)).length > 0)) {
183-
this.signer.addReference({ xpath: bodyXpath, transforms: references, digestAlgorithm: 'http://www.w3.org/2001/04/xmlenc#sha256' });
186+
this.signer.addReference({ xpath: bodyXpath, transforms: references, digestAlgorithm: this.signer.digestAlgorithm });
184187
}
185188

186189
for (const name of this.additionalReferences) {
187190
const xpath = `//*[name(.)='${name}']`;
188191
if (!(this.signer.references.filter((ref: { xpath: string; }) => (ref.xpath === xpath)).length > 0)) {
189-
this.signer.addReference({ xpath: xpath, transforms: references, digestAlgorithm: 'http://www.w3.org/2001/04/xmlenc#sha256' });
192+
this.signer.addReference({ xpath: xpath, transforms: references, digestAlgorithm: this.signer.digestAlgorithm });
190193
}
191194
}
192195

193196
const timestampXpath = `//*[name(.)='wsse:Security']/*[local-name(.)='Timestamp']`;
194197
if (this.hasTimeStamp && !(this.signer.references.filter((ref: { xpath: string; }) => (ref.xpath === timestampXpath)).length > 0)) {
195-
this.signer.addReference({ xpath: timestampXpath, transforms: references, digestAlgorithm: 'http://www.w3.org/2001/04/xmlenc#sha256' });
198+
this.signer.addReference({ xpath: timestampXpath, transforms: references, digestAlgorithm: this.signer.digestAlgorithm });
196199
}
197200

198201
this.signer.computeSignature(xmlWithSec, this.signerOptions);

test/security/WSSecurityCert.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,14 @@ describe('WSSecurityCert', function () {
256256
var xml = instance.postProcess('<soap:Envelope><soap:Header></soap:Header><soap:Body><Body></Body></soap:Body></soap:Envelope>', 'soap');
257257
xml.should.containEql('SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha512"');
258258
});
259+
260+
it('should use digest method when the digestAlgorithm option is set on WSSecurityCert', function () {
261+
var instance = new WSSecurityCert(key, cert, '', {
262+
hasTimeStamp: false,
263+
digestAlgorithm: 'http://www.w3.org/2000/09/xmldsig#sha1'
264+
});
265+
var xml = instance.postProcess('<soap:Envelope><soap:Header></soap:Header><soap:Body><Body></Body></soap:Body></soap:Envelope>', 'soap');
266+
xml.should.containEql('DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"');
267+
});
268+
259269
});

0 commit comments

Comments
 (0)