Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/sun/security/util/DerInputBuffer/PaddedBitString.java
66645 views
1
/*
2
* Copyright (c) 2002, 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 4511556
27
* @summary Verify BitString value containing padding bits is accepted.
28
* @modules java.base/sun.security.util
29
* @library /test/lib
30
*/
31
import java.io.*;
32
import java.math.BigInteger;
33
import java.util.Arrays;
34
import java.util.HexFormat;
35
import jdk.test.lib.Asserts;
36
import jdk.test.lib.Utils;
37
38
import sun.security.util.BitArray;
39
import sun.security.util.DerInputStream;
40
41
public class PaddedBitString {
42
43
// Relaxed the BitString parsing routine to accept bit strings
44
// with padding bits, ex. treat DER_BITSTRING_PAD6_b as the same
45
// bit string as DER_BITSTRING_PAD6_0/DER_BITSTRING_NOPAD.
46
// Note:
47
// 1. the number of padding bits has to be in [0...7]
48
// 2. value of the padding bits is ignored
49
50
// With no padding bits
51
private final static byte[] DER_BITSTRING_NOPAD = { 3, 3, 0,
52
(byte)0x5d, (byte)0xc0 };
53
// With 6 zero padding bits (01011101 11000000)
54
private final static byte[] DER_BITSTRING_PAD6_0 = { 3, 3, 6,
55
(byte)0x5d, (byte)0xc0 };
56
57
// With 6 nonzero padding bits (01011101 11001011)
58
private final static byte[] DER_BITSTRING_PAD6_b = { 3, 3, 6,
59
(byte)0x5d, (byte)0xcb };
60
61
// With 8 padding bits
62
private final static byte[] DER_BITSTRING_PAD8_0 = { 3, 3, 8,
63
(byte)0x5d, (byte)0xc0 };
64
65
private final static byte[] BITS = { (byte)0x5d, (byte)0xc0 };
66
67
static enum Type {
68
BIT_STRING,
69
UNALIGNED_BIT_STRING;
70
}
71
72
public static void main(String args[]) throws Exception {
73
test(DER_BITSTRING_NOPAD, new BitArray(16, BITS));
74
test(DER_BITSTRING_PAD6_0, new BitArray(10, BITS));
75
test(DER_BITSTRING_PAD6_b, new BitArray(10, BITS));
76
test(DER_BITSTRING_PAD8_0, null);
77
System.out.println("Tests Passed");
78
}
79
80
private static void test(byte[] in, BitArray ans) throws IOException {
81
System.out.println("Testing " +
82
HexFormat.of().withUpperCase().formatHex(in));
83
for (Type t : Type.values()) {
84
DerInputStream derin = new DerInputStream(in);
85
boolean shouldPass = (ans != null);
86
switch (t) {
87
case BIT_STRING:
88
if (shouldPass) {
89
Asserts.assertTrue(Arrays.equals(ans.toByteArray(),
90
derin.getBitString()));
91
} else {
92
Utils.runAndCheckException(() -> derin.getBitString(),
93
IOException.class);
94
}
95
break;
96
case UNALIGNED_BIT_STRING:
97
if (shouldPass) {
98
Asserts.assertEQ(ans, derin.getUnalignedBitString());
99
} else {
100
Utils.runAndCheckException(() ->
101
derin.getUnalignedBitString(), IOException.class);
102
}
103
break;
104
}
105
}
106
}
107
}
108
109