-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plain java key management for non aws environment move from postgres to aws/ non aws profile
- Loading branch information
Showing
15 changed files
with
177 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/com/vauthenticator/server/keys/adapter/local/BouncyCastleKeyDecrypter.kt
This file contains 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,12 @@ | ||
package com.vauthenticator.server.keys.adapter.local | ||
|
||
import com.vauthenticator.server.extentions.encoder | ||
import com.vauthenticator.server.keys.domain.KeyDecrypter | ||
import com.vauthenticator.server.keys.domain.MasterKid | ||
|
||
class BouncyCastleKeyDecrypter(private val keyCryptographicOperations: KeyCryptographicOperations) : KeyDecrypter { | ||
override fun decryptKey(encrypted: String): String { | ||
return encoder.encode(keyCryptographicOperations.decryptKeyWith(MasterKid(""), encrypted.toByteArray())) | ||
.decodeToString() | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/com/vauthenticator/server/keys/adapter/local/BouncyCastleKeyGenerator.kt
This file contains 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,32 @@ | ||
package com.vauthenticator.server.keys.adapter.local | ||
|
||
import com.vauthenticator.server.keys.domain.DataKey | ||
import com.vauthenticator.server.keys.domain.KeyGenerator | ||
import com.vauthenticator.server.keys.domain.MasterKid | ||
import java.util.* | ||
|
||
|
||
class BouncyCastleKeyGenerator( | ||
private val keyCryptographicOperations: KeyCryptographicOperations | ||
) : KeyGenerator { | ||
|
||
|
||
override fun dataKeyPairFor(masterKid: MasterKid): DataKey { | ||
val generateRSAKeyPair = keyCryptographicOperations.generateRSAKeyPair() | ||
return DataKey( | ||
keyCryptographicOperations.encryptKeyWith(masterKid, generateRSAKeyPair.private.encoded), | ||
Optional.of(generateRSAKeyPair.public.encoded) | ||
) | ||
} | ||
|
||
override fun dataKeyFor(masterKid: MasterKid): DataKey { | ||
val generateRSAKeyPair = keyCryptographicOperations.generateRSAKeyPair() | ||
return DataKey( | ||
keyCryptographicOperations.encryptKeyWith(masterKid, generateRSAKeyPair.private.encoded), | ||
Optional.empty() | ||
) | ||
} | ||
|
||
|
||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...m/vauthenticator/server/keys/adapter/local/BouncyCastleKeyGeneratorMasterKeyRepository.kt
This file contains 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,14 @@ | ||
package com.vauthenticator.server.keys.adapter.local | ||
|
||
import com.vauthenticator.server.extentions.toSha256 | ||
import com.vauthenticator.server.keys.domain.MasterKid | ||
val toSha256 = "secret".toSha256() | ||
|
||
class BouncyCastleKeyGeneratorMasterKeyRepository { | ||
|
||
//TODO to improve | ||
fun maskerKeyFor(masterKeyId: MasterKid): String { | ||
return toSha256 | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
src/main/kotlin/com/vauthenticator/server/keys/adapter/local/KeyCryptographicOperations.kt
This file contains 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,52 @@ | ||
package com.vauthenticator.server.keys.adapter.local | ||
|
||
import com.vauthenticator.server.extentions.decoder | ||
import com.vauthenticator.server.keys.domain.MasterKid | ||
import org.bouncycastle.jce.provider.BouncyCastleProvider | ||
import java.security.KeyPair | ||
import java.security.KeyPairGenerator | ||
import java.security.Security | ||
import java.security.spec.RSAKeyGenParameterSpec | ||
import javax.crypto.Cipher | ||
import javax.crypto.spec.SecretKeySpec | ||
|
||
|
||
class KeyCryptographicOperations( | ||
private val repository: BouncyCastleKeyGeneratorMasterKeyRepository, | ||
) { | ||
companion object { | ||
init { | ||
Security.addProvider(BouncyCastleProvider()); | ||
} | ||
} | ||
|
||
fun generateRSAKeyPair(): KeyPair { | ||
val keyPair: KeyPair | ||
try { | ||
val keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC") | ||
keyPairGenerator.initialize(RSAKeyGenParameterSpec(2048, RSAKeyGenParameterSpec.F4)) | ||
keyPair = keyPairGenerator.generateKeyPair() | ||
} catch (ex: Exception) { | ||
throw IllegalStateException(ex) | ||
} | ||
return keyPair | ||
} | ||
|
||
fun encryptKeyWith(masterKid: MasterKid, encodedPlainText: ByteArray): ByteArray { | ||
val masterKey = repository.maskerKeyFor(masterKid); | ||
val key = SecretKeySpec(masterKey.toByteArray(), "AES") | ||
val cipher = Cipher.getInstance("AES") | ||
cipher.init(Cipher.ENCRYPT_MODE, key) | ||
return cipher.doFinal(decoder.decode(encodedPlainText)) | ||
} | ||
|
||
fun decryptKeyWith(masterKid: MasterKid, encodedEncryptedText: ByteArray): ByteArray { | ||
val masterKey = repository.maskerKeyFor(masterKid); | ||
val key = SecretKeySpec(masterKey.toByteArray(), "AES") | ||
val cipher = Cipher.getInstance("AES") | ||
cipher.init(Cipher.DECRYPT_MODE, key) | ||
return cipher.doFinal(decoder.decode(encodedEncryptedText)) | ||
} | ||
|
||
|
||
} |
This file contains 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 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 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 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
8 changes: 8 additions & 0 deletions
8
src/test/kotlin/com/vauthenticator/server/keys/adapter/kms/KmsKeyGeneratorTest.kt
This file contains 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,8 @@ | ||
package com.vauthenticator.server.keys.adapter.kms | ||
|
||
import org.junit.jupiter.api.Assertions.* | ||
|
||
class KmsKeyGeneratorTest { | ||
|
||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/test/kotlin/com/vauthenticator/server/keys/adapter/local/BouncyCastleKeyGeneratorTest.kt
This file contains 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,9 @@ | ||
package com.vauthenticator.server.keys.adapter.local | ||
|
||
import org.junit.jupiter.api.Assertions.* | ||
|
||
class BouncyCastleKeyGeneratorTest { | ||
|
||
|
||
|
||
} |