Skip to content

Commit 9022e06

Browse files
committed
Backport 8f400b9aab57d0639721add2ba511bfc0459bd89
1 parent d70ea49 commit 9022e06

File tree

2 files changed

+100
-7
lines changed

2 files changed

+100
-7
lines changed

src/java.base/share/classes/javax/crypto/CryptoPolicyParser.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ final class CryptoPolicyParser {
7070
// Convenience variables for parsing
7171
private StreamTokenizer st;
7272
private int lookahead;
73+
private boolean allPermEntryFound = false;
7374

7475
/**
7576
* Creates a CryptoPolicyParser object.
@@ -128,7 +129,7 @@ void read(Reader policy)
128129
* The crypto jurisdiction policy must be consistent. The
129130
* following hashtable is used for checking consistency.
130131
*/
131-
Hashtable<String, Vector<String>> processedPermissions = null;
132+
Hashtable<String, Vector<String>> processedPermissions = new Hashtable<>();
132133

133134
/*
134135
* The main parsing loop. The loop is executed once for each entry
@@ -191,6 +192,16 @@ private CryptoPermissionEntry parsePermissionEntry(
191192
e.cryptoPermission = match("permission type");
192193

193194
if (e.cryptoPermission.equals("javax.crypto.CryptoAllPermission")) {
195+
/*
196+
* This catches while processing the "javax.crypto.CryptoAllPermission"
197+
* entry, but the "processedPermissions" Hashtable already contains
198+
* an entry for "javax.crypto.CryptoPermission".
199+
*/
200+
if (!processedPermissions.isEmpty()) {
201+
throw new ParsingException(st.lineno(), "Inconsistent policy");
202+
}
203+
allPermEntryFound = true;
204+
194205
// Done with the CryptoAllPermission entry.
195206
e.alg = CryptoAllPermission.ALG_NAME;
196207
e.maxKeySize = Integer.MAX_VALUE;
@@ -498,18 +509,21 @@ private boolean isConsistent(String alg, String exemptionMechanism,
498509
String thisExemptionMechanism =
499510
exemptionMechanism == null ? "none" : exemptionMechanism;
500511

501-
if (processedPermissions == null) {
502-
processedPermissions = new Hashtable<String, Vector<String>>();
512+
/*
513+
* This catches while processing a "javax.crypto.CryptoPermission" entry, but
514+
* "javax.crypto.CryptoAllPermission" entry already exists.
515+
*/
516+
if (allPermEntryFound) {
517+
return false;
518+
}
519+
520+
if (processedPermissions.isEmpty()) {
503521
Vector<String> exemptionMechanisms = new Vector<>(1);
504522
exemptionMechanisms.addElement(thisExemptionMechanism);
505523
processedPermissions.put(alg, exemptionMechanisms);
506524
return true;
507525
}
508526

509-
if (processedPermissions.containsKey(CryptoAllPermission.ALG_NAME)) {
510-
return false;
511-
}
512-
513527
Vector<String> exemptionMechanisms;
514528

515529
if (processedPermissions.containsKey(alg)) {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) 2022, 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 8286779
29+
* @summary Test limited/default_local.policy containing inconsistent entries
30+
* @run main/manual InconsistentEntries
31+
*/
32+
import javax.crypto.*;
33+
import java.io.File;
34+
import java.nio.file.Files;
35+
import java.nio.file.Path;
36+
import java.security.Security;
37+
38+
public class InconsistentEntries {
39+
40+
public static void main(String[] args) throws Exception {
41+
System.out.println("***********************************************************");
42+
System.out.println("// This is a manual test to test a custom \"default_local.policy\" containing inconsistent entries");
43+
System.out.println("// under a new subfolder \"$JAVA_HOME/conf/security/policy\" directory.");
44+
System.out.println("// This test fails when the policy directory \"testlimited\" or the policy \"default_local.policy");
45+
System.out.println("// does not exist or is empty.");
46+
System.out.println("// - Create a new subfolder \"testlimited\" under \"$JAVA_HOME/conf/security/policy\"");
47+
System.out.println("// - Place the custom \"default_local.policy\" under \"testlimited\" directory");
48+
System.out.println("// - default_local.policy contains:");
49+
System.out.println("// grant {");
50+
System.out.println("// permission javax.crypto.CryptoAllPermission;");
51+
System.out.println("// permission javax.crypto.CryptoPermission \"DES\", 64;");
52+
System.out.println("// };");
53+
System.out.println("***********************************************************");
54+
55+
String JAVA_HOME = System.getProperty("java.home");
56+
String FS = System.getProperty("file.separator");
57+
Path testlimited = Path.of(JAVA_HOME + FS + "conf" + FS + "security" +
58+
FS + "policy" + FS + "testlimited");
59+
if (!Files.exists(testlimited)) {
60+
throw new RuntimeException("custom policy subdirectory: testlimited does not exist");
61+
}
62+
63+
File testpolicy = new File(JAVA_HOME + FS + "conf" + FS + "security" +
64+
FS + "policy" + FS + "testlimited" + FS + "default_local.policy");
65+
if (testpolicy.length() == 0) {
66+
throw new RuntimeException("policy: default_local.policy does not exist or is empty");
67+
}
68+
69+
Security.setProperty("crypto.policy", "testlimited");
70+
71+
try {
72+
int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
73+
throw new RuntimeException("Should fail due to inconsistent entries in policy file");
74+
} catch (ExceptionInInitializerError e) {
75+
e.printStackTrace();
76+
System.out.println("Test completed successfully");
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)