gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [libeufin] branch master updated: Generating private key an


From: gnunet
Subject: [GNUnet-SVN] [libeufin] branch master updated: Generating private key and storing it into database.
Date: Thu, 24 Oct 2019 19:48:12 +0200

This is an automated email from the git hooks/post-receive script.

marcello pushed a commit to branch master
in repository libeufin.

The following commit(s) were added to refs/heads/master by this push:
     new fd7f50c  Generating private key and storing it into database.
fd7f50c is described below

commit fd7f50ca8f4d846350493de840e6c341e9e75543
Author: Marcello Stanisci <address@hidden>
AuthorDate: Thu Oct 24 19:47:48 2019 +0200

    Generating private key and storing it into database.
---
 sandbox/src/main/kotlin/DB.kt                     | 18 +++++++---
 sandbox/src/main/kotlin/Main.kt                   | 44 +++++++++++++++++++++++
 sandbox/src/test/kotlin/GeneratePrivateKeyTest.kt | 26 ++++++++++++++
 3 files changed, 84 insertions(+), 4 deletions(-)

diff --git a/sandbox/src/main/kotlin/DB.kt b/sandbox/src/main/kotlin/DB.kt
index 3b5ec63..bc07959 100644
--- 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
index 92ec7cf..7122e03 100644
--- 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
new file mode 100644
index 0000000..d5f6496
--- /dev/null
+++ 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

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]