title: X509v3CertificateBuilder: java.lang.IllegalArgumentException: cannot produce certificate signature
body: <p>I'm creating a <code>KeyPair</code> and trying to create a <code>X509Certificate</code> with it (using BouncyCastle on Android), but running into the following error:</p>
  
  <pre><code>java.lang.IllegalArgumentException: cannot produce certificate signature
      at org.bouncycastle.cert.X509v3CertificateBuilder.build(Unknown Source:57)
  </code></pre>
  
  <p>Here's how I'm creating the <code>KeyPair</code>:</p>
  
  <pre><code>        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()
  </code></pre>
  
  <p>And how I'm trying to generate the certificate:</p>
  
  <pre><code>        // 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) &lt;- THE ERROR OCCURS HERE
          val cert = JcaX509CertificateConverter().getCertificate(certHolder)
  </code></pre>
  
  <p>The error occurs while calling <code>X509v3CertificateBuilder</code>' <code>build()</code> method, but the error message is not really helpful.</p>
  
  <p>EDIT: Here is the full stacktrace:</p>
  
  <pre><code>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)
  </code></pre>