Skip to content

Commit

Permalink
Remove default for signature algorithm (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbarth authored Nov 11, 2023
1 parent b6cc9c0 commit b0541b3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ _Canonicalization/Transformation Algorithm:_ Exclusive Canonicalization <http://

_Hashing/Digest Algorithm:_ Must be specified by the user

_Signature Algorithm:_ RSA-SHA1 <http://www.w3.org/2000/09/xmldsig#rsa-sha1>
_Signature Algorithm:_ Must be specified by the user

[You are able to extend xml-crypto with custom algorithms.](#customizing-algorithms)

Expand Down
7 changes: 5 additions & 2 deletions src/signed-xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class SignedXml {
* One of the supported signature algorithms.
* @see {@link SignatureAlgorithmType}
*/
signatureAlgorithm: SignatureAlgorithmType = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
signatureAlgorithm?: SignatureAlgorithmType = undefined;
/**
* Rules used to convert an XML document into its canonical form.
*/
Expand Down Expand Up @@ -347,7 +347,10 @@ export class SignedXml {
}
}

private findSignatureAlgorithm(name: SignatureAlgorithmType) {
private findSignatureAlgorithm(name?: SignatureAlgorithmType) {
if (name == null) {
throw new Error("signatureAlgorithm is required");
}
const algo = this.SignatureAlgorithms[name];
if (algo) {
return new algo();
Expand Down
1 change: 1 addition & 0 deletions test/key-info-tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe("KeyInfo tests", function () {
sig.privateKey = fs.readFileSync("./test/static/client.pem");
sig.publicCert = fs.readFileSync("./test/static/client_public.pem");
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml);
const signedXml = sig.getSignedXml();
const doc = new xmldom.DOMParser().parseFromString(signedXml);
Expand Down
2 changes: 2 additions & 0 deletions test/signature-integration-tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe("Signature integration tests", function () {
});

sig.canonicalizationAlgorithm = canonicalizationAlgorithm;
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml);
const signed = sig.getSignedXml();

Expand Down Expand Up @@ -177,6 +178,7 @@ describe("Signature integration tests", function () {
});
sig.privateKey = fs.readFileSync("./test/static/client.pem");
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml);

const signed = sig.getSignedXml();
Expand Down
18 changes: 18 additions & 0 deletions test/signature-unit-tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe("Signature unit tests", function () {
});

sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml);
const signedXml = sig.getOriginalXmlWithIds();
const doc = new xmldom.DOMParser().parseFromString(signedXml);
Expand Down Expand Up @@ -72,6 +73,7 @@ describe("Signature unit tests", function () {
});

sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml, {
existingPrefixes: {
wsu: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd",
Expand All @@ -95,6 +97,7 @@ describe("Signature unit tests", function () {
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
});
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml);
const signedXml = sig.getOriginalXmlWithIds();
const doc = new xmldom.DOMParser().parseFromString(signedXml);
Expand Down Expand Up @@ -129,6 +132,7 @@ describe("Signature unit tests", function () {
});

sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml, {
attrs: attrs,
});
Expand Down Expand Up @@ -163,6 +167,7 @@ describe("Signature unit tests", function () {
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
});
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml);

const doc = new xmldom.DOMParser().parseFromString(sig.getSignedXml());
Expand All @@ -186,6 +191,7 @@ describe("Signature unit tests", function () {
});

sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml, {
location: {
reference: "/root/name",
Expand Down Expand Up @@ -216,6 +222,7 @@ describe("Signature unit tests", function () {
});

sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml, {
location: {
reference: "/root/name",
Expand Down Expand Up @@ -245,6 +252,7 @@ describe("Signature unit tests", function () {
});

sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml, {
location: {
reference: "/root/name",
Expand Down Expand Up @@ -275,6 +283,7 @@ describe("Signature unit tests", function () {
});

sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml, {
location: {
reference: "/root/name",
Expand Down Expand Up @@ -637,6 +646,7 @@ describe("Signature unit tests", function () {
});

sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml);
const signedXml = sig.getSignedXml();
const expected =
Expand Down Expand Up @@ -956,6 +966,7 @@ describe("Signature unit tests", function () {
});

sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml);
const signedXml = sig.getSignedXml();
const doc = new xmldom.DOMParser().parseFromString(signedXml);
Expand Down Expand Up @@ -1013,6 +1024,7 @@ describe("Signature unit tests", function () {
sig.getKeyInfoContent = getKeyInfoContentWithAssertionId.bind(this, { assertionId });
sig.privateKey = fs.readFileSync("./test/static/client.pem");
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml, {
prefix: "ds",
location: {
Expand Down Expand Up @@ -1045,6 +1057,7 @@ describe("Signature unit tests", function () {
});

sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml);
const signedXml = sig.getSignedXml();

Expand Down Expand Up @@ -1081,6 +1094,7 @@ describe("Signature unit tests", function () {
});

sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml);
const signedXml = sig.getSignedXml();

Expand All @@ -1105,6 +1119,7 @@ describe("Signature unit tests", function () {
});

sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml);
const signedXml = sig.getSignedXml();

Expand Down Expand Up @@ -1143,6 +1158,7 @@ describe("Signature unit tests", function () {
});

sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml);
const signedXml = sig.getSignedXml();

Expand All @@ -1169,6 +1185,7 @@ describe("Signature unit tests", function () {
sig.getKeyInfoContent = () => "<dummy/>";

sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml);
const signedXml = sig.getSignedXml();

Expand Down Expand Up @@ -1200,6 +1217,7 @@ describe("Signature unit tests", function () {
sig.privateKey = pemBuffer;
sig.publicCert = pemBuffer;
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml);
const signedXml = sig.getSignedXml();

Expand Down

0 comments on commit b0541b3

Please sign in to comment.