2828import java .security .cert .X509Certificate ;
2929import java .security .spec .PKCS8EncodedKeySpec ;
3030import java .util .Calendar ;
31+ import java .util .HashSet ;
3132import java .util .Iterator ;
3233import java .util .List ;
3334import java .util .Locale ;
35+ import java .util .Set ;
3436import java .util .TimeZone ;
3537import java .util .UUID ;
3638import java .util .zip .Deflater ;
@@ -967,6 +969,11 @@ public static Boolean validateSignNode(Node signNode, X509Certificate cert, Stri
967969 Element sigElement = (Element ) signNode ;
968970 XMLSignature signature = new XMLSignature (sigElement , "" , true );
969971
972+ String sigMethodAlg = signature .getSignedInfo ().getSignatureMethodURI ();
973+ if (!isAlgorithmWhitelisted (sigMethodAlg )){
974+ throw new Exception (sigMethodAlg + " is not a valid supported algorithm" );
975+ }
976+
970977 if (cert != null ) {
971978 res = signature .checkSignatureValue (cert );
972979 } else {
@@ -987,6 +994,36 @@ public static Boolean validateSignNode(Node signNode, X509Certificate cert, Stri
987994 return res ;
988995 }
989996
997+ /**
998+ * Whitelist the XMLSignature algorithm
999+ *
1000+ * @param signNode
1001+ * The document we should validate
1002+ * @param cert
1003+ * The public certificate
1004+ * @param fingerprint
1005+ * The fingerprint of the public certificate
1006+ * @param alg
1007+ * The signature algorithm method
1008+ *
1009+ * @return True if the sign is valid, false otherwise.
1010+ */
1011+ public static boolean isAlgorithmWhitelisted (String alg ) {
1012+ Set <String > whiteListedAlgorithm = new HashSet <String >();
1013+ whiteListedAlgorithm .add (Constants .DSA_SHA1 );
1014+ whiteListedAlgorithm .add (Constants .RSA_SHA1 );
1015+ whiteListedAlgorithm .add (Constants .RSA_SHA256 );
1016+ whiteListedAlgorithm .add (Constants .RSA_SHA384 );
1017+ whiteListedAlgorithm .add (Constants .RSA_SHA512 );
1018+
1019+ Boolean whitelisted = false ;
1020+ if (whiteListedAlgorithm .contains (alg )) {
1021+ whitelisted = true ;
1022+ }
1023+
1024+ return whitelisted ;
1025+ }
1026+
9901027 /**
9911028 * Decrypt an encrypted element.
9921029 *
0 commit comments