Skip to content

Commit 7c3c1a7

Browse files
Aleksei Voitylovgnu-andrew
authored andcommitted
8381519: Enhance Der Value Handling
Reviewed-by: tpushkin, andrew Backport-of: 96239a84aaa93b1e1ffd704799fd7cef1bf98a5e
1 parent 6d199d3 commit 7c3c1a7

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

src/java.base/share/classes/sun/security/util/DerValue.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -156,6 +156,9 @@ public class DerValue {
156156
*/
157157
public static final byte tag_SetOf = 0x31;
158158

159+
// Max nested depth for constructed data
160+
private static final int MAX_CONSTRUCTED_NEST = 30;
161+
159162
/*
160163
* These values are the high order bits for the other kinds of tags.
161164
*/
@@ -503,6 +506,14 @@ private byte[] append(byte[] a, byte[] b) {
503506
* @return the octet string held in this DER value
504507
*/
505508
public byte[] getOctetString() throws IOException {
509+
return getOctetString(0);
510+
}
511+
512+
private byte[] getOctetString(int limit) throws IOException {
513+
if (++limit > MAX_CONSTRUCTED_NEST) {
514+
throw new IOException("Nested OctetString limit reached ("
515+
+ MAX_CONSTRUCTED_NEST + ").");
516+
}
506517

507518
if (tag != tag_OctetString && !isConstructed(tag_OctetString)) {
508519
throw new IOException(
@@ -529,7 +540,7 @@ public byte[] getOctetString() throws IOException {
529540
buffer.allowBER);
530541
bytes = null;
531542
while (in.available() != 0) {
532-
bytes = append(bytes, in.getOctetString());
543+
bytes = append(bytes, in.getDerValue().getOctetString(limit));
533544
}
534545
}
535546
return bytes;

0 commit comments

Comments
 (0)