Skip to content

Commit

Permalink
Add option for digest algorithm (#1273)
Browse files Browse the repository at this point in the history
* Add option for digest algorithm
* Set default value for digestAlgorithm to sha256, add test, and update documentation.
  • Loading branch information
pzemljic-git authored Feb 12, 2025
1 parent 1c5c857 commit afd156a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,7 @@ The `options` object is optional and can contain the following properties:
* `hasTimeStamp`: Includes Timestamp tags (default: `true`)
* `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
* `signatureAlgorithm`: set to `http://www.w3.org/2001/04/xmldsig-more#rsa-sha256` to use sha256
* `digestAlgorithm`: set to `http://www.w3.org/2000/09/xmldsig#sha1` to use sha1 (default `http://www.w3.org/2001/04/xmlenc#sha256`)
* `additionalReferences` : (optional) Array of Soap headers that need to be signed. This need to be added using `client.addSoapHeader('header')`
* `signerOptions`: (optional) passes options to the XML Signer package - from (https://github.com/yaronn/xml-crypto)
* `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' }`)
Expand Down
11 changes: 7 additions & 4 deletions src/security/WSSecurityCert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface IWSSecurityCertOptions {
hasTimeStamp?: boolean;
signatureTransformations?: string[];
signatureAlgorithm?: string;
digestAlgorithm?: string;
additionalReferences?: string[];
signerOptions?: IXmlSignerOptions;
}
Expand Down Expand Up @@ -73,8 +74,10 @@ export class WSSecurityCert implements ISecurity {

this.signer = new SignedXml({
idMode: options?.signerOptions?.idMode,
signatureAlgorithm: options?.signatureAlgorithm });
signatureAlgorithm: options?.signatureAlgorithm,
});

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

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

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

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

this.signer.computeSignature(xmlWithSec, this.signerOptions);
Expand Down
10 changes: 10 additions & 0 deletions test/security/WSSecurityCert.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,14 @@ describe('WSSecurityCert', function () {
var xml = instance.postProcess('<soap:Envelope><soap:Header></soap:Header><soap:Body><Body></Body></soap:Body></soap:Envelope>', 'soap');
xml.should.containEql('SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha512"');
});

it('should use digest method when the digestAlgorithm option is set on WSSecurityCert', function () {
var instance = new WSSecurityCert(key, cert, '', {
hasTimeStamp: false,
digestAlgorithm: 'http://www.w3.org/2000/09/xmldsig#sha1'
});
var xml = instance.postProcess('<soap:Envelope><soap:Header></soap:Header><soap:Body><Body></Body></soap:Body></soap:Envelope>', 'soap');
xml.should.containEql('DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"');
});

});

0 comments on commit afd156a

Please sign in to comment.