commit e30e4700e165ec5c38d21a9619b4f24ef8f0f21a
parent c48f48113387f7b84a3b5109ee91b3b2f7888696
Author: Florian Dold <florian.dold@gmail.com>
Date: Mon, 4 Nov 2019 11:40:54 +0100
wip: EBICS E002 encryption
Diffstat:
3 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/sandbox/src/main/kotlin/CryptoUtil.kt b/sandbox/src/main/kotlin/CryptoUtil.kt
@@ -19,16 +19,20 @@
package tech.libeufin.sandbox
+import org.bouncycastle.jce.provider.BouncyCastleProvider
import java.lang.Exception
import java.math.BigInteger
import java.security.KeyFactory
import java.security.KeyPairGenerator
+import java.security.PrivateKey
import java.security.PublicKey
import java.security.interfaces.RSAPrivateCrtKey
import java.security.interfaces.RSAPublicKey
import java.security.spec.PKCS8EncodedKeySpec
import java.security.spec.RSAPublicKeySpec
import java.security.spec.X509EncodedKeySpec
+import javax.crypto.Cipher
+import javax.crypto.KeyGenerator
/**
* RSA key pair.
@@ -39,7 +43,16 @@ data class RsaCrtKeyPair(val private: RSAPrivateCrtKey, val public: RSAPublicKey
* Helpers for dealing with crypographic operations in EBICS / LibEuFin.
*/
class CryptoUtil {
+
+ data class EncryptionResult(
+ val encryptedTransactionKey: ByteArray,
+ val pubKeyDigest: ByteArray,
+ val encryptedData: ByteArray
+ )
+
companion object {
+ private val bouncyCastleProvider = BouncyCastleProvider()
+
/**
* Load an RSA private key from its binary PKCS#8 encoding.
*/
@@ -106,5 +119,16 @@ class CryptoUtil {
val tmp = RSAPublicKeySpec(modulusBigInt, exponentBigInt)
return keyFactory.generatePublic(tmp) as RSAPublicKey
}
+
+ fun encryptEbicsE002(data: ByteArray, signingPrivateKey: RSAPrivateCrtKey) {
+ val prov = BouncyCastleProvider()
+ val keygen = KeyGenerator.getInstance("AES", bouncyCastleProvider)
+ keygen.init(128)
+ val transportKey = keygen.generateKey()
+
+ val cipher = Cipher.getInstance("AES/CBC/X9.23Padding", bouncyCastleProvider)
+ cipher.init(Cipher.ENCRYPT_MODE, transportKey)
+ val encryptedData = cipher.doFinal(data)
+ }
}
}
diff --git a/sandbox/src/main/kotlin/Main.kt b/sandbox/src/main/kotlin/Main.kt
@@ -323,8 +323,7 @@ private suspend fun ApplicationCall.ebicsweb() {
"ebicsNoPubKeyDigestsRequest" -> {
val requestJaxb = XMLUtil.convertDomToJaxb(EbicsNoPubKeyDigestsRequest::class.java, bodyDocument)
val staticHeader = requestJaxb.value.header.static
- val orderType = staticHeader.orderDetails.orderType
- when (orderType) {
+ when (val orderType = staticHeader.orderDetails.orderType) {
"HPB" -> {
val subscriberKeys = transaction {
val ebicsSubscriber =
diff --git a/sandbox/src/test/kotlin/CryptoUtilTest.kt b/sandbox/src/test/kotlin/CryptoUtilTest.kt
@@ -56,4 +56,11 @@ class CryptoUtilTest {
assertEquals(keyPair.private, otherKeyPair.private)
assertEquals(keyPair.public, otherKeyPair.public)
}
+
+ @Test
+ fun testEbicsE002() {
+ val data = "Hello, World!"
+ val keyPair = CryptoUtil.generateRsaKeyPair(1024)
+ CryptoUtil.encryptEbicsE002(data.toByteArray(), keyPair.private)
+ }
}
\ No newline at end of file