bouncycastle - How to parse SAN from CSR using java or bouncy castle? -
i have generated csr using openssl. want parse csr , display ipaddress, othername available in csr.
i have written following code. able display dns, url not able display ipaddress , othername in correct format.
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(); (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 convert pem 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; }
above code prints ipaddress, othername per below:
ipaddress: #c0a80701 ipaddress: #00130000000000000000000000000017 othername: [1.2.3.4, [0]some other identifier]
how can retrieve ipadress , othername in correct format?
thanks.
that 'correct' format. there no other way besides manual conversion other answer suggests.
bouncycastle encodes internally in generalname.java class constructor.
153 else if (tag == ipaddress) 154 { 155 byte[] enc = togeneralnameencoding(name); 156 if (enc != null) 157 { 158 this.obj = new deroctetstring(enc); 159 } 160 else 161 { 162 throw new illegalargumentexception("ip address invalid"); 163 } 164 }
see: http://www.docjar.org/html/api/org/bouncycastle/asn1/x509/generalname.java.html
when extract generalnames csr incorporate them certificate, bouncycastle decodes again original value ends in final certificate.
regarding othername property: "for x400address, othername , edipartyname there no common string format defined." there no 'correct' format there.
see: http://www.eecs.berkeley.edu/~jonah/javadoc/org/bouncycastle/asn1/x509/generalname.html
Comments
Post a Comment