Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/util/DerValue/Indefinite.java
41152 views
1
/*
2
* Copyright (c) 2008, 2020, 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 6731685 8249783
27
* @summary CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
28
* @modules java.base/sun.security.util:+open
29
* @library /test/lib
30
*/
31
32
import java.io.*;
33
import java.lang.reflect.Field;
34
import java.util.Arrays;
35
36
import jdk.test.lib.Asserts;
37
import jdk.test.lib.Utils;
38
import sun.security.util.*;
39
40
public class Indefinite {
41
42
public static void main(String[] args) throws Exception {
43
44
// Indefinite length with trailing bytes
45
test(true, new byte[] {
46
// An OCTET-STRING in 2 parts
47
0x24, (byte) 0x80, 4, 2, 'a', 'b', 4, 2, 'c', 'd', 0, 0,
48
// Garbage follows, may be falsely recognized as EOC
49
0, 0, 0, 0,
50
// and more
51
7, 8, 9, 10});
52
53
// Definite length with trailing bytes
54
test(false, new byte[] {
55
4, 4, 'a', 'b', 'c', 'd',
56
0, 0, 0, 0, 7, 8, 9, 10 });
57
}
58
59
static void test(boolean indefinite, byte[] input) throws Exception {
60
61
// 1. parse stream
62
InputStream ins = new ByteArrayInputStream(input);
63
DerValue v = new DerValue(ins);
64
Asserts.assertEQ(new String(v.getOctetString()), "abcd");
65
66
if (indefinite) {
67
// Trailing bytes might be consumed by the conversion but can
68
// be found in DerValue "after end".
69
Field buffer = DerValue.class.getDeclaredField("buffer");
70
Field end = DerValue.class.getDeclaredField("end");
71
buffer.setAccessible(true);
72
end.setAccessible(true);
73
int bufferLen = ((byte[])buffer.get(v)).length;
74
int endPos = end.getInt(v);
75
// Data "after end": bufferLen - endPos
76
// Data remained in stream: ins.available()x`
77
Asserts.assertEQ(bufferLen - endPos + ins.available(), 8);
78
} else {
79
// Trailing bytes remain in the stream for definite length
80
Asserts.assertEQ(ins.available(), 8);
81
}
82
83
// 2. parse DerInputStream
84
DerInputStream ds = new DerInputStream(input);
85
Asserts.assertEQ(new String(ds.getDerValue().getOctetString()), "abcd");
86
Asserts.assertEQ(ds.available(), 8);
87
Asserts.assertTrue(Arrays.equals(ds.toByteArray(),
88
new byte[]{0,0,0,0,7,8,9,10}));
89
90
// 3. Parse full byte array
91
Utils.runAndCheckException(() -> new DerValue(input),
92
e -> Asserts.assertTrue(e instanceof IOException
93
&& e.getMessage().equals("extra data at the end")));
94
95
// 4. Parse exact byte array
96
Asserts.assertEQ(new String(new DerValue(Arrays.copyOf(input, input.length - 8))
97
.getOctetString()), "abcd");
98
}
99
}
100
101