Change keypair to "keyPair"/"key pair"

Change "keypair" to "keyPair"/"key pair" to correctly indicate it is two words,
not a single word.
This commit is contained in:
Ross Nicoll 2016-09-19 16:02:17 +01:00
parent 89ead30e82
commit 80ccf4df34
8 changed files with 44 additions and 45 deletions

View File

@ -177,7 +177,7 @@ fun PublicKey.toStringShort(): String {
fun Iterable<PublicKey>.toStringsShort(): String = map { it.toStringShort() }.toString() fun Iterable<PublicKey>.toStringsShort(): String = map { it.toStringShort() }.toString()
// Allow Kotlin destructuring: val (private, public) = keypair // Allow Kotlin destructuring: val (private, public) = keyPair
operator fun KeyPair.component1() = this.private operator fun KeyPair.component1() = this.private
operator fun KeyPair.component2() = this.public operator fun KeyPair.component2() = this.public

View File

@ -252,17 +252,16 @@ object X509Utilities {
return JcaPKCS10CertificationRequestBuilder(subject, keyPair.public).build(signer) return JcaPKCS10CertificationRequestBuilder(subject, keyPair.public).build(signer)
} }
/** /**
* Helper data class to pass around public certificate and KeyPair entities when using CA certs * Helper data class to pass around public certificate and [KeyPair] entities when using CA certs.
*/ */
data class CACertAndKey(val certificate: X509Certificate, val keypair: KeyPair) data class CACertAndKey(val certificate: X509Certificate, val keyPair: KeyPair)
/** /**
* Create a de novo root self-signed X509 v3 CA cert and KeyPair. * Create a de novo root self-signed X509 v3 CA cert and [KeyPair].
* @param domain The Common (CN) field of the cert Subject will be populated with the domain string * @param domain The Common (CN) field of the cert Subject will be populated with the domain string
* @return A data class is returned containing the new root CA Cert and its KeyPair for signing downstream certificates. * @return A data class is returned containing the new root CA Cert and its [KeyPair] for signing downstream certificates.
* Note the generated certificate tree is capped at max depth of 2 to be in line with commercially available certificates * Note the generated certificate tree is capped at max depth of 2 to be in line with commercially available certificates
*/ */
fun createSelfSignedCACert(myLegalName: String): CACertAndKey { fun createSelfSignedCACert(myLegalName: String): CACertAndKey {
@ -341,10 +340,10 @@ object X509Utilities {
builder.addExtension(Extension.extendedKeyUsage, false, builder.addExtension(Extension.extendedKeyUsage, false,
DERSequence(purposes)) DERSequence(purposes))
val cert = signCertificate(builder, certificateAuthority.keypair.private) val cert = signCertificate(builder, certificateAuthority.keyPair.private)
cert.checkValidity(Date()) cert.checkValidity(Date())
cert.verify(certificateAuthority.keypair.public) cert.verify(certificateAuthority.keyPair.public)
return CACertAndKey(cert, keyPair) return CACertAndKey(cert, keyPair)
} }
@ -404,10 +403,10 @@ object X509Utilities {
val subjectAlternativeNamesExtension = DERSequence(subjectAlternativeNames.toTypedArray()) val subjectAlternativeNamesExtension = DERSequence(subjectAlternativeNames.toTypedArray())
builder.addExtension(Extension.subjectAlternativeName, false, subjectAlternativeNamesExtension) builder.addExtension(Extension.subjectAlternativeName, false, subjectAlternativeNamesExtension)
val cert = signCertificate(builder, certificateAuthority.keypair.private) val cert = signCertificate(builder, certificateAuthority.keyPair.private)
cert.checkValidity(Date()) cert.checkValidity(Date())
cert.verify(certificateAuthority.keypair.public) cert.verify(certificateAuthority.keyPair.public)
return cert return cert
} }
@ -482,7 +481,7 @@ object X509Utilities {
if (!keyStore.containsAlias(alias)) { if (!keyStore.containsAlias(alias)) {
val selfSignCert = keyGenerator() val selfSignCert = keyGenerator()
// Save to the key store. // Save to the key store.
keyStore.addOrReplaceKey(alias, selfSignCert.keypair.private, keyPassword.toCharArray(), arrayOf(selfSignCert.certificate)) keyStore.addOrReplaceKey(alias, selfSignCert.keyPair.private, keyPassword.toCharArray(), arrayOf(selfSignCert.certificate))
X509Utilities.saveKeyStore(keyStore, keyStoreFilePath, storePassword) X509Utilities.saveKeyStore(keyStore, keyStoreFilePath, storePassword)
} }
@ -527,14 +526,14 @@ object X509Utilities {
val rootCA = X509Utilities.createSelfSignedCACert("Corda Node Root CA") val rootCA = X509Utilities.createSelfSignedCACert("Corda Node Root CA")
val intermediateCA = X509Utilities.createIntermediateCert("Corda Node Intermediate CA", rootCA) val intermediateCA = X509Utilities.createIntermediateCert("Corda Node Intermediate CA", rootCA)
val keypass = keyPassword.toCharArray() val keyPass = keyPassword.toCharArray()
val keyStore = loadOrCreateKeyStore(keyStoreFilePath, storePassword) val keyStore = loadOrCreateKeyStore(keyStoreFilePath, storePassword)
keyStore.addOrReplaceKey(CORDA_ROOT_CA_PRIVATE_KEY, rootCA.keypair.private, keypass, arrayOf(rootCA.certificate)) keyStore.addOrReplaceKey(CORDA_ROOT_CA_PRIVATE_KEY, rootCA.keyPair.private, keyPass, arrayOf(rootCA.certificate))
keyStore.addOrReplaceKey(CORDA_INTERMEDIATE_CA_PRIVATE_KEY, keyStore.addOrReplaceKey(CORDA_INTERMEDIATE_CA_PRIVATE_KEY,
intermediateCA.keypair.private, intermediateCA.keyPair.private,
keypass, keyPass,
arrayOf(intermediateCA.certificate, rootCA.certificate)) arrayOf(intermediateCA.certificate, rootCA.certificate))
saveKeyStore(keyStore, keyStoreFilePath, storePassword) saveKeyStore(keyStore, keyStoreFilePath, storePassword)
@ -560,8 +559,8 @@ object X509Utilities {
fun loadCertificateAndKey(keyStore: KeyStore, fun loadCertificateAndKey(keyStore: KeyStore,
keyPassword: String, keyPassword: String,
alias: String): CACertAndKey { alias: String): CACertAndKey {
val keypass = keyPassword.toCharArray() val keyPass = keyPassword.toCharArray()
val key = keyStore.getKey(alias, keypass) as PrivateKey val key = keyStore.getKey(alias, keyPass) as PrivateKey
val cert = keyStore.getCertificate(alias) as X509Certificate val cert = keyStore.getCertificate(alias) as X509Certificate
return CACertAndKey(cert, KeyPair(cert.publicKey, key)) return CACertAndKey(cert, KeyPair(cert.publicKey, key))
} }
@ -597,12 +596,12 @@ object X509Utilities {
if (host.canonicalHostName == host.hostName) listOf() else listOf(host.hostName), if (host.canonicalHostName == host.hostName) listOf() else listOf(host.hostName),
listOf(host.hostAddress)) listOf(host.hostAddress))
val keypass = keyPassword.toCharArray() val keyPass = keyPassword.toCharArray()
val keyStore = loadOrCreateKeyStore(keyStoreFilePath, storePassword) val keyStore = loadOrCreateKeyStore(keyStoreFilePath, storePassword)
keyStore.addOrReplaceKey(CORDA_CLIENT_CA_PRIVATE_KEY, keyStore.addOrReplaceKey(CORDA_CLIENT_CA_PRIVATE_KEY,
serverKey.private, serverKey.private,
keypass, keyPass,
arrayOf(serverCert, intermediateCA.certificate, rootCA.certificate)) arrayOf(serverCert, intermediateCA.certificate, rootCA.certificate))
keyStore.addOrReplaceCertificate(CORDA_CLIENT_CA, serverCert) keyStore.addOrReplaceCertificate(CORDA_CLIENT_CA, serverCert)

View File

@ -33,7 +33,7 @@ class X509UtilitiesTest {
assertTrue { caCertAndKey.certificate.subjectDN.name.contains("CN=Test Cert") } // using our subject common name assertTrue { caCertAndKey.certificate.subjectDN.name.contains("CN=Test Cert") } // using our subject common name
assertEquals(caCertAndKey.certificate.issuerDN, caCertAndKey.certificate.subjectDN) //self-signed assertEquals(caCertAndKey.certificate.issuerDN, caCertAndKey.certificate.subjectDN) //self-signed
caCertAndKey.certificate.checkValidity(Date()) // throws on verification problems caCertAndKey.certificate.checkValidity(Date()) // throws on verification problems
caCertAndKey.certificate.verify(caCertAndKey.keypair.public) // throws on verification problems caCertAndKey.certificate.verify(caCertAndKey.keyPair.public) // throws on verification problems
assertTrue { caCertAndKey.certificate.keyUsage[5] } // Bit 5 == keyCertSign according to ASN.1 spec (see full comment on KeyUsage property) assertTrue { caCertAndKey.certificate.keyUsage[5] } // Bit 5 == keyCertSign according to ASN.1 spec (see full comment on KeyUsage property)
assertTrue { caCertAndKey.certificate.basicConstraints > 0 } // This returns the signing path length Would be -1 for non-CA certificate assertTrue { caCertAndKey.certificate.basicConstraints > 0 } // This returns the signing path length Would be -1 for non-CA certificate
} }
@ -54,12 +54,12 @@ class X509UtilitiesTest {
fun `create valid server certificate chain`() { fun `create valid server certificate chain`() {
val caCertAndKey = X509Utilities.createSelfSignedCACert("Test CA Cert") val caCertAndKey = X509Utilities.createSelfSignedCACert("Test CA Cert")
val subjectDN = X509Utilities.getDevX509Name("Server Cert") val subjectDN = X509Utilities.getDevX509Name("Server Cert")
val keypair = X509Utilities.generateECDSAKeyPairForSSL() val keyPair = X509Utilities.generateECDSAKeyPairForSSL()
val serverCert = X509Utilities.createServerCert(subjectDN, keypair.public, caCertAndKey, listOf("alias name"), listOf("10.0.0.54")) val serverCert = X509Utilities.createServerCert(subjectDN, keyPair.public, caCertAndKey, listOf("alias name"), listOf("10.0.0.54"))
assertTrue { serverCert.subjectDN.name.contains("CN=Server Cert") } // using our subject common name assertTrue { serverCert.subjectDN.name.contains("CN=Server Cert") } // using our subject common name
assertEquals(caCertAndKey.certificate.issuerDN, serverCert.issuerDN) // Issued by our CA cert assertEquals(caCertAndKey.certificate.issuerDN, serverCert.issuerDN) // Issued by our CA cert
serverCert.checkValidity(Date()) // throws on verification problems serverCert.checkValidity(Date()) // throws on verification problems
serverCert.verify(caCertAndKey.keypair.public) // throws on verification problems serverCert.verify(caCertAndKey.keyPair.public) // throws on verification problems
assertFalse { serverCert.keyUsage[5] } // Bit 5 == keyCertSign according to ASN.1 spec (see full comment on KeyUsage property) assertFalse { serverCert.keyUsage[5] } // Bit 5 == keyCertSign according to ASN.1 spec (see full comment on KeyUsage property)
assertTrue { serverCert.basicConstraints === -1 } // This returns the signing path length should be -1 for non-CA certificate assertTrue { serverCert.basicConstraints === -1 } // This returns the signing path length should be -1 for non-CA certificate
assertEquals(3, serverCert.subjectAlternativeNames.size) assertEquals(3, serverCert.subjectAlternativeNames.size)
@ -166,7 +166,7 @@ class X509UtilitiesTest {
// Now sign something with private key and verify against certificate public key // Now sign something with private key and verify against certificate public key
val testData = "123456".toByteArray() val testData = "123456".toByteArray()
val signer = Signature.getInstance(X509Utilities.SIGNATURE_ALGORITHM) val signer = Signature.getInstance(X509Utilities.SIGNATURE_ALGORITHM)
signer.initSign(serverCertAndKey.keypair.private) signer.initSign(serverCertAndKey.keyPair.private)
signer.update(testData) signer.update(testData)
val signature = signer.sign() val signature = signer.sign()
val verifier = Signature.getInstance(X509Utilities.SIGNATURE_ALGORITHM) val verifier = Signature.getInstance(X509Utilities.SIGNATURE_ALGORITHM)

View File

@ -399,15 +399,15 @@ abstract class AbstractNode(val dir: Path, val configuration: NodeConfiguration,
val checkpointStorage = PerFileCheckpointStorage(dir.resolve("checkpoints")) val checkpointStorage = PerFileCheckpointStorage(dir.resolve("checkpoints"))
val transactionStorage = PerFileTransactionStorage(dir.resolve("transactions")) val transactionStorage = PerFileTransactionStorage(dir.resolve("transactions"))
_servicesThatAcceptUploads += attachments _servicesThatAcceptUploads += attachments
val (identity, keypair) = obtainKeyPair(dir) val (identity, keyPair) = obtainKeyPair(dir)
return Pair(constructStorageService(attachments, transactionStorage, keypair, identity),checkpointStorage) return Pair(constructStorageService(attachments, transactionStorage, keyPair, identity),checkpointStorage)
} }
protected open fun constructStorageService(attachments: NodeAttachmentService, protected open fun constructStorageService(attachments: NodeAttachmentService,
transactionStorage: TransactionStorage, transactionStorage: TransactionStorage,
keypair: KeyPair, keyPair: KeyPair,
identity: Party) = identity: Party) =
StorageServiceImpl(attachments, transactionStorage, keypair, identity) StorageServiceImpl(attachments, transactionStorage, keyPair, identity)
private fun obtainKeyPair(dir: Path): Pair<Party, KeyPair> { private fun obtainKeyPair(dir: Path): Pair<Party, KeyPair> {
// Load the private identity key, creating it if necessary. The identity key is a long term well known key that // Load the private identity key, creating it if necessary. The identity key is a long term well known key that
@ -420,13 +420,13 @@ abstract class AbstractNode(val dir: Path, val configuration: NodeConfiguration,
return if (!Files.exists(privKeyFile)) { return if (!Files.exists(privKeyFile)) {
log.info("Identity key not found, generating fresh key!") log.info("Identity key not found, generating fresh key!")
val keypair: KeyPair = generateKeyPair() val keyPair: KeyPair = generateKeyPair()
keypair.serialize().writeToFile(privKeyFile) keyPair.serialize().writeToFile(privKeyFile)
val myIdentity = Party(configuration.myLegalName, keypair.public) val myIdentity = Party(configuration.myLegalName, keyPair.public)
// We include the Party class with the file here to help catch mixups when admins provide files of the // We include the Party class with the file here to help catch mixups when admins provide files of the
// wrong type by mistake. // wrong type by mistake.
myIdentity.serialize().writeToFile(pubIdentityFile) myIdentity.serialize().writeToFile(pubIdentityFile)
Pair(myIdentity, keypair) Pair(myIdentity, keyPair)
} else { } else {
// 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 // This is just a sanity check. It shouldn't fail unless the admin has fiddled with the files and messed
@ -436,8 +436,8 @@ abstract class AbstractNode(val dir: Path, val configuration: NodeConfiguration,
throw ConfigurationException("The legal name in the config file doesn't match the stored identity file:" + throw ConfigurationException("The legal name in the config file doesn't match the stored identity file:" +
"${configuration.myLegalName} vs ${myIdentity.name}") "${configuration.myLegalName} vs ${myIdentity.name}")
// Load the private key. // Load the private key.
val keypair = Files.readAllBytes(privKeyFile).deserialize<KeyPair>() val keyPair = Files.readAllBytes(privKeyFile).deserialize<KeyPair>()
Pair(myIdentity, keypair) Pair(myIdentity, keyPair)
} }
} }

View File

@ -41,10 +41,10 @@ class E2ETestKeyManagementService(initialKeys: Set<KeyPair>) : SingletonSerializ
override val keys: Map<PublicKey, PrivateKey> get() = mutex.locked { HashMap(keys) } override val keys: Map<PublicKey, PrivateKey> get() = mutex.locked { HashMap(keys) }
override fun freshKey(): KeyPair { override fun freshKey(): KeyPair {
val keypair = generateKeyPair() val keyPair = generateKeyPair()
mutex.locked { mutex.locked {
keys[keypair.public] = keypair.private keys[keyPair.public] = keyPair.private
} }
return keypair return keyPair
} }
} }

View File

@ -33,10 +33,10 @@ class PersistentKeyManagementService(initialKeys: Set<KeyPair>) : SingletonSeria
override val keys: Map<PublicKey, PrivateKey> get() = mutex.locked { HashMap(keys) } override val keys: Map<PublicKey, PrivateKey> get() = mutex.locked { HashMap(keys) }
override fun freshKey(): KeyPair { override fun freshKey(): KeyPair {
val keypair = generateKeyPair() val keyPair = generateKeyPair()
mutex.locked { mutex.locked {
keys[keypair.public] = keypair.private keys[keyPair.public] = keyPair.private
} }
return keypair return keyPair
} }
} }

View File

@ -190,9 +190,9 @@ class TwoPartyTradeProtocolTests {
// That constructs the storage service object in a customised way ... // That constructs the storage service object in a customised way ...
override fun constructStorageService(attachments: NodeAttachmentService, override fun constructStorageService(attachments: NodeAttachmentService,
transactionStorage: TransactionStorage, transactionStorage: TransactionStorage,
keypair: KeyPair, keyPair: KeyPair,
identity: Party): StorageServiceImpl { identity: Party): StorageServiceImpl {
return StorageServiceImpl(attachments, RecordingTransactionStorage(transactionStorage), keypair, identity) return StorageServiceImpl(attachments, RecordingTransactionStorage(transactionStorage), keyPair, identity)
} }
} }
} }