gpt4 book ai didi

java - 以编程方式在 java 中生成 CSR 时添加 SAN 名称

转载 作者:行者123 更新时间:2023-12-04 22:38:29 30 4
gpt4 key购买 nike

我正在使用以下代码在 java 中生成 CSR:

package demo;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;

import sun.security.pkcs10.PKCS10;
import sun.security.util.DerInputStream;
import sun.security.util.DerValue;
import sun.security.x509.CertificateExtensions;
import sun.security.x509.GeneralName;
import sun.security.x509.GeneralNames;
import sun.security.x509.SubjectAlternativeNameExtension;
import sun.security.x509.X500Name;

/**
* This class generates PKCS10 certificate signing request
*
* @author Pankaj@JournalDev.com
* @version 1.0
*/
public class GenerateCSR {
private static PublicKey publicKey = null;
private static PrivateKey privateKey = null;
private static KeyPairGenerator keyGen = null;
private static GenerateCSR gcsr = null;

private GenerateCSR() {
try {
keyGen = KeyPairGenerator.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
keyGen.initialize(2048, new SecureRandom());
KeyPair keypair = keyGen.generateKeyPair();
publicKey = keypair.getPublic();
privateKey = keypair.getPrivate();
}

public static GenerateCSR getInstance() {
if (gcsr == null)
gcsr = new GenerateCSR();
return gcsr;
}

public String getCSR(String cn) throws Exception {
byte[] csr = generatePKCS10(cn, "Java", "JournalDev", "Cupertino", "California", "USA");
return new String(csr);
}

/**
*
* @param CN Common Name, is X.509 speak for the name that distinguishes the Certificate best, and ties it to your
* Organization
* @param OU Organizational unit
* @param O Organization NAME
* @param L Location
* @param S State
* @param C Country
* @return
* @throws Exception
*/
private static byte[] generatePKCS10(String CN, String OU, String O, String L, String S, String C)
throws Exception {
GeneralNames generalNames = new GeneralNames();
generalNames.add(new GeneralName(new DerValue("b")));
generalNames.add(new GeneralName(new DerValue("a")));

CertificateExtensions ext = new CertificateExtensions();

ext.set(SubjectAlternativeNameExtension.NAME, new SubjectAlternativeNameExtension(generalNames));

// generate PKCS10 certificate request
String sigAlg = "MD5WithRSA";
PKCS10 pkcs10 = new PKCS10(publicKey);
Signature signature = Signature.getInstance(sigAlg);
signature.initSign(privateKey);
// common, orgUnit, org, locality, state, country
X500Name x500Name = new X500Name(CN, OU, O, L, S, C);
pkcs10.encodeAndSign(x500Name, signature);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(bs);
pkcs10.print(ps);
byte[] c = bs.toByteArray();
try {
if (ps != null)
ps.close();
if (bs != null)
bs.close();
} catch (Throwable th) {
}
return c;
}

public PublicKey getPublicKey() {
return publicKey;
}

public PrivateKey getPrivateKey() {
return privateKey;
}

public static void main(String[] args) throws Exception {
GenerateCSR gcsr = GenerateCSR.getInstance();

System.out.println("Public Key:\n" + gcsr.getPublicKey().toString());

System.out.println("Private Key:\n" + gcsr.getPrivateKey().toString());
String csr = gcsr.getCSR("journaldev.com <https://www.journaldev.com>");
System.out.println("CSR Request Generated!!");
System.out.println(csr);
}

}

如您所见,我正在使用以下代码添加 SAN 名称
      GeneralNames generalNames = new GeneralNames();
generalNames.add(new GeneralName(new DerValue("b")));
generalNames.add(new GeneralName(new DerValue("a")));

CertificateExtensions ext = new CertificateExtensions();

ext.set(SubjectAlternativeNameExtension.NAME, new SubjectAlternativeNameExtension(generalNames));
我的问题是:
  • 这是正确的方法吗?
  • 如果是如何使用 CertificateExtensions 的对象以及在哪里传递它。

  • 我指的是 this问题,有人提到我必须在我的证书的构造函数中传递它,但我不能 X500Name 的构造函数只允许字符串值。

    最佳答案

    正如我在评论中所说,sun.*类并不打算被其他程序员使用,我个人永远不会使用它们。 BouncycaSTLe 库可以做这一切,甚至更多。但是,如果您坚持使用这些类,那么您将需要使用更多才能获得预期的效果。注意:由于没有记录这些类(class),我在这里所拥有的主要是实验的结果。
    请注意,MD5 不能用于签名,这样的应用程序完全不安全。我已将其替换为 SHA256。
    考虑一下我修改过的这段代码。名称本身只是示例:

    // ...

    import sun.security.pkcs10.PKCS10Attribute;
    import sun.security.pkcs10.PKCS10Attributes;
    import sun.security.x509.*;
    import sun.security.pkcs10.PKCS10;
    import sun.security.pkcs.PKCS9Attribute;

    // ....

    GeneralNames generalNames = new GeneralNames();
    generalNames.add(new GeneralName(new DNSName("a.example.com")));
    generalNames.add(new GeneralName(new DNSName("never.ever.example.com")));
    generalNames.add(new GeneralName(new IPAddressName("192.168.1.250")));
    CertificateExtensions ext = new CertificateExtensions();

    ext.set(SubjectAlternativeNameExtension.NAME, new SubjectAlternativeNameExtension(generalNames));
    var pkcs9Attr = new PKCS9Attribute(PKCS9Attribute.EXTENSION_REQUEST_OID, ext);
    var pkcs10Attrs = new PKCS10Attributes(new PKCS10Attribute[] {
    new PKCS10Attribute(pkcs9Attr)
    });

    // generate PKCS10 certificate request
    String sigAlg = "SHA256WithRSA";
    PKCS10 pkcs10 = new PKCS10(publicKey, pkcs10Attrs);
    Signature signature = Signature.getInstance(sigAlg);
    signature.initSign(privateKey);
    // common, orgUnit, org, locality, state, country
    X500Name x500Name = new X500Name(CN, OU, O, L, S, C);
    pkcs10.encodeAndSign(x500Name, signature);
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(bs);
    pkcs10.print(ps);
    这应该会产生一个带有适当的 subjectAltName 扩展的 PKCS10 证书请求。

    关于java - 以编程方式在 java 中生成 CSR 时添加 SAN 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68941041/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com