Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/main/java/co/rsk/bitcoinj/script/ScriptChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,22 @@ private int decodePositiveNConsideringEncoding() {
throw new IllegalArgumentException("Chunk has null data.");
}
int dataLength = data.length;
byte mostSignificantByte = data[dataLength - 1];

int signByte = data[dataLength - 1] & 0x80;
int signByte = mostSignificantByte & 0x80;
boolean isPositive = signByte == 0;
if (!isPositive) {
throw new IllegalArgumentException("Number from chunk is not positive.");
}

boolean hasPadding = mostSignificantByte == 0;
if (hasPadding) {
boolean shouldAllowPadding = (data[dataLength - 2] & 0x80) != 0;
if (!shouldAllowPadding) {
throw new IllegalArgumentException("Number from chunk does not have minimal encoding.");
}
}

if (dataLength > 4) {
throw new IllegalArgumentException("Number from chunk has more than 4 bytes.");
}
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/co/rsk/bitcoinj/script/ScriptChunkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import org.junit.Test;

import java.util.Arrays;

public class ScriptChunkTest {

@Test
Expand Down Expand Up @@ -175,6 +177,42 @@ public void decodePositiveN_forZero_throwsIAE() {
assertThrows(IllegalArgumentException.class, chunk::decodePositiveN);
}

@Test
public void decodePositiveN_forNumberWithoutMinimalEncoding_throwsIAE() {
byte zero = 0x00;

// from 1 to 15, numbers are small numbers and therefore pushed as opcodes
// with null chunk data
for (int i=1; i<16; i++) {
byte[] numWithoutMinimalEncoding = new byte[] {(byte) i, zero};

ScriptBuilder builder = new ScriptBuilder();
builder.data(numWithoutMinimalEncoding);
Script script = builder.build();

ScriptChunk chunk = script.chunks.get(0);
assertFalse(chunk.isPositiveN());
assertThrows(IllegalArgumentException.class, chunk::decodePositiveN);
}

for (int n=4; n<20; n++) {
int i = (1 << n); // i = (2^n)
ScriptBuilder builder = new ScriptBuilder();
ScriptChunk chunk = builder.number(i).build().getChunks().get(0);
byte[] chunkData = chunk.data;

// add zero padding so the number does not have minimal encoding
byte [] numWithoutMinimalEncoding = Arrays.copyOf(chunkData, chunkData.length + 1);
numWithoutMinimalEncoding[chunkData.length] = zero;

// build the chunk for the number without minimal encoding
builder = new ScriptBuilder();
ScriptChunk newChunk = builder.data(numWithoutMinimalEncoding).build().chunks.get(0);
assertFalse(newChunk.isPositiveN());
assertThrows(IllegalArgumentException.class, newChunk::decodePositiveN);
}
}

@Test
public void decodePositiveN_withNegativeNumber_throwsIAE() {
for (int n=0; n<20; n++) { // technically we could go down to Integer.MIN_VALUE, but it's not worth it to go that deep
Expand Down