Skip to content

Commit 8e692cf

Browse files
authored
Add support for sha256-rsa-MGF1 signing algorithm (#328) (#488)
1 parent 9b91edf commit 8e692cf

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ A pre requisite it to have [openssl](http://www.openssl.org/) installed and its
4646

4747
- RSA-SHA1 <http://www.w3.org/2000/09/xmldsig#rsa-sha1>
4848
- RSA-SHA256 <http://www.w3.org/2001/04/xmldsig-more#rsa-sha256>
49+
- RSA-SHA256 with MGF1 <http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1>
4950
- RSA-SHA512 <http://www.w3.org/2001/04/xmldsig-more#rsa-sha512>
5051

5152
HMAC-SHA1 is also available but it is disabled by default

src/signature-algorithms.ts

Lines changed: 47 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export class SignedXml {
115115
SignatureAlgorithms: Record<SignatureAlgorithmType, new () => SignatureAlgorithm> = {
116116
"http://www.w3.org/2000/09/xmldsig#rsa-sha1": signatureAlgorithms.RsaSha1,
117117
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha256": signatureAlgorithms.RsaSha256,
118+
"http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1": signatureAlgorithms.RsaSha256Mgf1,
118119
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha512": signatureAlgorithms.RsaSha512,
119120
// Disabled by default due to key confusion concerns.
120121
// 'http://www.w3.org/2000/09/xmldsig#hmac-sha1': SignatureAlgorithms.HmacSha1

src/types.ts

Lines changed: 1 addition & 0 deletions
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)