Skip to content

Commit 85eaae5

Browse files
committed
Check RestrictedSecurity profile for errors
This commit checks the misspelled properties and provider order numbers in RestrictedSecurity mode. If there is a misspelled property or the provider order numbers are not consecutive. The RestrictedSecurity mode initializion will stop and throw errors. Signed-off-by: Tao Liu <[email protected]>
1 parent d3956a6 commit 85eaae5

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

closed/src/java.base/share/classes/openj9/internal/security/RestrictedSecurity.java

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
import java.time.format.DateTimeParseException;
3636
import java.util.ArrayList;
3737
import java.util.Deque;
38+
import java.util.Enumeration;
3839
import java.util.HashMap;
3940
import java.util.HashSet;
4041
import java.util.Iterator;
41-
import java.util.LinkedList;
4242
import java.util.List;
4343
import java.util.Map;
4444
import java.util.Objects;
@@ -994,6 +994,9 @@ private static final class ProfileParser {
994994
// The java.security properties.
995995
private final Properties securityProps;
996996

997+
private final Set<String> profileCheckPropertyNames;
998+
private final Set<String> profileCheckProviderNames;
999+
9971000
/**
9981001
*
9991002
* @param id the restricted security custom profile ID
@@ -1016,8 +1019,13 @@ private ProfileParser(String id, Properties props) {
10161019

10171020
parsedProfiles = new HashSet<>();
10181021

1022+
profileCheckPropertyNames = new HashSet<>();
1023+
profileCheckProviderNames = new HashSet<>();
1024+
10191025
// Initialize the properties.
10201026
init(profileID);
1027+
1028+
checkProfileCheck(profileID);
10211029
}
10221030

10231031
private RestrictedSecurityProperties getProperties() {
@@ -1040,12 +1048,17 @@ private void init(String profileID) {
10401048
printStackTraceAndExit(profileID + " has already been parsed. Potential infinite recursion.");
10411049
}
10421050

1043-
String potentialExtendsProfileID = parseProperty(securityProps.getProperty(profileID + ".extends"));
1051+
loadProfileCheck(profileID);
1052+
1053+
String profileExtends = profileID + ".extends";
1054+
String potentialExtendsProfileID = parseProperty(securityProps.getProperty(profileExtends));
10441055
if (potentialExtendsProfileID != null) { // If profile extends another profile.
10451056
if (debug != null) {
10461057
debug.println("\t'" + profileID + "' extends '" + potentialExtendsProfileID + "'.");
10471058
}
10481059

1060+
profileCheckPropertyNames.remove(profileExtends);
1061+
10491062
// Check if extended profile exists.
10501063
String extendsProfileID = null;
10511064
if (potentialExtendsProfileID.indexOf('.') != potentialExtendsProfileID.lastIndexOf('.')) {
@@ -1098,6 +1111,7 @@ private void init(String profileID) {
10981111
// Save info to be hashed and expected result to be checked later.
10991112
profilesHashes.put(profileID, hashValue);
11001113
profilesInfo.put(profileID, allInfo);
1114+
profileCheckPropertyNames.remove(hashProperty);
11011115
} else if (!isFIPS1402Profile(profileID)) {
11021116
// A hash is mandatory, but not for older 140-2 profiles.
11031117
printStackTraceAndExit(profileID + " is a base profile, so a hash value is mandatory.");
@@ -1134,6 +1148,7 @@ private void update(String profileExtensionId) {
11341148
// Save info to be hashed and expected result to be checked later.
11351149
profilesHashes.put(profileID, hashValue);
11361150
profilesInfo.put(profileID, allInfo);
1151+
profileCheckPropertyNames.remove(hashProperty);
11371152
}
11381153
} catch (Exception e) {
11391154
if (debug != null) {
@@ -1254,6 +1269,7 @@ private void initProviders(String profileID, List<String> allInfo) {
12541269
allInfo.add(property + "=" + providerInfo);
12551270

12561271
parseProvider(providerInfo, pNum, false);
1272+
profileCheckProviderNames.remove(property);
12571273
}
12581274

12591275
if (providers.isEmpty()) {
@@ -1284,6 +1300,7 @@ private void updateProviders(String profileExtensionId, List<String> allInfo) {
12841300
removedProvider = true;
12851301
break;
12861302
}
1303+
profileCheckProviderNames.remove(property);
12871304
}
12881305
}
12891306

@@ -1311,6 +1328,7 @@ private void updateProviders(String profileExtensionId, List<String> allInfo) {
13111328
allInfo.add(property + "=" + providerInfo);
13121329

13131330
parseProvider(providerInfo, i, false);
1331+
profileCheckProviderNames.remove(property);
13141332
}
13151333
}
13161334

@@ -1634,6 +1652,7 @@ private boolean setProperty(String property, String propertyKey, List<String> al
16341652
newValue = value;
16351653
}
16361654
profileProperties.put(property, newValue);
1655+
profileCheckPropertyNames.remove(propertyKey);
16371656
return true;
16381657
}
16391658
if (debug != null) {
@@ -1706,6 +1725,39 @@ private static void checkProviderFormat(String providerInfo, boolean update) {
17061725
printStackTraceAndExit("Provider format is incorrect: " + providerInfo);
17071726
}
17081727
}
1728+
1729+
private void loadProfileCheck(String profileID) {
1730+
Enumeration<?> pNames = securityProps.propertyNames();
1731+
String profileDot = profileID + '.';
1732+
while (pNames.hasMoreElements()) {
1733+
String name = (String) pNames.nextElement();
1734+
if (name.startsWith(profileDot)) {
1735+
if (name.contains(".jce.provider.")) {
1736+
profileCheckProviderNames.add(name);
1737+
} else {
1738+
profileCheckPropertyNames.add(name);
1739+
}
1740+
}
1741+
}
1742+
}
1743+
1744+
private void checkProfileCheck(String profileID) {
1745+
if (!profileCheckProviderNames.isEmpty()) {
1746+
printStackTraceAndExit(
1747+
"The order numbers of providers in profile " + profileID
1748+
+ " (or a base profile) are not consecutive.");
1749+
}
1750+
if (!profileCheckPropertyNames.isEmpty()) {
1751+
printStackTraceAndExit(
1752+
"The property names: "
1753+
+ profileCheckPropertyNames
1754+
.stream()
1755+
.sorted()
1756+
.collect(Collectors.joining(", "))
1757+
+ " in profile " + profileID
1758+
+ " (or a base profile) are not recognized.");
1759+
}
1760+
}
17091761
}
17101762

17111763
/**

0 commit comments

Comments
 (0)