-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #259 from VAuthenticator/local-key-material
Local key material
- Loading branch information
Showing
22 changed files
with
491 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Profile | ||
|
||
VAuthenticator can be configured to be strongly AWS integrated using DynamoDB for the persistence layer and KMS for Key management. | ||
|
||
If your organization or for you run VAuthenticator so tiny integrated with AWS does not is suitable you can decide to switch postgresql instead dynamodb for the persistence | ||
and plain java security key management instead of KMS | ||
|
||
All what you need is enable the relative spring profile as below: | ||
|
||
use ```spring.profiles.active``` with | ||
|
||
- ```database```: to use PostgresSQL | ||
- ```dynamo```: to use DyanamoDB | ||
- ```kms``` to use KMS | ||
- omitting ```kms``` to use plain java security api | ||
|
||
in case of plain java security implementation the follow configuration is required: | ||
|
||
|
||
```yaml | ||
key: | ||
master-key: | ||
storage: | ||
content: | ||
key : value | ||
key2 : value2 | ||
``` | ||
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
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
52 changes: 52 additions & 0 deletions
52
...kotlin/com/vauthenticator/server/keys/adapter/java/JavaSecurityCryptographicOperations.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.java | ||
|
||
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 JavaSecurityCryptographicOperations( | ||
private val repository: KeyGeneratorMasterKeyRepository | ||
) { | ||
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 = decoder.decode(repository.maskerKeyFor(masterKid)); | ||
val key = SecretKeySpec(masterKey, "AES") | ||
val cipher = Cipher.getInstance("AES") | ||
cipher.init(Cipher.ENCRYPT_MODE, key) | ||
return cipher.doFinal(encodedPlainText) | ||
} | ||
|
||
fun decryptKeyWith(masterKid: MasterKid, encodedEncryptedText: ByteArray): ByteArray { | ||
val masterKey = decoder.decode(repository.maskerKeyFor(masterKid)); | ||
val key = SecretKeySpec(masterKey, "AES") | ||
val cipher = Cipher.getInstance("AES") | ||
cipher.init(Cipher.DECRYPT_MODE, key) | ||
return cipher.doFinal(decoder.decode(encodedEncryptedText)) | ||
} | ||
|
||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/vauthenticator/server/keys/adapter/java/JavaSecurityKeyDecrypter.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,15 @@ | ||
package com.vauthenticator.server.keys.adapter.java | ||
|
||
import com.vauthenticator.server.extentions.encoder | ||
import com.vauthenticator.server.keys.domain.KeyDecrypter | ||
import com.vauthenticator.server.keys.domain.MasterKid | ||
|
||
class JavaSecurityKeyDecrypter( | ||
private val maserKid: String, | ||
private val javaSecurityCryptographicOperations: JavaSecurityCryptographicOperations | ||
) : KeyDecrypter { | ||
override fun decryptKey(encrypted: String): String { | ||
return encoder.encode(javaSecurityCryptographicOperations.decryptKeyWith(MasterKid(maserKid), encrypted.toByteArray())) | ||
.decodeToString() | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/com/vauthenticator/server/keys/adapter/java/JavaSecurityKeyGenerator.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.java | ||
|
||
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 JavaSecurityKeyGenerator( | ||
private val javaSecurityCryptographicOperations: JavaSecurityCryptographicOperations | ||
) : KeyGenerator { | ||
|
||
|
||
override fun dataKeyPairFor(masterKid: MasterKid): DataKey { | ||
val generateRSAKeyPair = javaSecurityCryptographicOperations.generateRSAKeyPair() | ||
return DataKey( | ||
javaSecurityCryptographicOperations.encryptKeyWith(masterKid, generateRSAKeyPair.private.encoded), | ||
Optional.of(generateRSAKeyPair.public.encoded) | ||
) | ||
} | ||
|
||
override fun dataKeyFor(masterKid: MasterKid): DataKey { | ||
val generateRSAKeyPair = javaSecurityCryptographicOperations.generateRSAKeyPair() | ||
return DataKey( | ||
javaSecurityCryptographicOperations.encryptKeyWith(masterKid, generateRSAKeyPair.private.encoded), | ||
Optional.empty() | ||
) | ||
} | ||
|
||
|
||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...ain/kotlin/com/vauthenticator/server/keys/adapter/java/KeyGeneratorMasterKeyRepository.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,30 @@ | ||
package com.vauthenticator.server.keys.adapter.java | ||
|
||
import com.vauthenticator.server.keys.domain.MasterKid | ||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Profile | ||
|
||
|
||
class KeyGeneratorMasterKeyRepository( | ||
val storage: KeyGeneratorMasterKeyStorage | ||
) { | ||
|
||
fun maskerKeyFor(masterKeyId: MasterKid): String { | ||
return storage.content[masterKeyId.content()]!! | ||
} | ||
|
||
} | ||
|
||
@Profile("!kms") | ||
@Configuration(proxyBeanMethods = false) | ||
@EnableConfigurationProperties(KeyGeneratorMasterKeyStorage::class) | ||
class KeyGeneratorMasterKeyRepositoryConfig { | ||
|
||
} | ||
|
||
@ConfigurationProperties(prefix = "key.master-key.storage") | ||
data class KeyGeneratorMasterKeyStorage(val content: Map<String, String>) { | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/com/vauthenticator/server/keys/adapter/java/KeyInitJob.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,31 @@ | ||
package com.vauthenticator.server.keys.adapter.java | ||
|
||
import com.vauthenticator.server.keys.domain.* | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.boot.ApplicationArguments | ||
import org.springframework.boot.ApplicationRunner | ||
import org.springframework.context.annotation.Profile | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
@Profile("!kms") | ||
class KeyInitJob( | ||
@Value("\${key.master-key}") private val maserKid: String, | ||
private val keyStorage: KeyStorage, | ||
private val keyRepository: KeyRepository | ||
) : ApplicationRunner { | ||
|
||
override fun run(args: ApplicationArguments) { | ||
|
||
if (keyStorage.signatureKeys().keys.isEmpty()) { | ||
val kid = keyRepository.createKeyFrom( | ||
masterKid = MasterKid(maserKid), | ||
keyPurpose = KeyPurpose.SIGNATURE, | ||
keyType = KeyType.ASYMMETRIC, | ||
) | ||
println(kid) | ||
} | ||
|
||
} | ||
|
||
} |
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
Oops, something went wrong.