我正在努力使用ECDSA实施DTLS 1.2握手,并且遇到了客户端证书的麻烦。当我生成它时,似乎似乎subjectPublicKeyInfo是不正确的:在Wireshark中,而不是在算法内部看到ECParameters: namedCurve
,我似乎得到了一个未解析的OID。
我正在像这样生成证书(我希望这充满了错误,我很乐意那里的任何指导!)
生成密钥对:
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()
}
我在这里做错了什么?我大概在正确的轨道上吗?
答案 0 :(得分:1)
我认为我设法偶然找到了解决方法。我将generateEcKeyPair
的实现更改为以下内容(在将BouncyCastleProvider
添加为提供者之后):
fun generateEcKeyPair(): KeyPair {
val keyGen = KeyPairGenerator.getInstance("EC", "BC")
val ecCurveSpec = ECNamedCurveTable.getParameterSpec("secp256r1")
keyGen.initialize(ecCurveSpec)
return keyGen.generateKeyPair()
}
看起来我现在在证书中正确命名了曲线。
也许我之前传递的参数不正确?还是手动创建它不能正确保存命名曲线?