23
23
*/
24
24
package openj9 .internal .security ;
25
25
26
+ import java .lang .StringBuilder ;
26
27
import java .nio .charset .StandardCharsets ;
27
28
import java .security .MessageDigest ;
28
29
import java .security .NoSuchAlgorithmException ;
@@ -239,7 +240,7 @@ public static boolean isFIPSEnabled() {
239
240
*/
240
241
public static boolean isServiceAllowed (Service service ) {
241
242
if (securityEnabled ) {
242
- return restricts .isRestrictedServiceAllowed (service , false );
243
+ return restricts .isRestrictedServiceAllowed (service , true );
243
244
}
244
245
return true ;
245
246
}
@@ -252,7 +253,7 @@ public static boolean isServiceAllowed(Service service) {
252
253
*/
253
254
public static boolean canServiceBeRegistered (Service service ) {
254
255
if (securityEnabled ) {
255
- return restricts .isRestrictedServiceAllowed (service , true );
256
+ return restricts .isRestrictedServiceAllowed (service , false );
256
257
}
257
258
return true ;
258
259
}
@@ -753,10 +754,11 @@ private RestrictedSecurityProperties(String profileID, ProfileParser parser) {
753
754
/**
754
755
* Check if the Service is allowed in restricted security mode.
755
756
*
756
- * @param service the Service to check
757
+ * @param service the Service to check
758
+ * @param checkUse should its attempted use be checked against the accepted
757
759
* @return true if the Service is allowed
758
760
*/
759
- boolean isRestrictedServiceAllowed (Service service , boolean isServiceAdded ) {
761
+ boolean isRestrictedServiceAllowed (Service service , boolean checkUse ) {
760
762
Provider provider = service .getProvider ();
761
763
String providerClassName = provider .getClass ().getName ();
762
764
@@ -854,12 +856,8 @@ boolean isRestrictedServiceAllowed(Service service, boolean isServiceAdded) {
854
856
855
857
// See if accepted uses have been specified and apply
856
858
// them to the call stack.
857
- if (! isServiceAdded && !isNullOrBlank (cAcceptedUses )) {
859
+ if (checkUse && !isNullOrBlank (cAcceptedUses )) {
858
860
String [] optionAndValue = cAcceptedUses .split (":" );
859
- if (optionAndValue .length != 2 ) {
860
- printStackTraceAndExit ("Incorrect specification of accepted uses in constraint: '"
861
- + constraint + "'. Couldn't find option and value separated by ':'" );
862
- }
863
861
String option = optionAndValue [0 ];
864
862
String value = optionAndValue [1 ];
865
863
StackTraceElement [] stackElements = Thread .currentThread ().getStackTrace ();
@@ -870,43 +868,33 @@ boolean isRestrictedServiceAllowed(Service service, boolean isServiceAdded) {
870
868
}
871
869
String stackElemModule = stackElement .getModuleName ();
872
870
String stackElemFullClassName = stackElement .getClassName ();
873
- int stackElemEnd = stackElemFullClassName .lastIndexOf ("." );
871
+ int stackElemEnd = stackElemFullClassName .lastIndexOf ('.' );
874
872
String stackElemPackage = null ;
875
873
if (stackElemEnd != -1 ) {
876
874
stackElemPackage = stackElemFullClassName .substring (0 , stackElemEnd );
877
875
}
878
876
String module ;
879
877
switch (option ) {
880
- case "ModuleAndFullClassName" :
881
- String [] moduleAndFullClassName = value .split ("/" );
882
- if (moduleAndFullClassName .length != 2 ) {
883
- printStackTraceAndExit ("Incorrect specification of accepted uses in constraint: '"
884
- + constraint + "'. Couldn't find module and classname separated by '/'" );
885
- }
886
- module = moduleAndFullClassName [0 ];
887
- String fullClassName = moduleAndFullClassName [1 ];
888
- found = (stackElemModule != null ) && stackElemModule .equals (module )
889
- && stackElemFullClassName .equals (fullClassName );
890
- break ;
891
- case "ModuleAndPackage" :
892
- String [] moduleAndPackage = value .split ("/" );
893
- if (moduleAndPackage .length != 2 ) {
894
- printStackTraceAndExit ("Incorrect specification of accepted uses in constraint: '"
895
- + constraint + "'. Couldn't find module and classname separated by '/'" );
896
- }
897
- module = moduleAndPackage [0 ];
898
- String packageValue = moduleAndPackage [1 ];
899
- found = (stackElemModule != null ) && stackElemModule .equals (module )
900
- && (stackElemPackage != null ) && stackElemPackage .equals (packageValue );
901
- break ;
902
- case "FullClassName" :
903
- found = stackElemFullClassName .equals (value );
904
- break ;
905
- case "Package" :
906
- found = (stackElemPackage != null ) && stackElemPackage .equals (value );
907
- break ;
908
- default :
909
- printStackTraceAndExit ("Incorrect option to match in constraint: " + constraint );
878
+ case "ModuleAndFullClassName" :
879
+ String [] moduleAndFullClassName = value .split ("/" );
880
+ module = moduleAndFullClassName [0 ];
881
+ String fullClassName = moduleAndFullClassName [1 ];
882
+ found = module .equals (stackElemModule ) && stackElemFullClassName .equals (fullClassName );
883
+ break ;
884
+ case "ModuleAndPackage" :
885
+ String [] moduleAndPackage = value .split ("/" );
886
+ module = moduleAndPackage [0 ];
887
+ String packageValue = moduleAndPackage [1 ];
888
+ found = module .equals (stackElemModule ) && packageValue .equals (stackElemPackage );
889
+ break ;
890
+ case "FullClassName" :
891
+ found = stackElemFullClassName .equals (value );
892
+ break ;
893
+ case "Package" :
894
+ found = value .equals (stackElemPackage );
895
+ break ;
896
+ default :
897
+ printStackTraceAndExit ("Incorrect option to match in constraint: " + constraint );
910
898
}
911
899
912
900
if (found ) {
@@ -1549,10 +1537,40 @@ private void setConstraints(String providerName, String providerInfo, boolean pr
1549
1537
String inAttributes = m .group (3 );
1550
1538
String inAcceptedUses = m .group (4 );
1551
1539
1552
- if (isNullOrBlank (inAcceptedUses )) {
1553
- inAcceptedUses = null ;
1554
- } else {
1540
+ if (inAcceptedUses != null ) {
1555
1541
inAcceptedUses = inAcceptedUses .substring (1 );
1542
+ boolean isSpecIncorrect = false ;
1543
+ String [] optionAndValue = inAcceptedUses .split (":" );
1544
+ if (optionAndValue .length != 2 ) {
1545
+ isSpecIncorrect = true ;
1546
+ }
1547
+ String option = optionAndValue [0 ];
1548
+ String value = optionAndValue [1 ];
1549
+ switch (option ) {
1550
+ case "ModuleAndFullClassName" :
1551
+ String [] moduleAndFullClassName = value .split ("/" );
1552
+ if (moduleAndFullClassName .length != 2 ) {
1553
+ isSpecIncorrect = true ;
1554
+ }
1555
+ break ;
1556
+ case "ModuleAndPackage" :
1557
+ String [] moduleAndPackage = value .split ("/" );
1558
+ if (moduleAndPackage .length != 2 ) {
1559
+ isSpecIncorrect = true ;
1560
+ }
1561
+ break ;
1562
+ case "FullClassName" :
1563
+ case "Package" :
1564
+ // Nothing further to check in those options.
1565
+ break ;
1566
+ default :
1567
+ isSpecIncorrect = true ;
1568
+ break ;
1569
+ }
1570
+ if (isSpecIncorrect ) {
1571
+ printStackTraceAndExit ("Incorrect specification of accepted uses in constraint for "
1572
+ + inType + ", " + inAlgorithm + ": " + inAcceptedUses );
1573
+ }
1556
1574
}
1557
1575
1558
1576
// Each attribute must includes 2 fields (key and value) or *.
@@ -1913,9 +1931,15 @@ private static final class Constraint {
1913
1931
1914
1932
@ Override
1915
1933
public String toString () {
1916
- String constraintInfo = type + ", " + algorithm + ", " + attributes ;
1917
- constraintInfo = (acceptedUses != null ) ? constraintInfo + acceptedUses : constraintInfo ;
1918
- return "{" + constraintInfo + "}" ;
1934
+ StringBuilder buffer = new StringBuilder ();
1935
+ buffer .append ("{" ).append (type );
1936
+ buffer .append (", " ).append (algorithm );
1937
+ buffer .append (", " ).append (attributes );
1938
+ if (acceptedUses != null ) {
1939
+ buffer .append (", " ).append (acceptedUses );
1940
+ }
1941
+ buffer .append ("}" );
1942
+ return buffer .toString ();
1919
1943
}
1920
1944
1921
1945
@ Override
0 commit comments