无法获取EC参数:证书的subjectPublicKeyInfo中的namedcurve

时间:2019-04-07 22:37:15

标签: ssl kotlin certificate ssl-certificate bouncycastle

我正在努力使用ECDSA实施DTLS 1.2握手,并且遇到了客户端证书的麻烦。当我生成它时,似乎似乎subjectPublicKeyInfo是不正确的:在Wireshark中,而不是在算法内部看到ECParameters: namedCurve,我似乎得到了一个未解析的OID。

我期望的是: What I expect 我所看到的: What I see

我正在像这样生成证书(我希望这充满了错误,我很乐意那里的任何指导!)

生成密钥对:

private fun generateEcKeyPair(): AsymmetricCipherKeyPair {
    val generator = ECKeyPairGenerator()

    val curveSpec = ECNamedCurveTable.getParameterSpec("secp256r1")
    generator.init(
    ECKeyGenerationParameters(
        ECDomainParameters(
            curveSpec.curve,
            curveSpec.g,
            curveSpec.n,
            curveSpec.h,
            curveSpec.seed
            ),
            SecureRandom()
        )
    )
    return generator.generateKeyPair()
}

生成证书(使用通过上述方法生成的密钥对):

private fun generateX509Certificate(
    subject: X500Name,
    keyPair: AsymmetricCipherKeyPair
): org.bouncycastle.asn1.x509.Certificate {
    val now = System.currentTimeMillis()
    val notBefore = Date(now - Duration.ofDays(1).toMillis())
    val notAfter = Date(now + Duration.ofDays(7).toMillis())

    val certBuilder = X509v3CertificateBuilder(
        subject,
        BigInteger.valueOf(now),
        notBefore,
        notAfter,
        subject,
        SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(keyPair.public)
    )
    val signatureAlgoIdentifier =
        DefaultSignatureAlgorithmIdentifierFinder().find(signatureAlgo)
    val digestAlgoIdentifier =
        DefaultDigestAlgorithmIdentifierFinder().find(signatureAlgoIdentifier)
    val signer =
        BcECContentSignerBuilder(signatureAlgoIdentifier, digestAlgoIdentifier).build(keyPair.private)

    return certBuilder.build(signer).toASN1Structure()
}

我在这里做错了什么?我大概在正确的轨道上吗?

1 个答案:

答案 0 :(得分:1)

我认为我设法偶然找到了解决方法。我将generateEcKeyPair的实现更改为以下内容(在将BouncyCastleProvider添加为提供者之后):

fun generateEcKeyPair(): KeyPair {
    val keyGen = KeyPairGenerator.getInstance("EC", "BC")
    val ecCurveSpec = ECNamedCurveTable.getParameterSpec("secp256r1")

    keyGen.initialize(ecCurveSpec)

    return keyGen.generateKeyPair()
}

看起来我现在在证书中正确命名了曲线。

也许我之前传递的参数不正确?还是手动创建它不能正确保存命名曲线?