Skip to content

Commit 856fe4d

Browse files
committed
signature: prevent malleability and overflows
1 parent 6048941 commit 856fe4d

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

lib/elliptic/ec/signature.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,24 @@ function getLength(buf, p) {
3232
return initial;
3333
}
3434
var octetLen = initial & 0xf;
35+
36+
// Indefinite length or overflow
37+
if (octetLen === 0 || octetLen > 4) {
38+
return false;
39+
}
40+
3541
var val = 0;
3642
for (var i = 0, off = p.place; i < octetLen; i++, off++) {
3743
val <<= 8;
3844
val |= buf[off];
45+
val >>>= 0;
3946
}
47+
48+
// Leading zeroes
49+
if (val <= 0x7f) {
50+
return false;
51+
}
52+
4053
p.place = off;
4154
return val;
4255
}
@@ -60,28 +73,47 @@ Signature.prototype._importDER = function _importDER(data, enc) {
6073
return false;
6174
}
6275
var len = getLength(data, p);
76+
if (len === false) {
77+
return false;
78+
}
6379
if ((len + p.place) !== data.length) {
6480
return false;
6581
}
6682
if (data[p.place++] !== 0x02) {
6783
return false;
6884
}
6985
var rlen = getLength(data, p);
86+
if (rlen === false) {
87+
return false;
88+
}
7089
var r = data.slice(p.place, rlen + p.place);
7190
p.place += rlen;
7291
if (data[p.place++] !== 0x02) {
7392
return false;
7493
}
7594
var slen = getLength(data, p);
95+
if (slen === false) {
96+
return false;
97+
}
7698
if (data.length !== slen + p.place) {
7799
return false;
78100
}
79101
var s = data.slice(p.place, slen + p.place);
80-
if (r[0] === 0 && (r[1] & 0x80)) {
81-
r = r.slice(1);
102+
if (r[0] === 0) {
103+
if (r[1] & 0x80) {
104+
r = r.slice(1);
105+
} else {
106+
// Leading zeroes
107+
return false;
108+
}
82109
}
83-
if (s[0] === 0 && (s[1] & 0x80)) {
84-
s = s.slice(1);
110+
if (s[0] === 0) {
111+
if (s[1] & 0x80) {
112+
s = s.slice(1);
113+
} else {
114+
// Leading zeroes
115+
return false;
116+
}
85117
}
86118

87119
this.r = new BN(r);

0 commit comments

Comments
 (0)