commit fd7f50ca8f4d846350493de840e6c341e9e75543
parent bd89d19f158e15aabc03e712f5cd63c804a9c125
Author: Marcello Stanisci <stanisci.m@gmail.com>
Date: Thu, 24 Oct 2019 19:47:48 +0200
Generating private key and storing it into database.
Diffstat:
3 files changed, 85 insertions(+), 4 deletions(-)
diff --git a/sandbox/src/main/kotlin/DB.kt b/sandbox/src/main/kotlin/DB.kt
@@ -10,7 +10,8 @@ const val EBICS_PARTNER_ID_MAX_LENGTH = 10
const val EBICS_SYSTEM_ID_MAX_LENGTH = 10
const val PUBLIC_KEY_MAX_MODULUS_LENGTH = 2048 // FIXME review this value!
const val PUBLIC_KEY_MAX_EXPONENT_LENGTH = 64 // FIXME review this value!
-const val PRIV_KEY_MAX_LENGTH = 512 // FIXME review this value!
+const val PRIVATE_KEY_MODULUS_LENGTH = 1024 // FIXME review this value!
+const val PRIVATE_KEY_EXPONENT_LENGTH = 10
/**
* All the states to give a subscriber.
@@ -215,8 +216,16 @@ fun createSubscriber() : EbicsSubscriber {
/**
* This table stores RSA private keys.
*/
-object EbicsPrivateKey: IntIdTable() {
- val pub = binary("priv", PRIV_KEY_MAX_LENGTH)
+object EbicsBankPrivateKeys: IntIdTable() {
+ val modulus = binary("modulus", PRIVATE_KEY_MODULUS_LENGTH)
+ val exponent = binary("exponent", PRIVATE_KEY_EXPONENT_LENGTH)
+}
+
+class EbicsBankPrivateKey(id: EntityID<Int>) : IntEntity(id) {
+ companion object : IntEntityClass<EbicsBankPrivateKey>(EbicsBankPrivateKeys)
+
+ var modulus by EbicsBankPrivateKeys.modulus
+ var exponent by EbicsBankPrivateKeys.exponent
}
fun dbCreateTables() {
@@ -230,7 +239,8 @@ fun dbCreateTables() {
EbicsUsers,
EbicsPartners,
EbicsSystems,
- EbicsSubscribers
+ EbicsSubscribers,
+ EbicsBankPrivateKeys
)
}
}
diff --git a/sandbox/src/main/kotlin/Main.kt b/sandbox/src/main/kotlin/Main.kt
@@ -48,8 +48,13 @@ import java.math.BigInteger
import java.nio.charset.StandardCharsets.US_ASCII
import java.text.DateFormat
import java.security.KeyFactory
+import java.security.KeyPairGenerator
+import java.security.PrivateKey
import java.security.PublicKey
+import java.security.interfaces.RSAPrivateKey
+import java.security.spec.RSAPrivateKeySpec
import java.security.spec.RSAPublicKeySpec
+import java.util.*
import java.util.zip.InflaterInputStream
val logger = LoggerFactory.getLogger("tech.libeufin.sandbox")
@@ -175,6 +180,45 @@ fun loadRsaPublicKey (modulus: ByteArray, exponent: ByteArray) : PublicKey {
return keyFactory.generatePublic(tmp)
}
+/**
+ * The function tries to get the bank private key from the database.
+ * If it does not find it, it generates a new one and stores it in
+ * database.
+ *
+ * @return the key (whether from database or freshly created)
+ */
+fun getOrMakePrivateKey(): PrivateKey {
+
+ // bank has always one private key in database.
+ var tmp = transaction {
+ EbicsBankPrivateKey.findById(1)
+ }
+
+ // must generate one now
+ if (tmp == null) {
+
+ val privateExponent = BigInteger(PRIVATE_KEY_EXPONENT_LENGTH, Random()) // shall be set to some well-known value?
+ val privateModulus = BigInteger(PRIVATE_KEY_MODULUS_LENGTH, Random())
+
+ tmp = transaction {
+ EbicsBankPrivateKey.new {
+ modulus = privateModulus.toByteArray()
+ exponent = privateExponent.toByteArray()
+ }
+ }
+ }
+
+ val keySpec = RSAPrivateKeySpec(
+ BigInteger(tmp.modulus),
+ BigInteger(tmp.exponent)
+ )
+
+ val factory = KeyFactory.getInstance("RSA")
+ val privateKey = factory.generatePrivate(keySpec)
+
+ return privateKey
+}
+
private suspend fun ApplicationCall.adminCustomers() {
val body = try {
diff --git a/sandbox/src/test/kotlin/GeneratePrivateKeyTest.kt b/sandbox/src/test/kotlin/GeneratePrivateKeyTest.kt
@@ -0,0 +1,26 @@
+package tech.libeufin.sandbox
+
+import org.junit.Test
+import junit.framework.TestCase.assertTrue
+import org.jetbrains.exposed.sql.transactions.transaction
+import org.junit.Before
+
+class GeneratePrivateKeyTest {
+
+ @Before
+ fun setUp() {
+ dbCreateTables()
+ }
+
+ @Test
+ fun loadOrGeneratePrivateKey() {
+
+ val x = getOrMakePrivateKey()
+
+ assertTrue(
+ transaction {
+ EbicsBankPrivateKey.findById(1)
+ } != null
+ )
+ }
+}
+\ No newline at end of file