28
28
import java .security .MessageDigest ;
29
29
import java .security .NoSuchAlgorithmException ;
30
30
import java .security .PrivilegedAction ;
31
+ import java .security .Provider ;
31
32
import java .security .Provider .Service ;
32
33
import java .time .LocalDate ;
33
34
import java .time .format .DateTimeFormatter ;
@@ -267,6 +268,12 @@ public static boolean isServiceAllowed(Service service) {
267
268
*/
268
269
public static boolean isProviderAllowed (String providerName ) {
269
270
if (securityEnabled ) {
271
+ // Remove argument, e.g. -NSS-FIPS, if present.
272
+ int pos = providerName .indexOf ('-' );
273
+ if (pos >= 0 ) {
274
+ providerName = providerName .substring (0 , pos );
275
+ }
276
+
270
277
return restricts .isRestrictedProviderAllowed (providerName );
271
278
}
272
279
return true ;
@@ -280,17 +287,17 @@ public static boolean isProviderAllowed(String providerName) {
280
287
*/
281
288
public static boolean isProviderAllowed (Class <?> providerClazz ) {
282
289
if (securityEnabled ) {
283
- String providerName = providerClazz .getName ();
290
+ String providerClassName = providerClazz .getName ();
284
291
285
292
// Check if the specified class extends java.security.Provider.
286
293
if (java .security .Provider .class .isAssignableFrom (providerClazz )) {
287
- return restricts .isRestrictedProviderAllowed (providerName );
294
+ return restricts .isRestrictedProviderAllowed (providerClassName );
288
295
}
289
296
290
297
// For a class that doesn't extend java.security.Provider, no need to
291
298
// check allowed or not allowed, always return true to load it.
292
299
if (debug != null ) {
293
- debug .println ("The provider class " + providerName + " does not extend java.security.Provider." );
300
+ debug .println ("The provider class " + providerClassName + " does not extend java.security.Provider." );
294
301
}
295
302
}
296
303
return true ;
@@ -659,27 +666,6 @@ private static boolean isAsterisk(String string) {
659
666
return "*" .equals (string );
660
667
}
661
668
662
- /**
663
- * Get the provider name defined in provider construction method.
664
- *
665
- * @param providerName provider name or provider with packages
666
- * @return provider name defined in provider construction method
667
- */
668
- private static String getProvidersSimpleName (String providerName ) {
669
- if (providerName .equals ("com.sun.security.sasl.Provider" )) {
670
- // The main class for the SunSASL provider is com.sun.security.sasl.Provider.
671
- return "SunSASL" ;
672
- } else {
673
- // Remove the provider's class package names if present.
674
- int pos = providerName .lastIndexOf ('.' );
675
- if (pos >= 0 ) {
676
- providerName = providerName .substring (pos + 1 );
677
- }
678
- // Provider without package names.
679
- return providerName ;
680
- }
681
- }
682
-
683
669
/**
684
670
* This class is used to save and operate on restricted security
685
671
* properties which are loaded from the java.security file.
@@ -713,7 +699,7 @@ private static final class RestrictedSecurityProperties {
713
699
// Provider with argument (provider name + optional argument).
714
700
private final List <String > providers ;
715
701
// Provider without argument.
716
- private final List <String > providersSimpleName ;
702
+ private final List <String > providersFullyQualifiedClassName ;
717
703
// The map is keyed by provider name.
718
704
private final Map <String , Constraint []> providerConstraints ;
719
705
@@ -745,7 +731,7 @@ private RestrictedSecurityProperties(String profileID, ProfileParser parser) {
745
731
this .jdkFipsMode = parser .getProperty ("jdkFipsMode" );
746
732
747
733
this .providers = new ArrayList <>(parser .providers );
748
- this .providersSimpleName = new ArrayList <>(parser .providersSimpleName );
734
+ this .providersFullyQualifiedClassName = new ArrayList <>(parser .providersFullyQualifiedClassName );
749
735
this .providerConstraints = parser .providerConstraints
750
736
.entrySet ()
751
737
.stream ()
@@ -767,30 +753,26 @@ private RestrictedSecurityProperties(String profileID, ProfileParser parser) {
767
753
* @return true if the Service is allowed
768
754
*/
769
755
boolean isRestrictedServiceAllowed (Service service ) {
770
- String providerName = service .getProvider ().getName ();
756
+ Provider provider = service .getProvider ();
757
+ String providerClassName = provider .getClass ().getName ();
771
758
772
759
if (debug != null ) {
773
- debug .println ("Checking service " + service .toString () + " offered by provider " + providerName + "." );
760
+ debug .println ("Checking service " + service .toString () + " offered by provider " + providerClassName + "." );
774
761
}
775
762
776
- // Provider with argument, remove argument.
777
- // e.g. SunPKCS11-NSS-FIPS, remove argument -NSS-FIPS.
778
- int pos = providerName .indexOf ('-' );
779
- providerName = (pos < 0 ) ? providerName : providerName .substring (0 , pos );
780
-
781
- Constraint [] constraints = providerConstraints .get (providerName );
763
+ Constraint [] constraints = providerConstraints .get (providerClassName );
782
764
783
765
if (constraints == null ) {
784
766
// Disallow unknown providers.
785
767
if (debug != null ) {
786
768
debug .println ("Security constraints check."
787
- + " Disallow unknown provider: " + providerName );
769
+ + " Disallow unknown provider: " + providerClassName );
788
770
}
789
771
return false ;
790
772
} else if (constraints .length == 0 ) {
791
773
// Allow this provider with no constraints.
792
774
if (debug != null ) {
793
- debug .println ("No constraints for provider " + providerName + "." );
775
+ debug .println ("No constraints for provider " + providerClassName + "." );
794
776
}
795
777
return true ;
796
778
}
@@ -834,7 +816,7 @@ boolean isRestrictedServiceAllowed(Service service) {
834
816
debug .println ("The following service:"
835
817
+ "\n \t Service type: " + type
836
818
+ "\n \t Algorithm: " + algorithm
837
- + "\n is allowed in provider: " + providerName );
819
+ + "\n is allowed in provider: " + providerClassName );
838
820
}
839
821
return true ;
840
822
}
@@ -864,7 +846,7 @@ boolean isRestrictedServiceAllowed(Service service) {
864
846
+ "\n \t Service type: " + type
865
847
+ "\n \t Algorithm: " + algorithm
866
848
+ "\n \t Attribute: " + cAttribute
867
- + "\n is NOT allowed in provider: " + providerName );
849
+ + "\n is NOT allowed in provider: " + providerClassName );
868
850
}
869
851
return false ;
870
852
}
@@ -878,7 +860,7 @@ boolean isRestrictedServiceAllowed(Service service) {
878
860
+ "\n \t Service type: " + type
879
861
+ "\n \t Algorithm: " + algorithm
880
862
+ "\n \t Attribute: " + cAttribute
881
- + "\n is allowed in provider: " + providerName );
863
+ + "\n is allowed in provider: " + providerClassName );
882
864
}
883
865
return true ;
884
866
}
@@ -889,42 +871,33 @@ boolean isRestrictedServiceAllowed(Service service) {
889
871
debug .println ("The following service:"
890
872
+ "\n \t Service type: " + type
891
873
+ "\n \t Algorithm: " + algorithm
892
- + "\n is NOT allowed in provider: " + providerName );
874
+ + "\n is NOT allowed in provider: " + providerClassName );
893
875
}
894
876
return false ;
895
877
}
896
878
897
879
/**
898
880
* Check if the provider is allowed in restricted security mode.
899
881
*
900
- * @param providerName the provider to check
882
+ * @param providerClassName the provider to check
901
883
* @return true if the provider is allowed
902
884
*/
903
- boolean isRestrictedProviderAllowed (String providerName ) {
885
+ boolean isRestrictedProviderAllowed (String providerClassName ) {
904
886
if (debug != null ) {
905
- debug .println ("Checking the provider " + providerName + " in restricted security mode." );
906
- }
907
-
908
- // Remove argument, e.g. -NSS-FIPS, if present.
909
- int pos = providerName .indexOf ('-' );
910
- if (pos >= 0 ) {
911
- providerName = providerName .substring (0 , pos );
887
+ debug .println ("Checking the provider " + providerClassName + " in restricted security mode." );
912
888
}
913
889
914
- // Provider name defined in provider construction method.
915
- providerName = getProvidersSimpleName (providerName );
916
-
917
- // Check if the provider is in restricted security provider list.
918
- // If not, the provider won't be registered.
919
- if (providersSimpleName .contains (providerName )) {
890
+ // Check if the provider fully-qualified cLass name is in restricted
891
+ // security provider list. If not, the provider won't be registered.
892
+ if (providersFullyQualifiedClassName .contains (providerClassName )) {
920
893
if (debug != null ) {
921
- debug .println ("The provider " + providerName + " is allowed in restricted security mode." );
894
+ debug .println ("The provider " + providerClassName + " is allowed in restricted security mode." );
922
895
}
923
896
return true ;
924
897
}
925
898
926
899
if (debug != null ) {
927
- debug .println ("The provider " + providerName + " is not allowed in restricted security mode." );
900
+ debug .println ("The provider " + providerClassName + " is not allowed in restricted security mode." );
928
901
929
902
debug .println ("Stack trace:" );
930
903
StackTraceElement [] elements = Thread .currentThread ().getStackTrace ();
@@ -963,8 +936,8 @@ private void listUsedProfile() {
963
936
for (int providerPosition = 0 ; providerPosition < providers .size (); providerPosition ++) {
964
937
printProperty (profileID + ".jce.provider." + (providerPosition + 1 ) + ": " ,
965
938
providers .get (providerPosition ));
966
- String providerSimpleName = providersSimpleName .get (providerPosition );
967
- for (Constraint providerConstraint : providerConstraints .get (providerSimpleName )) {
939
+ String providerFullyQualifiedClassName = providersFullyQualifiedClassName .get (providerPosition );
940
+ for (Constraint providerConstraint : providerConstraints .get (providerFullyQualifiedClassName )) {
968
941
System .out .println ("\t " + providerConstraint .toString ());
969
942
}
970
943
}
@@ -1007,7 +980,7 @@ private static final class ProfileParser {
1007
980
// Provider with argument (provider name + optional argument).
1008
981
private final List <String > providers ;
1009
982
// Provider without argument.
1010
- private final List <String > providersSimpleName ;
983
+ private final List <String > providersFullyQualifiedClassName ;
1011
984
// The map is keyed by provider name.
1012
985
private final Map <String , List <Constraint >> providerConstraints ;
1013
986
@@ -1035,7 +1008,7 @@ private ProfileParser(String id, Properties props) {
1035
1008
profileProperties = new HashMap <>();
1036
1009
1037
1010
providers = new ArrayList <>();
1038
- providersSimpleName = new ArrayList <>();
1011
+ providersFullyQualifiedClassName = new ArrayList <>();
1039
1012
providerConstraints = new HashMap <>();
1040
1013
1041
1014
profilesHashes = new HashMap <>();
@@ -1193,21 +1166,13 @@ private void parseProvider(String providerInfo, int providerPos, boolean update)
1193
1166
}
1194
1167
providerName = providerName .trim ();
1195
1168
1196
- // Remove argument, e.g. -NSS-FIPS, if present.
1197
- pos = providerName .indexOf ('-' );
1198
- if (pos >= 0 ) {
1199
- providerName = providerName .substring (0 , pos );
1200
- }
1201
-
1202
- // Provider name defined in provider construction method.
1203
- providerName = getProvidersSimpleName (providerName );
1204
1169
boolean providerChanged = false ;
1205
1170
if (update ) {
1206
- String previousProviderName = providersSimpleName .get (providerPos - 1 );
1171
+ String previousProviderName = providersFullyQualifiedClassName .get (providerPos - 1 );
1207
1172
providerChanged = !previousProviderName .equals (providerName );
1208
- providersSimpleName .set (providerPos - 1 , providerName );
1173
+ providersFullyQualifiedClassName .set (providerPos - 1 , providerName );
1209
1174
} else {
1210
- providersSimpleName .add (providerPos - 1 , providerName );
1175
+ providersFullyQualifiedClassName .add (providerPos - 1 , providerName );
1211
1176
}
1212
1177
1213
1178
if (debug != null ) {
@@ -1223,14 +1188,14 @@ private void removeProvider(String profileExtensionId, int providerPos) {
1223
1188
debug .println ("\t \t Removing provider in position " + providerPos );
1224
1189
}
1225
1190
1226
- int numOfExistingProviders = providersSimpleName .size ();
1191
+ int numOfExistingProviders = providersFullyQualifiedClassName .size ();
1227
1192
1228
1193
// If this is the last provider, remove from all lists.
1229
1194
if (providerPos == numOfExistingProviders ) {
1230
1195
if (debug != null ) {
1231
1196
debug .println ("\t \t \t Last provider. Only one to be removed." );
1232
1197
}
1233
- String providerRemoved = providersSimpleName .remove (providerPos - 1 );
1198
+ String providerRemoved = providersFullyQualifiedClassName .remove (providerPos - 1 );
1234
1199
providers .remove (providerPos - 1 );
1235
1200
providerConstraints .remove (providerRemoved );
1236
1201
@@ -1254,7 +1219,7 @@ private void removeProvider(String profileExtensionId, int providerPos) {
1254
1219
}
1255
1220
1256
1221
// Remove all of the providers that are set to empty.
1257
- String providerRemoved = providersSimpleName .remove (i - 1 );
1222
+ String providerRemoved = providersFullyQualifiedClassName .remove (i - 1 );
1258
1223
providers .remove (i - 1 );
1259
1224
providerConstraints .remove (providerRemoved );
1260
1225
@@ -1303,7 +1268,7 @@ private void initProviders(String profileID, List<String> allInfo) {
1303
1268
1304
1269
private void updateProviders (String profileExtensionId , List <String > allInfo ) {
1305
1270
boolean removedProvider = false ;
1306
- int numOfExistingProviders = providersSimpleName .size ();
1271
+ int numOfExistingProviders = providersFullyQualifiedClassName .size ();
1307
1272
// Deal with update of existing providers.
1308
1273
for (int i = 1 ; i <= numOfExistingProviders ; i ++) {
1309
1274
String property = profileExtensionId + ".jce.provider." + i ;
0 commit comments