Skip to content

Commit

Permalink
Add support for sha256-rsa-MGF1 signing algorithm (node-saml#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
cornzz committed Feb 13, 2025
1 parent f9b3682 commit ef49b7e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/signature-algorithms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,53 @@ export class RsaSha256 implements SignatureAlgorithm {
};
}

export class RsaSha256Mgf1 implements SignatureAlgorithm {
getSignature = createOptionalCallbackFunction(
(signedInfo: crypto.BinaryLike, privateKey: crypto.KeyLike): string => {
if (!(typeof privateKey === "string" || Buffer.isBuffer(privateKey))) {
throw new Error("keys must be strings or buffers");
}
const signer = crypto.createSign("RSA-SHA256");
signer.update(signedInfo);
const res = signer.sign(
{
key: privateKey,
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
saltLength: crypto.constants.RSA_PSS_SALTLEN_DIGEST,
},
"base64",
);

return res;
},
);

verifySignature = createOptionalCallbackFunction(
(material: string, key: crypto.KeyLike, signatureValue: string): boolean => {
if (!(typeof key === "string" || Buffer.isBuffer(key))) {
throw new Error("keys must be strings or buffers");
}
const verifier = crypto.createVerify("RSA-SHA256");
verifier.update(material);
const res = verifier.verify(
{
key: key,
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
saltLength: crypto.constants.RSA_PSS_SALTLEN_DIGEST,
},
signatureValue,
"base64",
);

return res;
},
);

getAlgorithmName = () => {
return "http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1";
};
}

export class RsaSha512 implements SignatureAlgorithm {
getSignature = createOptionalCallbackFunction(
(signedInfo: crypto.BinaryLike, privateKey: crypto.KeyLike): string => {
Expand Down
1 change: 1 addition & 0 deletions src/signed-xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class SignedXml {
SignatureAlgorithms: Record<SignatureAlgorithmType, new () => SignatureAlgorithm> = {
"http://www.w3.org/2000/09/xmldsig#rsa-sha1": signatureAlgorithms.RsaSha1,
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha256": signatureAlgorithms.RsaSha256,
"http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1": signatureAlgorithms.RsaSha256Mgf1,
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha512": signatureAlgorithms.RsaSha512,
// Disabled by default due to key confusion concerns.
// 'http://www.w3.org/2000/09/xmldsig#hmac-sha1': SignatureAlgorithms.HmacSha1
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type HashAlgorithmType =
export type SignatureAlgorithmType =
| "http://www.w3.org/2000/09/xmldsig#rsa-sha1"
| "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"
| "http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1"
| "http://www.w3.org/2001/04/xmldsig-more#rsa-sha512"
| "http://www.w3.org/2000/09/xmldsig#hmac-sha1"
| string;
Expand Down

0 comments on commit ef49b7e

Please sign in to comment.