Skip to content

Commit af6cd72

Browse files
committed
Refactor ASN1Null parsing
1 parent 9a62750 commit af6cd72

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

core/src/main/java/org/bouncycastle/asn1/ASN1InputStream.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,10 @@ static ASN1Primitive createPrimitiveDERObject(
540540
case INTEGER:
541541
return ASN1Integer.createPrimitive(defIn.toByteArray());
542542
case NULL:
543-
return ASN1Null.createPrimitive(defIn.toByteArray());
543+
{
544+
ASN1Null.checkContentsLength(defIn.getRemaining());
545+
return ASN1Null.createPrimitive();
546+
}
544547
case NUMERIC_STRING:
545548
return ASN1NumericString.createPrimitive(defIn.toByteArray());
546549
case OBJECT_DESCRIPTOR:

core/src/main/java/org/bouncycastle/asn1/ASN1Null.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public abstract class ASN1Null
1212
{
1313
ASN1Primitive fromImplicitPrimitive(DEROctetString octetString)
1414
{
15-
return createPrimitive(octetString.getOctets());
15+
checkContentsLength(octetString.getOctetsLength());
16+
return createPrimitive();
1617
}
1718
};
1819

@@ -88,12 +89,16 @@ public String toString()
8889
return "NULL";
8990
}
9091

91-
static ASN1Null createPrimitive(byte[] contents)
92+
static void checkContentsLength(int contentsLength)
9293
{
93-
if (0 != contents.length)
94+
if (0 != contentsLength)
9495
{
9596
throw new IllegalStateException("malformed NULL encoding encountered");
9697
}
98+
}
99+
100+
static ASN1Null createPrimitive()
101+
{
97102
return DERNull.INSTANCE;
98103
}
99104
}

0 commit comments

Comments
 (0)