Skip to content

Commit

Permalink
fixed issue with signing large playloads ad removed sync function (I´…
Browse files Browse the repository at this point in the history
…m sorry)
  • Loading branch information
erdtman committed Nov 2, 2021
1 parent a451543 commit aa436b3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 17 deletions.
14 changes: 3 additions & 11 deletions lib/sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const EC = require('elliptic').ec;
const crypto = require('crypto');
const NodeRSA = require('node-rsa');
const common = require('./common');
const Promise = require('any-promise');
const EMPTY_BUFFER = common.EMPTY_BUFFER;
const Tagged = cbor.Tagged;

Expand Down Expand Up @@ -79,7 +78,7 @@ function doSign (SigStructure, signer, alg) {
return sig;
}

exports.createSync = function (headers, payload, signers, options) {
exports.create = function (headers, payload, signers, options) {
options = options || {};
let u = headers.u || {};
let p = headers.p || {};
Expand Down Expand Up @@ -121,7 +120,7 @@ exports.createSync = function (headers, payload, signers, options) {
p = cbor.encode(p);
}
const signed = [p, u, payload, [[signerP, signerU, sig]]];
return cbor.encode(options.excludetag ? signed : new Tagged(SignTag, signed));
return cbor.encodeAsync(options.excludetag ? signed : new Tagged(SignTag, signed));
} else {
const signer = signers;
const externalAAD = signer.externalAAD || EMPTY_BUFFER;
Expand All @@ -139,17 +138,10 @@ exports.createSync = function (headers, payload, signers, options) {
p = cbor.encode(p);
}
const signed = [p, u, payload, sig];
return cbor.encodeCanonical(options.excludetag ? signed : new Tagged(Sign1Tag, signed));
return cbor.encodeAsync(options.excludetag ? signed : new Tagged(Sign1Tag, signed), { canonical: true });
}
};

exports.create = function (headers, payload, signers, options) {
return new Promise((resolve, reject) => {
const internal = exports.createSync(headers, payload, signers, options);
resolve(internal);
});
};

function doVerify (SigStructure, verifier, alg, sig) {
if (!AlgFromTags[alg]) {
throw new Error('Unknown algorithm, ' + alg);
Expand Down
4 changes: 2 additions & 2 deletions test/rsa-pkcs-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ test('create rsa-pkcs-01', (t) => {
});
});

test('create rsa-pkcs-01 Sync', (t) => {
test('create rsa-pkcs-01 Sync', async (t) => {
const example = jsonfile.readFileSync('test/rsa-pkcs-examples/rsa-pkcs-01.json');
const p = example.input.sign0.protected;
const u = example.input.sign0.unprotected;
Expand All @@ -72,7 +72,7 @@ test('create rsa-pkcs-01 Sync', (t) => {
}, { private: true })
};

const buf = cose.sign.createSync(
const buf = await cose.sign.create(
{ p: p, u: u },
plaintext,
signer,
Expand Down
4 changes: 2 additions & 2 deletions test/sign-performance-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test('create and verify really huge payload', (t) => {
u: example.input.sign.signers[0].unprotected,
p: example.input.sign.signers[0].protected
}];
const plaintext = Buffer.from('a'.repeat(100 * 160));
const plaintext = Buffer.from('a'.repeat(100 * 1000));

const verifier = {
key: {
Expand All @@ -40,7 +40,7 @@ test('create and verify really huge payload', (t) => {
t.true(buf.length > 0);
return cbor.decodeFirst(buf)
.then(actual => {
t.is(actual.value[2].length, 100 * 160);
t.is(actual.value[2].length, 100 * 1000);
})
.then(() => buf);
})
Expand Down
4 changes: 2 additions & 2 deletions test/sign-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ test('create sign-pass-01', (t) => {
});
});

test('create sign-pass-01 Sync', (t) => {
test('create sign-pass-01 Sync', async (t) => {
const example = jsonfile.readFileSync('test/Examples/sign-tests/sign-pass-01.json');
const p = example.input.sign.protected;
const u = example.input.sign.unprotected;
Expand All @@ -271,7 +271,7 @@ test('create sign-pass-01 Sync', (t) => {
p: example.input.sign.signers[0].protected
}];

const buf = cose.sign.createSync(
const buf = await cose.sign.create(
{ p: p, u: u },
plaintext,
signers
Expand Down

0 comments on commit aa436b3

Please sign in to comment.