Standardise Public Key hash size as per discussions with KC.

This commit is contained in:
josecoll 2017-10-17 14:00:58 +01:00
parent cda3ecbc0a
commit b861f4b7cb
4 changed files with 31 additions and 20 deletions

View File

@ -12,6 +12,7 @@ import net.corda.core.serialization.SingletonSerializeAsToken
import net.corda.core.utilities.debug import net.corda.core.utilities.debug
import net.corda.core.utilities.loggerFor import net.corda.core.utilities.loggerFor
import net.corda.node.utilities.AppendOnlyPersistentMap import net.corda.node.utilities.AppendOnlyPersistentMap
import net.corda.node.utilities.MAX_HASH_HEX_SIZE
import net.corda.node.utilities.NODE_DATABASE_PREFIX import net.corda.node.utilities.NODE_DATABASE_PREFIX
import org.bouncycastle.cert.X509CertificateHolder import org.bouncycastle.cert.X509CertificateHolder
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
@ -72,8 +73,8 @@ class PersistentIdentityService(identities: Iterable<PartyAndCertificate> = empt
@javax.persistence.Table(name = "${NODE_DATABASE_PREFIX}identities") @javax.persistence.Table(name = "${NODE_DATABASE_PREFIX}identities")
class PersistentIdentity( class PersistentIdentity(
@Id @Id
@Column(name = "pk_hash", length = 64) @Column(name = "pk_hash", length = MAX_HASH_HEX_SIZE)
var publicKeyHash: String = "", var publicKeyHash: String,
@Lob @Lob
@Column @Column
@ -87,7 +88,7 @@ class PersistentIdentityService(identities: Iterable<PartyAndCertificate> = empt
@Column(name = "name", length = 128) @Column(name = "name", length = 128)
var name: String = "", var name: String = "",
@Column(name = "pk_hash", length = 64) @Column(name = "pk_hash", length = MAX_HASH_HEX_SIZE)
var publicKeyHash: String = "" var publicKeyHash: String = ""
) )

View File

@ -8,10 +8,8 @@ import net.corda.core.serialization.SerializationDefaults
import net.corda.core.serialization.SingletonSerializeAsToken import net.corda.core.serialization.SingletonSerializeAsToken
import net.corda.core.serialization.deserialize import net.corda.core.serialization.deserialize
import net.corda.core.serialization.serialize import net.corda.core.serialization.serialize
import net.corda.core.utilities.hexToBase58
import net.corda.core.utilities.parsePublicKeyBase58
import net.corda.core.utilities.toBase58String
import net.corda.node.utilities.AppendOnlyPersistentMap import net.corda.node.utilities.AppendOnlyPersistentMap
import net.corda.node.utilities.MAX_HASH_HEX_SIZE
import net.corda.node.utilities.NODE_DATABASE_PREFIX import net.corda.node.utilities.NODE_DATABASE_PREFIX
import org.bouncycastle.operator.ContentSigner import org.bouncycastle.operator.ContentSigner
import java.security.KeyPair import java.security.KeyPair
@ -36,27 +34,31 @@ class PersistentKeyManagementService(val identityService: IdentityService,
@javax.persistence.Table(name = "${NODE_DATABASE_PREFIX}our_key_pairs") @javax.persistence.Table(name = "${NODE_DATABASE_PREFIX}our_key_pairs")
class PersistentKey( class PersistentKey(
@Id @Id
@Column(name = "public_key_hash", length = 64) @Column(name = "public_key_hash", length = MAX_HASH_HEX_SIZE)
var publicKeyHash: String, var publicKeyHash: String,
@Lob
@Column(name = "public_key")
var publicKey: ByteArray = ByteArray(0),
@Lob @Lob
@Column(name = "private_key") @Column(name = "private_key")
var privateKey: ByteArray = ByteArray(0) var privateKey: ByteArray = ByteArray(0)
) { ) {
constructor(publicKey: PublicKey, privateKey: ByteArray) constructor(publicKey: PublicKey, privateKey: PrivateKey)
: this(publicKey.encoded.sha256().toString(), privateKey) : this(publicKey.toStringShort(),
publicKey.serialize(context = SerializationDefaults.STORAGE_CONTEXT).bytes,
privateKey.serialize(context = SerializationDefaults.STORAGE_CONTEXT).bytes)
} }
private companion object { private companion object {
fun createKeyMap(): AppendOnlyPersistentMap<PublicKey, PrivateKey, PersistentKey, String> { fun createKeyMap(): AppendOnlyPersistentMap<PublicKey, PrivateKey, PersistentKey, String> {
return AppendOnlyPersistentMap( return AppendOnlyPersistentMap(
toPersistentEntityKey = { it.toBase58String() }, toPersistentEntityKey = { it.toStringShort() },
fromPersistentEntity = { fromPersistentEntity = { Pair(it.publicKey.deserialize(context = SerializationDefaults.STORAGE_CONTEXT),
Pair(parsePublicKeyBase58(it.publicKeyHash.hexToBase58()), it.privateKey.deserialize(context = SerializationDefaults.STORAGE_CONTEXT)) },
it.privateKey.deserialize(context = SerializationDefaults.STORAGE_CONTEXT))
},
toPersistentEntity = { key: PublicKey, value: PrivateKey -> toPersistentEntity = { key: PublicKey, value: PrivateKey ->
PersistentKey(key, value.serialize(context = SerializationDefaults.STORAGE_CONTEXT).bytes) PersistentKey(key, value)
}, },
persistentEntityClass = PersistentKey::class.java persistentEntityClass = PersistentKey::class.java
) )

View File

@ -1,7 +1,6 @@
package net.corda.node.services.network package net.corda.node.services.network
import net.corda.core.crypto.SecureHash import net.corda.core.crypto.toStringShort
import net.corda.core.crypto.sha256
import net.corda.core.identity.PartyAndCertificate import net.corda.core.identity.PartyAndCertificate
import net.corda.core.internal.ThreadBox import net.corda.core.internal.ThreadBox
import net.corda.core.messaging.SingleMessageRecipient import net.corda.core.messaging.SingleMessageRecipient
@ -10,6 +9,7 @@ import net.corda.core.serialization.deserialize
import net.corda.core.serialization.serialize import net.corda.core.serialization.serialize
import net.corda.node.services.api.NetworkMapCacheInternal import net.corda.node.services.api.NetworkMapCacheInternal
import net.corda.node.services.messaging.MessagingService import net.corda.node.services.messaging.MessagingService
import net.corda.node.utilities.MAX_HASH_HEX_SIZE
import net.corda.node.utilities.NODE_DATABASE_PREFIX import net.corda.node.utilities.NODE_DATABASE_PREFIX
import net.corda.node.utilities.PersistentMap import net.corda.node.utilities.PersistentMap
import net.corda.nodeapi.ArtemisMessagingComponent import net.corda.nodeapi.ArtemisMessagingComponent
@ -34,7 +34,7 @@ class PersistentNetworkMapService(network: MessagingService, networkMapCache: Ne
@Table(name = "${NODE_DATABASE_PREFIX}network_map_nodes") @Table(name = "${NODE_DATABASE_PREFIX}network_map_nodes")
class NetworkNode( class NetworkNode(
@Id @Id
@Column(name = "node_party_key_hash", length = 64) @Column(name = "node_party_key_hash", length = MAX_HASH_HEX_SIZE)
var publicKeyHash: String, var publicKeyHash: String,
@Column @Column
@ -61,14 +61,14 @@ class PersistentNetworkMapService(network: MessagingService, networkMapCache: Ne
fun createNetworkNodesMap(): PersistentMap<PartyAndCertificate, NodeRegistrationInfo, NetworkNode, String> { fun createNetworkNodesMap(): PersistentMap<PartyAndCertificate, NodeRegistrationInfo, NetworkNode, String> {
return PersistentMap( return PersistentMap(
toPersistentEntityKey = { SecureHash.sha256(it.owningKey.encoded).toString() }, toPersistentEntityKey = { it.owningKey.toStringShort() },
fromPersistentEntity = { fromPersistentEntity = {
Pair(PartyAndCertificate(factory.generateCertPath(ByteArrayInputStream(it.nodeParty.certPath))), Pair(PartyAndCertificate(factory.generateCertPath(ByteArrayInputStream(it.nodeParty.certPath))),
it.registrationInfo.deserialize(context = SerializationDefaults.STORAGE_CONTEXT)) it.registrationInfo.deserialize(context = SerializationDefaults.STORAGE_CONTEXT))
}, },
toPersistentEntity = { key: PartyAndCertificate, value: NodeRegistrationInfo -> toPersistentEntity = { key: PartyAndCertificate, value: NodeRegistrationInfo ->
NetworkNode( NetworkNode(
publicKeyHash = key.owningKey.encoded.sha256().toString(), publicKeyHash = key.owningKey.toStringShort(),
nodeParty = NodeParty( nodeParty = NodeParty(
key.name.toString(), key.name.toString(),
key.certificate.encoded, key.certificate.encoded,

View File

@ -20,6 +20,14 @@ import java.util.concurrent.CopyOnWriteArrayList
*/ */
const val NODE_DATABASE_PREFIX = "node_" const val NODE_DATABASE_PREFIX = "node_"
/**
* The maximum supported field-size for hash HEX-encoded outputs (e.g. database fields).
* This value is enough to support hash functions with outputs up to 512 bits (e.g. SHA3-512), in which
* case 128 HEX characters are required.
* 130 was selected instead of 128, to allow for 2 extra characters that will be used as hash-scheme identifiers.
*/
const val MAX_HASH_HEX_SIZE = 130
//HikariDataSource implements Closeable which allows CordaPersistence to be Closeable //HikariDataSource implements Closeable which allows CordaPersistence to be Closeable
class CordaPersistence(var dataSource: HikariDataSource, private val schemaService: SchemaService, class CordaPersistence(var dataSource: HikariDataSource, private val schemaService: SchemaService,
private val createIdentityService: () -> IdentityService, databaseProperties: Properties) : Closeable { private val createIdentityService: () -> IdentityService, databaseProperties: Properties) : Closeable {