Skip to content

Commit

Permalink
Add FIPS services check in Provider put() method and debug info
Browse files Browse the repository at this point in the history
Signed-off-by: Tao Liu <tao.liu@ibm.com>
  • Loading branch information
taoliult committed Jun 5, 2023
1 parent 359f15b commit 10330be
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -585,10 +585,10 @@ private void initProviders() {
// Provider with argument (provider name + optional argument).
providers.add(pNum - 1, providerName);

// Remove the provider's optional arguments if there are.
// Remove the provider's optional arguments if present.
pos = providerName.indexOf(' ');
providerName = (pos < 0) ? providerName.trim() : providerName.substring(0, pos).trim();
// Remove the provider's class package names if there are.
// Remove the provider's class package names if present.
pos = providerName.lastIndexOf('.');
providerName = (pos < 0) ? providerName : providerName.substring(pos + 1, providerName.length());
// Provider without arguments and package names.
Expand Down Expand Up @@ -673,7 +673,7 @@ private void initConstraints() {
continue;
}

// Remove the whitespaces in the format separator if there are.
// Remove the whitespaces in the format separator if present.
providerInfo = providerInfo.trim()
.replaceAll("\\[\\s+\\{", "[{")
.replaceAll("\\}\\s+\\]", "}]")
Expand Down Expand Up @@ -759,6 +759,10 @@ boolean isRestrictedServiceAllowed(Service service) {

if (constraints == null) {
// Disallow unknown providers.
if (debug != null) {
debug.println("Security constraints check."
+ " Disallow unknown provider: " + providerName);
}
return false;
} else if (constraints.length == 0) {
// Allow this provider with no constraints.
Expand All @@ -774,12 +778,12 @@ boolean isRestrictedServiceAllowed(Service service) {
String cAlgorithm = constraint.algorithm;
String cAttribute = constraint.attributes;

if (!isAsterisk(cType) && !type.equals(cType)) {
if (!isAsterisk(cType) && !type.equalsIgnoreCase(cType)) {
// The constraint doesn't apply to the service type.
continue;
}
if (!isAsterisk(cAlgorithm) && !algorithm.equalsIgnoreCase(cAlgorithm)) {
// The constraint doesn't apply to the service algorith.
// The constraint doesn't apply to the service algorithm.
continue;
}

Expand All @@ -789,7 +793,7 @@ boolean isRestrictedServiceAllowed(Service service) {
debug.println("Security constraints check."
+ " Service type: " + type
+ " Algorithm: " + algorithm
+ " is allowed in provider " + providerName);
+ " is allowed in provider: " + providerName);
}
return true;
}
Expand Down Expand Up @@ -832,7 +836,7 @@ boolean isRestrictedServiceAllowed(Service service) {
debug.println("Security constraints check."
+ " Service type: " + type
+ " Algorithm: " + algorithm
+ " is NOT allowed in provider " + providerName);
+ " is NOT allowed in provider: " + providerName);
}
// No match for any constraint, return NOT allowed.
return false;
Expand All @@ -849,11 +853,11 @@ boolean isRestrictedProviderAllowed(String providerName) {
debug.println("Checking the provider " + providerName + " in restricted security mode.");
}

// Remove argument, e.g. -NSS-FIPS, if there is.
// Remove argument, e.g. -NSS-FIPS, if present.
int pos = providerName.indexOf('-');
providerName = (pos < 0) ? providerName : providerName.substring(0, pos);

// Remove the provider class package name if there is.
// Remove the provider class package name if present.
pos = providerName.lastIndexOf('.');
providerName = (pos < 0) ? providerName : providerName.substring(pos + 1, providerName.length());

Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/security/Provider.java
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ private void removeInvalidServices(Map<ServiceKey,Service> map) {
for (Iterator<Map.Entry<ServiceKey, Service>> t =
map.entrySet().iterator(); t.hasNext(); ) {
Service s = t.next().getValue();
if (s.isValid() == false) {
if ((s.isValid() == false) || !RestrictedSecurity.isServiceAllowed(s)) {
t.remove();
}
}
Expand Down

0 comments on commit 10330be

Please sign in to comment.