Saving certificate chain in a pkcs12 keystore

后端 未结 2 389
南笙
南笙 2020-12-14 07:40

The following code:

//used Bouncy Castle provider for keyStore
keyStore.setKeyEntry(alias, (Key)keyPair.getPrivate(), pwd, certChain);  

w

相关标签:
2条回答
  • 2020-12-14 08:20

    Your code has 2 error:

    first: You not set Issuer for certificate (client cert should be issued by CA to make valid chain).

    second: You use wrong order when create certificate chain (should be client ferts, CA last)

    here is reworked SSCCE, and it works without errors.

    @Test
    public void testKeyStore() throws Exception{
            try {
            String storeName =  "/home/grigory/outstore.pkcs12";
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(1024);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            PublicKey publicKey = keyPair.getPublic();
            PrivateKey privateKey = keyPair.getPrivate();
            Certificate trustCert =  createCertificate("CN=CA", "CN=CA", publicKey, privateKey);
            Certificate[] outChain = { createCertificate("CN=Client", "CN=CA", publicKey, privateKey), trustCert };
    
            KeyStore outStore = KeyStore.getInstance("PKCS12");
            outStore.load(null, "secret".toCharArray());
            outStore.setKeyEntry("mykey", privateKey, "secret".toCharArray(), outChain);
            OutputStream outputStream = new FileOutputStream(storeName);
            outStore.store(outputStream, "secret".toCharArray());
            outputStream.flush();
            outputStream.close();
    
            KeyStore inStore = KeyStore.getInstance("PKCS12");
            inStore.load(new FileInputStream(storeName), "secret".toCharArray());
            Key key = outStore.getKey("myKey", "secret".toCharArray());
            Assert.assertEquals(privateKey, key);
    
            Certificate[] inChain = outStore.getCertificateChain("mykey");
            Assert.assertNotNull(inChain);
            Assert.assertEquals(outChain.length, inChain.length);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AssertionError(e.getMessage());
        }
       }
        private static X509Certificate createCertificate(String dn, String issuer, PublicKey publicKey, PrivateKey privateKey) throws Exception {
            X509V3CertificateGenerator certGenerator = new X509V3CertificateGenerator();
            certGenerator.setSerialNumber(BigInteger.valueOf(Math.abs(new Random().nextLong())));
            certGenerator.setIssuerDN(new X509Name(dn));
            certGenerator.setSubjectDN(new X509Name(dn));
            certGenerator.setIssuerDN(new X509Name(issuer)); // Set issuer!
            certGenerator.setNotBefore(Calendar.getInstance().getTime());
            certGenerator.setNotAfter(Calendar.getInstance().getTime());
            certGenerator.setPublicKey(publicKey);
            certGenerator.setSignatureAlgorithm("SHA1withRSA");
            X509Certificate certificate = (X509Certificate)certGenerator.generate(privateKey, "BC");
            return certificate;
        }
    
    0 讨论(0)
  • 2020-12-14 08:33

    Depending which JDK you use, there are different way to package your application. It happens to us when some people where using Linux and OpenJDK and some other developping on Windows with SunJDK (Oracle).

    The lastest have some extra configuration to do in order to be able to use the strongest algorithms. This article can help you if your problem is related to the JCE Policy.

    0 讨论(0)
提交回复
热议问题