SubjectDNParser { public static String parse(String subjectDistinguishedName, RDN relativeDistinguishedName) { for (String RDN : subjectDistinguishedName.split(",")) { if (RDN.trim().startsWith(relativeDistinguishedName.getOID())) { String RDNValue = RDN.split("=")[1]; String RDNValueWithOutPrefix = RDNValue.substring(5); return removeQuotes( new String(Hex.decode(RDNValueWithOutPrefix), StandardCharsets.UTF_8)); } else if (RDN.trim().toUpperCase().startsWith(relativeDistinguishedName.name())) { return removeQuotes(RDN.split("=")[1]); } } return null; } private  SubjectDNParser(); static String parse(String subjectDistinguishedName, RDN relativeDistinguishedName);  }
@Test public void parseSerialNumberAsOID() { String subjectDN = "2.5.4.5=#130b3437313031303130303333,2.5.4.42=#0c05504552454e494d49,2.5.4.4=#0c074545534e494d49,CN=PERENIMI\\,EESNIMI\\,47101010033,OU=digital signature,O=ESTEID,C=EE"; String parsedSerialNumber = SubjectDNParser.parse(subjectDN, SubjectDNParser.RDN.SERIALNUMBER); assertEquals("47101010033", parsedSerialNumber); }
@Test public void parseSerialNumberAsName() { String subjectDN = "SERIALNUMBER=47101010033,SN=PERENIMI,GN=EESNIMI,CN=PERENIMI\\,EESNIMI\\,47101010033,OU=digital signature,O=ESTEID,C=EE"; String parsedSerialNumber = SubjectDNParser.parse(subjectDN, SubjectDNParser.RDN.SERIALNUMBER); assertEquals("47101010033", parsedSerialNumber); }
@Test public void parsedRDNValueQuotesRemovedIfPresent() { String subjectDN = "CN=\"47101010033\",OU=digital signature,O=ESTEID,C=EE"; String parsedSerialNumber = SubjectDNParser.parse(subjectDN, SubjectDNParser.RDN.CN); assertEquals("47101010033", parsedSerialNumber); }
@Test public void searchedSubjectNameNotPresent() { String subjectDN = "SN=PERENIMI,GN=EESNIMI,CN=PERENIMI\\,EESNIMI\\,47101010033,OU=digital signature,O=ESTEID,C=EE"; String parsedSerialNumber = SubjectDNParser.parse(subjectDN, SubjectDNParser.RDN.SERIALNUMBER); assertNull(parsedSerialNumber); }