mirror of
https://github.com/corda/corda.git
synced 2025-02-06 11:09:18 +00:00
* Remove Kryo from ServiceIdentityGenerator (#1083)
* Store encoded private, public and composite key to file instead of Party and Key using Kryo.
This commit is contained in:
parent
9d3ad5fe06
commit
0e0b99eaf0
@ -718,11 +718,12 @@ abstract class AbstractNode(open val configuration: NodeConfiguration,
|
|||||||
if (!keyStore.containsAlias(privateKeyAlias)) {
|
if (!keyStore.containsAlias(privateKeyAlias)) {
|
||||||
val privKeyFile = configuration.baseDirectory / privateKeyAlias
|
val privKeyFile = configuration.baseDirectory / privateKeyAlias
|
||||||
val pubIdentityFile = configuration.baseDirectory / "$serviceId-public"
|
val pubIdentityFile = configuration.baseDirectory / "$serviceId-public"
|
||||||
|
val compositeKeyFile = configuration.baseDirectory / compositeKeyAlias
|
||||||
// TODO: Remove use of [ServiceIdentityGenerator.generateToDisk].
|
// TODO: Remove use of [ServiceIdentityGenerator.generateToDisk].
|
||||||
// Get keys from key file.
|
// Get keys from key file.
|
||||||
// TODO: this is here to smooth out the key storage transition, remove this migration in future release.
|
// TODO: this is here to smooth out the key storage transition, remove this migration in future release.
|
||||||
if (privKeyFile.exists()) {
|
if (privKeyFile.exists()) {
|
||||||
migrateKeysFromFile(keyStore, serviceName, pubIdentityFile, privKeyFile, privateKeyAlias, compositeKeyAlias)
|
migrateKeysFromFile(keyStore, serviceName, pubIdentityFile, privKeyFile, compositeKeyFile, privateKeyAlias, compositeKeyAlias)
|
||||||
} else {
|
} else {
|
||||||
log.info("$privateKeyAlias not found in keystore ${configuration.nodeKeystore}, generating fresh key!")
|
log.info("$privateKeyAlias not found in keystore ${configuration.nodeKeystore}, generating fresh key!")
|
||||||
keyStore.saveNewKeyPair(serviceName, privateKeyAlias, generateKeyPair())
|
keyStore.saveNewKeyPair(serviceName, privateKeyAlias, generateKeyPair())
|
||||||
@ -750,21 +751,17 @@ abstract class AbstractNode(open val configuration: NodeConfiguration,
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun migrateKeysFromFile(keyStore: KeyStoreWrapper, serviceName: X500Name,
|
private fun migrateKeysFromFile(keyStore: KeyStoreWrapper, serviceName: X500Name,
|
||||||
pubIdentityFile: Path, privKeyFile: Path,
|
pubKeyFile: Path, privKeyFile: Path, compositeKeyFile:Path,
|
||||||
privateKeyAlias: String, compositeKeyAlias: String) {
|
privateKeyAlias: String, compositeKeyAlias: String) {
|
||||||
log.info("Migrating $privateKeyAlias from file to keystore...")
|
log.info("Migrating $privateKeyAlias from file to keystore...")
|
||||||
val myIdentity = pubIdentityFile.readAll().deserialize<Party>()
|
|
||||||
// Check that the identity in the config file matches the identity file we have stored to disk.
|
// Check that the identity in the config file matches the identity file we have stored to disk.
|
||||||
// This is just a sanity check. It shouldn't fail unless the admin has fiddled with the files and messed
|
|
||||||
// things up for us.
|
|
||||||
if (myIdentity.name != serviceName)
|
|
||||||
throw ConfigurationException("The legal name in the config file doesn't match the stored identity file:$serviceName vs ${myIdentity.name}")
|
|
||||||
// Load the private key.
|
// Load the private key.
|
||||||
val keyPair = privKeyFile.readAll().deserialize<KeyPair>()
|
val publicKey = Crypto.decodePublicKey(pubKeyFile.readAll())
|
||||||
keyStore.saveNewKeyPair(serviceName, privateKeyAlias, keyPair)
|
val privateKey = Crypto.decodePrivateKey(privKeyFile.readAll())
|
||||||
|
keyStore.saveNewKeyPair(serviceName, privateKeyAlias, KeyPair(publicKey, privateKey))
|
||||||
// Store composite key separately.
|
// Store composite key separately.
|
||||||
if (myIdentity.owningKey is CompositeKey) {
|
if (compositeKeyFile.exists()) {
|
||||||
keyStore.savePublicKey(serviceName, compositeKeyAlias, myIdentity.owningKey)
|
keyStore.savePublicKey(serviceName, compositeKeyAlias, Crypto.decodePublicKey(compositeKeyFile.readAll()))
|
||||||
}
|
}
|
||||||
log.info("Finish migrating $privateKeyAlias from file to keystore.")
|
log.info("Finish migrating $privateKeyAlias from file to keystore.")
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,6 @@ package net.corda.node.utilities
|
|||||||
import net.corda.core.crypto.composite.CompositeKey
|
import net.corda.core.crypto.composite.CompositeKey
|
||||||
import net.corda.core.crypto.generateKeyPair
|
import net.corda.core.crypto.generateKeyPair
|
||||||
import net.corda.core.identity.Party
|
import net.corda.core.identity.Party
|
||||||
import net.corda.core.serialization.serialize
|
|
||||||
import net.corda.core.serialization.storageKryo
|
|
||||||
import net.corda.core.utilities.loggerFor
|
import net.corda.core.utilities.loggerFor
|
||||||
import net.corda.core.utilities.trace
|
import net.corda.core.utilities.trace
|
||||||
import org.bouncycastle.asn1.x500.X500Name
|
import org.bouncycastle.asn1.x500.X500Name
|
||||||
@ -33,16 +31,16 @@ object ServiceIdentityGenerator {
|
|||||||
val keyPairs = (1..dirs.size).map { generateKeyPair() }
|
val keyPairs = (1..dirs.size).map { generateKeyPair() }
|
||||||
val notaryKey = CompositeKey.Builder().addKeys(keyPairs.map { it.public }).build(threshold)
|
val notaryKey = CompositeKey.Builder().addKeys(keyPairs.map { it.public }).build(threshold)
|
||||||
// Avoid adding complexity! This class is a hack that needs to stay runnable in the gradle environment.
|
// Avoid adding complexity! This class is a hack that needs to stay runnable in the gradle environment.
|
||||||
val notaryParty = Party(serviceName, notaryKey)
|
|
||||||
val notaryPartyBytes = notaryParty.serialize()
|
|
||||||
val privateKeyFile = "$serviceId-private-key"
|
val privateKeyFile = "$serviceId-private-key"
|
||||||
val publicKeyFile = "$serviceId-public"
|
val publicKeyFile = "$serviceId-public"
|
||||||
|
val compositeKeyFile = "$serviceId-composite-key"
|
||||||
keyPairs.zip(dirs) { keyPair, dir ->
|
keyPairs.zip(dirs) { keyPair, dir ->
|
||||||
Files.createDirectories(dir)
|
Files.createDirectories(dir)
|
||||||
notaryPartyBytes.writeToFile(dir.resolve(publicKeyFile))
|
Files.write(dir.resolve(compositeKeyFile), notaryKey.encoded)
|
||||||
// Use storageKryo as our whitelist is not available in the gradle build environment:
|
// Use storageKryo as our whitelist is not available in the gradle build environment:
|
||||||
keyPair.serialize(storageKryo()).writeToFile(dir.resolve(privateKeyFile))
|
Files.write(dir.resolve(privateKeyFile), keyPair.private.encoded)
|
||||||
|
Files.write(dir.resolve(publicKeyFile), keyPair.public.encoded)
|
||||||
}
|
}
|
||||||
return notaryParty
|
return Party(serviceName, notaryKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user