forked from corda/corda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved X509Utilities, and some other crypto utilities in node, into no…
…de-api so that they can be used by services outside of the node. There's also some cleanup as well.
- Loading branch information
1 parent
0e37132
commit 2ceb628
Showing
30 changed files
with
248 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...da/node/utilities/ContentSignerBuilder.kt → ...i/internal/crypto/ContentSignerBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
node-api/src/main/kotlin/net/corda/nodeapi/internal/crypto/KeyStoreWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package net.corda.nodeapi.internal.crypto | ||
|
||
import net.corda.core.identity.CordaX500Name | ||
import net.corda.core.internal.cert | ||
import net.corda.core.internal.read | ||
import java.nio.file.Path | ||
import java.security.KeyPair | ||
import java.security.PublicKey | ||
import java.security.cert.CertPath | ||
import java.security.cert.Certificate | ||
import java.security.cert.CertificateFactory | ||
|
||
class KeyStoreWrapper(private val storePath: Path, private val storePassword: String) { | ||
private val keyStore = storePath.read { loadKeyStore(it, storePassword) } | ||
|
||
private fun createCertificate(serviceName: CordaX500Name, pubKey: PublicKey): CertPath { | ||
val clientCertPath = keyStore.getCertificateChain(X509Utilities.CORDA_CLIENT_CA) | ||
// Assume key password = store password. | ||
val clientCA = certificateAndKeyPair(X509Utilities.CORDA_CLIENT_CA) | ||
// Create new keys and store in keystore. | ||
val cert = X509Utilities.createCertificate(CertificateType.IDENTITY, clientCA.certificate, clientCA.keyPair, serviceName, pubKey) | ||
val certPath = X509CertificateFactory().delegate.generateCertPath(listOf(cert.cert) + clientCertPath) | ||
require(certPath.certificates.isNotEmpty()) { "Certificate path cannot be empty" } | ||
// TODO: X509Utilities.validateCertificateChain() | ||
return certPath | ||
} | ||
|
||
fun signAndSaveNewKeyPair(serviceName: CordaX500Name, privateKeyAlias: String, keyPair: KeyPair) { | ||
val certPath = createCertificate(serviceName, keyPair.public) | ||
// Assume key password = store password. | ||
keyStore.addOrReplaceKey(privateKeyAlias, keyPair.private, storePassword.toCharArray(), certPath.certificates.toTypedArray()) | ||
keyStore.save(storePath, storePassword) | ||
} | ||
|
||
fun savePublicKey(serviceName: CordaX500Name, pubKeyAlias: String, pubKey: PublicKey) { | ||
val certPath = createCertificate(serviceName, pubKey) | ||
// Assume key password = store password. | ||
keyStore.addOrReplaceCertificate(pubKeyAlias, certPath.certificates.first()) | ||
keyStore.save(storePath, storePassword) | ||
} | ||
|
||
// Delegate methods to keystore. Sadly keystore doesn't have an interface. | ||
fun containsAlias(alias: String) = keyStore.containsAlias(alias) | ||
|
||
fun getX509Certificate(alias: String) = keyStore.getX509Certificate(alias) | ||
|
||
fun getCertificateChain(alias: String): Array<out Certificate> = keyStore.getCertificateChain(alias) | ||
|
||
fun getCertificate(alias: String): Certificate = keyStore.getCertificate(alias) | ||
|
||
fun certificateAndKeyPair(alias: String): CertificateAndKeyPair = keyStore.getCertificateAndKeyPair(alias, storePassword) | ||
} |
Oops, something went wrong.