Skip to content

Commit 98a0fbe

Browse files
committed
added sanity checks for boolean flags - related to github #1575
1 parent 47ce473 commit 98a0fbe

File tree

5 files changed

+92
-3
lines changed

5 files changed

+92
-3
lines changed

pg/src/main/java/org/bouncycastle/bcpg/sig/Exportable.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public Exportable(
2626

2727
public boolean isExportable()
2828
{
29-
return data[0] != 0;
29+
return Utils.booleanFromByteArray(data);
3030
}
3131
}

pg/src/main/java/org/bouncycastle/bcpg/sig/PrimaryUserID.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public PrimaryUserID(
2626

2727
public boolean isPrimaryUserID()
2828
{
29-
return data[0] != 0;
29+
return Utils.booleanFromByteArray(data);
3030
}
3131
}

pg/src/main/java/org/bouncycastle/bcpg/sig/Revocable.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public Revocable(
2626

2727
public boolean isRevocable()
2828
{
29-
return data[0] != 0;
29+
return Utils.booleanFromByteArray(data);
3030
}
3131
}

pg/src/main/java/org/bouncycastle/bcpg/sig/Utils.java

+29
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,35 @@ static byte[] booleanToByteArray(boolean value)
1919
return data;
2020
}
2121

22+
/**
23+
* Convert a one-entry byte array into a boolean.
24+
* If the byte array doesn't have one entry, or if this entry is neither a 0 nor 1, this method throws an
25+
* {@link IllegalArgumentException}.
26+
* A 1 is translated into true, a 0 into false.
27+
*
28+
* @param bytes byte array
29+
* @return boolean
30+
*/
31+
static boolean booleanFromByteArray(byte[] bytes)
32+
{
33+
if (bytes.length != 1)
34+
{
35+
throw new IllegalStateException("Byte array has unexpected length. Expected length 1, got " + bytes.length);
36+
}
37+
if (bytes[0] == 0)
38+
{
39+
return false;
40+
}
41+
else if (bytes[0] == 1)
42+
{
43+
return true;
44+
}
45+
else
46+
{
47+
throw new IllegalStateException("Unexpected byte value for boolean encoding: " + bytes[0]);
48+
}
49+
}
50+
2251
static byte[] timeToBytes(
2352
long t)
2453
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.bouncycastle.util.utiltest;
2+
3+
import junit.framework.TestCase;
4+
import org.bouncycastle.bcpg.sig.PrimaryUserID;
5+
6+
public class BytesBooleansTest
7+
extends TestCase
8+
{
9+
public void testParseFalse()
10+
{
11+
PrimaryUserID primaryUserID = new PrimaryUserID(true, false);
12+
13+
byte[] bFalse = primaryUserID.getData();
14+
assertEquals(1, bFalse.length);
15+
assertEquals(0, bFalse[0]);
16+
assertFalse(primaryUserID.isPrimaryUserID());
17+
}
18+
19+
public void testParseTrue()
20+
{
21+
PrimaryUserID primaryUserID = new PrimaryUserID(true, true);
22+
23+
byte[] bTrue = primaryUserID.getData();
24+
25+
assertEquals(1, bTrue.length);
26+
assertEquals(1, bTrue[0]);
27+
assertTrue(primaryUserID.isPrimaryUserID());
28+
}
29+
30+
public void testParseTooShort()
31+
{
32+
PrimaryUserID primaryUserID = new PrimaryUserID(true, false, new byte[0]);
33+
byte[] bTooShort = primaryUserID.getData();
34+
try
35+
{
36+
primaryUserID.isPrimaryUserID();
37+
fail("Should throw.");
38+
}
39+
catch (IllegalStateException e)
40+
{
41+
// expected.
42+
}
43+
}
44+
45+
public void testParseTooLong()
46+
{
47+
PrimaryUserID primaryUserID = new PrimaryUserID(true, false, new byte[42]);
48+
byte[] bTooLong = primaryUserID.getData();
49+
50+
try
51+
{
52+
primaryUserID.isPrimaryUserID();
53+
fail("Should throw.");
54+
}
55+
catch (IllegalStateException e)
56+
{
57+
// expected.
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)