From fe9843548ed889888f3c1bd8500beb4d350c2bf1 Mon Sep 17 00:00:00 2001 From: Gregor Gaertner Date: Sun, 10 Jan 2021 20:42:13 +0100 Subject: [PATCH] Fixes 53#issue-782891651 (java.lang.ArrayIndexOutOfBoundsException from StreamDeserializationContext.read() if Hash.from() is called for more than 1048576 bytes) --- .../ots/StreamDeserializationContext.java | 4 +-- .../java/com/eternitywall/ots/op/TestOps.java | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/eternitywall/ots/StreamDeserializationContext.java b/src/main/java/com/eternitywall/ots/StreamDeserializationContext.java index 55ead82..183a86f 100644 --- a/src/main/java/com/eternitywall/ots/StreamDeserializationContext.java +++ b/src/main/java/com/eternitywall/ots/StreamDeserializationContext.java @@ -28,8 +28,8 @@ public byte[] read(int l) { return null; } - if (l > this.buffer.length) { - l = this.buffer.length; + if (l+this.counter > this.buffer.length) { + l = this.buffer.length-this.counter; } // const uint8Array = new Uint8Array(this.buffer,this.counter,l); diff --git a/src/test/java/com/eternitywall/ots/op/TestOps.java b/src/test/java/com/eternitywall/ots/op/TestOps.java index dea354f..949c48f 100644 --- a/src/test/java/com/eternitywall/ots/op/TestOps.java +++ b/src/test/java/com/eternitywall/ots/op/TestOps.java @@ -1,8 +1,11 @@ package com.eternitywall.ots.op; import com.eternitywall.ots.DetachedTimestampFile; +import com.eternitywall.ots.Hash; import com.eternitywall.ots.StreamDeserializationContext; import com.eternitywall.ots.Utils; +import java.io.IOException; +import java.security.NoSuchAlgorithmException; import org.junit.Test; import static org.junit.Assert.assertArrayEquals; @@ -85,4 +88,36 @@ public void test100M() throws Exception { byte[] fileDigest = timestampFile.fileDigest(); assertArrayEquals(Utils.hexToBytes(hash), fileDigest); } + + // Test "Hash.from" below chunk size + @Test + public void hashFd1() throws Exception { + String hash = "CA7ED0C4A8E67CBDC461C4CB0D286D2FABBD9F0C41A7F42B665F72EBAA8AEC56"; + int size = 1048576-1; + byte[] buffer = new byte[size]; + Hash myHash=Hash.from(buffer, OpSHA256._TAG); + assertArrayEquals(Utils.hexToBytes(hash), myHash.getValue()); + } + + // Test "Hash.from" at edge case of being equal to chunk size + @Test + public void hashFd2() throws Exception { + String hash = "30E14955EBF1352266DC2FF8067E68104607E750ABB9D3B36582B8AF909FCB58"; + int size = 1048576; + byte[] buffer = new byte[size]; + Hash myHash=Hash.from(buffer, OpSHA256._TAG); + assertArrayEquals(Utils.hexToBytes(hash), myHash.getValue()); + } + + // Test "Hash.from" above chunk size + @Test + public void hashFd3() throws Exception { + String hash = "2CB74EDBA754A81D121C9DB6833704A8E7D417E5B13D1A19F4A52F007D644264"; + int size = 1048576+1; + byte[] buffer = new byte[size]; + Hash myHash=Hash.from(buffer, OpSHA256._TAG); + assertArrayEquals(Utils.hexToBytes(hash), myHash.getValue()); + } + + }