Skip to content

Commit 8767739

Browse files
btjljharb
authored andcommittedSep 18, 2023
[Fix] sign: throw on unsupported padding scheme
Do not silently apply the wrong padding scheme.
1 parent 5f6fb17 commit 8767739

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed
 

‎.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"rules": {
77
"func-style": "warn",
88
"indent": ["error", 2],
9+
"multiline-comment-style": "off",
910
"sort-keys": "off",
1011
},
1112

‎browser/sign.js

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ var BN = require('bn.js');
99
var parseKeys = require('parse-asn1');
1010
var curves = require('./curves.json');
1111

12+
var RSA_PKCS1_PADDING = 1;
13+
1214
function sign(hash, key, hashType, signType, tag) {
1315
var priv = parseKeys(key);
1416
if (priv.curve) {
@@ -20,6 +22,7 @@ function sign(hash, key, hashType, signType, tag) {
2022
return dsaSign(hash, priv, hashType);
2123
}
2224
if (signType !== 'rsa' && signType !== 'ecdsa/rsa') { throw new Error('wrong private key type'); }
25+
if (key.padding !== undefined && key.padding !== RSA_PKCS1_PADDING) { throw new Error('illegal or unsupported padding mode'); }
2326

2427
hash = Buffer.concat([tag, hash]);
2528
var len = priv.modulus.byteLength();

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"tests-only": "nyc tape 'test/**/*.js'",
2222
"pretest": "npm run lint",
2323
"test": "npm run tests-only",
24-
"posttest": "aud --production"
24+
"posttest": "aud --production"
2525
},
2626
"dependencies": {
2727
"bn.js": "^5.2.1",
@@ -39,6 +39,7 @@
3939
"aud": "^2.0.3",
4040
"eslint": "=8.8.0",
4141
"nyc": "^10.3.2",
42+
"semver": "^6.3.1",
4243
"tape": "^5.6.6"
4344
},
4445
"browser": "browser/index.js",

‎test/index.js

+31-10
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,15 @@ var Buffer = require('safe-buffer').Buffer;
44
var asn1 = require('parse-asn1/asn1');
55
var test = require('tape').test;
66
var nCrypto = require('crypto');
7+
var semver = require('semver');
78
var bCrypto = require('../browser');
89
var fixtures = require('./fixtures');
910

10-
function isNode10() {
11-
return parseInt(process.version.split('.')[1], 10) <= 10;
12-
}
13-
1411
fixtures.valid.rsa.forEach(function (f) {
1512
var message = Buffer.from(f.message);
1613
var pub = Buffer.from(f['public'], 'base64');
1714
var priv;
1815

19-
// skip passphrase tests in node 10
20-
if (f.passphrase && isNode10()) { return; }
21-
2216
if (f.passphrase) {
2317
priv = {
2418
key: Buffer.from(f['private'], 'base64'),
@@ -63,14 +57,41 @@ fixtures.valid.rsa.forEach(function (f) {
6357
});
6458
});
6559

60+
// node has padding support since 8.0
61+
// TODO: figure out why node v8.0 - v8.6 is broken
62+
(semver.satisfies(process.versions.node, '>= 8.6') ? test : test.skip)('padding option', function (t) {
63+
var f = fixtures.valid.rsa[0];
64+
var message = Buffer.from(f.message);
65+
var priv = {
66+
key: Buffer.from(f['private'], 'base64'),
67+
padding: 11646841 // Some invalid value
68+
};
69+
70+
t.test('invalid padding option', function (st) {
71+
var bSign = bCrypto.createSign(f.scheme);
72+
var nSign = nCrypto.createSign(f.scheme);
73+
st['throws'](
74+
function () { bSign.update(message).sign(priv); },
75+
/illegal or unsupported padding mode/,
76+
'browser throws exception with proper message'
77+
);
78+
st['throws'](
79+
function () { nSign.update(message).sign(priv); },
80+
/illegal or unsupported padding mode/,
81+
'node throws exception with proper message'
82+
);
83+
84+
st.end();
85+
});
86+
87+
t.end();
88+
});
89+
6690
fixtures.valid.ec.forEach(function (f) {
6791
var message = Buffer.from(f.message);
6892
var pub = Buffer.from(f['public'], 'base64');
6993
var priv;
7094

71-
// skip passphrase tests in node 10
72-
if (f.passphrase && isNode10()) { return; }
73-
7495
if (f.passphrase) {
7596
priv = {
7697
key: Buffer.from(f['private'], 'base64'),

0 commit comments

Comments
 (0)
Please sign in to comment.