Skip to content

Commit 27e45bc

Browse files
authored
Adding public key verification to the X509Utilities.createCertificateSigningRequest (corda#2784)
1 parent f454b94 commit 27e45bc

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

core/src/main/kotlin/net/corda/core/internal/InternalUtils.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,14 @@ val KClass<*>.packageName: String get() = java.`package`.name
326326

327327
fun URL.openHttpConnection(): HttpURLConnection = openConnection() as HttpURLConnection
328328

329-
fun URL.post(serializedData: OpaqueBytes) {
330-
openHttpConnection().apply {
329+
fun URL.post(serializedData: OpaqueBytes): ByteArray {
330+
return openHttpConnection().run {
331331
doOutput = true
332332
requestMethod = "POST"
333333
setRequestProperty("Content-Type", "application/octet-stream")
334334
outputStream.use { serializedData.open().copyTo(it) }
335335
checkOkResponse()
336+
inputStream.use { it.readBytes() }
336337
}
337338
}
338339

node-api/src/main/kotlin/net/corda/nodeapi/internal/crypto/X509Utilities.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import net.corda.core.CordaOID
44
import net.corda.core.crypto.Crypto
55
import net.corda.core.crypto.SignatureScheme
66
import net.corda.core.crypto.random63BitValue
7-
import net.corda.core.internal.*
7+
import net.corda.core.internal.CertRole
8+
import net.corda.core.internal.reader
9+
import net.corda.core.internal.uncheckedCast
10+
import net.corda.core.internal.writer
811
import net.corda.core.utilities.days
912
import net.corda.core.utilities.millis
1013
import org.bouncycastle.asn1.*
@@ -26,6 +29,7 @@ import java.math.BigInteger
2629
import java.nio.file.Path
2730
import java.security.KeyPair
2831
import java.security.PublicKey
32+
import java.security.SignatureException
2933
import java.security.cert.*
3034
import java.security.cert.Certificate
3135
import java.time.Duration
@@ -265,7 +269,11 @@ object X509Utilities {
265269
return JcaPKCS10CertificationRequestBuilder(subject, keyPair.public)
266270
.addAttribute(BCStyle.E, DERUTF8String(email))
267271
.addAttribute(ASN1ObjectIdentifier(CordaOID.X509_EXTENSION_CORDA_ROLE), certRole)
268-
.build(signer)
272+
.build(signer).apply {
273+
if (!isSignatureValid()) {
274+
throw SignatureException("The certificate signing request signature validation failed.")
275+
}
276+
}
269277
}
270278

271279
fun createCertificateSigningRequest(subject: X500Principal, email: String, keyPair: KeyPair, certRole: CertRole = CertRole.NODE_CA): PKCS10CertificationRequest {
@@ -311,6 +319,13 @@ val Certificate.x509: X509Certificate get() = requireNotNull(this as? X509Certif
311319

312320
val Array<Certificate>.x509: List<X509Certificate> get() = map { it.x509 }
313321

322+
/**
323+
* Validates the signature of the CSR
324+
*/
325+
fun PKCS10CertificationRequest.isSignatureValid(): Boolean {
326+
return this.isSignatureValid(JcaContentVerifierProviderBuilder().build(this.subjectPublicKeyInfo))
327+
}
328+
314329
/**
315330
* Wraps a [CertificateFactory] to remove boilerplate. It's unclear whether [CertificateFactory] is threadsafe so best
316331
* so assume this class is not.

0 commit comments

Comments
 (0)