forked from corda/corda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CORDA-540: Add verification to ensure that private keys can only be s…
…erialized with specific contexts (corda#1800)
- Loading branch information
1 parent
e8d21f2
commit 33ba145
Showing
5 changed files
with
67 additions
and
10 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
12 changes: 12 additions & 0 deletions
12
node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/UseCaseAwareness.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,12 @@ | ||
package net.corda.nodeapi.internal.serialization | ||
|
||
import net.corda.core.serialization.SerializationContext | ||
import net.corda.core.serialization.SerializationFactory | ||
import java.util.* | ||
|
||
internal fun checkUseCase(allowedUseCases: EnumSet<SerializationContext.UseCase>) { | ||
val currentContext: SerializationContext = SerializationFactory.currentFactory?.currentContext ?: throw IllegalStateException("Current context is not set") | ||
if(!allowedUseCases.contains(currentContext.useCase)) { | ||
throw IllegalStateException("UseCase '${currentContext.useCase}' is not within '$allowedUseCases'") | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
...i/src/test/kotlin/net/corda/nodeapi/internal/serialization/PrivateKeySerializationTest.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,42 @@ | ||
package net.corda.nodeapi.internal.serialization | ||
|
||
import net.corda.core.crypto.Crypto | ||
import net.corda.core.serialization.SerializationContext.UseCase.* | ||
import net.corda.core.serialization.SerializationDefaults | ||
import net.corda.core.serialization.serialize | ||
import net.corda.testing.TestDependencyInjectionBase | ||
import org.assertj.core.api.Assertions.assertThatThrownBy | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.Parameterized | ||
import java.security.PrivateKey | ||
import kotlin.test.assertTrue | ||
|
||
@RunWith(Parameterized::class) | ||
class PrivateKeySerializationTest(private val privateKey: PrivateKey, private val testName: String) : TestDependencyInjectionBase() { | ||
|
||
companion object { | ||
@JvmStatic | ||
@Parameterized.Parameters(name = "{1}") | ||
fun data(): Collection<Array<Any>> { | ||
val privateKeys: List<PrivateKey> = Crypto.supportedSignatureSchemes().filterNot { Crypto.COMPOSITE_KEY === it } | ||
.map { Crypto.generateKeyPair(it).private } | ||
|
||
return privateKeys.map { arrayOf<Any>(it, PrivateKeySerializationTest::class.java.simpleName + "-" + it.javaClass.simpleName) } | ||
} | ||
} | ||
|
||
@Test | ||
fun `passed with expected UseCases`() { | ||
assertTrue { privateKey.serialize(context = SerializationDefaults.STORAGE_CONTEXT).bytes.isNotEmpty() } | ||
assertTrue { privateKey.serialize(context = SerializationDefaults.CHECKPOINT_CONTEXT).bytes.isNotEmpty() } | ||
} | ||
|
||
@Test | ||
fun `failed with wrong UseCase`() { | ||
assertThatThrownBy { privateKey.serialize(context = SerializationDefaults.P2P_CONTEXT) } | ||
.isInstanceOf(IllegalStateException::class.java) | ||
.hasMessageContaining("UseCase '${P2P}' is not within") | ||
|
||
} | ||
} |