Skip to content

Commit f6185da

Browse files
committed
OPENAM-6768 FR-880
Based on: commit a6671bb Author: Jon Jonthomas <[email protected]> Date: Tue Oct 6 11:33:29 2015 +0000
1 parent 1440d0c commit f6185da

File tree

1 file changed

+24
-6
lines changed
  • openam-authentication/openam-auth-oath/src/main/java/org/forgerock/openam/authentication/modules/oath

1 file changed

+24
-6
lines changed

openam-authentication/openam-auth-oath/src/main/java/org/forgerock/openam/authentication/modules/oath/OATH.java

+24-6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import java.util.HashMap;
5353
import java.util.Collections;
5454
import java.util.ResourceBundle;
55+
import java.security.MessageDigest;
5556
import javax.security.auth.Subject;
5657
import javax.security.auth.callback.Callback;
5758
import javax.security.auth.callback.PasswordCallback;
@@ -516,7 +517,7 @@ private boolean checkOTP(String otp) throws AuthLoginException {
516517
passLen,
517518
checksum,
518519
truncationOffset);
519-
if (otpGen.equals(otp)) {
520+
if (isEqual(otpGen, otp)) {
520521
//OTP is correct set the counter value to counter+i
521522
setCounterAttr(id, counter + i);
522523
return true;
@@ -584,6 +585,11 @@ private boolean checkOTP(String otp) throws AuthLoginException {
584585
long localTime = time;
585586
localTime /= totpTimeStep;
586587

588+
if(lastLoginTime == localTime){
589+
debug.error("OATH.checkOTP(): Login failed attempting to use the same OTP in same Time Step: " + localTime);
590+
throw new InvalidPasswordException(amAuthOATH, "authFailed", null, userName, null);
591+
}
592+
587593
boolean sameWindow = false;
588594

589595
//check if we are in the time window to prevent 2
@@ -603,7 +609,7 @@ private boolean checkOTP(String otp) throws AuthLoginException {
603609
otpGen = TOTPAlgorithm.generateTOTP(secretKey,
604610
Long.toHexString(localTime),
605611
passLenStr);
606-
if (otpGen.equals(otp)) {
612+
if (isEqual(otpGen, otp)) {
607613
setLoginTime(id, localTime);
608614
return true;
609615
}
@@ -616,7 +622,7 @@ private boolean checkOTP(String otp) throws AuthLoginException {
616622
otpGen = TOTPAlgorithm.generateTOTP(secretKey,
617623
Long.toHexString(time1),
618624
passLenStr);
619-
if (otpGen.equals(otp)) {
625+
if (isEqual(otpGen, otp)) {
620626
setLoginTime(id, time1);
621627
return true;
622628
}
@@ -626,12 +632,12 @@ private boolean checkOTP(String otp) throws AuthLoginException {
626632
otpGen = TOTPAlgorithm.generateTOTP(secretKey,
627633
Long.toHexString(time2),
628634
passLenStr);
629-
if (otpGen.equals(otp) && sameWindow){
635+
if (isEqual(otpGen, otp) && sameWindow) {
630636
debug.error("OATH" +
631637
".checkOTP() : " +
632-
"Loging in in the same window with a OTP that is older than the current times OTP");
638+
"Login the same window with a OTP that is older than the current OTP");
633639
return false;
634-
} else if(otpGen.equals(otp) && !sameWindow) {
640+
} else if (isEqual(otpGen, otp) && !sameWindow) {
635641
setLoginTime(id, time2);
636642
return true;
637643
}
@@ -769,4 +775,16 @@ private void setLoginTime(AMIdentity id, long time)
769775
}
770776
return;
771777
}
778+
779+
/**
780+
* Perform time constant equality check.
781+
* Both values should not be null.
782+
*
783+
* @param str1 first value
784+
* @param str2 second vale
785+
* @return true if values are equal
786+
*/
787+
private boolean isEqual(String str1, String str2) {
788+
return MessageDigest.isEqual(str1.getBytes(StandardCharsets.UTF_8), str2.getBytes(StandardCharsets.UTF_8));
789+
}
772790
}

0 commit comments

Comments
 (0)