diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 2f4dc7fa74..cf28b922a1 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -28,12 +28,14 @@ + + + + - - @@ -184,4 +186,4 @@ - + \ No newline at end of file diff --git a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentCertificateSigningRequestStorage.kt b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentCertificateSigningRequestStorage.kt index ec5dc6bc4f..d8e78b422d 100644 --- a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentCertificateSigningRequestStorage.kt +++ b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentCertificateSigningRequestStorage.kt @@ -13,11 +13,11 @@ package com.r3.corda.networkmanage.common.persistence import com.r3.corda.networkmanage.common.persistence.entity.CertificateDataEntity import com.r3.corda.networkmanage.common.persistence.entity.CertificateSigningRequestEntity import com.r3.corda.networkmanage.common.utils.getCertRole -import com.r3.corda.networkmanage.common.utils.hashString import net.corda.core.crypto.Crypto.toSupportedPublicKey import net.corda.core.crypto.SecureHash import net.corda.core.identity.CordaX500Name import net.corda.core.internal.CertRole +import net.corda.core.internal.hash import net.corda.nodeapi.internal.crypto.x509Certificates import net.corda.nodeapi.internal.persistence.CordaPersistence import net.corda.nodeapi.internal.persistence.DatabaseTransaction @@ -70,7 +70,7 @@ class PersistentCertificateSigningRequestStorage(private val database: CordaPers val requestEntity = CertificateSigningRequestEntity( requestId = requestId, legalName = legalNameOrRejectMessage as? CordaX500Name, - publicKeyHash = toSupportedPublicKey(request.subjectPublicKeyInfo).hashString(), + publicKeyHash = toSupportedPublicKey(request.subjectPublicKeyInfo).hash, request = request, remark = legalNameOrRejectMessage as? String, modifiedBy = CertificateSigningRequestStorage.DOORMAN_SIGNATURE, @@ -136,7 +136,7 @@ class PersistentCertificateSigningRequestStorage(private val database: CordaPers session.createQuery( "select a.certificateData.certPath from ${CertificateSigningRequestEntity::class.java.name} a " + "where a.publicKeyHash = :publicKeyHash and a.status = 'DONE' and a.certificateData.certificateStatus = 'VALID'", CertPath::class.java) - .setParameter("publicKeyHash", publicKey.hashString()) + .setParameter("publicKeyHash", publicKey.hash.toString()) .uniqueResult() } } @@ -175,7 +175,7 @@ class PersistentCertificateSigningRequestStorage(private val database: CordaPers // What if we approved something by mistake. nonRejectedRequestExists(CertificateSigningRequestEntity::legalName.name, legalName) -> throw RequestValidationException(legalName.toString(), "Duplicate legal name") //TODO Consider following scenario: There is a CSR that is signed but the certificate itself has expired or was revoked - nonRejectedRequestExists(CertificateSigningRequestEntity::publicKeyHash.name, toSupportedPublicKey(request.subjectPublicKeyInfo).hashString()) -> throw RequestValidationException(legalName.toString(), "Duplicate public key") + nonRejectedRequestExists(CertificateSigningRequestEntity::publicKeyHash.name, toSupportedPublicKey(request.subjectPublicKeyInfo).hash) -> throw RequestValidationException(legalName.toString(), "Duplicate public key") else -> legalName } } diff --git a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentNodeInfoStorage.kt b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentNodeInfoStorage.kt index 24b00ccc8e..df4cc7c7cc 100644 --- a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentNodeInfoStorage.kt +++ b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentNodeInfoStorage.kt @@ -14,12 +14,12 @@ import com.r3.corda.networkmanage.common.persistence.entity.CertificateSigningRe import com.r3.corda.networkmanage.common.persistence.entity.NodeInfoEntity import com.r3.corda.networkmanage.common.persistence.entity.ParametersUpdateEntity import com.r3.corda.networkmanage.common.persistence.entity.UpdateStatus -import com.r3.corda.networkmanage.common.utils.hashString import com.r3.corda.networkmanage.common.utils.logger import net.corda.core.crypto.SecureHash import net.corda.core.crypto.sha256 import net.corda.core.internal.CertRole import net.corda.core.internal.CertRole.NODE_CA +import net.corda.core.internal.hash import net.corda.nodeapi.internal.NodeInfoAndSigned import net.corda.nodeapi.internal.SignedNodeInfo import net.corda.nodeapi.internal.crypto.x509Certificates @@ -67,7 +67,7 @@ class PersistentNodeInfoStorage(private val database: CordaPersistence) : NodeIn session.saveOrUpdate(NodeInfoEntity( nodeInfoHash = nodeInfoHash.toString(), - publicKeyHash = nodeInfo.legalIdentities[0].owningKey.hashString(), + publicKeyHash = nodeInfo.legalIdentities[0].owningKey.hash, certificateSigningRequest = request, signedNodeInfo = signedNodeInfo, isCurrent = true, @@ -101,7 +101,7 @@ class PersistentNodeInfoStorage(private val database: CordaPersistence) : NodeIn return database.transaction { val nodeInfoEntity = session.fromQuery( "n where n.publicKeyHash = :publicKeyHash and isCurrent = true") - .setParameter("publicKeyHash", publicKey.hashString()) + .setParameter("publicKeyHash", publicKey.hash) .singleResult val parametersUpdateEntity = session.fromQuery( "u where u.networkParameters.hash = :acceptedParametersHash"). @@ -116,7 +116,7 @@ class PersistentNodeInfoStorage(private val database: CordaPersistence) : NodeIn private fun DatabaseTransaction.getSignedRequestByPublicHash(publicKeyHash: SecureHash): CertificateSigningRequestEntity? { return uniqueEntityWhere { builder, path -> - val publicKeyEq = builder.equal(path.get(CertificateSigningRequestEntity::publicKeyHash.name), publicKeyHash.toString()) + val publicKeyEq = builder.equal(path.get(CertificateSigningRequestEntity::publicKeyHash.name), publicKeyHash) val statusEq = builder.equal(path.get(CertificateSigningRequestEntity::status.name), RequestStatus.DONE) builder.and(publicKeyEq, statusEq) } diff --git a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/AttributeConverters.kt b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/AttributeConverters.kt index 3902113fcf..576156053d 100644 --- a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/AttributeConverters.kt +++ b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/AttributeConverters.kt @@ -1,6 +1,8 @@ package com.r3.corda.networkmanage.common.persistence.entity import com.r3.corda.networkmanage.common.utils.buildCertPath +import net.corda.core.crypto.SecureHash +import net.corda.core.identity.CordaX500Name import net.corda.core.node.NetworkParameters import net.corda.core.serialization.SerializationFactory import net.corda.core.serialization.serialize @@ -13,7 +15,6 @@ import java.security.cert.CertPath import java.security.cert.X509CRL import java.security.cert.X509Certificate import javax.persistence.AttributeConverter -import net.corda.core.identity.CordaX500Name class PKCS10CertificationRequestConverter : AttributeConverter { override fun convertToEntityAttribute(dbData: ByteArray?): PKCS10CertificationRequest? = dbData?.let(::PKCS10CertificationRequest) @@ -45,7 +46,11 @@ class NetworkMapConverter : CordaSerializationConverter(NetworkMap:: class SignedNodeInfoConverter : CordaSerializationConverter(SignedNodeInfo::class.java) -sealed class CordaSerializationConverter(private val clazz: Class) : AttributeConverter { +class CordaX500NameAttributeConverter : ToStringConverter(CordaX500Name.Companion::parse) + +class SecureHashAttributeConverter : ToStringConverter(SecureHash.Companion::parse) + +abstract class CordaSerializationConverter(private val clazz: Class) : AttributeConverter { override fun convertToEntityAttribute(dbData: ByteArray?): T? { return dbData?.let { val serializationFactory = SerializationFactory.defaultFactory @@ -56,14 +61,7 @@ sealed class CordaSerializationConverter(private val clazz: Class) : override fun convertToDatabaseColumn(attribute: T?): ByteArray? = attribute?.serialize()?.bytes } -class CordaX500NameAttributeConverter : AttributeConverter { - override fun convertToDatabaseColumn(attribute: CordaX500Name?): String? = attribute?.toString() - override fun convertToEntityAttribute(dbData: String?): CordaX500Name? = dbData?.let { CordaX500Name.parse(it) } +abstract class ToStringConverter(private val parser: (String) -> T) : AttributeConverter { + override fun convertToDatabaseColumn(attribute: T?): String? = attribute?.toString() + override fun convertToEntityAttribute(dbData: String?): T? = dbData?.let { parser(it) } } - -// TODO Use SecureHash in entities -//class SecureHashAttributeConverter : AttributeConverter { -// override fun convertToDatabaseColumn(attribute: SecureHash?): String? = attribute?.toString() -// override fun convertToEntityAttribute(dbData: String?): SecureHash? = dbData?.let { SecureHash.parse(it) } -//} - diff --git a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/CertificateSigningRequestEntity.kt b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/CertificateSigningRequestEntity.kt index 96661f4594..4834ea5019 100644 --- a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/CertificateSigningRequestEntity.kt +++ b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/CertificateSigningRequestEntity.kt @@ -38,7 +38,8 @@ data class CertificateSigningRequestEntity( val legalName: CordaX500Name?, @Column(name = "public_key_hash", length = 64, nullable = false) - val publicKeyHash: String, + @Convert(converter = SecureHashAttributeConverter::class) + val publicKeyHash: SecureHash, // Setting [columnDefinition] is a work around for a hibernate problem when using SQL database. // TODO: Remove this when we find out the cause of the problem. @@ -75,7 +76,7 @@ data class CertificateSigningRequestEntity( return CertificateSigningRequest( requestId = requestId, legalName = legalName, - publicKeyHash = SecureHash.parse(publicKeyHash), + publicKeyHash = publicKeyHash, status = status, request = request, remark = remark, diff --git a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/NetworkParametersEntity.kt b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/NetworkParametersEntity.kt index 3c8424e95f..31c3f8b6af 100644 --- a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/NetworkParametersEntity.kt +++ b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/NetworkParametersEntity.kt @@ -24,6 +24,7 @@ import javax.persistence.* class NetworkParametersEntity( @Id @Column(name = "hash", length = 64, nullable = false) + // AttributeConverters can't be used on @Id attributes, otherwise this would be SecureHash val hash: String, @Column(nullable = false) diff --git a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/NodeInfoEntity.kt b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/NodeInfoEntity.kt index b28f9eda00..100973b572 100644 --- a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/NodeInfoEntity.kt +++ b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/NodeInfoEntity.kt @@ -10,6 +10,7 @@ package com.r3.corda.networkmanage.common.persistence.entity +import net.corda.core.crypto.SecureHash import net.corda.nodeapi.internal.SignedNodeInfo import java.io.Serializable import java.time.Instant @@ -21,10 +22,12 @@ data class NodeInfoEntity( // Hash of serialized [NodeInfo] without signatures. @Id @Column(name = "node_info_hash", length = 64) + // AttributeConverters can't be used on @Id attributes, otherwise this would be SecureHash val nodeInfoHash: String, @Column(name = "public_key_hash", length = 64) - val publicKeyHash: String, + @Convert(converter = SecureHashAttributeConverter::class) + val publicKeyHash: SecureHash, @ManyToOne(optional = false, fetch = FetchType.LAZY) @JoinColumn(name = "cert_signing_request", nullable = false) diff --git a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/utils/Utils.kt b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/utils/Utils.kt index 8fb7e72c9b..3028e2a37e 100644 --- a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/utils/Utils.kt +++ b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/utils/Utils.kt @@ -46,12 +46,6 @@ data class CertPathAndKey(val certPath: List, val key: PrivateK fun toKeyPair(): KeyPair = KeyPair(certPath[0].publicKey, key) } -// TODO: replace this with Crypto.hash when its available. -/** - * Returns SHA256 hash of this public key - */ -fun PublicKey.hashString() = encoded.sha256().toString() - inline fun parseConfig(file: Path): T { val config = ConfigFactory.parseFile(file.toFile(), ConfigParseOptions.defaults().setAllowMissing(true)).resolve() logger.info(config.root().render(ConfigRenderOptions.defaults())) diff --git a/network-management/src/main/kotlin/com/r3/corda/networkmanage/doorman/webservice/CertificateRevocationRequestWebService.kt b/network-management/src/main/kotlin/com/r3/corda/networkmanage/doorman/webservice/CertificateRevocationRequestWebService.kt index 310b94bbe5..7b683aba8e 100644 --- a/network-management/src/main/kotlin/com/r3/corda/networkmanage/doorman/webservice/CertificateRevocationRequestWebService.kt +++ b/network-management/src/main/kotlin/com/r3/corda/networkmanage/doorman/webservice/CertificateRevocationRequestWebService.kt @@ -2,7 +2,7 @@ package com.r3.corda.networkmanage.doorman.webservice import com.r3.corda.networkmanage.doorman.signer.CrrHandler import com.r3.corda.networkmanage.doorman.webservice.CertificateRevocationRequestWebService.Companion.CRR_PATH -import net.corda.core.serialization.deserialize +import net.corda.core.internal.readObject import net.corda.core.utilities.contextLogger import net.corda.nodeapi.internal.network.CertificateRevocationRequest import java.io.InputStream @@ -28,7 +28,7 @@ class CertificateRevocationRequestWebService(private val crrHandler: CrrHandler) @Produces(MediaType.TEXT_PLAIN) fun submitRequest(input: InputStream): Response { return try { - val request = input.readBytes().deserialize() + val request = input.readObject() val requestId = crrHandler.saveRevocationRequest(request) ok(requestId) } catch (e: Exception) { diff --git a/network-management/src/main/kotlin/com/r3/corda/networkmanage/doorman/webservice/NetworkMapWebService.kt b/network-management/src/main/kotlin/com/r3/corda/networkmanage/doorman/webservice/NetworkMapWebService.kt index d3e4eac3e7..8e3a1c3e77 100644 --- a/network-management/src/main/kotlin/com/r3/corda/networkmanage/doorman/webservice/NetworkMapWebService.kt +++ b/network-management/src/main/kotlin/com/r3/corda/networkmanage/doorman/webservice/NetworkMapWebService.kt @@ -22,9 +22,9 @@ import net.corda.core.crypto.SecureHash import net.corda.core.crypto.SignedData import net.corda.core.crypto.sha256 import net.corda.core.internal.CertRole +import net.corda.core.internal.readObject import net.corda.core.node.NetworkParameters import net.corda.core.node.NodeInfo -import net.corda.core.serialization.deserialize import net.corda.core.serialization.serialize import net.corda.core.utilities.contextLogger import net.corda.core.utilities.debug @@ -84,7 +84,7 @@ class NetworkMapWebService(private val nodeInfoStorage: NodeInfoStorage, @Path("publish") @Consumes(MediaType.APPLICATION_OCTET_STREAM) fun registerNode(input: InputStream): Response { - val signedNodeInfo = input.readBytes().deserialize() + val signedNodeInfo = input.readObject() var nodeInfo: NodeInfo? = null return try { // Store the NodeInfo @@ -111,7 +111,7 @@ class NetworkMapWebService(private val nodeInfoStorage: NodeInfoStorage, @Consumes(MediaType.APPLICATION_OCTET_STREAM) fun ackNetworkParameters(input: InputStream): Response { return try { - val signedParametersHash = input.readBytes().deserialize>() + val signedParametersHash = input.readObject>() val hash = signedParametersHash.verified() networkMapStorage.getSignedNetworkParameters(hash) ?: throw IllegalArgumentException("No network parameters with hash $hash") logger.debug { "Received ack-parameters with $hash from ${signedParametersHash.sig.by}" } diff --git a/network-management/src/main/kotlin/com/r3/corda/networkmanage/doorman/webservice/RegistrationWebService.kt b/network-management/src/main/kotlin/com/r3/corda/networkmanage/doorman/webservice/RegistrationWebService.kt index 45e9fa949c..5a47d45602 100644 --- a/network-management/src/main/kotlin/com/r3/corda/networkmanage/doorman/webservice/RegistrationWebService.kt +++ b/network-management/src/main/kotlin/com/r3/corda/networkmanage/doorman/webservice/RegistrationWebService.kt @@ -12,6 +12,7 @@ package com.r3.corda.networkmanage.doorman.webservice import com.r3.corda.networkmanage.common.persistence.CertificateResponse import com.r3.corda.networkmanage.doorman.signer.CsrHandler +import net.corda.core.internal.readFully import net.corda.nodeapi.internal.crypto.X509Utilities.CORDA_CLIENT_CA import net.corda.nodeapi.internal.crypto.X509Utilities.CORDA_INTERMEDIATE_CA import net.corda.nodeapi.internal.crypto.X509Utilities.CORDA_ROOT_CA @@ -45,7 +46,7 @@ class RegistrationWebService(private val csrHandler: CsrHandler, private val cli @Consumes(MediaType.APPLICATION_OCTET_STREAM) @Produces(MediaType.TEXT_PLAIN) fun submitRequest(input: InputStream): Response { - val csr = input.use { JcaPKCS10CertificationRequest(it.readBytes()) } + val csr = JcaPKCS10CertificationRequest(input.readFully()) if (!csr.isSignatureValid()) { return status(Response.Status.BAD_REQUEST).entity("Invalid CSR signature").build() } diff --git a/network-management/src/main/kotlin/com/r3/corda/networkmanage/hsm/authentication/Authenticator.kt b/network-management/src/main/kotlin/com/r3/corda/networkmanage/hsm/authentication/Authenticator.kt index ec988d2282..5fcda421d5 100644 --- a/network-management/src/main/kotlin/com/r3/corda/networkmanage/hsm/authentication/Authenticator.kt +++ b/network-management/src/main/kotlin/com/r3/corda/networkmanage/hsm/authentication/Authenticator.kt @@ -12,7 +12,6 @@ package com.r3.corda.networkmanage.hsm.authentication import CryptoServerJCE.CryptoServerProvider import com.r3.corda.networkmanage.common.signer.AuthenticationException -import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.nio.file.Path import kotlin.reflect.full.memberProperties @@ -155,9 +154,6 @@ fun createProvider(config: CryptoServerProviderConfig): CryptoServerProvider { writer.write("${property.name} = ${property.get(config)}\n") } writer.close() - val cfg = ByteArrayInputStream(cfgBuffer.toByteArray()) - cfgBuffer.close() - val provider = CryptoServerProvider(cfg) - cfg.close() - return provider + val cfg = cfgBuffer.toByteArray().inputStream() + return CryptoServerProvider(cfg) } \ No newline at end of file diff --git a/network-management/src/test/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentNetworkMapStorageTest.kt b/network-management/src/test/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentNetworkMapStorageTest.kt index 5a3676bb16..4f41e03d75 100644 --- a/network-management/src/test/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentNetworkMapStorageTest.kt +++ b/network-management/src/test/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentNetworkMapStorageTest.kt @@ -14,9 +14,7 @@ import com.r3.corda.networkmanage.TestBase import com.r3.corda.networkmanage.common.persistence.entity.NodeInfoEntity import com.r3.corda.networkmanage.common.persistence.entity.ParametersUpdateEntity import com.r3.corda.networkmanage.common.persistence.entity.UpdateStatus -import com.r3.corda.networkmanage.common.utils.hashString import net.corda.core.crypto.SecureHash -import net.corda.core.crypto.sha256 import net.corda.core.serialization.serialize import net.corda.core.utilities.days import net.corda.nodeapi.internal.createDevNetworkMapCa diff --git a/network-management/src/test/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentNodeInfoStorageTest.kt b/network-management/src/test/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentNodeInfoStorageTest.kt index dafe94172d..143b9216c5 100644 --- a/network-management/src/test/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentNodeInfoStorageTest.kt +++ b/network-management/src/test/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentNodeInfoStorageTest.kt @@ -12,12 +12,11 @@ package com.r3.corda.networkmanage.common.persistence import com.r3.corda.networkmanage.TestBase import com.r3.corda.networkmanage.common.persistence.entity.NodeInfoEntity -import com.r3.corda.networkmanage.common.utils.hashString import net.corda.core.crypto.Crypto -import net.corda.core.crypto.SecureHash import net.corda.core.crypto.sha256 import net.corda.core.identity.CordaX500Name import net.corda.core.internal.CertRole +import net.corda.core.internal.hash import net.corda.core.serialization.serialize import net.corda.core.utilities.days import net.corda.nodeapi.internal.NodeInfoAndSigned @@ -86,14 +85,14 @@ class PersistentNodeInfoStorageTest : TestBase() { requestStorage.markRequestTicketCreated(requestId) requestStorage.approveRequest(requestId, CertificateSigningRequestStorage.DOORMAN_SIGNATURE) - assertNull(nodeInfoStorage.getCertificatePath(SecureHash.parse(keyPair.public.hashString()))) + assertNull(nodeInfoStorage.getCertificatePath(keyPair.public.hash)) requestStorage.putCertificatePath( requestId, X509Utilities.buildCertPath(nodeCaCert, doormanCertAndKeyPair.certificate, rootCaCert), CertificateSigningRequestStorage.DOORMAN_SIGNATURE) - val storedCertPath = nodeInfoStorage.getCertificatePath(SecureHash.parse(keyPair.public.hashString())) + val storedCertPath = nodeInfoStorage.getCertificatePath(keyPair.public.hash) assertNotNull(storedCertPath) assertEquals(nodeCaCert, storedCertPath!!.certificates.first())