Skip to content

Commit

Permalink
Allow encoded server key files to be in base64 [DPP-761] (#11796)
Browse files Browse the repository at this point in the history
* Allow encoded server key files to be in base64

CHANGELOG_BEGIN
CHANGELOG_END

* format

* dry tests
  • Loading branch information
mziolekda authored Nov 23, 2021
1 parent cc3f551 commit f2aa09c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import java.nio.charset.StandardCharsets
import java.nio.file.Files
import javax.crypto.Cipher
import javax.crypto.spec.{IvParameterSpec, SecretKeySpec}

import org.apache.commons.codec.binary.Hex
import org.apache.commons.io.IOUtils
import spray.json.{DefaultJsonProtocol, RootJsonFormat}

import scala.util.Using
import java.util.Base64
import scala.util.{Try, Using}

final class PrivateKeyDecryptionException(cause: Throwable) extends Exception(cause)

Expand All @@ -39,14 +39,24 @@ case class DecryptionParameters(
transformation.split("/")(0)
}

private def decodeBase64OrGetVerbatim(encrypted: Array[Byte]): Array[Byte] = {
val potentialBase64Char =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/+=\n\r".getBytes()
encrypted.find(!potentialBase64Char.contains(_)) match {
case None => Try(Base64.getMimeDecoder.decode(encrypted)).getOrElse(encrypted)
case _ => encrypted
}
}

private[tls] def decrypt(encrypted: Array[Byte]): Array[Byte] = {
val key: Array[Byte] = Hex.decodeHex(keyInHex)
val secretKey = new SecretKeySpec(key, algorithm)
val iv: Array[Byte] = Hex.decodeHex(initializationVectorInHex)
val cipher = Cipher.getInstance(transformation)
val ivParameterSpec = new IvParameterSpec(iv)
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec)
cipher.doFinal(encrypted)
val binary = decodeBase64OrGetVerbatim(encrypted)
cipher.doFinal(binary)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ package com.daml.ledger.api.tls
import java.io.ByteArrayInputStream
import java.nio.file.Files
import javax.crypto.{Cipher, KeyGenerator, SecretKey}

import org.apache.commons.codec.binary.Hex
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

import java.util.Base64

class DecryptionParametersTest extends AnyWordSpec with Matchers {

"decryption parameters" should {

// given
val key: SecretKey = KeyGenerator.getInstance("AES").generateKey()
val clearText = "clearText123"
val clearText = "clearText123 " * 10
val clearTextBytes = clearText.getBytes
val transformation = "AES/CBC/PKCS5Padding"
val cipher = Cipher.getInstance(transformation)
Expand All @@ -41,11 +42,20 @@ class DecryptionParametersTest extends AnyWordSpec with Matchers {
new String(actual) shouldBe clearText
}

"decrypt a file" in {
"decrypt a file" in
testFileDecoding("-verbatim", cipherText)

"decrypt a file in base64" in
testFileDecoding("-base64", Base64.getEncoder.encode(cipherText))

"decrypt a file in MIME base64" in
testFileDecoding("-mime-base64", Base64.getMimeEncoder.encode(cipherText))

def testFileDecoding(fileSuffix: String, content: Array[Byte]) = {
// given
val tmpFilePath = Files.createTempFile("cipher-text", ".enc")
Files.write(tmpFilePath, cipherText)
assume(Files.readAllBytes(tmpFilePath) sameElements cipherText)
val tmpFilePath = Files.createTempFile(s"cipher-text$fileSuffix", ".enc")
Files.write(tmpFilePath, content)
assume(Files.readAllBytes(tmpFilePath) sameElements content)

// when
val actual: Array[Byte] = tested.decrypt(tmpFilePath.toFile)
Expand Down
2 changes: 1 addition & 1 deletion ledger/test-common/test-certificates/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ chmod 444 $(location server.crt)
# Encrypt server's key and dump encryption parameters to a JSON file.
# NOTE: Encryption details used to encrypt the private must be kept in sync with `ledger/test-common/files/server-pem-decryption-parameters.json`
$(location @openssl_dev_env//:openssl) enc -aes-128-cbc \
$(location @openssl_dev_env//:openssl) enc -aes-128-cbc -base64 \
-in $(location server.pem) \
-out $(location server.pem.enc) \
-K 0034567890abcdef1234567890abcdef \
Expand Down

0 comments on commit f2aa09c

Please sign in to comment.