Add extra salt length check for BCrypt

If the salt length is 28 characters and the
version is 2{a,x,y}, an IndexOutOfBoundsException
is thrown. This commit adds an extra check that
the salt length should be at least 29 characters long
if the version is not equal to "2".

Fixes: gh-6907
This commit is contained in:
Léon van der Kaap 2019-06-08 23:28:07 +02:00 committed by Josh Cummings
parent 10a7af2d53
commit d2248d185b
No known key found for this signature in database
GPG Key ID: 49EF60DD7FF83443
2 changed files with 9 additions and 0 deletions

View File

@ -780,6 +780,10 @@ public class BCrypt {
// Extract number of rounds
if (salt.charAt(off + 2) > '$')
throw new IllegalArgumentException ("Missing salt rounds");
if (off == 4 && saltLength < 29) {
throw new IllegalArgumentException("Invalid salt");
}
rounds = Integer.parseInt(salt.substring(off, off + 2));
real_salt = salt.substring(off + 3, off + 25);

View File

@ -338,6 +338,11 @@ public class BCryptTests {
"$2$05$......................bvpG2UfzdyW/S0ny/4YyEZrmczoJfVm");
}
@Test(expected = IllegalArgumentException.class)
public void hashpwFailsWhenSaltIsTooShort() {
BCrypt.hashpw("password", "$2a$10$123456789012345678901");
}
@Test
public void equalsOnStringsIsCorrect() {
assertThat(BCrypt.equalsNoEarlyReturn("", "")).isTrue();