We are working on a Java application that handles a bunch of X509 certificates. We extract the subject name and the issuer e.g. CN=Dummy CN,C=DE,L=cf-eu10,O=TEST SE,OU=TEST etc.
Now the subjects come in a variety of formats such as:
CN,L,OU,OU,O,C
CN,OU,O,L,C
C,O,OU,OU,L,CN
CN,OU,O,L,C
and I am looking for an option to standardise them to a common format such as: CN,L,OU,OU,O,C
Is there a way to do this without writing custom string tokenising logic? I tried using JDK classes like
new X500Principal(certSubjectName).getName(X500Principal.RFC2253)
but it does not really order the attributes. I am guessing what I am looking for is what the openssl CLI does with this command
openssl x509 -noout -subject -in <cert name> -nameopt rfc2253
Suppose this is the code that I have
String name = "CN=internal:eu10:asd, L=some-svc, OU=abcdefgh, OU=Canary, OU=My Clients, O=Org, C=XX"
System.out.println(new X500Principal(name).getName());
CN=internal:eu10:asd,L=some-svc,OU=abcdefgh,OU=Canary,OU=My Clients,O=Org,C=XX
However if the order of tokens is changed
String name = "C=XX, L=some-svc, CN=internal:eu10:asd, OU=abcdefgh, OU=Canary, OU=My Clients, O=Org";
System.out.println(new X500Principal(name).getName());
C=XX,L=some-svc,CN=internal:eu10:asd,OU=abcdefgh,OU=Canary,OU=My Clients,O=Org
Suppose the tokens are ordered differently
String name = "C=XX, L=some-svc, CN=internal:eu10:asd, OU=abcdefgh, OU=Canary, OU=My Clients, O=Org";
System.out.println(new X500Principal(name).getName());
C=XX,L=some-svc,CN=internal:eu10:asd,OU=abcdefgh,OU=Canary,OU=My Clients,O=Org
Is there a way I can get CN=internal:eu10:asd,L=some-svc,OU=abcdefgh,OU=Canary,OU=My Clients,O=Org,C=XX in the second case as well?
Thanks a lot, Prabal