Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/crypto/CryptoPermission/CryptoPolicyFallback.java
38840 views
1
/*
2
* Copyright (c) 2016, 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 8169335
29
* @summary Add a crypto policy fallback in case Security Property
30
* 'crypto.policy' does not exist.
31
* @run main/othervm CryptoPolicyFallback
32
*/
33
import java.io.*;
34
import java.nio.file.*;
35
import java.util.stream.*;
36
import javax.crypto.*;
37
38
/*
39
* Take the current java.security file, strip out the 'crypto.policy' entry,
40
* write to a new file in the current directory, then use that file as the
41
* replacement java.security file. This test will fail if the crypto.policy
42
* entry doesn't match the compiled in value.
43
*/
44
public class CryptoPolicyFallback {
45
46
private static final String FILENAME = "java.security";
47
48
public static void main(String[] args) throws Exception {
49
50
String javaHome = System.getProperty("java.home");
51
52
Path path = Paths.get(javaHome, "lib", "security", FILENAME);
53
54
/*
55
* Get the default value.
56
*/
57
String defaultPolicy;
58
try (Stream<String> lines = Files.lines(path)) {
59
/*
60
* If the input java.security file is malformed
61
* (missing crypto.policy, attribute/no value, etc), throw
62
* exception. split() might throw AIOOB which
63
* is ok behavior.
64
*/
65
String s = lines.filter(x -> x.startsWith("crypto.policy="))
66
.findFirst().orElse("");
67
if (!s.isEmpty()) {
68
defaultPolicy = s.split("=")[1].trim();
69
} else {
70
defaultPolicy = s;
71
}
72
}
73
74
/*
75
* We know there is at least one crypto.policy entry, strip
76
* all of them out of the java.security file.
77
*/
78
try (PrintWriter out = new PrintWriter(FILENAME);
79
Stream<String> lines = Files.lines(path)) {
80
lines.filter(x -> !x.trim().startsWith("crypto.policy="))
81
.forEach(out::println);
82
}
83
84
/*
85
* "-Djava.security.properties==file" does a complete replacement
86
* of the system java.security file. i.e. value must be "=file"
87
*/
88
System.setProperty("java.security.properties", "=" + FILENAME);
89
90
/*
91
* Find out expected value.
92
*/
93
int expected;
94
switch (defaultPolicy) {
95
case "limited":
96
expected = 128;
97
break;
98
case "":
99
case "unlimited":
100
expected = Integer.MAX_VALUE;
101
break;
102
default:
103
throw new Exception(
104
"Unexpected Default Policy Value: " + defaultPolicy);
105
}
106
107
/*
108
* Do the actual check. If the JCE Framework can't initialize
109
* an Exception is normally thrown here.
110
*/
111
int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
112
113
System.out.println("Default Policy: " + defaultPolicy
114
+ "\nExpected max AES key length: " + expected
115
+ ", received : " + maxKeyLen);
116
117
if (expected != maxKeyLen) {
118
throw new Exception("Wrong Key Length size!");
119
}
120
121
System.out.println("PASSED!");
122
}
123
}
124
125