【问题标题】:X509v3CertificateBuilder: java.lang.IllegalArgumentException: cannot produce certificate signatureX509v3CertificateBuilder:java.lang.IllegalArgumentException:无法生成证书签名
【发布时间】:2020-07-02 15:13:12
【问题描述】:

我正在创建一个 KeyPair 并尝试使用它创建一个 X509Certificate(在 Android 上使用 BouncyCastle),但遇到以下错误:

java.lang.IllegalArgumentException: cannot produce certificate signature
    at org.bouncycastle.cert.X509v3CertificateBuilder.build(Unknown Source:57)

这是我创建 KeyPair 的方式:

        val kpg = KeyPairGenerator.getInstance(
                KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore")
        kpg.initialize(KeyGenParameterSpec.Builder(
                       "alias",
                        KeyProperties.PURPOSE_SIGN or KeyProperties.PURPOSE_VERIFY)
                .setDigests(KeyProperties.DIGEST_SHA256,
                        KeyProperties.DIGEST_SHA512,
                        KeyProperties.DIGEST_NONE)
                .setSignaturePaddings(KeyProperties.SIGNATURE_PADDING_RSA_PKCS1)
                .setUserAuthenticationRequired(true)
                .build())
        return kpg.generateKeyPair()

以及我是如何尝试生成证书的:

        // replace the default BC implementation, and use:  implementation 'org.bouncycastle:bcpkix-jdk15on:1.64'
        Security.removeProvider("BC")
        val bc = BouncyCastleProvider()
        Security.insertProviderAt(bc, 1)

        val builder = JcaContentSignerBuilder("SHA256WithRSA")
        builder.setProvider("AndroidKeyStoreBCWorkaround")
        val certGen: X509v3CertificateBuilder = JcaX509v3CertificateBuilder(X500Name("name removed"),
                BigInteger.valueOf(SecureRandom().nextLong()),
                Date(),
                Date(System.currentTimeMillis() + 365 * 24 * 60 * 60 * 1000),
                X500Name("Name removed"),
                keyPair.public)


        val contentSigner: ContentSigner = builder.build(keyPair.private)
        val certHolder = certGen.build(contentSigner) <- THE ERROR OCCURS HERE
        val cert = JcaX509CertificateConverter().getCertificate(certHolder)

调用X509v3CertificateBuilder'build()方法时出现错误,但错误信息并没有真正的帮助。

编辑:这是完整的堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.app, PID: 31547
java.lang.IllegalArgumentException: cannot produce certificate signature
    at org.bouncycastle.cert.X509v3CertificateBuilder.build(Unknown Source:57)
    at com.example.app.Helper$Companion.createX509Certificate(Helper.kt:104)
    at com.example.app.Presenter.getLocalKeyPair(Presenter.kt:123)
    at com.example.app.Presenter$getKey$1.onComplete(Presenter.kt:109)
    at com.example.app.Manager$getKey$1.onResponse(Manager.kt:30)
    at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6626)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

【问题讨论】:

  • 发布完整的异常堆栈跟踪。
  • @PresidentJamesMoveonPolk 刚刚提交了包含完整堆栈跟踪的编辑。
  • 您对错误消息感到失望是对的。出于某种原因,Bouncycastle 捕获了 IOException 并将其作为 IllegalArgumentException 重新抛出。我不反对将已检查的异常更改为未检查的异常,但至少用新异常包装原始异常。不幸的是,我不知道是什么导致了 ContentSigner 中的 IOException。我很想重新编译 Bouncycastle PKIX 库并通过包装底层 IOException 来改进此时抛出的异常。

标签: android kotlin bouncycastle key-pair


【解决方案1】:

又过了几个小时,我找到了解决这个问题的办法。

我试图调试我想到的所有东西,我注意到contentSigner.signature 正在引发异常:

android.security.KeyStoreException: Key user not authenticated

这当然表明生物特征认证有问题。改变:

.setUserAuthenticationRequired(false)

似乎一切正常。

最后我使用了以下两行:

.setUserAuthenticationRequired(true)    
.setUserAuthenticationValidityDurationSeconds(100)

这也可以按预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-16
    • 2016-07-11
    • 2020-01-17
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    相关资源
    最近更新 更多