PKCS#12在Android设备上创建密钥库

raf*_*roa 5 android bouncycastle keystore pfx android-keystore

我目前正在开发一个生成RSA密钥对的Android应用程序,构建并向一个CA发送PKCS#10认证请求,等待CA的响应(包含证书链,包括为最终实体发布的),然后构建要安装在Android KeyStore中的PKCS#12 KeyStore.

我只是告诉你一些代码:

// It removes Android-BC
Security.removeProvider("BC");
// I've tried with SpongyCastle but it also fails.
// I've previously imported BC jars.
Security.addProvider(new BouncyCastleProvider()); 
KeyStore ks = KeyStore.getInstance("PKCS12","BC");
ks.load(null);

// KeyPair generated by RSA with BC, and certificates obtained from CA.
PrivateKey prvK;
Certificate eeCert, caCert;

PKCS12BagAttributeCarrier caCertBagAttr = 
   (PKCS12BagAttributeCarrier) caCert;
caCertBagAttr.setBagAttribute(
   PKCSObjectIdentifiers.pkcs_9_at_friendlyName,
   new DERBMPString("CA"));

PKCS12BagAttributeCarrier certBagAttr =
   (PKCS12BagAttributeCarrier) eeCert;
certBagAttr.setBagAttribute(
   PKCSObjectIdentifiers.pkcs_9_at_friendlyName,
   new DERBMPString("EE Certificate"));
certBagAttr.setBagAttribute(
   PKCSObjectIdentifiers.pkcs_9_at_localKeyId,
   new SubjectKeyIdentifierStructure(eeCert.getPublicKey()));

Certificate[] chain = new Certificate[2];
chain[1] = caCert;
chain[0] = eeCert;

PKCS12BagAttributeCarrier privBagAttr = 
   (PKCS12BagAttributeCarrier) prvK;
privBagAttr.setBagAttribute(
   PKCSObjectIdentifiers.pkcs_9_at_friendlyName,
   new DERBMPString("EE Certificate Key"));
privBagAttr.setBagAttribute(
   PKCSObjectIdentifiers.pkcs_9_at_localKeyId,
   new SubjectKeyIdentifierStructure(eeCert.getPublicKey()));

// Finally, keystore is saved to file.
ks.setKeyEntry("P12", prvK, null, chain);
saveP12File(ks);
Run Code Online (Sandbox Code Playgroud)

问题是当我在我的Windows PC上运行它时,一切正常(BouncyCastle 1.49),但是当我在Android上运行它时创建的PKCS#12文件不能用于SSL握手或签名,因为Logcat说它没有有任何相关的PrivateKey.

如果我在加载文件后在Java上打印KeyStore的完整内容,似乎一切都是正确的,但是当我在Android浏览器上使用或者当我在Windows上使用它并在Windows上安装时,其私钥无法使用.

Android和PC上的相同代码,同一版本的BC,PKCS#12似乎没有格格不入.

有谁能够帮我?提前致谢.

编辑

我已经构建了一个生成RSA密钥对的servlet,并以PEM格式发送回我的应用程序.一切似乎都行得通

  • 我可以在Android设备上或通过外部servlet生成有效的(加密公共和解密测试工作良好)RSA密钥对.
  • 我从CA收到CA和EE证书.
  • 我能够构建一个"有效的"PKCS12(我称之为有效,因为它可以被Android,Windows读取)

但:

  • 我无法使用EE证书打开SSL通道作为握手的客户端证书.在Windows或Android上.

我已经上传到我的Dropbox:

  1. CA证书
  2. CA颁发的EE证书
  3. EE证书的私钥
  4. EE证书的公钥
  5. PKCS12在Android上创建
  6. Java Android代码

使用空的trustStore(不管CA发出的客户端证书不成功)对Tomcat进行了测试失败.

有人能看到可以解释它的东西吗?