11
Hy Guys!我正在嘗試使用bouncycastle創建x.509證書,該證書應由另一證書籤名並存儲爲PEM base 64格式。使用帶證書路徑的bouncycastle創建x.509證書(cert鏈)
我已經有自簽證書(公鑰和私鑰)。現在我想創建一個新的簽名並使用現有的自簽名證書籤名。
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
keyPairGenerator.initialize(1024, new SecureRandom());
KeyPair keyPair = keyPairGenerator.generateKeyPair();
X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
X500Principal dnName = new X500Principal("CN=Sergey");
certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
certGen.setSubjectDN(dnName);
certGen.setIssuerDN(caCert.getSubjectX500Principal());
certGen.setNotBefore(validityBeginDate);
certGen.setNotAfter(validityEndDate);
certGen.setPublicKey(keyPair.getPublic());
certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");
certGen.addExtension(X509Extensions.AuthorityKeyIdentifier, false, new AuthorityKeyIdentifierStructure(caCert));
certGen.addExtension(X509Extensions.SubjectKeyIdentifier, false, new SubjectKeyIdentifierStructure(keyPair.getPublic()));
X509Certificate cert = certGen.generate(caCertPrivateKey, "BC");
驗證通過沒有例外,從我的角度來看,它已成功CACERT簽署意味着:
cert.verify(caCert.getPublicKey());
然後我把它解碼成PEM基地64:
PEMWriter pemWriter = new PEMWriter(new PrintWriter(System.out));
pemWriter.writeObject(cert);
pemWriter.flush();
我在輸出中得到這樣的東西:
----- BEGIN CERTIFICATE -----
MIIDDjCCAnegAwIBAgIBFDAN ........
----- END CERTIFICATE -----
當我打開它,我看到了未來:
爲什麼在caCert成功簽署認證鏈時沒有認證鏈?
我的代碼中需要更改哪些內容才能看到認證鏈?