Skip to content

Commit e6d0501

Browse files
committed
Improve deprecated-alg code
1 parent 1d79874 commit e6d0501

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

core/src/main/java/com/onelogin/saml2/logout/LogoutRequest.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -457,14 +457,9 @@ public Boolean isValid() throws Exception {
457457
signAlg = Constants.RSA_SHA1;
458458
}
459459

460-
if (signAlg.equals(Constants.RSA_SHA1)) {
461-
Boolean rejectDeprecatedAlg = settings.getRejectDeprecatedAlg();
462-
if (rejectDeprecatedAlg) {
463-
LOGGER.error("A deprecated algorithm (RSA_SHA1) found in the Signature element, rejecting it");
464-
return false;
465-
} else {
466-
LOGGER.info("RSA_SHA1 alg found in a Signature element, consider request a more robust alg");
467-
}
460+
Boolean rejectDeprecatedAlg = settings.getRejectDeprecatedAlg();
461+
if (Util.mustRejectDeprecatedSignatureAlgo(signAlg, rejectDeprecatedAlg)) {
462+
return false;
468463
}
469464

470465
String relayState = request.getEncodedParameter("RelayState");

core/src/main/java/com/onelogin/saml2/logout/LogoutResponse.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,9 @@ public Boolean isValid(String requestId) {
258258
signAlg = Constants.RSA_SHA1;
259259
}
260260

261-
if (signAlg.equals(Constants.RSA_SHA1)) {
262-
Boolean rejectDeprecatedAlg = settings.getRejectDeprecatedAlg();
263-
if (rejectDeprecatedAlg) {
264-
LOGGER.error("A deprecated algorithm (RSA_SHA1) found in the Signature element, rejecting it");
265-
return false;
266-
} else {
267-
LOGGER.info("RSA_SHA1 alg found in a Signature element, consider request a more robust alg");
268-
}
261+
Boolean rejectDeprecatedAlg = settings.getRejectDeprecatedAlg();
262+
if (Util.mustRejectDeprecatedSignatureAlgo(signAlg, rejectDeprecatedAlg)) {
263+
return false;
269264
}
270265

271266
String signedQuery = "SAMLResponse=" + request.getEncodedParameter("SAMLResponse");

core/src/main/java/com/onelogin/saml2/util/Util.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.security.cert.CertificateFactory;
2828
import java.security.cert.X509Certificate;
2929
import java.security.spec.PKCS8EncodedKeySpec;
30+
import java.util.Arrays;
3031
import java.util.Calendar;
3132
import java.util.HashMap;
3233
import java.util.HashSet;
@@ -116,6 +117,8 @@ public final class Util {
116117
/** Indicates if JAXP 1.5 support has been detected. */
117118
private static boolean JAXP_15_SUPPORTED = isJaxp15Supported();
118119

120+
private static final Set<String> DEPRECATED_ALGOS = new HashSet<>(Arrays.asList(Constants.RSA_SHA1, Constants.DSA_SHA1));
121+
119122
static {
120123
System.setProperty("org.apache.xml.security.ignoreLineBreaks", "true");
121124
org.apache.xml.security.Init.init();
@@ -1093,13 +1096,8 @@ private static Map<String,Object> getSignatureData(Node signNode, String alg, Bo
10931096
throw new Exception(sigMethodAlg + " is not a valid supported algorithm");
10941097
}
10951098

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-
}
1099+
if (Util.mustRejectDeprecatedSignatureAlgo(sigMethodAlg, rejectDeprecatedAlg)) {
1100+
return signatureData;
11031101
}
11041102

11051103
signatureData.put("signature", signature);
@@ -1122,6 +1120,19 @@ private static Map<String,Object> getSignatureData(Node signNode, String alg, Bo
11221120
return signatureData;
11231121
}
11241122

1123+
public static Boolean mustRejectDeprecatedSignatureAlgo(String signAlg, Boolean rejectDeprecatedAlg) {
1124+
if (DEPRECATED_ALGOS.contains(signAlg)) {
1125+
String errorMsg = "Found a deprecated algorithm "+ signAlg +" related to the Signature element,";
1126+
if (rejectDeprecatedAlg) {
1127+
LOGGER.error(errorMsg + " rejecting it");
1128+
return true;
1129+
} else {
1130+
LOGGER.info(errorMsg + " consider requesting a more robust algorithm");
1131+
}
1132+
}
1133+
return false;
1134+
}
1135+
11251136
/**
11261137
* Validate signature of the Node.
11271138
*

0 commit comments

Comments
 (0)