Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/crypto/CryptoPermissions/TestExemption.java
41149 views
1
/*
2
* Copyright (c) 2016, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
/*
27
* @test
28
* @bug 8161527 8180568
29
* @summary NPE is thrown if exempt application is bundled with specific
30
* cryptoPerms
31
* @requires java.runtime.name ~= "OpenJDK.*"
32
* @library /test/lib
33
* @run main TestExemption
34
*/
35
import javax.crypto.*;
36
import java.nio.file.Path;
37
import java.security.*;
38
import java.util.ArrayList;
39
import java.util.List;
40
import jdk.test.lib.process.OutputAnalyzer;
41
import jdk.test.lib.process.ProcessTools;
42
import jdk.test.lib.util.JarUtils;
43
44
public class TestExemption {
45
46
private static final String SRC = System.getProperty("test.src");
47
private static final String CLASSES = System.getProperty("test.classes");
48
private static final String NAME = TestExemption.class.getName();
49
private static final String SRC_CLS = NAME + ".class";
50
private static final String JAR_FILE = NAME + ".jar";
51
private static final String CRYPT_PERM = "cryptoPerms";
52
53
public static void main(String[] args) throws Exception {
54
55
// With no argument passed, compile the same class, jar it and run the
56
// test section of the jar file which is nothing but else section here.
57
if (args.length == 0) {
58
JarUtils.createJarFile(
59
Path.of(JAR_FILE), Path.of(CLASSES), Path.of(SRC_CLS));
60
JarUtils.updateJarFile(
61
Path.of(JAR_FILE), Path.of(SRC), Path.of(CRYPT_PERM));
62
OutputAnalyzer oa = ProcessTools.executeTestJava(
63
getParameters().toArray(String[]::new));
64
System.out.println(oa.getOutput());
65
oa.shouldHaveExitValue(0);
66
} else {
67
// Set the crypto policy to limited so that additional policy can be
68
// supplemented through cryptoPerms when bundled inside a jar file.
69
Security.setProperty("crypto.policy", "limited");
70
KeyGenerator kg = KeyGenerator.getInstance("AES");
71
kg.init(128);
72
SecretKey key128 = kg.generateKey();
73
74
kg.init(192);
75
SecretKey key192 = kg.generateKey();
76
77
kg.init(256);
78
SecretKey key256 = kg.generateKey();
79
80
int maxAllowed = Cipher.getMaxAllowedKeyLength("AES");
81
System.out.println("Max allowed: " + maxAllowed);
82
// With limited crypto and bundled cryptoPerms maximum allowed
83
// length of AES is upto 192.
84
if (maxAllowed > 192) {
85
throw new RuntimeException(">192 not supported");
86
}
87
88
Cipher c = Cipher.getInstance("AES/CBC/NoPadding");
89
System.out.println("Testing 128-bit");
90
c.init(Cipher.ENCRYPT_MODE, key128);
91
92
System.out.println("Testing 192-bit");
93
c.init(Cipher.ENCRYPT_MODE, key192);
94
try {
95
System.out.println("Testing 256-bit");
96
c.init(Cipher.ENCRYPT_MODE, key256);
97
throw new RuntimeException("Shouldn't reach here");
98
} catch (InvalidKeyException e) {
99
System.out.println("Caught the right exception");
100
}
101
System.out.println("DONE!");
102
}
103
}
104
105
private static List<String> getParameters() {
106
107
List<String> cmds = new ArrayList<>();
108
cmds.add("-cp");
109
cmds.add(JAR_FILE);
110
cmds.add(NAME);
111
// Argument to run the Test section of class inside the jar file.
112
cmds.add("run");
113
return cmds;
114
}
115
116
}
117
118