Skip to content

Commit 3d2b4cc

Browse files
committed
8264864: Multiple byte tag not supported by ASN.1 encoding
Reviewed-by: xuelei
1 parent ccefa5e commit 3d2b4cc

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2021, 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
@@ -221,6 +221,9 @@ public boolean isConstructed(byte constructedTag) {
221221
* Creates a new DerValue by specifying all its fields.
222222
*/
223223
DerValue(byte tag, byte[] buffer, int start, int end, boolean allowBER) {
224+
if ((tag & 0x1f) == 0x1f) {
225+
throw new IllegalArgumentException("Tag number over 30 is not supported");
226+
}
224227
this.tag = tag;
225228
this.buffer = buffer;
226229
this.start = start;
@@ -315,6 +318,9 @@ public DerValue(byte[] encoding) throws IOException {
315318
}
316319
int pos = offset;
317320
tag = buf[pos++];
321+
if ((tag & 0x1f) == 0x1f) {
322+
throw new IOException("Tag number over 30 at " + offset + " is not supported");
323+
}
318324
int lenByte = buf[pos++];
319325

320326
int length;
@@ -388,6 +394,9 @@ public DerValue(byte[] encoding) throws IOException {
388394
// arg to control whether DER checks are enforced.
389395
DerValue(InputStream in, boolean allowBER) throws IOException {
390396
this.tag = (byte)in.read();
397+
if ((tag & 0x1f) == 0x1f) {
398+
throw new IOException("Tag number over 30 is not supported");
399+
}
391400
int length = DerInputStream.getLength(in);
392401
if (length == -1) { // indefinite length encoding found
393402
if (!allowBER) {
@@ -1140,6 +1149,9 @@ public static boolean isPrintableStringChar(char ch) {
11401149
* @param val the tag value
11411150
*/
11421151
public static byte createTag(byte tagClass, boolean form, byte val) {
1152+
if (val < 0 || val > 30) {
1153+
throw new IllegalArgumentException("Tag number over 30 is not supported");
1154+
}
11431155
byte tag = (byte)(tagClass | val);
11441156
if (form) {
11451157
tag |= (byte)0x20;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8264864
27+
* @summary Multiple byte tag not supported by ASN.1 encoding
28+
* @modules java.base/sun.security.util
29+
* @library /test/lib
30+
*/
31+
32+
import jdk.test.lib.Utils;
33+
import sun.security.util.DerInputStream;
34+
import sun.security.util.DerValue;
35+
36+
import java.io.IOException;
37+
38+
public class WideTag {
39+
40+
public static void main(String[] args) throws Exception {
41+
42+
// Small ones
43+
DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)30);
44+
DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0);
45+
46+
// Big ones
47+
Utils.runAndCheckException(
48+
() -> DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)31),
49+
IllegalArgumentException.class);
50+
Utils.runAndCheckException(
51+
() -> DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)222),
52+
IllegalArgumentException.class);
53+
54+
// We don't accept number 31
55+
Utils.runAndCheckException(() -> new DerValue((byte)0xbf, new byte[10]),
56+
IllegalArgumentException.class);
57+
58+
// CONTEXT [98] size 97. Not supported. Should fail.
59+
// Before this fix, it was interpreted as CONTEXT [31] size 98.
60+
byte[] wideDER = new byte[100];
61+
wideDER[0] = (byte)0xBF;
62+
wideDER[1] = (byte)98;
63+
wideDER[2] = (byte)97;
64+
65+
Utils.runAndCheckException(() -> new DerValue(wideDER),
66+
IOException.class);
67+
Utils.runAndCheckException(() -> new DerInputStream(wideDER).getDerValue(),
68+
IOException.class);
69+
}
70+
}

0 commit comments

Comments
 (0)