Skip to content

Commit ef49b7e

Browse files
committed
Add support for sha256-rsa-MGF1 signing algorithm (node-saml#328)
1 parent f9b3682 commit ef49b7e

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/signature-algorithms.ts

+47
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,53 @@ export class RsaSha256 implements SignatureAlgorithm {
5353
};
5454
}
5555

56+
export class RsaSha256Mgf1 implements SignatureAlgorithm {
57+
getSignature = createOptionalCallbackFunction(
58+
(signedInfo: crypto.BinaryLike, privateKey: crypto.KeyLike): string => {
59+
if (!(typeof privateKey === "string" || Buffer.isBuffer(privateKey))) {
60+
throw new Error("keys must be strings or buffers");
61+
}
62+
const signer = crypto.createSign("RSA-SHA256");
63+
signer.update(signedInfo);
64+
const res = signer.sign(
65+
{
66+
key: privateKey,
67+
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
68+
saltLength: crypto.constants.RSA_PSS_SALTLEN_DIGEST,
69+
},
70+
"base64",
71+
);
72+
73+
return res;
74+
},
75+
);
76+
77+
verifySignature = createOptionalCallbackFunction(
78+
(material: string, key: crypto.KeyLike, signatureValue: string): boolean => {
79+
if (!(typeof key === "string" || Buffer.isBuffer(key))) {
80+
throw new Error("keys must be strings or buffers");
81+
}
82+
const verifier = crypto.createVerify("RSA-SHA256");
83+
verifier.update(material);
84+
const res = verifier.verify(
85+
{
86+
key: key,
87+
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
88+
saltLength: crypto.constants.RSA_PSS_SALTLEN_DIGEST,
89+
},
90+
signatureValue,
91+
"base64",
92+
);
93+
94+
return res;
95+
},
96+
);
97+
98+
getAlgorithmName = () => {
99+
return "http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1";
100+
};
101+
}
102+
56103
export class RsaSha512 implements SignatureAlgorithm {
57104
getSignature = createOptionalCallbackFunction(
58105
(signedInfo: crypto.BinaryLike, privateKey: crypto.KeyLike): string => {

src/signed-xml.ts

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export class SignedXml {
102102
SignatureAlgorithms: Record<SignatureAlgorithmType, new () => SignatureAlgorithm> = {
103103
"http://www.w3.org/2000/09/xmldsig#rsa-sha1": signatureAlgorithms.RsaSha1,
104104
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha256": signatureAlgorithms.RsaSha256,
105+
"http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1": signatureAlgorithms.RsaSha256Mgf1,
105106
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha512": signatureAlgorithms.RsaSha512,
106107
// Disabled by default due to key confusion concerns.
107108
// 'http://www.w3.org/2000/09/xmldsig#hmac-sha1': SignatureAlgorithms.HmacSha1

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export type HashAlgorithmType =
3030
export type SignatureAlgorithmType =
3131
| "http://www.w3.org/2000/09/xmldsig#rsa-sha1"
3232
| "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"
33+
| "http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1"
3334
| "http://www.w3.org/2001/04/xmldsig-more#rsa-sha512"
3435
| "http://www.w3.org/2000/09/xmldsig#hmac-sha1"
3536
| string;

0 commit comments

Comments
 (0)