【问题标题】:How to extract SubjectAlternativeNames form PKCS10 request如何从 PKCS10 请求中提取 SubjectAlternativeNames
【发布时间】:2021-12-27 13:20:21
【问题描述】:

我想从 JAVA 中的 PKCS10 请求中提取主题备用名称 (SAN)。

拳头,我使用 bouncycastle 获得 PKCS10CertificationRequest 如下:

PKCS10CertificationRequest certificationRequest = getPKCS10CertificationRequest(csr);

但是,我不知道是否有办法从certificationRequest 中提取SAN 值。

有什么帮助吗?

【问题讨论】:

    标签: java bouncycastle pkcs#10


    【解决方案1】:
        byte[] der = Files.readAllBytes(Paths.get(args[0])); // for example
        // assuming all BouncyCastle classes imported as needed and 
        // given a CSR in der, to get the SAN extension as an object
        // (minimal error handling, you may want to improve)
        Attribute[] attrs = new PKCS10CertificationRequest(der).getAttributes(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest);
        if( attrs.length != 1 ) throw new Exception("bad");
        ASN1Encodable[] valus = attrs[0].getAttributeValues();
        if( valus.length != 1 ) throw new Exception("bad");
        Extension extn = Extensions.getInstance(valus[0]).getExtension(Extension.subjectAlternativeName);
        if( extn == null ) throw new Exception("missing");
        // to get the _value_ of the extension, now extn.getExtnValus().getOctets()
        // to _use_ the _value_ of the extension, parse as GeneralNames:
        GeneralNames sanv = GeneralNames.getInstance(extn.getExtnValue().getOctets());
        for( GeneralName item : sanv.getNames() ){ // example of possible usage
            System.out.println (item.toString()); // you probably want something else
        }
    

    【讨论】:

    • 感谢您的完整回答 :) ,当我尝试计算 attrs 时出现此错误:方法 getAttributes(DERObjectIdentifier) 未定义类型 PKCS10CertificationRequestJava(67108964)
    • 零:确保使用org.bouncycastle.pkcs(bcpkix)中的PKCS10CertificationRequest,而不是org.bouncycastle.jce(bcprov)中的(已弃用!)之一。我没注意到有两个。
    猜你喜欢
    • 2016-01-16
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多