Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sha256-rsa-MGF1 signing algorithm (#328) #488

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ A pre requisite it to have [openssl](http://www.openssl.org/) installed and its

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

HMAC-SHA1 is also available but it is disabled by default
Expand Down
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