java - 如何在 Java 中生成多域 (UCC) 证书?

标签 java ssl-certificate bouncycastle multiple-domains

目前我正在使用 BouncyCastle库生成证书。像这样:

X509V3CertificateGenerator certGenerator = new X509V3CertificateGenerator();
certGenerator.setIssuerDN( rootCertificate.getSubjectX500Principal() );
certGenerator.setSignatureAlgorithm( "SHA1withRSA" );
certGenerator.setSerialNumber( serial );
certGenerator.setNotBefore( notBefore );
certGenerator.setNotAfter( notAfter );
certGenerator.setPublicKey( rootCertificate.getPublicKey() );

Hashtable<DERObjectIdentifier, String> attrs = new Hashtable<DERObjectIdentifier, String>();
Vector<DERObjectIdentifier> order = new Vector<DERObjectIdentifier>();

attrs.put( X509Principal.C, "RU" );
// other attrs.put() calls here

order.addElement( X509Principal.C );
// other order.addElement() calls here

certGenerator.setSubjectDN( new X509Principal( order, attrs ) );
certGenerator.addExtension( X509Extensions.AuthorityKeyIdentifier, false, new AuthorityKeyIdentifierStructure( rootCertificate ) );
certGenerator.addExtension( X509Extensions.SubjectKeyIdentifier, false, new SubjectKeyIdentifierStructure( newKeyPair.getPublic() ) );

return certGenerator.generate( rootPrivateKey, "BC" );

我可以在生成的证书中添加 SubjectAltNames 字段吗?

最佳答案

要完成任务,请在 certGenerator.generate() 调用之前插入以下内容:

ASN1EncodableVector alternativeNames = new ASN1EncodableVector();
for( String domainName : domainNames )
{
  alternativeNames.add( new GeneralName( GeneralName.dNSName, domainName ) );
}
certGenerator.addExtension( X509Extensions.SubjectAlternativeName, false, new GeneralNames( new DERSequence( alternativeNames ) ) );

(双V提供答案)

关于java - 如何在 Java 中生成多域 (UCC) 证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6911719/

相关文章:

java - 如何从 *.properties 和 build.xml 文件导入?

Python SSL 验证失败

iis - 从 powershell 创建自签名 iis ssl 证书

ssl-certificate - Azure云服务可以使用sha256证书吗

java Cucumber,如何在失败时继续

java - 用于接收短信的应用程序第一次运行时工作正常,第二次运行时所有消息均不显示

java - 将 @ManyToMany 集合映射到 ID 集合

java - 如何使用 CRT 参数创建 BCRSAPrivateCrtKey 对象?

c# - 解密包含私钥的受密码保护的 PEM

java - BouncyCaSTLe 是如何生成 ECDH "Keys"的?