Skip to content

Commit e6fc0d4

Browse files
authored
Bound check for ASN.1 node length (#9)
1 parent cbb9982 commit e6fc0d4

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/Asn1Decode.sol

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,10 @@ library Asn1Decode {
211211
function readNodeLength(bytes memory der, uint256 ix) private pure returns (Asn1Ptr) {
212212
require(der[ix] & 0x1f != 0x1f, "ASN.1 tags longer than 1-byte are not supported");
213213
uint256 length;
214-
uint80 ixFirstContentByte;
214+
uint256 ixFirstContentByte;
215215
if ((der[ix + 1] & 0x80) == 0) {
216216
length = uint8(der[ix + 1]);
217-
ixFirstContentByte = uint80(ix + 2);
217+
ixFirstContentByte = ix + 2;
218218
} else {
219219
uint8 lengthbytesLength = uint8(der[ix + 1] & 0x7F);
220220
if (lengthbytesLength == 1) {
@@ -223,10 +223,11 @@ library Asn1Decode {
223223
length = der.readUint16(ix + 2);
224224
} else {
225225
length = uint256(readBytesN(der, ix + 2, lengthbytesLength) >> (32 - lengthbytesLength) * 8);
226+
require(length <= 2 ** 64 - 1); // bound to max uint64 to be safe
226227
}
227-
ixFirstContentByte = uint80(ix + 2 + lengthbytesLength);
228+
ixFirstContentByte = ix + 2 + lengthbytesLength;
228229
}
229-
return LibAsn1Ptr.toAsn1Ptr(ix, ixFirstContentByte, uint80(length));
230+
return LibAsn1Ptr.toAsn1Ptr(ix, ixFirstContentByte, length);
230231
}
231232

232233
function readBytesN(bytes memory self, uint256 idx, uint256 len) private pure returns (bytes32 ret) {

0 commit comments

Comments
 (0)