-
Notifications
You must be signed in to change notification settings - Fork 71
Support better algorithms for password hashing and add encryption with salt #7111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
stweil
wants to merge
11
commits into
kitodo:main
Choose a base branch
from
stweil:weak_password_hashing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f5e0a55
Fix Codacy issues: weak password hashing
stweil cb00594
Document why LM and NTLM hashes are retained in LdapUser
stweil 1904baf
Restore configurable password encryption algorithm with salt
stweil 4cf1445
Centralize LDAP password prefix in PasswordEncryption enum
stweil ed5f479
Add unit tests for PasswordEncryption enum and LdapUser password hashing
stweil acebeb7
Fix style issues
stweil a8cad84
Fix more style issues
stweil b5efb0c
Add missing LF at EOF
stweil 0b7e2bf
Add adaptive password hashing using bcrypt
stweil ef6da93
Add SCRYPT and PBKDF2 adaptive password hashing
stweil e1accbc
Fix checkstyle issues in AdaptivePasswordEncoder
stweil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...o-DataManagement/src/test/java/org/kitodo/data/database/enums/PasswordEncryptionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * (c) Kitodo. Key to digital objects e. V. <contact@kitodo.org> | ||
| * | ||
| * This file is part of the Kitodo project. | ||
| * | ||
| * It is licensed under GNU General Public License version 3 or later. | ||
| * | ||
| * For the full copyright and license information, please read the | ||
| * GPL3-License.txt file that was distributed with this source code. | ||
| */ | ||
|
|
||
| package org.kitodo.data.database.enums; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class PasswordEncryptionTest { | ||
|
|
||
| @Test | ||
| public void shaHasCorrectValues() { | ||
| assertEquals(0, PasswordEncryption.SHA.getValue()); | ||
| assertEquals("SHA", PasswordEncryption.SHA.getTitle()); | ||
| assertEquals("{SSHA}", PasswordEncryption.SHA.getLdapPrefix()); | ||
| } | ||
|
|
||
| @Test | ||
| public void md5HasCorrectValues() { | ||
| assertEquals(1, PasswordEncryption.MD5.getValue()); | ||
| assertEquals("MD5", PasswordEncryption.MD5.getTitle()); | ||
| assertEquals("{SMD5}", PasswordEncryption.MD5.getLdapPrefix()); | ||
| } | ||
|
|
||
| @Test | ||
| public void sha256HasCorrectValues() { | ||
| assertEquals(2, PasswordEncryption.SHA_256.getValue()); | ||
| assertEquals("SHA-256", PasswordEncryption.SHA_256.getTitle()); | ||
| assertEquals("{SSHA-256}", PasswordEncryption.SHA_256.getLdapPrefix()); | ||
| } | ||
|
|
||
| @Test | ||
| public void getEncryptionFromValueReturnsCorrectEnum() { | ||
| assertEquals(PasswordEncryption.SHA, PasswordEncryption.getEncryptionFromValue(0)); | ||
| assertEquals(PasswordEncryption.MD5, PasswordEncryption.getEncryptionFromValue(1)); | ||
| assertEquals(PasswordEncryption.SHA_256, PasswordEncryption.getEncryptionFromValue(2)); | ||
| } | ||
|
|
||
| @Test | ||
| public void getEncryptionFromValueReturnsDefaultForNull() { | ||
| assertEquals(PasswordEncryption.SHA, PasswordEncryption.getEncryptionFromValue(null)); | ||
| } | ||
|
|
||
| @Test | ||
| public void getEncryptionFromValueReturnsDefaultForUnknownValue() { | ||
| assertEquals(PasswordEncryption.SHA, PasswordEncryption.getEncryptionFromValue(99)); | ||
| } | ||
|
|
||
| @Test | ||
| public void allEnumValuesAreUnique() { | ||
| Set<Integer> seen = new HashSet<>(); | ||
| for (PasswordEncryption pe : PasswordEncryption.values()) { | ||
| assertTrue(seen.add(pe.getValue()), "Duplicate getValue(): " + pe.getValue()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
Kitodo/src/main/java/org/kitodo/production/security/password/AdaptivePasswordEncoder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * (c) Kitodo. Key to digital objects e. V. <contact@kitodo.org> | ||
| * | ||
| * This file is part of the Kitodo project. | ||
| * | ||
| * It is licensed under GNU General Public License version 3 or later. | ||
| * | ||
| * For the full copyright and license information, please read the | ||
| * GPL3-License.txt file that was distributed with this source code. | ||
| */ | ||
|
|
||
| package org.kitodo.production.security.password; | ||
|
|
||
| import java.security.SecureRandom; | ||
| import java.security.spec.KeySpec; | ||
|
|
||
| import javax.crypto.SecretKeyFactory; | ||
| import javax.crypto.spec.PBEKeySpec; | ||
|
|
||
| import org.apache.commons.codec.binary.Base64; | ||
| import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
| import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder; | ||
|
|
||
| public class AdaptivePasswordEncoder { | ||
|
|
||
| private static final BCryptPasswordEncoder BCRYPT_ENCODER = new BCryptPasswordEncoder(16); | ||
| private static final SCryptPasswordEncoder SCRYPT_ENCODER = new SCryptPasswordEncoder(16, 8, 1, 32, 16); | ||
| private static final int PBKDF2_ITERATIONS = 185000; | ||
| private static final int SALT_LENGTH = 16; | ||
|
|
||
| public String hashBcrypt(String rawPassword) { | ||
| return BCRYPT_ENCODER.encode(rawPassword); | ||
| } | ||
|
|
||
| public boolean matchesBcrypt(String rawPassword, String encodedPassword) { | ||
| return BCRYPT_ENCODER.matches(rawPassword, encodedPassword); | ||
| } | ||
|
|
||
| public String hashScrypt(String rawPassword) { | ||
| return SCRYPT_ENCODER.encode(rawPassword); | ||
| } | ||
|
|
||
| public boolean matchesScrypt(String rawPassword, String encodedPassword) { | ||
| return SCRYPT_ENCODER.matches(rawPassword, encodedPassword); | ||
| } | ||
|
|
||
| public String hashPbkdf2(String rawPassword) { | ||
| try { | ||
| SecureRandom random = new SecureRandom(); | ||
| byte[] salt = new byte[SALT_LENGTH]; | ||
| random.nextBytes(salt); | ||
| KeySpec spec = new PBEKeySpec(rawPassword.toCharArray(), salt, PBKDF2_ITERATIONS, 256); | ||
| SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); | ||
| byte[] hash = factory.generateSecret(spec).getEncoded(); | ||
| byte[] hashAndSalt = new byte[hash.length + salt.length]; | ||
| System.arraycopy(hash, 0, hashAndSalt, 0, hash.length); | ||
| System.arraycopy(salt, 0, hashAndSalt, hash.length, salt.length); | ||
| return Base64.encodeBase64String(hashAndSalt); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("PBKDF2 hashing failed", e); | ||
| } | ||
| } | ||
|
|
||
| public boolean matchesPbkdf2(String rawPassword, String encodedPassword) { | ||
| try { | ||
| byte[] decoded = Base64.decodeBase64(encodedPassword); | ||
| byte[] storedHash = new byte[32]; | ||
| byte[] storedSalt = new byte[SALT_LENGTH]; | ||
| System.arraycopy(decoded, 0, storedHash, 0, 32); | ||
| System.arraycopy(decoded, 32, storedSalt, 0, SALT_LENGTH); | ||
| KeySpec spec = new PBEKeySpec(rawPassword.toCharArray(), storedSalt, PBKDF2_ITERATIONS, 256); | ||
| SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); | ||
| byte[] computedHash = factory.generateSecret(spec).getEncoded(); | ||
| byte[] computedHashAndSalt = new byte[computedHash.length + storedSalt.length]; | ||
| System.arraycopy(computedHash, 0, computedHashAndSalt, 0, computedHash.length); | ||
| System.arraycopy(storedSalt, 0, computedHashAndSalt, computedHash.length, storedSalt.length); | ||
| return Base64.encodeBase64String(computedHashAndSalt).equals(encodedPassword); | ||
| } catch (Exception e) { | ||
| return false; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If i understand correctly, the long term goal is to remove write access to the LDAP from Kitodo production, so enhancing password security would benefit an writable-LDAP implementation, which we eventually want to replace
#4646 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, this is an important information. So the question remains what "long term" means. Maybe removing write access can be done early to fix the issue?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#4646 has been assessed in multiple development fund rounds but has not been funded yet. Newer Debian/Ubuntu versions are not only moving to newer Samba versions but keep on deprecating related functionality in newer Kernels. So i do not know if this is already urgent or if Debian 13/Ubuntu 26 still support the current architecture.