【发布时间】:2014-08-18 09:24:04
【问题描述】:
我已经使用 openssl 生成了 CSR。现在我想解析 CSR 并显示 CSR 中可用的 ipaddress、Othername。
我编写了以下代码。它能够正确显示 dns、url 但我无法以正确的格式显示 ipaddress 和 othername。
public static void testReadCertificateSigningRequest() {
String csrPEM = null;
try {
FileInputStream fis = new FileInputStream("E://test.txt");
csrPEM = IOUtils.toString(fis);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PKCS10CertificationRequest csr = convertPemToPKCS10CertificationRequest(csrPEM);
X500Name x500Name = csr.getSubject();
System.out.println("x500Name is: " + x500Name + "\n");
Attribute[] certAttributes = csr.getAttributes();
for (Attribute attribute : certAttributes) {
if (attribute.getAttrType().equals(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest)) {
Extensions extensions = Extensions.getInstance(attribute.getAttrValues().getObjectAt(0));
//Extension ext = extensions.getExtension(Extension.subjectAlternativeName);
GeneralNames gns = GeneralNames.fromExtensions(extensions,Extension.subjectAlternativeName);
GeneralName[] names = gns.getNames();
for(int k=0; k < names.length; k++) {
String title = "";
if(names[k].getTagNo() == GeneralName.dNSName) {
title = "dNSName";
}
else if(names[k].getTagNo() == GeneralName.iPAddress) {
title = "iPAddress";
names[k].toASN1Object();
}
else if(names[k].getTagNo() == GeneralName.otherName) {
title = "otherName";
}
System.out.println(title + ": "+ names[k].getName());
}
}
}
}
// Method to convert PEM to PKCS10CertificationRequest
private static PKCS10CertificationRequest convertPemToPKCS10CertificationRequest(String pem) {
PEMParser pRd = new PEMParser(new StringReader(pem));
org.bouncycastle.pkcs.PKCS10CertificationRequest csr = null;
try {
csr = (org.bouncycastle.pkcs.PKCS10CertificationRequest) pRd.readObject();
} catch (IOException e) {
e.printStackTrace();
}
return csr;
}
上面的代码打印 iPAddress,otherName 如下:
iPA地址:#c0a80701 IP地址:#00130000000000000000000000000017 otherName: [1.2.3.4, [0]其他标识符]
我怎样才能以正确的格式检索 ipAdress 和 othername?
谢谢。
【问题讨论】:
-
感谢您发布此示例代码。我在将 SAN 从 pkcs10 中拉出时遇到了问题,您的示例对我帮助很大......虽然我有点担心“attribute.getAttrValues().getObjectAt(0)”这一点
标签: bouncycastle