Skip to content

Commit

Permalink
added sanity checks for boolean flags - related to github #1575
Browse files Browse the repository at this point in the history
  • Loading branch information
dghgit committed Mar 16, 2024
1 parent 47ce473 commit 98a0fbe
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pg/src/main/java/org/bouncycastle/bcpg/sig/Exportable.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public Exportable(

public boolean isExportable()
{
return data[0] != 0;
return Utils.booleanFromByteArray(data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public PrimaryUserID(

public boolean isPrimaryUserID()
{
return data[0] != 0;
return Utils.booleanFromByteArray(data);
}
}
2 changes: 1 addition & 1 deletion pg/src/main/java/org/bouncycastle/bcpg/sig/Revocable.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public Revocable(

public boolean isRevocable()
{
return data[0] != 0;
return Utils.booleanFromByteArray(data);
}
}
29 changes: 29 additions & 0 deletions pg/src/main/java/org/bouncycastle/bcpg/sig/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,35 @@ static byte[] booleanToByteArray(boolean value)
return data;
}

/**
* Convert a one-entry byte array into a boolean.
* If the byte array doesn't have one entry, or if this entry is neither a 0 nor 1, this method throws an
* {@link IllegalArgumentException}.
* A 1 is translated into true, a 0 into false.
*
* @param bytes byte array
* @return boolean
*/
static boolean booleanFromByteArray(byte[] bytes)
{
if (bytes.length != 1)
{
throw new IllegalStateException("Byte array has unexpected length. Expected length 1, got " + bytes.length);
}
if (bytes[0] == 0)
{
return false;
}
else if (bytes[0] == 1)
{
return true;
}
else
{
throw new IllegalStateException("Unexpected byte value for boolean encoding: " + bytes[0]);
}
}

static byte[] timeToBytes(
long t)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.bouncycastle.util.utiltest;

import junit.framework.TestCase;
import org.bouncycastle.bcpg.sig.PrimaryUserID;

public class BytesBooleansTest
extends TestCase
{
public void testParseFalse()
{
PrimaryUserID primaryUserID = new PrimaryUserID(true, false);

byte[] bFalse = primaryUserID.getData();
assertEquals(1, bFalse.length);
assertEquals(0, bFalse[0]);
assertFalse(primaryUserID.isPrimaryUserID());
}

public void testParseTrue()
{
PrimaryUserID primaryUserID = new PrimaryUserID(true, true);

byte[] bTrue = primaryUserID.getData();

assertEquals(1, bTrue.length);
assertEquals(1, bTrue[0]);
assertTrue(primaryUserID.isPrimaryUserID());
}

public void testParseTooShort()
{
PrimaryUserID primaryUserID = new PrimaryUserID(true, false, new byte[0]);
byte[] bTooShort = primaryUserID.getData();
try
{
primaryUserID.isPrimaryUserID();
fail("Should throw.");
}
catch (IllegalStateException e)
{
// expected.
}
}

public void testParseTooLong()
{
PrimaryUserID primaryUserID = new PrimaryUserID(true, false, new byte[42]);
byte[] bTooLong = primaryUserID.getData();

try
{
primaryUserID.isPrimaryUserID();
fail("Should throw.");
}
catch (IllegalStateException e)
{
// expected.
}
}
}

0 comments on commit 98a0fbe

Please sign in to comment.