@@ -923,13 +923,30 @@ public static boolean validateSign(final Document doc, final X509Certificate cer
923923 */
924924 public static boolean validateSign (final Document doc , final List <X509Certificate > certList , final String fingerprint ,
925925 final String alg , final String xpath ) {
926+ return validateSign (doc , certList , fingerprint ,alg , xpath , false );
927+ }
928+
929+ /**
930+ * Validate the signature pointed to by the xpath
931+ *
932+ * @param doc The document we should validate
933+ * @param certList The public certificates
934+ * @param fingerprint The fingerprint of the public certificate
935+ * @param alg The signature algorithm method
936+ * @param xpath the xpath of the ds:Signture node to validate
937+ * @param rejectDeprecatedAlg Flag to invalidate or not Signatures with deprecated alg
938+ *
939+ * @return True if the signature exists and is valid, false otherwise.
940+ */
941+ public static boolean validateSign (final Document doc , final List <X509Certificate > certList , final String fingerprint ,
942+ final String alg , final String xpath , final Boolean rejectDeprecatedAlg ) {
926943 try {
927944 final NodeList signatures = query (doc , xpath );
928945
929946 if (signatures .getLength () == 1 ) {
930947 final Node signNode = signatures .item (0 );
931948
932- Map <String ,Object > signatureData = getSignatureData (signNode , alg );
949+ Map <String ,Object > signatureData = getSignatureData (signNode , alg , rejectDeprecatedAlg );
933950 if (signatureData .isEmpty ()) {
934951 return false ;
935952 }
@@ -984,6 +1001,26 @@ public static boolean validateSign(final Document doc, final List<X509Certificat
9841001 * @return True if the sign is valid, false otherwise.
9851002 */
9861003 public static Boolean validateMetadataSign (Document doc , X509Certificate cert , String fingerprint , String alg ) {
1004+ return validateMetadataSign (doc , cert , fingerprint , alg , false );
1005+ }
1006+
1007+ /**
1008+ * Validate signature (Metadata).
1009+ *
1010+ * @param doc
1011+ * The document we should validate
1012+ * @param cert
1013+ * The public certificate
1014+ * @param fingerprint
1015+ * The fingerprint of the public certificate
1016+ * @param alg
1017+ * The signature algorithm method
1018+ * @param rejectDeprecatedAlg
1019+ * Flag to invalidate or not Signatures with deprecated alg
1020+ *
1021+ * @return True if the sign is valid, false otherwise.
1022+ */
1023+ public static Boolean validateMetadataSign (Document doc , X509Certificate cert , String fingerprint , String alg , Boolean rejectDeprecatedAlg ) {
9871024 NodeList signNodesToValidate ;
9881025 try {
9891026 signNodesToValidate = query (doc , "/md:EntitiesDescriptor/ds:Signature" );
@@ -999,7 +1036,7 @@ public static Boolean validateMetadataSign(Document doc, X509Certificate cert, S
9991036 if (signNodesToValidate .getLength () > 0 ) {
10001037 for (int i = 0 ; i < signNodesToValidate .getLength (); i ++) {
10011038 Node signNode = signNodesToValidate .item (i );
1002- if (!validateSignNode (signNode , cert , fingerprint , alg )) {
1039+ if (!validateSignNode (signNode , cert , fingerprint , alg , rejectDeprecatedAlg )) {
10031040 return false ;
10041041 }
10051042 }
@@ -1026,6 +1063,26 @@ public static Boolean validateMetadataSign(Document doc, X509Certificate cert, S
10261063 * @return True if the sign is valid, false otherwise.
10271064 */
10281065 private static Map <String ,Object > getSignatureData (Node signNode , String alg ) {
1066+ return getSignatureData (signNode , alg , false );
1067+ }
1068+
1069+ /**
1070+ * Validate signature (Metadata).
1071+ *
1072+ * @param doc
1073+ * The document we should validate
1074+ * @param cert
1075+ * The public certificate
1076+ * @param fingerprint
1077+ * The fingerprint of the public certificate
1078+ * @param alg
1079+ * The signature algorithm method
1080+ * @param rejectDeprecatedAlg
1081+ * Flag to invalidate or not Signatures with deprecated alg
1082+ *
1083+ * @return True if the sign is valid, false otherwise.
1084+ */
1085+ private static Map <String ,Object > getSignatureData (Node signNode , String alg , Boolean rejectDeprecatedAlg ) {
10291086 Map <String ,Object > signatureData = new HashMap <>();
10301087 try {
10311088 Element sigElement = (Element ) signNode ;
@@ -1036,6 +1093,15 @@ private static Map<String,Object> getSignatureData(Node signNode, String alg) {
10361093 throw new Exception (sigMethodAlg + " is not a valid supported algorithm" );
10371094 }
10381095
1096+ if (sigMethodAlg .equals (Constants .RSA_SHA1 )) {
1097+ if (rejectDeprecatedAlg ) {
1098+ LOGGER .error ("A deprecated algorithm (RSA_SHA1) found in the Signature element, rejecting it" );
1099+ return signatureData ;
1100+ } else {
1101+ LOGGER .info ("RSA_SHA1 alg found in a Signature element, consider request a more robust alg" );
1102+ }
1103+ }
1104+
10391105 signatureData .put ("signature" , signature );
10401106
10411107 String extractedFingerprint = null ;
@@ -1073,7 +1139,29 @@ private static Map<String,Object> getSignatureData(Node signNode, String alg) {
10731139 * @throws Exception
10741140 */
10751141 public static Boolean validateSignNode (Node signNode , X509Certificate cert , String fingerprint , String alg ) {
1076- Map <String ,Object > signatureData = getSignatureData (signNode , alg );
1142+ return validateSignNode (signNode , cert , fingerprint , alg , false );
1143+ }
1144+
1145+ /**
1146+ * Validate signature of the Node.
1147+ *
1148+ * @param signNode
1149+ * The document we should validate
1150+ * @param cert
1151+ * The public certificate
1152+ * @param fingerprint
1153+ * The fingerprint of the public certificate
1154+ * @param alg
1155+ * The signature algorithm method
1156+ * @param rejectDeprecatedAlg
1157+ * Flag to invalidate or not Signatures with deprecated alg
1158+ *
1159+ * @return True if the sign is valid, false otherwise.
1160+ *
1161+ * @throws Exception
1162+ */
1163+ public static Boolean validateSignNode (Node signNode , X509Certificate cert , String fingerprint , String alg , Boolean rejectDeprecatedAlg ) {
1164+ Map <String ,Object > signatureData = getSignatureData (signNode , alg , rejectDeprecatedAlg );
10771165 if (signatureData .isEmpty ()) {
10781166 return false ;
10791167 }
0 commit comments