-
Notifications
You must be signed in to change notification settings - Fork 0
feat: implement JCE crypto adapter and harden registration infrastructure #19
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a3c39af
feat: make crypto parameters configurable in UserCreationService
Treszyk 14a0ea3
implement JCE crypto adapter with explicit memory management and tests
Treszyk b46f881
refactor: migrate to BouncyCastle for raw-byte PBKDF2 parity
Treszyk a643e91
feat: wire crypto adapter and domain services into Spring context
Treszyk 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
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
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
36 changes: 36 additions & 0 deletions
36
vaultonapi/src/main/java/dev/vaulton/vaultonapi/infrastructure/config/CryptoConfig.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,36 @@ | ||
| package dev.vaulton.vaultonapi.infrastructure.config; | ||
|
|
||
| import dev.vaulton.vaultonapi.domain.repository.UserRepository; | ||
| import dev.vaulton.vaultonapi.domain.service.shared.CryptoService; | ||
| import dev.vaulton.vaultonapi.domain.service.usercreation.UserCreationService; | ||
| import dev.vaulton.vaultonapi.domain.service.usercreation.UserCreationServiceImpl; | ||
| import dev.vaulton.vaultonapi.infrastructure.crypto.BouncyCryptoAdapter; | ||
| import java.security.SecureRandom; | ||
| import java.util.Base64; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| public class CryptoConfig { | ||
|
|
||
| @Bean | ||
| public CryptoService cryptoService( | ||
| @Value("${vaulton.auth.fake-salt-secret}") String fakeSaltB64) { | ||
| byte[] secret = Base64.getDecoder().decode(fakeSaltB64); | ||
|
|
||
| return new BouncyCryptoAdapter(new SecureRandom(), secret); | ||
| } | ||
|
|
||
| @Bean | ||
| public UserCreationService userCreationService( | ||
| CryptoService cryptoService, | ||
| UserRepository userRepository, | ||
| @Value("${vaulton.auth.pepper}") String pepperB64, | ||
| @Value("${vaulton.auth.iterations}") int iterations) { | ||
|
|
||
| byte[] pepper = Base64.getDecoder().decode(pepperB64); | ||
|
|
||
| return new UserCreationServiceImpl(iterations, pepper, cryptoService, userRepository); | ||
| } | ||
| } |
116 changes: 116 additions & 0 deletions
116
...onapi/src/main/java/dev/vaulton/vaultonapi/infrastructure/crypto/BouncyCryptoAdapter.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,116 @@ | ||
| package dev.vaulton.vaultonapi.infrastructure.crypto; | ||
|
|
||
| import dev.vaulton.vaultonapi.domain.crypto.CryptoConstants; | ||
| import dev.vaulton.vaultonapi.domain.crypto.SecureBuffer; | ||
| import dev.vaulton.vaultonapi.domain.service.shared.CryptoService; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.security.InvalidKeyException; | ||
| import java.security.NoSuchAlgorithmException; | ||
| import java.security.SecureRandom; | ||
| import java.util.UUID; | ||
| import javax.crypto.Mac; | ||
| import javax.crypto.spec.SecretKeySpec; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.bouncycastle.crypto.digests.SHA256Digest; | ||
| import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; | ||
| import org.bouncycastle.crypto.params.KeyParameter; | ||
|
|
||
| @RequiredArgsConstructor | ||
| public class BouncyCryptoAdapter implements CryptoService { | ||
| private final SecureRandom randGen; | ||
| private final byte[] fakeSaltSecret; | ||
|
|
||
| void zeroizeBuffer(byte[] buffer) { | ||
| if (buffer != null) java.util.Arrays.fill(buffer, (byte) 0x00); | ||
| } | ||
|
|
||
| @Override | ||
| public SecureBuffer computeStoredVerifier( | ||
| SecureBuffer verifierRaw, SecureBuffer salt, int iterations, byte[] pepper) { | ||
| PKCS5S2ParametersGenerator gen = null; | ||
| byte[] verifierBytes = null; | ||
| byte[] saltBytes = null; | ||
| byte[] pepperedVerifier = null; | ||
| byte[] hash = null; | ||
|
|
||
| try { | ||
| gen = new PKCS5S2ParametersGenerator(new SHA256Digest()); | ||
|
|
||
| verifierBytes = verifierRaw.bytes(); | ||
| saltBytes = salt.bytes(); | ||
| pepperedVerifier = new byte[verifierBytes.length + pepper.length]; | ||
| System.arraycopy(verifierBytes, 0, pepperedVerifier, 0, verifierBytes.length); | ||
| System.arraycopy(pepper, 0, pepperedVerifier, verifierBytes.length, pepper.length); | ||
|
|
||
| gen.init(pepperedVerifier, saltBytes, iterations); | ||
| KeyParameter keyParameter = (KeyParameter) gen.generateDerivedParameters(256); | ||
| hash = keyParameter.getKey(); | ||
|
|
||
| return new SecureBuffer(hash); | ||
| } finally { | ||
| zeroizeBuffer(pepperedVerifier); | ||
| zeroizeBuffer(verifierBytes); | ||
| zeroizeBuffer(saltBytes); | ||
| zeroizeBuffer(hash); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public SecureBuffer generateRandomBytes(int length) { | ||
| byte[] randBytes = null; | ||
| try { | ||
| randBytes = new byte[length]; | ||
| randGen.nextBytes(randBytes); | ||
|
|
||
| return new SecureBuffer(randBytes); | ||
| } finally { | ||
| zeroizeBuffer(randBytes); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public SecureBuffer computeFakeSalt(UUID accountId) { | ||
| byte[] idBytes = null; | ||
| byte[] contextBytes = null; | ||
| byte[] combined = null; | ||
| byte[] hmacResult = null; | ||
| byte[] truncatedResult = null; | ||
|
|
||
| ByteBuffer idBuffer = null; | ||
| Mac mac = null; | ||
| SecretKeySpec spec = new SecretKeySpec(fakeSaltSecret, "HmacSHA256"); | ||
|
|
||
| try { | ||
| contextBytes = "Vaulton.FakeSalt.v1".getBytes(StandardCharsets.UTF_8); | ||
|
|
||
| idBuffer = ByteBuffer.allocate(CryptoConstants.SALT_LEN); | ||
| idBuffer.putLong(accountId.getMostSignificantBits()); | ||
| idBuffer.putLong(accountId.getLeastSignificantBits()); | ||
| idBytes = idBuffer.array(); | ||
|
|
||
| combined = new byte[idBytes.length + contextBytes.length]; | ||
| System.arraycopy(contextBytes, 0, combined, 0, contextBytes.length); | ||
| System.arraycopy(idBytes, 0, combined, contextBytes.length, idBytes.length); | ||
|
|
||
| mac = Mac.getInstance("HmacSHA256"); | ||
| mac.init(spec); | ||
| hmacResult = mac.doFinal(combined); | ||
|
|
||
| truncatedResult = new byte[CryptoConstants.SALT_LEN]; | ||
| System.arraycopy(hmacResult, 0, truncatedResult, 0, truncatedResult.length); | ||
|
|
||
| return new SecureBuffer(truncatedResult); | ||
| } catch (InvalidKeyException | NoSuchAlgorithmException e) { | ||
| throw new RuntimeException(e); | ||
| } finally { | ||
| zeroizeBuffer(idBytes); | ||
| if (idBuffer != null) idBuffer.clear(); | ||
| if (mac != null) mac.reset(); | ||
| zeroizeBuffer(contextBytes); | ||
| zeroizeBuffer(combined); | ||
| zeroizeBuffer(hmacResult); | ||
| zeroizeBuffer(truncatedResult); | ||
| } | ||
| } | ||
| } | ||
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
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
77 changes: 77 additions & 0 deletions
77
...i/src/test/java/dev/vaulton/vaultonapi/infrastructure/crypto/BouncyCryptoAdapterTest.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,77 @@ | ||
| package dev.vaulton.vaultonapi.infrastructure.crypto; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import dev.vaulton.vaultonapi.domain.crypto.CryptoConstants; | ||
| import dev.vaulton.vaultonapi.domain.crypto.SecureBuffer; | ||
| import java.security.SecureRandom; | ||
| import java.util.UUID; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| @SuppressWarnings("resource") | ||
| class BouncyCryptoAdapterTest { | ||
|
|
||
| private BouncyCryptoAdapter adapter; | ||
| private final byte[] fakeSaltSecret = new byte[32]; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| adapter = new BouncyCryptoAdapter(new SecureRandom(), fakeSaltSecret); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldGenerateRandomBytesOfRequestedLength() { | ||
|
|
||
| SecureBuffer testBuffer = adapter.generateRandomBytes(32); | ||
| SecureBuffer secondTestBuffer = adapter.generateRandomBytes(32); | ||
|
|
||
| assertEquals(32, testBuffer.length()); | ||
| assertEquals(32, secondTestBuffer.length()); | ||
|
|
||
| assertNotEquals(testBuffer, secondTestBuffer); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldComputeConsistentVerifier() { | ||
| SecureBuffer verifierRaw = adapter.generateRandomBytes(CryptoConstants.VERIFIER_LEN); | ||
| SecureBuffer salt = adapter.generateRandomBytes(CryptoConstants.SALT_LEN); | ||
| int iterations = 1000; | ||
| byte[] pepper = adapter.generateRandomBytes(CryptoConstants.PEPPER_LEN).bytes(); | ||
|
|
||
| SecureBuffer firstCompute = | ||
| adapter.computeStoredVerifier(verifierRaw, salt, iterations, pepper); | ||
| SecureBuffer secondCompute = | ||
| adapter.computeStoredVerifier(verifierRaw, salt, iterations, pepper); | ||
| SecureBuffer diffPepperCompute = | ||
| adapter.computeStoredVerifier( | ||
| verifierRaw, | ||
| salt, | ||
| iterations, | ||
| adapter.generateRandomBytes(CryptoConstants.PEPPER_LEN).bytes()); | ||
|
|
||
| assertEquals(CryptoConstants.VERIFIER_LEN, firstCompute.length()); | ||
| assertEquals(CryptoConstants.VERIFIER_LEN, secondCompute.length()); | ||
| assertEquals(CryptoConstants.VERIFIER_LEN, diffPepperCompute.length()); | ||
| assertEquals(firstCompute, secondCompute); | ||
|
|
||
| assertNotEquals(firstCompute, diffPepperCompute); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldComputeDeterministicFakeSalt() { | ||
| UUID firstUUID = UUID.randomUUID(); | ||
| UUID secondUUID = UUID.randomUUID(); | ||
|
|
||
| SecureBuffer firstSalt = adapter.computeFakeSalt(firstUUID); | ||
| SecureBuffer secondSalt = adapter.computeFakeSalt(firstUUID); | ||
| SecureBuffer diffIdSalt = adapter.computeFakeSalt(secondUUID); | ||
|
|
||
| assertEquals(CryptoConstants.SALT_LEN, firstSalt.length()); | ||
| assertEquals(CryptoConstants.SALT_LEN, secondSalt.length()); | ||
| assertEquals(CryptoConstants.SALT_LEN, diffIdSalt.length()); | ||
|
|
||
| assertEquals(firstSalt, secondSalt); | ||
| assertNotEquals(firstSalt, diffIdSalt); | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -1,10 +1,5 @@ | ||
| spring: | ||
| datasource: | ||
| url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1 | ||
| driver-class-name: org.h2.Driver | ||
| username: sa | ||
| password: | ||
| jpa: | ||
| database-platform: org.hibernate.dialect.H2Dialect | ||
| hibernate: | ||
| ddl-auto: create-drop | ||
| vaulton: | ||
| auth: | ||
| pepper: MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI= | ||
| fake-salt-secret: MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI= | ||
| iterations: 1000 # small amount of iters to make tests faster |
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.