mirror of
https://github.com/corda/corda.git
synced 2025-05-30 06:04:24 +00:00
ENT-2967 Fix up use of various JDK performance contention points (#4608)
* ENT-2967 Various JDK contention points * Move new private method to bottom of class.
This commit is contained in:
parent
ee4f4f5702
commit
197a13611d
@ -290,7 +290,7 @@ object Crypto {
|
|||||||
fun decodePrivateKey(encodedKey: ByteArray): PrivateKey {
|
fun decodePrivateKey(encodedKey: ByteArray): PrivateKey {
|
||||||
val keyInfo = PrivateKeyInfo.getInstance(encodedKey)
|
val keyInfo = PrivateKeyInfo.getInstance(encodedKey)
|
||||||
val signatureScheme = findSignatureScheme(keyInfo.privateKeyAlgorithm)
|
val signatureScheme = findSignatureScheme(keyInfo.privateKeyAlgorithm)
|
||||||
val keyFactory = KeyFactory.getInstance(signatureScheme.algorithmName, providerMap[signatureScheme.providerName])
|
val keyFactory = keyFactory(signatureScheme)
|
||||||
return keyFactory.generatePrivate(PKCS8EncodedKeySpec(encodedKey))
|
return keyFactory.generatePrivate(PKCS8EncodedKeySpec(encodedKey))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -323,7 +323,7 @@ object Crypto {
|
|||||||
"Unsupported key/algorithm for schemeCodeName: ${signatureScheme.schemeCodeName}"
|
"Unsupported key/algorithm for schemeCodeName: ${signatureScheme.schemeCodeName}"
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
val keyFactory = KeyFactory.getInstance(signatureScheme.algorithmName, providerMap[signatureScheme.providerName])
|
val keyFactory = keyFactory(signatureScheme)
|
||||||
return keyFactory.generatePrivate(PKCS8EncodedKeySpec(encodedKey))
|
return keyFactory.generatePrivate(PKCS8EncodedKeySpec(encodedKey))
|
||||||
} catch (ikse: InvalidKeySpecException) {
|
} catch (ikse: InvalidKeySpecException) {
|
||||||
throw InvalidKeySpecException("This private key cannot be decoded, please ensure it is PKCS8 encoded and that " +
|
throw InvalidKeySpecException("This private key cannot be decoded, please ensure it is PKCS8 encoded and that " +
|
||||||
@ -342,7 +342,7 @@ object Crypto {
|
|||||||
fun decodePublicKey(encodedKey: ByteArray): PublicKey {
|
fun decodePublicKey(encodedKey: ByteArray): PublicKey {
|
||||||
val subjectPublicKeyInfo = SubjectPublicKeyInfo.getInstance(encodedKey)
|
val subjectPublicKeyInfo = SubjectPublicKeyInfo.getInstance(encodedKey)
|
||||||
val signatureScheme = findSignatureScheme(subjectPublicKeyInfo.algorithm)
|
val signatureScheme = findSignatureScheme(subjectPublicKeyInfo.algorithm)
|
||||||
val keyFactory = KeyFactory.getInstance(signatureScheme.algorithmName, providerMap[signatureScheme.providerName])
|
val keyFactory = keyFactory(signatureScheme)
|
||||||
return keyFactory.generatePublic(X509EncodedKeySpec(encodedKey))
|
return keyFactory.generatePublic(X509EncodedKeySpec(encodedKey))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -377,7 +377,7 @@ object Crypto {
|
|||||||
"Unsupported key/algorithm for schemeCodeName: ${signatureScheme.schemeCodeName}"
|
"Unsupported key/algorithm for schemeCodeName: ${signatureScheme.schemeCodeName}"
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
val keyFactory = KeyFactory.getInstance(signatureScheme.algorithmName, providerMap[signatureScheme.providerName])
|
val keyFactory = keyFactory(signatureScheme)
|
||||||
return keyFactory.generatePublic(X509EncodedKeySpec(encodedKey))
|
return keyFactory.generatePublic(X509EncodedKeySpec(encodedKey))
|
||||||
} catch (ikse: InvalidKeySpecException) {
|
} catch (ikse: InvalidKeySpecException) {
|
||||||
throw throw InvalidKeySpecException("This public key cannot be decoded, please ensure it is X509 encoded and " +
|
throw throw InvalidKeySpecException("This public key cannot be decoded, please ensure it is X509 encoded and " +
|
||||||
@ -1063,4 +1063,8 @@ object Crypto {
|
|||||||
private fun setBouncyCastleRNG() {
|
private fun setBouncyCastleRNG() {
|
||||||
CryptoServicesRegistrar.setSecureRandom(newSecureRandom())
|
CryptoServicesRegistrar.setSecureRandom(newSecureRandom())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun keyFactory(signatureScheme: SignatureScheme) = signatureScheme.getKeyFactory {
|
||||||
|
KeyFactory.getInstance(signatureScheme.algorithmName, providerMap[signatureScheme.providerName])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package net.corda.core.crypto
|
|||||||
|
|
||||||
import net.corda.core.KeepForDJVM
|
import net.corda.core.KeepForDJVM
|
||||||
import org.bouncycastle.asn1.x509.AlgorithmIdentifier
|
import org.bouncycastle.asn1.x509.AlgorithmIdentifier
|
||||||
|
import java.security.KeyFactory
|
||||||
import java.security.Signature
|
import java.security.Signature
|
||||||
import java.security.spec.AlgorithmParameterSpec
|
import java.security.spec.AlgorithmParameterSpec
|
||||||
|
|
||||||
@ -33,4 +34,15 @@ data class SignatureScheme(
|
|||||||
val algSpec: AlgorithmParameterSpec?,
|
val algSpec: AlgorithmParameterSpec?,
|
||||||
val keySize: Int?,
|
val keySize: Int?,
|
||||||
val desc: String
|
val desc: String
|
||||||
)
|
) {
|
||||||
|
@Volatile
|
||||||
|
private var memoizedKeyFactory: KeyFactory? = null
|
||||||
|
|
||||||
|
internal fun getKeyFactory(factoryFactory: () -> KeyFactory): KeyFactory {
|
||||||
|
return memoizedKeyFactory ?: run {
|
||||||
|
val newFactory = factoryFactory()
|
||||||
|
memoizedKeyFactory = newFactory
|
||||||
|
newFactory
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -124,7 +124,7 @@ internal fun checkConstraintValidity(state: TransactionState<*>) {
|
|||||||
*/
|
*/
|
||||||
internal fun ContractClassName.contractHasAutomaticConstraintPropagation(classLoader: ClassLoader? = null): Boolean {
|
internal fun ContractClassName.contractHasAutomaticConstraintPropagation(classLoader: ClassLoader? = null): Boolean {
|
||||||
return (classLoader ?: NoConstraintPropagation::class.java.classLoader)
|
return (classLoader ?: NoConstraintPropagation::class.java.classLoader)
|
||||||
.loadClass(this)
|
.run { Class.forName(this@contractHasAutomaticConstraintPropagation, false, this) }
|
||||||
.getAnnotation(NoConstraintPropagation::class.java) == null
|
.getAnnotation(NoConstraintPropagation::class.java) == null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,6 @@ class IncompatibleTypeIdentifierException(message: String) : NotSerializableExce
|
|||||||
* [TypeIdentifier] provides a family of type identifiers, together with a [prettyPrint] method for displaying them.
|
* [TypeIdentifier] provides a family of type identifiers, together with a [prettyPrint] method for displaying them.
|
||||||
*/
|
*/
|
||||||
sealed class TypeIdentifier {
|
sealed class TypeIdentifier {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The name of the type.
|
* The name of the type.
|
||||||
*/
|
*/
|
||||||
@ -38,7 +37,7 @@ sealed class TypeIdentifier {
|
|||||||
* @throws IncompatibleTypeIdentifierException if the type identifier is incompatible with the locally-defined type
|
* @throws IncompatibleTypeIdentifierException if the type identifier is incompatible with the locally-defined type
|
||||||
* to which it refers.
|
* to which it refers.
|
||||||
*/
|
*/
|
||||||
abstract fun getLocalType(classLoader: ClassLoader = ClassLoader.getSystemClassLoader()): Type
|
abstract fun getLocalType(classLoader: ClassLoader = systemClassLoader): Type
|
||||||
|
|
||||||
open val erased: TypeIdentifier get() = this
|
open val erased: TypeIdentifier get() = this
|
||||||
|
|
||||||
@ -61,6 +60,9 @@ sealed class TypeIdentifier {
|
|||||||
if (simplifyClassNames) split(".", "$").last() else this
|
if (simplifyClassNames) split(".", "$").last() else this
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
// This method has locking. So we memo the value here.
|
||||||
|
private val systemClassLoader: ClassLoader = ClassLoader.getSystemClassLoader()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtain the [TypeIdentifier] for an erased Java class.
|
* Obtain the [TypeIdentifier] for an erased Java class.
|
||||||
*
|
*
|
||||||
@ -204,7 +206,7 @@ sealed class TypeIdentifier {
|
|||||||
|
|
||||||
override fun toString() = "Parameterised(${prettyPrint()})"
|
override fun toString() = "Parameterised(${prettyPrint()})"
|
||||||
override fun getLocalType(classLoader: ClassLoader): Type {
|
override fun getLocalType(classLoader: ClassLoader): Type {
|
||||||
val rawType = classLoader.loadClass(name)
|
val rawType = Class.forName(name, false, classLoader)
|
||||||
if (rawType.typeParameters.size != parameters.size) {
|
if (rawType.typeParameters.size != parameters.size) {
|
||||||
throw IncompatibleTypeIdentifierException(
|
throw IncompatibleTypeIdentifierException(
|
||||||
"Class $rawType expects ${rawType.typeParameters.size} type arguments, " +
|
"Class $rawType expects ${rawType.typeParameters.size} type arguments, " +
|
||||||
|
Loading…
x
Reference in New Issue
Block a user