8381519: Enhance Der Value Handling

Reviewed-by: mschoene, jnimeh, valeriep
This commit is contained in:
Anthony Scarpino 2026-04-16 17:15:33 +00:00 committed by bchristi
parent 33e220059b
commit 48d32601cd

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -157,6 +157,9 @@ public class DerValue {
*/
public static final byte tag_SetOf = 0x31;
// Max nested depth for constructed data
private static final int MAX_CONSTRUCTED_NEST = 30;
// This class is mostly immutable except that:
//
// 1. resetTag() modifies the tag
@ -564,6 +567,14 @@ public class DerValue {
* @return the octet string held in this DER value
*/
public byte[] getOctetString() throws IOException {
return getOctetString(0);
}
private byte[] getOctetString(int limit) throws IOException {
if (++limit > MAX_CONSTRUCTED_NEST) {
throw new IOException("Nested OctetString limit reached ("
+ MAX_CONSTRUCTED_NEST + ").");
}
if (tag != tag_OctetString && !isConstructed(tag_OctetString)) {
throw new IOException(
@ -582,7 +593,7 @@ public class DerValue {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DerInputStream dis = data();
while (dis.available() > 0) {
bout.write(dis.getDerValue().getOctetString());
bout.write(dis.getDerValue().getOctetString(limit));
}
return bout.toByteArray();
}